tsadmetrics 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- entorno/bin/activate_this.py +32 -0
 - entorno/bin/rst2html.py +23 -0
 - entorno/bin/rst2html4.py +26 -0
 - entorno/bin/rst2html5.py +33 -0
 - entorno/bin/rst2latex.py +26 -0
 - entorno/bin/rst2man.py +27 -0
 - entorno/bin/rst2odt.py +28 -0
 - entorno/bin/rst2odt_prepstyles.py +20 -0
 - entorno/bin/rst2pseudoxml.py +23 -0
 - entorno/bin/rst2s5.py +24 -0
 - entorno/bin/rst2xetex.py +27 -0
 - entorno/bin/rst2xml.py +23 -0
 - entorno/bin/rstpep2html.py +25 -0
 - tests/__init__.py +0 -0
 - tests/test_binary.py +759 -0
 - tests/test_non_binary.py +371 -0
 - tsadmetrics/_tsadeval/affiliation/__init__.py +0 -0
 - tsadmetrics/_tsadeval/affiliation/_affiliation_zone.py +86 -0
 - tsadmetrics/_tsadeval/affiliation/_integral_interval.py +464 -0
 - tsadmetrics/_tsadeval/affiliation/_single_ground_truth_event.py +68 -0
 - tsadmetrics/_tsadeval/affiliation/generics.py +135 -0
 - tsadmetrics/_tsadeval/affiliation/metrics.py +114 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/File_IO.py +175 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Range.py +50 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Time_Plot.py +184 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/__init__.py +0 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/__init__.py +0 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/etapr.py +386 -0
 - tsadmetrics/_tsadeval/eTaPR_pkg/tapr.py +362 -0
 - tsadmetrics/_tsadeval/prts/__init__.py +0 -0
 - tsadmetrics/_tsadeval/prts/base/__init__.py +0 -0
 - tsadmetrics/_tsadeval/prts/base/time_series_metrics.py +165 -0
 - tsadmetrics/_tsadeval/prts/basic_metrics_ts.py +121 -0
 - tsadmetrics/_tsadeval/prts/time_series_metrics/__init__.py +0 -0
 - tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py +61 -0
 - tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py +86 -0
 - tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py +21 -0
 - tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py +85 -0
 - tsadmetrics/utils.py +10 -4
 - {tsadmetrics-0.1.4.dist-info → tsadmetrics-0.1.6.dist-info}/METADATA +1 -1
 - tsadmetrics-0.1.6.dist-info/RECORD +58 -0
 - tsadmetrics-0.1.6.dist-info/top_level.txt +3 -0
 - tsadmetrics-0.1.4.dist-info/RECORD +0 -20
 - tsadmetrics-0.1.4.dist-info/top_level.txt +0 -1
 - {tsadmetrics-0.1.4.dist-info → tsadmetrics-0.1.6.dist-info}/WHEEL +0 -0
 
    
        tests/test_non_binary.py
    ADDED
    
    | 
         @@ -0,0 +1,371 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            import unittest
         
     | 
| 
      
 2 
     | 
    
         
            +
            from tsadmetrics import *
         
     | 
| 
      
 3 
     | 
    
         
            +
            import numpy as np
         
     | 
| 
      
 4 
     | 
    
         
            +
            import random
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            class TestPrecisionAtK(unittest.TestCase):
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 10 
     | 
    
         
            +
                    """
         
     | 
| 
      
 11 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 12 
     | 
    
         
            +
                    """
         
     | 
| 
      
 13 
     | 
    
         
            +
             
         
     | 
| 
      
 14 
     | 
    
         
            +
                    self.y_true1 =  np.array([0,0,1,1])
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                    self.y_pred1 = np.array([0.2, 0.9, 0.3, 0.8])
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                    self.y_pred2 = np.array([1, 2, 3, 4])
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                    self.y_pred3 = np.array([3, 4, 1, 2])
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                    self.y_true2 =  np.array([1,1,1,0])
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                    self.y_pred4 = np.array([3, 4, 1, 2])
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                def test_precision_at_k_score(self):
         
     | 
| 
      
 31 
     | 
    
         
            +
                    """
         
     | 
| 
      
 32 
     | 
    
         
            +
                    Prueba para la función precision_at_k_score.
         
     | 
| 
      
 33 
     | 
    
         
            +
                    """
         
     | 
| 
      
 34 
     | 
    
         
            +
                    score = round(precision_at_k(self.y_true1, self.y_pred1),2)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    expected_score = 0.5
         
     | 
| 
      
 36 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
                    score = round(precision_at_k(self.y_true1, self.y_pred2),2)
         
     | 
| 
      
 39 
     | 
    
         
            +
                    expected_score = 1
         
     | 
| 
      
 40 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                    score = round(precision_at_k(self.y_true1, self.y_pred3),2)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    expected_score = 0
         
     | 
| 
      
 44 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                    score = round(precision_at_k(self.y_true2, self.y_pred4),2)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    expected_score = round(2/3,2)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 49 
     | 
    
         
            +
                    
         
     | 
| 
      
 50 
     | 
    
         
            +
                def test_precision_at_k_score_consistency(self):
         
     | 
| 
      
 51 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 52 
     | 
    
         
            +
                        for _ in range(100):
         
     | 
| 
      
 53 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 54 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
                            score = precision_at_k(y_true, y_pred)
         
     | 
| 
      
 57 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 58 
     | 
    
         
            +
                        self.fail(f"precision_at_k_score raised an exception {e}")
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
            class TestAUCROCPW(unittest.TestCase):
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 64 
     | 
    
         
            +
                    """
         
     | 
| 
      
 65 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 66 
     | 
    
         
            +
                    """
         
     | 
| 
      
 67 
     | 
    
         
            +
             
         
     | 
| 
      
 68 
     | 
    
         
            +
                    self.y_true1 =  np.array([0,0,1,1])
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                    self.y_pred1 = np.array([1, 3, 2, 4])
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
                    self.y_pred2 = np.array([1, 2, 3, 4])
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                    self.y_pred3 = np.array([4, 4, 4, 4])
         
     | 
| 
      
 76 
     | 
    
         
            +
                
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                def test_auc_roc_pw(self):
         
     | 
| 
      
 79 
     | 
    
         
            +
                    """
         
     | 
| 
      
 80 
     | 
    
         
            +
                    Prueba para la función auc_roc_pw.
         
     | 
| 
      
 81 
     | 
    
         
            +
                    """
         
     | 
| 
      
 82 
     | 
    
         
            +
                    score = round(auc_roc_pw(self.y_true1, self.y_pred1),2)
         
     | 
| 
      
 83 
     | 
    
         
            +
                    expected_score = 0.75
         
     | 
| 
      
 84 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
                    score = round(auc_roc_pw(self.y_true1, self.y_pred2),2)
         
     | 
| 
      
 87 
     | 
    
         
            +
                    expected_score = 1
         
     | 
| 
      
 88 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
                    score = round(auc_roc_pw(self.y_true1, self.y_pred3),2)
         
     | 
| 
      
 91 
     | 
    
         
            +
                    expected_score = 0.5
         
     | 
| 
      
 92 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                    
         
     | 
| 
      
 95 
     | 
    
         
            +
                def test_auc_roc_pw_consistency(self):
         
     | 
| 
      
 96 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 97 
     | 
    
         
            +
                        for _ in range(100):
         
     | 
| 
      
 98 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 99 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 100 
     | 
    
         
            +
             
     | 
| 
      
 101 
     | 
    
         
            +
                            score = auc_roc_pw(y_true, y_pred)
         
     | 
| 
      
 102 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 103 
     | 
    
         
            +
                        self.fail(f"auc_roc_pw raised an exception {e}")
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
            class TestAUCPRPW(unittest.TestCase):
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 108 
     | 
    
         
            +
                    """
         
     | 
| 
      
 109 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 110 
     | 
    
         
            +
                    """
         
     | 
| 
      
 111 
     | 
    
         
            +
             
         
     | 
| 
      
 112 
     | 
    
         
            +
                    self.y_true1 =  np.array([0,0,1,1])
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
             
     | 
| 
      
 115 
     | 
    
         
            +
                    self.y_pred1 = np.array([1, 3, 2, 4])
         
     | 
| 
      
 116 
     | 
    
         
            +
             
     | 
| 
      
 117 
     | 
    
         
            +
                    self.y_pred2 = np.array([1, 2, 3, 4])
         
     | 
| 
      
 118 
     | 
    
         
            +
             
     | 
| 
      
 119 
     | 
    
         
            +
                    self.y_pred3 = np.array([4, 4, 4, 4])
         
     | 
| 
      
 120 
     | 
    
         
            +
                
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
                def test_auc_pr_pw(self):
         
     | 
| 
      
 123 
     | 
    
         
            +
                    """
         
     | 
| 
      
 124 
     | 
    
         
            +
                    Prueba para la función auc_pr_pw.
         
     | 
| 
      
 125 
     | 
    
         
            +
                    """
         
     | 
| 
      
 126 
     | 
    
         
            +
                    score = round(auc_pr_pw(self.y_true1, self.y_pred1),2)
         
     | 
| 
      
 127 
     | 
    
         
            +
                    expected_score = 0.83
         
     | 
| 
      
 128 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
                    score = round(auc_pr_pw(self.y_true1, self.y_pred2),2)
         
     | 
| 
      
 131 
     | 
    
         
            +
                    expected_score = 1
         
     | 
| 
      
 132 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 133 
     | 
    
         
            +
             
     | 
| 
      
 134 
     | 
    
         
            +
                    score = round(auc_pr_pw(self.y_true1, self.y_pred3),2)
         
     | 
| 
      
 135 
     | 
    
         
            +
                    expected_score = 0.5
         
     | 
| 
      
 136 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 137 
     | 
    
         
            +
             
     | 
| 
      
 138 
     | 
    
         
            +
                    
         
     | 
| 
      
 139 
     | 
    
         
            +
                def test_auc_pr_consistency(self):
         
     | 
| 
      
 140 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 141 
     | 
    
         
            +
                        for _ in range(100):
         
     | 
| 
      
 142 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 143 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 144 
     | 
    
         
            +
             
     | 
| 
      
 145 
     | 
    
         
            +
                            score = auc_pr_pw(y_true, y_pred)
         
     | 
| 
      
 146 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 147 
     | 
    
         
            +
                        self.fail(f"auc_pr raised an exception {e}")
         
     | 
| 
      
 148 
     | 
    
         
            +
             
     | 
| 
      
 149 
     | 
    
         
            +
             
     | 
| 
      
 150 
     | 
    
         
            +
                
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
            class TestAUCPRPA(unittest.TestCase):
         
     | 
| 
      
 153 
     | 
    
         
            +
             
     | 
| 
      
 154 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 155 
     | 
    
         
            +
                    """
         
     | 
| 
      
 156 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 157 
     | 
    
         
            +
                    """
         
     | 
| 
      
 158 
     | 
    
         
            +
             
         
     | 
| 
      
 159 
     | 
    
         
            +
                    self.y_true1 =  np.array([0,0,1,1])
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
             
     | 
| 
      
 162 
     | 
    
         
            +
                    self.y_pred1 = np.array([1, 3, 2, 4])
         
     | 
| 
      
 163 
     | 
    
         
            +
             
     | 
| 
      
 164 
     | 
    
         
            +
                    self.y_pred2 = np.array([1, 2, 3, 4])
         
     | 
| 
      
 165 
     | 
    
         
            +
             
     | 
| 
      
 166 
     | 
    
         
            +
                    self.y_pred3 = np.array([4, 4, 4, 4])
         
     | 
| 
      
 167 
     | 
    
         
            +
             
     | 
| 
      
 168 
     | 
    
         
            +
                    self.y_true2 = [0,1,1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1,1,0
         
     | 
| 
      
 169 
     | 
    
         
            +
            ,1,1,1,0,0,1,0,0,1,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,1,1,0,1,1
         
     | 
| 
      
 170 
     | 
    
         
            +
            ,1,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,1,0,0,1,0,0,1,0,1,1]
         
     | 
| 
      
 171 
     | 
    
         
            +
             
     | 
| 
      
 172 
     | 
    
         
            +
             
     | 
| 
      
 173 
     | 
    
         
            +
                    self.y_pred4 = [0.1280475, 0.12059283 ,0.29936968 ,0.85866402 ,0.74071874 ,0.22310849
         
     | 
| 
      
 174 
     | 
    
         
            +
             ,0.11281839 ,0.26133246 ,0.33696106 ,0.01442675 ,0.51962876 ,0.07828833
         
     | 
| 
      
 175 
     | 
    
         
            +
             ,0.45337844 ,0.09444483 ,0.91216588 ,0.18847595 ,0.26828481 ,0.65248919
         
     | 
| 
      
 176 
     | 
    
         
            +
             ,0.46291981 ,0.43730757 ,0.78087553 ,0.45031043 ,0.88661033 ,0.56209352
         
     | 
| 
      
 177 
     | 
    
         
            +
             ,0.45029423 ,0.17638205 ,0.9261279 ,0.58830652 ,0.01602648 ,0.73903379
         
     | 
| 
      
 178 
     | 
    
         
            +
             ,0.61831379 ,0.74779903 ,0.42682106 ,0.82583519 ,0.19709012 ,0.44925962
         
     | 
| 
      
 179 
     | 
    
         
            +
             ,0.62752415 ,0.52458327 ,0.46291768 ,0.33937527 ,0.34868777 ,0.12293847
         
     | 
| 
      
 180 
     | 
    
         
            +
             ,0.84477504 ,0.10225254 ,0.37048167 ,0.04476031 ,0.36680499 ,0.11346155
         
     | 
| 
      
 181 
     | 
    
         
            +
             ,0.10583112 ,0.09493136 ,0.54878736 ,0.68514489 ,0.5940307 ,0.14526962
         
     | 
| 
      
 182 
     | 
    
         
            +
             ,0.69385728 ,0.38888727 ,0.61495304 ,0.06795402 ,0.02894603 ,0.08293609
         
     | 
| 
      
 183 
     | 
    
         
            +
             ,0.22865685 ,0.63531487 ,0.97966126 ,0.31418622 ,0.8943095 ,0.22974177
         
     | 
| 
      
 184 
     | 
    
         
            +
             ,0.94402929 ,0.13140625 ,0.80539267 ,0.40160344 ,0.38151339 ,0.65011626
         
     | 
| 
      
 185 
     | 
    
         
            +
             ,0.71657942 ,0.93297398 ,0.32043329 ,0.54667941 ,0.90645979 ,0.98730183
         
     | 
| 
      
 186 
     | 
    
         
            +
             ,0.82351336 ,0.10404812 ,0.6962921 ,0.72890752 ,0.49700666 ,0.47461103
         
     | 
| 
      
 187 
     | 
    
         
            +
             ,0.59696079 ,0.85876179 ,0.247344 ,0.38187879 ,0.23906861 ,0.5266315
         
     | 
| 
      
 188 
     | 
    
         
            +
             ,0.08171512 ,0.27903375 ,0.61112439 ,0.20784267 ,0.90652453 ,0.87575255
         
     | 
| 
      
 189 
     | 
    
         
            +
             ,0.26972245 ,0.78780138 ,0.37649185 ,0.08467683]
         
     | 
| 
      
 190 
     | 
    
         
            +
             
     | 
| 
      
 191 
     | 
    
         
            +
             
     | 
| 
      
 192 
     | 
    
         
            +
                def test_auc_pr_pa(self):
         
     | 
| 
      
 193 
     | 
    
         
            +
                    """
         
     | 
| 
      
 194 
     | 
    
         
            +
                    Prueba para la función auc_pr_pa.
         
     | 
| 
      
 195 
     | 
    
         
            +
                    """
         
     | 
| 
      
 196 
     | 
    
         
            +
                    score = round(auc_pr_pa(self.y_true1, self.y_pred1),2)
         
     | 
| 
      
 197 
     | 
    
         
            +
                    expected_score = 1.0
         
     | 
| 
      
 198 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 199 
     | 
    
         
            +
             
     | 
| 
      
 200 
     | 
    
         
            +
                    score = round(auc_pr_pa(self.y_true1, self.y_pred2),2)
         
     | 
| 
      
 201 
     | 
    
         
            +
                    expected_score = 1.0
         
     | 
| 
      
 202 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 203 
     | 
    
         
            +
             
     | 
| 
      
 204 
     | 
    
         
            +
                    score = round(auc_pr_pa(self.y_true1, self.y_pred3),2)
         
     | 
| 
      
 205 
     | 
    
         
            +
                    expected_score = 0.5
         
     | 
| 
      
 206 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 207 
     | 
    
         
            +
             
     | 
| 
      
 208 
     | 
    
         
            +
                    if len(self.y_true2) == len(self.y_pred4):
         
     | 
| 
      
 209 
     | 
    
         
            +
                        score = round(auc_pr_pa(self.y_true2, self.y_pred4),2)
         
     | 
| 
      
 210 
     | 
    
         
            +
                        expected_score = 0.78
         
     | 
| 
      
 211 
     | 
    
         
            +
                        self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 212 
     | 
    
         
            +
             
     | 
| 
      
 213 
     | 
    
         
            +
                    
         
     | 
| 
      
 214 
     | 
    
         
            +
                def test_auc_pr_pa_consistency(self):
         
     | 
| 
      
 215 
     | 
    
         
            +
                    y_true, y_pred = [],[]
         
     | 
| 
      
 216 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 217 
     | 
    
         
            +
                        for _ in range(100):
         
     | 
| 
      
 218 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 219 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 220 
     | 
    
         
            +
                            score = auc_pr_pa(y_true, y_pred)
         
     | 
| 
      
 221 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 222 
     | 
    
         
            +
                        self.fail(f"auc_roc_pr_pa raised an exception {e}")
         
     | 
| 
      
 223 
     | 
    
         
            +
                        
         
     | 
| 
      
 224 
     | 
    
         
            +
             
     | 
| 
      
 225 
     | 
    
         
            +
            class TestAUCPRSW(unittest.TestCase):
         
     | 
| 
      
 226 
     | 
    
         
            +
             
     | 
| 
      
 227 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 228 
     | 
    
         
            +
                    """
         
     | 
| 
      
 229 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 230 
     | 
    
         
            +
                    """
         
     | 
| 
      
 231 
     | 
    
         
            +
             
         
     | 
| 
      
 232 
     | 
    
         
            +
                    self.y_true1 =  np.array([0,0,1,1])
         
     | 
| 
      
 233 
     | 
    
         
            +
             
     | 
| 
      
 234 
     | 
    
         
            +
             
     | 
| 
      
 235 
     | 
    
         
            +
                    self.y_pred1 = np.array([1, 3, 2, 4])
         
     | 
| 
      
 236 
     | 
    
         
            +
             
     | 
| 
      
 237 
     | 
    
         
            +
                    self.y_pred2 = np.array([1, 2, 3, 4])
         
     | 
| 
      
 238 
     | 
    
         
            +
             
     | 
| 
      
 239 
     | 
    
         
            +
                    self.y_pred3 = np.array([4, 4, 4, 4])
         
     | 
| 
      
 240 
     | 
    
         
            +
                
         
     | 
| 
      
 241 
     | 
    
         
            +
             
     | 
| 
      
 242 
     | 
    
         
            +
                def test_auc_pr_sw(self):
         
     | 
| 
      
 243 
     | 
    
         
            +
                    """
         
     | 
| 
      
 244 
     | 
    
         
            +
                    Prueba para la función auc_pr_sw.
         
     | 
| 
      
 245 
     | 
    
         
            +
                    """
         
     | 
| 
      
 246 
     | 
    
         
            +
                    score = round(auc_pr_sw(self.y_true1, self.y_pred1),2)
         
     | 
| 
      
 247 
     | 
    
         
            +
                    expected_score = 1.0
         
     | 
| 
      
 248 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 249 
     | 
    
         
            +
             
     | 
| 
      
 250 
     | 
    
         
            +
                    score = round(auc_pr_sw(self.y_true1, self.y_pred2),2)
         
     | 
| 
      
 251 
     | 
    
         
            +
                    expected_score = 1
         
     | 
| 
      
 252 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 253 
     | 
    
         
            +
             
     | 
| 
      
 254 
     | 
    
         
            +
                    score = round(auc_pr_sw(self.y_true1, self.y_pred3),2)
         
     | 
| 
      
 255 
     | 
    
         
            +
                    expected_score = 0.5
         
     | 
| 
      
 256 
     | 
    
         
            +
                    self.assertAlmostEqual(score, expected_score, places=4)
         
     | 
| 
      
 257 
     | 
    
         
            +
             
     | 
| 
      
 258 
     | 
    
         
            +
                    
         
     | 
| 
      
 259 
     | 
    
         
            +
                # def test_auc_pr_sw_consistency(self):
         
     | 
| 
      
 260 
     | 
    
         
            +
                #     try:
         
     | 
| 
      
 261 
     | 
    
         
            +
                #         for _ in range(100):
         
     | 
| 
      
 262 
     | 
    
         
            +
                #             y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 263 
     | 
    
         
            +
                #             y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 264 
     | 
    
         
            +
             
     | 
| 
      
 265 
     | 
    
         
            +
                #             score = auc_pr_sw(y_true, y_pred)
         
     | 
| 
      
 266 
     | 
    
         
            +
                #     except Exception as e:
         
     | 
| 
      
 267 
     | 
    
         
            +
                #         self.fail(f"auc_pr_sw raised an exception {e}")
         
     | 
| 
      
 268 
     | 
    
         
            +
             
     | 
| 
      
 269 
     | 
    
         
            +
             
     | 
| 
      
 270 
     | 
    
         
            +
            class TestVUSROC(unittest.TestCase):
         
     | 
| 
      
 271 
     | 
    
         
            +
             
     | 
| 
      
 272 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 273 
     | 
    
         
            +
                    """
         
     | 
| 
      
 274 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 275 
     | 
    
         
            +
                    """
         
     | 
| 
      
 276 
     | 
    
         
            +
             
         
     | 
| 
      
 277 
     | 
    
         
            +
                    self.y_true1 =  np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
         
     | 
| 
      
 278 
     | 
    
         
            +
                    self.y_true2 =  np.array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0])
         
     | 
| 
      
 279 
     | 
    
         
            +
             
     | 
| 
      
 280 
     | 
    
         
            +
                    self.y_pred1 = np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
         
     | 
| 
      
 281 
     | 
    
         
            +
             
     | 
| 
      
 282 
     | 
    
         
            +
                    self.y_pred2 = np.array([8, 0, 9, 1, 7, 2, 3, 4, 5, 6])
         
     | 
| 
      
 283 
     | 
    
         
            +
             
     | 
| 
      
 284 
     | 
    
         
            +
             
     | 
| 
      
 285 
     | 
    
         
            +
                
         
     | 
| 
      
 286 
     | 
    
         
            +
             
     | 
| 
      
 287 
     | 
    
         
            +
                def test_vus_roc(self):
         
     | 
| 
      
 288 
     | 
    
         
            +
                    """
         
     | 
| 
      
 289 
     | 
    
         
            +
                    Prueba para la función vus_roc.
         
     | 
| 
      
 290 
     | 
    
         
            +
                    """
         
     | 
| 
      
 291 
     | 
    
         
            +
                    score = round(vus_roc(self.y_true1, self.y_pred1,window=4),2)
         
     | 
| 
      
 292 
     | 
    
         
            +
                    self.assertTrue(score <= 0.1)
         
     | 
| 
      
 293 
     | 
    
         
            +
             
     | 
| 
      
 294 
     | 
    
         
            +
                    score = round(vus_roc(self.y_true2, self.y_pred2,window=4),2)
         
     | 
| 
      
 295 
     | 
    
         
            +
                    self.assertTrue(score > 0.4)
         
     | 
| 
      
 296 
     | 
    
         
            +
             
     | 
| 
      
 297 
     | 
    
         
            +
                    score = vus_roc(self.y_true2, self.y_pred2,window=0)
         
     | 
| 
      
 298 
     | 
    
         
            +
                    self.assertTrue(score < 0.4)
         
     | 
| 
      
 299 
     | 
    
         
            +
             
     | 
| 
      
 300 
     | 
    
         
            +
                    
         
     | 
| 
      
 301 
     | 
    
         
            +
                def test_vus_roc_consistency(self):
         
     | 
| 
      
 302 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 303 
     | 
    
         
            +
                        for _ in range(10):
         
     | 
| 
      
 304 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 305 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 306 
     | 
    
         
            +
                            score = vus_roc(y_true, y_pred, window=4)
         
     | 
| 
      
 307 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 308 
     | 
    
         
            +
                        self.fail(f"auc_roc raised an exception {e}")
         
     | 
| 
      
 309 
     | 
    
         
            +
             
     | 
| 
      
 310 
     | 
    
         
            +
             
     | 
| 
      
 311 
     | 
    
         
            +
            class TestVUSPR(unittest.TestCase):
         
     | 
| 
      
 312 
     | 
    
         
            +
             
     | 
| 
      
 313 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 314 
     | 
    
         
            +
                    """
         
     | 
| 
      
 315 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 316 
     | 
    
         
            +
                    """
         
     | 
| 
      
 317 
     | 
    
         
            +
             
         
     | 
| 
      
 318 
     | 
    
         
            +
                    self.y_true1 =  np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
         
     | 
| 
      
 319 
     | 
    
         
            +
                    self.y_true2 =  np.array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0])
         
     | 
| 
      
 320 
     | 
    
         
            +
             
     | 
| 
      
 321 
     | 
    
         
            +
                    self.y_pred1 = np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
         
     | 
| 
      
 322 
     | 
    
         
            +
             
     | 
| 
      
 323 
     | 
    
         
            +
                    self.y_pred2 = np.array([8, 0, 9, 1, 7, 2, 3, 4, 5, 6])
         
     | 
| 
      
 324 
     | 
    
         
            +
             
     | 
| 
      
 325 
     | 
    
         
            +
             
     | 
| 
      
 326 
     | 
    
         
            +
                
         
     | 
| 
      
 327 
     | 
    
         
            +
             
     | 
| 
      
 328 
     | 
    
         
            +
                def test_vus_pr(self):
         
     | 
| 
      
 329 
     | 
    
         
            +
                    """
         
     | 
| 
      
 330 
     | 
    
         
            +
                    Prueba para la función vus_pr.
         
     | 
| 
      
 331 
     | 
    
         
            +
                    """
         
     | 
| 
      
 332 
     | 
    
         
            +
                    score = round(vus_pr(self.y_true1, self.y_pred1,window=4),2)
         
     | 
| 
      
 333 
     | 
    
         
            +
                    self.assertTrue(score <= 0.2)
         
     | 
| 
      
 334 
     | 
    
         
            +
             
     | 
| 
      
 335 
     | 
    
         
            +
                    score = round(vus_pr(self.y_true2, self.y_pred2,window=4),2)
         
     | 
| 
      
 336 
     | 
    
         
            +
                    self.assertTrue(score > 0.5)
         
     | 
| 
      
 337 
     | 
    
         
            +
             
     | 
| 
      
 338 
     | 
    
         
            +
                    score = vus_pr(self.y_true2, self.y_pred2,window=0)
         
     | 
| 
      
 339 
     | 
    
         
            +
                    self.assertTrue(score < 0.5)
         
     | 
| 
      
 340 
     | 
    
         
            +
             
     | 
| 
      
 341 
     | 
    
         
            +
                    
         
     | 
| 
      
 342 
     | 
    
         
            +
                def test_vus_pr_consistency(self):
         
     | 
| 
      
 343 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 344 
     | 
    
         
            +
                        for _ in range(10):
         
     | 
| 
      
 345 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 346 
     | 
    
         
            +
                            y_pred = np.random.random( size=(100,))
         
     | 
| 
      
 347 
     | 
    
         
            +
                            score = vus_pr(y_true, y_pred, window=4)
         
     | 
| 
      
 348 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 349 
     | 
    
         
            +
                        self.fail(f"auc_roc raised an exception {e}")
         
     | 
| 
      
 350 
     | 
    
         
            +
             
     | 
| 
      
 351 
     | 
    
         
            +
             
     | 
| 
      
 352 
     | 
    
         
            +
            class TestNonBinaryPATE(unittest.TestCase):
         
     | 
| 
      
 353 
     | 
    
         
            +
             
     | 
| 
      
 354 
     | 
    
         
            +
                def setUp(self):
         
     | 
| 
      
 355 
     | 
    
         
            +
                    """
         
     | 
| 
      
 356 
     | 
    
         
            +
                    Configuración inicial para las pruebas.
         
     | 
| 
      
 357 
     | 
    
         
            +
                    """
         
     | 
| 
      
 358 
     | 
    
         
            +
                    pass
         
     | 
| 
      
 359 
     | 
    
         
            +
             
     | 
| 
      
 360 
     | 
    
         
            +
                
         
     | 
| 
      
 361 
     | 
    
         
            +
             
     | 
| 
      
 362 
     | 
    
         
            +
                    
         
     | 
| 
      
 363 
     | 
    
         
            +
                def test_pate_consistency(self):
         
     | 
| 
      
 364 
     | 
    
         
            +
                    try:
         
     | 
| 
      
 365 
     | 
    
         
            +
                        for _ in range(10):
         
     | 
| 
      
 366 
     | 
    
         
            +
                            y_true = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 367 
     | 
    
         
            +
                            y_pred = np.random.choice([0, 1], size=(100,))
         
     | 
| 
      
 368 
     | 
    
         
            +
             
     | 
| 
      
 369 
     | 
    
         
            +
                            score = real_pate(y_true, y_pred, early=5, delay=5)
         
     | 
| 
      
 370 
     | 
    
         
            +
                    except Exception as e:
         
     | 
| 
      
 371 
     | 
    
         
            +
                        self.fail(f"real_pate raised an exception {e}")
         
     | 
| 
         
            File without changes
         
     | 
| 
         @@ -0,0 +1,86 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env python3
         
     | 
| 
      
 2 
     | 
    
         
            +
            # -*- coding: utf-8 -*-
         
     | 
| 
      
 3 
     | 
    
         
            +
            from ._integral_interval import interval_intersection
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            def t_start(j, Js = [(1,2),(3,4),(5,6)], Trange = (1,10)):
         
     | 
| 
      
 6 
     | 
    
         
            +
                """
         
     | 
| 
      
 7 
     | 
    
         
            +
                Helper for `E_gt_func`
         
     | 
| 
      
 8 
     | 
    
         
            +
                
         
     | 
| 
      
 9 
     | 
    
         
            +
                :param j: index from 0 to len(Js) (included) on which to get the start
         
     | 
| 
      
 10 
     | 
    
         
            +
                :param Js: ground truth events, as a list of couples
         
     | 
| 
      
 11 
     | 
    
         
            +
                :param Trange: range of the series where Js is included
         
     | 
| 
      
 12 
     | 
    
         
            +
                :return: generalized start such that the middle of t_start and t_stop 
         
     | 
| 
      
 13 
     | 
    
         
            +
                always gives the affiliation zone
         
     | 
| 
      
 14 
     | 
    
         
            +
                """
         
     | 
| 
      
 15 
     | 
    
         
            +
                b = max(Trange)
         
     | 
| 
      
 16 
     | 
    
         
            +
                n = len(Js)
         
     | 
| 
      
 17 
     | 
    
         
            +
                if j == n:
         
     | 
| 
      
 18 
     | 
    
         
            +
                    return(2*b - t_stop(n-1, Js, Trange))
         
     | 
| 
      
 19 
     | 
    
         
            +
                else:
         
     | 
| 
      
 20 
     | 
    
         
            +
                    return(Js[j][0])
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            def t_stop(j, Js = [(1,2),(3,4),(5,6)], Trange = (1,10)):
         
     | 
| 
      
 23 
     | 
    
         
            +
                """
         
     | 
| 
      
 24 
     | 
    
         
            +
                Helper for `E_gt_func`
         
     | 
| 
      
 25 
     | 
    
         
            +
                
         
     | 
| 
      
 26 
     | 
    
         
            +
                :param j: index from 0 to len(Js) (included) on which to get the stop
         
     | 
| 
      
 27 
     | 
    
         
            +
                :param Js: ground truth events, as a list of couples
         
     | 
| 
      
 28 
     | 
    
         
            +
                :param Trange: range of the series where Js is included
         
     | 
| 
      
 29 
     | 
    
         
            +
                :return: generalized stop such that the middle of t_start and t_stop 
         
     | 
| 
      
 30 
     | 
    
         
            +
                always gives the affiliation zone
         
     | 
| 
      
 31 
     | 
    
         
            +
                """
         
     | 
| 
      
 32 
     | 
    
         
            +
                if j == -1:
         
     | 
| 
      
 33 
     | 
    
         
            +
                    a = min(Trange)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    return(2*a - t_start(0, Js, Trange))
         
     | 
| 
      
 35 
     | 
    
         
            +
                else:
         
     | 
| 
      
 36 
     | 
    
         
            +
                    return(Js[j][1])
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
            def E_gt_func(j, Js, Trange):
         
     | 
| 
      
 39 
     | 
    
         
            +
                """
         
     | 
| 
      
 40 
     | 
    
         
            +
                Get the affiliation zone of element j of the ground truth
         
     | 
| 
      
 41 
     | 
    
         
            +
                
         
     | 
| 
      
 42 
     | 
    
         
            +
                :param j: index from 0 to len(Js) (excluded) on which to get the zone
         
     | 
| 
      
 43 
     | 
    
         
            +
                :param Js: ground truth events, as a list of couples
         
     | 
| 
      
 44 
     | 
    
         
            +
                :param Trange: range of the series where Js is included, can 
         
     | 
| 
      
 45 
     | 
    
         
            +
                be (-math.inf, math.inf) for distance measures
         
     | 
| 
      
 46 
     | 
    
         
            +
                :return: affiliation zone of element j of the ground truth represented
         
     | 
| 
      
 47 
     | 
    
         
            +
                as a couple
         
     | 
| 
      
 48 
     | 
    
         
            +
                """
         
     | 
| 
      
 49 
     | 
    
         
            +
                range_left = (t_stop(j-1, Js, Trange) + t_start(j, Js, Trange))/2
         
     | 
| 
      
 50 
     | 
    
         
            +
                range_right = (t_stop(j, Js, Trange) + t_start(j+1, Js, Trange))/2
         
     | 
| 
      
 51 
     | 
    
         
            +
                return((range_left, range_right))
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
            def get_all_E_gt_func(Js, Trange):
         
     | 
| 
      
 54 
     | 
    
         
            +
                """
         
     | 
| 
      
 55 
     | 
    
         
            +
                Get the affiliation partition from the ground truth point of view
         
     | 
| 
      
 56 
     | 
    
         
            +
                
         
     | 
| 
      
 57 
     | 
    
         
            +
                :param Js: ground truth events, as a list of couples
         
     | 
| 
      
 58 
     | 
    
         
            +
                :param Trange: range of the series where Js is included, can 
         
     | 
| 
      
 59 
     | 
    
         
            +
                be (-math.inf, math.inf) for distance measures
         
     | 
| 
      
 60 
     | 
    
         
            +
                :return: affiliation partition of the events
         
     | 
| 
      
 61 
     | 
    
         
            +
                """
         
     | 
| 
      
 62 
     | 
    
         
            +
                # E_gt is the limit of affiliation/attraction for each ground truth event
         
     | 
| 
      
 63 
     | 
    
         
            +
                E_gt = [E_gt_func(j, Js, Trange) for j in range(len(Js))]
         
     | 
| 
      
 64 
     | 
    
         
            +
                return(E_gt)
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
            def affiliation_partition(Is = [(1,1.5),(2,5),(5,6),(8,9)], E_gt = [(1,2.5),(2.5,4.5),(4.5,10)]):
         
     | 
| 
      
 67 
     | 
    
         
            +
                """
         
     | 
| 
      
 68 
     | 
    
         
            +
                Cut the events into the affiliation zones
         
     | 
| 
      
 69 
     | 
    
         
            +
                The presentation given here is from the ground truth point of view,
         
     | 
| 
      
 70 
     | 
    
         
            +
                but it is also used in the reversed direction in the main function.
         
     | 
| 
      
 71 
     | 
    
         
            +
                
         
     | 
| 
      
 72 
     | 
    
         
            +
                :param Is: events as a list of couples
         
     | 
| 
      
 73 
     | 
    
         
            +
                :param E_gt: range of the affiliation zones
         
     | 
| 
      
 74 
     | 
    
         
            +
                :return: a list of list of intervals (each interval represented by either 
         
     | 
| 
      
 75 
     | 
    
         
            +
                a couple or None for empty interval). The outer list is indexed by each
         
     | 
| 
      
 76 
     | 
    
         
            +
                affiliation zone of `E_gt`. The inner list is indexed by the events of `Is`.
         
     | 
| 
      
 77 
     | 
    
         
            +
                """
         
     | 
| 
      
 78 
     | 
    
         
            +
                out = [None] * len(E_gt)
         
     | 
| 
      
 79 
     | 
    
         
            +
                for j in range(len(E_gt)):
         
     | 
| 
      
 80 
     | 
    
         
            +
                    E_gt_j = E_gt[j]
         
     | 
| 
      
 81 
     | 
    
         
            +
                    discarded_idx_before = [I[1] < E_gt_j[0] for I in Is]  # end point of predicted I is before the begin of E
         
     | 
| 
      
 82 
     | 
    
         
            +
                    discarded_idx_after = [I[0] > E_gt_j[1] for I in Is] # start of predicted I is after the end of E
         
     | 
| 
      
 83 
     | 
    
         
            +
                    kept_index = [not(a or b) for a, b in zip(discarded_idx_before, discarded_idx_after)]
         
     | 
| 
      
 84 
     | 
    
         
            +
                    Is_j = [x for x, y in zip(Is, kept_index)]
         
     | 
| 
      
 85 
     | 
    
         
            +
                    out[j] = [interval_intersection(I, E_gt[j]) for I in Is_j]
         
     | 
| 
      
 86 
     | 
    
         
            +
                return(out)
         
     |