stouputils 1.14.0__py3-none-any.whl → 1.14.1__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 (108) hide show
  1. stouputils/__init__.pyi +15 -0
  2. stouputils/_deprecated.pyi +12 -0
  3. stouputils/all_doctests.pyi +46 -0
  4. stouputils/applications/__init__.pyi +2 -0
  5. stouputils/applications/automatic_docs.py +3 -0
  6. stouputils/applications/automatic_docs.pyi +106 -0
  7. stouputils/applications/upscaler/__init__.pyi +3 -0
  8. stouputils/applications/upscaler/config.pyi +18 -0
  9. stouputils/applications/upscaler/image.pyi +109 -0
  10. stouputils/applications/upscaler/video.pyi +60 -0
  11. stouputils/archive.pyi +67 -0
  12. stouputils/backup.pyi +109 -0
  13. stouputils/collections.pyi +86 -0
  14. stouputils/continuous_delivery/__init__.pyi +5 -0
  15. stouputils/continuous_delivery/cd_utils.pyi +129 -0
  16. stouputils/continuous_delivery/github.pyi +162 -0
  17. stouputils/continuous_delivery/pypi.pyi +52 -0
  18. stouputils/continuous_delivery/pyproject.pyi +67 -0
  19. stouputils/continuous_delivery/stubs.pyi +39 -0
  20. stouputils/ctx.pyi +211 -0
  21. stouputils/data_science/config/get.py +51 -51
  22. stouputils/data_science/data_processing/image/__init__.py +66 -66
  23. stouputils/data_science/data_processing/image/auto_contrast.py +79 -79
  24. stouputils/data_science/data_processing/image/axis_flip.py +58 -58
  25. stouputils/data_science/data_processing/image/bias_field_correction.py +74 -74
  26. stouputils/data_science/data_processing/image/binary_threshold.py +73 -73
  27. stouputils/data_science/data_processing/image/blur.py +59 -59
  28. stouputils/data_science/data_processing/image/brightness.py +54 -54
  29. stouputils/data_science/data_processing/image/canny.py +110 -110
  30. stouputils/data_science/data_processing/image/clahe.py +92 -92
  31. stouputils/data_science/data_processing/image/common.py +30 -30
  32. stouputils/data_science/data_processing/image/contrast.py +53 -53
  33. stouputils/data_science/data_processing/image/curvature_flow_filter.py +74 -74
  34. stouputils/data_science/data_processing/image/denoise.py +378 -378
  35. stouputils/data_science/data_processing/image/histogram_equalization.py +123 -123
  36. stouputils/data_science/data_processing/image/invert.py +64 -64
  37. stouputils/data_science/data_processing/image/laplacian.py +60 -60
  38. stouputils/data_science/data_processing/image/median_blur.py +52 -52
  39. stouputils/data_science/data_processing/image/noise.py +59 -59
  40. stouputils/data_science/data_processing/image/normalize.py +65 -65
  41. stouputils/data_science/data_processing/image/random_erase.py +66 -66
  42. stouputils/data_science/data_processing/image/resize.py +69 -69
  43. stouputils/data_science/data_processing/image/rotation.py +80 -80
  44. stouputils/data_science/data_processing/image/salt_pepper.py +68 -68
  45. stouputils/data_science/data_processing/image/sharpening.py +55 -55
  46. stouputils/data_science/data_processing/image/shearing.py +64 -64
  47. stouputils/data_science/data_processing/image/threshold.py +64 -64
  48. stouputils/data_science/data_processing/image/translation.py +71 -71
  49. stouputils/data_science/data_processing/image/zoom.py +83 -83
  50. stouputils/data_science/data_processing/image_augmentation.py +118 -118
  51. stouputils/data_science/data_processing/image_preprocess.py +183 -183
  52. stouputils/data_science/data_processing/prosthesis_detection.py +359 -359
  53. stouputils/data_science/data_processing/technique.py +481 -481
  54. stouputils/data_science/dataset/__init__.py +45 -45
  55. stouputils/data_science/dataset/dataset.py +292 -292
  56. stouputils/data_science/dataset/dataset_loader.py +135 -135
  57. stouputils/data_science/dataset/grouping_strategy.py +296 -296
  58. stouputils/data_science/dataset/image_loader.py +100 -100
  59. stouputils/data_science/dataset/xy_tuple.py +696 -696
  60. stouputils/data_science/metric_dictionnary.py +106 -106
  61. stouputils/data_science/mlflow_utils.py +206 -206
  62. stouputils/data_science/models/abstract_model.py +149 -149
  63. stouputils/data_science/models/all.py +85 -85
  64. stouputils/data_science/models/keras/all.py +38 -38
  65. stouputils/data_science/models/keras/convnext.py +62 -62
  66. stouputils/data_science/models/keras/densenet.py +50 -50
  67. stouputils/data_science/models/keras/efficientnet.py +60 -60
  68. stouputils/data_science/models/keras/mobilenet.py +56 -56
  69. stouputils/data_science/models/keras/resnet.py +52 -52
  70. stouputils/data_science/models/keras/squeezenet.py +233 -233
  71. stouputils/data_science/models/keras/vgg.py +42 -42
  72. stouputils/data_science/models/keras/xception.py +38 -38
  73. stouputils/data_science/models/keras_utils/callbacks/__init__.py +20 -20
  74. stouputils/data_science/models/keras_utils/callbacks/colored_progress_bar.py +219 -219
  75. stouputils/data_science/models/keras_utils/callbacks/learning_rate_finder.py +148 -148
  76. stouputils/data_science/models/keras_utils/callbacks/model_checkpoint_v2.py +31 -31
  77. stouputils/data_science/models/keras_utils/callbacks/progressive_unfreezing.py +249 -249
  78. stouputils/data_science/models/keras_utils/callbacks/warmup_scheduler.py +66 -66
  79. stouputils/data_science/models/keras_utils/losses/__init__.py +12 -12
  80. stouputils/data_science/models/keras_utils/losses/next_generation_loss.py +56 -56
  81. stouputils/data_science/models/keras_utils/visualizations.py +416 -416
  82. stouputils/data_science/models/sandbox.py +116 -116
  83. stouputils/data_science/range_tuple.py +234 -234
  84. stouputils/data_science/utils.py +285 -285
  85. stouputils/decorators.pyi +242 -0
  86. stouputils/image.pyi +172 -0
  87. stouputils/installer/__init__.py +18 -18
  88. stouputils/installer/__init__.pyi +5 -0
  89. stouputils/installer/common.pyi +39 -0
  90. stouputils/installer/downloader.pyi +24 -0
  91. stouputils/installer/linux.py +144 -144
  92. stouputils/installer/linux.pyi +39 -0
  93. stouputils/installer/main.py +223 -223
  94. stouputils/installer/main.pyi +57 -0
  95. stouputils/installer/windows.py +136 -136
  96. stouputils/installer/windows.pyi +31 -0
  97. stouputils/io.pyi +213 -0
  98. stouputils/parallel.py +13 -10
  99. stouputils/parallel.pyi +211 -0
  100. stouputils/print.pyi +136 -0
  101. stouputils/py.typed +1 -1
  102. stouputils/stouputils/parallel.pyi +4 -4
  103. stouputils/version_pkg.pyi +15 -0
  104. {stouputils-1.14.0.dist-info → stouputils-1.14.1.dist-info}/METADATA +1 -1
  105. stouputils-1.14.1.dist-info/RECORD +171 -0
  106. stouputils-1.14.0.dist-info/RECORD +0 -140
  107. {stouputils-1.14.0.dist-info → stouputils-1.14.1.dist-info}/WHEEL +0 -0
  108. {stouputils-1.14.0.dist-info → stouputils-1.14.1.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,242 @@
1
+ from .ctx import MeasureTime as MeasureTime, Muffle as Muffle
2
+ from .print import error as error, progress as progress, warning as warning
3
+ from collections.abc import Callable as Callable
4
+ from enum import Enum
5
+ from typing import Any, Literal
6
+
7
+ def measure_time(func: Callable[..., Any] | None = None, *, printer: Callable[..., None] = ..., message: str = '', perf_counter: bool = True, is_generator: bool = False) -> Callable[..., Any]:
8
+ ''' Decorator that will measure the execution time of a function and print it with the given print function
9
+
10
+ \tArgs:
11
+ \t\tfunc\t\t\t(Callable[..., Any] | None): Function to decorate
12
+ \t\tprinter\t\t\t(Callable):\tFunction to use to print the execution time (e.g. debug, info, warning, error, etc.)
13
+ \t\tmessage\t\t\t(str):\t\tMessage to display with the execution time (e.g. "Execution time of Something"),
14
+ \t\t\tdefaults to "Execution time of {func.__name__}"
15
+ \t\tperf_counter\t(bool):\t\tWhether to use time.perf_counter_ns or time.time_ns
16
+ \t\t\tdefaults to True (use time.perf_counter_ns)
17
+ \t\tis_generator\t(bool):\t\tWhether the function is a generator or not (default: False)
18
+ \t\t\tWhen True, the decorator will yield from the function instead of returning it.
19
+
20
+ \tReturns:
21
+ \t\tCallable: Decorator to measure the time of the function.
22
+
23
+ \tExamples:
24
+ \t\t.. code-block:: python
25
+
26
+ \t\t\t> @measure_time(printer=info)
27
+ \t\t\t> def test():
28
+ \t\t\t> pass
29
+ \t\t\t> test() # [INFO HH:MM:SS] Execution time of test: 0.000ms (400ns)
30
+ \t'''
31
+
32
+ class LogLevels(Enum):
33
+ """ Log level for the errors in the decorator handle_error() """
34
+ NONE = 0
35
+ WARNING = 1
36
+ WARNING_TRACEBACK = 2
37
+ ERROR_TRACEBACK = 3
38
+ RAISE_EXCEPTION = 4
39
+
40
+ force_raise_exception: bool
41
+
42
+ def handle_error(func: Callable[..., Any] | None = None, *, exceptions: tuple[type[BaseException], ...] | type[BaseException] = ..., message: str = '', error_log: LogLevels = ..., sleep_time: float = 0.0) -> Callable[..., Any]:
43
+ ''' Decorator that handle an error with different log levels.
44
+
45
+ \tArgs:
46
+ \t\tfunc (Callable[..., Any] | None): \tFunction to decorate
47
+ \t\texceptions\t(tuple[type[BaseException]], ...):\tExceptions to handle
48
+ \t\tmessage\t\t(str):\t\t\t\t\t\t\t\tMessage to display with the error. (e.g. "Error during something")
49
+ \t\terror_log\t(LogLevels):\t\t\t\t\t\tLog level for the errors
50
+ \t\t\tLogLevels.NONE:\t\t\t\t\tNone
51
+ \t\t\tLogLevels.WARNING:\t\t\t\tShow as warning
52
+ \t\t\tLogLevels.WARNING_TRACEBACK:\tShow as warning with traceback
53
+ \t\t\tLogLevels.ERROR_TRACEBACK:\t\tShow as error with traceback
54
+ \t\t\tLogLevels.RAISE_EXCEPTION:\t\tRaise exception
55
+ \t\tsleep_time\t(float):\t\t\t\t\t\t\tTime to sleep after the error (e.g. 0.0 to not sleep, 1.0 to sleep for 1 second)
56
+
57
+ \tExamples:
58
+ \t\t>>> @handle_error
59
+ \t\t... def might_fail():
60
+ \t\t... raise ValueError("Let\'s fail")
61
+
62
+ \t\t>>> @handle_error(error_log=LogLevels.WARNING)
63
+ \t\t... def test():
64
+ \t\t... raise ValueError("Let\'s fail")
65
+ \t\t>>> # test()\t# [WARNING HH:MM:SS] Error during test: (ValueError) Let\'s fail
66
+ \t'''
67
+ def timeout(func: Callable[..., Any] | None = None, *, seconds: float = 60.0, message: str = '') -> Callable[..., Any]:
68
+ ''' Decorator that raises a TimeoutError if the function runs longer than the specified timeout.
69
+
70
+ \tNote: This decorator uses SIGALRM on Unix systems, which only works in the main thread.
71
+ \tOn Windows or in non-main threads, it will fall back to a polling-based approach.
72
+
73
+ \tArgs:
74
+ \t\tfunc\t\t(Callable[..., Any] | None):\tFunction to apply timeout to
75
+ \t\tseconds\t\t(float):\t\t\t\t\t\tTimeout duration in seconds (default: 60.0)
76
+ \t\tmessage\t\t(str):\t\t\t\t\t\t\tCustom timeout message (default: "Function \'{func_name}\' timed out after {seconds} seconds")
77
+
78
+ \tReturns:
79
+ \t\tCallable[..., Any]: Decorator that enforces timeout on the function
80
+
81
+ \tRaises:
82
+ \t\tTimeoutError: If the function execution exceeds the timeout duration
83
+
84
+ \tExamples:
85
+ \t\t>>> @timeout(seconds=2.0)
86
+ \t\t... def slow_function():
87
+ \t\t... time.sleep(5)
88
+ \t\t>>> slow_function() # Raises TimeoutError after 2 seconds
89
+ \t\tTraceback (most recent call last):
90
+ \t\t\t...
91
+ \t\tTimeoutError: Function \'slow_function\' timed out after 2.0 seconds
92
+
93
+ \t\t>>> @timeout(seconds=1.0, message="Custom timeout message")
94
+ \t\t... def another_slow_function():
95
+ \t\t... time.sleep(3)
96
+ \t\t>>> another_slow_function() # Raises TimeoutError after 1 second
97
+ \t\tTraceback (most recent call last):
98
+ \t\t\t...
99
+ \t\tTimeoutError: Custom timeout message
100
+ \t'''
101
+ def retry(func: Callable[..., Any] | None = None, *, exceptions: tuple[type[BaseException], ...] | type[BaseException] = ..., max_attempts: int = 10, delay: float = 1.0, backoff: float = 1.0, message: str = '') -> Callable[..., Any]:
102
+ ''' Decorator that retries a function when specific exceptions are raised.
103
+
104
+ \tArgs:
105
+ \t\tfunc\t\t\t(Callable[..., Any] | None):\t\tFunction to retry
106
+ \t\texceptions\t\t(tuple[type[BaseException], ...]):\tExceptions to catch and retry on
107
+ \t\tmax_attempts\t(int | None):\t\t\t\t\t\tMaximum number of attempts (None for infinite retries)
108
+ \t\tdelay\t\t\t(float):\t\t\t\t\t\t\tInitial delay in seconds between retries (default: 1.0)
109
+ \t\tbackoff\t\t\t(float):\t\t\t\t\t\t\tMultiplier for delay after each retry (default: 1.0 for constant delay)
110
+ \t\tmessage\t\t\t(str):\t\t\t\t\t\t\t\tCustom message to display before ", retrying" (default: "{ExceptionName} encountered while running {func_name}")
111
+
112
+ \tReturns:
113
+ \t\tCallable[..., Any]: Decorator that retries the function on specified exceptions
114
+
115
+ \tExamples:
116
+ \t\t>>> import os
117
+ \t\t>>> @retry(exceptions=PermissionError, max_attempts=3, delay=0.1)
118
+ \t\t... def write_file():
119
+ \t\t... with open("test.txt", "w") as f:
120
+ \t\t... f.write("test")
121
+
122
+ \t\t>>> @retry(exceptions=(OSError, IOError), delay=0.5, backoff=2.0)
123
+ \t\t... def network_call():
124
+ \t\t... pass
125
+
126
+ \t\t>>> @retry(max_attempts=5, delay=1.0)
127
+ \t\t... def might_fail():
128
+ \t\t... pass
129
+ \t'''
130
+ def simple_cache(func: Callable[..., Any] | None = None, *, method: Literal['str', 'pickle'] = 'str') -> Callable[..., Any]:
131
+ ''' Decorator that caches the result of a function based on its arguments.
132
+
133
+ \tThe str method is often faster than the pickle method (by a little) but not as accurate with complex objects.
134
+
135
+ \tArgs:
136
+ \t\tfunc (Callable[..., Any] | None): Function to cache
137
+ \t\tmethod (Literal["str", "pickle"]): The method to use for caching.
138
+ \tReturns:
139
+ \t\tCallable[..., Any]: A decorator that caches the result of a function.
140
+ \tExamples:
141
+ \t\t>>> @simple_cache
142
+ \t\t... def test1(a: int, b: int) -> int:
143
+ \t\t... return a + b
144
+
145
+ \t\t>>> @simple_cache(method="str")
146
+ \t\t... def test2(a: int, b: int) -> int:
147
+ \t\t... return a + b
148
+ \t\t>>> test2(1, 2)
149
+ \t\t3
150
+ \t\t>>> test2(1, 2)
151
+ \t\t3
152
+ \t\t>>> test2(3, 4)
153
+ \t\t7
154
+ \t'''
155
+ def abstract(func: Callable[..., Any] | None = None, *, error_log: LogLevels = ...) -> Callable[..., Any]:
156
+ """ Decorator that marks a function as abstract.
157
+
158
+ \tContrary to the abstractmethod decorator from the abc module that raises a TypeError
159
+ \twhen you try to instantiate a class that has abstract methods, this decorator raises
160
+ \ta NotImplementedError ONLY when the decorated function is called, indicating that the function
161
+ \tmust be implemented by a subclass.
162
+
163
+ \tArgs:
164
+ \t\tfunc (Callable[..., Any] | None): The function to mark as abstract
165
+ \t\terror_log (LogLevels): Log level for the error handling
166
+ \t\t\tLogLevels.NONE: None
167
+ \t\t\tLogLevels.WARNING: Show as warning
168
+ \t\t\tLogLevels.WARNING_TRACEBACK: Show as warning with traceback
169
+ \t\t\tLogLevels.ERROR_TRACEBACK: Show as error with traceback
170
+ \t\t\tLogLevels.RAISE_EXCEPTION: Raise exception
171
+
172
+ \tReturns:
173
+ \t\tCallable[..., Any]: Decorator that raises NotImplementedError when called
174
+
175
+ \tExamples:
176
+ \t\t>>> class Base:
177
+ \t\t... @abstract
178
+ \t\t... def method(self):
179
+ \t\t... pass
180
+ \t\t>>> Base().method()
181
+ \t\tTraceback (most recent call last):
182
+ \t\t\t...
183
+ \t\tNotImplementedError: Function 'method' is abstract and must be implemented by a subclass
184
+ \t"""
185
+ def deprecated(func: Callable[..., Any] | None = None, *, message: str = '', version: str = '', error_log: LogLevels = ...) -> Callable[..., Any]:
186
+ ''' Decorator that marks a function as deprecated.
187
+
188
+ \tArgs:
189
+ \t\tfunc (Callable[..., Any] | None): Function to mark as deprecated
190
+ \t\tmessage (str): Additional message to display with the deprecation warning
191
+ \t\tversion (str): Version since when the function is deprecated (e.g. "v1.2.0")
192
+ \t\terror_log (LogLevels): Log level for the deprecation warning
193
+ \t\t\tLogLevels.NONE: None
194
+ \t\t\tLogLevels.WARNING: Show as warning
195
+ \t\t\tLogLevels.WARNING_TRACEBACK: Show as warning with traceback
196
+ \t\t\tLogLevels.ERROR_TRACEBACK: Show as error with traceback
197
+ \t\t\tLogLevels.RAISE_EXCEPTION: Raise exception
198
+ \tReturns:
199
+ \t\tCallable[..., Any]: Decorator that marks a function as deprecated
200
+
201
+ \tExamples:
202
+ \t\t>>> @deprecated
203
+ \t\t... def old_function():
204
+ \t\t... pass
205
+
206
+ \t\t>>> @deprecated(message="Use \'new_function()\' instead", error_log=LogLevels.WARNING)
207
+ \t\t... def another_old_function():
208
+ \t\t... pass
209
+ \t'''
210
+ def silent(func: Callable[..., Any] | None = None, *, mute_stderr: bool = False) -> Callable[..., Any]:
211
+ ''' Decorator that makes a function silent (disable stdout, and stderr if specified).
212
+
213
+ \tAlternative to stouputils.ctx.Muffle.
214
+
215
+ \tArgs:
216
+ \t\tfunc\t\t\t(Callable[..., Any] | None):\tFunction to make silent
217
+ \t\tmute_stderr\t\t(bool):\t\t\t\t\t\t\tWhether to mute stderr or not
218
+
219
+ \tExamples:
220
+ \t\t>>> @silent
221
+ \t\t... def test():
222
+ \t\t... print("Hello, world!")
223
+ \t\t>>> test()
224
+
225
+ \t\t>>> @silent(mute_stderr=True)
226
+ \t\t... def test2():
227
+ \t\t... print("Hello, world!")
228
+ \t\t>>> test2()
229
+
230
+ \t\t>>> silent(print)("Hello, world!")
231
+ \t'''
232
+ def _get_func_name(func: Callable[..., Any]) -> str:
233
+ ''' Get the name of a function, returns "<unknown>" if the name cannot be retrieved. '''
234
+ def _get_wrapper_name(decorator_name: str, func: Callable[..., Any]) -> str:
235
+ ''' Get a descriptive name for a wrapper function.
236
+
237
+ \tArgs:
238
+ \t\tdecorator_name\t(str):\t\t\t\t\tName of the decorator
239
+ \t\tfunc\t\t\t(Callable[..., Any]):\tFunction being decorated
240
+ \tReturns:
241
+ \t\tstr: Combined name for the wrapper function (e.g., "stouputils.decorators.handle_error@function_name")
242
+ \t'''
stouputils/image.pyi ADDED
@@ -0,0 +1,172 @@
1
+ import numpy as np
2
+ from .io import super_open as super_open
3
+ from .print import debug as debug, info as info
4
+ from PIL import Image
5
+ from collections.abc import Callable
6
+ from numpy.typing import NDArray
7
+ from typing import Any, TypeVar
8
+
9
+ PIL_Image_or_NDArray = TypeVar('PIL_Image_or_NDArray', bound='Image.Image | NDArray[np.number]')
10
+
11
+ def image_resize[PIL_Image_or_NDArray](image: PIL_Image_or_NDArray, max_result_size: int, resampling: Image.Resampling | None = None, min_or_max: Callable[[int, int], int] = ..., return_type: type[PIL_Image_or_NDArray] | str = 'same', keep_aspect_ratio: bool = True) -> Any:
12
+ ''' Resize an image while preserving its aspect ratio by default.
13
+ \tScales the image so that its largest dimension equals max_result_size.
14
+
15
+ \tArgs:
16
+ \t\timage (Image.Image | np.ndarray): The image to resize.
17
+ \t\tmax_result_size (int): Maximum size for the largest dimension.
18
+ \t\tresampling (Image.Resampling | None): PIL resampling filter to use (default: Image.Resampling.LANCZOS).
19
+ \t\tmin_or_max (Callable): Function to use to get the minimum or maximum of the two ratios.
20
+ \t\treturn_type (type | str): Type of the return value (Image.Image, np.ndarray, or "same" to match input type).
21
+ \t\tkeep_aspect_ratio (bool): Whether to keep the aspect ratio.
22
+ \tReturns:
23
+ \t\tImage.Image | NDArray[np.number]: The resized image with preserved aspect ratio.
24
+ \tExamples:
25
+ \t\t>>> # Test with (height x width x channels) numpy array
26
+ \t\t>>> import numpy as np
27
+ \t\t>>> array = np.random.randint(0, 255, (100, 50, 3), dtype=np.uint8)
28
+ \t\t>>> image_resize(array, 100).shape
29
+ \t\t(100, 50, 3)
30
+ \t\t>>> image_resize(array, 100, min_or_max=max).shape
31
+ \t\t(100, 50, 3)
32
+ \t\t>>> image_resize(array, 100, min_or_max=min).shape
33
+ \t\t(200, 100, 3)
34
+
35
+ \t\t>>> # Test with PIL Image
36
+ \t\t>>> from PIL import Image
37
+ \t\t>>> pil_image: Image.Image = Image.new(\'RGB\', (200, 100))
38
+ \t\t>>> image_resize(pil_image, 50).size
39
+ \t\t(50, 25)
40
+ \t\t>>> # Test with different return types
41
+ \t\t>>> resized_array = image_resize(array, 50, return_type=np.ndarray)
42
+ \t\t>>> isinstance(resized_array, np.ndarray)
43
+ \t\tTrue
44
+ \t\t>>> resized_array.shape
45
+ \t\t(50, 25, 3)
46
+ \t\t>>> # Test with different resampling methods
47
+ \t\t>>> image_resize(pil_image, 50, resampling=Image.Resampling.NEAREST).size
48
+ \t\t(50, 25)
49
+ \t'''
50
+ def auto_crop[PIL_Image_or_NDArray](image: PIL_Image_or_NDArray, mask: NDArray[np.bool_] | None = None, threshold: int | float | Callable[[NDArray[np.number]], int | float] | None = None, return_type: type[PIL_Image_or_NDArray] | str = 'same', contiguous: bool = True) -> Any:
51
+ ''' Automatically crop an image to remove zero or uniform regions.
52
+
53
+ \tThis function crops the image to keep only the region where pixels are non-zero
54
+ \t(or above a threshold). It can work with a mask or directly analyze the image.
55
+
56
+ \tArgs:
57
+ \t\timage (Image.Image | NDArray):\t The image to crop.
58
+ \t\tmask (NDArray[bool] | None): Optional binary mask indicating regions to keep.
59
+ \t\tthreshold (int | float | Callable): Threshold value or function (default: np.min).
60
+ \t\treturn_type (type | str): Type of the return value (Image.Image, NDArray[np.number], or "same" to match input type).
61
+ \t\tcontiguous (bool): If True (default), crop to bounding box. If False, remove entire rows/columns with no content.
62
+ \tReturns:
63
+ \t\tImage.Image | NDArray[np.number]: The cropped image.
64
+
65
+ \tExamples:
66
+ \t\t>>> # Test with numpy array with zeros on edges
67
+ \t\t>>> import numpy as np
68
+ \t\t>>> array = np.zeros((100, 100, 3), dtype=np.uint8)
69
+ \t\t>>> array[20:80, 30:70] = 255 # White rectangle in center
70
+ \t\t>>> cropped = auto_crop(array, return_type=np.ndarray)
71
+ \t\t>>> cropped.shape
72
+ \t\t(60, 40, 3)
73
+
74
+ \t\t>>> # Test with custom mask
75
+ \t\t>>> mask = np.zeros((100, 100), dtype=bool)
76
+ \t\t>>> mask[10:90, 10:90] = True
77
+ \t\t>>> cropped_with_mask = auto_crop(array, mask=mask, return_type=np.ndarray)
78
+ \t\t>>> cropped_with_mask.shape
79
+ \t\t(80, 80, 3)
80
+
81
+ \t\t>>> # Test with PIL Image
82
+ \t\t>>> from PIL import Image
83
+ \t\t>>> pil_image = Image.new(\'RGB\', (100, 100), (0, 0, 0))
84
+ \t\t>>> from PIL import ImageDraw
85
+ \t\t>>> draw = ImageDraw.Draw(pil_image)
86
+ \t\t>>> draw.rectangle([25, 25, 75, 75], fill=(255, 255, 255))
87
+ \t\t>>> cropped_pil = auto_crop(pil_image)
88
+ \t\t>>> cropped_pil.size
89
+ \t\t(51, 51)
90
+
91
+ \t\t>>> # Test with threshold
92
+ \t\t>>> array_gray = np.ones((100, 100), dtype=np.uint8) * 10
93
+ \t\t>>> array_gray[20:80, 30:70] = 255
94
+ \t\t>>> cropped_threshold = auto_crop(array_gray, threshold=50, return_type=np.ndarray)
95
+ \t\t>>> cropped_threshold.shape
96
+ \t\t(60, 40)
97
+
98
+ \t\t>>> # Test with callable threshold (using lambda to avoid min value)
99
+ \t\t>>> array_gray2 = np.ones((100, 100), dtype=np.uint8) * 10
100
+ \t\t>>> array_gray2[20:80, 30:70] = 255
101
+ \t\t>>> cropped_max = auto_crop(array_gray2, threshold=lambda x: 50, return_type=np.ndarray)
102
+ \t\t>>> cropped_max.shape
103
+ \t\t(60, 40)
104
+
105
+ \t>>> # Test with non-contiguous crop
106
+ \t>>> array_sparse = np.zeros((100, 100, 3), dtype=np.uint8)
107
+ \t>>> array_sparse[10, 10] = 255
108
+ \t>>> array_sparse[50, 50] = 255
109
+ \t>>> array_sparse[90, 90] = 255
110
+ \t>>> cropped_contiguous = auto_crop(array_sparse, contiguous=True, return_type=np.ndarray)
111
+ \t>>> cropped_contiguous.shape # Bounding box from (10,10) to (90,90)
112
+ \t(81, 81, 3)
113
+ \t>>> cropped_non_contiguous = auto_crop(array_sparse, contiguous=False, return_type=np.ndarray)
114
+ \t>>> cropped_non_contiguous.shape # Only rows/cols 10, 50, 90
115
+ \t(3, 3, 3)
116
+
117
+ \t>>> # Test with 3D crop on depth dimension
118
+ \t>>> array_3d = np.zeros((50, 50, 10), dtype=np.uint8)
119
+ \t>>> array_3d[10:40, 10:40, 2:8] = 255 # Content only in depth slices 2-7
120
+ \t>>> cropped_3d = auto_crop(array_3d, contiguous=True, return_type=np.ndarray)
121
+ \t>>> cropped_3d.shape # Should crop all 3 dimensions
122
+ \t(30, 30, 6)
123
+ \t'''
124
+ def numpy_to_gif(path: str, array: NDArray[np.integer | np.floating | np.bool_], duration: int = 100, loop: int = 0, mkdir: bool = True, **kwargs: Any) -> None:
125
+ ''' Generate a \'.gif\' file from a numpy array for 3D/4D visualization.
126
+
127
+ \tArgs:
128
+ \t\tpath (str): Path to the output .gif file.
129
+ \t\tarray (NDArray): Numpy array to be dumped (must be 3D or 4D).
130
+ \t\t\t3D: (depth, height, width) - e.g. (64, 1024, 1024)
131
+ \t\t\t4D: (depth, height, width, channels) - e.g. (50, 64, 1024, 3)
132
+ \t\tduration (int): Duration between frames in milliseconds.
133
+ \t\tloop (int): Number of loops (0 = infinite).
134
+ \t\tmkdir (bool): Create the directory if it does not exist.
135
+ \t\t**kwargs (Any): Additional keyword arguments for PIL.Image.save().
136
+
137
+ \tExamples:
138
+
139
+ \t\t.. code-block:: python
140
+
141
+ \t\t\t> # 3D array example
142
+ \t\t\t> array = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8)
143
+ \t\t\t> numpy_to_gif("output_10_frames_100x100.gif", array, duration=200, loop=0)
144
+
145
+ \t\t\t> # 4D array example (batch of 3D images)
146
+ \t\t\t> array_4d = np.random.randint(0, 256, (5, 10, 100, 3), dtype=np.uint8)
147
+ \t\t\t> numpy_to_gif("output_50_frames_100x100.gif", array_4d, duration=200)
148
+
149
+ \t\t\t> total_duration = 1000 # 1 second
150
+ \t\t\t> numpy_to_gif("output_1s.gif", array, duration=total_duration // len(array))
151
+ \t'''
152
+ def numpy_to_obj(path: str, array: NDArray[np.integer | np.floating | np.bool_], threshold: float = 0.5, step_size: int = 1, pad_array: bool = True, verbose: int = 0) -> None:
153
+ ''' Generate a \'.obj\' file from a numpy array for 3D visualization using marching cubes.
154
+
155
+ \tArgs:
156
+ \t\tpath (str): Path to the output .obj file.
157
+ \t\tarray (NDArray): Numpy array to be dumped (must be 3D).
158
+ \t\tthreshold (float): Threshold level for marching cubes (0.5 for binary data).
159
+ \t\tstep_size (int): Step size for marching cubes (higher = simpler mesh, faster generation).
160
+ \t\tpad_array (bool): If True, pad array with zeros to ensure closed volumes for border cells.
161
+ \t\tverbose (int): Verbosity level (0 = no output, 1 = some output, 2 = full output).
162
+
163
+ \tExamples:
164
+
165
+ \t\t.. code-block:: python
166
+
167
+ \t\t\t> array = np.random.rand(64, 64, 64) > 0.5 # Binary volume
168
+ \t\t\t> numpy_to_obj("output_mesh.obj", array, threshold=0.5, step_size=2, pad_array=True, verbose=1)
169
+
170
+ \t\t\t> array = my_3d_data # Some 3D numpy array (e.g. human lung scan)
171
+ \t\t\t> numpy_to_obj("output_mesh.obj", array, threshold=0.3)
172
+ \t'''
@@ -1,18 +1,18 @@
1
- """ Installer module for stouputils.
2
-
3
- Provides functions for platform-agnostic installation tasks by dispatching
4
- to platform-specific implementations (Windows, Linux/macOS).
5
-
6
- It handles getting installation paths, adding programs to the PATH environment variable,
7
- and installing programs from local zip files or URLs.
8
- """
9
- # ruff: noqa: F403
10
- # ruff: noqa: F405
11
-
12
- # Imports
13
- from .common import *
14
- from .downloader import *
15
- from .linux import *
16
- from .main import *
17
- from .windows import *
18
-
1
+ """ Installer module for stouputils.
2
+
3
+ Provides functions for platform-agnostic installation tasks by dispatching
4
+ to platform-specific implementations (Windows, Linux/macOS).
5
+
6
+ It handles getting installation paths, adding programs to the PATH environment variable,
7
+ and installing programs from local zip files or URLs.
8
+ """
9
+ # ruff: noqa: F403
10
+ # ruff: noqa: F405
11
+
12
+ # Imports
13
+ from .common import *
14
+ from .downloader import *
15
+ from .linux import *
16
+ from .main import *
17
+ from .windows import *
18
+
@@ -0,0 +1,5 @@
1
+ from .common import *
2
+ from .downloader import *
3
+ from .linux import *
4
+ from .main import *
5
+ from .windows import *
@@ -0,0 +1,39 @@
1
+ from ..print import warning as warning
2
+ from typing import Literal
3
+
4
+ def prompt_for_path(prompt_message: str, default_path: str) -> str:
5
+ """ Prompt the user to override a default path.
6
+
7
+ \tArgs:
8
+ \t\tprompt_message (str): The message to display to the user.
9
+ \t\tdefault_path (str): The default path to suggest.
10
+
11
+ \tReturns:
12
+ \t\tstr: The path entered by the user, or the default path if they pressed Enter.
13
+ \t"""
14
+ def ask_install_type(ask_global: int, default_local_path: str, default_global_path: str | None) -> Literal['g', 'l']:
15
+ ''' Determine the installation type (global \'g\' or local \'l\') based on user input.
16
+
17
+ \tArgs:
18
+ \t\task_global (int): 0 = ask, 1 = force global, 2 = force local.
19
+ \t\tdefault_local_path (str): The default local path.
20
+ \t\tdefault_global_path (str | None): The default global path (if applicable).
21
+
22
+ \tReturns:
23
+ \t\tLiteral["g", "l"]: \'g\' for global install, \'l\' for local install.
24
+
25
+ \tExamples:
26
+ \t\t.. code-block:: python
27
+
28
+ \t\t\t> # Ask the user while providing default paths
29
+ \t\t\t> install_choice: str = ask_install_type(0, f"{os.getcwd()}/MyProgram", "C:\\Program Files\\MyProgram")
30
+ \t\t\tg
31
+
32
+ \t\t\t> # Don\'t ask, force global
33
+ \t\t\t> install_choice: str = ask_install_type(1, ...)
34
+ \t\t\tg
35
+
36
+ \t\t\t> # Don\'t ask, force local
37
+ \t\t\t> install_choice: str = ask_install_type(2, ...)
38
+ \t\t\tl
39
+ \t'''
@@ -0,0 +1,24 @@
1
+ from ..print import info as info, warning as warning
2
+ from .main import install_program as install_program
3
+
4
+ def download_executable(download_urls: dict[str, str], program_name: str, append_to_path: str = '') -> bool:
5
+ """ Ask the user if they want to download the program (ex: waifu2x-ncnn-vulkan).
6
+ \tIf yes, try to download the program from the GitHub releases page.
7
+
8
+ \tArgs:
9
+ \t\tdownload_urls (dict[str, str]): The URLs to download the program from.
10
+ \t\tprogram_name (str): The name of the program to download.
11
+
12
+ \tReturns:
13
+ \t\tbool: True if the program is now ready to use, False otherwise.
14
+ \t"""
15
+ def check_executable(executable: str, executable_help_text: str, download_urls: dict[str, str], append_to_path: str = '') -> None:
16
+ ''' Check if the executable exists, optionally download it if it doesn\'t.
17
+
18
+ \tArgs:
19
+ \t\texecutable (str): The path to the executable.
20
+ \t\texecutable_help_text (str): The help text to check for in the executable\'s output.
21
+ \t\tdownload_urls (dict[str, str]): The URLs to download the executable from.
22
+ \t\tappend_to_path (str): The path to append to the executable\'s path.
23
+ \t\t\t(ex: "bin" if executables are in the bin folder)
24
+ \t'''