MCPcopy Create free account
hub / github.com/ByteDance-Seed/Bagel / NumpyEncoder

Class NumpyEncoder

eval/gen/rise/utils.py:13–29  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class NumpyEncoder(json.JSONEncoder):
14 def default(self, obj):
15 if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
16 np.int16, np.int32, np.int64, np.uint8,
17 np.uint16, np.uint32, np.uint64)):
18 return int(obj)
19 elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
20 return float(obj)
21 elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
22 return {'real': obj.real, 'imag': obj.imag}
23 elif isinstance(obj, (np.ndarray,)):
24 return obj.tolist()
25 elif isinstance(obj, (np.bool_)):
26 return bool(obj)
27 elif isinstance(obj, (np.void)):
28 return None
29 return json.JSONEncoder.default(self, obj)
30
31def dump(data, f, **kwargs):
32 def dump_pkl(data, pth, **kwargs):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected