cleaning dicts

This commit is contained in:
Martin Diehl 2021-03-30 19:41:36 +02:00
parent b19ec4f8ae
commit 989992004d
2 changed files with 26 additions and 6 deletions

View File

@ -1318,11 +1318,11 @@ class Result:
v.save(f'{self.fname.stem}_inc{inc[ln:].zfill(N_digits)}') 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 = {} 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: 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':{}} r[inc] = {'phase':{},'homogenization':{}}
for ph in self.visible['phases']: for ph in self.visible['phases']:
r[inc]['phase'][ph] = {} r[inc]['phase'][ph] = {}
@ -1340,13 +1340,16 @@ class Result:
if da in labels_: if da in labels_:
r[inc]['homogenization'][ho][me][da] = \ r[inc]['homogenization'][ho][me][da] = \
f[os.path.join(inc,'homogenization',ho,me,da)][()] f[os.path.join(inc,'homogenization',ho,me,da)][()]
if strip: r = util.dict_strip(r)
return r return r
def place(self,labels,fill_int=0,fill_float=0,constituents=None): def place(self,labels,fill_int=0,fill_float=0,constituents=None):
r = {} 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: if constituents is None:
constituents_ = range(self.N_constituents) constituents_ = range(self.N_constituents)
else: else:

View File

@ -25,7 +25,8 @@ __all__=[
'execution_stamp', 'execution_stamp',
'shapeshifter', 'shapeblender', 'shapeshifter', 'shapeblender',
'extend_docstring', 'extended_docstring', '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 # 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. 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): for i,item in enumerate(iterable):
yield item yield item
@ -398,6 +399,22 @@ def DREAM3D_cell_data_group(fname):
return cell_data_group 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 # Classes