From 989992004dfcc142a408cba2b8ccf7f0e2193397 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 30 Mar 2021 19:41:36 +0200 Subject: [PATCH] cleaning dicts --- python/damask/_result.py | 11 +++++++---- python/damask/util.py | 21 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/python/damask/_result.py b/python/damask/_result.py index 3affa2af8..e832ad573 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -1318,11 +1318,11 @@ class Result: v.save(f'{self.fname.stem}_inc{inc[ln:].zfill(N_digits)}') - def read(self,labels): + def read(self,labels,compress=True,strip=True): r = {} - labels_ = labels if isinstance(labels,list) else [labels] # check for arbitrary iterable + labels_ = [labels] if isinstance(labels,str) else labels with h5py.File(self.fname,'r') as f: - for inc in util.show_progress(self.visible['increments'],len(self.visible['increments'])): + for inc in util.show_progress(self.visible['increments']): r[inc] = {'phase':{},'homogenization':{}} for ph in self.visible['phases']: r[inc]['phase'][ph] = {} @@ -1340,13 +1340,16 @@ class Result: if da in labels_: r[inc]['homogenization'][ho][me][da] = \ f[os.path.join(inc,'homogenization',ho,me,da)][()] + + if strip: r = util.dict_strip(r) + return r def place(self,labels,fill_int=0,fill_float=0,constituents=None): r = {} - labels_ = labels if isinstance(labels,list) else [labels] # check for arbitrary iterable + labels_ = [labels] if isinstance(labels,str) else labels if constituents is None: constituents_ = range(self.N_constituents) else: diff --git a/python/damask/util.py b/python/damask/util.py index 902e0409c..3f0846588 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -25,7 +25,8 @@ __all__=[ 'execution_stamp', 'shapeshifter', 'shapeblender', 'extend_docstring', 'extended_docstring', - 'DREAM3D_base_group', 'DREAM3D_cell_data_group' + 'DREAM3D_base_group', 'DREAM3D_cell_data_group', + 'dict_strip', 'dict_compress' ] # https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py @@ -130,7 +131,7 @@ def show_progress(iterable,N_iter=None,prefix='',bar_length=50): Character length of bar. Defaults to 50. """ - status = _ProgressBar(N_iter if N_iter else len(iterable),prefix,bar_length) + status = _ProgressBar(N_iter if N_iter is not None else len(iterable),prefix,bar_length) for i,item in enumerate(iterable): yield item @@ -398,6 +399,22 @@ def DREAM3D_cell_data_group(fname): return cell_data_group +def dict_strip(a_dict): + # https://stackoverflow.com/questions/48151953 + new_dict = {} + for k, v in a_dict.items(): + if isinstance(v, dict): + v = dict_strip(v) + if not isinstance(v,dict) or v != {}: + new_dict[k] = v + return new_dict + + +def dict_compress(a_dict): + + + return None + #################################################################################################### # Classes