|
13 | 13 |
|
14 | 14 | import numpy as np |
15 | 15 | from power_grid_model import initialize_array |
16 | | -from power_grid_model._utils import is_nan |
17 | 16 | from power_grid_model.data_types import ComponentList, Dataset, SingleDataset, SinglePythonDataset |
18 | 17 | from power_grid_model.utils import json_deserialize, json_serialize |
19 | 18 |
|
@@ -216,8 +215,7 @@ def _is_batch(data: Dataset) -> bool: |
216 | 215 | is_batch = is_dense_batch or is_sparse_batch |
217 | 216 | return bool(is_batch) |
218 | 217 |
|
219 | | - @staticmethod |
220 | | - def _serialize_dataset(data: SingleDataset, extra_info: Optional[ExtraInfo] = None) -> SinglePythonDataset: |
| 218 | + def _serialize_dataset(self, data: SingleDataset, extra_info: Optional[ExtraInfo] = None) -> SinglePythonDataset: |
221 | 219 | """This function converts a single power-grid-model dataset to a structured dataset |
222 | 220 |
|
223 | 221 | Args: |
@@ -248,7 +246,7 @@ def _serialize_dataset(data: SingleDataset, extra_info: Optional[ExtraInfo] = No |
248 | 246 | { |
249 | 247 | attribute: obj[attribute].tolist() |
250 | 248 | for attribute in objects.dtype.names |
251 | | - if not is_nan(obj[attribute]) |
| 249 | + if not self._is_nan(obj[attribute]) |
252 | 250 | }, |
253 | 251 | extra_info.get(obj["id"], {}), |
254 | 252 | ) |
@@ -296,3 +294,21 @@ def _get_first_by(data: List[Dict[str, Any]], field: str, value: Any) -> Optiona |
296 | 294 | return entry |
297 | 295 |
|
298 | 296 | return None |
| 297 | + |
| 298 | + @staticmethod |
| 299 | + def _is_nan(data: np.ndarray) -> bool: |
| 300 | + """ |
| 301 | + Determine if the data point is valid |
| 302 | + Args: |
| 303 | + data: a single scaler or numpy array |
| 304 | +
|
| 305 | + Returns: |
| 306 | + True if all the data points are invalid |
| 307 | + False otherwise |
| 308 | + """ |
| 309 | + nan_func = { |
| 310 | + np.dtype("f8"): lambda x: np.all(np.isnan(x)), |
| 311 | + np.dtype("i4"): lambda x: np.all(x == np.iinfo("i4").min), |
| 312 | + np.dtype("i1"): lambda x: np.all(x == np.iinfo("i1").min), |
| 313 | + } |
| 314 | + return bool(nan_func[data.dtype](data)) |
0 commit comments