Skip to content

Commit 3ce2f0b

Browse files
committed
add isnan function
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent 114b95e commit 3ce2f0b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/power_grid_model_io/converters/pgm_json_converter.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import numpy as np
1515
from power_grid_model import initialize_array
16-
from power_grid_model._utils import is_nan
1716
from power_grid_model.data_types import ComponentList, Dataset, SingleDataset, SinglePythonDataset
1817
from power_grid_model.utils import json_deserialize, json_serialize
1918

@@ -216,8 +215,7 @@ def _is_batch(data: Dataset) -> bool:
216215
is_batch = is_dense_batch or is_sparse_batch
217216
return bool(is_batch)
218217

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:
221219
"""This function converts a single power-grid-model dataset to a structured dataset
222220
223221
Args:
@@ -248,7 +246,7 @@ def _serialize_dataset(data: SingleDataset, extra_info: Optional[ExtraInfo] = No
248246
{
249247
attribute: obj[attribute].tolist()
250248
for attribute in objects.dtype.names
251-
if not is_nan(obj[attribute])
249+
if not self._is_nan(obj[attribute])
252250
},
253251
extra_info.get(obj["id"], {}),
254252
)
@@ -296,3 +294,21 @@ def _get_first_by(data: List[Dict[str, Any]], field: str, value: Any) -> Optiona
296294
return entry
297295

298296
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

Comments
 (0)