|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +pysteps.postprocessing.interface |
| 4 | +==================== |
| 5 | +
|
| 6 | +Interface for the postprocessing module. |
| 7 | +
|
| 8 | +Support postprocessing types: |
| 9 | + - ensmeblestats |
| 10 | + - diagnostics |
| 11 | +
|
| 12 | +.. currentmodule:: pysteps.postprocessing.interface |
| 13 | +
|
| 14 | +.. autosummary:: |
| 15 | + :toctree: ../generated/ |
| 16 | +
|
| 17 | + get_method |
| 18 | +""" |
| 19 | +import importlib |
| 20 | + |
| 21 | +from pysteps.postprocessing import diagnostics, ensemblestats |
| 22 | +from pprint import pprint |
| 23 | +import warnings |
| 24 | + |
| 25 | +_diagnostics_methods = dict() |
| 26 | + |
| 27 | +_ensemblestats_methods = dict( |
| 28 | + mean=ensemblestats.mean, |
| 29 | + excprob=ensemblestats.excprob, |
| 30 | + banddepth=ensemblestats.banddepth, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def add_postprocessor( |
| 35 | + postprocessors_function_name, _postprocessors, module, attributes |
| 36 | +): |
| 37 | + """ |
| 38 | + Add the postprocessor to the appropriate _methods dictionary and to the module. |
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | +
|
| 42 | + postprocessors_function_name: str |
| 43 | + for example, e.g. diagnostic_example1 |
| 44 | + _postprocessors: function |
| 45 | + the function to be added |
| 46 | + @param module: the module where the function is added, e.g. 'diagnostics' |
| 47 | + @param attributes: the existing functions in the selected module |
| 48 | + """ |
| 49 | + # the dictionary where the function is added |
| 50 | + methods_dict = ( |
| 51 | + _diagnostics_methods if "diagnostic" in module else _ensemblestats_methods |
| 52 | + ) |
| 53 | + |
| 54 | + # get funtion name without mo |
| 55 | + short_name = postprocessors_function_name.replace(f"{module}_", "") |
| 56 | + if short_name not in methods_dict: |
| 57 | + methods_dict[short_name] = _postprocessors |
| 58 | + else: |
| 59 | + warnings.warn( |
| 60 | + f"The {module} identifier '{short_name}' is already available in " |
| 61 | + f"'pysteps.postprocessing.interface_{module}_methods'.\n" |
| 62 | + f"Skipping {module}:{'.'.join(attributes)}", |
| 63 | + RuntimeWarning, |
| 64 | + ) |
| 65 | + |
| 66 | + if hasattr(globals()[module], postprocessors_function_name): |
| 67 | + warnings.warn( |
| 68 | + f"The {module} function '{short_name}' is already an attribute" |
| 69 | + f"of 'pysteps.postprocessing.{module}'.\n" |
| 70 | + f"Skipping {module}:{'.'.join(attributes)}", |
| 71 | + RuntimeWarning, |
| 72 | + ) |
| 73 | + else: |
| 74 | + setattr(globals()[module], postprocessors_function_name, _postprocessors) |
| 75 | + |
| 76 | + |
| 77 | +def discover_postprocessors(): |
| 78 | + """ |
| 79 | + Search for installed postprocessing plugins in the entrypoint 'pysteps.plugins.postprocessors' |
| 80 | +
|
| 81 | + The postprocessors found are added to the appropriate `_methods` |
| 82 | + dictionary in 'pysteps.postprocessing.interface' containing the available postprocessors. |
| 83 | + """ |
| 84 | + |
| 85 | + # The pkg resources needs to be reloaded to detect new packages installed during |
| 86 | + # the execution of the python application. For example, when the plugins are |
| 87 | + # installed during the tests |
| 88 | + import pkg_resources |
| 89 | + |
| 90 | + importlib.reload(pkg_resources) |
| 91 | + |
| 92 | + # Discover the postprocessors available in the plugins |
| 93 | + for plugintype in ["diagnostic", "ensemblestat"]: |
| 94 | + for entry_point in pkg_resources.iter_entry_points( |
| 95 | + group=f"pysteps.plugins.{plugintype}", name=None |
| 96 | + ): |
| 97 | + _postprocessors = entry_point.load() |
| 98 | + |
| 99 | + postprocessors_function_name = _postprocessors.__name__ |
| 100 | + |
| 101 | + if plugintype in entry_point.module_name: |
| 102 | + add_postprocessor( |
| 103 | + postprocessors_function_name, |
| 104 | + _postprocessors, |
| 105 | + f"{plugintype}s", |
| 106 | + entry_point.attrs, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def print_postprocessors_info(module_name, interface_methods, module_methods): |
| 111 | + """ |
| 112 | + Helper function to print the postprocessors available in the module and in the interface. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + module_name: str |
| 117 | + Name of the module, for example 'pysteps.postprocessing.diagnostics'. |
| 118 | + interface_methods: dict |
| 119 | + Dictionary of the postprocessors declared in the interface, for example _diagnostics_methods. |
| 120 | + module_methods: list |
| 121 | + List of the postprocessors available in the module, for example 'diagnostic_example1'. |
| 122 | +
|
| 123 | + """ |
| 124 | + print(f"\npostprocessors available in the {module_name} module") |
| 125 | + pprint(module_methods) |
| 126 | + |
| 127 | + print( |
| 128 | + "\npostprocessors available in the pysteps.postprocessing.get_method interface" |
| 129 | + ) |
| 130 | + pprint([(short_name, f.__name__) for short_name, f in interface_methods.items()]) |
| 131 | + |
| 132 | + module_methods_set = set(module_methods) |
| 133 | + interface_methods_set = set(interface_methods.keys()) |
| 134 | + |
| 135 | + difference = module_methods_set ^ interface_methods_set |
| 136 | + if len(difference) > 0: |
| 137 | + # print("\nIMPORTANT:") |
| 138 | + _diff = module_methods_set - interface_methods_set |
| 139 | + if len(_diff) > 0: |
| 140 | + print( |
| 141 | + f"\nIMPORTANT:\nThe following postprocessors are available in {module_name} module but not in the pysteps.postprocessing.get_method interface" |
| 142 | + ) |
| 143 | + pprint(_diff) |
| 144 | + _diff = interface_methods_set - module_methods_set |
| 145 | + if len(_diff) > 0: |
| 146 | + print( |
| 147 | + "\nWARNING:\n" |
| 148 | + f"The following postprocessors are available in the pysteps.postprocessing.get_method interface but not in the {module_name} module" |
| 149 | + ) |
| 150 | + pprint(_diff) |
| 151 | + |
| 152 | + |
| 153 | +def postprocessors_info(): |
| 154 | + """Print all the available postprocessors.""" |
| 155 | + |
| 156 | + available_postprocessors = set() |
| 157 | + postprocessors_in_the_interface = set() |
| 158 | + # List the plugins that have been added to the postprocessing.[plugintype] module |
| 159 | + for plugintype in ["diagnostics", "ensemblestats"]: |
| 160 | + # in the dictionary and found by get_methods() function |
| 161 | + interface_methods = ( |
| 162 | + _diagnostics_methods |
| 163 | + if plugintype == "diagnostics" |
| 164 | + else _ensemblestats_methods |
| 165 | + ) |
| 166 | + # in the pysteps.postprocessing module |
| 167 | + module_name = f"pysteps.postprocessing.{plugintype}" |
| 168 | + available_module_methods = [ |
| 169 | + attr |
| 170 | + for attr in dir(importlib.import_module(module_name)) |
| 171 | + if attr.startswith(plugintype[:-1]) |
| 172 | + ] |
| 173 | + # add the pre-existing ensemblestats functions (see _ensemblestats_methods above) |
| 174 | + # that do not follow the convention to start with "ensemblestat_" as the plugins |
| 175 | + if "ensemblestats" in plugintype: |
| 176 | + available_module_methods += [ |
| 177 | + em |
| 178 | + for em in _ensemblestats_methods.keys() |
| 179 | + if not em.startswith("ensemblestat_") |
| 180 | + ] |
| 181 | + print_postprocessors_info( |
| 182 | + module_name, interface_methods, available_module_methods |
| 183 | + ) |
| 184 | + available_postprocessors = available_postprocessors.union( |
| 185 | + available_module_methods |
| 186 | + ) |
| 187 | + postprocessors_in_the_interface = postprocessors_in_the_interface.union( |
| 188 | + interface_methods.keys() |
| 189 | + ) |
| 190 | + |
| 191 | + return available_postprocessors, postprocessors_in_the_interface |
| 192 | + |
| 193 | + |
| 194 | +def get_method(name, method_type): |
| 195 | + """ |
| 196 | + Return a callable function for the method corresponding to the given |
| 197 | + name. |
| 198 | +
|
| 199 | + Parameters |
| 200 | + ---------- |
| 201 | + name: str |
| 202 | + Name of the method. The available options are:\n |
| 203 | +
|
| 204 | + diagnostics: |
| 205 | + [nothing pre-installed] |
| 206 | +
|
| 207 | + ensemblestats: |
| 208 | + pre-installed: mean, excprob, banddepth |
| 209 | +
|
| 210 | + Additional options might exist if plugins are installed. |
| 211 | +
|
| 212 | + method_type: {'diagnostics', 'ensemblestats'} |
| 213 | + Type of the method (see tables above). |
| 214 | +
|
| 215 | + """ |
| 216 | + |
| 217 | + if isinstance(method_type, str): |
| 218 | + method_type = method_type.lower() |
| 219 | + else: |
| 220 | + raise TypeError( |
| 221 | + "Only strings supported for for the method_type" |
| 222 | + + " argument\n" |
| 223 | + + "The available types are: 'diagnostics', 'ensemblestats'" |
| 224 | + ) from None |
| 225 | + |
| 226 | + if isinstance(name, str): |
| 227 | + name = name.lower() |
| 228 | + else: |
| 229 | + raise TypeError( |
| 230 | + "Only strings supported for the method's names.\n" |
| 231 | + + "\nAvailable diagnostics names:" |
| 232 | + + str(list(_diagnostics_methods.keys())) |
| 233 | + + "\nAvailable ensemblestats names:" |
| 234 | + + str(list(_ensemblestats_methods.keys())) |
| 235 | + ) from None |
| 236 | + |
| 237 | + if method_type == "diagnostics": |
| 238 | + methods_dict = _diagnostics_methods |
| 239 | + elif method_type == "ensemblestats": |
| 240 | + methods_dict = _ensemblestats_methods |
| 241 | + else: |
| 242 | + raise ValueError( |
| 243 | + "Unknown method type {}\n".format(method_type) |
| 244 | + + "The available types are: 'diagnostics', 'ensemblestats'" |
| 245 | + ) from None |
| 246 | + |
| 247 | + try: |
| 248 | + return methods_dict[name] |
| 249 | + except KeyError: |
| 250 | + raise ValueError( |
| 251 | + "Unknown {} method {}\n".format(method_type, name) |
| 252 | + + "The available methods are:" |
| 253 | + + str(list(methods_dict.keys())) |
| 254 | + ) from None |
0 commit comments