|
| 1 | +# (C) Copyright 2024 Anemoi contributors. |
| 2 | +# |
| 3 | +# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 4 | +# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 5 | +# |
| 6 | +# In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 | +# granted to it by virtue of its status as an intergovernmental organisation |
| 8 | +# nor does it submit to any jurisdiction. |
| 9 | + |
| 10 | + |
| 11 | +import logging |
| 12 | +from abc import ABC |
| 13 | +from typing import Optional |
| 14 | + |
| 15 | +import torch |
| 16 | + |
| 17 | +from anemoi.models.data_indices.collection import IndexCollection |
| 18 | +from anemoi.models.preprocessing import BasePreprocessor |
| 19 | +from anemoi.models.preprocessing.mappings import boxcox_converter |
| 20 | +from anemoi.models.preprocessing.mappings import expm1_converter |
| 21 | +from anemoi.models.preprocessing.mappings import inverse_boxcox_converter |
| 22 | +from anemoi.models.preprocessing.mappings import log1p_converter |
| 23 | +from anemoi.models.preprocessing.mappings import noop |
| 24 | +from anemoi.models.preprocessing.mappings import sqrt_converter |
| 25 | +from anemoi.models.preprocessing.mappings import square_converter |
| 26 | + |
| 27 | +LOGGER = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +class Monomapper(BasePreprocessor, ABC): |
| 31 | + """Remap and convert variables for single variables.""" |
| 32 | + |
| 33 | + supported_methods = { |
| 34 | + method: [f, inv] |
| 35 | + for method, f, inv in zip( |
| 36 | + ["log1p", "sqrt", "boxcox", "none"], |
| 37 | + [log1p_converter, sqrt_converter, boxcox_converter, noop], |
| 38 | + [expm1_converter, square_converter, inverse_boxcox_converter, noop], |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + config=None, |
| 45 | + data_indices: Optional[IndexCollection] = None, |
| 46 | + statistics: Optional[dict] = None, |
| 47 | + ) -> None: |
| 48 | + super().__init__(config, data_indices, statistics) |
| 49 | + self._create_remapping_indices(statistics) |
| 50 | + self._validate_indices() |
| 51 | + |
| 52 | + def _validate_indices(self): |
| 53 | + assert ( |
| 54 | + len(self.index_training_input) |
| 55 | + == len(self.index_inference_input) |
| 56 | + == len(self.index_inference_output) |
| 57 | + == len(self.index_training_out) |
| 58 | + == len(self.remappers) |
| 59 | + ), ( |
| 60 | + f"Error creating conversion indices {len(self.index_training_input)}, " |
| 61 | + f"{len(self.index_inference_input)}, {len(self.index_training_input)}, {len(self.index_training_out)}, {len(self.remappers)}" |
| 62 | + ) |
| 63 | + |
| 64 | + def _create_remapping_indices( |
| 65 | + self, |
| 66 | + statistics=None, |
| 67 | + ): |
| 68 | + """Create the parameter indices for remapping.""" |
| 69 | + # list for training and inference mode as position of parameters can change |
| 70 | + name_to_index_training_input = self.data_indices.data.input.name_to_index |
| 71 | + name_to_index_inference_input = self.data_indices.model.input.name_to_index |
| 72 | + name_to_index_training_output = self.data_indices.data.output.name_to_index |
| 73 | + name_to_index_inference_output = self.data_indices.model.output.name_to_index |
| 74 | + self.num_training_input_vars = len(name_to_index_training_input) |
| 75 | + self.num_inference_input_vars = len(name_to_index_inference_input) |
| 76 | + self.num_training_output_vars = len(name_to_index_training_output) |
| 77 | + self.num_inference_output_vars = len(name_to_index_inference_output) |
| 78 | + |
| 79 | + ( |
| 80 | + self.remappers, |
| 81 | + self.backmappers, |
| 82 | + self.index_training_input, |
| 83 | + self.index_training_out, |
| 84 | + self.index_inference_input, |
| 85 | + self.index_inference_output, |
| 86 | + ) = ( |
| 87 | + [], |
| 88 | + [], |
| 89 | + [], |
| 90 | + [], |
| 91 | + [], |
| 92 | + [], |
| 93 | + ) |
| 94 | + |
| 95 | + # Create parameter indices for remapping variables |
| 96 | + for name in name_to_index_training_input: |
| 97 | + method = self.methods.get(name, self.default) |
| 98 | + if method in self.supported_methods: |
| 99 | + self.remappers.append(self.supported_methods[method][0]) |
| 100 | + self.backmappers.append(self.supported_methods[method][1]) |
| 101 | + self.index_training_input.append(name_to_index_training_input[name]) |
| 102 | + if name in name_to_index_training_output: |
| 103 | + self.index_training_out.append(name_to_index_training_output[name]) |
| 104 | + else: |
| 105 | + self.index_training_out.append(None) |
| 106 | + if name in name_to_index_inference_input: |
| 107 | + self.index_inference_input.append(name_to_index_inference_input[name]) |
| 108 | + else: |
| 109 | + self.index_inference_input.append(None) |
| 110 | + if name in name_to_index_inference_output: |
| 111 | + self.index_inference_output.append(name_to_index_inference_output[name]) |
| 112 | + else: |
| 113 | + # this is a forcing variable. It is not in the inference output. |
| 114 | + self.index_inference_output.append(None) |
| 115 | + else: |
| 116 | + raise KeyError[f"Unknown remapping method for {name}: {method}"] |
| 117 | + |
| 118 | + def transform(self, x, in_place: bool = True) -> torch.Tensor: |
| 119 | + if not in_place: |
| 120 | + x = x.clone() |
| 121 | + if x.shape[-1] == self.num_training_input_vars: |
| 122 | + idx = self.index_training_input |
| 123 | + elif x.shape[-1] == self.num_inference_input_vars: |
| 124 | + idx = self.index_inference_input |
| 125 | + else: |
| 126 | + raise ValueError( |
| 127 | + f"Input tensor ({x.shape[-1]}) does not match the training " |
| 128 | + f"({self.num_training_input_vars}) or inference shape ({self.num_inference_input_vars})", |
| 129 | + ) |
| 130 | + for i, remapper in zip(idx, self.remappers): |
| 131 | + if i is not None: |
| 132 | + x[..., i] = remapper(x[..., i]) |
| 133 | + return x |
| 134 | + |
| 135 | + def inverse_transform(self, x, in_place: bool = True) -> torch.Tensor: |
| 136 | + if not in_place: |
| 137 | + x = x.clone() |
| 138 | + if x.shape[-1] == self.num_training_output_vars: |
| 139 | + idx = self.index_training_out |
| 140 | + elif x.shape[-1] == self.num_inference_output_vars: |
| 141 | + idx = self.index_inference_output |
| 142 | + else: |
| 143 | + raise ValueError( |
| 144 | + f"Input tensor ({x.shape[-1]}) does not match the training " |
| 145 | + f"({self.num_training_output_vars}) or inference shape ({self.num_inference_output_vars})", |
| 146 | + ) |
| 147 | + for i, backmapper in zip(idx, self.backmappers): |
| 148 | + if i is not None: |
| 149 | + x[..., i] = backmapper(x[..., i]) |
| 150 | + return x |
0 commit comments