tensorneko-util 0.3.17__py3-none-any.whl → 0.3.19__py3-none-any.whl

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 (39) hide show
  1. tensorneko_util/io/_path_conversion.py +25 -0
  2. tensorneko_util/io/audio/audio_reader.py +7 -4
  3. tensorneko_util/io/audio/audio_writer.py +9 -5
  4. tensorneko_util/io/hdf5/hdf5_reader.py +9 -3
  5. tensorneko_util/io/hdf5/hdf5_writer.py +4 -1
  6. tensorneko_util/io/image/image_reader.py +11 -5
  7. tensorneko_util/io/image/image_writer.py +13 -8
  8. tensorneko_util/io/json/json_reader.py +12 -7
  9. tensorneko_util/io/json/json_writer.py +8 -3
  10. tensorneko_util/io/matlab/mat_reader.py +8 -4
  11. tensorneko_util/io/matlab/mat_writer.py +8 -4
  12. tensorneko_util/io/npy/npy_reader.py +14 -8
  13. tensorneko_util/io/npy/npy_writer.py +19 -10
  14. tensorneko_util/io/pickle/pickle_reader.py +9 -3
  15. tensorneko_util/io/pickle/pickle_writer.py +9 -3
  16. tensorneko_util/io/reader.py +6 -2
  17. tensorneko_util/io/text/text_reader.py +11 -3
  18. tensorneko_util/io/text/text_writer.py +11 -3
  19. tensorneko_util/io/toml/toml_reader.py +9 -3
  20. tensorneko_util/io/toml/toml_writer.py +9 -3
  21. tensorneko_util/io/video/video_reader.py +15 -8
  22. tensorneko_util/io/video/video_writer.py +11 -6
  23. tensorneko_util/io/writer.py +6 -2
  24. tensorneko_util/io/yaml/yaml_reader.py +8 -4
  25. tensorneko_util/io/yaml/yaml_writer.py +9 -6
  26. tensorneko_util/msg/gotify.py +2 -2
  27. tensorneko_util/notebook/__init__.py +2 -1
  28. tensorneko_util/notebook/animation.py +20 -0
  29. tensorneko_util/notebook/display.py +6 -1
  30. tensorneko_util/util/__init__.py +3 -1
  31. tensorneko_util/util/multi_layer_indexer.py +113 -0
  32. tensorneko_util/version.txt +1 -1
  33. tensorneko_util/visualization/watcher/web/dist/assets/{index.53207489.js → index.901ba3f5.js} +47 -47
  34. tensorneko_util/visualization/watcher/web/dist/index.html +1 -1
  35. {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/METADATA +1 -1
  36. {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/RECORD +39 -36
  37. {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/LICENSE +0 -0
  38. {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/WHEEL +0 -0
  39. {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,113 @@
1
+ from typing import Union, List
2
+
3
+ T_Counts = Union[int, List["Counts"]]
4
+
5
+
6
+ class MultiLayerIndexer:
7
+ """
8
+ Multi-layer indexer for hierarchical indexing structures.
9
+
10
+ Args:
11
+ counts (``int`` | ``List[int | List]``): A nested structure representing the counts at each layer.
12
+ It can be an integer or a list of counts (which can themselves be lists).
13
+
14
+ Examples::
15
+
16
+ # Test Case 1
17
+ counts = [5, 10, 15]
18
+ indexer = MultiLayerIndexer(counts)
19
+ print(len(indexer), indexer(12)) # Output: 30 [1, 7]
20
+
21
+ # Test Case 2
22
+ counts = [[3, 2], 4, [1, 1, 1]]
23
+ indexer = MultiLayerIndexer(counts)
24
+ print(len(indexer), indexer(8)) # Output: 12 [1, 3]
25
+
26
+ # Test Case 3 (Error Handling)
27
+ try:
28
+ counts = [2, [3, [4, 5]]]
29
+ indexer = MultiLayerIndexer(counts)
30
+ print(len(indexer), indexer(20)) # Should raise IndexError
31
+ except IndexError as e:
32
+ print(e) # Output: Index out of bounds
33
+
34
+ """
35
+
36
+ def __init__(self, counts: T_Counts):
37
+ self._counts = counts
38
+ self._total = self._total_counts(counts)
39
+
40
+ def __call__(self, index: int) -> List[int]:
41
+ """
42
+ Given a global index, return the indices at each layer.
43
+
44
+ Args:
45
+ index (``int``): The global index.
46
+
47
+ Returns:
48
+ ``List[int]``: A list of indices at each layer corresponding to the global index.
49
+
50
+ Raises:
51
+ ``IndexError``: If the provided global index is out of bounds.
52
+ """
53
+ return self._multi_layer_index(self._counts, index)
54
+
55
+ @classmethod
56
+ def _multi_layer_index(cls, counts: T_Counts, index: int) -> List[int]:
57
+ """
58
+ A recursive helper function to compute the indices at each layer.
59
+
60
+ Args:
61
+ counts (``int`` | ``List[int | List]``): The counts at the current layer.
62
+ index (``int``): The index at the current layer.
63
+
64
+ Returns:
65
+ ``list``: A list of indices at each layer.
66
+
67
+ Raises:
68
+ ``IndexError``: If the provided index exceeds the available counts.
69
+ """
70
+ if isinstance(counts, int):
71
+ if index < counts:
72
+ return [index]
73
+ else:
74
+ raise IndexError("Index out of bounds")
75
+ elif isinstance(counts, list):
76
+ cumulative_counts = [0]
77
+ for c in counts:
78
+ total_c = cls._total_counts(c)
79
+ cumulative_counts.append(cumulative_counts[-1] + total_c)
80
+ for i in range(len(cumulative_counts) - 1):
81
+ if cumulative_counts[i] <= index < cumulative_counts[i + 1]:
82
+ sub_idx = index - cumulative_counts[i]
83
+ sub_indices = cls._multi_layer_index(counts[i], sub_idx)
84
+ return [i] + sub_indices
85
+ raise IndexError("Index out of bounds")
86
+ else:
87
+ raise ValueError("Invalid counts structure")
88
+
89
+ @classmethod
90
+ def _total_counts(cls, counts: T_Counts) -> int:
91
+ """
92
+ Compute the total counts of items under the given counts structure.
93
+
94
+ Args:
95
+ counts (``int`` | ``List[int | List]``): The counts structure.
96
+
97
+ Returns:
98
+ ``int``: The total count of items.
99
+
100
+ Raises:
101
+ ``ValueError``: If the counts structure is invalid.
102
+ """
103
+ if isinstance(counts, int):
104
+ if counts < 0:
105
+ raise ValueError("Counts cannot be negative")
106
+ return counts
107
+ elif isinstance(counts, list):
108
+ return sum(cls._total_counts(c) for c in counts)
109
+ else:
110
+ raise ValueError("Invalid counts structure")
111
+
112
+ def __len__(self) -> int:
113
+ return self._total
@@ -1 +1 @@
1
- 0.3.17
1
+ 0.3.19