utils2p/synchronization.py
def process_cam_line(line, seven_camera_metadata):
    """
    Removes superfluous signals and uses frame numbers in array.
    The cam line signal form the h5 file is a binary sequence.
    Rising edges mark the acquisition of a new frame.
    The setup keeps producing rising edges after the acquisition of the
    last frame. These rising edges are ignored.
    This function converts the binary line to frame numbers using the
    information stored in the metadata file of the seven camera setup.
    In the metadata file the keys are the indices of the file names
    and the values are the grabbed frame numbers. Suppose the 3
    frame was dropped. Then the entries in the dictionary will
    be as follows:
    "2": 2
    "3": 4
    "4": 5

    Parameters
    ----------
    line : numpy array
        Line signal from h5 file.
    seven_camera_metadata : string
        Path to the json file saved by our camera software.
        This file is usually located in the same folder as the frames
        and is called 'capture_metadata.json'. If None, it is assumed
        that no frames were dropped.

    Returns
    -------
    processed_line : numpy array
        Array with frame number for each time point.
        If no frame is available for a given time,
        the value is -9223372036854775808.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> seven_camera_metadata = utils2p.find_seven_camera_metadata_file("data/mouse_kidney_raw")
    >>> line_names = ["Basler"]
    >>> (cam_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> set(np.diff(cam_line))
    {0, 8, 4294967288}
    >>> processed_cam_line = utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata)
    >>> set(np.diff(processed_cam_line))
    {0, 1, -9223372036854775808, 9223372036854775749}
    >>> cam_line = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0])
    >>> utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata=None)
    array([-9223372036854775808,                    0,                    0,
                              0,                    0,                    0,
                              1,                    1,                    1,
                              1,                    1])
    """
    # Check that sequence is binary
    if len(set(line)) > 2:
        raise ValueError("Invalid line argument. Sequence is not binary.")

    # Find indices of the start of each frame acquisition
    rising_edges = edges(line, (0, np.inf))[0]

    # Load capture metadata or generate default
    if seven_camera_metadata is not None:
        with open(seven_camera_metadata, "r") as f:
            capture_info = json.load(f)
    else:
        capture_info = _capture_metadata([
            len(rising_edges),
        ])

    # Find the number of frames for each camera
    n_frames = []
    for cam_idx in capture_info["Frame Counts"].keys():
        max_in_json = max(capture_info["Frame Counts"][cam_idx].values())
        n_frames.append(max_in_json + 1)

    # Ensure all cameras acquired the same number of frames
    if len(np.unique(n_frames)) > 1:
        raise SynchronizationError(
            "The frames across cameras are not synchronized.")

    # Last rising edge that corresponds to a frame
    last_tick = max(n_frames)

    # check that there is a rising edge for every frame
    if len(rising_edges) < last_tick:
        raise ValueError(
            "The provided cam line and metadata are inconsistent. " +
            "cam line has less frame acquisitions than metadata.")

    # Ensure correct handling if no rising edges are present after last frame
    if len(rising_edges) == int(last_tick):
        average_frame_length = int(np.mean(np.diff(rising_edges)))
        last_rising_edge = rising_edges[-1]
        additional_edge = last_rising_edge + average_frame_length
        if additional_edge > len(line):
            additional_edge = len(line)
        rising_edges = list(rising_edges)
        rising_edges.append(additional_edge)
        rising_edges = np.array(rising_edges)

    processed_line = np.ones_like(line) * np.nan

    current_frame = 0
    first_camera_used = sorted(list(capture_info["Frame Counts"].keys()))[0]
    for i, (start, stop) in enumerate(
            zip(rising_edges[:last_tick], rising_edges[1:last_tick + 1])):
        if capture_info["Frame Counts"][first_camera_used][str(current_frame +
                                                               1)] <= i:
            current_frame += 1
        processed_line[start:stop] = current_frame
    return processed_line.astype(np.int)

    Removes superfluous signals and uses frame numbers in array.
    The cam line signal form the h5 file is a binary sequence.
    Rising edges mark the acquisition of a new frame.
    The setup keeps producing rising edges after the acquisition of the
    last frame. These rising edges are ignored.
    This function converts the binary line to frame numbers using the
    information stored in the metadata file of the seven camera setup.
    In the metadata file the keys are the indices of the file names
    and the values are the grabbed frame numbers. Suppose the 3
    frame was dropped. Then the entries in the dictionary will
    be as follows:
    "2": 2
    "3": 4
    "4": 5

    Parameters
    ----------
    line : numpy array
        Line signal from h5 file.
    seven_camera_metadata : string
        Path to the json file saved by our camera software.
        This file is usually located in the same folder as the frames
        and is called 'capture_metadata.json'. If None, it is assumed
        that no frames were dropped.

    Returns
    -------
    processed_line : numpy array
        Array with frame number for each time point.
        If no frame is available for a given time,
        the value is -9223372036854775808.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> seven_camera_metadata = utils2p.find_seven_camera_metadata_file("data/mouse_kidney_raw")
    >>> line_names = ["Basler"]
    >>> (cam_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> set(np.diff(cam_line))
    {0, 8, 4294967288}
    >>> processed_cam_line = utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata)
    >>> set(np.diff(processed_cam_line))
    {0, 1, -9223372036854775808, 9223372036854775749}
    >>> cam_line = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0])
    >>> utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata=None)
    array([-9223372036854775808,                    0,                    0,
                              0,                    0,                    0,
                              1,                    1,                    1,
                              1,                    1])
    
Removes superfluous signals and uses frame numbers in array.
The cam line signal form the h5 file is a binary sequence.
Rising edges mark the acquisition of a new frame.
The setup keeps producing rising edges after the acquisition of the
last frame. These rising edges are ignored.
This function converts the binary line to frame numbers using the
information stored in the metadata file of the seven camera setup.
In the metadata file the keys are the indices of the file names
and the values are the grabbed frame numbers. Suppose the 3
frame was dropped.

Parameters

line : numpy array
Line signal from h5 file.
seven_camera_metadata : string
Path to the json file saved by our camera software.
This file is usually located in the same folder as the frames
and is called 'capture_metadata.json'. If None, it is assumed
that no frames were dropped.

Returns

processed_line : numpy array
Array with frame number for each time point.
If no frame is available for a given time,
the value is -9223372036854775808.

Examples


Removes superfluous signals and uses frame numbers in array . The cam line signal form the h5 file is a binary sequence . Rising edges mark the acquisition of a new frame . The setup keeps producing rising edges after the acquisition of the last frame . These rising edges are ignored . This function converts the binary line to frame numbers using the information stored in the metadata file of the seven camera setup . In the metadata file the keys are the indices of the file names and the values are the grabbed frame numbers . Suppose the 3 frame was dropped . Parameters line : numpy array Line signal from h5 file . seven_camera_metadata : string Path to the json file saved by our camera software . This file is usually located in the same folder as the frames and is called ' capture_metadata . json ' . If None it is assumed that no frames were dropped . Returns processed_line : numpy array Array with frame number for each time point . If no frame is available for a given time the value is - 9223372036854775808 . Examples
def process_cam_line(line, seven_camera_metadata):
    if len(set(line)) > 2:
        raise ValueError("Invalid line argument. Sequence is not binary.")
    rising_edges = edges(line, (0, np.inf))[0]
    if seven_camera_metadata is not None:
        with open(seven_camera_metadata, "r") as f:
            capture_info = json.load(f)
    else:
        capture_info = _capture_metadata([
            len(rising_edges),
        ])
    n_frames = []
    for cam_idx in capture_info["Frame Counts"].keys():
        max_in_json = max(capture_info["Frame Counts"][cam_idx].values())
        n_frames.append(max_in_json + 1)
    if len(np.unique(n_frames)) > 1:
        raise SynchronizationError(
            "The frames across cameras are not synchronized.")
    last_tick = max(n_frames)
    if len(rising_edges) < last_tick:
        raise ValueError(
            "The provided cam line and metadata are inconsistent. " +
            "cam line has less frame acquisitions than metadata.")
    if len(rising_edges) == int(last_tick):
        average_frame_length = int(np.mean(np.diff(rising_edges)))
        last_rising_edge = rising_edges[-1]
        additional_edge = last_rising_edge + average_frame_length
        if additional_edge > len(line):
            additional_edge = len(line)
        rising_edges = list(rising_edges)
        rising_edges.append(additional_edge)
        rising_edges = np.array(rising_edges)
    processed_line = np.ones_like(line) * np.nan
    current_frame = 0
    first_camera_used = sorted(list(capture_info["Frame Counts"].keys()))[0]
    for i, (start, stop) in enumerate(
            zip(rising_edges[:last_tick], rising_edges[1:last_tick + 1])):
        if capture_info["Frame Counts"][first_camera_used][str(current_frame +
                                                               1)] <= i:
            current_frame += 1
        processed_line[start:stop] = current_frame
    return processed_line.astype(np.int)
def process_cam_line ( line , seven_camera_metadata ) : if len ( set ( line ) ) > 2 : raise ValueError ( "Invalid line argument. Sequence is not binary." ) rising_edges = edges ( line , ( 0 , np . inf ) ) [ 0 ] if seven_camera_metadata is not None : with open ( seven_camera_metadata , "r" ) as f : capture_info = json . load ( f ) else : capture_info = _capture_metadata ( [ len ( rising_edges ) , ] ) n_frames = [ ] for cam_idx in capture_info [ "Frame Counts" ] . keys ( ) : max_in_json = max ( capture_info [ "Frame Counts" ] [ cam_idx ] . values ( ) ) n_frames . append ( max_in_json + 1 ) if len ( np . unique ( n_frames ) ) > 1 : raise SynchronizationError ( "The frames across cameras are not synchronized." ) last_tick = max ( n_frames ) if len ( rising_edges ) < last_tick : raise ValueError ( "The provided cam line and metadata are inconsistent. " + "cam line has less frame acquisitions than metadata." ) if len ( rising_edges ) == int ( last_tick ) : average_frame_length = int ( np . mean ( np . diff ( rising_edges ) ) ) last_rising_edge = rising_edges [ - 1 ] additional_edge = last_rising_edge + average_frame_length if additional_edge > len ( line ) : additional_edge = len ( line ) rising_edges = list ( rising_edges ) rising_edges . append ( additional_edge ) rising_edges = np . array ( rising_edges ) processed_line = np . ones_like ( line ) * np . nan current_frame = 0 first_camera_used = sorted ( list ( capture_info [ "Frame Counts" ] . keys ( ) ) ) [ 0 ] for i , ( start , stop ) in enumerate ( zip ( rising_edges [ : last_tick ] , rising_edges [ 1 : last_tick + 1 ] ) ) : if capture_info [ "Frame Counts" ] [ first_camera_used ] [ str ( current_frame + 1 ) ] <= i : current_frame += 1 processed_line [ start : stop ] = current_frame return processed_line . astype ( np . int )
Removes superfluous signals and uses frame numbers in array.
Removes superfluous signals and uses frame numbers in array .
"""
    Removes superfluous signals and uses frame numbers in array.
    The cam line signal form the h5 file is a binary sequence.
    Rising edges mark the acquisition of a new frame.
    The setup keeps producing rising edges after the acquisition of the
    last frame. These rising edges are ignored.
    This function converts the binary line to frame numbers using the
    information stored in the metadata file of the seven camera setup.
    In the metadata file the keys are the indices of the file names
    and the values are the grabbed frame numbers. Suppose the 3
    frame was dropped. Then the entries in the dictionary will
    be as follows:
    "2": 2
    "3": 4
    "4": 5

    Parameters
    ----------
    line : numpy array
        Line signal from h5 file.
    seven_camera_metadata : string
        Path to the json file saved by our camera software.
        This file is usually located in the same folder as the frames
        and is called 'capture_metadata.json'. If None, it is assumed
        that no frames were dropped.

    Returns
    -------
    processed_line : numpy array
        Array with frame number for each time point.
        If no frame is available for a given time,
        the value is -9223372036854775808.

    Examples
    --------
    >>> import utils2p
    >>> import utils2p.synchronization
    >>> import numpy as np
    >>> h5_file = utils2p.find_sync_file("data/mouse_kidney_raw")
    >>> seven_camera_metadata = utils2p.find_seven_camera_metadata_file("data/mouse_kidney_raw")
    >>> line_names = ["Basler"]
    >>> (cam_line,) = utils2p.synchronization.get_lines_from_h5_file(h5_file, line_names)
    >>> set(np.diff(cam_line))
    {0, 8, 4294967288}
    >>> processed_cam_line = utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata)
    >>> set(np.diff(processed_cam_line))
    {0, 1, -9223372036854775808, 9223372036854775749}
    >>> cam_line = np.array([0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0])
    >>> utils2p.synchronization.process_cam_line(cam_line, seven_camera_metadata=None)
    array([-9223372036854775808,                    0,                    0,
                              0,                    0,                    0,
                              1,                    1,                    1,
                              1,                    1])
    """ # Check that sequence is binary # Find indices of the start of each frame acquisition # Load capture metadata or generate default # Find the number of frames for each camera # Ensure all cameras acquired the same number of frames # Last rising edge that corresponds to a frame # check that there is a rising edge for every frame # Ensure correct handling if no rising edges are present after last frame
line
seven_camera_metadata