89 |
90 |
Source code for geometricus.utility
91 | import typing
92 | from pathlib import Path
93 |
94 | import numba as nb
95 | import numpy as np
96 |
97 |
98 | [docs]@nb.njit
99 |
def nan_normalize(numbers):
100 |
minv, maxv = np.nanmin(numbers), np.nanmax(numbers)
101 |
return (numbers - minv) / (maxv - minv)
102 |
103 |
104 | [docs]@nb.njit
105 |
def normalize(numbers):
106 |
minv, maxv = np.min(numbers), np.max(numbers)
107 |
return (numbers - minv) / (maxv - minv)
108 |
109 |
110 | [docs]@nb.njit
111 |
def nb_mean_axis_0(array: np.ndarray) -> np.ndarray:
112 |
"""
113 |
Same as np.mean(array, axis=0) but njitted
114 |
"""
115 |
mean_array = np.zeros(array.shape[1])
116 |
for i in range(array.shape[1]):
117 |
mean_array[i] = np.mean(array[:, i])
118 |
return mean_array
119 |
120 |
121 | [docs]def get_file_parts(input_filename: typing.Union[str, Path]) -> tuple:
122 |
"""
123 |
Gets directory path, name, and extension from a filename
124 |
Parameters
125 |
----------
126 |
input_filename
127 |
128 |
Returns
129 |
-------
130 |
(path, name, extension)
131 |
"""
132 |
input_filename = Path(input_filename)
133 |
path = str(input_filename.parent)
134 |
extension = input_filename.suffix
135 |
name = input_filename.stem
136 |
return path, name, extension
137 |
138 |
139 | [docs]def group_indices(input_list: list) -> list:
140 |
"""
141 |
[1, 1, 1, 2, 2, 3, 3, 3, 4] -> [[0, 1, 2], [3, 4], [5, 6, 7], [8]]
142 |
Parameters
143 |
----------
144 |
input_list
145 |
146 |
Returns
147 |
-------
148 |
list of lists
149 |
"""
150 |
output_list = []
151 |
current_list = []
152 |
current_index = None
153 |
for i in range(len(input_list)):
154 |
if current_index is None:
155 |
current_index = input_list[i]
156 |
if input_list[i] == current_index:
157 |
current_list.append(i)
158 |
else:
159 |
output_list.append(current_list)
160 |
current_list = [i]
161 |
current_index = input_list[i]
162 |
output_list.append(current_list)
163 |
return output_list
164 |
165 |
166 |