ProgramComputer
commited on
Commit
•
30420d9
1
Parent(s):
950b4cb
Update test.py
Browse files
test.py
CHANGED
@@ -125,6 +125,100 @@ class NestedDataStructure:
|
|
125 |
else:
|
126 |
return [data]
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
def _mp_download(
|
129 |
url,
|
130 |
tmp_path,
|
|
|
125 |
else:
|
126 |
return [data]
|
127 |
|
128 |
+
def map_nested(
|
129 |
+
function: Callable[[Any], Any],
|
130 |
+
data_struct: Any,
|
131 |
+
dict_only: bool = False,
|
132 |
+
map_list: bool = True,
|
133 |
+
map_tuple: bool = False,
|
134 |
+
map_numpy: bool = False,
|
135 |
+
num_proc: Optional[int] = None,
|
136 |
+
parallel_min_length: int = 2,
|
137 |
+
types: Optional[tuple] = None,
|
138 |
+
disable_tqdm: bool = True,
|
139 |
+
desc: Optional[str] = None,
|
140 |
+
) -> Any:
|
141 |
+
"""Apply a function recursively to each element of a nested data struct.
|
142 |
+
|
143 |
+
Use multiprocessing if num_proc > 1 and the length of data_struct is greater than or equal to
|
144 |
+
`parallel_min_length`.
|
145 |
+
|
146 |
+
<Changed version="2.5.0">
|
147 |
+
|
148 |
+
Before version 2.5.0, multiprocessing was not used if `num_proc` was greater than or equal to ``len(iterable)``.
|
149 |
+
|
150 |
+
Now, if `num_proc` is greater than or equal to ``len(iterable)``, `num_proc` is set to ``len(iterable)`` and
|
151 |
+
multiprocessing is used.
|
152 |
+
|
153 |
+
</Changed>
|
154 |
+
|
155 |
+
Args:
|
156 |
+
function (`Callable`): Function to be applied to `data_struct`.
|
157 |
+
data_struct (`Any`): Data structure to apply `function` to.
|
158 |
+
dict_only (`bool`, default `False`): Whether only apply `function` recursively to `dict` values in
|
159 |
+
`data_struct`.
|
160 |
+
map_list (`bool`, default `True`): Whether also apply `function` recursively to `list` elements (besides `dict`
|
161 |
+
values).
|
162 |
+
map_tuple (`bool`, default `False`): Whether also apply `function` recursively to `tuple` elements (besides
|
163 |
+
`dict` values).
|
164 |
+
map_numpy (`bool, default `False`): Whether also apply `function` recursively to `numpy.array` elements (besides
|
165 |
+
`dict` values).
|
166 |
+
num_proc (`int`, *optional*): Number of processes.
|
167 |
+
parallel_min_length (`int`, default `2`): Minimum length of `data_struct` required for parallel
|
168 |
+
processing.
|
169 |
+
<Added version="2.5.0"/>
|
170 |
+
types (`tuple`, *optional*): Additional types (besides `dict` values) to apply `function` recursively to their
|
171 |
+
elements.
|
172 |
+
disable_tqdm (`bool`, default `True`): Whether to disable the tqdm progressbar.
|
173 |
+
desc (`str`, *optional*): Prefix for the tqdm progressbar.
|
174 |
+
|
175 |
+
Returns:
|
176 |
+
`Any`
|
177 |
+
"""
|
178 |
+
if types is None:
|
179 |
+
types = []
|
180 |
+
if not dict_only:
|
181 |
+
if map_list:
|
182 |
+
types.append(list)
|
183 |
+
if map_tuple:
|
184 |
+
types.append(tuple)
|
185 |
+
if map_numpy:
|
186 |
+
types.append(np.ndarray)
|
187 |
+
types = tuple(types)
|
188 |
+
|
189 |
+
# Singleton
|
190 |
+
if not isinstance(data_struct, dict) and not isinstance(data_struct, types):
|
191 |
+
return function(data_struct)
|
192 |
+
|
193 |
+
disable_tqdm = disable_tqdm or not logging.is_progress_bar_enabled()
|
194 |
+
iterable = list(data_struct.values()) if isinstance(data_struct, dict) else data_struct
|
195 |
+
|
196 |
+
if num_proc is None:
|
197 |
+
num_proc = 1
|
198 |
+
if num_proc != -1 and num_proc <= 1 or len(iterable) < parallel_min_length:
|
199 |
+
mapped = [
|
200 |
+
_single_map_nested((function, obj, types, None, True, None))
|
201 |
+
for obj in logging.tqdm(iterable, disable=disable_tqdm, desc=desc)
|
202 |
+
]
|
203 |
+
else:
|
204 |
+
with warnings.catch_warnings():
|
205 |
+
warnings.filterwarnings(
|
206 |
+
"ignore",
|
207 |
+
message=".* is experimental and might be subject to breaking changes in the future\\.$",
|
208 |
+
category=UserWarning,
|
209 |
+
)
|
210 |
+
mapped = parallel_map(function, iterable, num_proc, types, disable_tqdm, desc, _single_map_nested)
|
211 |
+
|
212 |
+
if isinstance(data_struct, dict):
|
213 |
+
return dict(zip(data_struct.keys(), mapped))
|
214 |
+
else:
|
215 |
+
if isinstance(data_struct, list):
|
216 |
+
return mapped
|
217 |
+
elif isinstance(data_struct, tuple):
|
218 |
+
return tuple(mapped)
|
219 |
+
else:
|
220 |
+
return np.array(mapped)
|
221 |
+
|
222 |
def _mp_download(
|
223 |
url,
|
224 |
tmp_path,
|