tsadmetrics 0.1.15__py3-none-any.whl → 0.1.17__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.
tsadmetrics/utils.py CHANGED
@@ -1,7 +1,8 @@
1
1
  import numpy as np
2
2
  import pandas as pd
3
3
  import time
4
-
4
+ import sys
5
+ import tsadmetrics
5
6
  def compute_metrics(y_true: np.array,y_pred: np.array, metrics: list, metrics_params: dict, is_anomaly_score = False, verbose = False):
6
7
  """
7
8
  Computes the specified metrics for the given true and predicted values.
@@ -52,4 +53,72 @@ def compute_metrics(y_true: np.array,y_pred: np.array, metrics: list, metrics_pa
52
53
  metrics_df['metric_name'] = results.keys()
53
54
  metrics_df['metric_value'] = results.values()
54
55
 
55
- return metrics_df
56
+ return metrics_df
57
+
58
+
59
+ def compute_metrics_from_file(results_file: str, conf_file: str, output_dir: str = '.'):
60
+ """
61
+ Computes metrics based on prediction results from a CSV file and configuration from a JSON file.
62
+
63
+ Parameters:
64
+ results_file (str):
65
+ Path to CSV file containing y_true and y_pred columns.
66
+ conf_file (str):
67
+ Path to JSON configuration file with metrics and parameters.
68
+
69
+ Returns:
70
+ pd.DataFrame: DataFrame with computed metrics.
71
+ """
72
+ # Read results data
73
+ res = pd.read_csv(results_file)
74
+ y_true = res['y_true'].values
75
+ y_pred = res['y_pred'].values
76
+
77
+ # Determine if predictions are binary or scores
78
+ is_anomaly_score = False
79
+ unique_values = np.unique(y_pred)
80
+ if not (np.array_equal(unique_values, [0, 1]) or
81
+ np.array_equal(unique_values, [0]) or
82
+ np.array_equal(unique_values, [1])):
83
+ is_anomaly_score = True
84
+ if not np.all((y_pred >= 0) & (y_pred <= 1)):
85
+ raise ValueError("y_pred must be either binary (0/1) or anomaly scores in range [0, 1]")
86
+
87
+ # Read configuration from JSON using pandas
88
+ try:
89
+ config_df = pd.read_json(conf_file, orient='records')
90
+ except ValueError as e:
91
+ raise ValueError(f"Invalid JSON format in configuration file: {str(e)}")
92
+
93
+ # Convert pandas DataFrame to format expected by compute_metrics
94
+ metrics = []
95
+ metrics_params = {}
96
+
97
+ for _, row in config_df.iterrows():
98
+ metric_name = row['name']
99
+ try:
100
+ metric_func = getattr(tsadmetrics, metric_name)
101
+ except AttributeError:
102
+ raise ValueError(f"Metric function '{metric_name}' not found in tsadmetrics module")
103
+
104
+ metrics.append((metric_name, metric_func))
105
+
106
+ # Handle params (convert from pandas Series to dict if needed)
107
+ params = row.get('params', {})
108
+ if pd.notna(params) and params: # Check for non-empty params
109
+ if isinstance(params, pd.Series):
110
+ metrics_params[metric_name] = params.to_dict()
111
+ else:
112
+ metrics_params[metric_name] = params
113
+
114
+ # Compute metrics
115
+ metrics_df = compute_metrics(
116
+ y_true=y_true,
117
+ y_pred=y_pred,
118
+ metrics=metrics,
119
+ metrics_params=metrics_params,
120
+ is_anomaly_score=is_anomaly_score,
121
+ verbose=False
122
+ )
123
+ metrics_df.to_csv(output_dir+'/computed_metrics.csv', index=False)
124
+
@@ -0,0 +1,35 @@
1
+ import numpy as np
2
+
3
+ def check_gt_binary_array(arr):
4
+ if len(arr.shape) != 1:
5
+ raise ValueError("Ground truth input must be a 1D binary array.")
6
+ if not np.all(np.isin(arr, [0, 1])):
7
+ raise ValueError("Ground truth input array must contain only binary values (0 or 1).")
8
+ return True
9
+
10
+ def check_pred_binary_array(arr):
11
+ if len(arr.shape) != 1:
12
+ raise ValueError("Prediction input must be a 1D binary array.")
13
+ if not np.all(np.isin(arr, [0, 1])):
14
+ raise ValueError("Prediction input array must contain only binary values (0 or 1).")
15
+ return True
16
+ def check_same_length(arr1, arr2):
17
+ if len(arr1) != len(arr2):
18
+ raise ValueError("Ground truth and prediction arrays must have the same length.")
19
+ return True
20
+
21
+ def check_pred_continuous_array(arr):
22
+ if len(arr.shape) != 1:
23
+ raise ValueError("Prediction input must be a 1D continuous array.")
24
+ if not np.all((arr >= 0) & (arr <= 1)):
25
+ raise ValueError("All values in the array must be in the range [0, 1].")
26
+ return True
27
+
28
+ def validate_binary_inputs(y_true, y_pred):
29
+ check_gt_binary_array(y_true)
30
+ check_pred_binary_array(y_pred)
31
+ check_same_length(y_true, y_pred)
32
+
33
+ def validate_non_binary_inputs(y_true, y_anomaly_scores):
34
+ check_gt_binary_array(y_true)
35
+ check_same_length(y_true, y_anomaly_scores)
@@ -0,0 +1,54 @@
1
+ Metadata-Version: 2.1
2
+ Name: tsadmetrics
3
+ Version: 0.1.17
4
+ Summary: =?unknown-8bit?q?Librer=C3=ADa_para_evaluaci=C3=B3n_de_detecci=C3=B3n_de_anomal=C3=ADas?= en series temporales
5
+ Home-page: https://github.com/pathsko/TSADmetrics
6
+ Author: Pedro Rafael Velasco Priego
7
+ Author-email: Pedro Rafael Velasco Priego <i12veprp@uco.es>
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: joblib (==1.4.2)
11
+ Requires-Dist: numpy (==1.24.4)
12
+ Requires-Dist: pandas (==2.0.3)
13
+ Requires-Dist: PATE (==0.1.1)
14
+ Requires-Dist: patsy (==0.5.6)
15
+ Requires-Dist: python-dateutil (==2.9.0.post0)
16
+ Requires-Dist: pytz (==2024.1)
17
+ Requires-Dist: scikit-learn (==1.3.2)
18
+ Requires-Dist: scipy (==1.10.1)
19
+ Requires-Dist: six (==1.16.0)
20
+ Requires-Dist: statsmodels (==0.14.1)
21
+ Requires-Dist: threadpoolctl (==3.5.0)
22
+ Requires-Dist: tzdata (==2024.1)
23
+
24
+ # TSADmetrics - Time Series Anomaly Detection Metrics
25
+
26
+ **TSADmetrics** is a Python library for evaluating anomaly detection algorithms in time series data. It provides a comprehensive set of binary and non-binary metrics designed specifically for the challenges of anomaly detection in temporal contexts.
27
+
28
+ ## Features
29
+
30
+ - **Binary Metrics**: Evaluate discrete anomaly predictions (0/1 labels)
31
+
32
+ - **Non-Binary Metrics**: Assess continuous anomaly scores
33
+
34
+ - **Efficient Computation**: Compute multiple metrics at once
35
+
36
+ - **CLI Tool**: Evaluate metrics directly from CSV/JSON files
37
+
38
+ ## Installation
39
+
40
+ Install TSADmetrics via pip:
41
+
42
+ ```bash
43
+ pip install tsadmetrics
44
+ ```
45
+
46
+ ## Documentation
47
+
48
+ The complete documentation for TSADmetrics is available at:
49
+ ���� [https://tsadmetrics.readthedocs.io/](https://tsadmetrics.readthedocs.io/)
50
+
51
+ ## Acknowledgements
52
+
53
+ This library is based on the concepts and implementations from:
54
+ S��rb��, S., & Ruocco, M. (2023). *Navigating the metric maze: a taxonomy of evaluation metrics for anomaly detection in time series*. https://doi.org/10.1007/s10618-023-00988-8
@@ -1,4 +1,6 @@
1
1
  docs/conf.py,sha256=UvAyr0jPk75vQyREMEG3TIs96Pk-hslOgLQUpySp2tw,1645
2
+ docs_api/conf.py,sha256=Ba8bUV-53VLgJ93hOesXr1xzLMpXlATnyZYlmyoM85g,2333
3
+ docs_manual/conf.py,sha256=uUY57MH63QsFLhn_W7LIbwMMQO3rcdUxhCAdzx_d0Z0,2331
2
4
  entorno/bin/activate_this.py,sha256=45dnJsdtOWIt5LtVSBmBfB8E7AlKcnhnZe9e3WGclak,1199
3
5
  entorno/bin/rst2html.py,sha256=h4RydG-iAectsUra0lNFGwB4_1mngxrtPPgQrxUWQ3A,643
4
6
  entorno/bin/rst2html4.py,sha256=Xiv3Zb1gk4jT7DYFVlf5w4LJtI5ZI3pW3b1KLxyPS5A,765
@@ -13,14 +15,16 @@ entorno/bin/rst2xetex.py,sha256=spisB81JgqAmMAkjdTaP8awFQS_Zuob9HIcbMi1kOS8,922
13
15
  entorno/bin/rst2xml.py,sha256=uoIfpn3prnir2tzqdycsAjOg-OWw663XOK47IeHCZdY,651
14
16
  entorno/bin/rstpep2html.py,sha256=sthYQHEgYfj4JqwG45URwVbRAs-HYuwKget7SUwp9fc,719
15
17
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
- tests/test_binary.py,sha256=dj9BsKBo5rpWw4JGiKKoVkg4rIW4YylTie2VxH2DAGo,29787
17
- tests/test_non_binary.py,sha256=syANlwm0DKsL6geGeq6nQI6ZVe6T_YXWTyk2-Hmck4s,11308
18
- tsadmetrics/__init__.py,sha256=MTWOa43fgOdkMNo5NglCReRnB8hoF0ob2PIvDziCNHw,1575
19
- tsadmetrics/binary_metrics.py,sha256=XK1HSkCwg7lIYN0KHrSNDWSFBxlWfubp6U-2fgdCIrU,63587
20
- tsadmetrics/metric_utils.py,sha256=fm8v0X37_AlqWpkcUT9r3680QsjLljrHe2YuXkRLAZ4,10873
21
- tsadmetrics/non_binary_metrics.py,sha256=O6AqceHrjCVV1kJPBzXQIgtiu6afzoiJz2biNsxf3_4,13389
18
+ tests/test_binary.py,sha256=Qt14fP-F-TW6KlPz6X-2DhtFpaHNrODiMA2DI39JrBI,39311
19
+ tests/test_non_binary.py,sha256=jBn3GfBpbK7ktIS9FNIeYi0ob8jkkbMCyCX7-_8LI-k,15755
20
+ tests/test_utils.py,sha256=8Favmlyix1YaAm03XuzMfEjLnq_Ud0YV_6aFwsIMpl8,2192
21
+ tsadmetrics/__init__.py,sha256=BxSqvH0NselzEIZhw4rpnUh6UkzRh0eDpkNvChqWv_8,1604
22
+ tsadmetrics/binary_metrics.py,sha256=PiecIZ2z2B3-uCx1H3KXfLXdSIu8vxY5sUsIb2vmobk,69729
23
+ tsadmetrics/metric_utils.py,sha256=1nuHQp5fc7whPMfJTfWmKb6XmSngoe6p7fdsoP0Vz-I,2876
24
+ tsadmetrics/non_binary_metrics.py,sha256=FsXRMeZIvQsEP5BLT02nW7_yjDnF9wD-h8h-B6eTRVs,14238
22
25
  tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- tsadmetrics/utils.py,sha256=BqsG4DyP3AffuMFQCJ-Qy4YaDu4IkFudZWYCvyTGvvY,2444
26
+ tsadmetrics/utils.py,sha256=mu49QEftRI4HEXvjfiE_CnsujgiNd62U9DRWCEvtO9M,4913
27
+ tsadmetrics/validation.py,sha256=fseGfpGhN-3zAMo2WZLxahcOAsOOyBb2RAFRDKB1KI8,1340
24
28
  tsadmetrics/_tsadeval/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
29
  tsadmetrics/_tsadeval/auc_roc_pr_plot.py,sha256=PHqJUXq2qI248XV9o04D8SsUJgowetaKq0Cu5bYrIAE,12689
26
30
  tsadmetrics/_tsadeval/discontinuity_graph.py,sha256=Ci65l_DPi6HTtb8NvQJe1najgGrRuEpOMWvSyi2AeR0,4088
@@ -53,7 +57,10 @@ tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py,sha256=pJz4iuPyVGNvwsaR
53
57
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py,sha256=jLkcMg7UNl25SHtZUBGkP-RV8HsvaZCtjakryl7PFWU,3204
54
58
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py,sha256=OhUJSm_I7VZ_gX_SSg8AYUq3_NW9rMIy7lAVsnOFw4Q,417
55
59
  tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py,sha256=LL-0pPer3ymovVRlktaHo5XDzpgiDhWOVfdPOzKR6og,3152
56
- tsadmetrics-0.1.15.dist-info/METADATA,sha256=Ks2Kq9EAPqiV8kPwnYUY4Ytzgq4_ig7TwvpcC94Zmjg,831
57
- tsadmetrics-0.1.15.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
58
- tsadmetrics-0.1.15.dist-info/top_level.txt,sha256=s2VIr_ePl-WZbYt9FsYbsDGM7J-Qc5cgpwEOeQ3FVpM,31
59
- tsadmetrics-0.1.15.dist-info/RECORD,,
60
+ tsadmetrics/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
61
+ tsadmetrics/scripts/compute_metrics.py,sha256=PwtH6XmpKEWwdY62pMfZGrgIBTIms0z3qVpw5LjnuwE,991
62
+ tsadmetrics-0.1.17.dist-info/METADATA,sha256=Lcjim8NZYuZ-WRBosqpYy_CISaVXF2SOZ7dI-RjoiVI,1936
63
+ tsadmetrics-0.1.17.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
64
+ tsadmetrics-0.1.17.dist-info/entry_points.txt,sha256=fnsO232FxrQC6pmeZnyZ4UaiXyvN1rKxksLKQO9n7q8,81
65
+ tsadmetrics-0.1.17.dist-info/top_level.txt,sha256=jFJiGEaMZwBStgmkeS19H9fyWDDcPDgmNjinjVrh0jk,52
66
+ tsadmetrics-0.1.17.dist-info/RECORD,,
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ tsadmetrics-compute = tsadmetrics.scripts.compute_metrics:main
@@ -1,4 +1,6 @@
1
1
  docs
2
+ docs_api
3
+ docs_manual
2
4
  entorno
3
5
  tests
4
6
  tsadmetrics
@@ -1,23 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: tsadmetrics
3
- Version: 0.1.15
4
- Summary: =?unknown-8bit?q?Librer=C3=ADa_para_evaluaci=C3=B3n_de_detecci=C3=B3n_de_anomal=C3=ADas?= en series temporales
5
- Home-page: https://github.com/pathsko/TSADmetrics
6
- Author: Pedro Rafael Velasco Priego
7
- Author-email: Pedro Rafael Velasco Priego <i12veprp@uco.es>
8
- Requires-Python: >=3.8
9
- Description-Content-Type: text/markdown
10
- Requires-Dist: joblib (==1.4.2)
11
- Requires-Dist: numpy (==1.24.4)
12
- Requires-Dist: pandas (==2.0.3)
13
- Requires-Dist: PATE (==0.1.1)
14
- Requires-Dist: patsy (==0.5.6)
15
- Requires-Dist: python-dateutil (==2.9.0.post0)
16
- Requires-Dist: pytz (==2024.1)
17
- Requires-Dist: scikit-learn (==1.3.2)
18
- Requires-Dist: scipy (==1.10.1)
19
- Requires-Dist: six (==1.16.0)
20
- Requires-Dist: statsmodels (==0.14.1)
21
- Requires-Dist: threadpoolctl (==3.5.0)
22
- Requires-Dist: tzdata (==2024.1)
23
-