Skip to content

Commit bcaa4a7

Browse files
committed
make sure json can handle numpy arrays
1 parent 868895e commit bcaa4a7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

ecg_bench/utils/dir_file_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ def open_json(path: Union[str, Path]) -> dict:
2121
@staticmethod
2222
def save_json(data: dict, path: Union[str, Path]):
2323
"""Save a dictionary to a JSON file."""
24+
def convert_numpy(obj):
25+
if isinstance(obj, np.ndarray):
26+
return obj.tolist()
27+
elif isinstance(obj, dict):
28+
return {key: convert_numpy(value) for key, value in obj.items()}
29+
elif isinstance(obj, list):
30+
return [convert_numpy(item) for item in obj]
31+
else:
32+
return obj
33+
2434
with open(path, 'w') as f:
25-
json.dump(data, f)
35+
json.dump(convert_numpy(data), f)
2636

2737
@staticmethod
2838
def get_system_prompt(system_prompt_path: Union[str, Path]):

0 commit comments

Comments
 (0)