def test_3d_array(self):
        '''Correctly convert 3D array'''
        array = np.arange(2 * 3 * 4).reshape(2, 3, 4)
        image = numpy_to_vtkImageData(array)

        npt.assert_array_almost_equal(image.GetDimensions(), array.shape)

        for index, x in np.ndenumerate(array):
            self.assertAlmostEqual(array[index],
                                   image.GetScalarComponentAsDouble(*index, 0))
    def test_numpy_consistent(self):
        '''Conversion is consistent with numpy'''
        array = np.arange(2 * 3 * 4).reshape(2, 3, 4)
        image = numpy_to_vtkImageData(array)
        array2 = vtkImageData_to_numpy(image)

        npt.assert_array_almost_equal(image.GetDimensions(), array.shape)
        npt.assert_array_almost_equal(array2.shape, array.shape)

        for index, x in np.ndenumerate(array):
            self.assertAlmostEqual(array[index],
                                   image.GetScalarComponentAsDouble(*index, 0))
            self.assertAlmostEqual(array[index], array2[index])
Esempio n. 3
0
def ExtractFields(input_model,
                  output_image,
                  field_name,
                  outside_value=0.0,
                  lower_threshold=0.0,
                  upper_threshold=0.0,
                  output_type='float'):

    # Python 2/3 compatible input
    from six.moves import input

    # Check requested output type
    output_type = output_type.lower()
    if output_type not in EXTRACT_FIELDS_SUPPORTED_TYPES.keys():
        print('Valid output types: {}'.format(', '.join(
            EXTRACT_FIELDS_SUPPORTED_TYPES.keys())))
        os.sys.exit(
            '[ERROR] Requested type {} not supported'.format(output_type))

    # Read input
    if not os.path.isfile(input_model):
        os.sys.exit('[ERROR] Cannot find file \"{}\"'.format(input_model))

    # Test field name
    if field_name not in valid_fields():
        print('Valid field names: {}'.format(valid_fields()))
        os.sys.exit('[ERROR] Please provide a valid field name')

    print('Reading elements into image array')
    image, spacing, origin, n_elements = field_to_image(
        input_model, field_name, outside_value)
    print('  Spacing:            {}'.format(spacing))
    print('  Origin:             {}'.format(origin))
    print('  Dimensions:         {}'.format(image.shape))
    print('  Number of elements: {}'.format(n_elements))
    print('')

    # Convert to VTK
    print('Converting to vtkImageData')
    vtkImage = numpy_to_vtkImageData(image, spacing, origin)
    print('')

    if output_type == 'float':
        print('Not rescaling data')
    else:
        print('Rescaling data into {} dynamic range'.format(output_type))
        # Hack to get data type range
        source = vtk.vtkImageEllipsoidSource()
        source.SetWholeExtent(0, 1, 0, 1, 0, 0)
        source.SetCenter(0, 0, 0)
        source.SetRadius(0, 0, 0)
        source.SetOutputScalarType(EXTRACT_FIELDS_SUPPORTED_TYPES[output_type])
        source.Update()

        # Compute min/max
        if lower_threshold == upper_threshold:
            scalar_range = vtkImage.GetScalarRange()
        else:
            scalar_range = [lower_threshold, upper_threshold]
        dtype_range = [0, source.GetOutput().GetScalarTypeMax()]
        print(' Image range:  {}'.format(vtkImage.GetScalarRange()))
        print(' Input range:  {}'.format(scalar_range))
        print(' Output range: {}'.format(dtype_range))

        # Note the equation for shift/scale:
        #   u = (v + ScalarShift)*ScalarScale
        m = float(dtype_range[1] - dtype_range[0]) / float(scalar_range[1] -
                                                           scalar_range[0])
        b = float(dtype_range[1] - m * scalar_range[1])
        b = b / m

        print(' Shift: {}'.format(b))
        print(' Scale: {}'.format(m))

        scaler = vtk.vtkImageShiftScale()
        scaler.SetInputData(vtkImage)
        scaler.SetShift(b)
        scaler.SetScale(m)
        scaler.ClampOverflowOn()
        scaler.SetOutputScalarType(EXTRACT_FIELDS_SUPPORTED_TYPES[output_type])
        scaler.Update()
        vtkImage = scaler.GetOutput()

        print(' Output image range:  {}'.format(vtkImage.GetScalarRange()))
    print('')

    # Write image
    print('Saving image ' + output_image)
    writer = get_vtk_writer(output_image)
    if writer is None:
        os.sys.exit(
            '[ERROR] Cannot find writer for file \"{}\"'.format(output_image))
    writer.SetInputData(vtkImage)
    writer.SetFileName(output_image)
    writer.Update()
 def test_3d_array_origin(self):
     '''Correctly set origin'''
     array = np.arange(2 * 3 * 4).reshape(2, 3, 4)
     image = numpy_to_vtkImageData(array, origin=[-12.0, 33.0, 104.0])
     npt.assert_array_almost_equal(image.GetOrigin(), [-12.0, 33.0, 104.0])
 def test_3d_array_spacing(self):
     '''Correctly set spacing'''
     array = np.arange(2 * 3 * 4).reshape(2, 3, 4)
     image = numpy_to_vtkImageData(array, spacing=[2.0, 3.0, 4.0])
     npt.assert_array_almost_equal(image.GetSpacing(), [2.0, 3.0, 4.0])