def get_standard_tiff_data(new_tiff_filename, axis_order="tzyxc", pages_to_channel=1, memmap=False): """ Reads a tiff file and returns a standard 5D array and the metadata. Args: new_tiff_filename(str): the TIFF file to read in axis_order(int): how to order the axes (by default returns "tzyxc"). pages_to_channel(int): if channels are not normally stored in the channel variable, but are stored as pages (or as a mixture), then this will split neighboring pages into separate channels. (by default is 1 so changes nothing) memmap(bool): allows one to load the array using a memory mapped file as opposed to reading it directly. (by default is False) Returns: (ndarray/memmap, ndarray): an array with the axis order specified and description metadata. """ assert (pages_to_channel > 0) new_tiff_description = [] with tifffile.TiffFile(new_tiff_filename) as new_tiff_file: if memmap: try: # tifffile >= 0.13.0 new_tiff_array = new_tiff_file.asarray(out="memmap") except TypeError: # tifffile < 0.13.0 new_tiff_array = new_tiff_file.asarray(memmap=True) else: new_tiff_array = new_tiff_file.asarray() for i in iters.irange(0, len(new_tiff_file.pages), pages_to_channel): new_tiff_description.append([]) for j in iters.irange(pages_to_channel): each_page = new_tiff_file.pages[i + j] each_metadata = each_page.tags each_desc = u"" try: each_desc = unicode( each_metadata["image_description"].value) except KeyError: pass new_tiff_description[-1].append(each_desc) new_tiff_description = numpy.array(new_tiff_description) # Add a singleton channel if none is present. if new_tiff_array.ndim == 3: new_tiff_array = new_tiff_array[None] # Fit the old VIGRA style array. (may try to remove in the future) new_tiff_array = new_tiff_array.transpose( tuple(iters.irange(new_tiff_array.ndim - 1, 1, -1)) + (1, 0)) # Check to make sure the dimensions are ok if (new_tiff_array.ndim == 5): pass elif (new_tiff_array.ndim == 4): # Has no z. So, add this. new_tiff_array = xnumpy.add_singleton_axis_beginning(new_tiff_array) else: raise Exception( "Invalid dimensionality for TIFF. Found shape to be \"" + repr(new_tiff_array.shape) + "\".") # Some people use pages to hold time and channel data. So, we need to # restructure it. However, if they are properly structuring their TIFF # file, then they shouldn't incur a penalty. if pages_to_channel > 1: new_tiff_array = new_tiff_array.reshape(new_tiff_array.shape[:-2] + ( new_tiff_array.shape[-2] // pages_to_channel, pages_to_channel * new_tiff_array.shape[-1], )) new_tiff_array = xnumpy.tagging_reorder_array(new_tiff_array, from_axis_order="zyxtc", to_axis_order=axis_order, to_copy=True) # Currently is `tc` order so convert it to the expected order. if axis_order.index("t") > axis_order.index("c"): new_tiff_description = new_tiff_description.T.copy() return (new_tiff_array, new_tiff_description)
def get_standard_tiff_data(new_tiff_filename, axis_order="tzyxc", pages_to_channel=1, memmap=False): """ Reads a tiff file and returns a standard 5D array and the metadata. Args: new_tiff_filename(str): the TIFF file to read in axis_order(int): how to order the axes (by default returns "tzyxc"). pages_to_channel(int): if channels are not normally stored in the channel variable, but are stored as pages (or as a mixture), then this will split neighboring pages into separate channels. (by default is 1 so changes nothing) memmap(bool): allows one to load the array using a memory mapped file as opposed to reading it directly. (by default is False) Returns: (ndarray/memmap, ndarray): an array with the axis order specified and description metadata. """ assert (pages_to_channel > 0) new_tiff_description = [] with tifffile.TiffFile(new_tiff_filename) as new_tiff_file: new_tiff_array = new_tiff_file.asarray(memmap=memmap) for i in iters.irange( 0, len(new_tiff_file), pages_to_channel ): new_tiff_description.append([]) for j in iters.irange(pages_to_channel): each_page = new_tiff_file[i + j] each_metadata = each_page.tags each_desc = u"" try: each_desc = unicode( each_metadata["image_description"].value ) except KeyError: pass new_tiff_description[-1].append( each_desc ) new_tiff_description = numpy.array(new_tiff_description) # Add a singleton channel if none is present. if new_tiff_array.ndim == 3: new_tiff_array = new_tiff_array[None] # Fit the old VIGRA style array. (may try to remove in the future) new_tiff_array = new_tiff_array.transpose( tuple(iters.irange(new_tiff_array.ndim - 1, 1, -1)) + (1, 0) ) # Check to make sure the dimensions are ok if (new_tiff_array.ndim == 5): pass elif (new_tiff_array.ndim == 4): # Has no z. So, add this. new_tiff_array = xnumpy.add_singleton_axis_beginning(new_tiff_array) else: raise Exception( "Invalid dimensionality for TIFF. Found shape to be \"" + repr(new_tiff_array.shape) + "\"." ) # Some people use pages to hold time and channel data. So, we need to # restructure it. However, if they are properly structuring their TIFF # file, then they shouldn't incur a penalty. if pages_to_channel > 1: new_tiff_array = new_tiff_array.reshape( new_tiff_array.shape[:-2] + (new_tiff_array.shape[-2] / pages_to_channel, pages_to_channel * new_tiff_array.shape[-1],) ) new_tiff_array = xnumpy.tagging_reorder_array( new_tiff_array, from_axis_order="zyxtc", to_axis_order=axis_order, to_copy=True ) # Currently is `tc` order so convert it to the expected order. if axis_order.index("t") > axis_order.index("c"): new_tiff_description = new_tiff_description.T.copy() return(new_tiff_array, new_tiff_description)
def get_standard_tiff_array(new_tiff_filename, axis_order = "tzyxc", pages_to_channel = 1): """ Reads a tiff file and returns a standard 5D array. Args: new_tiff_filename(str): the TIFF file to read in axis_order(int): how to order the axes (by default returns "tzyxc"). pages_to_channel(int): if channels are not normally stored in the channel variable, but are stored as pages (or as a mixture), then this will split neighboring pages into separate channels. (by default is 1 so changes nothing) Returns: (numpy.ndarray): an array with the axis order specified. """ assert (pages_to_channel > 0) # Get the shape and dtype information shape, dtype = get_multipage_tiff_shape_dtype(new_tiff_filename).values() # Turn dtype into something that VIGRA's readImage or readVolume will take dtype_str = "" if dtype is None: dtype_str = "" elif (dtype is numpy.float64) or (dtype is float): dtype_str = "DOUBLE" elif (dtype is numpy.float32): dtype_str = "FLOAT" elif (dtype is numpy.uint32): dtype_str = "UINT32" elif (dtype is numpy.int32): dtype_str = "INT32" elif (dtype is numpy.uint16): dtype_str = "UINT16" elif (dtype is numpy.int16): dtype_str = "INT16" elif (dtype is numpy.uint8): dtype_str = "UINT8" else: raise Exception("Unacceptable dtype of " + repr(dtype)) if shape[-2] > 1: # Our algorithm expect double precision new_tiff_array = vigra.impex.readVolume(new_tiff_filename, dtype = dtype_str) # Convert to normal array new_tiff_array = new_tiff_array.view(numpy.ndarray) else: # Our algorithm expect double precision new_tiff_array = vigra.impex.readImage(new_tiff_filename, dtype = dtype_str) # Convert to normal array new_tiff_array = new_tiff_array.view(numpy.ndarray) # Need to add singleton time dimension before channel new_tiff_array = xnumpy.add_singleton_axis_pos(new_tiff_array, -2) # Check to make sure the dimensions are ok if (new_tiff_array.ndim == 5): pass elif (new_tiff_array.ndim == 4): # Has no z. So, add this. new_tiff_array = xnumpy.add_singleton_axis_beginning(new_tiff_array) else: raise Exception("Invalid dimensionality for TIFF. Found shape to be \"" + repr(new_tiff_array.shape) + "\".") # Some people use pages to hold time and channel data. So, we need to restructure it. # However, if they are properly structuring their TIFF file, then they shouldn't incur a penalty. if pages_to_channel > 1: new_tiff_array = new_tiff_array.reshape(new_tiff_array.shape[:-2] + (new_tiff_array.shape[-2] / pages_to_channel, pages_to_channel * new_tiff_array.shape[-1],)) new_tiff_array = xnumpy.tagging_reorder_array(new_tiff_array, from_axis_order = "zyxtc", to_axis_order = axis_order, to_copy = True) return(new_tiff_array)
def get_standard_tiff_array(new_tiff_filename, axis_order="tzyxc", pages_to_channel=1, memmap=False): """ Reads a tiff file and returns a standard 5D array. Args: new_tiff_filename(str): the TIFF file to read in axis_order(int): how to order the axes (by default returns "tzyxc"). pages_to_channel(int): if channels are not normally stored in the channel variable, but are stored as pages (or as a mixture), then this will split neighboring pages into separate channels. (by default is 1 so changes nothing) memmap(bool): allows one to load the array using a memory mapped file as opposed to reading it directly. (by default is False) Returns: (numpy.ndarray or numpy.memmap): an array with the axis order specified. """ assert (pages_to_channel > 0) with tifffile.TiffFile(new_tiff_filename) as new_tiff_file: new_tiff_array = new_tiff_file.asarray(memmap=memmap) # Add a singleton channel if none is present. if new_tiff_array.ndim == 3: new_tiff_array = new_tiff_array[None] # Fit the old VIGRA style array. (may try to remove in the future) new_tiff_array = new_tiff_array.transpose( tuple(iters.irange(new_tiff_array.ndim - 1, 1, -1)) + (1, 0) ) # Check to make sure the dimensions are ok if (new_tiff_array.ndim == 5): pass elif (new_tiff_array.ndim == 4): # Has no z. So, add this. new_tiff_array = xnumpy.add_singleton_axis_beginning(new_tiff_array) else: raise Exception( "Invalid dimensionality for TIFF. Found shape to be \"" + repr(new_tiff_array.shape) + "\"." ) # Some people use pages to hold time and channel data. So, we need to # restructure it. However, if they are properly structuring their TIFF # file, then they shouldn't incur a penalty. if pages_to_channel > 1: new_tiff_array = new_tiff_array.reshape( new_tiff_array.shape[:-2] + (new_tiff_array.shape[-2] / pages_to_channel, pages_to_channel * new_tiff_array.shape[-1],) ) new_tiff_array = xnumpy.tagging_reorder_array( new_tiff_array, from_axis_order="zyxtc", to_axis_order=axis_order, to_copy=True ) return(new_tiff_array)