|
8 | 8 | .. autosummary::
|
9 | 9 | :toctree: ../generated/
|
10 | 10 |
|
| 11 | + postprocess_import |
11 | 12 | check_motion_input_image
|
12 | 13 | """
|
| 14 | +import inspect |
| 15 | +from collections import defaultdict |
13 | 16 | from functools import wraps
|
14 | 17 |
|
15 | 18 | import numpy as np
|
16 | 19 |
|
17 | 20 |
|
| 21 | +def postprocess_import(fillna=np.nan, dtype='double'): |
| 22 | + """ |
| 23 | + Postprocess the imported precipitation data. |
| 24 | + Operations: |
| 25 | + - Allow type casting (dtype keyword) |
| 26 | + - Set invalid or missing data to predefined value (fillna keyword) |
| 27 | +
|
| 28 | + This decorator replaces the text "{extra_kwargs}" in the function's |
| 29 | + docstring with the documentation of the keywords used in the postprocessing. |
| 30 | + The additional docstrings are added as "Other Parameters" in the importer function. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + dtype : str |
| 35 | + Default data type for precipitation. Double precision by default. |
| 36 | + fillna : float or np.nan |
| 37 | + Default value used to represent the missing data ("No Coverage"). |
| 38 | + By default, np.nan is used. |
| 39 | + """ |
| 40 | + |
| 41 | + def _postprocess_import(importer): |
| 42 | + @wraps(importer) |
| 43 | + def _import_with_postprocessing(*args, **kwargs): |
| 44 | + |
| 45 | + precip, *other_args = importer(*args, **kwargs) |
| 46 | + |
| 47 | + _dtype = kwargs.get("dtype", dtype) |
| 48 | + |
| 49 | + accepted_precisions = ["float32", "float64", "single", "double"] |
| 50 | + if _dtype not in accepted_precisions: |
| 51 | + raise ValueError( |
| 52 | + "The selected precision does not correspond to a valid value." |
| 53 | + "The accepted values are: " + str(accepted_precisions) |
| 54 | + ) |
| 55 | + |
| 56 | + if isinstance(precip, np.ma.MaskedArray): |
| 57 | + invalid_mask = np.ma.getmaskarray(precip) |
| 58 | + precip.data[invalid_mask] = fillna |
| 59 | + else: |
| 60 | + # If plain numpy arrays are used, the importers should indicate |
| 61 | + # the invalid values with np.nan. |
| 62 | + _fillna = kwargs.get("fillna", fillna) |
| 63 | + if _fillna is not np.nan: |
| 64 | + mask = ~np.isfinite(precip) |
| 65 | + precip[mask] = _fillna |
| 66 | + |
| 67 | + return (precip.astype(_dtype),) + tuple(other_args) |
| 68 | + |
| 69 | + extra_kwargs_doc = """ |
| 70 | + Other Parameters |
| 71 | + ---------------- |
| 72 | + dtype : str |
| 73 | + Data-type to which the array is cast. |
| 74 | + Valid values: "float32", "float64", "single", and "double". |
| 75 | + fillna : float or np.nan |
| 76 | + Value used to represent the missing data ("No Coverage"). |
| 77 | + By default, np.nan is used. |
| 78 | + """ |
| 79 | + |
| 80 | + # Clean up indentation from docstrings for the |
| 81 | + # docstrings to be merged correctly. |
| 82 | + extra_kwargs_doc = inspect.cleandoc(extra_kwargs_doc) |
| 83 | + _import_with_postprocessing.__doc__ = inspect.cleandoc(_import_with_postprocessing.__doc__) |
| 84 | + |
| 85 | + # Add extra kwargs docstrings |
| 86 | + _import_with_postprocessing.__doc__ = _import_with_postprocessing.__doc__.format_map( |
| 87 | + defaultdict(str, extra_kwargs_doc=extra_kwargs_doc)) |
| 88 | + |
| 89 | + return _import_with_postprocessing |
| 90 | + |
| 91 | + return _postprocess_import |
| 92 | + |
| 93 | + |
18 | 94 | def check_input_frames(minimum_input_frames=2,
|
19 | 95 | maximum_input_frames=np.inf,
|
20 | 96 | just_ndim=False):
|
21 | 97 | """
|
22 | 98 | Check that the input_images used as inputs in the optical-flow
|
23 |
| - methods has the correct shape (t, x, y ). |
| 99 | + methods have the correct shape (t, x, y ). |
24 | 100 | """
|
25 | 101 |
|
26 | 102 | def _check_input_frames(motion_method_func):
|
| 103 | + |
27 | 104 | @wraps(motion_method_func)
|
28 | 105 | def new_function(*args, **kwargs):
|
29 | 106 | """
|
|
0 commit comments