1
1
"""
2
2
The module provides an EOTask for the computation of a T-Digest representation of an EOPatch.
3
+ Requires installation of `eolearn.ml_tools[TDIGEST]`.
3
4
4
5
Copyright (c) 2017- Sinergise and contributors
5
6
For the full list of contributors, see the CREDITS file in the root directory of this source tree.
16
17
from eolearn .core import EOPatch , EOTask , FeatureType
17
18
from eolearn .core .types import FeatureSpec , FeaturesSpecification
18
19
19
- ModeTypes = Literal ["standard" , "timewise" , "monthly" , "total" ]
20
+ ModeTypes = Union [ Literal ["standard" , "timewise" , "monthly" , "total" ], Callable ]
20
21
21
22
22
23
class TDigestTask (EOTask ):
@@ -47,7 +48,7 @@ def __init__(
47
48
* | `'total'` computes the total T-Digest representation of the whole feature accumulating all timestamps,
48
49
| bands and pixels. Cannot be used with `pixelwise=True`.
49
50
* | Callable computes the T-Digest representation defined by the processing function given as mode. Receives
50
- | the input_array of the feature, the timestamps, the shape and the pixelwise and filternan keywords as an input.
51
+ | the input_array of the feature, timestamps, shape and pixelwise and filternan keywords as an input.
51
52
:param pixelwise: Decider whether to compute the T-Digest representation accumulating pixels or per pixel.
52
53
Cannot be used with `mode='total'`.
53
54
:param filternan: Decider whether to filter out nan-values before computing the T-Digest.
@@ -83,8 +84,13 @@ def execute(self, eopatch: EOPatch) -> EOPatch:
83
84
for in_feature_ , out_feature_ , shape in _looper (
84
85
in_feature = self .in_feature , out_feature = self .out_feature , eopatch = eopatch
85
86
):
86
- eopatch [out_feature_ ] = _processing_function .get (self .mode , self .mode )(
87
- input_array = eopatch [in_feature_ ], timestamps = eopatch .timestamps , shape = shape , pixelwise = self .pixelwise , filternan = self .filternan
87
+ processing_func = self .mode if callable (self .mode ) else _processing_function [self .mode ]
88
+ eopatch [out_feature_ ] = processing_func (
89
+ input_array = eopatch [in_feature_ ],
90
+ timestamps = eopatch .timestamps ,
91
+ shape = shape ,
92
+ pixelwise = self .pixelwise ,
93
+ filternan = self .filternan ,
88
94
)
89
95
90
96
return eopatch
@@ -120,7 +126,9 @@ def _looper(
120
126
yield in_feature_ , out_feature_ , shape
121
127
122
128
123
- def _process_standard (input_array : np .ndarray , shape : np .ndarray , pixelwise : bool , filternan : bool , ** _ : Any ) -> np .ndarray :
129
+ def _process_standard (
130
+ input_array : np .ndarray , shape : np .ndarray , pixelwise : bool , filternan : bool , ** _ : Any
131
+ ) -> np .ndarray :
124
132
if pixelwise :
125
133
array = np .empty (shape [- 3 :], dtype = object )
126
134
for i , j , k in product (range (shape [- 3 ]), range (shape [- 2 ]), range (shape [- 1 ])):
@@ -134,7 +142,9 @@ def _process_standard(input_array: np.ndarray, shape: np.ndarray, pixelwise: boo
134
142
return array
135
143
136
144
137
- def _process_timewise (input_array : np .ndarray , shape : np .ndarray , pixelwise : bool , filternan : bool , ** _ : Any ) -> np .ndarray :
145
+ def _process_timewise (
146
+ input_array : np .ndarray , shape : np .ndarray , pixelwise : bool , filternan : bool , ** _ : Any
147
+ ) -> np .ndarray :
138
148
if pixelwise :
139
149
array = np .empty (shape , dtype = object )
140
150
for time_ , i , j , k in product (range (shape [0 ]), range (shape [1 ]), range (shape [2 ]), range (shape [3 ])):
0 commit comments