Apply statistical operation to a sample (image/ndarray/tensor). Notes: Percentile operation uses a partial function that embeds different kwargs (q). In order to print the result nicely, data_addon is added to map the numbers generated by percentile to different key
| 42 | |
| 43 | |
| 44 | class SampleOperations(Operations): |
| 45 | """ |
| 46 | Apply statistical operation to a sample (image/ndarray/tensor). |
| 47 | |
| 48 | Notes: |
| 49 | Percentile operation uses a partial function that embeds different kwargs (q). |
| 50 | In order to print the result nicely, data_addon is added to map the numbers |
| 51 | generated by percentile to different keys ("percentile_00_5" for example). |
| 52 | Annotation of the postfix means the percentage for percentile computation. |
| 53 | For example, _00_5 means 0.5% and _99_5 means 99.5%. |
| 54 | |
| 55 | Example: |
| 56 | |
| 57 | .. code-block:: python |
| 58 | |
| 59 | # use the existing operations |
| 60 | import numpy as np |
| 61 | op = SampleOperations() |
| 62 | data_np = np.random.rand(10, 10).astype(np.float64) |
| 63 | print(op.evaluate(data_np)) |
| 64 | |
| 65 | # add a new operation |
| 66 | op.update({"sum": np.sum}) |
| 67 | print(op.evaluate(data_np)) |
| 68 | """ |
| 69 | |
| 70 | def __init__(self) -> None: |
| 71 | self.data = { |
| 72 | "max": max, |
| 73 | "mean": mean, |
| 74 | "median": median, |
| 75 | "min": min, |
| 76 | "stdev": std, |
| 77 | "percentile": partial(percentile, q=[0.5, 10, 90, 99.5]), |
| 78 | } |
| 79 | self.data_addon = { |
| 80 | "percentile_00_5": ("percentile", 0), |
| 81 | "percentile_10_0": ("percentile", 1), |
| 82 | "percentile_90_0": ("percentile", 2), |
| 83 | "percentile_99_5": ("percentile", 3), |
| 84 | } |
| 85 | |
| 86 | def evaluate(self, data: Any, **kwargs: Any) -> dict: |
| 87 | """ |
| 88 | Applies the callables to the data, and convert the |
| 89 | numerics to list or Python numeric types (int/float). |
| 90 | |
| 91 | Args: |
| 92 | data: input data |
| 93 | """ |
| 94 | ret = super().evaluate(data, **kwargs) |
| 95 | for k, v in self.data_addon.items(): |
| 96 | cache = v[0] |
| 97 | idx = v[1] |
| 98 | if isinstance(v, tuple) and cache in ret: |
| 99 | ret.update({k: ret[cache][idx]}) |
| 100 | |
| 101 | for k, v in ret.items(): |
no outgoing calls
searching dependent graphs…