Beispiel #1
0
 def get_range(self, attr='scalars', mode='point'):
     assert mode in ('point', 'cell')
     assert attr in ('scalars', 'vectors')
     dataset = self.dataset
     da = dataset.PointData if mode == 'point' else dataset.CellData
     x = self._get_attr(da, attr, mode)
     if x is None:
         return None, [0.0, 1.0]
     name, x = x
     if self._composite:
         # Don't bother with Nans for composite data for now.
         if isinstance(x, dsa.VTKNoneArray):
             res = [0.0, 1.0]
         elif attr == 'scalars':
             res = [algs.min(x), algs.max(x)]
         else:
             max_norm = np.sqrt(algs.max(algs.sum(x * x, axis=1)))
             res = [0.0, max_norm]
     else:
         has_nan = np.isnan(x).any()
         if attr == 'scalars':
             if has_nan:
                 res = [float(np.nanmin(x)), float(np.nanmax(x))]
             else:
                 res = list(x.GetRange())
         else:
             if has_nan:
                 d_mag = np.sqrt((x * x).sum(axis=1))
                 res = [float(np.nanmin(d_mag)), float(np.nanmax(d_mag))]
             else:
                 res = [0.0, x.GetMaxNorm()]
     return name, res
def testArrays(rtData, rtData2, grad, grad2, total_npts):
    " Test various parallel algorithms."
    if rank == 0:
        print('-----------------------')
    PRINT("SUM ones:", algs.sum(rtData / rtData) - total_npts)

    PRINT(
        "SUM sin:",
        (algs.sum(algs.sin(rtData) + 1) - numpy.sum(numpy.sin(rtData2) + 1)) /
        numpy.sum(numpy.sin(rtData2) + 1))

    PRINT("rtData min:", algs.min(rtData) - numpy.min(rtData2))
    PRINT("rtData max:", algs.max(rtData) - numpy.max(rtData2))
    PRINT("rtData sum:",
          (algs.sum(rtData) - numpy.sum(rtData2)) / (2 * numpy.sum(rtData2)))
    PRINT("rtData mean:", (algs.mean(rtData) - numpy.mean(rtData2)) /
          (2 * numpy.mean(rtData2)))
    PRINT("rtData var:",
          (algs.var(rtData) - numpy.var(rtData2)) / numpy.var(rtData2))
    PRINT("rtData std:",
          (algs.std(rtData) - numpy.std(rtData2)) / numpy.std(rtData2))

    PRINT("grad min:", algs.min(grad) - numpy.min(grad2))
    PRINT("grad max:", algs.max(grad) - numpy.max(grad2))
    PRINT("grad min 0:", algs.min(grad, 0) - numpy.min(grad2, 0))
    PRINT("grad max 0:", algs.max(grad, 0) - numpy.max(grad2, 0))
    PRINT("grad min 1:",
          algs.sum(algs.min(grad, 1)) - numpy.sum(numpy.min(grad2, 1)))
    PRINT("grad max 1:",
          algs.sum(algs.max(grad, 1)) - numpy.sum(numpy.max(grad2, 1)))
    PRINT("grad sum 1:",
          algs.sum(algs.sum(grad, 1)) - numpy.sum(numpy.sum(grad2, 1)))
    PRINT("grad var:", (algs.var(grad) - numpy.var(grad2)) / numpy.var(grad2))
    PRINT("grad var 0:",
          (algs.var(grad, 0) - numpy.var(grad2, 0)) / numpy.var(grad2, 0))
Beispiel #3
0
def testArrays(rtData, rtData2, grad, grad2, total_npts):
    " Test various parallel algorithms."
    if rank == 0:
        print "-----------------------"
    PRINT("SUM ones:", algs.sum(rtData / rtData) - total_npts)

    PRINT(
        "SUM sin:",
        (algs.sum(algs.sin(rtData) + 1) - numpy.sum(numpy.sin(rtData2) + 1)) / numpy.sum(numpy.sin(rtData2) + 1),
    )

    PRINT("rtData min:", algs.min(rtData) - numpy.min(rtData2))
    PRINT("rtData max:", algs.max(rtData) - numpy.max(rtData2))
    PRINT("rtData sum:", (algs.sum(rtData) - numpy.sum(rtData2)) / (2 * numpy.sum(rtData2)))
    PRINT("rtData mean:", (algs.mean(rtData) - numpy.mean(rtData2)) / (2 * numpy.mean(rtData2)))
    PRINT("rtData var:", (algs.var(rtData) - numpy.var(rtData2)) / numpy.var(rtData2))
    PRINT("rtData std:", (algs.std(rtData) - numpy.std(rtData2)) / numpy.std(rtData2))

    PRINT("grad min:", algs.min(grad) - numpy.min(grad2))
    PRINT("grad max:", algs.max(grad) - numpy.max(grad2))
    PRINT("grad min 0:", algs.min(grad, 0) - numpy.min(grad2, 0))
    PRINT("grad max 0:", algs.max(grad, 0) - numpy.max(grad2, 0))
    PRINT("grad min 1:", algs.sum(algs.min(grad, 1)) - numpy.sum(numpy.min(grad2, 1)))
    PRINT("grad max 1:", algs.sum(algs.max(grad, 1)) - numpy.sum(numpy.max(grad2, 1)))
    PRINT("grad sum 1:", algs.sum(algs.sum(grad, 1)) - numpy.sum(numpy.sum(grad2, 1)))
    PRINT("grad var:", (algs.var(grad) - numpy.var(grad2)) / numpy.var(grad2))
    PRINT("grad var 0:", (algs.var(grad, 0) - numpy.var(grad2, 0)) / numpy.var(grad2, 0))
Beispiel #4
0
 def get_range(self, attr='scalars', mode='point'):
     assert mode in ('point', 'cell')
     assert attr in ('scalars', 'vectors')
     dataset = self.dataset
     da = dataset.PointData if mode == 'point' else dataset.CellData
     x = self._get_attr(da, attr, mode)
     if x is None:
         return None, [0.0, 1.0]
     name, x = x
     if self._composite:
         # Don't bother with Nans for composite data for now.
         if isinstance(x, dsa.VTKNoneArray):
             res = [0.0, 1.0]
         elif attr == 'scalars':
             res = [algs.min(x), algs.max(x)]
         else:
             max_norm = np.sqrt(algs.max(algs.sum(x*x, axis=1)))
             res = [0.0, max_norm]
     else:
         has_nan = np.isnan(x).any()
         if attr == 'scalars':
             if has_nan:
                 res = [float(np.nanmin(x)), float(np.nanmax(x))]
             else:
                 res = list(x.GetRange())
         else:
             if has_nan:
                 d_mag = np.sqrt((x*x).sum(axis=1))
                 res = [float(np.nanmin(d_mag)),
                        float(np.nanmax(d_mag))]
             else:
                 res = [0.0, x.GetMaxNorm()]
     return name, res
Beispiel #5
0
 def get_bounds(self):
     """Return the bounds of the data.
     """
     if self._composite:
         c1 = algs.min(self.dataset.Points, axis=0)
         c2 = algs.max(self.dataset.Points, axis=0)
         result = np.zeros(6)
         result[::2] = c1
         result[1::2] = c2
         return result
     else:
         return self.dataset.GetBounds()
Beispiel #6
0
 def get_bounds(self):
     """Return the bounds of the data.
     """
     if self._composite:
         c1 = algs.min(self.dataset.Points, axis=0)
         c2 = algs.max(self.dataset.Points, axis=0)
         result = np.zeros(6)
         result[::2] = c1
         result[1::2] = c2
         return result
     else:
         return self.dataset.GetBounds()
Beispiel #7
0
def test_dataset(ds):
  p2c = vtk.vtkPointDataToCellData()
  p2c.SetInputData(ds)
  p2c.Update()

  d1 = dsa.WrapDataObject(p2c.GetOutput())

  vtkm_p2c = vtk.vtkmAverageToCells()
  vtkm_p2c.SetInputData(ds)
  vtkm_p2c.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, "RTData")
  vtkm_p2c.Update()

  d2 = dsa.WrapDataObject(vtkm_p2c.GetOutput())

  rtD1 = d1.PointData['RTData']
  rtD2 = d2.PointData['RTData']

  assert (algs.max(algs.abs(rtD1 - rtD2)) < 10E-4)
def test_dataset(ds):
  p2c = vtk.vtkPointDataToCellData()
  p2c.SetInputData(ds)
  p2c.Update()

  c2p = vtk.vtkCellDataToPointData()
  c2p.SetInputConnection(p2c.GetOutputPort())
  c2p.Update()

  d1 = dsa.WrapDataObject(c2p.GetOutput())

  c2p = vtk.vtkmAverageToPoints()
  c2p.SetInputData(p2c.GetOutput())
  c2p.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS, "RTData")
  c2p.Update()

  d2 = dsa.WrapDataObject(c2p.GetOutput())

  rtD1 = d1.PointData['RTData']
  rtD2 = d2.PointData['RTData']

  assert (algs.max(algs.abs(rtD1 - rtD2)) < 10E-4)
Beispiel #9
0
def test_dataset(ds):
  p2c = vtk.vtkPointDataToCellData()
  p2c.SetInputData(ds)
  p2c.Update()

  c2p = vtk.vtkCellDataToPointData()
  c2p.SetInputConnection(p2c.GetOutputPort())
  c2p.Update()

  d1 = dsa.WrapDataObject(c2p.GetOutput())

  c2p = vtk.vtkmAverageToPoints()
  c2p.SetInputData(p2c.GetOutput())
  c2p.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS, "RTData")
  c2p.Update()

  d2 = dsa.WrapDataObject(c2p.GetOutput())

  rtD1 = d1.PointData['RTData']
  rtD2 = d2.PointData['RTData']

  assert (algs.max(algs.abs(rtD1 - rtD2)) < 10E-4)
Beispiel #10
0
dbgRt.SetValue(3, 19.47)
dbgRt.SetValue(4, 3.350)
dbgRt.SetValue(5, 0.212)
dbgRt.SetValue(6, 1023.)
dbg.GetPointData().AddArray(dbgRt)

test_dataset(dbg)
print("Success!")

print("Testing homogeneous image data...")
source = vtk.vtkRTAnalyticSource()
source.Update()
imgData = source.GetOutput()
test_dataset(imgData)
print("Success!")

d = dsa.WrapDataObject(imgData)
rtData = d.PointData['RTData']
rtMin = algs.min(rtData)
rtMax = algs.max(rtData)
clipScalar = 0.5 * (rtMin + rtMax)

print("Testing non-homogenous unstructured grid...")
clip = vtk.vtkClipDataSet()
clip.SetInputData(imgData)
clip.SetValue(clipScalar)
clip.Update()
ugrid = clip.GetOutput()
test_dataset(ugrid)
print("Success!")
g2 = algs.gradient(g)

res = True
dummy = vtk.vtkDummyController()
for axis in [None, 0]:
    for array in [rtdata, g, g2]:
        if rank == 0:
            array2 = array/2
            min = algs.min_per_block(array2, axis=axis)
            res &= numpy.all(min.Arrays[NUM_BLOCKS - 1] == numpy.min(array, axis=axis))
            all_min = algs.min(min, controller=dummy)
            all_min_true = numpy.min([algs.min(array, controller=dummy), algs.min(array2, controller=dummy)])
            res &= all_min == all_min_true
            max = algs.max_per_block(array2, axis=axis)
            res &= numpy.all(max.Arrays[NUM_BLOCKS - 1] == numpy.max(array, axis=axis))
            all_max = algs.max(max, controller=dummy)
            all_max_true = numpy.max([algs.max(array, controller=dummy), algs.max(array2, controller=dummy)])
            res &= all_max == all_max_true
            sum = algs.sum_per_block(array2, axis=axis)
            sum_true = numpy.sum(array2.Arrays[0]) * (NUM_BLOCKS-1)
            sum_true += numpy.sum(array.Arrays[0]) * 3
            res &= numpy.sum(algs.sum(sum, controller=dummy) - algs.sum(sum_true, controller=dummy)) == 0
            mean = algs.mean_per_block(array2, axis=axis)
            res &= numpy.sum(mean.Arrays[0] - numpy.mean(array2.Arrays[0], axis=axis)) < 1E-6
            if len(array.Arrays[0].shape) == 1:
                stk = numpy.hstack
            else:
                stk = numpy.vstack
            res &= numpy.sum(mean.Arrays[NUM_BLOCKS-2] - numpy.mean(stk((array.Arrays[0], array2.Arrays[0])), axis=axis)) < 1E-4
        elif rank == 2:
            min = algs.min_per_block(dsa.NoneArray, axis=axis)
Beispiel #12
0
g2 = algs.gradient(g)

res = True
dummy = vtk.vtkDummyController()
for axis in [None, 0]:
    for array in [rtdata, g, g2]:
        if rank == 0:
            array2 = array/2
            min = algs.min_per_block(array2, axis=axis)
            res &= numpy.all(min.Arrays[NUM_BLOCKS - 1] == numpy.min(array, axis=axis))
            all_min = algs.min(min, controller=dummy)
            all_min_true = numpy.min([algs.min(array, controller=dummy), algs.min(array2, controller=dummy)])
            res &= all_min == all_min_true
            max = algs.max_per_block(array2, axis=axis)
            res &= numpy.all(max.Arrays[NUM_BLOCKS - 1] == numpy.max(array, axis=axis))
            all_max = algs.max(max, controller=dummy)
            all_max_true = numpy.max([algs.max(array, controller=dummy), algs.max(array2, controller=dummy)])
            res &= all_max == all_max_true
            sum = algs.sum_per_block(array2, axis=axis)
            sum_true = numpy.sum(array2.Arrays[0]) * (NUM_BLOCKS-1)
            sum_true += numpy.sum(array.Arrays[0]) * 3
            res &= numpy.sum(algs.sum(sum, controller=dummy) - algs.sum(sum_true, controller=dummy)) == 0
            mean = algs.mean_per_block(array2, axis=axis)
            res &= numpy.sum(mean.Arrays[0] - numpy.mean(array2.Arrays[0], axis=axis)) < 1E-6
            if len(array.Arrays[0].shape) == 1:
                stk = numpy.hstack
            else:
                stk = numpy.vstack
            res &= numpy.sum(mean.Arrays[NUM_BLOCKS-2] - numpy.mean(stk((array.Arrays[0], array2.Arrays[0])), axis=axis)) < 1E-4
        elif rank == 2:
            min = algs.min_per_block(dsa.NoneArray, axis=axis)
    print("This test requires numpy!")
    from vtk.test import Testing
    Testing.skip()

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.numpy_interface import algorithms as algs

rt = vtk.vtkRTAnalyticSource()

p2c = vtk.vtkPointDataToCellData()
p2c.SetInputConnection(rt.GetOutputPort())
p2c.Update()

c2p = vtk.vtkCellDataToPointData()
c2p.SetInputConnection(p2c.GetOutputPort())
c2p.Update()

d1 = dsa.WrapDataObject(c2p.GetOutput())

c2p = vtk.vtkmAverageToPoints()
c2p.SetInputData(p2c.GetOutput())
c2p.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,
                           "RTData")
c2p.Update()

d2 = dsa.WrapDataObject(c2p.GetOutput())

assert (algs.max(algs.abs(d1.PointData['RTData'] - d2.PointData['RTData'])) <
        10E-4)
Beispiel #14
0
mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, pd)
pd2 = vtk.vtkPolyData()
mb.SetBlock(1, pd2)
mbw = dsa.WrapDataObject(mb)

mbw.PointData.append(dsa.NoneArray, 'foo')
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 0
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 0

mbw.PointData.append(na2, 'foo')
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 1
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 0
assert mbw.GetBlock(0).GetPointData().GetArray(0).GetName() == 'foo'

mbw.PointData.append(algs.max(na2), "maxfoo")
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 2
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 1
assert mbw.GetBlock(0).GetPointData().GetArray(1).GetName() == 'maxfoo'

# --------------------------------------

mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, vtk.vtkImageData())
mb.SetBlock(1, vtk.vtkImageData())
assert dsa.WrapDataObject(mb).Points is na

mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, vtk.vtkStructuredGrid())
mb.SetBlock(1, vtk.vtkImageData())
assert dsa.WrapDataObject(mb).Points is na
Beispiel #15
0
import sys

try:
    import numpy
except ImportError:
    print("Numpy (http://numpy.scipy.org) not found.")
    print("This test requires numpy!")
    from vtk.test import Testing
    Testing.skip()

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.numpy_interface import algorithms as algs

rt = vtk.vtkRTAnalyticSource()

p2c = vtk.vtkPointDataToCellData()
p2c.SetInputConnection(rt.GetOutputPort())
p2c.Update()

d1 = dsa.WrapDataObject(p2c.GetOutput())

vtkm_p2c = vtk.vtkmAverageToCells()
vtkm_p2c.SetInputData(rt.GetOutput())
vtkm_p2c.SetInputArrayToProcess(0, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, "RTData")
vtkm_p2c.Update()

d2 = dsa.WrapDataObject(vtkm_p2c.GetOutput())

assert (algs.max(algs.abs(d1.CellData['RTData'] - d2.CellData['RTData'])) < 10E-4)
 for array in [rtdata, g, g2]:
     if rank == 0:
         array2 = array / 2
         min = algs.min_per_block(array2, axis=axis)
         res &= numpy.all(min.Arrays[NUM_BLOCKS -
                                     1] == numpy.min(array, axis=axis))
         all_min = algs.min(min, controller=dummy)
         all_min_true = numpy.min([
             algs.min(array, controller=dummy),
             algs.min(array2, controller=dummy)
         ])
         res &= all_min == all_min_true
         max = algs.max_per_block(array2, axis=axis)
         res &= numpy.all(max.Arrays[NUM_BLOCKS -
                                     1] == numpy.max(array, axis=axis))
         all_max = algs.max(max, controller=dummy)
         all_max_true = numpy.max([
             algs.max(array, controller=dummy),
             algs.max(array2, controller=dummy)
         ])
         res &= all_max == all_max_true
         sum = algs.sum_per_block(array2, axis=axis)
         sum_true = numpy.sum(array2.Arrays[0]) * (NUM_BLOCKS - 1)
         sum_true += numpy.sum(array.Arrays[0]) * 3
         res &= numpy.sum(
             algs.sum(sum, controller=dummy) -
             algs.sum(sum_true, controller=dummy)) == 0
         mean = algs.mean_per_block(array2, axis=axis)
         res &= numpy.sum(mean.Arrays[0] -
                          numpy.mean(array2.Arrays[0], axis=axis)) < 1E-6
         if len(array.Arrays[0].shape) == 1:
try:
    import numpy
except ImportError:
    print("Numpy (http://numpy.scipy.org) not found.")
    print("This test requires numpy!")
    sys.exit(0)

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.numpy_interface import algorithms as algs

rt = vtk.vtkRTAnalyticSource()

p2c = vtk.vtkPointDataToCellData()
p2c.SetInputConnection(rt.GetOutputPort())
p2c.Update()

d1 = dsa.WrapDataObject(p2c.GetOutput())

vtkm_p2c = vtk.vtkmAverageToCells()
vtkm_p2c.SetInputData(rt.GetOutput())
vtkm_p2c.SetInputArrayToProcess(0, 0, 0,
                                vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS,
                                "RTData")
vtkm_p2c.Update()

d2 = dsa.WrapDataObject(vtkm_p2c.GetOutput())

assert (algs.max(algs.abs(d1.CellData['RTData'] - d2.CellData['RTData'])) <
        10E-4)
Beispiel #18
0
mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, pd)
pd2 = vtk.vtkPolyData()
mb.SetBlock(1, pd2)
mbw = dsa.WrapDataObject(mb)

mbw.PointData.append(dsa.NoneArray, 'foo')
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 0
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 0

mbw.PointData.append(na2, 'foo')
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 1
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 0
assert mbw.GetBlock(0).GetPointData().GetArray(0).GetName() == 'foo'

mbw.PointData.append(algs.max(na2), "maxfoo")
assert mbw.GetBlock(0).GetPointData().GetNumberOfArrays() == 2
assert mbw.GetBlock(1).GetPointData().GetNumberOfArrays() == 1
assert mbw.GetBlock(0).GetPointData().GetArray(1).GetName() == 'maxfoo'

# --------------------------------------

mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, vtk.vtkImageData())
mb.SetBlock(1, vtk.vtkImageData())
assert dsa.WrapDataObject(mb).Points is na

mb = vtk.vtkMultiBlockDataSet()
mb.SetBlock(0, vtk.vtkStructuredGrid())
mb.SetBlock(1, vtk.vtkImageData())
assert dsa.WrapDataObject(mb).Points is na
Beispiel #19
0
import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.numpy_interface import algorithms as algs
reader = vtk.vtkMPASReader()
reader.SetFileName('MPASReader.nc')
# Have reader examine what information is available
reader.UpdateInformation()
# Print out available arrays
for i in range(reader.GetNumberOfCellArrays()):
    print reader.GetCellArrayName(i)
# Don't read in the ke cell data array
reader.SetCellArrayStatus('ke', 0)
reader.Update()

# Wrap the reader output to make simplify access
wrappedreader = dsa.WrapDataObject(reader.GetOutput())
print wrappedreader.PointData.keys()
print wrappedreader.CellData['vorticity']
vorticity = wrappedreader.CellData['vorticity']
# Perform some operations on the data
wrappedreader.CellData.append(vorticity + 1, 'vorticity plus one')
print algs.max(vorticity)
print algs.max(wrappedreader.CellData['vorticity plus one'])