| 210 | |
| 211 | |
| 212 | class Normalizer: |
| 213 | def __init__( |
| 214 | self, |
| 215 | datamodule_config: DictConfig, |
| 216 | input_normalization: Optional[str] = None, |
| 217 | output_normalization: Optional[str] = None, |
| 218 | spatial_normalization_in: bool = False, |
| 219 | spatial_normalization_out: bool = False, |
| 220 | log_scaling: Union[bool, List[str]] = False, |
| 221 | data_dir: Optional[str] = None, |
| 222 | verbose: bool = True |
| 223 | ): |
| 224 | """ |
| 225 | input_normalization (str): "z" for z-scaling (zero mean and unit standard deviation) |
| 226 | """ |
| 227 | if not verbose: |
| 228 | log.setLevel(logging.WARNING) |
| 229 | |
| 230 | if data_dir is None: |
| 231 | data_dir = datamodule_config.get("data_dir") or constants.DATA_DIR |
| 232 | exp_type = datamodule_config.get("exp_type") |
| 233 | target_type = datamodule_config.get("target_type") |
| 234 | target_variable = datamodule_config.get("target_variable") |
| 235 | |
| 236 | self._layer_mask = 45 if exp_type == constants.CLEAR_SKY else 14 |
| 237 | self._recover_meta_info(data_dir) |
| 238 | self._input_normalizer: Dict[str, NormalizationMethod] = dict() |
| 239 | self._output_normalizer: Optional[Dict[str, NormalizationMethod]] = None |
| 240 | |
| 241 | self._target_variables = get_target_variable_names(target_type, target_variable) |
| 242 | if input_normalization is not None: |
| 243 | norma_type = '_spatial' if spatial_normalization_in else '' |
| 244 | info_msg = f" Applying {norma_type.lstrip('_')} {input_normalization} normalization to input data," \ |
| 245 | f" based on pre-computed stats." |
| 246 | log.info(info_msg) |
| 247 | |
| 248 | precomputed_stats = get_statistics(data_dir) |
| 249 | precomputed_stats = {k: precomputed_stats[k] for k in precomputed_stats.keys() if |
| 250 | (('spatial' in k and spatial_normalization_in) or ('spatial' not in k))} |
| 251 | if isinstance(log_scaling, list) or log_scaling: |
| 252 | log.info(' Log scaling pressure and height variables! (no other normalization is applied to them)') |
| 253 | post_log_vals = dict(pressg=(11.473797, 0.10938317), |
| 254 | layer_pressure=(9.29207, 2.097411), |
| 255 | dz=(6.5363674, 1.044927), |
| 256 | layer_thickness=(6.953938313568889, 1.3751644503732554), |
| 257 | level_pressure=(9.252319, 2.1721559)) |
| 258 | vars_to_log_scale = ['pressg', 'layer_pressure', 'dz', 'layer_thickness', 'level_pressure'] |
| 259 | self._layer_log_mask = torch.tensor([2, 5, 12]) |
| 260 | for var in vars_to_log_scale: |
| 261 | dtype = self._variables[var]['data_type'] |
| 262 | s, e = self.feature_by_var[dtype][var]['start'], self.feature_by_var[dtype][var]['end'] |
| 263 | # precomputed_stats[f'{dtype}{prefix}_mean'][..., s:e] = 0 |
| 264 | # precomputed_stats[f'{dtype}{prefix}_std'][..., s:e] = 1 |
| 265 | precomputed_stats[f'{dtype}{norma_type}_mean'][..., s:e] = post_log_vals[var][0] |
| 266 | precomputed_stats[f'{dtype}{norma_type}_std'][..., s:e] = post_log_vals[var][1] |
| 267 | |
| 268 | def log_scaler(X: Dict[str, Tensor]) -> Dict[str, Tensor]: |
| 269 | # layer_log_mask = torch.tensor([2, 5, 12]) |
nothing calls this directly
no outgoing calls
no test coverage detected