torchwrench 0.7.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.
Files changed (135) hide show
  1. torchwrench-0.7.0/LICENSE +21 -0
  2. torchwrench-0.7.0/PKG-INFO +306 -0
  3. torchwrench-0.7.0/README.md +225 -0
  4. torchwrench-0.7.0/pyproject.toml +104 -0
  5. torchwrench-0.7.0/setup.cfg +4 -0
  6. torchwrench-0.7.0/setup.py +7 -0
  7. torchwrench-0.7.0/src/torchwrench/__init__.py +167 -0
  8. torchwrench-0.7.0/src/torchwrench/__main__.py +7 -0
  9. torchwrench-0.7.0/src/torchwrench/core/__init__.py +2 -0
  10. torchwrench-0.7.0/src/torchwrench/core/dtype_enum.py +190 -0
  11. torchwrench-0.7.0/src/torchwrench/core/make.py +117 -0
  12. torchwrench-0.7.0/src/torchwrench/core/packaging.py +75 -0
  13. torchwrench-0.7.0/src/torchwrench/core/version.py +24 -0
  14. torchwrench-0.7.0/src/torchwrench/entries.py +49 -0
  15. torchwrench-0.7.0/src/torchwrench/extras/__init__.py +16 -0
  16. torchwrench-0.7.0/src/torchwrench/extras/colorlog.py +43 -0
  17. torchwrench-0.7.0/src/torchwrench/extras/hdf/__init__.py +26 -0
  18. torchwrench-0.7.0/src/torchwrench/extras/hdf/common.py +76 -0
  19. torchwrench-0.7.0/src/torchwrench/extras/hdf/dataset.py +690 -0
  20. torchwrench-0.7.0/src/torchwrench/extras/hdf/pack.py +621 -0
  21. torchwrench-0.7.0/src/torchwrench/extras/numpy/__init__.py +40 -0
  22. torchwrench-0.7.0/src/torchwrench/extras/numpy/_numpy_fallback.py +82 -0
  23. torchwrench-0.7.0/src/torchwrench/extras/numpy/definitions.py +37 -0
  24. torchwrench-0.7.0/src/torchwrench/extras/numpy/functional.py +219 -0
  25. torchwrench-0.7.0/src/torchwrench/extras/numpy/saving.py +56 -0
  26. torchwrench-0.7.0/src/torchwrench/extras/numpy/scan_info.py +216 -0
  27. torchwrench-0.7.0/src/torchwrench/extras/safetensors.py +99 -0
  28. torchwrench-0.7.0/src/torchwrench/extras/tensorboard.py +176 -0
  29. torchwrench-0.7.0/src/torchwrench/extras/torchaudio.py +104 -0
  30. torchwrench-0.7.0/src/torchwrench/extras/yaml.py +221 -0
  31. torchwrench-0.7.0/src/torchwrench/hub/__init__.py +8 -0
  32. torchwrench-0.7.0/src/torchwrench/hub/download.py +97 -0
  33. torchwrench-0.7.0/src/torchwrench/hub/paths.py +41 -0
  34. torchwrench-0.7.0/src/torchwrench/hub/registry.py +254 -0
  35. torchwrench-0.7.0/src/torchwrench/nn/__init__.py +7 -0
  36. torchwrench-0.7.0/src/torchwrench/nn/functional/__init__.py +131 -0
  37. torchwrench-0.7.0/src/torchwrench/nn/functional/activation.py +28 -0
  38. torchwrench-0.7.0/src/torchwrench/nn/functional/checksum.py +170 -0
  39. torchwrench-0.7.0/src/torchwrench/nn/functional/cropping.py +93 -0
  40. torchwrench-0.7.0/src/torchwrench/nn/functional/indices.py +163 -0
  41. torchwrench-0.7.0/src/torchwrench/nn/functional/make.py +17 -0
  42. torchwrench-0.7.0/src/torchwrench/nn/functional/mask.py +401 -0
  43. torchwrench-0.7.0/src/torchwrench/nn/functional/multiclass.py +265 -0
  44. torchwrench-0.7.0/src/torchwrench/nn/functional/multilabel.py +337 -0
  45. torchwrench-0.7.0/src/torchwrench/nn/functional/new.py +1369 -0
  46. torchwrench-0.7.0/src/torchwrench/nn/functional/others.py +430 -0
  47. torchwrench-0.7.0/src/torchwrench/nn/functional/padding.py +305 -0
  48. torchwrench-0.7.0/src/torchwrench/nn/functional/powerset.py +177 -0
  49. torchwrench-0.7.0/src/torchwrench/nn/functional/predicate.py +264 -0
  50. torchwrench-0.7.0/src/torchwrench/nn/functional/segments.py +203 -0
  51. torchwrench-0.7.0/src/torchwrench/nn/functional/transform.py +1048 -0
  52. torchwrench-0.7.0/src/torchwrench/nn/modules/__init__.py +95 -0
  53. torchwrench-0.7.0/src/torchwrench/nn/modules/_mixins.py +698 -0
  54. torchwrench-0.7.0/src/torchwrench/nn/modules/activation.py +55 -0
  55. torchwrench-0.7.0/src/torchwrench/nn/modules/container.py +207 -0
  56. torchwrench-0.7.0/src/torchwrench/nn/modules/crop.py +88 -0
  57. torchwrench-0.7.0/src/torchwrench/nn/modules/layer.py +65 -0
  58. torchwrench-0.7.0/src/torchwrench/nn/modules/mask.py +55 -0
  59. torchwrench-0.7.0/src/torchwrench/nn/modules/module.py +5 -0
  60. torchwrench-0.7.0/src/torchwrench/nn/modules/multiclass.py +277 -0
  61. torchwrench-0.7.0/src/torchwrench/nn/modules/multilabel.py +292 -0
  62. torchwrench-0.7.0/src/torchwrench/nn/modules/numpy.py +85 -0
  63. torchwrench-0.7.0/src/torchwrench/nn/modules/pad.py +160 -0
  64. torchwrench-0.7.0/src/torchwrench/nn/modules/powerset.py +82 -0
  65. torchwrench-0.7.0/src/torchwrench/nn/modules/tensor.py +566 -0
  66. torchwrench-0.7.0/src/torchwrench/nn/modules/transform.py +521 -0
  67. torchwrench-0.7.0/src/torchwrench/optim/__init__.py +6 -0
  68. torchwrench-0.7.0/src/torchwrench/optim/schedulers.py +43 -0
  69. torchwrench-0.7.0/src/torchwrench/optim/utils.py +68 -0
  70. torchwrench-0.7.0/src/torchwrench/serialization/__init__.py +12 -0
  71. torchwrench-0.7.0/src/torchwrench/serialization/common.py +302 -0
  72. torchwrench-0.7.0/src/torchwrench/serialization/csv.py +237 -0
  73. torchwrench-0.7.0/src/torchwrench/serialization/dump_fn.py +110 -0
  74. torchwrench-0.7.0/src/torchwrench/serialization/hdf.py +14 -0
  75. torchwrench-0.7.0/src/torchwrench/serialization/json.py +37 -0
  76. torchwrench-0.7.0/src/torchwrench/serialization/load_fn.py +120 -0
  77. torchwrench-0.7.0/src/torchwrench/serialization/numpy.py +4 -0
  78. torchwrench-0.7.0/src/torchwrench/serialization/pickle.py +25 -0
  79. torchwrench-0.7.0/src/torchwrench/serialization/safetensors.py +8 -0
  80. torchwrench-0.7.0/src/torchwrench/serialization/torch.py +89 -0
  81. torchwrench-0.7.0/src/torchwrench/serialization/torchaudio.py +7 -0
  82. torchwrench-0.7.0/src/torchwrench/serialization/yaml.py +9 -0
  83. torchwrench-0.7.0/src/torchwrench/special/__init__.py +4 -0
  84. torchwrench-0.7.0/src/torchwrench/types/__init__.py +97 -0
  85. torchwrench-0.7.0/src/torchwrench/types/_typing.py +117 -0
  86. torchwrench-0.7.0/src/torchwrench/types/guards.py +54 -0
  87. torchwrench-0.7.0/src/torchwrench/types/tensor_subclasses.py +2604 -0
  88. torchwrench-0.7.0/src/torchwrench/types/variable_fns.py +17 -0
  89. torchwrench-0.7.0/src/torchwrench/utils/__init__.py +2 -0
  90. torchwrench-0.7.0/src/torchwrench/utils/data/__init__.py +17 -0
  91. torchwrench-0.7.0/src/torchwrench/utils/data/collate.py +81 -0
  92. torchwrench-0.7.0/src/torchwrench/utils/data/dataloader.py +22 -0
  93. torchwrench-0.7.0/src/torchwrench/utils/data/dataset.py +210 -0
  94. torchwrench-0.7.0/src/torchwrench/utils/data/slicer.py +177 -0
  95. torchwrench-0.7.0/src/torchwrench/utils/data/split.py +155 -0
  96. torchwrench-0.7.0/src/torchwrench/utils/return_types.py +73 -0
  97. torchwrench-0.7.0/src/torchwrench.egg-info/PKG-INFO +306 -0
  98. torchwrench-0.7.0/src/torchwrench.egg-info/SOURCES.txt +133 -0
  99. torchwrench-0.7.0/src/torchwrench.egg-info/dependency_links.txt +1 -0
  100. torchwrench-0.7.0/src/torchwrench.egg-info/entry_points.txt +2 -0
  101. torchwrench-0.7.0/src/torchwrench.egg-info/requires.txt +33 -0
  102. torchwrench-0.7.0/src/torchwrench.egg-info/top_level.txt +1 -0
  103. torchwrench-0.7.0/tests/test_compat.py +113 -0
  104. torchwrench-0.7.0/tests/test_core_dtype_enum.py +60 -0
  105. torchwrench-0.7.0/tests/test_extras_hdf.py +342 -0
  106. torchwrench-0.7.0/tests/test_extras_numpy_functional.py +80 -0
  107. torchwrench-0.7.0/tests/test_extras_numpy_scan_info.py +52 -0
  108. torchwrench-0.7.0/tests/test_extras_yaml.py +50 -0
  109. torchwrench-0.7.0/tests/test_hub.py +69 -0
  110. torchwrench-0.7.0/tests/test_nn_functional_checksum.py +82 -0
  111. torchwrench-0.7.0/tests/test_nn_functional_crop.py +21 -0
  112. torchwrench-0.7.0/tests/test_nn_functional_indices.py +68 -0
  113. torchwrench-0.7.0/tests/test_nn_functional_mask.py +336 -0
  114. torchwrench-0.7.0/tests/test_nn_functional_multiclass.py +71 -0
  115. torchwrench-0.7.0/tests/test_nn_functional_multilabel.py +195 -0
  116. torchwrench-0.7.0/tests/test_nn_functional_others.py +150 -0
  117. torchwrench-0.7.0/tests/test_nn_functional_pad.py +185 -0
  118. torchwrench-0.7.0/tests/test_nn_functional_powerset.py +37 -0
  119. torchwrench-0.7.0/tests/test_nn_functional_predicate.py +109 -0
  120. torchwrench-0.7.0/tests/test_nn_functional_segments.py +74 -0
  121. torchwrench-0.7.0/tests/test_nn_functional_transform.py +162 -0
  122. torchwrench-0.7.0/tests/test_nn_modules.py +84 -0
  123. torchwrench-0.7.0/tests/test_nn_modules_mixins.py +89 -0
  124. torchwrench-0.7.0/tests/test_nn_modules_multiclass.py +47 -0
  125. torchwrench-0.7.0/tests/test_nn_modules_multilabel.py +46 -0
  126. torchwrench-0.7.0/tests/test_nn_modules_tensor.py +103 -0
  127. torchwrench-0.7.0/tests/test_nn_modules_transform.py +29 -0
  128. torchwrench-0.7.0/tests/test_readme.py +85 -0
  129. torchwrench-0.7.0/tests/test_serialization.py +146 -0
  130. torchwrench-0.7.0/tests/test_types_guards.py +142 -0
  131. torchwrench-0.7.0/tests/test_types_tensor_subclasses.py +297 -0
  132. torchwrench-0.7.0/tests/test_utils_data_collate.py +26 -0
  133. torchwrench-0.7.0/tests/test_utils_data_dataset.py +31 -0
  134. torchwrench-0.7.0/tests/test_utils_data_slicer.py +81 -0
  135. torchwrench-0.7.0/tests/test_utils_data_split.py +30 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Labbeti
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.
@@ -0,0 +1,306 @@
1
+ Metadata-Version: 2.4
2
+ Name: torchwrench
3
+ Version: 0.7.0
4
+ Summary: Collection of functions and modules to help development in PyTorch.
5
+ Author-email: "Étienne Labbé (Labbeti)" <labbeti.pub@gmail.com>
6
+ Maintainer-email: "Étienne Labbé (Labbeti)" <labbeti.pub@gmail.com>
7
+ License: MIT License
8
+
9
+ Copyright (c) 2025 Labbeti
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+
29
+ Project-URL: Homepage, https://pypi.org/project/torchwrench/
30
+ Project-URL: Documentation, https://torchwrench.readthedocs.io/
31
+ Project-URL: Repository, https://github.com/Labbeti/torchwrench.git
32
+ Project-URL: Changelog, https://github.com/Labbeti/torchwrench/blob/main/CHANGELOG.md
33
+ Project-URL: Tracker, https://github.com/Labbeti/torchwrench/issues
34
+ Keywords: pytorch,deep-learning
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: Intended Audience :: Science/Research
37
+ Classifier: Programming Language :: Python :: 3
38
+ Classifier: Programming Language :: Python :: 3.9
39
+ Classifier: Programming Language :: Python :: 3.10
40
+ Classifier: Programming Language :: Python :: 3.11
41
+ Classifier: Programming Language :: Python :: 3.12
42
+ Classifier: Programming Language :: Python :: 3.13
43
+ Classifier: Topic :: Scientific/Engineering
44
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
45
+ Classifier: Operating System :: OS Independent
46
+ Requires-Python: >=3.9
47
+ Description-Content-Type: text/markdown
48
+ License-File: LICENSE
49
+ Requires-Dist: pythonwrench~=0.1.0
50
+ Requires-Dist: torch>=1.10.0
51
+ Requires-Dist: typing-extensions>=4.10.0
52
+ Provides-Extra: dev
53
+ Requires-Dist: coverage[toml]>=7.9.1; extra == "dev"
54
+ Requires-Dist: flake8>=7.2.0; extra == "dev"
55
+ Requires-Dist: ipython>=8.18.1; extra == "dev"
56
+ Requires-Dist: pre-commit>=4.2.0; extra == "dev"
57
+ Requires-Dist: pytest>=8.4.1; extra == "dev"
58
+ Requires-Dist: ruff>=0.12.0; extra == "dev"
59
+ Requires-Dist: setuptools>=80.9.0; extra == "dev"
60
+ Requires-Dist: soundfile>=0.13.1; extra == "dev"
61
+ Requires-Dist: sphinx>=7.4.7; extra == "dev"
62
+ Requires-Dist: sphinx-press-theme>=0.8.0; extra == "dev"
63
+ Requires-Dist: twine>=6.1.0; extra == "dev"
64
+ Requires-Dist: typing-extensions>=4.10.0; extra == "dev"
65
+ Requires-Dist: ipykernel>=6.29.5; extra == "dev"
66
+ Requires-Dist: numpy!=2.0.0,!=2.0.1,!=2.0.2; extra == "dev"
67
+ Provides-Extra: extras
68
+ Requires-Dist: colorlog>=6.9.0; extra == "extras"
69
+ Requires-Dist: h5py>=3.14.0; extra == "extras"
70
+ Requires-Dist: numpy!=2.0.0,!=2.0.1,!=2.0.2; extra == "extras"
71
+ Requires-Dist: omegaconf>=2.3.0; extra == "extras"
72
+ Requires-Dist: pandas>=2.3.0; extra == "extras"
73
+ Requires-Dist: pyyaml>=6.0.2; extra == "extras"
74
+ Requires-Dist: safetensors>=0.5.3; extra == "extras"
75
+ Requires-Dist: scipy>=1.13.1; extra == "extras"
76
+ Requires-Dist: tensorboard>=2.19.0; extra == "extras"
77
+ Requires-Dist: torchaudio>=0.10.0; extra == "extras"
78
+ Requires-Dist: torchvision>=0.11.0; extra == "extras"
79
+ Requires-Dist: tqdm>=4.67.1; extra == "extras"
80
+ Dynamic: license-file
81
+
82
+ # torchwrench
83
+
84
+ <center>
85
+
86
+ <a href="https://www.python.org/">
87
+ <img alt="Python" src="https://img.shields.io/badge/-Python 3.9+-blue?style=for-the-badge&logo=python&logoColor=white">
88
+ </a>
89
+ <a href="https://github.com/Labbeti/torchwrench/actions">
90
+ <img alt="Build" src="https://img.shields.io/github/actions/workflow/status/Labbeti/torchwrench/test.yaml?branch=main&style=for-the-badge&logo=github">
91
+ </a>
92
+ <a href='https://torchwrench.readthedocs.io/'>
93
+ <img src='https://readthedocs.org/projects/torchwrench/badge/?version=stable&style=for-the-badge' alt='Documentation Status' />
94
+ </a>
95
+ <a href="https://pytorch.org/get-started/locally/">
96
+ <img alt="PyTorch" src="https://img.shields.io/badge/-PyTorch 1.10+-ee4c2c?style=for-the-badge&logo=pytorch&logoColor=white">
97
+ </a>
98
+
99
+ Collection of functions and modules to help development in PyTorch.
100
+
101
+ </center>
102
+
103
+
104
+ ## Installation
105
+
106
+ With pip:
107
+ ```bash
108
+ pip install torchwrench
109
+ ```
110
+
111
+ With uv:
112
+ ```bash
113
+ uv add torchwrench
114
+ ```
115
+
116
+ The main requirement is **[PyTorch](https://pytorch.org/)**.
117
+
118
+ To check if the package is installed and show the package version, you can use the following command in your terminal:
119
+ ```bash
120
+ torchwrench-info
121
+ ```
122
+
123
+ This library works on all Python versions **>=3.9**, all PyTorch versions **>= 1.10**, and on **Linux, Mac and Windows** systems.
124
+
125
+ ## Examples
126
+
127
+ `torchwrench` functions and modules can be used like `torch` ones. The default acronym for `torchwrench` is `tw`.
128
+
129
+ ### Label conversions
130
+ Supports **multiclass** labels conversions between probabilities, classes indices, classes names and onehot encoding.
131
+
132
+ ```python
133
+ import torchwrench as tw
134
+
135
+ probs = tw.as_tensor([[0.9, 0.1], [0.4, 0.6]])
136
+ names = tw.probs_to_name(probs, idx_to_name={0: "Cat", 1: "Dog"})
137
+ # ["Cat", "Dog"]
138
+ ```
139
+
140
+ This package also supports **multilabel** labels conversions between probabilities, classes multi-indices, classes multi-names and multihot encoding.
141
+
142
+ ```python
143
+ import torchwrench as tw
144
+
145
+ multihot = tw.as_tensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
146
+ indices = tw.multihot_to_indices(multihot)
147
+ # [[0], [1, 2], []]
148
+ ```
149
+
150
+ Finally, this packages includes the **powerset multilabel** conversions :
151
+
152
+ ```python
153
+ import torchwrench as tw
154
+
155
+ multihot = tw.as_tensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
156
+ indices = tw.multilabel_to_powerset(multihot, num_classes=3, max_set_size=2)
157
+ # tensor([[0, 1, 0, 0, 0, 0, 0],
158
+ # [0, 0, 0, 0, 0, 0, 1],
159
+ # [1, 0, 0, 0, 0, 0, 0]])
160
+ ```
161
+
162
+ ### Typing
163
+
164
+ Typing with number of dimensions :
165
+
166
+ ```python
167
+ import torchwrench as tw
168
+
169
+ x1 = tw.as_tensor([1, 2])
170
+ print(isinstance(x1, tw.Tensor2D)) # False
171
+ x2 = tw.as_tensor([[1, 2], [3, 4]])
172
+ print(isinstance(x2, tw.Tensor2D)) # True
173
+ ```
174
+
175
+ Typing with tensor dtype :
176
+
177
+ ```python
178
+ import torchwrench as tw
179
+
180
+ x1 = tw.as_tensor([1, 2], dtype=tw.int)
181
+ print(isinstance(x1, tw.SignedIntegerTensor)) # True
182
+
183
+ x2 = tw.as_tensor([1, 2], dtype=tw.long)
184
+ print(isinstance(x2, tw.SignedIntegerTensor1D)) # True
185
+
186
+ x3 = tw.as_tensor([1, 2], dtype=tw.float)
187
+ print(isinstance(x3, tw.SignedIntegerTensor)) # False
188
+ ```
189
+
190
+ ### Padding & cropping
191
+
192
+ Pad a specific dimension :
193
+
194
+ ```python
195
+ import torchwrench as tw
196
+
197
+ x = tw.rand(10, 3, 1)
198
+ padded = tw.pad_dim(x, target_length=5, dim=1, pad_value=-1)
199
+ # x2 has shape (10, 5, 1), padded with -1
200
+ ```
201
+
202
+ Pad nested list of tensors to a single one :
203
+
204
+ ```python
205
+ import torchwrench as tw
206
+
207
+ tensors = [tw.rand(10, 2), [tw.rand(3)] * 5, tw.rand(0, 5)]
208
+ padded = tw.pad_and_stack_rec(tensors, pad_value=0)
209
+ # padded has shape (3, 10, 5), padded with 0
210
+ ```
211
+
212
+ Remove values at a specific dimension :
213
+
214
+ ```python
215
+ import torchwrench as tw
216
+
217
+ x = tw.rand(10, 5, 3)
218
+ cropped = tw.crop_dim(x, dim=1, target_length=2)
219
+ # cropped has shape (10, 2, 3)
220
+ ```
221
+
222
+ ### Masking
223
+
224
+ ```python
225
+ import torchwrench as tw
226
+
227
+ x = tw.as_tensor([3, 1, 2])
228
+ mask = tw.lengths_to_non_pad_mask(x, max_len=4)
229
+ # Each row i contains x[i] True values for non-padding mask
230
+ # tensor([[True, True, True, False],
231
+ # [True, False, False, False],
232
+ # [True, True, False, False]])
233
+ ```
234
+
235
+ ```python
236
+ import torchwrench as tw
237
+
238
+ x = tw.as_tensor([1, 2, 3, 4])
239
+ mask = tw.as_tensor([True, True, False, False])
240
+ result = tw.masked_mean(x, mask)
241
+ # result contains the mean of the values marked as True: 1.5
242
+ ```
243
+
244
+ ### Others tensors manipulations!
245
+
246
+ ```python
247
+ import torchwrench as tw
248
+
249
+ x = tw.as_tensor([1, 2, 3, 4])
250
+ result = tw.insert_at_indices(x, indices=[0, 2], values=5)
251
+ # result contains tensor with inserted values: tensor([5, 1, 2, 5, 3, 4])
252
+ ```
253
+
254
+ ```python
255
+ import torchwrench as tw
256
+
257
+ perm = tw.randperm(10)
258
+ inv_perm = tw.get_inverse_perm(perm)
259
+
260
+ x1 = tw.rand(10)
261
+ x2 = x1[perm]
262
+ x3 = x2[inv_perm]
263
+ # inv_perm are indices that allow us to get x3 from x2, i.e. x1 == x3 here
264
+ ```
265
+
266
+ ### Extra: pre-compute datasets to HDF files
267
+
268
+ Here is an example of pre-computing spectrograms of torchaudio `SPEECHCOMMANDS` dataset, using `pack_dataset` function:
269
+
270
+ ```python
271
+ from torchaudio.datasets import SPEECHCOMMANDS
272
+ from torchaudio.transforms import Spectrogram
273
+ from torchwrench import nn
274
+ from torchwrench.extras.hdf import pack_to_hdf
275
+
276
+ speech_commands_root = "path/to/speech_commands"
277
+ packed_root = "path/to/packed_dataset.hdf"
278
+
279
+ dataset = SPEECHCOMMANDS(speech_commands_root, download=True, subset="validation")
280
+ # dataset[0] is a tuple, contains waveform and other metadata
281
+
282
+ class MyTransform(nn.Module):
283
+ def __init__(self) -> None:
284
+ super().__init__()
285
+ self.spectrogram_extractor = Spectrogram()
286
+
287
+ def forward(self, item):
288
+ waveform = item[0]
289
+ spectrogram = self.spectrogram_extractor(waveform)
290
+ return (spectrogram,) + item[1:]
291
+
292
+ pack_to_hdf(dataset, packed_root, MyTransform())
293
+ ```
294
+
295
+ Then you can load the pre-computed dataset using `HDFDataset`:
296
+ ```python
297
+ from torchwrench.extras.hdf import HDFDataset
298
+
299
+ packed_root = "path/to/packed_dataset.hdf"
300
+ packed_dataset = HDFDataset(packed_root)
301
+ packed_dataset[0] # == first transformed item, i.e. transform(dataset[0])
302
+ ```
303
+
304
+ ## Contact
305
+ Maintainer:
306
+ - [Étienne Labbé](https://labbeti.github.io/) "Labbeti": labbeti.pub@gmail.com
@@ -0,0 +1,225 @@
1
+ # torchwrench
2
+
3
+ <center>
4
+
5
+ <a href="https://www.python.org/">
6
+ <img alt="Python" src="https://img.shields.io/badge/-Python 3.9+-blue?style=for-the-badge&logo=python&logoColor=white">
7
+ </a>
8
+ <a href="https://github.com/Labbeti/torchwrench/actions">
9
+ <img alt="Build" src="https://img.shields.io/github/actions/workflow/status/Labbeti/torchwrench/test.yaml?branch=main&style=for-the-badge&logo=github">
10
+ </a>
11
+ <a href='https://torchwrench.readthedocs.io/'>
12
+ <img src='https://readthedocs.org/projects/torchwrench/badge/?version=stable&style=for-the-badge' alt='Documentation Status' />
13
+ </a>
14
+ <a href="https://pytorch.org/get-started/locally/">
15
+ <img alt="PyTorch" src="https://img.shields.io/badge/-PyTorch 1.10+-ee4c2c?style=for-the-badge&logo=pytorch&logoColor=white">
16
+ </a>
17
+
18
+ Collection of functions and modules to help development in PyTorch.
19
+
20
+ </center>
21
+
22
+
23
+ ## Installation
24
+
25
+ With pip:
26
+ ```bash
27
+ pip install torchwrench
28
+ ```
29
+
30
+ With uv:
31
+ ```bash
32
+ uv add torchwrench
33
+ ```
34
+
35
+ The main requirement is **[PyTorch](https://pytorch.org/)**.
36
+
37
+ To check if the package is installed and show the package version, you can use the following command in your terminal:
38
+ ```bash
39
+ torchwrench-info
40
+ ```
41
+
42
+ This library works on all Python versions **>=3.9**, all PyTorch versions **>= 1.10**, and on **Linux, Mac and Windows** systems.
43
+
44
+ ## Examples
45
+
46
+ `torchwrench` functions and modules can be used like `torch` ones. The default acronym for `torchwrench` is `tw`.
47
+
48
+ ### Label conversions
49
+ Supports **multiclass** labels conversions between probabilities, classes indices, classes names and onehot encoding.
50
+
51
+ ```python
52
+ import torchwrench as tw
53
+
54
+ probs = tw.as_tensor([[0.9, 0.1], [0.4, 0.6]])
55
+ names = tw.probs_to_name(probs, idx_to_name={0: "Cat", 1: "Dog"})
56
+ # ["Cat", "Dog"]
57
+ ```
58
+
59
+ This package also supports **multilabel** labels conversions between probabilities, classes multi-indices, classes multi-names and multihot encoding.
60
+
61
+ ```python
62
+ import torchwrench as tw
63
+
64
+ multihot = tw.as_tensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
65
+ indices = tw.multihot_to_indices(multihot)
66
+ # [[0], [1, 2], []]
67
+ ```
68
+
69
+ Finally, this packages includes the **powerset multilabel** conversions :
70
+
71
+ ```python
72
+ import torchwrench as tw
73
+
74
+ multihot = tw.as_tensor([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
75
+ indices = tw.multilabel_to_powerset(multihot, num_classes=3, max_set_size=2)
76
+ # tensor([[0, 1, 0, 0, 0, 0, 0],
77
+ # [0, 0, 0, 0, 0, 0, 1],
78
+ # [1, 0, 0, 0, 0, 0, 0]])
79
+ ```
80
+
81
+ ### Typing
82
+
83
+ Typing with number of dimensions :
84
+
85
+ ```python
86
+ import torchwrench as tw
87
+
88
+ x1 = tw.as_tensor([1, 2])
89
+ print(isinstance(x1, tw.Tensor2D)) # False
90
+ x2 = tw.as_tensor([[1, 2], [3, 4]])
91
+ print(isinstance(x2, tw.Tensor2D)) # True
92
+ ```
93
+
94
+ Typing with tensor dtype :
95
+
96
+ ```python
97
+ import torchwrench as tw
98
+
99
+ x1 = tw.as_tensor([1, 2], dtype=tw.int)
100
+ print(isinstance(x1, tw.SignedIntegerTensor)) # True
101
+
102
+ x2 = tw.as_tensor([1, 2], dtype=tw.long)
103
+ print(isinstance(x2, tw.SignedIntegerTensor1D)) # True
104
+
105
+ x3 = tw.as_tensor([1, 2], dtype=tw.float)
106
+ print(isinstance(x3, tw.SignedIntegerTensor)) # False
107
+ ```
108
+
109
+ ### Padding & cropping
110
+
111
+ Pad a specific dimension :
112
+
113
+ ```python
114
+ import torchwrench as tw
115
+
116
+ x = tw.rand(10, 3, 1)
117
+ padded = tw.pad_dim(x, target_length=5, dim=1, pad_value=-1)
118
+ # x2 has shape (10, 5, 1), padded with -1
119
+ ```
120
+
121
+ Pad nested list of tensors to a single one :
122
+
123
+ ```python
124
+ import torchwrench as tw
125
+
126
+ tensors = [tw.rand(10, 2), [tw.rand(3)] * 5, tw.rand(0, 5)]
127
+ padded = tw.pad_and_stack_rec(tensors, pad_value=0)
128
+ # padded has shape (3, 10, 5), padded with 0
129
+ ```
130
+
131
+ Remove values at a specific dimension :
132
+
133
+ ```python
134
+ import torchwrench as tw
135
+
136
+ x = tw.rand(10, 5, 3)
137
+ cropped = tw.crop_dim(x, dim=1, target_length=2)
138
+ # cropped has shape (10, 2, 3)
139
+ ```
140
+
141
+ ### Masking
142
+
143
+ ```python
144
+ import torchwrench as tw
145
+
146
+ x = tw.as_tensor([3, 1, 2])
147
+ mask = tw.lengths_to_non_pad_mask(x, max_len=4)
148
+ # Each row i contains x[i] True values for non-padding mask
149
+ # tensor([[True, True, True, False],
150
+ # [True, False, False, False],
151
+ # [True, True, False, False]])
152
+ ```
153
+
154
+ ```python
155
+ import torchwrench as tw
156
+
157
+ x = tw.as_tensor([1, 2, 3, 4])
158
+ mask = tw.as_tensor([True, True, False, False])
159
+ result = tw.masked_mean(x, mask)
160
+ # result contains the mean of the values marked as True: 1.5
161
+ ```
162
+
163
+ ### Others tensors manipulations!
164
+
165
+ ```python
166
+ import torchwrench as tw
167
+
168
+ x = tw.as_tensor([1, 2, 3, 4])
169
+ result = tw.insert_at_indices(x, indices=[0, 2], values=5)
170
+ # result contains tensor with inserted values: tensor([5, 1, 2, 5, 3, 4])
171
+ ```
172
+
173
+ ```python
174
+ import torchwrench as tw
175
+
176
+ perm = tw.randperm(10)
177
+ inv_perm = tw.get_inverse_perm(perm)
178
+
179
+ x1 = tw.rand(10)
180
+ x2 = x1[perm]
181
+ x3 = x2[inv_perm]
182
+ # inv_perm are indices that allow us to get x3 from x2, i.e. x1 == x3 here
183
+ ```
184
+
185
+ ### Extra: pre-compute datasets to HDF files
186
+
187
+ Here is an example of pre-computing spectrograms of torchaudio `SPEECHCOMMANDS` dataset, using `pack_dataset` function:
188
+
189
+ ```python
190
+ from torchaudio.datasets import SPEECHCOMMANDS
191
+ from torchaudio.transforms import Spectrogram
192
+ from torchwrench import nn
193
+ from torchwrench.extras.hdf import pack_to_hdf
194
+
195
+ speech_commands_root = "path/to/speech_commands"
196
+ packed_root = "path/to/packed_dataset.hdf"
197
+
198
+ dataset = SPEECHCOMMANDS(speech_commands_root, download=True, subset="validation")
199
+ # dataset[0] is a tuple, contains waveform and other metadata
200
+
201
+ class MyTransform(nn.Module):
202
+ def __init__(self) -> None:
203
+ super().__init__()
204
+ self.spectrogram_extractor = Spectrogram()
205
+
206
+ def forward(self, item):
207
+ waveform = item[0]
208
+ spectrogram = self.spectrogram_extractor(waveform)
209
+ return (spectrogram,) + item[1:]
210
+
211
+ pack_to_hdf(dataset, packed_root, MyTransform())
212
+ ```
213
+
214
+ Then you can load the pre-computed dataset using `HDFDataset`:
215
+ ```python
216
+ from torchwrench.extras.hdf import HDFDataset
217
+
218
+ packed_root = "path/to/packed_dataset.hdf"
219
+ packed_dataset = HDFDataset(packed_root)
220
+ packed_dataset[0] # == first transformed item, i.e. transform(dataset[0])
221
+ ```
222
+
223
+ ## Contact
224
+ Maintainer:
225
+ - [Étienne Labbé](https://labbeti.github.io/) "Labbeti": labbeti.pub@gmail.com
@@ -0,0 +1,104 @@
1
+ [project]
2
+ name = "torchwrench"
3
+ authors = [
4
+ {name = "Étienne Labbé (Labbeti)", email = "labbeti.pub@gmail.com"},
5
+ ]
6
+ description = "Collection of functions and modules to help development in PyTorch."
7
+ readme = "README.md"
8
+ requires-python = ">=3.9"
9
+ keywords = ["pytorch", "deep-learning"]
10
+ license = {file = "LICENSE"}
11
+ classifiers = [
12
+ "Intended Audience :: Developers",
13
+ "Intended Audience :: Science/Research",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3.9",
16
+ "Programming Language :: Python :: 3.10",
17
+ "Programming Language :: Python :: 3.11",
18
+ "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Topic :: Scientific/Engineering",
21
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
22
+ "Operating System :: OS Independent",
23
+ ]
24
+ maintainers = [
25
+ {name = "Étienne Labbé (Labbeti)", email = "labbeti.pub@gmail.com"},
26
+ ]
27
+ dynamic = ["version"]
28
+ dependencies = [
29
+ "pythonwrench~=0.1.0",
30
+ "torch>=1.10.0",
31
+ "typing-extensions>=4.10.0",
32
+ ]
33
+
34
+ [project.urls]
35
+ Homepage = "https://pypi.org/project/torchwrench/"
36
+ Documentation = "https://torchwrench.readthedocs.io/"
37
+ Repository = "https://github.com/Labbeti/torchwrench.git"
38
+ Changelog = "https://github.com/Labbeti/torchwrench/blob/main/CHANGELOG.md"
39
+ Tracker = "https://github.com/Labbeti/torchwrench/issues"
40
+
41
+ [project.scripts]
42
+ torchwrench-info = "torchwrench.entries:print_install_info"
43
+
44
+ [build-system]
45
+ requires = ["setuptools >= 61.0"]
46
+ build-backend = "setuptools.build_meta"
47
+
48
+ [tool.setuptools.packages.find]
49
+ where = ["src"] # list of folders that contain the packages (["."] by default)
50
+ # package names should match these glob patterns (["*"] by default)
51
+ # note: star after package names is required to work
52
+ include = ["torchwrench*"]
53
+
54
+ [tool.setuptools.dynamic]
55
+ version = {attr = "torchwrench.__version__"}
56
+
57
+ [tool.pyright]
58
+ include = ["src"]
59
+ exclude = ["**/__pycache__"]
60
+ pythonVersion = "3.9"
61
+
62
+ [tool.pytest.ini_options]
63
+ testpaths = ["tests"]
64
+
65
+ [tool.coverage.run]
66
+ source = ["src"]
67
+
68
+ [tool.ruff.lint.extend-per-file-ignores]
69
+ "__init__.py" = ["F401", "F403"]
70
+
71
+ [tool.uv.sources]
72
+ torchwrench = { workspace = true }
73
+
74
+ [project.optional-dependencies]
75
+ dev = [
76
+ "coverage[toml]>=7.9.1",
77
+ "flake8>=7.2.0",
78
+ "ipython>=8.18.1",
79
+ "pre-commit>=4.2.0",
80
+ "pytest>=8.4.1",
81
+ "ruff>=0.12.0",
82
+ "setuptools>=80.9.0",
83
+ "soundfile>=0.13.1",
84
+ "sphinx>=7.4.7",
85
+ "sphinx-press-theme>=0.8.0",
86
+ "twine>=6.1.0",
87
+ "typing-extensions>=4.10.0",
88
+ "ipykernel>=6.29.5",
89
+ "numpy!=2.0.0,!=2.0.1,!=2.0.2",
90
+ ]
91
+ extras = [
92
+ "colorlog>=6.9.0",
93
+ "h5py>=3.14.0",
94
+ "numpy!=2.0.0,!=2.0.1,!=2.0.2",
95
+ "omegaconf>=2.3.0",
96
+ "pandas>=2.3.0",
97
+ "pyyaml>=6.0.2",
98
+ "safetensors>=0.5.3",
99
+ "scipy>=1.13.1",
100
+ "tensorboard>=2.19.0",
101
+ "torchaudio>=0.10.0",
102
+ "torchvision>=0.11.0",
103
+ "tqdm>=4.67.1",
104
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ from setuptools import setup
5
+
6
+ if __name__ == "__main__":
7
+ setup()