Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 82cd22e

Browse files
sakshamarora1pdxjohnny
authored andcommitted
util: data: export_value now converts numpy array to JSON serializable datatype
Fixes: #822 Signed-off-by: sakshamarora1 <[email protected]>
1 parent 48c54b9 commit 82cd22e

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
105105
packaging the model.
106106
- IntegrationCLITestCase creates a new directory and chdir into it for each test
107107
### Fixed
108+
- `export_value` now converts numpy array to JSON serializable datatype
108109
- CSV source overwriting configloaded data to every row
109110
- Race condition in `MemoryRedundancyChecker` when more than 4 possible
110111
parameter sets for an operation.

dffml/util/data.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def type_lookup(typename):
205205

206206
def export_value(obj, key, value):
207207
# export and _asdict are not classmethods
208+
typename_lower = str(type(value)).lower()
208209
if hasattr(value, "ENTRY_POINT_ORIG_LABEL") and hasattr(value, "config"):
209210
obj[key] = {"plugin": value.ENTRY_POINT_ORIG_LABEL}
210211
export_value(obj[key], "config", value.config)
@@ -216,14 +217,17 @@ def export_value(obj, key, value):
216217
obj[key] = value.export()
217218
elif hasattr(value, "_asdict"):
218219
obj[key] = value._asdict()
219-
elif (
220-
getattr(type(value), "__module__", None) == "numpy"
221-
and isinstance(value, collections.abc.Iterable)
222-
and isinstance(
223-
getattr(value, "flatten", None), collections.abc.Callable
224-
)
225-
):
226-
obj[key] = tuple(value.flatten())
220+
elif "numpy" in typename_lower:
221+
if isinstance(value, collections.abc.Iterable) and isinstance(
222+
getattr(value, "tolist", None), collections.abc.Callable
223+
):
224+
obj[key] = value.tolist()
225+
elif ".int" in typename_lower or ".uint" in typename_lower:
226+
obj[key] = int(value)
227+
elif "float" in typename_lower:
228+
obj[key] = float(value)
229+
else:
230+
raise ValueError(f"Unknown numpy type: {typename_lower}")
227231
elif dataclasses.is_dataclass(value):
228232
obj[key] = export_dict(**dataclasses.asdict(value))
229233
elif getattr(value, "__module__", None) == "typing":

0 commit comments

Comments
 (0)