utils2p/synchronization.py
def reduce_during_frame(line, values, function):
    """
    Reduces all values occurring during the acquisition of a
    frame to a single value using the `function` given by the user.
    The line function should be of the resolution of
    the ThorSync ticks and have the frame index as values.
    Possible choices are the processed frame_counter line or the
    processed cam_line.

    Parameters
    ----------
    line : numpy array
        Line holding frame indices.
    values : numpy array
        Values upsampled to the frequency of ThorSync,
        i.e. 1D numpy array of the same length as
        `frame_counter`.
    function : function
        Function used to reduce the value,
        e.g. np.mean.

    Returns
    -------
    reduced : numpy array
        Numpy array with value for each 2p frame.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> line_names = ["Frame Counter", "CO2_Stim"]
    >>> (frame_counter, stimulus_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> frame_counter = utils2p.synchronization.process_frame_counter(frame_counter, steps_per_frame=1)
    >>> stimulus_line = utils2p.synchronization.process_stimulus_line(stimulus_line)
    >>> np.max(frame_counter)
    4
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.mean)
    >>> len(stimulus_during_2p_frames)
    5
    >>> np.max(stimulus_during_2p_frames)
    0.7136134613556422
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.max)
    >>> len(stimulus_during_2p_frames)
    5
    >>> set(stimulus_during_2p_frames)
    {0.0, 1.0}
    """
    if len(line) != len(values):
        raise ValueError("line and values need to have the same length.")

    thor_sync_indices = tuple(edges(line, (0, np.inf))[0])

    starts = thor_sync_indices
    stops = thor_sync_indices[1:] + (len(line), )

    if not line[0] == -9223372036854775808:
        starts = (0, ) + starts
        stops = (thor_sync_indices[0], ) + stops

    dtype = values.dtype
    if np.issubdtype(dtype, np.number):
        dtype = np.float
    else:
        dtype = np.object
    reduced = np.empty(len(starts), dtype=dtype)

    for i, (start, stop) in enumerate(zip(starts, stops)):
        reduced[i] = function(values[start:stop])

    return reduced

    Reduces all values occurring during the acquisition of a
    frame to a single value using the `function` given by the user.
    The line function should be of the resolution of
    the ThorSync ticks and have the frame index as values.
    Possible choices are the processed frame_counter line or the
    processed cam_line.

    Parameters
    ----------
    line : numpy array
        Line holding frame indices.
    values : numpy array
        Values upsampled to the frequency of ThorSync,
        i.e. 1D numpy array of the same length as
        `frame_counter`.
    function : function
        Function used to reduce the value,
        e.g. np.mean.

    Returns
    -------
    reduced : numpy array
        Numpy array with value for each 2p frame.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> line_names = ["Frame Counter", "CO2_Stim"]
    >>> (frame_counter, stimulus_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> frame_counter = utils2p.synchronization.process_frame_counter(frame_counter, steps_per_frame=1)
    >>> stimulus_line = utils2p.synchronization.process_stimulus_line(stimulus_line)
    >>> np.max(frame_counter)
    4
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.mean)
    >>> len(stimulus_during_2p_frames)
    5
    >>> np.max(stimulus_during_2p_frames)
    0.7136134613556422
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.max)
    >>> len(stimulus_during_2p_frames)
    5
    >>> set(stimulus_during_2p_frames)
    {0.0, 1.0}
    
Reduces all values occurring during the acquisition of a
frame to a single value using the `function` given by the user.
The line function should be of the resolution of
the ThorSync ticks and have the frame index as values.
Possible choices are the processed frame_counter line or the
processed cam_line.

Parameters

line : numpy array
Line holding frame indices.
values : numpy array
Values upsampled to the frequency of ThorSync,
i.e. 1D numpy array of the same length as
`frame_counter`.
function : function
Function used to reduce the value,
e.g.

Returns

reduced : numpy array
Numpy array with value for each 2p frame.

Examples


Reduces all values occurring during the acquisition of a frame to a single value using the ` function ` given by the user . The line function should be of the resolution of the ThorSync ticks and have the frame index as values . Possible choices are the processed frame_counter line or the processed cam_line . Parameters line : numpy array Line holding frame indices . values : numpy array Values upsampled to the frequency of ThorSync i . e . 1D numpy array of the same length as ` frame_counter ` . function : function Function used to reduce the value e . g . Returns reduced : numpy array Numpy array with value for each 2p frame . Examples
def reduce_during_frame(line, values, function):
    if len(line) != len(values):
        raise ValueError("line and values need to have the same length.")
    thor_sync_indices = tuple(edges(line, (0, np.inf))[0])
    starts = thor_sync_indices
    stops = thor_sync_indices[1:] + (len(line), )
    if not line[0] == -9223372036854775808:
        starts = (0, ) + starts
        stops = (thor_sync_indices[0], ) + stops
    dtype = values.dtype
    if np.issubdtype(dtype, np.number):
        dtype = np.float
    else:
        dtype = np.object
    reduced = np.empty(len(starts), dtype=dtype)
    for i, (start, stop) in enumerate(zip(starts, stops)):
        reduced[i] = function(values[start:stop])
    return reduced
def reduce_during_frame ( line , values , function ) : if len ( line ) != len ( values ) : raise ValueError ( "line and values need to have the same length." ) thor_sync_indices = tuple ( edges ( line , ( 0 , np . inf ) ) [ 0 ] ) starts = thor_sync_indices stops = thor_sync_indices [ 1 : ] + ( len ( line ) , ) if not line [ 0 ] == - 9223372036854775808 : starts = ( 0 , ) + starts stops = ( thor_sync_indices [ 0 ] , ) + stops dtype = values . dtype if np . issubdtype ( dtype , np . number ) : dtype = np . float else : dtype = np . object reduced = np . empty ( len ( starts ) , dtype = dtype ) for i , ( start , stop ) in enumerate ( zip ( starts , stops ) ) : reduced [ i ] = function ( values [ start : stop ] ) return reduced
Reduces all values occurring during the acquisition of a
frame to a single value using the `function` given by the user.
Reduces all values occurring during the acquisition of a frame to a single value using the ` function ` given by the user .
"""
    Reduces all values occurring during the acquisition of a
    frame to a single value using the `function` given by the user.
    The line function should be of the resolution of
    the ThorSync ticks and have the frame index as values.
    Possible choices are the processed frame_counter line or the
    processed cam_line.

    Parameters
    ----------
    line : numpy array
        Line holding frame indices.
    values : numpy array
        Values upsampled to the frequency of ThorSync,
        i.e. 1D numpy array of the same length as
        `frame_counter`.
    function : function
        Function used to reduce the value,
        e.g. np.mean.

    Returns
    -------
    reduced : numpy array
        Numpy array with value for each 2p frame.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> line_names = ["Frame Counter", "CO2_Stim"]
    >>> (frame_counter, stimulus_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> frame_counter = utils2p.synchronization.process_frame_counter(frame_counter, steps_per_frame=1)
    >>> stimulus_line = utils2p.synchronization.process_stimulus_line(stimulus_line)
    >>> np.max(frame_counter)
    4
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.mean)
    >>> len(stimulus_during_2p_frames)
    5
    >>> np.max(stimulus_during_2p_frames)
    0.7136134613556422
    >>> stimulus_during_2p_frames = utils2p.synchronization.reduce_during_frame(frame_counter, stimulus_line, np.max)
    >>> len(stimulus_during_2p_frames)
    5
    >>> set(stimulus_during_2p_frames)
    {0.0, 1.0}
    """
line
values
function