ProgramComputer commited on
Commit
e1e898e
1 Parent(s): 670c546

Update test.py

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