| 1 |
|
| 2 |
) |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
from numbers import Integral, Real |
| 10 |
from packaging import version |
| 11 |
|
| 12 |
import numpy as np |
| 13 |
|
| 14 |
|
| 15 |
BOOLS = (bool, np.bool_) |
| 16 |
|
| 17 |
|
| 18 |
class DataType: |
| 19 |
UInt8 = np.uint8 |
| 20 |
UInt16 = np.uint16 |
| 21 |
UInt32 = np.uint32 |
| 22 |
UInt64 = np.uint64 |
| 23 |
Int8 = np.int8 |
| 24 |
Int16 = np.int16 |
| 25 |
Int32 = np.int32 |
| 26 |
Int64 = np.int64 |
| 27 |
Float = np.float32 |
| 28 |
Double = np.double |
| 29 |
if version.parse(np.__version__) < version.parse("2.0"): |
| 30 |
String = np.unicode_ |
| 31 |
else: |
| 32 |
String = np.str_ |
| 33 |
Bool = np.bool_ |
| 34 |
|
| 35 |
|
| 36 |
IntTypes = (Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64) |
| 37 |
FloatTypes = (Float, Double) |
| 38 |
|
| 39 |
@classmethod |
| 40 |
def get_dtype(cls, value): |
| 41 |
if isinstance(value, BOOLS): |
| 42 |
return cls.Bool |
| 43 |
elif isinstance(value, Integral): |
| 44 |
return cls.Int64 |
| 45 |
elif isinstance(value, Real): |
| 46 |
return cls.Double |
| 47 |
elif isinstance(value, str): |
| 48 |
return cls.String |
| 49 |
else: |
| 50 |
raise ValueError("Unknown type for value {}".format(value)) |
| 51 |
|
| 52 |
@classmethod |
| 53 |
def is_numeric_dtype(cls, dtype): |
| 54 |
return dtype in cls.IntTypes + cls.FloatTypes |