support for string arrays
allows to add phase label from mapping. Only scalar string arrays are allowed (no real restriction)
This commit is contained in:
parent
6e1fe712c7
commit
18aa6f7f12
|
@ -1352,12 +1352,12 @@ class Result:
|
|||
at_cell_ph = []
|
||||
in_data_ph = []
|
||||
for c in range(self.N_constituents):
|
||||
at_cell_ph.append({label: np.where(f['/'.join(['cell_to','phase'])][:,c]['label'] == label.encode())[0] \
|
||||
at_cell_ph.append({label: np.where(self.phase[:,c] == label)[0] \
|
||||
for label in self.visible['phases']})
|
||||
in_data_ph.append({label: f['/'.join(['cell_to','phase'])]['entry'][at_cell_ph[c][label]][:,c] \
|
||||
for label in self.visible['phases']})
|
||||
|
||||
at_cell_ho = {label: np.where(f['/'.join(['cell_to','homogenization'])][:]['label'] == label.encode())[0] \
|
||||
at_cell_ho = {label: np.where(self.homogenization[:] == label)[0] \
|
||||
for label in self.visible['homogenizations']}
|
||||
in_data_ho = {label: f['/'.join(['cell_to','homogenization'])]['entry'][at_cell_ho[label]] \
|
||||
for label in self.visible['homogenizations']}
|
||||
|
|
|
@ -265,10 +265,18 @@ class VTK:
|
|||
raise ValueError('No label defined for numpy.ndarray')
|
||||
|
||||
N_data = data.shape[0]
|
||||
data_ = np.where(data.mask,data.fill_value,data) if isinstance(data,np.ma.MaskedArray) else\
|
||||
data
|
||||
d = np_to_vtk((data_.astype(np.single) if data_.dtype in [np.double, np.longdouble] else
|
||||
data_).reshape(N_data,-1),deep=True) # avoid large files
|
||||
data_ = (data if not isinstance(data,np.ma.MaskedArray) else
|
||||
np.where(data.mask,data.fill_value,data)).reshape(N_data,-1)
|
||||
|
||||
if data_.dtype in [np.double,np.longdouble]:
|
||||
d = np_to_vtk(data_.astype(np.single),deep=True) # avoid large files
|
||||
elif data_.dtype.type is np.str_:
|
||||
d = vtk.vtkStringArray()
|
||||
for s in np.squeeze(data_):
|
||||
d.InsertNextValue(s)
|
||||
else:
|
||||
d = np_to_vtk(data_,deep=True)
|
||||
|
||||
d.SetName(label)
|
||||
|
||||
if N_data == N_points:
|
||||
|
@ -305,13 +313,23 @@ class VTK:
|
|||
cell_data = self.vtk_data.GetCellData()
|
||||
for a in range(cell_data.GetNumberOfArrays()):
|
||||
if cell_data.GetArrayName(a) == label:
|
||||
try:
|
||||
return vtk_to_np(cell_data.GetArray(a))
|
||||
except AttributeError:
|
||||
vtk_array = cell_data.GetAbstractArray(a) # string array
|
||||
|
||||
point_data = self.vtk_data.GetPointData()
|
||||
for a in range(point_data.GetNumberOfArrays()):
|
||||
if point_data.GetArrayName(a) == label:
|
||||
try:
|
||||
return vtk_to_np(point_data.GetArray(a))
|
||||
except AttributeError:
|
||||
vtk_array = point_data.GetAbstractArray(a) # string array
|
||||
|
||||
try:
|
||||
# string array
|
||||
return np.array([vtk_array.GetValue(i) for i in range(vtk_array.GetNumberOfValues())]).astype(str)
|
||||
except UnboundLocalError:
|
||||
raise ValueError(f'Array "{label}" not found.')
|
||||
|
||||
|
||||
|
|
|
@ -135,6 +135,18 @@ class TestVTK:
|
|||
with pytest.raises(TypeError):
|
||||
default.add('invalid_type','valid')
|
||||
|
||||
@pytest.mark.parametrize('data_type,shape',[(float,(3,)),
|
||||
(float,(3,3)),
|
||||
(float,(1,)),
|
||||
(int,(4,)),
|
||||
(str,(1,))])
|
||||
@pytest.mark.parametrize('N_values',[5*6*7,6*7*8])
|
||||
def test_add_get(self,default,data_type,shape,N_values):
|
||||
data = np.squeeze(np.random.randint(0,100,(N_values,)+shape)).astype(data_type)
|
||||
default.add(data,'data')
|
||||
assert (np.squeeze(data.reshape(N_values,-1)) == default.get('data')).all()
|
||||
|
||||
|
||||
def test_add_masked(self,default):
|
||||
data = np.random.rand(5*6*7,3)
|
||||
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
|
||||
|
|
Loading…
Reference in New Issue