0
# Data Type Management
1
2
Functions for manipulating and optimizing numpy data types to balance memory usage and value range requirements. These utilities help optimize memory consumption by selecting appropriate data types for your data's actual range.
3
4
## Capabilities
5
6
### Data Type Fitting
7
8
Find the smallest data type of the same kind that can accommodate a specific value.
9
10
```python { .api }
11
def fit_dtype(
12
dtype: DTypeLike,
13
value: Union[int, float, np.integer, np.unsignedinteger, np.complexfloating, np.floating],
14
exotics: bool = False
15
) -> DTypeLike:
16
"""
17
Find smallest dtype of same kind that can fit the given value.
18
19
Args:
20
dtype: Input data type
21
value: Value to fit
22
exotics: Allow exotic dtypes
23
24
Returns:
25
The fitted dtype
26
"""
27
```
28
29
**Usage Example:**
30
31
```python
32
import fastremap
33
import numpy as np
34
35
# Find dtype to fit a specific value
36
fitted_dtype = fastremap.fit_dtype(np.int32, 500)
37
# Result: np.int16 (since 500 fits in int16)
38
39
fitted_dtype = fastremap.fit_dtype(np.uint8, 300)
40
# Result: np.uint16 (since 300 doesn't fit in uint8)
41
```
42
43
### Data Type Narrowing
44
45
Reduce a data type to the next smaller size of the same type.
46
47
```python { .api }
48
def narrow_dtype(
49
dtype: DTypeLike,
50
exotics: bool = False
51
) -> DTypeLike:
52
"""
53
Narrow the given dtype to the next smaller size of the same type.
54
55
Args:
56
dtype: Input data type
57
exotics: Include exotic dtypes
58
59
Returns:
60
The narrowed dtype
61
"""
62
```
63
64
**Usage Example:**
65
66
```python
67
import fastremap
68
import numpy as np
69
70
# Narrow integer types
71
narrowed = fastremap.narrow_dtype(np.uint32)
72
# Result: np.uint16
73
74
narrowed = fastremap.narrow_dtype(np.int64)
75
# Result: np.int32
76
77
# Narrow float types
78
narrowed = fastremap.narrow_dtype(np.float64)
79
# Result: np.float32
80
```
81
82
### Data Type Widening
83
84
Expand a data type to the next larger size of the same type.
85
86
```python { .api }
87
def widen_dtype(
88
dtype: DTypeLike,
89
exotics: bool = False
90
) -> DTypeLike:
91
"""
92
Widen the given dtype to the next larger size of the same type.
93
94
Args:
95
dtype: Input data type
96
exotics: Include exotic dtypes
97
98
Returns:
99
The widened dtype
100
"""
101
```
102
103
**Usage Example:**
104
105
```python
106
import fastremap
107
import numpy as np
108
109
# Widen integer types
110
widened = fastremap.widen_dtype(np.uint16)
111
# Result: np.uint32
112
113
widened = fastremap.widen_dtype(np.int8)
114
# Result: np.int16
115
116
# Widen float types
117
widened = fastremap.widen_dtype(np.float32)
118
# Result: np.float64
119
```
120
121
## Types
122
123
```python { .api }
124
DTypeLike = Union[np.dtype, type, str]
125
```