diff --git a/python/damask/_result.py b/python/damask/_result.py index e832ad573..1a301a27c 100644 --- a/python/damask/_result.py +++ b/python/damask/_result.py @@ -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 diff --git a/python/damask/util.py b/python/damask/util.py index 3f0846588..735db39a0 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -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