|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from bayesflow.utils.serialization import serializable, serialize |
| 4 | +from .transform import Transform |
| 5 | + |
| 6 | + |
| 7 | +@serializable("bayesflow.adapters") |
| 8 | +class NanToNum(Transform): |
| 9 | + """ |
| 10 | + Replace NaNs with a default value, and optionally encode a missing-data mask as a separate output key. |
| 11 | +
|
| 12 | + This is based on "Missing data in amortized simulation-based neural posterior estimation" by Wang et al. (2024). |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + default_value : float |
| 17 | + Value to substitute wherever data is NaN. |
| 18 | + return_mask : bool, default=False |
| 19 | + If True, a mask array will be returned under a new key. |
| 20 | + mask_prefix : str, default='mask_' |
| 21 | + Prefix for the mask key in the output dictionary. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, key: str, default_value: float = 0.0, return_mask: bool = False, mask_prefix: str = "mask"): |
| 25 | + super().__init__() |
| 26 | + self.key = key |
| 27 | + self.default_value = default_value |
| 28 | + self.return_mask = return_mask |
| 29 | + self.mask_prefix = mask_prefix |
| 30 | + |
| 31 | + def get_config(self) -> dict: |
| 32 | + return serialize( |
| 33 | + { |
| 34 | + "key": self.key, |
| 35 | + "default_value": self.default_value, |
| 36 | + "return_mask": self.return_mask, |
| 37 | + "mask_prefix": self.mask_prefix, |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + @property |
| 42 | + def mask_key(self) -> str: |
| 43 | + """ |
| 44 | + Key under which the mask will be stored in the output dictionary. |
| 45 | + """ |
| 46 | + return f"{self.mask_prefix}_{self.key}" |
| 47 | + |
| 48 | + def forward(self, data: dict[str, any], **kwargs) -> dict[str, any]: |
| 49 | + """ |
| 50 | + Forward transform: fill NaNs and optionally output mask under 'mask_<key>'. |
| 51 | + """ |
| 52 | + data = data.copy() |
| 53 | + |
| 54 | + # Check if the mask key already exists in the data |
| 55 | + if self.mask_key in data.keys(): |
| 56 | + raise ValueError( |
| 57 | + f"Mask key '{self.mask_key}' already exists in the data. Please choose a different mask_prefix." |
| 58 | + ) |
| 59 | + |
| 60 | + # Identify NaNs and fill with default value |
| 61 | + mask = np.isnan(data[self.key]) |
| 62 | + data[self.key] = np.nan_to_num(data[self.key], copy=False, nan=self.default_value) |
| 63 | + |
| 64 | + if not self.return_mask: |
| 65 | + return data |
| 66 | + |
| 67 | + # Prepare mask array (1 for valid, 0 for NaN) |
| 68 | + mask_array = (~mask).astype(np.int8) |
| 69 | + |
| 70 | + # Return both the filled data and the mask under separate keys |
| 71 | + data[self.mask_key] = mask_array |
| 72 | + return data |
| 73 | + |
| 74 | + def inverse(self, data: dict[str, any], **kwargs) -> dict[str, any]: |
| 75 | + """ |
| 76 | + Inverse transform: restore NaNs using the mask under 'mask_<key>'. |
| 77 | + """ |
| 78 | + data = data.copy() |
| 79 | + |
| 80 | + # Retrieve mask and values to reconstruct NaNs |
| 81 | + values = data[self.key] |
| 82 | + |
| 83 | + if not self.return_mask: |
| 84 | + values[values == self.default_value] = np.nan # we assume default_value is not in data |
| 85 | + else: |
| 86 | + mask_array = data[self.mask_key].astype(bool) |
| 87 | + # Put NaNs where mask is 0 |
| 88 | + values[~mask_array] = np.nan |
| 89 | + |
| 90 | + data[self.key] = values |
| 91 | + return data |
0 commit comments