-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract dtypes and mappings from
merlin.dtype
module's init
- Loading branch information
1 parent
d4c0bbe
commit 5ad1de6
Showing
8 changed files
with
245 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# | ||
# Copyright (c) 2022, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from dataclasses import dataclass, replace | ||
from enum import Enum | ||
from typing import Optional, Tuple | ||
|
||
from merlin.dtype.registry import _dtype_registry | ||
|
||
|
||
class ElementType(Enum): | ||
Bool = "bool" | ||
Int = "int" | ||
UInt = "uint" | ||
Float = "float" | ||
String = "string" | ||
DateTime = "datetime" | ||
Object = "object" | ||
|
||
|
||
@dataclass(eq=True, frozen=True) | ||
class DType: | ||
name: str | ||
elemtype: ElementType | ||
elemsize: Optional[int] = None | ||
signed: Optional[bool] = None | ||
shape: Optional[Tuple] = None | ||
|
||
# These properties refer to what's in a single row of the DataFrame/DictArray | ||
@property | ||
def is_list(self): | ||
return self.shape is not None and len(self.shape) > 1 | ||
|
||
@property | ||
def is_ragged(self): | ||
return self.is_list and None in self.shape[1:] | ||
|
||
def to(self, mapping_name): | ||
mapping = _dtype_registry.mappings[mapping_name] | ||
|
||
# Ignore the shape when matching dtypes | ||
dtype = replace(dtype, **{"shape": None}) | ||
|
||
# Always translate to the first external dtype in the list | ||
return mapping.from_merlin[dtype][0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# | ||
# Copyright (c) 2022, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
from merlin.dtype.base import DType, ElementType | ||
|
||
# Unsigned Integer | ||
uint8 = DType("uint8", ElementType.UInt, 8) | ||
uint16 = DType("uint16", ElementType.UInt, 16) | ||
uint32 = DType("uint32", ElementType.UInt, 32) | ||
uint64 = DType("uint64", ElementType.UInt, 64) | ||
|
||
# Signed Integer | ||
int8 = DType("int8", ElementType.Int, 8, signed=True) | ||
int16 = DType("int16", ElementType.Int, 16, signed=True) | ||
int32 = DType("int32", ElementType.Int, 32, signed=True) | ||
int64 = DType("int64", ElementType.Int, 64, signed=True) | ||
|
||
# Float | ||
float16 = DType("float16", ElementType.Float, 16, signed=True) | ||
float32 = DType("float32", ElementType.Float, 32, signed=True) | ||
float64 = DType("float64", ElementType.Float, 64, signed=True) | ||
|
||
# Date/Time | ||
datetime64us = DType("datetime64[us]", ElementType.DateTime, 64) | ||
datetime64ns = DType("datetime64[ns]", ElementType.DateTime, 64) | ||
|
||
# Miscellaneous | ||
string = DType("str", ElementType.String) | ||
boolean = DType("bool", ElementType.Bool) | ||
object_ = DType("object", ElementType.Object) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# Copyright (c) 2022, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import numpy as np | ||
|
||
from merlin.dtype import dtypes | ||
from merlin.dtype.registry import _dtype_registry | ||
|
||
python_dtypes = { | ||
dtypes.boolean: bool, | ||
dtypes.int64: int, | ||
dtypes.float64: float, | ||
dtypes.string: str, | ||
} | ||
_dtype_registry.register("python", python_dtypes) | ||
|
||
|
||
numpy_dtypes = { | ||
# Unsigned Integer | ||
dtypes.uint8: [np.uint8, np.dtype("uint8")], | ||
dtypes.uint16: [np.uint16, np.dtype("uint16")], | ||
dtypes.uint32: [np.uint32, np.dtype("uint32")], | ||
dtypes.uint64: [np.uint64, np.dtype("uint64")], | ||
# Signed integer | ||
dtypes.int8: [np.int8, np.dtype("int8")], | ||
dtypes.int16: [np.int16, np.dtype("int16")], | ||
dtypes.int32: [np.int32, np.dtype("int32")], | ||
dtypes.int64: [np.int64, np.dtype("int64")], | ||
# Floating Point | ||
dtypes.float16: [np.float16, np.dtype("float16")], | ||
dtypes.float32: [np.float32, np.dtype("float32")], | ||
dtypes.float64: [np.float64, np.dtype("float64")], | ||
# Date/Time | ||
# TODO: Figure out which datetime64 unit is the default | ||
dtypes.datetime64ns: [np.datetime64, np.dtype("datetime64[ns]")], | ||
dtypes.datetime64us: [np.datetime64, np.dtype("datetime64[us]")], | ||
# Miscellaneous | ||
dtypes.string: [np.str], | ||
dtypes.object_: [np.dtype("O")], | ||
dtypes.boolean: [np.bool, np.dtype("bool")], | ||
} | ||
_dtype_registry.register("numpy", numpy_dtypes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# Copyright (c) 2022, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from typing import Dict, Union | ||
|
||
|
||
class DTypeMapping: | ||
def __init__(self, mapping): | ||
self.from_merlin = mapping | ||
self.to_merlin = {} | ||
|
||
for key, values in mapping.items(): | ||
if not isinstance(values, list): | ||
values = [values] | ||
for value in values: | ||
self.to_merlin[value] = key | ||
|
||
|
||
class DTypeMappingRegistry: | ||
def __init__(self): | ||
self.mappings = {} | ||
|
||
def __iter__(self): | ||
return iter(self.mappings) | ||
|
||
def register(self, name: str, mapping: Union[Dict, DTypeMapping]): | ||
if not isinstance(mapping, DTypeMapping): | ||
mapping = DTypeMapping(mapping) | ||
|
||
self.mappings[name] = mapping | ||
|
||
|
||
_dtype_registry = DTypeMappingRegistry() |
Oops, something went wrong.