pthelma 1.0.0__cp311-cp311-win_amd64.whl → 1.1.0__cp311-cp311-win_amd64.whl

Sign up to get free protection for your applications and to get access to all the features.
haggregate/__init__.py CHANGED
@@ -1,5 +1,2 @@
1
1
  from .haggregate import * # NOQA
2
2
  from .regularize import * # NOQA
3
-
4
- __author__ = """Antonis Christofides"""
5
- __email__ = "antonis@antonischristofides.com"
Binary file
pthelma/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '1.0.0'
16
- __version_tuple__ = version_tuple = (1, 0, 0)
15
+ __version__ = version = '1.1.0'
16
+ __version_tuple__ = version_tuple = (1, 1, 0)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pthelma
3
- Version: 1.0.0
3
+ Version: 1.1.0
4
4
  Summary: Utilities for hydrological and meteorological time series processing
5
5
  Author-email: Antonis Christofides <antonis@antonischristofides.com>
6
6
  Maintainer-email: Antonis Christofides <antonis@antonischristofides.com>
@@ -34,9 +34,9 @@ Requires-Dist: gdal<4,>=1.10; extra == "all"
34
34
  pthelma
35
35
  =======
36
36
 
37
- .. image:: https://github.com/openmeteo/pthelma/actions/workflows/run-tests-automatically.yml/badge.svg
37
+ .. image:: https://github.com/openmeteo/pthelma/actions/workflows/run-tests.yml/badge.svg
38
38
  :alt: Build button
39
- :target: https://github.com/openmeteo/pthelma/actions/workflows/run-tests-automatically.yml
39
+ :target: https://github.com/openmeteo/pthelma/actions/workflows/run-tests.yml
40
40
 
41
41
  .. image:: https://codecov.io/github/openmeteo/pthelma/coverage.svg?branch=master
42
42
  :alt: Coverage
@@ -45,10 +45,6 @@ pthelma
45
45
  .. image:: https://img.shields.io/pypi/v/pthelma.svg
46
46
  :target: https://pypi.python.org/pypi/pthelma
47
47
 
48
- .. image:: https://pyup.io/repos/github/openmeteo/pthelma/shield.svg
49
- :target: https://pyup.io/repos/github/openmeteo/pthelma/
50
- :alt: Updates
51
-
52
48
  A Python library with utilities for hydrological and meteorological time
53
49
  series processing.
54
50
 
@@ -5,10 +5,10 @@ enhydris_cache/enhydris_cache.py,sha256=Mvpk2JxF6hSgk0qmGWEVFjIAoehFbdK41LyBJjAY
5
5
  evaporation/__init__.py,sha256=ekZNT_-GEt1lDmwbX_RIaOvHSRjX05iVWeupaxZV75Y,126
6
6
  evaporation/cli.py,sha256=4HdusE0Trt81HsNlClvlrAu-493GtGa6bn_KuV4opzs,28442
7
7
  evaporation/evaporation.py,sha256=twTkFsuI5QRcof1XbnOG5vhnEMgrA2zXYHxYleKV5-k,16702
8
- haggregate/__init__.py,sha256=rqRWh8aJ44DEnAnxfwOWfqFKMZGkvewH81SH5RcSGGM,160
8
+ haggregate/__init__.py,sha256=KfFmAI6bvEsnaflhq9pK1nzOdoyySFiXnw9o1-Nmcus,70
9
9
  haggregate/cli.py,sha256=Ou2zQRnj9ZsNM6it7HFGLavrdGaExZDartlPGPuaNLc,3363
10
10
  haggregate/haggregate.py,sha256=pXzT2z_LshxFD9USiV1je-swAj8n-iY7fetxVO8XJ-A,5200
11
- haggregate/regularize.cp311-win_amd64.pyd,sha256=UX0_-HwpktLrMD_ONjIm_orEh0TkBKZo1vGw8Z_n9NY,102912
11
+ haggregate/regularize.cp311-win_amd64.pyd,sha256=iG3hesVikAHnnSBXpDYehKaXYOdNfigCHk3iu2kNAsI,102912
12
12
  haggregate/regularize.pyx,sha256=aaxx-3wJtuo51G9n7GIzFb2qt6m7rXuoAES9zoI7Zcw,5800
13
13
  hspatial/__init__.py,sha256=dlTLFy7IqQwun4nz01qtYMQbU9kBmHEKoOXGIgCRf9c,123
14
14
  hspatial/cli.py,sha256=alkp-IxwMBErwL2sjZmSLWeRG3DXr0c4A4GV0O0Fk0o,10686
@@ -18,10 +18,13 @@ htimeseries/__init__.py,sha256=by-XbIt4Q-lHHKlHEcgXLi7AjEGNv0VGNmM0ZMl8L7M,75
18
18
  htimeseries/htimeseries.py,sha256=y8rIFabSCdBwmS3vWGEuyRMtQvIZy81jqg304-kIg3w,19771
19
19
  htimeseries/timezone_utils.py,sha256=jTWhKljHXQuD_drNgfI2ytD6atf6dwnsL4qCVK8wpgA,1282
20
20
  pthelma/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
- pthelma/_version.py,sha256=dJiaCltLFgci5aFfMnwDnv8BAhveHDCtWSHatUxAG1Q,427
22
- pthelma-1.0.0.dist-info/LICENSE.rst,sha256=v1ndaPoHsiO-q3mq-ibN0HooeO-LCZBvKPfwlJRIMWs,1379
23
- pthelma-1.0.0.dist-info/METADATA,sha256=gBNt0yEQnHNL-PA_mwPtpStaW7yHwk677_IJVF4fGdQ,2209
24
- pthelma-1.0.0.dist-info/WHEEL,sha256=nkBcd8Ko0v5sEcSagm2-x_RVrb8gBSkTa8VFFZ0Mr1o,101
25
- pthelma-1.0.0.dist-info/entry_points.txt,sha256=Kh2gKKRhObmmOASJWIwX9dDEZQ5htc3ftBlqeoGer-s,155
26
- pthelma-1.0.0.dist-info/top_level.txt,sha256=tmHh9NPLZEWJBUT19b-ZqJmAOG6QAfyM_Z3d-B7XS7w,87
27
- pthelma-1.0.0.dist-info/RECORD,,
21
+ pthelma/_version.py,sha256=mLrG-mC5i2vs5IEM3ZgT5S9ZdaqM1wQHESPk1-Rj79o,427
22
+ rocc/__init__.py,sha256=KfFB3rfJXgYL4HnVa7MNYcotG3am-CajVvh65gm9v_s,281
23
+ rocc/calculation.cp311-win_amd64.pyd,sha256=sIa_Qb0c9Kq_UYD21rPjN7n31DC5yzphei1R3cJeMrY,98304
24
+ rocc/calculation.pyx,sha256=tWWSB1u2JTCnYv6mb_YwK97vPrL7JT_ymx_5TlmG_9Y,5938
25
+ pthelma-1.1.0.dist-info/LICENSE.rst,sha256=v1ndaPoHsiO-q3mq-ibN0HooeO-LCZBvKPfwlJRIMWs,1379
26
+ pthelma-1.1.0.dist-info/METADATA,sha256=JEQWBdyBL7HdQQV2Psn7F78mZJi2SWnuaxEkgTSRb8o,2018
27
+ pthelma-1.1.0.dist-info/WHEEL,sha256=nkBcd8Ko0v5sEcSagm2-x_RVrb8gBSkTa8VFFZ0Mr1o,101
28
+ pthelma-1.1.0.dist-info/entry_points.txt,sha256=Kh2gKKRhObmmOASJWIwX9dDEZQ5htc3ftBlqeoGer-s,155
29
+ pthelma-1.1.0.dist-info/top_level.txt,sha256=kWL0AYuVd_5KzKWRBkAG576ag46gKbojnWdeH_c-bzo,92
30
+ pthelma-1.1.0.dist-info/RECORD,,
@@ -5,3 +5,4 @@ haggregate
5
5
  hspatial
6
6
  htimeseries
7
7
  pthelma
8
+ rocc
rocc/__init__.py ADDED
@@ -0,0 +1,9 @@
1
+ from collections import namedtuple
2
+
3
+ from .calculation import Rocc
4
+
5
+ Threshold = namedtuple("Threshold", ["delta_t", "allowed_diff"])
6
+
7
+
8
+ def rocc(*, timeseries, thresholds, symmetric=False, flag="TEMPORAL"):
9
+ return Rocc(timeseries, thresholds, symmetric, flag).execute()
Binary file
rocc/calculation.pyx ADDED
@@ -0,0 +1,182 @@
1
+ # cython: language_level=3
2
+ import datetime as dt
3
+
4
+ cimport numpy as np
5
+ from cpython cimport array
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+
10
+
11
+ class Rocc:
12
+ def __init__(self, timeseries, thresholds, symmetric, flag):
13
+ self.htimeseries = timeseries
14
+ self.thresholds = thresholds
15
+ self.symmetric = symmetric
16
+ self.flag = flag or ""
17
+
18
+ def execute(self):
19
+ self._transform_thresholds()
20
+ self._transform_to_plain_numpy()
21
+ failures = self._do_actual_job()
22
+ self._transform_to_pandas()
23
+ return failures
24
+
25
+ def _transform_thresholds(self):
26
+ threshold_deltas = array.array("l")
27
+ threshold_allowed_diffs = array.array("d")
28
+
29
+ for threshold in self.thresholds:
30
+ delta_t = int(self._get_delta_t_transformed(threshold.delta_t))
31
+ threshold_deltas.append(delta_t)
32
+ threshold_allowed_diffs.append(threshold.allowed_diff)
33
+ self.threshold_deltas = threshold_deltas
34
+ self.threshold_allowed_diffs = threshold_allowed_diffs
35
+
36
+ def _get_delta_t_transformed(self, delta_t):
37
+ if not delta_t[0].isdigit():
38
+ delta_t = "1" + delta_t
39
+ return pd.Timedelta(delta_t).to_timedelta64()
40
+
41
+ def _transform_to_plain_numpy(self):
42
+ flag_lengths = self.htimeseries.data["flags"].str.len()
43
+ max_flag_length = 0 if flag_lengths.empty else max(flag_lengths)
44
+ flags_dtype = "U" + str(max_flag_length + 1 + len(self.flag))
45
+ self.ts_index = self.htimeseries.data.index.values.astype(long)
46
+ self.ts_values = self.htimeseries.data["value"].values
47
+ self.ts_flags = self.htimeseries.data["flags"].values.astype(flags_dtype)
48
+ try:
49
+ utc_offset = self.htimeseries.data.index.tz.utcoffset(dt.datetime.now())
50
+ except AttributeError:
51
+ utc_offset = dt.timedelta(0)
52
+ self.ts_utc_offset_minutes = int(utc_offset.total_seconds() / 60)
53
+
54
+ def _do_actual_job(self):
55
+ return _perform_rocc(
56
+ self.ts_index,
57
+ self.ts_values,
58
+ self.ts_flags,
59
+ self.ts_utc_offset_minutes,
60
+ list(self.thresholds),
61
+ self.threshold_deltas,
62
+ self.threshold_allowed_diffs,
63
+ self.symmetric,
64
+ self.flag,
65
+ )
66
+
67
+ def _transform_to_pandas(self):
68
+ self.htimeseries.data = pd.DataFrame(
69
+ index=self.htimeseries.data.index,
70
+ columns=["value", "flags"],
71
+ data=np.vstack((self.ts_values, self.ts_flags)).transpose(),
72
+ )
73
+ self.htimeseries.data["value"] = self.htimeseries.data["value"].astype(np.float64)
74
+
75
+
76
+ # IMPORTANT: There's some plain Python in the Cython below. Specifically, there are some
77
+ # Python lists and some places with undeclared variables. These are only used when a
78
+ # failure is found. Given that failures should be very few, this should not affect the
79
+ # overall speed. But I'm not really a Cython expert and I don't know exactly how it
80
+ # works.
81
+
82
+
83
+ def _perform_rocc(
84
+ np.ndarray ts_index,
85
+ np.ndarray ts_values,
86
+ np.ndarray ts_flags,
87
+ int ts_utc_offset_minutes,
88
+ list thresholds,
89
+ array.array threshold_deltas,
90
+ array.array threshold_allowed_diffs,
91
+ int symmetric,
92
+ str flag,
93
+ ):
94
+ cdef int i, record_fails_check
95
+ cdef list failures = []
96
+
97
+ for i in range(ts_index.size):
98
+ record_fails_check = _record_fails_check(
99
+ i,
100
+ ts_index,
101
+ ts_values,
102
+ ts_utc_offset_minutes,
103
+ thresholds,
104
+ threshold_deltas,
105
+ threshold_allowed_diffs,
106
+ symmetric,
107
+ failures,
108
+ )
109
+ if record_fails_check and flag:
110
+ _add_flag(i, ts_flags, flag)
111
+ return failures
112
+
113
+
114
+ def _add_flag(int i, np.ndarray ts_flags, str flag):
115
+ if ts_flags[i]:
116
+ ts_flags[i] = ts_flags[i] + " "
117
+ ts_flags[i] = ts_flags[i] + flag
118
+
119
+
120
+ def _record_fails_check(
121
+ int record_index,
122
+ np.ndarray ts_index,
123
+ np.ndarray ts_values,
124
+ int ts_utc_offset_minutes,
125
+ list thresholds,
126
+ array.array threshold_deltas,
127
+ array.array threshold_allowed_diffs,
128
+ int symmetric,
129
+ list failures,
130
+ ):
131
+ cdef int ti
132
+ cdef double diff
133
+
134
+ for ti in range(len(threshold_deltas)):
135
+ diff = _record_fails_threshold(
136
+ record_index,
137
+ threshold_deltas[ti],
138
+ threshold_allowed_diffs[ti],
139
+ ts_index,
140
+ ts_values,
141
+ symmetric,
142
+ )
143
+ if diff:
144
+ timestamp = ts_index[record_index].item()
145
+ datestr = str(
146
+ np.datetime64(timestamp, "ns") + np.timedelta64(ts_utc_offset_minutes, "m")
147
+ )[:16]
148
+ diffsign = '+' if diff > 0 else ''
149
+ thresholdsign = '-' if diff < 0 else ''
150
+ cmpsign = '>' if diff > 0 else '<'
151
+ failures.append(
152
+ f"{datestr} {diffsign}{diff} in {thresholds[ti].delta_t} "
153
+ f"({cmpsign} {thresholdsign}{threshold_allowed_diffs[ti]})"
154
+ )
155
+ return True
156
+ return False
157
+
158
+
159
+ def _record_fails_threshold(
160
+ int record_index,
161
+ long threshold_delta,
162
+ double threshold_allowed_diff,
163
+ np.ndarray ts_index,
164
+ np.ndarray ts_values,
165
+ int symmetric,
166
+ ):
167
+ cdef double current_value = ts_values[record_index]
168
+ cdef long current_timestamp = ts_index[record_index]
169
+ cdef int i, fails
170
+ cdef double diff;
171
+
172
+ for i in range(record_index - 1, -1, -1):
173
+ if current_timestamp - ts_index[i] > threshold_delta:
174
+ return False
175
+ diff = current_value - ts_values[i];
176
+ fails = (
177
+ diff > threshold_allowed_diff
178
+ or (symmetric and diff < -threshold_allowed_diff)
179
+ )
180
+ if fails:
181
+ return diff
182
+ return False