-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathstatistics.py
382 lines (316 loc) · 9.45 KB
/
statistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
""" Contains features that perform some statistics operation on the input.
These features reduce some dimension of the input by applying a statistical
operation (sum, mean, etc.). They follow the syntax of the equivalent numpy
function, meaning that 'axis' and 'keepdims' are valid arguments. Moreover,
they all accept the `distributed` keyword, which determines if each image in
the input list should be handled individually or not.
Module Structure
----------------
Classes:
- `Reducer`: Base class for features that reduce input dimensionality using a
statistical function.
- `Sum`: Computes the sum along the specified axis.
- `Prod`: Computes the product along the specified axis.
- `Mean`: Computes the arithmetic mean along the specified axis.
- `Median`: Computes the median along the specified axis.
- `Std`: Computes the standard deviation along the specified axis.
- `Variance`: Computes the variance along the specified axis.
- `Cumsum`: Computes the cumulative sum along the specified axis.
- `Min`: Computes the minimum value along the specified axis.
- `Max`: Computes the maximum value along the specified axis.
- `PeakToPeak`: Computes the range (max - min) along the specified axis.
- `Quantile`: Computes the q-th quantile along the specified axis.
- `Percentile`: Computes the q-th percentile along the specified axis.
Example
-------
Reduce input dimensions using the `Sum` operation, with 'distributed' set
to True:
>>> import numpy as np
>>> from deeptrack import statistics
>>> input_values = [np.ones((2,)), np.zeros((2,))]
>>> sum_operation = statistics.Sum(axis=0, distributed=True)
>>> sum_result = sum_operation(input_values)
>>> print(sum_result) # Output: [2, 0]
Reduce input dimensions using the `Sum` operation, with 'distributed' set
to False:
>>> sum_operation = statistics.Sum(axis=0, distributed=False)
>>> sum_result = sum_operation(input_values)
>>> print(sum_result) # Output: [1, 1]
Reduce input dimensions using the `Mean` operation:
>>> mean_operation = statistics.Mean(axis=0, distributed=True)
>>> mean_result = mean_operation(input_values)
>>> print(mean_result) # Output: [1, 0]
Reducers can be added to the pipeline in two ways:
>>> summed_pipeline = some_pipeline_of_features >> Sum(axis=0)
>>> summed_pipeline = Sum(some_pipeline_of_features, axis=0)
Combining the two ways is not supported, and the behaviour is not guaranteed.
For example:
>>> incorrectly_summed_pipline = some_feature >> Sum(
>>> some_pipeline_of_features, axis=0
>>> )
However, other operators can be used in this way:
>>> correctly_summed_and_subtracted_pipline = some_feature - Sum(
>>> some_pipeline_of_features, axis=0
>>> )
"""
from typing import List
import numpy as np
from deeptrack import Image
from deeptrack import Feature
class Reducer(Feature):
"""Base class of features that reduce the dimensionality of the input.
Parameters
==========
function : Callable
The function used to reduce the input.
feature : Feature, optional
If not None, the output of this feature is used as the input.
distributed : bool
Whether to apply the reducer to each image in the input list
individually.
axis : int or tuple of int
The axis / axes to reduce over.
keepdims : bool
Whether to keep the singleton dimensions after reducing or squeezing
them.
"""
def __init__(self, function, feature=None, distributed=True, **kwargs):
self.function = function
if feature:
super().__init__(_input=feature, distributed=distributed, **kwargs)
else:
super().__init__(distributed=distributed, **kwargs)
def _process_and_get(self, image_list, **feature_input) -> List[Image]:
self.__distributed__ = feature_input["distributed"]
return super()._process_and_get(image_list, **feature_input)
def get(self, image, axis, keepdims=None, **kwargs):
if keepdims is None:
return self.function(image, axis=axis)
else:
return self.function(image, axis=axis, keepdims=keepdims)
class Sum(Reducer):
"""Compute the sum along the specified axis"""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.sum,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Prod(Reducer):
"""Compute the product along the specified axis"""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.prod,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Mean(Reducer):
"""Compute the arithmetic mean along the specified axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.mean,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Median(Reducer):
"""Compute the median along the specified axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.median,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Std(Reducer):
"""Compute the standard deviation along the specified axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.std,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Variance(Reducer):
"""Compute the variance along the specified axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.var,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Cumsum(Reducer):
"""Compute the cummulative sum along the specified axis."""
def __init__(self, feature=None, axis=None, distributed=True, **kwargs):
super().__init__(
np.cumsum,
feature=feature,
axis=axis,
distributed=distributed,
**kwargs
)
class Min(Reducer):
"""Return the minimum of an array or minimum along an axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.min,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Max(Reducer):
"""Return the maximum of an array or maximum along an axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.max,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class PeakToPeak(Reducer):
"""Range of values (maximum - minimum) along an axis."""
def __init__(
self,
feature=None,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
super().__init__(
np.ptp,
feature=feature,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Quantile(Reducer):
"""Compute the q-th quantile of the data along the specified axis.
Parameters
==========
q : float
Quantile to compute (0 through 1).
"""
def __init__(
self,
feature=None,
q=0.95,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
def quantile(image, **kwargs):
return np.quantile(image, self.q(), **kwargs)
super().__init__(
quantile,
feature=feature,
q=q,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)
class Percentile(Reducer):
"""Compute the q-th percentile of the data along the specified axis.
Parameters
==========
q : float
Percentile to compute, (0 through 100).
"""
def __init__(
self,
feature=None,
q=95,
axis=None,
keepdims=False,
distributed=True,
**kwargs
):
def percentile(image, **kwargs):
return np.percentile(image, self.q(), **kwargs)
super().__init__(
percentile,
feature=feature,
q=q,
axis=axis,
keepdims=keepdims,
distributed=distributed,
**kwargs
)