File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change 4
4
import sys
5
5
from datetime import datetime
6
6
from pathlib import Path
7
+ import numpy as np
7
8
8
9
from datasets .utils .filelock import FileLock
9
10
10
11
from . import __version__
11
12
12
13
14
+ class NpEncoder (json .JSONEncoder ):
15
+ """Numpy aware JSON encoder.
16
+ """
17
+ def default (self , o ):
18
+ if isinstance (o , np .floating ):
19
+ return float (o )
20
+ if isinstance (o , np .integer ):
21
+ return int (o )
22
+ if isinstance (o , np .ndarray ):
23
+ return o .tolist ()
24
+ return super ().default (o )
25
+
26
+
13
27
def save (path_or_file , ** data ):
14
28
"""
15
29
Saves results to a JSON file. Also saves system information such as current time, current commit
@@ -40,7 +54,7 @@ def save(path_or_file, **data):
40
54
41
55
with FileLock (str (file_path ) + ".lock" ):
42
56
with open (file_path , "w" ) as f :
43
- json .dump (data , f )
57
+ json .dump (data , f , cls = NpEncoder )
44
58
45
59
# cleanup lock file
46
60
try :
You can’t perform that action at this time.
0 commit comments