dtypeflow 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dtypeflow-1.0.0/LICENSE +21 -0
- dtypeflow-1.0.0/PKG-INFO +93 -0
- dtypeflow-1.0.0/README.md +68 -0
- dtypeflow-1.0.0/pyproject.toml +46 -0
- dtypeflow-1.0.0/pyproject.toml.orig +35 -0
- dtypeflow-1.0.0/src/dtypeflow/__init__.py +5 -0
- dtypeflow-1.0.0/src/dtypeflow/core/__init__.py +1 -0
- dtypeflow-1.0.0/src/dtypeflow/core/candidate/__init__.py +40 -0
- dtypeflow-1.0.0/src/dtypeflow/core/candidate/base.py +37 -0
- dtypeflow-1.0.0/src/dtypeflow/core/candidate/factory.py +258 -0
- dtypeflow-1.0.0/src/dtypeflow/core/candidate/primitive.py +79 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dataset.py +46 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/__init__.py +70 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/base.py +14 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/hierachy.md +51 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/nested/__init__.py +5 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/nested/dictionary.py +14 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/__init__.py +39 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/base.py +6 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/boolean.py +20 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/float.py +40 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/integer/__init__.py +31 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/integer/base.py +17 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/integer/signed.py +37 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/integer/unsigned.py +37 -0
- dtypeflow-1.0.0/src/dtypeflow/core/dtype/primitive/string.py +14 -0
- dtypeflow-1.0.0/src/dtypeflow/core/override/__init__.py +41 -0
- dtypeflow-1.0.0/src/dtypeflow/core/override/base.py +22 -0
- dtypeflow-1.0.0/src/dtypeflow/core/override/primitive.py +158 -0
- dtypeflow-1.0.0/src/dtypeflow/core/profile/__init__.py +39 -0
- dtypeflow-1.0.0/src/dtypeflow/core/profile/base.py +50 -0
- dtypeflow-1.0.0/src/dtypeflow/core/profile/primitive.py +166 -0
- dtypeflow-1.0.0/src/dtypeflow/core/profiler.py +169 -0
- dtypeflow-1.0.0/src/dtypeflow/core/schema.py +33 -0
- dtypeflow-1.0.0/src/dtypeflow/core/table.py +29 -0
- dtypeflow-1.0.0/src/dtypeflow/py.typed +0 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/__init__.py +17 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/app.py +747 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/app.tcss +220 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/compatibility.py +38 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/dto.py +44 -0
- dtypeflow-1.0.0/src/dtypeflow/tui/service.py +213 -0
- dtypeflow-1.0.0/src/dtypeflow/utils.py +32 -0
dtypeflow-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pedro José Mora Gallegos
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dtypeflow-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dtypeflow
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Terminal UI to infer CSV column dtypes and convert CSV to Parquet, locally.
|
|
5
|
+
Keywords: csv,parquet,dtype,tui,textual,arrow
|
|
6
|
+
Author: Pedro José Mora Gallegos
|
|
7
|
+
Author-email: Pedro José Mora Gallegos <developmentbypedrojose@outlook.de>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Topic :: Utilities
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering
|
|
18
|
+
Requires-Dist: pyarrow>=17.0.0
|
|
19
|
+
Requires-Dist: textual>=0.89.0
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Project-URL: Homepage, https://github.com/pedrojosemoragallegos/dtypeflow
|
|
22
|
+
Project-URL: Repository, https://github.com/pedrojosemoragallegos/dtypeflow
|
|
23
|
+
Project-URL: Issues, https://github.com/pedrojosemoragallegos/dtypeflow/issues
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# dtypeflow
|
|
27
|
+
|
|
28
|
+
`dtypeflow` is a terminal UI to convert a CSV file to Parquet.
|
|
29
|
+
|
|
30
|
+
Point it at a CSV file, and for each column it infers the tightest-fitting
|
|
31
|
+
dtype (`Boolean`, unsigned/signed integers `UInt8`…`UInt64`/`Int8`…`Int64`,
|
|
32
|
+
floats `Float16`/`Float32`/`Float64`, `String`, or `Dictionary`), shows the
|
|
33
|
+
observed statistics, and lets you review or override the choice before
|
|
34
|
+
writing the result to Parquet — all locally, no data leaves your machine.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
uv tool install dtypeflow
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or with `pipx`:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pipx install dtypeflow
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Or with `pip`:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install dtypeflow
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
Launch the picker and choose a CSV file interactively:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
dtypeflow
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Or pass a path directly to skip straight to configuring column types:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
dtypeflow path/to/data.csv
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The workflow has three steps:
|
|
69
|
+
|
|
70
|
+
1. **Pick a file** — enter or browse for a CSV path.
|
|
71
|
+
2. **Configure types** — for each column, review the recommended dtype, its
|
|
72
|
+
stats (unique values, samples, range/precision/scale), and pick from
|
|
73
|
+
compatible alternatives grouped by safety (Safe / Too small / Discouraged).
|
|
74
|
+
3. **Export** — write the configured dataset to a Parquet file.
|
|
75
|
+
|
|
76
|
+
## Development
|
|
77
|
+
|
|
78
|
+
This project uses [uv](https://docs.astral.sh/uv/) for dependency management.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
uv sync
|
|
82
|
+
uv run dtypeflow
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Lint:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
uv run --with ruff ruff check src/
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# dtypeflow
|
|
2
|
+
|
|
3
|
+
`dtypeflow` is a terminal UI to convert a CSV file to Parquet.
|
|
4
|
+
|
|
5
|
+
Point it at a CSV file, and for each column it infers the tightest-fitting
|
|
6
|
+
dtype (`Boolean`, unsigned/signed integers `UInt8`…`UInt64`/`Int8`…`Int64`,
|
|
7
|
+
floats `Float16`/`Float32`/`Float64`, `String`, or `Dictionary`), shows the
|
|
8
|
+
observed statistics, and lets you review or override the choice before
|
|
9
|
+
writing the result to Parquet — all locally, no data leaves your machine.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv tool install dtypeflow
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Or with `pipx`:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pipx install dtypeflow
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or with `pip`:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install dtypeflow
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
Launch the picker and choose a CSV file interactively:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
dtypeflow
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Or pass a path directly to skip straight to configuring column types:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
dtypeflow path/to/data.csv
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The workflow has three steps:
|
|
44
|
+
|
|
45
|
+
1. **Pick a file** — enter or browse for a CSV path.
|
|
46
|
+
2. **Configure types** — for each column, review the recommended dtype, its
|
|
47
|
+
stats (unique values, samples, range/precision/scale), and pick from
|
|
48
|
+
compatible alternatives grouped by safety (Safe / Too small / Discouraged).
|
|
49
|
+
3. **Export** — write the configured dataset to a Parquet file.
|
|
50
|
+
|
|
51
|
+
## Development
|
|
52
|
+
|
|
53
|
+
This project uses [uv](https://docs.astral.sh/uv/) for dependency management.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
uv sync
|
|
57
|
+
uv run dtypeflow
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Lint:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
uv run --with ruff ruff check src/
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dtypeflow"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Terminal UI to infer CSV column dtypes and convert CSV to Parquet, locally."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
keywords = [
|
|
10
|
+
"csv",
|
|
11
|
+
"parquet",
|
|
12
|
+
"dtype",
|
|
13
|
+
"tui",
|
|
14
|
+
"textual",
|
|
15
|
+
"arrow",
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Environment :: Console",
|
|
20
|
+
"Intended Audience :: Developers",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Topic :: Utilities",
|
|
25
|
+
"Topic :: Scientific/Engineering",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"pyarrow>=17.0.0",
|
|
29
|
+
"textual>=0.89.0",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[[project.authors]]
|
|
33
|
+
name = "Pedro José Mora Gallegos"
|
|
34
|
+
email = "developmentbypedrojose@outlook.de"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/pedrojosemoragallegos/dtypeflow"
|
|
38
|
+
Repository = "https://github.com/pedrojosemoragallegos/dtypeflow"
|
|
39
|
+
Issues = "https://github.com/pedrojosemoragallegos/dtypeflow/issues"
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
dtypeflow = "dtypeflow.tui:run"
|
|
43
|
+
|
|
44
|
+
[build-system]
|
|
45
|
+
requires = ["uv_build>=0.12.9,<0.13.0"]
|
|
46
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "dtypeflow"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Terminal UI to infer CSV column dtypes and convert CSV to Parquet, locally."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Pedro José Mora Gallegos", email = "developmentbypedrojose@outlook.de" },
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
keywords = ["csv", "parquet", "dtype", "tui", "textual", "arrow"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Environment :: Console",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Topic :: Utilities",
|
|
21
|
+
"Topic :: Scientific/Engineering",
|
|
22
|
+
]
|
|
23
|
+
dependencies = ["pyarrow>=17.0.0", "textual>=0.89.0"]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/pedrojosemoragallegos/dtypeflow"
|
|
27
|
+
Repository = "https://github.com/pedrojosemoragallegos/dtypeflow"
|
|
28
|
+
Issues = "https://github.com/pedrojosemoragallegos/dtypeflow/issues"
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
dtypeflow = "dtypeflow.tui:run"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["uv_build>=0.12.9,<0.13.0"]
|
|
35
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .base import Candidate, Compatibility
|
|
4
|
+
from .factory import candidate_factory
|
|
5
|
+
from .primitive import (
|
|
6
|
+
Boolean as BooleanCandidate,
|
|
7
|
+
Dictionary as DictionaryCandidate,
|
|
8
|
+
Float16 as Float16Candidate,
|
|
9
|
+
Float32 as Float32Candidate,
|
|
10
|
+
Float64 as Float64Candidate,
|
|
11
|
+
Int8 as Int8Candidate,
|
|
12
|
+
Int16 as Int16Candidate,
|
|
13
|
+
Int32 as Int32Candidate,
|
|
14
|
+
Int64 as Int64Candidate,
|
|
15
|
+
String as StringCandidate,
|
|
16
|
+
UInt8 as UInt8Candidate,
|
|
17
|
+
UInt16 as UInt16Candidate,
|
|
18
|
+
UInt32 as UInt32Candidate,
|
|
19
|
+
UInt64 as UInt64Candidate,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__: list[str] = [
|
|
23
|
+
"BooleanCandidate",
|
|
24
|
+
"Candidate",
|
|
25
|
+
"Compatibility",
|
|
26
|
+
"DictionaryCandidate",
|
|
27
|
+
"Float16Candidate",
|
|
28
|
+
"Float32Candidate",
|
|
29
|
+
"Float64Candidate",
|
|
30
|
+
"Int8Candidate",
|
|
31
|
+
"Int16Candidate",
|
|
32
|
+
"Int32Candidate",
|
|
33
|
+
"Int64Candidate",
|
|
34
|
+
"StringCandidate",
|
|
35
|
+
"UInt8Candidate",
|
|
36
|
+
"UInt16Candidate",
|
|
37
|
+
"UInt32Candidate",
|
|
38
|
+
"UInt64Candidate",
|
|
39
|
+
"candidate_factory",
|
|
40
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC
|
|
4
|
+
from enum import Enum, auto
|
|
5
|
+
from typing import TYPE_CHECKING, ClassVar
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from dtypeflow.core.dtype import Dtype
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Compatibility(Enum):
|
|
12
|
+
FULL_RANGE_FIT = auto()
|
|
13
|
+
VALUES_PRESERVED = auto()
|
|
14
|
+
VALUES_NO_FIT = auto()
|
|
15
|
+
RANGE_NO_FIT = auto()
|
|
16
|
+
UNEVALUATED_NO_FIT = auto()
|
|
17
|
+
NOT_NUMERIC = auto()
|
|
18
|
+
OBSERVED_ONLY_FIT = auto()
|
|
19
|
+
DECIMAL_TRUNCATION = auto()
|
|
20
|
+
PRECISION_REDUCTION = auto()
|
|
21
|
+
BOOLEAN_MAPPING = auto()
|
|
22
|
+
DICTIONARY_ENCODING = auto()
|
|
23
|
+
DICTIONARY_INEFFICIENT = auto()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Candidate(ABC):
|
|
27
|
+
DTYPE: ClassVar[type[Dtype]]
|
|
28
|
+
|
|
29
|
+
def __init__(self, compatibility: Compatibility, /) -> None:
|
|
30
|
+
self._compatibility: Compatibility = compatibility
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def compatibility(self) -> Compatibility:
|
|
34
|
+
return self._compatibility
|
|
35
|
+
|
|
36
|
+
def __repr__(self) -> str:
|
|
37
|
+
return f"{type(self).__name__}(compatibility={self._compatibility.name})"
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from decimal import Decimal
|
|
4
|
+
from typing import Final, overload
|
|
5
|
+
|
|
6
|
+
from dtypeflow.core.dtype import (
|
|
7
|
+
ALL_TYPES,
|
|
8
|
+
BooleanDtype,
|
|
9
|
+
DictionaryDtype,
|
|
10
|
+
Dtype,
|
|
11
|
+
Float16Dtype,
|
|
12
|
+
Float32Dtype,
|
|
13
|
+
Float64Dtype,
|
|
14
|
+
FloatDtype,
|
|
15
|
+
Int8Dtype,
|
|
16
|
+
Int16Dtype,
|
|
17
|
+
Int32Dtype,
|
|
18
|
+
Int64Dtype,
|
|
19
|
+
IntegerDtype,
|
|
20
|
+
StringDtype,
|
|
21
|
+
UInt8Dtype,
|
|
22
|
+
UInt16Dtype,
|
|
23
|
+
UInt32Dtype,
|
|
24
|
+
UInt64Dtype,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
from .base import Candidate, Compatibility
|
|
28
|
+
from .primitive import (
|
|
29
|
+
Boolean as BooleanCandidate,
|
|
30
|
+
Dictionary as DictionaryCandidate,
|
|
31
|
+
Float16 as Float16Candidate,
|
|
32
|
+
Float32 as Float32Candidate,
|
|
33
|
+
Float64 as Float64Candidate,
|
|
34
|
+
Int8 as Int8Candidate,
|
|
35
|
+
Int16 as Int16Candidate,
|
|
36
|
+
Int32 as Int32Candidate,
|
|
37
|
+
Int64 as Int64Candidate,
|
|
38
|
+
String as StringCandidate,
|
|
39
|
+
UInt8 as UInt8Candidate,
|
|
40
|
+
UInt16 as UInt16Candidate,
|
|
41
|
+
UInt32 as UInt32Candidate,
|
|
42
|
+
UInt64 as UInt64Candidate,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
_MAX_BOOLEAN_UNIQUE: Final[int] = 2
|
|
46
|
+
_MAX_DICTIONARY_ABSOLUTE: Final[int] = 20
|
|
47
|
+
_MAX_DICTIONARY_RATIO: Final[float] = 0.05
|
|
48
|
+
_CANDIDATE_MAPPER: Final[dict[type[Dtype], type[Candidate]]] = {
|
|
49
|
+
BooleanDtype: BooleanCandidate,
|
|
50
|
+
UInt8Dtype: UInt8Candidate,
|
|
51
|
+
UInt16Dtype: UInt16Candidate,
|
|
52
|
+
UInt32Dtype: UInt32Candidate,
|
|
53
|
+
UInt64Dtype: UInt64Candidate,
|
|
54
|
+
Int8Dtype: Int8Candidate,
|
|
55
|
+
Int16Dtype: Int16Candidate,
|
|
56
|
+
Int32Dtype: Int32Candidate,
|
|
57
|
+
Int64Dtype: Int64Candidate,
|
|
58
|
+
Float16Dtype: Float16Candidate,
|
|
59
|
+
Float32Dtype: Float32Candidate,
|
|
60
|
+
Float64Dtype: Float64Candidate,
|
|
61
|
+
StringDtype: StringCandidate,
|
|
62
|
+
DictionaryDtype: DictionaryCandidate,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _full_bounds(dtype: type[Dtype], /) -> tuple[Decimal, Decimal] | None:
|
|
67
|
+
if issubclass(dtype, BooleanDtype):
|
|
68
|
+
return (Decimal(0), Decimal(1))
|
|
69
|
+
if issubclass(dtype, IntegerDtype):
|
|
70
|
+
return (Decimal(dtype.LOWER_BOUND), Decimal(dtype.HIGHER_BOUND))
|
|
71
|
+
if issubclass(dtype, FloatDtype):
|
|
72
|
+
return (-dtype.MAX_ABSOLUTE_VALUE, dtype.MAX_ABSOLUTE_VALUE)
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _full_precision(dtype: type[Dtype], /) -> int:
|
|
77
|
+
if issubclass(dtype, FloatDtype):
|
|
78
|
+
return dtype.MAX_SIGNIFICANT_DIGITS
|
|
79
|
+
bounds: tuple[Decimal, Decimal] | None = _full_bounds(dtype)
|
|
80
|
+
if bounds is None:
|
|
81
|
+
return 0
|
|
82
|
+
return len(str(int(max(abs(bounds[0]), abs(bounds[1])))))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _boolean_compatibility(num_unique: int | None, /) -> Compatibility:
|
|
86
|
+
if num_unique is not None and num_unique <= _MAX_BOOLEAN_UNIQUE:
|
|
87
|
+
return Compatibility.BOOLEAN_MAPPING
|
|
88
|
+
return Compatibility.VALUES_NO_FIT
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _dictionary_compatibility(
|
|
92
|
+
num_unique: int | None, total: int | None, /,
|
|
93
|
+
) -> Compatibility:
|
|
94
|
+
eligible: bool = num_unique is not None and (
|
|
95
|
+
num_unique <= _MAX_DICTIONARY_ABSOLUTE
|
|
96
|
+
or (
|
|
97
|
+
total is not None
|
|
98
|
+
and total > 0
|
|
99
|
+
and (num_unique / total <= _MAX_DICTIONARY_RATIO)
|
|
100
|
+
)
|
|
101
|
+
)
|
|
102
|
+
if not eligible:
|
|
103
|
+
return Compatibility.DICTIONARY_INEFFICIENT
|
|
104
|
+
return Compatibility.DICTIONARY_ENCODING
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _fits_full_range(source: type[Dtype], target: type[Dtype], /) -> bool:
|
|
108
|
+
source_bounds: tuple[Decimal, Decimal] | None = _full_bounds(source)
|
|
109
|
+
target_bounds: tuple[Decimal, Decimal] | None = _full_bounds(target)
|
|
110
|
+
return (
|
|
111
|
+
source_bounds is not None
|
|
112
|
+
and target_bounds is not None
|
|
113
|
+
and (target_bounds[0] <= source_bounds[0])
|
|
114
|
+
and (source_bounds[1] <= target_bounds[1])
|
|
115
|
+
and (
|
|
116
|
+
not issubclass(target, FloatDtype)
|
|
117
|
+
or _full_precision(source) <= target.MAX_SIGNIFICANT_DIGITS
|
|
118
|
+
)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _exceeds_precision(
|
|
123
|
+
target: type[FloatDtype],
|
|
124
|
+
min_value: int | Decimal,
|
|
125
|
+
max_value: int | Decimal,
|
|
126
|
+
precision: int | None,
|
|
127
|
+
/,
|
|
128
|
+
) -> bool:
|
|
129
|
+
if precision is not None:
|
|
130
|
+
observed_precision: int = precision
|
|
131
|
+
else:
|
|
132
|
+
bound: Decimal | int = max(abs(min_value), abs(max_value))
|
|
133
|
+
observed_precision = len(str(int(bound)))
|
|
134
|
+
return observed_precision > target.MAX_SIGNIFICANT_DIGITS
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _unevaluated_compatibility(*, full_range_fits: bool) -> Compatibility:
|
|
138
|
+
if full_range_fits:
|
|
139
|
+
return Compatibility.FULL_RANGE_FIT
|
|
140
|
+
return Compatibility.UNEVALUATED_NO_FIT
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _observed_compatibility(*, full_range_fits: bool) -> Compatibility:
|
|
144
|
+
if full_range_fits:
|
|
145
|
+
return Compatibility.FULL_RANGE_FIT
|
|
146
|
+
return Compatibility.OBSERVED_ONLY_FIT
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _range_compatibility(
|
|
150
|
+
source: type[Dtype],
|
|
151
|
+
target: type[Dtype],
|
|
152
|
+
/,
|
|
153
|
+
*,
|
|
154
|
+
min_value: int | Decimal | None,
|
|
155
|
+
max_value: int | Decimal | None,
|
|
156
|
+
precision: int | None,
|
|
157
|
+
scale: int | None,
|
|
158
|
+
) -> Compatibility:
|
|
159
|
+
full_range_fits: bool = _fits_full_range(source, target)
|
|
160
|
+
target_bounds: tuple[Decimal, Decimal] | None = _full_bounds(target)
|
|
161
|
+
if min_value is None or max_value is None or target_bounds is None:
|
|
162
|
+
if issubclass(source, StringDtype):
|
|
163
|
+
return Compatibility.NOT_NUMERIC
|
|
164
|
+
return _unevaluated_compatibility(full_range_fits=full_range_fits)
|
|
165
|
+
if issubclass(target, IntegerDtype) and (scale or 0) > 0:
|
|
166
|
+
return Compatibility.DECIMAL_TRUNCATION
|
|
167
|
+
if not (target_bounds[0] <= min_value and max_value <= target_bounds[1]):
|
|
168
|
+
return Compatibility.RANGE_NO_FIT
|
|
169
|
+
if issubclass(target, FloatDtype) and _exceeds_precision(
|
|
170
|
+
target, min_value, max_value, precision,
|
|
171
|
+
):
|
|
172
|
+
return Compatibility.PRECISION_REDUCTION
|
|
173
|
+
return _observed_compatibility(full_range_fits=full_range_fits)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _compatibility(
|
|
177
|
+
source: type[Dtype],
|
|
178
|
+
target: type[Dtype],
|
|
179
|
+
/,
|
|
180
|
+
*,
|
|
181
|
+
num_unique: int | None,
|
|
182
|
+
total: int | None,
|
|
183
|
+
min_value: int | Decimal | None,
|
|
184
|
+
max_value: int | Decimal | None,
|
|
185
|
+
precision: int | None,
|
|
186
|
+
scale: int | None,
|
|
187
|
+
) -> Compatibility:
|
|
188
|
+
if issubclass(target, StringDtype):
|
|
189
|
+
return Compatibility.VALUES_PRESERVED
|
|
190
|
+
if issubclass(target, BooleanDtype):
|
|
191
|
+
return _boolean_compatibility(num_unique)
|
|
192
|
+
if issubclass(target, DictionaryDtype):
|
|
193
|
+
return _dictionary_compatibility(num_unique, total)
|
|
194
|
+
return _range_compatibility(
|
|
195
|
+
source,
|
|
196
|
+
target,
|
|
197
|
+
min_value=min_value,
|
|
198
|
+
max_value=max_value,
|
|
199
|
+
precision=precision,
|
|
200
|
+
scale=scale,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
@overload
|
|
205
|
+
def candidate_factory(dtype: type[Dtype], /) -> tuple[Candidate, ...]: ...
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
@overload
|
|
209
|
+
def candidate_factory(
|
|
210
|
+
dtype: type[Dtype], /, *, num_unique: int, total: int | None = None,
|
|
211
|
+
) -> tuple[Candidate, ...]: ...
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@overload
|
|
215
|
+
def candidate_factory(
|
|
216
|
+
dtype: type[Dtype],
|
|
217
|
+
/,
|
|
218
|
+
*,
|
|
219
|
+
num_unique: int,
|
|
220
|
+
total: int | None = None,
|
|
221
|
+
min_value: int | Decimal,
|
|
222
|
+
max_value: int | Decimal,
|
|
223
|
+
precision: int | None = None,
|
|
224
|
+
scale: int | None = None,
|
|
225
|
+
) -> tuple[Candidate, ...]: ...
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def candidate_factory(
|
|
229
|
+
dtype: type[Dtype],
|
|
230
|
+
/,
|
|
231
|
+
*,
|
|
232
|
+
num_unique: int | None = None,
|
|
233
|
+
total: int | None = None,
|
|
234
|
+
min_value: int | Decimal | None = None,
|
|
235
|
+
max_value: int | Decimal | None = None,
|
|
236
|
+
precision: int | None = None,
|
|
237
|
+
scale: int | None = None,
|
|
238
|
+
) -> tuple[Candidate, ...]:
|
|
239
|
+
if dtype not in ALL_TYPES:
|
|
240
|
+
msg = f"Unsupported dtype: {dtype}"
|
|
241
|
+
raise ValueError(msg)
|
|
242
|
+
candidate_dtypes: tuple[type[Dtype], ...] = tuple(
|
|
243
|
+
target for target in ALL_TYPES if target is not dtype
|
|
244
|
+
)
|
|
245
|
+
candidates: list[Candidate] = []
|
|
246
|
+
for target in candidate_dtypes:
|
|
247
|
+
compatibility: Compatibility = _compatibility(
|
|
248
|
+
dtype,
|
|
249
|
+
target,
|
|
250
|
+
num_unique=num_unique,
|
|
251
|
+
total=total,
|
|
252
|
+
min_value=min_value,
|
|
253
|
+
max_value=max_value,
|
|
254
|
+
precision=precision,
|
|
255
|
+
scale=scale,
|
|
256
|
+
)
|
|
257
|
+
candidates.append(_CANDIDATE_MAPPER[target](compatibility))
|
|
258
|
+
return tuple(candidates)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Final
|
|
4
|
+
|
|
5
|
+
from dtypeflow.core.dtype import (
|
|
6
|
+
BooleanDtype,
|
|
7
|
+
DictionaryDtype,
|
|
8
|
+
Dtype,
|
|
9
|
+
Float16Dtype,
|
|
10
|
+
Float32Dtype,
|
|
11
|
+
Float64Dtype,
|
|
12
|
+
Int8Dtype,
|
|
13
|
+
Int16Dtype,
|
|
14
|
+
Int32Dtype,
|
|
15
|
+
Int64Dtype,
|
|
16
|
+
StringDtype,
|
|
17
|
+
UInt8Dtype,
|
|
18
|
+
UInt16Dtype,
|
|
19
|
+
UInt32Dtype,
|
|
20
|
+
UInt64Dtype,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
from .base import Candidate
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Boolean(Candidate):
|
|
27
|
+
DTYPE: Final[type[Dtype]] = BooleanDtype
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class UInt8(Candidate):
|
|
31
|
+
DTYPE: Final[type[Dtype]] = UInt8Dtype
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class UInt16(Candidate):
|
|
35
|
+
DTYPE: Final[type[Dtype]] = UInt16Dtype
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class UInt32(Candidate):
|
|
39
|
+
DTYPE: Final[type[Dtype]] = UInt32Dtype
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class UInt64(Candidate):
|
|
43
|
+
DTYPE: Final[type[Dtype]] = UInt64Dtype
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Int8(Candidate):
|
|
47
|
+
DTYPE: Final[type[Dtype]] = Int8Dtype
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class Int16(Candidate):
|
|
51
|
+
DTYPE: Final[type[Dtype]] = Int16Dtype
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class Int32(Candidate):
|
|
55
|
+
DTYPE: Final[type[Dtype]] = Int32Dtype
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Int64(Candidate):
|
|
59
|
+
DTYPE: Final[type[Dtype]] = Int64Dtype
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Float16(Candidate):
|
|
63
|
+
DTYPE: Final[type[Dtype]] = Float16Dtype
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Float32(Candidate):
|
|
67
|
+
DTYPE: Final[type[Dtype]] = Float32Dtype
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class Float64(Candidate):
|
|
71
|
+
DTYPE: Final[type[Dtype]] = Float64Dtype
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class String(Candidate):
|
|
75
|
+
DTYPE: Final[type[Dtype]] = StringDtype
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class Dictionary(Candidate):
|
|
79
|
+
DTYPE: Final[type[Dtype]] = DictionaryDtype
|