return minimal unique dictionaries per default

This commit is contained in:
Martin Diehl 2021-03-30 21:39:14 +02:00
parent 989992004d
commit 60119c1300
2 changed files with 21 additions and 5 deletions

View File

@ -1342,18 +1342,18 @@ class Result:
f[os.path.join(inc,'homogenization',ho,me,da)][()]
if strip: r = util.dict_strip(r)
if compress: r = util.dict_compress(r)
return r
def place(self,labels,fill_int=0,fill_float=0,constituents=None):
def place(self,labels,compress=True,strip=True,constituents=None,fill_int=0,fill_float=0.0):
r = {}
labels_ = [labels] if isinstance(labels,str) else labels
if constituents is None:
constituents_ = range(self.N_constituents)
else:
constituents_ = labels if isinstance(labels,list) else [labels] # allow abribtrary iterable
constituents_ = labels if isinstance(labels,list) else [labels] # fix
grp = 'mapping' if self.version_minor < 12 else 'cell_to'
@ -1388,4 +1388,6 @@ class Result:
dt = np.dtype(data.dtype,metadata={'description':description,
'unit':unit})
r[inc]['phase'][me][da] = ma.empty((N_cells,)+data.shape[1:],dtype=dt)
if strip: r = util.dict_strip(r)
if compress: r = util.dict_compress(r)
return r

View File

@ -411,10 +411,24 @@ def dict_strip(a_dict):
def dict_compress(a_dict):
# https://stackoverflow.com/questions/48151953
if isinstance(a_dict,dict) and len(a_dict) == 1:
key = list(a_dict.keys())[0]
entry = a_dict[key]
if isinstance(entry,dict):
new_dict = dict_compress(entry.copy())
else:
new_dict = entry
else:
new_dict = {}
for k, v in a_dict.items():
if isinstance(v, dict):
v = dict_compress(v)
if not isinstance(v,dict) or v != {}:
new_dict[k] = v
return new_dict
return None
####################################################################################################
# Classes