コード例 #1
0
 def test_xarray_dimension_order(self):
     xarray_grid = convert_ndarray_to_xarray(ARRAY_3D)
     displayed = display_image(xarray_grid, scaling=None)
     displayed_transposed = display_image(xarray_grid.transpose(),
                                          scaling=None)
     assert_obj_close(displayed, xarray_grid)
     assert_obj_close(displayed_transposed, xarray_grid)
コード例 #2
0
 def test_flat_colour_dimension_gives_greyscale(self):
     xr3 = convert_ndarray_to_xarray(ARRAY_4D[:, :, :, 0:1],
                                     extra_dims={ILLUM: [0]})
     displayed_xr = display_image(xr3)
     displayed_np = display_image(ARRAY_3D)
     displayed_np.attrs = displayed_xr.attrs
     assert_obj_close(displayed_xr.shape, displayed_np.shape)
コード例 #3
0
 def test_scaling_constricts_intensity_bounds(self):
     scale = (-5, 100)
     wide3 = ARRAY_3D.copy()
     wide3[0, 0, 0] = -5
     wide3[-1, -1, -1] = 100
     xr3 = convert_ndarray_to_xarray(ARRAY_3D) / 59
     assert_equal(display_image(wide3).attrs['_image_scaling'], scale)
     assert_obj_close(display_image(wide3, (0, 59)).values, xr3.values)
コード例 #4
0
 def test_specify_axes_for_numpy_arrays(self):
     transposed = np.transpose(ARRAY_3D, [1, 0, 2])
     displayed_transposed = display_image(transposed, scaling=None)
     xr_transposed = convert_ndarray_to_xarray(transposed)
     assert_obj_close(display_image(ARRAY_3D, depth_axis=1, scaling=None),
                      xr_transposed)
     assert_obj_close(
         display_image(ARRAY_3D, vert_axis=0, horiz_axis=2, scaling=None),
         xr_transposed)
コード例 #5
0
 def test_interpet_axes_for_numpy_arrays(self):
     xr2 = convert_ndarray_to_xarray(ARRAY_2D)
     xr3 = convert_ndarray_to_xarray(ARRAY_3D)
     displayed_2d = display_image(ARRAY_2D, scaling=None)
     displayed_3d = display_image(ARRAY_3D, scaling=None)
     displayed_transposed = display_image(np.transpose(ARRAY_3D, [1, 0, 2]),
                                          scaling=None)
     assert_obj_close(displayed_2d, xr2)
     assert_obj_close(displayed_3d, xr3)
     assert_obj_close(displayed_transposed, xr3)
コード例 #6
0
def save_image(filename, im, scaling='auto', depth=8):
    """Save an ndarray or image as a tiff.

    Parameters
    ----------
    im : ndarray or :class:`holopy.image.Image`
        image to save.
    filename : basestring
        filename in which to save image. If im is an image the
        function should default to the image's name field if no
        filename is specified
    scaling : 'auto', None, or (Int, Int)
        How the image should be scaled for saving. Ignored for float
        output. It defaults to auto, use the full range of the output
        format. Other options are None, meaning no scaling, or a pair
        of integers specifying the values which should be set to the
        maximum and minimum values of the image format.
    depth : 8, 16 or 'float'
        What type of image to save. Options other than 8bit may not be supported
        for many image types. You probably don't want to save 8bit images without
        some kind of scaling.

    """
    im = display_image(im, scaling)

    # if we don't have an extension, default to tif
    if os.path.splitext(filename)[1] is '':
        filename += '.tif'

    metadat = False
    if os.path.splitext(filename)[1] in tiflist:
        if im.name is None:
            im.name = os.path.splitext(os.path.split(filename)[-1])[0]
        metadat = pack_attrs(im, do_spacing=True)
        # import ifd2 - hidden here since it doesn't play nice in some cases.
        from PIL.TiffImagePlugin import ImageFileDirectory_v2 as ifd2
        tiffinfo = ifd2()
        # place metadata in the 'imagedescription' field of the tiff metadata
        tiffinfo[270] = yaml.dump(metadat, default_flow_style=True)

    im = im.values[0]

    if depth is not 'float':
        if depth is 8:
            depth = 8
            typestr = 'uint8'
        elif depth is 16 or depth is 32:
            depth = depth - 1
            typestr = 'int' + str(depth)
        else:
            raise Error("Unknown image depth")

        if im.max() <= 1:
            im = im * ((2**depth) - 1) + .499999
            im = im.astype(typestr)

    if metadat:
        pilimage.fromarray(im).save(filename, tiffinfo=tiffinfo)
    else:
        pilimage.fromarray(im).save(filename)
コード例 #7
0
 def test_colours_in_wrong_order(self):
     base = convert_ndarray_to_xarray(
         ARRAY_4D, extra_dims={ILLUM: ['red', 'green', 'blue']})
     xr4 = convert_ndarray_to_xarray(
         ARRAY_4D[:, :, :, [0, 2, 1]],
         extra_dims={ILLUM: ['red', 'blue', 'green']})
     assert_allclose(display_image(xr4, scaling=None).values, base.values)
コード例 #8
0
ファイル: io.py プロジェクト: galaturka/holopy
def save_images(filenames, ims, scaling='auto', depth=8):
    """
    Saves a volume as separate images (think of reconstruction volumes).

    Parameters
    ----------
    filenames : list
        List of filenames. There have to be the same number of filenames as of
        images to save. Each image will be saved in the corresponding file with
        the same index.
    ims : ndarray or :class:`holopy.image.Image`
        Images to save, with separate z-coordinates from which they each will
        be selected.
    scaling : 'auto', None, or (Int, Int)
        How the images should be scaled for saving. Ignored for float output.
        It defaults to auto, use the full range of the output format. Other
        options are None, meaning no scaling, or a pair of integers specifying
        the values which should be set to the maximum and minimum values of the
        image format.
    depth : 8, 16 or 'float'
        What type of image to save. Options other than 8bit may not be
        supported for many image types. You probably don't want to save 8bit
        images without some kind of scaling.
    """
    if len(ims) != len(filenames):
        raise ValueError("Not enough filenames or images provided.")

    for image_raw, filename in zip(ims, filenames):
        image_displayed = display_image(image_raw, scaling)
        _save_im(filename, image_displayed, depth)
コード例 #9
0
ファイル: io.py プロジェクト: galaturka/holopy
def save_image(filename, im, scaling='auto', depth=8):
    """Save an ndarray or image as a tiff.

    Parameters
    ----------
    filename : basestring
        filename in which to save image. If im is an image the
        function should default to the image's name field if no
        filename is specified
    im : ndarray or :class:`holopy.image.Image`
        image to save.
    scaling : 'auto', None, or (Int, Int)
        How the image should be scaled for saving. Ignored for float
        output. It defaults to auto, use the full range of the output
        format. Other options are None, meaning no scaling, or a pair
        of integers specifying the values which should be set to the
        maximum and minimum values of the image format.
    depth : 8, 16 or 'float'
        What type of image to save. Options other than 8bit may not be supported
        for many image types. You probably don't want to save 8bit images without
        some kind of scaling.

    """
    im = display_image(im, scaling)
    _save_im(filename, im, depth)
コード例 #10
0
 def test_complex_values_return_magnitude(self):
     xarray_real = convert_ndarray_to_xarray(ARRAY_3D)
     xarray_complex = xarray_real + 0j
     xarray_complex[0, 0, :] *= (1 + 1j) / np.sqrt(2)
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         displayed = display_image(xarray_complex, scaling=None)
         assert_obj_close(displayed, xarray_real)
コード例 #11
0
 def test_colour_name_formats(self):
     base = convert_ndarray_to_xarray(
         ARRAY_4D, extra_dims={ILLUM: ['red', 'green', 'blue']})
     cols = [['Red', 'Green', 'Blue'], ['r', 'g', 'b'], [0, 1, 2],
             ['a', 's', 'd']]
     for collist in cols:
         xr4 = convert_ndarray_to_xarray(ARRAY_4D,
                                         extra_dims={ILLUM: collist})
         assert_obj_close(display_image(xr4, scaling=None), base)
コード例 #12
0
 def test_maintains_metadata(self):
     base = convert_ndarray_to_xarray(ARRAY_3D)
     base.attrs = {
         'a': 2,
         'b': 3,
         'c': 4,
         'd': 5,
         '_image_scaling': (0, 59)
     }
     assert_equal(base.attrs, display_image(base).attrs)
コード例 #13
0
 def test_custom_dimension_names(self):
     xarray_real = convert_ndarray_to_xarray(ARRAY_3D)
     dims = xarray_real.assign_coords(dim1=xarray_real['x'],
                                      dim2=xarray_real['y'],
                                      dim3=xarray_real['z'])
     dims = dims.swap_dims({'x': 'dim1', 'y': 'dim2', 'z': 'dim3'})
     dims = display_image(dims,
                          vert_axis='dim1',
                          horiz_axis='dim2',
                          depth_axis='dim3',
                          scaling=None)
     is_ok = np.allclose(dims.values, xarray_real.values)
     self.assertTrue(is_ok)
コード例 #14
0
 def test_missing_colours(self):
     base = convert_ndarray_to_xarray(
         ARRAY_4D, extra_dims={ILLUM: ['red', 'green', 'blue']})
     slices = [[0, 2, 1], [1, 0], [0, 1], [0, 1]]
     possible_valid_colors = [['red', 'blue', 'green'], ['green', 'red'],
                              [0, 1], ['x-pol', 'y-pol']]
     dummy_channel = [None, 2, -1, -1]
     for i, c, d in zip(slices, possible_valid_colors, dummy_channel):
         xr4 = convert_ndarray_to_xarray(ARRAY_4D[:, :, :, i],
                                         extra_dims={ILLUM: c})
         xr4 = display_image(xr4, scaling=None)
         if d is not None:
             assert_equal(xr4.attrs['_dummy_channel'], d)
             del xr4.attrs['_dummy_channel']
         assert_obj_close(xr4, base)
コード例 #15
0
    def test_custom_extra_dimension_name(self):
        xarray_real = convert_ndarray_to_xarray(ARRAY_3D)
        extra_dims = OrderedDict([["t", [0, 1, 2]], [ILLUM, [0, 1, 2]]])
        xarray_5d = convert_ndarray_to_xarray(ARRAY_5D.transpose(
            [4, 1, 2, 0, 3]),
                                              extra_dims=extra_dims)
        displayed = display_image(xarray_5d, depth_axis='t', scaling=None)

        xarray_4d = convert_ndarray_to_xarray(ARRAY_4D,
                                              extra_dims={ILLUM: [0, 1, 2]})

        # There is no arithemtic operations on these, so the numbers
        # should be exactly the same and not just close:
        is_ok = np.all(displayed.values == xarray_4d.values)
        self.assertTrue(is_ok)
コード例 #16
0
 def test_scaling_exceeds_intensity_bounds(self):
     scale = (-5, 100)
     xr3 = (convert_ndarray_to_xarray(ARRAY_3D) + 5) / 105
     displayed = display_image(ARRAY_3D, scaling=scale)
     assert_allclose(displayed.values, xr3.values)
     assert_equal(displayed.attrs['_image_scaling'], scale)