dictionary of visible items should not be exposed to users
getters for individual items are easier to understand Note: increments_in_range and times_in_range operate now only on the visible increments
This commit is contained in:
parent
8def54c862
commit
7544e81349
|
@ -138,11 +138,11 @@ class Result:
|
||||||
fields += f['/'.join([self._increments[0],'homogenization',m])].keys()
|
fields += f['/'.join([self._increments[0],'homogenization',m])].keys()
|
||||||
self._fields = sorted(set(fields),key=util.natural_sort) # make unique
|
self._fields = sorted(set(fields),key=util.natural_sort) # make unique
|
||||||
|
|
||||||
self.visible = {'increments': self._increments,
|
self._visible = {'increments': self._increments,
|
||||||
'phases': self._phases,
|
'phases': self._phases,
|
||||||
'homogenizations': self._homogenizations,
|
'homogenizations': self._homogenizations,
|
||||||
'fields': self._fields,
|
'fields': self._fields,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.fname = Path(fname).expanduser().absolute()
|
self.fname = Path(fname).expanduser().absolute()
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ class Result:
|
||||||
header = [f'Created by {f.attrs["creator"]}',
|
header = [f'Created by {f.attrs["creator"]}',
|
||||||
f' on {f.attrs["created"]}',
|
f' on {f.attrs["created"]}',
|
||||||
f' executing "{f.attrs["call"]}"']
|
f' executing "{f.attrs["call"]}"']
|
||||||
visible_increments = self.visible['increments']
|
visible_increments = self._visible['increments']
|
||||||
|
|
||||||
first = self.view(increments=visible_increments[0:1]).list_data()
|
first = self.view(increments=visible_increments[0:1]).list_data()
|
||||||
|
|
||||||
|
@ -242,14 +242,14 @@ class Result:
|
||||||
choice.append(self._increments[idx-1])
|
choice.append(self._increments[idx-1])
|
||||||
|
|
||||||
valid = _match(choice,getattr(self,'_'+what))
|
valid = _match(choice,getattr(self,'_'+what))
|
||||||
existing = set(self.visible[what])
|
existing = set(self._visible[what])
|
||||||
|
|
||||||
if action == 'set':
|
if action == 'set':
|
||||||
dup.visible[what] = sorted(set(valid), key=util.natural_sort)
|
dup._visible[what] = sorted(set(valid), key=util.natural_sort)
|
||||||
elif action == 'add':
|
elif action == 'add':
|
||||||
dup.visible[what] = sorted(existing.union(valid), key=util.natural_sort)
|
dup._visible[what] = sorted(existing.union(valid), key=util.natural_sort)
|
||||||
elif action == 'del':
|
elif action == 'del':
|
||||||
dup.visible[what] = sorted(existing.difference(valid), key=util.natural_sort)
|
dup._visible[what] = sorted(existing.difference(valid), key=util.natural_sort)
|
||||||
|
|
||||||
return dup
|
return dup
|
||||||
|
|
||||||
|
@ -474,10 +474,10 @@ class Result:
|
||||||
raise PermissionError('rename datasets')
|
raise PermissionError('rename datasets')
|
||||||
|
|
||||||
with h5py.File(self.fname,'a') as f:
|
with h5py.File(self.fname,'a') as f:
|
||||||
for inc in self.visible['increments']:
|
for inc in self._visible['increments']:
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
path_src = '/'.join([inc,ty,label,field,name_src])
|
path_src = '/'.join([inc,ty,label,field,name_src])
|
||||||
path_dst = '/'.join([inc,ty,label,field,name_dst])
|
path_dst = '/'.join([inc,ty,label,field,name_dst])
|
||||||
if path_src in f.keys():
|
if path_src in f.keys():
|
||||||
|
@ -513,10 +513,10 @@ class Result:
|
||||||
raise PermissionError('delete datasets')
|
raise PermissionError('delete datasets')
|
||||||
|
|
||||||
with h5py.File(self.fname,'a') as f:
|
with h5py.File(self.fname,'a') as f:
|
||||||
for inc in self.visible['increments']:
|
for inc in self._visible['increments']:
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
path = '/'.join([inc,ty,label,field,name])
|
path = '/'.join([inc,ty,label,field,name])
|
||||||
if path in f.keys(): del f[path]
|
if path in f.keys(): del f[path]
|
||||||
|
|
||||||
|
@ -533,13 +533,13 @@ class Result:
|
||||||
"""
|
"""
|
||||||
msg = []
|
msg = []
|
||||||
with h5py.File(self.fname,'r') as f:
|
with h5py.File(self.fname,'r') as f:
|
||||||
for inc in self.visible['increments']:
|
for inc in self._visible['increments']:
|
||||||
msg += [f'\n{inc} ({self._times[int(inc.split("_")[1])]} s)']
|
msg += [f'\n{inc} ({self._times[int(inc.split("_")[1])]} s)']
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
msg += [f' {ty}']
|
msg += [f' {ty}']
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
msg += [f' {label}']
|
msg += [f' {label}']
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
msg += [f' {field}']
|
msg += [f' {field}']
|
||||||
for d in f['/'.join([inc,ty,label,field])].keys():
|
for d in f['/'.join([inc,ty,label,field])].keys():
|
||||||
dataset = f['/'.join([inc,ty,label,field,d])]
|
dataset = f['/'.join([inc,ty,label,field,d])]
|
||||||
|
@ -572,7 +572,7 @@ class Result:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def increments(self):
|
def increments(self):
|
||||||
return [int(i.split(prefix_inc)[-1]) for i in self.visible['increments']]
|
return [int(i.split(prefix_inc)[-1]) for i in self._visible['increments']]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def times(self):
|
def times(self):
|
||||||
|
@ -580,15 +580,15 @@ class Result:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def phases(self):
|
def phases(self):
|
||||||
return self.visible['phases']
|
return self._visible['phases']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def homogenizations(self):
|
def homogenizations(self):
|
||||||
return self.visible['homogenizations']
|
return self._visible['homogenizations']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fields(self):
|
def fields(self):
|
||||||
return self.visible['fields']
|
return self._visible['fields']
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1449,7 +1449,7 @@ class Result:
|
||||||
'meta':d.data.dtype.metadata}}
|
'meta':d.data.dtype.metadata}}
|
||||||
r = func(**dataset,**args)
|
r = func(**dataset,**args)
|
||||||
result = r['data'].reshape((-1,)+r['data'].shape[3:])
|
result = r['data'].reshape((-1,)+r['data'].shape[3:])
|
||||||
for x in self.visible[ty[0]+'s']:
|
for x in self._visible[ty[0]+'s']:
|
||||||
if ty[0] == 'phase':
|
if ty[0] == 'phase':
|
||||||
result1 = result[at_cell_ph[0][x]]
|
result1 = result[at_cell_ph[0][x]]
|
||||||
if ty[0] == 'homogenization':
|
if ty[0] == 'homogenization':
|
||||||
|
@ -1512,10 +1512,10 @@ class Result:
|
||||||
|
|
||||||
groups = []
|
groups = []
|
||||||
with h5py.File(self.fname,'r') as f:
|
with h5py.File(self.fname,'r') as f:
|
||||||
for inc in self.visible['increments']:
|
for inc in self._visible['increments']:
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
group = '/'.join([inc,ty,label,field])
|
group = '/'.join([inc,ty,label,field])
|
||||||
if set(datasets.values()).issubset(f[group].keys()): groups.append(group)
|
if set(datasets.values()).issubset(f[group].keys()): groups.append(group)
|
||||||
|
|
||||||
|
@ -1568,14 +1568,14 @@ class Result:
|
||||||
in_data_ph = []
|
in_data_ph = []
|
||||||
for c in range(self.N_constituents):
|
for c in range(self.N_constituents):
|
||||||
at_cell_ph.append({label: np.where(self.phase[:,c] == label)[0] \
|
at_cell_ph.append({label: np.where(self.phase[:,c] == label)[0] \
|
||||||
for label in self.visible['phases']})
|
for label in self._visible['phases']})
|
||||||
in_data_ph.append({label: f['/'.join(['cell_to','phase'])]['entry'][at_cell_ph[c][label]][:,c] \
|
in_data_ph.append({label: f['/'.join(['cell_to','phase'])]['entry'][at_cell_ph[c][label]][:,c] \
|
||||||
for label in self.visible['phases']})
|
for label in self._visible['phases']})
|
||||||
|
|
||||||
at_cell_ho = {label: np.where(self.homogenization[:] == label)[0] \
|
at_cell_ho = {label: np.where(self.homogenization[:] == label)[0] \
|
||||||
for label in self.visible['homogenizations']}
|
for label in self._visible['homogenizations']}
|
||||||
in_data_ho = {label: f['/'.join(['cell_to','homogenization'])]['entry'][at_cell_ho[label]] \
|
in_data_ho = {label: f['/'.join(['cell_to','homogenization'])]['entry'][at_cell_ho[label]] \
|
||||||
for label in self.visible['homogenizations']}
|
for label in self._visible['homogenizations']}
|
||||||
|
|
||||||
return at_cell_ph,in_data_ph,at_cell_ho,in_data_ho
|
return at_cell_ph,in_data_ph,at_cell_ho,in_data_ho
|
||||||
|
|
||||||
|
@ -1608,16 +1608,16 @@ class Result:
|
||||||
r: Dict[str,Any] = {}
|
r: Dict[str,Any] = {}
|
||||||
|
|
||||||
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 out in _match(output,f['/'.join([inc,'geometry'])].keys()):
|
for out in _match(output,f['/'.join([inc,'geometry'])].keys()):
|
||||||
r[inc]['geometry'][out] = _read(f['/'.join([inc,'geometry',out])])
|
r[inc]['geometry'][out] = _read(f['/'.join([inc,'geometry',out])])
|
||||||
|
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
r[inc][ty][label] = {}
|
r[inc][ty][label] = {}
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
r[inc][ty][label][field] = {}
|
r[inc][ty][label][field] = {}
|
||||||
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
||||||
r[inc][ty][label][field][out] = _read(f['/'.join([inc,ty,label,field,out])])
|
r[inc][ty][label][field][out] = _read(f['/'.join([inc,ty,label,field,out])])
|
||||||
|
@ -1683,15 +1683,15 @@ class Result:
|
||||||
|
|
||||||
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 out in _match(output,f['/'.join([inc,'geometry'])].keys()):
|
for out in _match(output,f['/'.join([inc,'geometry'])].keys()):
|
||||||
r[inc]['geometry'][out] = ma.array(_read(f['/'.join([inc,'geometry',out])]),fill_value = fill_float)
|
r[inc]['geometry'][out] = ma.array(_read(f['/'.join([inc,'geometry',out])]),fill_value = fill_float)
|
||||||
|
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
if field not in r[inc][ty].keys():
|
if field not in r[inc][ty].keys():
|
||||||
r[inc][ty][field] = {}
|
r[inc][ty][field] = {}
|
||||||
|
|
||||||
|
@ -1784,7 +1784,7 @@ class Result:
|
||||||
hdf5_link = (hdf5_dir if absolute_path else Path(os.path.relpath(hdf5_dir,out_dir.resolve())))/hdf5_name
|
hdf5_link = (hdf5_dir if absolute_path else Path(os.path.relpath(hdf5_dir,out_dir.resolve())))/hdf5_name
|
||||||
|
|
||||||
with h5py.File(self.fname,'r') as f:
|
with h5py.File(self.fname,'r') as f:
|
||||||
for inc in self.visible['increments']:
|
for inc in self._visible['increments']:
|
||||||
|
|
||||||
grid = ET.SubElement(collection,'Grid')
|
grid = ET.SubElement(collection,'Grid')
|
||||||
grid.attrib = {'GridType': 'Uniform',
|
grid.attrib = {'GridType': 'Uniform',
|
||||||
|
@ -1819,8 +1819,8 @@ class Result:
|
||||||
'Dimensions': '{} {} {} 3'.format(*(self.cells[::-1]+1))}
|
'Dimensions': '{} {} {} 3'.format(*(self.cells[::-1]+1))}
|
||||||
data_items[-1].text = f'{hdf5_link}:/{inc}/geometry/u_n'
|
data_items[-1].text = f'{hdf5_link}:/{inc}/geometry/u_n'
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f['/'.join([inc,ty,label])].keys()):
|
||||||
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
||||||
name = '/'.join([inc,ty,label,field,out])
|
name = '/'.join([inc,ty,label,field,out])
|
||||||
shape = f[name].shape[1:]
|
shape = f[name].shape[1:]
|
||||||
|
@ -1915,15 +1915,15 @@ class Result:
|
||||||
created = f.attrs['created'] if h5py3 else f.attrs['created'].decode()
|
created = f.attrs['created'] if h5py3 else f.attrs['created'].decode()
|
||||||
v.comments += [f'{creator} ({created})']
|
v.comments += [f'{creator} ({created})']
|
||||||
|
|
||||||
for inc in util.show_progress(self.visible['increments']):
|
for inc in util.show_progress(self._visible['increments']):
|
||||||
|
|
||||||
u = _read(f['/'.join([inc,'geometry','u_n' if mode.lower() == 'cell' else 'u_p'])])
|
u = _read(f['/'.join([inc,'geometry','u_n' if mode.lower() == 'cell' else 'u_p'])])
|
||||||
v = v.set('u',u)
|
v = v.set('u',u)
|
||||||
|
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for field in self.visible['fields']:
|
for field in self._visible['fields']:
|
||||||
outs: Dict[str, np.ma.core.MaskedArray] = {}
|
outs: Dict[str, np.ma.core.MaskedArray] = {}
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
if field not in f['/'.join([inc,ty,label])].keys(): continue
|
if field not in f['/'.join([inc,ty,label])].keys(): continue
|
||||||
|
|
||||||
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
for out in _match(output,f['/'.join([inc,ty,label,field])].keys()):
|
||||||
|
@ -1995,14 +1995,14 @@ class Result:
|
||||||
out_dir.mkdir(parents=True,exist_ok=True)
|
out_dir.mkdir(parents=True,exist_ok=True)
|
||||||
|
|
||||||
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']):
|
||||||
for c in range(self.N_constituents):
|
for c in range(self.N_constituents):
|
||||||
crystal_structure = [999]
|
crystal_structure = [999]
|
||||||
phase_name = ['Unknown Phase Type']
|
phase_name = ['Unknown Phase Type']
|
||||||
cell_orientation = np.zeros((np.prod(self.cells),3),np.float32)
|
cell_orientation = np.zeros((np.prod(self.cells),3),np.float32)
|
||||||
phase_ID = np.zeros((np.prod(self.cells)),dtype=np.int32)
|
phase_ID = np.zeros((np.prod(self.cells)),dtype=np.int32)
|
||||||
count = 1
|
count = 1
|
||||||
for label in self.visible['phases']:
|
for label in self._visible['phases']:
|
||||||
try:
|
try:
|
||||||
data = _read(f['/'.join([inc,'phase',label,'mechanical',q])])
|
data = _read(f['/'.join([inc,'phase',label,'mechanical',q])])
|
||||||
lattice = data.dtype.metadata['lattice']
|
lattice = data.dtype.metadata['lattice']
|
||||||
|
@ -2137,7 +2137,7 @@ class Result:
|
||||||
f_out['cell_to'].create_dataset('homogenization',data=mapping_homog.flatten())
|
f_out['cell_to'].create_dataset('homogenization',data=mapping_homog.flatten())
|
||||||
|
|
||||||
|
|
||||||
for inc in util.show_progress(self.visible['increments']):
|
for inc in util.show_progress(self._visible['increments']):
|
||||||
f_in.copy(inc,f_out,shallow=True)
|
f_in.copy(inc,f_out,shallow=True)
|
||||||
if mapping is None:
|
if mapping is None:
|
||||||
for label in ['u_p','u_n']:
|
for label in ['u_p','u_n']:
|
||||||
|
@ -2155,8 +2155,8 @@ class Result:
|
||||||
f_in[inc]['phase'].copy(label,f_out[inc]['phase'],shallow=True)
|
f_in[inc]['phase'].copy(label,f_out[inc]['phase'],shallow=True)
|
||||||
|
|
||||||
for ty in ['phase','homogenization']:
|
for ty in ['phase','homogenization']:
|
||||||
for label in self.visible[ty+'s']:
|
for label in self._visible[ty+'s']:
|
||||||
for field in _match(self.visible['fields'],f_in['/'.join([inc,ty,label])].keys()):
|
for field in _match(self._visible['fields'],f_in['/'.join([inc,ty,label])].keys()):
|
||||||
p = '/'.join([inc,ty,label,field])
|
p = '/'.join([inc,ty,label,field])
|
||||||
for out in _match(output,f_in[p].keys()):
|
for out in _match(output,f_in[p].keys()):
|
||||||
cp(f_in[p],f_out[p],out,None if mapping is None else mappings[ty][label.encode()])
|
cp(f_in[p],f_out[p],out,None if mapping is None else mappings[ty][label.encode()])
|
||||||
|
|
|
@ -78,6 +78,7 @@ class TestResult:
|
||||||
|
|
||||||
|
|
||||||
def test_view_all(self,default):
|
def test_view_all(self,default):
|
||||||
|
default = Result(default.fname)
|
||||||
a = default.view(increments=True).get('F')
|
a = default.view(increments=True).get('F')
|
||||||
|
|
||||||
assert dict_equal(a,default.view(increments='*').get('F'))
|
assert dict_equal(a,default.view(increments='*').get('F'))
|
||||||
|
@ -95,7 +96,7 @@ class TestResult:
|
||||||
label = 'increments' if what == 'times' else what
|
label = 'increments' if what == 'times' else what
|
||||||
|
|
||||||
assert n0.get('F') is n1.get('F') is None and \
|
assert n0.get('F') is n1.get('F') is None and \
|
||||||
len(n0.visible[label]) == len(n1.visible[label]) == 0
|
len(n0._visible[label]) == len(n1._visible[label]) == 0
|
||||||
|
|
||||||
@pytest.mark.parametrize('what',['increments','times','phases','fields']) # ToDo: discuss homogenizations
|
@pytest.mark.parametrize('what',['increments','times','phases','fields']) # ToDo: discuss homogenizations
|
||||||
def test_view_more(self,default,what):
|
def test_view_more(self,default,what):
|
||||||
|
@ -116,7 +117,7 @@ class TestResult:
|
||||||
label = 'increments' if what == 'times' else what
|
label = 'increments' if what == 'times' else what
|
||||||
|
|
||||||
assert n0.get('F') is n1.get('F') is None and \
|
assert n0.get('F') is n1.get('F') is None and \
|
||||||
len(n0.visible[label]) == len(n1.visible[label]) == 0
|
len(n0._visible[label]) == len(n1._visible[label]) == 0
|
||||||
|
|
||||||
def test_view_invalid_incstimes(self,default):
|
def test_view_invalid_incstimes(self,default):
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
|
@ -127,7 +128,7 @@ class TestResult:
|
||||||
def test_view_approxtimes(self,default,inc,sign):
|
def test_view_approxtimes(self,default,inc,sign):
|
||||||
eps = sign*1e-3
|
eps = sign*1e-3
|
||||||
times = list(default._times.values())
|
times = list(default._times.values())
|
||||||
assert [default._increments[inc]] == default.view(times=times[inc]+eps).visible['increments']
|
assert [default._increments[inc]] == default.view(times=times[inc]+eps)._visible['increments']
|
||||||
|
|
||||||
def test_add_invalid(self,default):
|
def test_add_invalid(self,default):
|
||||||
default.add_absolute('xxxx')
|
default.add_absolute('xxxx')
|
||||||
|
|
Loading…
Reference in New Issue