datastock 0.0.32__py3-none-any.whl → 0.0.34__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.
datastock/_saveload.py CHANGED
@@ -3,6 +3,8 @@
3
3
  import os
4
4
  import getpass
5
5
  import datetime as dtm
6
+ import itertools as itt
7
+ import warnings
6
8
 
7
9
 
8
10
  import numpy as np
@@ -13,6 +15,9 @@ from . import _generic_check
13
15
  from . import _generic_utils
14
16
 
15
17
 
18
+ _KEY_SEP = '--sep--'
19
+
20
+
16
21
  # #################################################################
17
22
  # #################################################################
18
23
  # Save
@@ -21,6 +26,7 @@ from . import _generic_utils
21
26
 
22
27
  def save(
23
28
  dflat=None,
29
+ sep=None,
24
30
  name=None,
25
31
  path=None,
26
32
  clsname=None,
@@ -78,6 +84,9 @@ def save(
78
84
  dt = dtm.datetime.now().strftime("%Y%m%d-%H%M%S")
79
85
  name = f'{clsname}_{name}_{user}_{dt}.npz'
80
86
 
87
+ # add sep
88
+ dflat[_KEY_SEP] = sep
89
+
81
90
  # save
82
91
  pfe = os.path.join(path, name)
83
92
  np.savez(pfe, **dflat)
@@ -145,6 +154,17 @@ def load(
145
154
 
146
155
  dflat = dict(np.load(pfe, allow_pickle=allow_pickle))
147
156
 
157
+ # ------------------------------
158
+ # load sep from file if exists
159
+
160
+ if _KEY_SEP in dflat.keys():
161
+ # new
162
+ sep = str(dflat[_KEY_SEP])
163
+ del dflat[_KEY_SEP]
164
+ else:
165
+ # back-compatibility
166
+ sep = [k0 for k0 in dflat.keys() if k0.startswith('_dref')][0][5]
167
+
148
168
  # ----------
149
169
  # reshape
150
170
 
@@ -179,13 +199,19 @@ def load(
179
199
  dout[k0] = None
180
200
  elif typ == 'ndarray':
181
201
  dout[k0] = dflat[k0]
202
+ elif any([ss in typ for ss in ['csc_', 'bsr_', 'coo_', 'csr_', 'dia_', 'dok_', 'lil_']]):
203
+ assert typ in type(dflat[k0]).__name__
204
+ dout[k0] = dflat[k0]
182
205
  elif 'Unit' in typ:
183
206
  dout[k0] = asunits.Unit(v0.tolist())
184
207
  elif typ == 'type':
185
208
  dout[k0] = dflat[k0]
186
209
  else:
187
210
  msg = (
188
- f"Don't know how to deal with dflat['{k0}']: {typ}"
211
+ f"Don't know how to deal with dflat['{k0}']:\n"
212
+ f"\t- typ: {typ}\n"
213
+ f"\t- type: {type(dflat[k0])}\n"
214
+ f"\t- value: {dflat[k0]}\n"
189
215
  )
190
216
  raise Exception(msg)
191
217
 
@@ -210,156 +236,235 @@ def load(
210
236
 
211
237
 
212
238
  def get_files(
213
- path=None,
214
- patterns=None,
215
- pfe=None,
216
- dpath=None,
239
+ dpfe=None,
240
+ returnas=None,
241
+ strict=None,
217
242
  ):
218
- """ Return a dict of path keys associated to list of file names
219
-
220
- A pfe is a str describing the path, file name and extension
243
+ """ Return a dict of path keys associated to list of file names, or a list
244
+
245
+ dpfe can be:
246
+ - str: a valid file path
247
+ - list of str: a list of valid file paths
248
+ - dict with:
249
+ keys = valid path str
250
+ values =
251
+ - str: valid file names in the associated path
252
+ - str: pattern to be found in the files names in that path
253
+ - list of str: list of the above (file names or patterns)
221
254
 
222
- If pfe is provided, it is just checked
255
+ If pattern is (or contains) tuples, the str in tuples are exclusive
223
256
 
224
- If path / patterns is provided, return all files in path matching patterns
257
+ """
225
258
 
226
- If dpath is provided, must be a dict with:
227
- - keys: valid path
228
- - values: dict with 'patterns' or 'pfe'
259
+ # ------------------------------------------
260
+ # check inputs
261
+ # ------------------------------------------
229
262
 
230
- If pattern is (or contains) tuples, the str in tuples are exclusive
263
+ # -----------------
264
+ # check returnas
231
265
 
232
- """
266
+ returnas = _generic_check._check_var(
267
+ returnas, 'returnas',
268
+ allowed=[dict, list],
269
+ default=list,
270
+ )
233
271
 
234
- # ------
235
- # pick
272
+ # -----------------
273
+ # check dpfe
236
274
 
237
275
  lc = [
238
- pfe is not None,
239
- patterns is not None,
240
- dpath is not None,
276
+ isinstance(dpfe, (str, tuple)),
277
+ isinstance(dpfe, list) and all([isinstance(pp, (str, tuple)) for pp in dpfe]),
278
+ isinstance(dpfe, dict) and all([isinstance(pp, str) for pp in dpfe.keys()])
241
279
  ]
242
280
 
243
- if np.sum(lc) != 1:
244
- msg = "Please provide pfe xor pattern xor case!"
281
+ if not any(lc):
282
+ msg = (
283
+ "Please provide dpfe as\n"
284
+ "\t- str: a valid file path\n"
285
+ "\t- dict with:\n"
286
+ "\t\tkeys = valid path str\n"
287
+ "\t\tvalues =\n"
288
+ "\t\t\t- str: valid file names in the associated path\n"
289
+ "\t\t\t- str: pattern to be found in the files names in that path\n"
290
+ "\t\t\t- list of str: list of the above (file names or patterns)\n"
291
+ )
245
292
  raise Exception(msg)
246
293
 
294
+ # --------------------------------------------
295
+ # sort cases
296
+ # --------------------------------------------
297
+
247
298
  # -----------
248
- # check path
299
+ # str or list
300
+
301
+ # str
302
+ if lc[0]:
303
+ dpfe = [dpfe]
304
+ lc[0] = False
305
+ lc[1] = True
306
+
307
+ # list of str
308
+ if lc[1]:
309
+ # call check on list of files
310
+ lpfe = _get_files_from_path(
311
+ lpfe=dpfe,
312
+ path=None,
313
+ strict=strict,
314
+ )
315
+ dpfe = None
316
+
317
+ # -----------
318
+ # dict
319
+
320
+ if lc[2]:
321
+
322
+ dout = {}
323
+ for k0, v0 in dpfe.items():
324
+
325
+ # back-compatibility
326
+ if isinstance(v0, dict):
327
+ if v0.get('patterns') is not None:
328
+ v0 = v0['patterns']
329
+ elif v0.get('pfe') is not None:
330
+ v0 = v0['pfe']
331
+ else:
332
+ msg = ()
333
+
334
+ # call pfe / pattern recognition
335
+ lpfe = _get_files_from_path(
336
+ lpfe=v0,
337
+ path=k0,
338
+ strict=strict,
339
+ )
340
+ dout[k0] = [os.path.split(pfe)[1] for pfe in lpfe]
341
+
342
+ lpfe = None
343
+ dpfe = dout
344
+
345
+ # -------------------------------------------------
346
+ # returnas
347
+ # -------------------------------------------------
348
+
349
+ if returnas is list:
350
+ if lpfe is None:
351
+ lpfe = list(itt.chain.from_iterable([
352
+ [os.path.join(k0, v1) for v1 in v0]
353
+ for k0, v0 in dpfe.items()
354
+ ]))
355
+ out = lpfe
249
356
 
250
- if path is not None:
251
- if not (isinstance(path, str) and os.path.isdir(path)):
252
- msg = f"Arg path must be a valid path name!\n{path}"
253
- return Exception(msg)
254
357
  else:
255
- path = './'
256
- path = os.path.abspath(path)
358
+ if dpfe is None:
359
+ lpath = {pfe.split()[0] for pfe in lpfe}
360
+ dpfe = {
361
+ path: [pfe for pfe in lpfe if path == pfe.split()[0]]
362
+ for path in lpath
363
+ }
364
+ out = dpfe
257
365
 
258
- # -----------
259
- # check pfe
366
+ return out
260
367
 
261
- if isinstance(pfe, str):
262
- pfe = [pfe]
263
368
 
264
- if pfe is not None:
369
+ def _get_files_from_path(
370
+ lpfe=None,
371
+ path=None,
372
+ strict=None,
373
+ ):
265
374
 
266
- err = False
267
- assert isinstance(pfe, (list, tuple))
268
- lout = [pp for pp in pfe if not os.path.isfile(pp)]
375
+ # -----------------
376
+ # preliminary check
269
377
 
270
- # check that each file exists
271
- if len(lout) == len(pfe):
272
- pfe = [os.path.join(path, pp) for pp in pfe]
273
- lout = [pp for pp in pfe if not os.path.isfile(pp)]
274
- if len(lout) > 0:
275
- err = True
276
- elif len(lout) > 0:
277
- err = True
378
+ if isinstance(lpfe, (str, tuple)):
379
+ lpfe = [lpfe]
278
380
 
279
- # Exception
280
- if err is True:
281
- msg = f"The following files do not exist:\n{lout}"
282
- raise Exception(msg)
381
+ if not all([isinstance(pfe, (str, tuple)) for pfe in lpfe]):
382
+ msg = (
383
+ "Please provide a list of str (pfe or patterns)!\n"
384
+ f"\t- Provided: {lpfe}"
385
+ )
386
+ raise Exception(msg)
283
387
 
284
- # ---------------------------
285
- # check pattern
388
+ # path
389
+ if path is None:
390
+ path = os.path.abspath('.')
391
+ if not os.path.isdir(path):
392
+ msg = f"Provided path is not valid!\n\t- path: {path}"
393
+ raise Exception(msg)
286
394
 
287
- if patterns is not None:
395
+ # strict
396
+ strict = _generic_check._check_var(
397
+ strict, 'strict',
398
+ types=bool,
399
+ default=True,
400
+ )
401
+
402
+ # ---------------
403
+ # pfe vs patterns
288
404
 
289
- if isinstance(patterns, (str, tuple)):
290
- patterns = [patterns]
405
+ lc = [
406
+ any([os.path.isfile(pfe) for pfe in lpfe if isinstance(pfe, str)]),
407
+ any([os.path.isfile(os.path.join(path, pfe)) for pfe in lpfe if isinstance(pfe, str)]),
408
+ ]
409
+
410
+ # ---------------------
411
+ # valid pfe
291
412
 
292
- if not all([isinstance(pp, (str, tuple)) for pp in patterns]):
293
- msg = f"Arg patterns must be a list of str / tuple!\n{patterns}"
413
+ if lc[0] or lc[1]:
414
+
415
+ if lc[0]:
416
+ out = [pfe for pfe in lpfe if os.path.isfile(pfe)]
417
+ lfail = [pfe for pfe in lpfe if not os.path.isfile(pfe)]
418
+
419
+ else:
420
+ out = [
421
+ os.path.join(path, pfe) for pfe in lpfe
422
+ if os.path.isfile(os.path.join(path, pfe))
423
+ ]
424
+ lfail = [
425
+ os.path.join(path, pfe) for pfe in lpfe
426
+ if not os.path.isfile(os.path.join(path, pfe))
427
+ ]
428
+
429
+ if len(lfail) > 0:
430
+ msg = (
431
+ "The following files do not exist:\n"
432
+ + "\n".join([f"\t- {k0}" for k0 in lfail])
433
+ )
434
+ if strict is True:
435
+ raise Exception(msg)
436
+ else:
437
+ warnings.warn(msg)
438
+
439
+ # ---------------------
440
+ # patterns
441
+
442
+ else:
443
+
444
+ if not all([isinstance(pp, (str, tuple)) for pp in lpfe]):
445
+ msg = f"Arg patterns must be a list of str / tuple!\n{lpfe}"
294
446
  raise Exception(msg)
295
447
 
296
- pfe = sorted([
448
+ out = sorted([
297
449
  os.path.join(path, ff) for ff in os.listdir(path)
298
450
  if os.path.isfile(os.path.join(path, ff))
299
451
  if all([
300
452
  p0 in ff if isinstance(p0, str)
301
453
  else all([p1 not in ff for p1 in p0])
302
- for p0 in patterns
454
+ for p0 in lpfe
303
455
  ])
304
456
  ])
305
457
 
306
- # ---------------------
307
- # format pfe into dpfe
308
-
309
- if dpath is None:
310
-
311
- lpf = [os.path.split(ff) for ff in pfe]
312
- lpu = sorted(set([ff[0] for ff in lpf]))
313
-
314
- dpfe = {
315
- os.path.abspath(k0): [ff[1] for ff in lpf if ff[0] == k0]
316
- for k0 in lpu
317
- }
318
-
319
- # ----------------
320
- # check case
321
-
322
- if dpath is not None:
323
-
324
- # check format
325
- c0 = (
326
- isinstance(dpath, dict)
327
- and all([
328
- isinstance(k0, str)
329
- and os.path.isdir(k0)
330
- and (
331
- isinstance(v0, (str, list))
332
- or (
333
- isinstance(v0, dict)
334
- and isinstance(v0.get('patterns', ''), (list, str, tuple))
335
- and isinstance(v0.get('pfe', ''), (list, str))
336
- )
337
- )
338
- for k0, v0 in dpath.items()
339
- ])
340
- )
341
- if not c0:
458
+ # safety check
459
+ if len(out) == 0:
342
460
  msg = (
343
- "Arg dpath must be a dict with:\n"
344
- "\t- keys: valid paths\n"
345
- "\t- values: dict with 'patterns' xor 'pfe'\n"
346
- f"Provided:\n{dpath}"
461
+ "The following list of files is empty:\n"
462
+ f"\t- lpfe = {lpfe}\n"
463
+ f"\t- path: {path}"
347
464
  )
348
- raise Exception(msg)
349
-
350
- # str => patterns
351
- for k0, v0 in dpath.items():
352
- if isinstance(v0, (str, list)):
353
- dpath[k0] = {'patterns': v0}
354
-
355
- # append list of files
356
- dpfe = {}
357
- for k0, v0 in dpath.items():
358
- dpfe.update(get_files(
359
- path=k0,
360
- pfe=v0.get('pfe'),
361
- patterns=v0.get('patterns'),
362
- dpath=None,
363
- ))
465
+ if strict is True:
466
+ raise Exception(msg)
467
+ else:
468
+ warnings.warn(msg)
364
469
 
365
- return dpfe
470
+ return out
datastock/version.py CHANGED
@@ -1,2 +1,2 @@
1
1
  # Do not edit, pipeline versioning governed by git tags!
2
- __version__ = '0.0.32'
2
+ __version__ = '0.0.34'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: datastock
3
- Version: 0.0.32
3
+ Version: 0.0.34
4
4
  Summary: A python library for generic class and data handling
5
5
  Home-page: https://github.com/ToFuProject/datastock
6
6
  Author: Didier VEZINET
@@ -2,9 +2,9 @@ _updateversion.py,sha256=OR6OJJozaHWzu7NWjKu5ERi0IyYqR61KrWvzf7kfoto,951
2
2
  datastock/_DataCollection_utils.py,sha256=hHf6HvGKMmM-psx3fj9QcY1TEmKrAtTdkRokH7SFqoo,7143
3
3
  datastock/__init__.py,sha256=MAUJgQG1Mz4Jq9Eu70T9phcV83iFjzLC1qjlqJZDbq4,183
4
4
  datastock/_class.py,sha256=Az9PS3aSskiPMb1ekt78Y2ynBujYVc_cDjJxW9xH9g4,47
5
- datastock/_class0.py,sha256=oV6Ar7in66OnjsDK7RRhe_OSt-ifBKul-x2NbucjF98,5442
5
+ datastock/_class0.py,sha256=QULjNJke13jJrGLIeM7SWHZVziorDK_KCIlqq8LgS9U,5883
6
6
  datastock/_class1.py,sha256=l4W0atz_Q-ykO45eONzDtXkwCkcPKwuf2EuD9Ztwak4,29435
7
- datastock/_class1_binning.py,sha256=G1rbejlwoDRYANI_LY4QOKWA-BEikddVM3-EklROoZQ,28336
7
+ datastock/_class1_binning.py,sha256=LWHv2LIfgZfSFWYwqdcN0DKpNe6q7Go3sxfcJqmzTrI,28085
8
8
  datastock/_class1_check.py,sha256=QN0o5Z9XOZAUIoe-dXTVNmbkj3l_v7U4lawcn8N0Yrw,52436
9
9
  datastock/_class1_compute.py,sha256=9ka4MXqTb2Gwe8ZnZR31rd5QDc7-nxuhFqXpQSn_sss,28211
10
10
  datastock/_class1_domain.py,sha256=TudSRoLedzU2qGOU6R1_Lx99MuqJzG0rOZl8-HZZfTU,6252
@@ -17,7 +17,7 @@ datastock/_direct_calls.py,sha256=EHFwI2mGMDqGz8_Bv2BseMBX4J8dSdE_RcNX3pt0ZYY,18
17
17
  datastock/_export_dataframe.py,sha256=fy-uJR3EhDlHvd9ls1EQna_C8fyha1jCJLu1DTKTkdo,1576
18
18
  datastock/_find_plateau.py,sha256=sqnAuy0361DXkqBb_Lo1MmIGjn35tnKFvcv6MW6hifs,2685
19
19
  datastock/_generic_check.py,sha256=nfD4PIInb63uiwioknFf78dE5lGpgSxoOvT5BgP2nts,24443
20
- datastock/_generic_utils.py,sha256=iSZa_XOhOJW1Rtr5kH2dxcs_2WaAxcuFls_eEKgOOTw,15382
20
+ datastock/_generic_utils.py,sha256=1RTAcMHVTmBRyz0kvpIBemV-ULxluxA01zMssuafZdk,21243
21
21
  datastock/_plot_BvsA_as_distribution.py,sha256=fpRhlbv3Bk96buANC46Brc9hdLxkOAsoKpE5A9pohG0,15389
22
22
  datastock/_plot_BvsA_as_distribution_check.py,sha256=7OkzsxqXvJBe6uc_7IiZNwMYWryyI1vKUYYRCkPfz7Q,13002
23
23
  datastock/_plot_as_array.py,sha256=s3Jl0B_1gJ-SewSGfyjXrtzMG1utvcq-Vyb0Vmn-rzI,75861
@@ -26,13 +26,13 @@ datastock/_plot_as_profile1d.py,sha256=rxMpLL-QMHqHiAYjH7gDXZOUkMVz3ps8SlUUVNXK8
26
26
  datastock/_plot_correlations.py,sha256=ITOypu_AEoKl0ihxocV-JVTXIHqut6p9TfG-xZmQysc,10175
27
27
  datastock/_plot_misc.py,sha256=XixTi2CiihKjtQP0TRycH0b25caWN1m35DgpsDeiWZE,21729
28
28
  datastock/_plot_text.py,sha256=wQPqjfpLyIioS2JeOt3E9C9HgYUJ49YEoOgRuKYvAR8,3143
29
- datastock/_saveload.py,sha256=8SM5AdRbcy2KZfDEq5Uesvf55DXzLvPxTMUUsZqhhmk,8885
30
- datastock/version.py,sha256=DoX-bepDn_EGe6LwIcMmvJjqwD_t5S7ZUGxeWnZ0YXA,80
29
+ datastock/_saveload.py,sha256=Ycu8-OXeak4gqPOJMCE_oeBSZRqdxsqSJs84ZMK4UQg,11663
30
+ datastock/version.py,sha256=ZPzh1gh33VoRYnkFgdCZ0tQKhQLqL6wrNOmoy2vEm0M,80
31
31
  datastock/tests/__init__.py,sha256=teOo2xP0IO7PQMuMDmum61XVHe2TuxW3BiHiL73X8jQ,35
32
32
  datastock/tests/test_01_DataStock.py,sha256=rIDlqIGGR6gCFtjAw66G9CF3-Mne95XF0eXtUU7wYfc,15260
33
33
  datastock/tests/output/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
34
- datastock-0.0.32.dist-info/LICENSE,sha256=V1uXqi3vxR0QhB4QdFyjkynl6jpN4wZmlB5EMYJk0NM,1068
35
- datastock-0.0.32.dist-info/METADATA,sha256=7V40HnzvYSYxee3fNKOk7FVtvwpVgABiz7ETfUAulZQ,8666
36
- datastock-0.0.32.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
37
- datastock-0.0.32.dist-info/top_level.txt,sha256=BzJsLLK_zZw13WQCoMhC74qWVKalnVCjBxdPXvJn7HQ,25
38
- datastock-0.0.32.dist-info/RECORD,,
34
+ datastock-0.0.34.dist-info/LICENSE,sha256=V1uXqi3vxR0QhB4QdFyjkynl6jpN4wZmlB5EMYJk0NM,1068
35
+ datastock-0.0.34.dist-info/METADATA,sha256=tFeEmOoG0f9tFVraH_cu38XphCD8rcBL7ufMj5JCBls,8666
36
+ datastock-0.0.34.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
37
+ datastock-0.0.34.dist-info/top_level.txt,sha256=BzJsLLK_zZw13WQCoMhC74qWVKalnVCjBxdPXvJn7HQ,25
38
+ datastock-0.0.34.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.41.2)
2
+ Generator: bdist_wheel (0.42.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5