utils2p/main.py
def load_raw(path, metadata):
    """
    This function loads a raw image generated by ThorImage as a numpy array.

    Parameters
    ----------
    path : string
        Path to raw file.
    metadata : ElementTree root
        Can be obtained with :func:`get_metadata`.

    Returns
    -------
    stacks : tuple of numpy arrays
        Number of numpy arrays depends on the number of channels recoded during
        the experiment. Has the following dimensions:
        TZYX or TYX for planar images.

    Examples
    --------
    >>> import utils2p
    >>> metadata = utils2p.Metadata('data/mouse_kidney_raw/2p/Untitled_001/Experiment.xml')
    >>> stack1, stack2 = utils2p.load_raw('data/mouse_kidney_raw/2p/Untitled_001/Image_0001_0001.raw',metadata)
    >>> type(stack1), type(stack2)
    (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>)
    >>> utils2p.save_img('stack1.tif',stack1)
    >>> utils2p.save_img('stack2.tif',stack2)
    """
    path = os.path.expanduser(os.path.expandvars(path))
    n_time_points = metadata.get_n_time_points()
    width = metadata.get_num_x_pixels()
    height = metadata.get_num_y_pixels()
    n_channels = metadata.get_n_channels()
    byte_size = os.stat(path).st_size

    assert not byte_size % 1, "File does not have an integer byte length."
    byte_size = int(byte_size)

    n_z = (
        byte_size / 2 / width / height / n_time_points / n_channels
    )  # divide by two because the values are of type short (16bit = 2byte)

    assert (
        not n_z %
        1), "Size given in metadata does not match the size of the raw file."
    n_z = int(n_z)

    # number of z slices from meta data can be different
    # because of flyback frames
    meta_n_z = metadata.get_n_z()

    if n_z == 1:
        stacks = np.zeros((n_channels, n_time_points, height, width),
                          dtype="uint16")
        image_size = width * height
        # number of values stored for a given time point
        # (this includes images for all channels)
        t_size = (width * height * n_channels)
        with open(path, "rb") as f:
            for t in range(n_time_points):
                # print('{}/{}'.format(t,n_time_points))
                a = array.array("H")
                a.fromfile(f, t_size)
                for c in range(n_channels):
                    stacks[c, t, :, :] = np.array(a[c * image_size:(c + 1) *
                                                    image_size]).reshape(
                                                        (height, width))
    elif n_z > 1:
        stacks = np.zeros((n_channels, n_time_points, meta_n_z, height, width),
                          dtype="uint16")
        image_size = width * height
        t_size = (
            width * height * n_z * n_channels
        )  # number of values stored for a given time point (this includes images for all channels)
        with open(path, "rb") as f:
            for t in range(n_time_points):
                # print('{}/{}'.format(t,n_time_points))
                a = array.array("H")
                a.fromfile(f, t_size)
                a = np.array(a).reshape(
                    (-1, image_size
                     ))  # each row is an image alternating between channels
                for c in range(n_channels):
                    stacks[c, t, :, :, :] = a[c::n_channels, :].reshape(
                        (n_z, height, width))[:meta_n_z, :, :]

    area_mode = metadata.get_area_mode()
    if area_mode in ('line', 'kymograph') and meta_n_z > 1:
        concatenated = []
        for stack in stacks:
            concatenated.append(concatenate_z(stack))
        stacks = concatenated

    if len(stacks) == 1:
        return (np.squeeze(stacks[0]), )
    return tuple(np.squeeze(stacks))

    This function loads a raw image generated by ThorImage as a numpy array.

    Parameters
    ----------
    path : string
        Path to raw file.
    metadata : ElementTree root
        Can be obtained with :func:`get_metadata`.

    Returns
    -------
    stacks : tuple of numpy arrays
        Number of numpy arrays depends on the number of channels recoded during
        the experiment. Has the following dimensions:
        TZYX or TYX for planar images.

    Examples
    --------
    >>> import utils2p
    >>> metadata = utils2p.Metadata('data/mouse_kidney_raw/2p/Untitled_001/Experiment.xml')
    >>> stack1, stack2 = utils2p.load_raw('data/mouse_kidney_raw/2p/Untitled_001/Image_0001_0001.raw',metadata)
    >>> type(stack1), type(stack2)
    (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>)
    >>> utils2p.save_img('stack1.tif',stack1)
    >>> utils2p.save_img('stack2.tif',stack2)
    
This function loads a raw image generated by ThorImage as a numpy array.
Parameters

path : string
Path to raw file.
metadata : ElementTree root
Can be obtained with :func:`get_metadata`.

Returns

stacks : tuple of numpy arrays
Number of numpy arrays depends on the number of channels recoded during
the experiment. Has the following dimensions:
TZYX or TYX for planar images.

Examples


This function loads a raw image generated by ThorImage as a numpy array . Parameters path : string Path to raw file . metadata : ElementTree root Can be obtained with : func : ` get_metadata ` . Returns stacks : tuple of numpy arrays Number of numpy arrays depends on the number of channels recoded during the experiment . Has the following dimensions : TZYX or TYX for planar images . Examples
def load_raw(path, metadata):
    path = os.path.expanduser(os.path.expandvars(path))
    n_time_points = metadata.get_n_time_points()
    width = metadata.get_num_x_pixels()
    height = metadata.get_num_y_pixels()
    n_channels = metadata.get_n_channels()
    byte_size = os.stat(path).st_size
    assert not byte_size % 1, "File does not have an integer byte length."
    byte_size = int(byte_size)
    n_z = (
        byte_size / 2 / width / height / n_time_points / n_channels
    )  
    assert (
        not n_z %
        1), "Size given in metadata does not match the size of the raw file."
    n_z = int(n_z)
    meta_n_z = metadata.get_n_z()
    if n_z == 1:
        stacks = np.zeros((n_channels, n_time_points, height, width),
                          dtype="uint16")
        image_size = width * height
        t_size = (width * height * n_channels)
        with open(path, "rb") as f:
            for t in range(n_time_points):
                a = array.array("H")
                a.fromfile(f, t_size)
                for c in range(n_channels):
                    stacks[c, t, :, :] = np.array(a[c * image_size:(c + 1) *
                                                    image_size]).reshape(
                                                        (height, width))
    elif n_z > 1:
        stacks = np.zeros((n_channels, n_time_points, meta_n_z, height, width),
                          dtype="uint16")
        image_size = width * height
        t_size = (
            width * height * n_z * n_channels
        )   (this includes images for all channels)
        with open(path, "rb") as f:
            for t in range(n_time_points):
                a = array.array("H")
                a.fromfile(f, t_size)
                a = np.array(a).reshape(
                    (-1, image_size
                     ))  
                for c in range(n_channels):
                    stacks[c, t, :, :, :] = a[c::n_channels, :].reshape(
                        (n_z, height, width))[:meta_n_z, :, :]
    area_mode = metadata.get_area_mode()
    if area_mode in ('line', 'kymograph') and meta_n_z > 1:
        concatenated = []
        for stack in stacks:
            concatenated.append(concatenate_z(stack))
        stacks = concatenated
    if len(stacks) == 1:
        return (np.squeeze(stacks[0]), )
    return tuple(np.squeeze(stacks))
def load_raw ( path , metadata ) : path = os . path . expanduser ( os . path . expandvars ( path ) ) n_time_points = metadata . get_n_time_points ( ) width = metadata . get_num_x_pixels ( ) height = metadata . get_num_y_pixels ( ) n_channels = metadata . get_n_channels ( ) byte_size = os . stat ( path ) . st_size assert not byte_size % 1 , "File does not have an integer byte length." byte_size = int ( byte_size ) n_z = ( byte_size / 2 / width / height / n_time_points / n_channels ) assert ( not n_z % 1 ) , "Size given in metadata does not match the size of the raw file." n_z = int ( n_z ) meta_n_z = metadata . get_n_z ( ) if n_z == 1 : stacks = np . zeros ( ( n_channels , n_time_points , height , width ) , dtype = "uint16" ) image_size = width * height t_size = ( width * height * n_channels ) with open ( path , "rb" ) as f : for t in range ( n_time_points ) : a = array . array ( "H" ) a . fromfile ( f , t_size ) for c in range ( n_channels ) : stacks [ c , t , : , : ] = np . array ( a [ c * image_size : ( c + 1 ) * image_size ] ) . reshape ( ( height , width ) ) elif n_z > 1 : stacks = np . zeros ( ( n_channels , n_time_points , meta_n_z , height , width ) , dtype = "uint16" ) image_size = width * height t_size = ( width * height * n_z * n_channels ) with open ( path , "rb" ) as f : for t in range ( n_time_points ) : a = array . array ( "H" ) a . fromfile ( f , t_size ) a = np . array ( a ) . reshape ( ( - 1 , image_size ) ) for c in range ( n_channels ) : stacks [ c , t , : , : , : ] = a [ c : : n_channels , : ] . reshape ( ( n_z , height , width ) ) [ : meta_n_z , : , : ] area_mode = metadata . get_area_mode ( ) if area_mode in ( 'line' , 'kymograph' ) and meta_n_z > 1 : concatenated = [ ] for stack in stacks : concatenated . append ( concatenate_z ( stack ) ) stacks = concatenated if len ( stacks ) == 1 : return ( np . squeeze ( stacks [ 0 ] ) , ) return tuple ( np . squeeze ( stacks ) )
This function loads a raw image generated by ThorImage as a numpy array.
This function loads a raw image generated by ThorImage as a numpy array .
"""
    This function loads a raw image generated by ThorImage as a numpy array.

    Parameters
    ----------
    path : string
        Path to raw file.
    metadata : ElementTree root
        Can be obtained with :func:`get_metadata`.

    Returns
    -------
    stacks : tuple of numpy arrays
        Number of numpy arrays depends on the number of channels recoded during
        the experiment. Has the following dimensions:
        TZYX or TYX for planar images.

    Examples
    --------
    >>> import utils2p
    >>> metadata = utils2p.Metadata('data/mouse_kidney_raw/2p/Untitled_001/Experiment.xml')
    >>> stack1, stack2 = utils2p.load_raw('data/mouse_kidney_raw/2p/Untitled_001/Image_0001_0001.raw',metadata)
    >>> type(stack1), type(stack2)
    (<class 'numpy.ndarray'>, <class 'numpy.ndarray'>)
    >>> utils2p.save_img('stack1.tif',stack1)
    >>> utils2p.save_img('stack2.tif',stack2)
    """ # divide by two because the values are of type short (16bit = 2byte) # number of z slices from meta data can be different # because of flyback frames # number of values stored for a given time point # (this includes images for all channels) # print('{}/{}'.format(t,n_time_points)) # number of values stored for a given time point (this includes images for all channels) # print('{}/{}'.format(t,n_time_points)) # each row is an image alternating between channels
path
metadata