support for masked array
masked arrays are the outcome of Result.place
This commit is contained in:
parent
58ac99831c
commit
cf1ce6a1fe
|
@ -3,6 +3,7 @@ import multiprocessing as mp
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import numpy.ma as ma
|
||||||
import vtk
|
import vtk
|
||||||
from vtk.util.numpy_support import numpy_to_vtk as np_to_vtk
|
from vtk.util.numpy_support import numpy_to_vtk as np_to_vtk
|
||||||
from vtk.util.numpy_support import numpy_to_vtkIdTypeArray as np_to_vtkIdTypeArray
|
from vtk.util.numpy_support import numpy_to_vtkIdTypeArray as np_to_vtkIdTypeArray
|
||||||
|
@ -230,7 +231,7 @@ class VTK:
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
data : numpy.ndarray
|
data : numpy.ndarray or numpy.ma.MaskedArray
|
||||||
Data to add. First dimension needs to match either
|
Data to add. First dimension needs to match either
|
||||||
number of cells or number of points.
|
number of cells or number of points.
|
||||||
label : str
|
label : str
|
||||||
|
@ -245,8 +246,10 @@ class VTK:
|
||||||
raise ValueError('No label defined for numpy.ndarray')
|
raise ValueError('No label defined for numpy.ndarray')
|
||||||
|
|
||||||
N_data = data.shape[0]
|
N_data = data.shape[0]
|
||||||
d = np_to_vtk((data.astype(np.single) if data.dtype in [np.double, np.longdouble] else
|
data_ = np.where(data.mask,data.fill_value,data) if isinstance(data,ma.MaskedArray) else\
|
||||||
data).reshape(N_data,-1),deep=True) # avoid large files
|
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
|
||||||
d.SetName(label)
|
d.SetName(label)
|
||||||
|
|
||||||
if N_data == N_points:
|
if N_data == N_points:
|
||||||
|
|
|
@ -4,6 +4,7 @@ import time
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import numpy.ma as ma
|
||||||
|
|
||||||
from damask import VTK
|
from damask import VTK
|
||||||
from damask import grid_filters
|
from damask import grid_filters
|
||||||
|
@ -134,6 +135,15 @@ class TestVTK:
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
default.add('invalid_type','valid')
|
default.add('invalid_type','valid')
|
||||||
|
|
||||||
|
def test_add_masked(self,default):
|
||||||
|
data = np.random.rand(5*6*7,3)
|
||||||
|
masked = ma.MaskedArray(data,mask=data<.4,fill_value=42.)
|
||||||
|
default.add(masked,'D')
|
||||||
|
result_masked = str(default)
|
||||||
|
default.add(np.where(masked.mask,masked.fill_value,masked),'D')
|
||||||
|
assert result_masked == str(default)
|
||||||
|
|
||||||
|
|
||||||
def test_comments(self,tmp_path,default):
|
def test_comments(self,tmp_path,default):
|
||||||
default.add_comments(['this is a comment'])
|
default.add_comments(['this is a comment'])
|
||||||
default.save(tmp_path/'with_comments',parallel=False)
|
default.save(tmp_path/'with_comments',parallel=False)
|
||||||
|
|
Loading…
Reference in New Issue