support for masked array

masked arrays are the outcome of Result.place
This commit is contained in:
Martin Diehl 2021-04-02 08:07:22 +02:00
parent 58ac99831c
commit cf1ce6a1fe
2 changed files with 16 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import multiprocessing as mp
from pathlib import Path
import numpy as np
import numpy.ma as ma
import 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
@ -230,7 +231,7 @@ class VTK:
Parameters
----------
data : numpy.ndarray
data : numpy.ndarray or numpy.ma.MaskedArray
Data to add. First dimension needs to match either
number of cells or number of points.
label : str
@ -245,8 +246,10 @@ class VTK:
raise ValueError('No label defined for numpy.ndarray')
N_data = data.shape[0]
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_ = np.where(data.mask,data.fill_value,data) if isinstance(data,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
d.SetName(label)
if N_data == N_points:

View File

@ -4,6 +4,7 @@ import time
import pytest
import numpy as np
import numpy.ma as ma
from damask import VTK
from damask import grid_filters
@ -134,6 +135,15 @@ class TestVTK:
with pytest.raises(TypeError):
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):
default.add_comments(['this is a comment'])
default.save(tmp_path/'with_comments',parallel=False)