polished and added tests

This commit is contained in:
Martin Diehl 2021-04-01 15:52:43 +02:00
parent 71d1a341e4
commit 84e117c6b3
10 changed files with 77 additions and 57 deletions

View File

@ -1326,7 +1326,7 @@ 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,compress=True,strip=True): def read(self,output,compress=True,strip=True):
""" """
Export data from file per phase/homogenization. Export data from file per phase/homogenization.
@ -1335,8 +1335,8 @@ class Result:
Parameters Parameters
---------- ----------
labels : str or list of, optional output : str or list of, optional
Labels of the datasets to be read. Name of the datasets to include.
compress : bool compress : bool
Squeeze out dictionaries that are not needed for a unique Squeeze out dictionaries that are not needed for a unique
structure. This might be beneficial in the case of single structure. This might be beneficial in the case of single
@ -1348,22 +1348,22 @@ class Result:
""" """
r = {} r = {}
labels_ = set([labels] if isinstance(labels,str) else labels) output_ = set([output] if isinstance(output,str) else output)
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']): for inc in util.show_progress(self.visible['increments']):
r[inc] = {'phase':{},'homogenization':{},'geometry':{}} r[inc] = {'phase':{},'homogenization':{},'geometry':{}}
for la in labels_.intersection(f['/'.join((inc,'geometry'))].keys()): for out in output_.intersection(f['/'.join((inc,'geometry'))].keys()):
r[inc]['geometry'][la] = _read(f,'/'.join((inc,'geometry',la))) r[inc]['geometry'][out] = _read(f,'/'.join((inc,'geometry',out)))
for ty in ['phase','homogenization']: for ty in ['phase','homogenization']:
for na in self.visible[ty+'s']: for label in self.visible[ty+'s']:
r[inc][ty][na] = {} r[inc][ty][label] = {}
for field in f['/'.join((inc,ty,na))].keys(): for field in f['/'.join((inc,ty,label))].keys():
r[inc][ty][na][field] = {} r[inc][ty][label][field] = {}
for la in labels_.intersection(f['/'.join((inc,ty,na,field))].keys()): for out in output_.intersection(f['/'.join((inc,ty,label,field))].keys()):
r[inc][ty][na][field][la] = _read(f,'/'.join((inc,ty,na,field,la))) r[inc][ty][label][field][out] = _read(f,'/'.join((inc,ty,label,field,out)))
if strip: r = util.dict_strip(r) if strip: r = util.dict_strip(r)
if compress: r = util.dict_compress(r) if compress: r = util.dict_compress(r)
@ -1371,7 +1371,7 @@ class Result:
return r return r
def place(self,labels,compress=True,strip=True,constituents=None,fill_float=0.0,fill_int=0): def place(self,output,compress=True,strip=True,constituents=None,fill_float=0.0,fill_int=0):
""" """
Export data from file suitable sorted for spatial operations. Export data from file suitable sorted for spatial operations.
@ -1384,7 +1384,7 @@ class Result:
Parameters Parameters
---------- ----------
labels : str or list of, optional output : str or list of, optional
Labels of the datasets to be read. Labels of the datasets to be read.
compress : bool compress : bool
Squeeze out dictionaries that are not needed for a unique Squeeze out dictionaries that are not needed for a unique
@ -1403,16 +1403,17 @@ class Result:
fill_int : int fill_int : int
Fill value for non existent entries of integer type. Fill value for non existent entries of integer type.
Defaults to 0. Defaults to 0.
""" """
r = {} r = {}
labels_ = set([labels] if isinstance(labels,str) else labels) output_ = set([output] if isinstance(output,str) else output)
if constituents is None: if constituents is None:
constituents_ = range(self.N_constituents) constituents_ = range(self.N_constituents)
else: else:
constituents_ = constituents if isinstance(constituents,Iterable) else [constituents] constituents_ = constituents if isinstance(constituents,Iterable) else [constituents]
suffixes = [''] if self.N_constituents == 1 or len(constituents_) == 1 else \ suffixes = [''] if self.N_constituents == 1 or isinstance(constituents,int) else \
[f'#{c}' for c in constituents_] [f'#{c}' for c in constituents_]
grp = 'mapping' if self.version_minor < 12 else 'cell_to' grp = 'mapping' if self.version_minor < 12 else 'cell_to'
@ -1423,10 +1424,10 @@ class Result:
at_cell_ph = [] at_cell_ph = []
in_data_ph = [] in_data_ph = []
for c in constituents_: for c in range(self.N_constituents):
at_cell_ph.append({label: np.where(f[os.path.join(grp,'phase')][:,c][name] == label.encode())[0] \ at_cell_ph.append({label: np.where(f[os.path.join(grp,'phase')][:,c][name] == label.encode())[0] \
for label in self.visible['phases']}) for label in self.visible['phases']})
in_data_ph.append({label: f[os.path.join(grp,'phase')][member][at_cell_ph[c][label]][...,0] \ in_data_ph.append({label: f[os.path.join(grp,'phase')][member][at_cell_ph[c][label]][...,c] \
for label in self.visible['phases']}) for label in self.visible['phases']})
at_cell_ho = {label: np.where(f[os.path.join(grp,'homogenization')][:][name] == label.encode())[0] \ at_cell_ho = {label: np.where(f[os.path.join(grp,'homogenization')][:][name] == label.encode())[0] \
@ -1437,45 +1438,41 @@ class Result:
for inc in util.show_progress(self.visible['increments']): for inc in util.show_progress(self.visible['increments']):
r[inc] = {'phase':{},'homogenization':{},'geometry':{}} r[inc] = {'phase':{},'homogenization':{},'geometry':{}}
for la in labels_.intersection(f[os.path.join(inc,'geometry')].keys()): for out in output_.intersection(f[os.path.join(inc,'geometry')].keys()):
r[inc]['geometry'][la] = _read(f,os.path.join(inc,'geometry',la)) r[inc]['geometry'][out] = _read(f,os.path.join(inc,'geometry',out))
for ph in self.visible['phases']: for ty in ['phase','homogenization']:
for field in f[os.path.join(inc,'phase',ph)].keys(): for label in self.visible[ty+'s']:
if field not in r[inc]['phase'].keys(): for field in f[os.path.join(inc,ty,label)].keys():
r[inc]['phase'][field] = {} if field not in r[inc][ty].keys():
r[inc][ty][field] = {}
for la in labels_.intersection(f[os.path.join(inc,'phase',ph,field)].keys()): for out in output_.intersection(f[os.path.join(inc,ty,label,field)].keys()):
data = ma.array(_read(f,os.path.join(inc,'phase',ph,field,la))) data = ma.array(_read(f,os.path.join(inc,ty,label,field,out)))
if la+suffixes[0] not in r[inc]['phase'][field].keys(): if ty == 'phase':
if out+suffixes[0] not in r[inc][ty][field].keys():
container = np.empty((self.N_materialpoints,)+data.shape[1:],dtype=data.dtype) container = np.empty((self.N_materialpoints,)+data.shape[1:],dtype=data.dtype)
fill_value = fill_float if data.dtype in np.sctypes['float'] else \ fill_value = fill_float if data.dtype in np.sctypes['float'] else \
fill_int fill_int
for c,suffix in zip(constituents_, suffixes): for c,suffix in zip(constituents_, suffixes):
r[inc]['phase'][field][la+suffix] = \ r[inc][ty][field][out+suffix] = \
ma.array(container,fill_value=fill_value,mask=True) ma.array(container,fill_value=fill_value,mask=True)
for c,suffix in zip(constituents_, suffixes): for c,suffix in zip(constituents_, suffixes):
r[inc]['phase'][field][la+suffix][at_cell_ph[c][ph]] = data[in_data_ph[c][ph]] r[inc][ty][field][out+suffix][at_cell_ph[c][label]] = data[in_data_ph[c][label]]
for ho in self.visible['homogenizations']: if ty == 'homogenization':
for field in f[os.path.join(inc,'homogenization',ho)].keys(): if out not in r[inc][ty][field].keys():
if field not in r[inc]['homogenization'].keys():
r[inc]['homogenization'][field] = {}
for la in labels_.intersection(f[os.path.join(inc,'homogenization',ho,field)].keys()):
data = ma.array(_read(f,os.path.join(inc,'homogenization',ho,field,la)))
if la not in r[inc]['homogenization'][field].keys():
container = np.empty((self.N_materialpoints,)+data.shape[1:],dtype=data.dtype) container = np.empty((self.N_materialpoints,)+data.shape[1:],dtype=data.dtype)
fill_value = fill_float if data.dtype in np.sctypes['float'] else \ fill_value = fill_float if data.dtype in np.sctypes['float'] else \
fill_int fill_int
r[inc]['homogenization'][field][la] = \ r[inc][ty][field][out] = \
ma.array(container,fill_value=fill_value,mask=True) ma.array(container,fill_value=fill_value,mask=True)
r[inc]['homogenization'][field][la][at_cell_ho[ho]] = data[in_data_ho[ho]] r[inc][ty][field][out][at_cell_ho[label]] = data[in_data_ho[label]]
if strip: r = util.dict_strip(r) if strip: r = util.dict_strip(r)
if compress: r = util.dict_compress(r) if compress: r = util.dict_compress(r)
return r return r

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -401,7 +401,7 @@ class TestResult:
with pytest.raises(TypeError): with pytest.raises(TypeError):
default.save_XDMF() default.save_XDMF()
@pytest.mark.parametrize('view,labels,compress,strip', @pytest.mark.parametrize('view,output,compress,strip',
[({},['F','P','F','L_p','F_e','F_p'],True,True), [({},['F','P','F','L_p','F_e','F_p'],True,True),
({'increments':3},'F',True,True), ({'increments':3},'F',True,True),
({'increments':[1,8,3,4,5,6,7]},['F','P'],True,True), ({'increments':[1,8,3,4,5,6,7]},['F','P'],True,True),
@ -411,18 +411,41 @@ class TestResult:
({'phases':False},['Delta_V'],True,True), ({'phases':False},['Delta_V'],True,True),
({},['u_p','u_n'],False,False)], ({},['u_p','u_n'],False,False)],
ids=list(range(8))) ids=list(range(8)))
def test_read(self,update,request,ref_path,view,labels,compress,strip): def test_read(self,update,request,ref_path,view,output,compress,strip):
result = Result(ref_path/'4grains2x4x3_compressionY.hdf5') result = Result(ref_path/'4grains2x4x3_compressionY.hdf5')
for key,value in view.items(): for key,value in view.items():
result.view(key,value) result.view(key,value)
N = request.node.name[8:].split('[')[1].split(']')[0] N = request.node.name[8:].split('[')[1].split(']')[0]
cur = result.read(labels,compress,strip) cur = result.read(output,compress,strip)
if update: if update:
with bz2.BZ2File(ref_path/f'read_{N}.pbz2','w') as f: with bz2.BZ2File(ref_path/f'read_{N}.pbz2','w') as f:
pickle.dump(cur,f) pickle.dump(cur,f)
with bz2.BZ2File(ref_path/f'read_{N}.pbz2') as f: with bz2.BZ2File(ref_path/f'read_{N}.pbz2') as f:
ref = pickle.load(f) assert dict_equal(cur,pickle.load(f))
assert dict_equal(cur,ref)
@pytest.mark.parametrize('view,output,compress,constituents,strip',
[({},['F','P','F','L_p','F_e','F_p'],True,True,None),
({'increments':3},'F',True,True,[0,1,2,3,4,5,6,7]),
({'increments':[1,8,3,4,5,6,7]},['F','P'],True,True,1),
({'phases':['A','B']},['F','P'],True,True,[1,2]),
({'phases':['A','C'],'homogenizations':False},['F','P','O'],True,True,[0,7]),
({'phases':False,'homogenizations':False},['F','P','O'],True,True,[1,2,3,4]),
({'phases':False},['Delta_V'],True,True,[1,2,4]),
({},['u_p','u_n'],False,False,None)],
ids=list(range(8)))
def test_place(self,update,request,ref_path,view,output,compress,strip,constituents):
result = Result(ref_path/'4grains2x4x3_compressionY.hdf5')
for key,value in view.items():
result.view(key,value)
N = request.node.name[8:].split('[')[1].split(']')[0]
cur = result.place(output,compress,strip,constituents)
if update:
with bz2.BZ2File(ref_path/f'place_{N}.pbz2','w') as f:
pickle.dump(cur,f)
with bz2.BZ2File(ref_path/f'place_{N}.pbz2') as f:
assert dict_equal(cur,pickle.load(f))