MCPcopy Create free account
hub / github.com/apple/ml-pointersect / default

Method default

cdslib/core/utils/print_and_save.py:33–70  ·  view source on GitHub ↗
(self, obj)

Source from the content-addressed store, hash-verified

31 """
32
33 def default(self, obj):
34 if isinstance(obj, torch.Tensor):
35 obj = obj.detach().cpu().numpy()
36
37 if isinstance(
38 obj,
39 (
40 np.int_,
41 np.intc,
42 np.intp,
43 np.int8,
44 np.int16,
45 np.int32,
46 np.int64,
47 np.uint8,
48 np.uint16,
49 np.uint32,
50 np.uint64,
51 ),
52 ):
53 return int(obj)
54
55 elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
56 return float(obj)
57
58 elif isinstance(obj, (np.complex_, np.complex64, np.complex128)):
59 return {"real": obj.real, "imag": obj.imag}
60
61 elif isinstance(obj, (np.ndarray,)):
62 return obj.tolist()
63
64 elif isinstance(obj, (np.bool_)):
65 return bool(obj)
66
67 elif isinstance(obj, (np.void)):
68 return None
69
70 return json.JSONEncoder.default(self, obj)
71
72
73def print1D(arr: T.Union[torch.Tensor, np.ndarray, T.Sequence], num_per_line=-1, format=">6.3f") -> bool:

Callers

nothing calls this directly

Calls 1

detachMethod · 0.45

Tested by

no test coverage detected