Skip to content

Commit

Permalink
New JSON encoder
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Podivin <jpodivin@gmail.com>
  • Loading branch information
jpodivin committed Jun 8, 2024
1 parent a4bdc10 commit b105dc6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/evaluate/saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
import sys
from datetime import datetime
from pathlib import Path
import numpy as np

from datasets.utils.filelock import FileLock

from . import __version__


class NpEncoder(json.JSONEncoder):
"""Numpy aware JSON encoder.
"""
def default(self, o):
if isinstance(o, np.floating):
return float(o)
if isinstance(o, np.integer):
return int(o)
if isinstance(o, np.ndarray):
return o.tolist()
return super().default(o)


def save(path_or_file, **data):
"""
Saves results to a JSON file. Also saves system information such as current time, current commit
Expand Down Expand Up @@ -40,7 +54,7 @@ def save(path_or_file, **data):

with FileLock(str(file_path) + ".lock"):
with open(file_path, "w") as f:
json.dump(data, f)
json.dump(data, f, cls=NpEncoder)

# cleanup lock file
try:
Expand Down

0 comments on commit b105dc6

Please sign in to comment.