Developer API

snub.io

snub.io.project.add_annotator(project_directory, name, labels=None, autosave=True, annotations=None, label_color=(255, 255, 255), off_color=(0, 0, 0), on_color=(255, 0, 0), label_font_size=12, height_ratio=1, order=0, initial_visibility=True)[source]

Add a widget for annotating frames of a video.

Parameters
  • project_directory (str) – Project that the heatmap should be added to.

  • name (str) – The name of the heatmap displayed in SNUB and used for editing the config file.

  • labels (list of str, default=None) – Classes to annotate. Required if annotations is not given.

  • autosave (bool, default=True) – Whether to automatically save annotations after every change.

  • annotations (dict, default=None) – Initial annotations, as dict mapping class names of lists of intervals. Required if labels is not given.

  • label_color ((int,int,int), default=(255,255,255)) – Color of the labels superimposed on the annotator heatmap.

  • off_color ((int,int,int), default=(0,0,0)) – Color of “off” frames (i.e. those outside an annotated interval)

  • on_color ((int,int,int), default=(255,0,0)) – Color of “on” frames (i.e. those inside an annotated interval)

  • label_font_size (int, default=12) – Size of the labels superimposed on the annotator heatmap.

  • height_ratio (int, default=1) – The relative height initially allocated to this data-view in the track-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the track-stack.

  • initial_visibility (bool, default=True) – Whether this data-view is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – annotator properties

Return type

dict

snub.io.project.add_heatmap(project_directory, name, data, time_intervals=None, binsize=None, start_time=None, sort_method=None, labels=None, initial_show_labels=True, max_label_width=300, label_color=(255, 255, 255), label_font_size=12, colormap='viridis', vmin=None, vmax=None, add_traceplot=True, height_ratio=1, trace_height_ratio=1, heatmap_height_ratio=2, order=0, initial_visibility=True)[source]

Add a heatmap to your SNUB project. If plotting neural data, it is helpful to sort the rows of the heatmap so that correlated neurons are clustered together (use the sort_method argument; see snub.io.sort() for options).

Parameters
  • project_directory (str) – Project that the heatmap should be added to.

  • name (str) – The name of the heatmap displayed in SNUB and used for editing the config file.

  • data (ndarray or str) – 2D array (or path to an array) where rows are variables and columns are time bins. Note that if data is given as a path, it will not be copied to the project directory. If you want to copy the data, load it into memory and pass the array directly.

  • time_intervals (ndarray, default=None) – Time interval (in seconds) associated with each column of the heatmap, given as a (N,2) array with [start,end] in each row. If time_intervals=None, then values for binsize and start_time must be given.

  • binsize (float, default=None) – Uniform time interval (in seconds) associated with each column of the heatmap. It is assumed that the intervals have no gaps or overlaps. If this is not the case, use the time_intervals argument.

  • start_time (float, default=None) – Start time (in seconds) of the earliest time interval in the heatmap. start_time is used in conjunction with binsize to construct the time interval for each column.

  • sort_method (str/ndarray, default=None) – Method for sorting the rows of the heatmap. sort_method can either be an array directly specifying the row order or a str defining a sort method from snub.io.sort(). If sort_method=None, the original ordering of the rows will be used.

  • labels (list of str, default=None) – Labels for each variable (row) in the heatmap. If add_traceplot=True, the labels are also used to plot specific variables in the trace plot associated with the heatmap. When no labels are given, they default to the integer order of each row. If the elements of labels are not unique, their integer order is prepended.

  • initial_show_labels (bool, default=True) – Determines whether row labels are initially visible.

  • max_label_width (int, default=300) – How far the label text can encroach on the heatmap (in pixels). This is only relevant if any of the labels are really long.

  • label_color ((int,int,int), default=(255,255,255)) – Color of the labels superimposed on the heatmap.

  • label_font_size (int, default=12) – Size of the labels superimposed on the heatmap.

  • colormap (str, default='viridis') – Colormap used for rendering the heatmap values. The colormap options are ported from matplotlib via cmapy

  • vmin (float, default=None) – Floor for the colormap. If vmin=None, it will be set to to the 1st percentile of the data values. This parameter can be adjusted within the browser.

  • vmax (float, default=None) – Ceiling for the colormap. If vmax=None, it will be set to to the 99th percentile of the data values. This parameter can be adjusted within the browser.

  • add_traceplot (bool, default=True) – Determines whether or not a trace plot is added with the heatmap. This is useful for pulling out and plotting specific rows from the heatmap.

  • height_ratio (int, default=1) – The relative height initially allocated to this data-view in the track-stack. Spacing can also be adjusted within the browser.

  • trace_height_ratio (int, default=1) – If add_traceplot=True, this parameter determines the relative height initially allocated to the trace plot associated with the heatmap. Spacing can also be adjusted within the browser.

  • heatmap_height_ratio (int, default=2) – If add_traceplot=True, this parameter determines the relative height initially allocated to the heatmap compared to its associated trace plot. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the track-stack.

  • initial_visibility (bool, default=True) – Whether the heatmap is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – heatmap properties

Return type

dict

snub.io.project.add_pose3D(project_directory, name, data, links=[], binsize=None, start_time=None, time_intervals=None, joint_labels=None, joint_colors=None, link_colors=None, floor_bounds=None, floor_height=0, floor_spacing=10, floor_color=(1, 1, 1, 0.5), joint_size=5, link_width=2, height_ratio=1, order=0, initial_visibility=True)[source]

Add a 3D pose plot to your SNUB project.

Parameters
  • project_directory (str) – Project that the 3D post viewer should be added to.

  • name (str) – The name of the 3D pose viewer displayed in SNUB and used for editing the config file.

  • data (ndarray | str) – 3D array with axes (time, keypoints, dims) containing the coordinates of each keypoint at each time. Use NaN values to represent missing data. Can be the array itself or the relative path to a npy file.

  • time_intervals (ndarray | str, default=None) – Time interval (in seconds) associated with each pose in the data array, given as a (N,2) array with [start,end] in each row. If time_intervals=None, then values for binsize and start_time must be given. time_intervals can also be a string, in which case it should be the relative path to a npy file.

  • binsize (float, default=None) – Uniform time interval (in seconds) associated with each pose in the data array. It is assumed that the intervals have no gaps or overlaps. If this is not the case, use the time_intervals argument.

  • start_time (float, default=None) – Start time (in seconds) of the earliest time interval in the data array. start_time is used in conjunction with binsize to construct the time interval for each column of the data array.

  • joint_labels (list of str, default=None) – Label for each joint. If joint_labels=None, the labels are automatically generated based on the order of the keypoints.

  • links (list, default=[]) – List of pairs of joint indexes to be connected by a line. For example, links=[[0,1],[1,2]] would connect the first three joints in each pose.

  • joint_colors (ndarray, default=None) – Array of colors for each joint. If joint_colors=None, all joints are assigned the color white.

  • link_colors (ndarray, default=None) – Array of colors for each link. If link_colors=None, all links are assigned the color white.

  • floor_bounds ((float,float,float,float), default=None) – Bounds of the floor in the format (xmin,xmax,ymin,ymax).

  • floor_height (float, default=0) – Height of the floor in the z-dimension.

  • floor_spacing (float, default=10) – Spacing between the floor grid lines.

  • floor_color ((float,float,float,float), default=(1,1,1,0.5)) – Color of the floor grid lines in the format (r,g,b,a).

  • joint_size (float, default=5) – Size of the joints in the plot.

  • link_width (float, default=2) – Width of the links in the plot.

  • height_ratio (int, default=1) – The relative height initially allocated to this data-view in the panel-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the panel-stack.

  • initial_visibility (bool, default=True) – Whether the 3D pose viewer is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – 3D pose viewer properties

Return type

dict

snub.io.project.add_roiplot(project_directory, name, rois, videopaths, copy_video=False, heatmap_name=None, fps=30, start_time=0, timestamps=None, labels=None, contour_colors={}, linewidth=1, height_ratio=1, order=0, initial_visibility=True)[source]

Add an ROI plot to your SNUB project. Can be associated with a heatmap.

Parameters
  • project_directory (str) – Project that the ROI plot should be added to.

  • name (str) – The name of the ROI plot displayed in SNUB and used for editing the config file.

  • rois (ndarray) – ROI shapes as (N,H,W) array where N is the number of rows in the heatmap, and W,H are the width and height respectively of the window containing the rois.

  • videopaths (dict) – Dictionary mapping video names to video paths. All the videos are assumed to share the same timestamps, and in particular to have the same number of frames. The videos will be read as 8bit RGB. Other video formats, such as TIF, will have to be converted to 8bit RGB before they can be added. The video should be have the same dimensions (W,H) as rois.

  • copy_video (bool, default=True) – Whether to copy the video to the project directory.

  • fps (float, default=30) – The video framerate. This parameter is used in conjunction with start_time to generate a timestamps file, unless an array of timestamps is directly provided.

  • start_time (float, default=0) – The start time of the video (in seconds). This parameter is used in conjunction with fps to generate a timestamps file, unless an array of timestamps is directly provided.

  • timestamps (str/array-like, default=None) – Array of timestamps in units of seconds. timestamps can either be an array, or the path to .npy file, or the path to a text file with a timestamp on each line. If timestamps=None, the timestamps will be created from fps and start_time.

  • labels (list of str, default=None) – Label for each ROI. If the ROI plot is bound to a heatmap, the labels are used to establish correspondence between the ROIs and the rows of the heatmap. When no labels are given, they default to the integer order of each ROI. If the elements of labels are not unique, their integer order is prepended.

  • contour_colors (dict, default={}) – To assign specific colors to any of the ROIs when plotting contours, use contour_colors[label] = (r,g,b), where r,g,b are ints [0-255].

  • heatmap_name (str, default=None) – Heatmap containing data associated with the ROIs. Each ROI will be matched with a row of the heatmap based on shared labels. Colors for each ROI will also be inherited from the heatmap.

  • linewidth (int, default=1) – Linewidth for plotting the contours outlining each ROI

  • height_ratio (int, default=1) – The relative height initially allocated to this data-view in the panel-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the panel-stack.

  • initial_visibility (bool, default=True) – Whether the ROI plot is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – ROI plot properties

Return type

dict

snub.io.project.add_scatter(project_directory, name, xy_coordinates, time_intervals=None, binsize=None, start_time=None, variables=None, variable_labels=None, colormap='viridis', xlim=None, ylim=None, pointsize=10, linewidth=1, facecolor=(180, 180, 180), edgecolor=(0, 0, 0), selected_edgecolor=(255, 255, 0), current_node_size=20, current_node_color=(255, 0, 0), selection_intersection_threshold=0.5, size_ratio=1, order=0, initial_visibility=True)[source]

Add a scatter plot to your SNUB project.

Parameters
  • project_directory (str) – Project that the scatter plot should be added to.

  • name (str) – The name of the scatter plot displayed in SNUB and used for editing the config file.

  • xy_coordinates (ndarray) – 2D coordinates as a (N,2) array.

  • time_intervals (ndarray, default=None) – Time interval (in seconds) associated with each point in the scatter plot, given as a (N,2) array with [start,end] in each row. If time_intervals=None, then values for binsize and start_time must be given.

  • binsize (float, default=None) – Uniform time interval (in seconds) associated with each point in the scatter plot. It is assumed that the intervals have no gaps or overlaps. If this is not the case, use the time_intervals argument.

  • start_time (float, default=None) – Start time (in seconds) of the earliest time interval associated with the scatter plot. start_time is used in conjunction with binsize to construct the time interval for each point.

  • variables (ndarray, default=None) – Variables to use for coloring nodes in the scatter plot. variables should be a numpy array of shape (N,M) where N is the number of nodes and M is the number of variables. variable_labels must be specified as well.

  • variable_labels (list(str), default=None) – Label corresponding to each column of variables.

  • colormap (str, default='viridis') – Colormap used for plotting variables (must be one of the colormaps in matplotlib).

  • xlim ([float, float], default=None) – Initial visible range of the scatter plot. If none is given, the range will be determined automatically using pyqtgraph. The visible range can be changed in the browser by panning and zooming.

  • ylim ([float, float], default=None) – Initial visible range of the scatter plot. If none is given, the range will be determined automatically using pyqtgraph. The visible range can be changed in the browser by panning and zooming.

  • pointsize (int, default=10) – Diameter of point in the scatter plot.

  • linewidth (int, default=1) – Width of the borderline on each point.

  • facecolor ((int,int,int), default=(180,180,180)) – Color of points in the scatter plot.

  • edgecolor ((int,int,int), default=(100,100,100)) – Edge color of points in the scatter plot when they are not selected.

  • selected_edgecolor ((int,int,int), default=(255,255,0)) – Edge color of points in the scatter plot when they are selected.

  • current_node_size (int, default=20) – If the current time overlaps with a point’s time interval, the point is highlighted by appearing larger and in another color. current_node_size determines the diameter of the highlighted point.

  • current_node_color ((int,int,int), default=(255,0,0)) – If the current time overlaps with a point’s time interval, the point is highlighted by appearing larger and in another color. current_node_size determines the color of the highlighted point.

  • selection_intersection_threshold (float, default=0.5) – When the current selection overlaps the interval associated with a point in the scatter plot, the point is highlighted. selection_intersection_threshold determines the amount of overlap required before the point is highlighted. A value of 1 means that the point’s time interval must be fully covered.

  • size_ratio (int, default=1) – The relative space initially allocated to this data-view in the panel-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the panel-stack. Panels are arranged top-to-bottom (in column mode) or left-to-right (row mode) by ascending rank of the order property.

  • initial_visibility (bool, default=True) – Whether the scatter plot is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – scatter properties

Return type

dict

snub.io.project.add_spikeplot(project_directory, name, spike_data, heatmap_range=10, window_size=0.2, window_step=0.05, labels=None, sort_method=None, initial_show_labels=True, max_label_width=300, label_color=(255, 255, 255), label_font_size=12, colormap='viridis', vmin=None, vmax=None, add_traceplot=True, height_ratio=1, trace_height_ratio=1, heatmap_height_ratio=2, order=0, initial_visibility=True)[source]

Add a spike plot to your SNUB project. By default, spike plots convert to heatmaps when sufficiently zoomed out. For electrophysiology, this corresponds to showing firing rates as opposed to firing events (see snub.io.firing_rates()). Most of the options for snub.io.add_heatmap() can also be used here.

If plotting neural data, it is helpful to sort the rows so that correlated neurons are clustered together (use sort_method argument; see snub.io.sort() for options; the firing rates are used for sorting).

Parameters
  • project_directory (str) – Project that the spike plot should be added to.

  • name (str) – Name of the spike plot displayed in SNUB and used for editing the config file.

  • spike_data (ndarray or str) – Spike times and unit labels as a (N,2) array or a path to such an array. The first column contains the spike times (in seconds) and the second column contains the unit labels (as integers). Note that if spike_data is given as a path, it will not be copied to the project directory. If you want to copy the data, load it into memory and pass the array directly.

  • heatmap_range (float, default=10) – Defines the zoom-level at which the spike-view converts to a heatmap-view. The transition occurs when the currently visible range in the timeline is equal to heatmap_range (in seconds)

  • window_size (float, default=0.2) – Length (in seconds) of the sliding window used to calculate firing rates

  • window_step (float, default=0.05) – Step-size (in seconds) between each window used to calculate firing rates

  • initial_visibility (bool, default=True) – Whether the spike plot is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – spikeplot properties

Return type

dict

snub.io.project.add_traceplot(project_directory, name, traces, linewidth=1, trace_colors={}, bgcolor=(0, 0, 0), height_ratio=1, order=0, initial_visibility=True)[source]

Add a traceplot to your SNUB project.

Parameters
  • project_directory (str) – Project that the trace plot should be added to.

  • name (str) – The name of the trace plot displayed in SNUB and used for editing the config file.

  • traces (dict) – Dictionary mapping trace names to trace data. The data for each trace should be a (N,2) array where the first column contains sorted time points (in seconds) and the second column contains the value of the trace at that timepoint.

  • linewidth (int, default=1) – Linewidth used for plotting the traces

  • trace_colors (dict, default={}) – To assign specific colors to any of the traces, use trace_colors[name] = (r,g,b), where r,g,b are ints [0-255].

  • bgcolor ((int,int,int), default=(0,0,0)) – Background color for the traceplot as (r,g,b) where r,g,b are ints [0-255].

  • height_ratio (int, default=1) – The relative space initially allocated to this data-view in the track-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the track-stack.

  • initial_visibility (bool, default=True) – Whether the trace plot is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – trace plot properties

Return type

dict

snub.io.project.add_video(project_directory, videopath, copy=False, name=None, fps=None, start_time=0, timestamps=None, size_ratio=1, order=0, initial_visibility=True)[source]

Add a video to your SNUB project.

Parameters
  • project_directory (str) – Project that the video should be added to.

  • videopath (str) – Path to an existing video. The video will be read as 8bit RGB. Other video formats, such as 16bit depth or 16bit monochrome, will have to be converted to 8bit RGB before they can be added. Functions for doing so are provided in snub.io.video_conversion.

  • copy (bool, default=False) – If copy=True, the video will be copied to the project directory. Otherwise, the video will be referenced in its current location.

  • name (str, default=None) – The name of the video, which is displayed in SNUB and can be used to edit the config file. If no name is given, the video’s filename will be used.

  • fps (float, default=None) – The video framerate. This parameter is used in conjunction with start_time to generate a timestamps file, unless an array of timestamps is directly provided. If None, it is inferred from the video file.

  • start_time (float, default=0) – The start time of the video (in seconds). This parameter is used in conjunction with fps to generate a timestamps file, unless an array of timestamps is directly provided.

  • timestamps (str/array-like, default=None) – Array of timestamps in units of seconds. timestamps can either be an array, or the path to .npy file, or the path to a text file with a timestamp on each line. If timestamps=None, the timestamps will be created from fps and start_time.

  • size_ratio (int, default=1) – The relative space initially allocated to this data-view in the panel-stack. Spacing can also be adjusted within the browser.

  • order (float, default=0) – Determines the order of placement within the panel-stack. Panels are arranged top-to-bottom (in column mode) or left-to-right (row mode) by ascending rank of the order property.

  • initial_visibility (bool, default=True) – Whether the video is initially visible when the project is opened. Visibility can also be toggled within the browser.

Returns

props – video properties

Return type

dict

snub.io.project.create_project(project_directory, overwrite=False, start_time=0, duration=None, end_time=None, layout_mode='columns', tracks_size_ratio=2, panels_size_ratio=1, min_step=0.03333333333333333, animation_fps=30, zoom_gain=0.001, min_range=0.25, init_current_time=None, initial_playspeed=1, track_playhead=True)[source]

Set up a new SNUB project by creating a directory and config.json file.

Parameters
  • project_directory (str) – Project path. A directory will be created at this location.

  • overwrite (bool, default=False) – If a config.json file already exists in the project directory, overwrite=True will cause the file to be overwritten. To edit but not overwrite an existing config file, use snub.io.edit_config().

  • start_time (float, default=0) – Lower bound (in seconds) of the timeline in the SNUB browser.

  • duration (float, default=None) – Sets the upper bound of the timeline in the SNUB browser. If duration is not None, the timeline will bounds will be [start_time,start_time+duration] in seconds. Alternatively, the upper bound can be set using the end_time argument.

  • end_time (float, default=None) – Sets the upper bound of the timeline in the SNUB browser. If end_time is not None, the timeline will bounds will be [start_time,end_time] in seconds. The upper bound can also be set using the duration argument.

  • layout_mode ({'columns', 'rows'}, default='columns') – Sets the layout mode when the project is first opened.

  • tracks_size_ratio (int, default=2) – Relative space initially allocated to the track stack, as opposed to the panel stack. The spacing can also be adjusted within the browser.

  • panels_size_ratio (int, default=1) – Relative space initially allocated to the panel stack, as opposed to the panel stack. The spacing can also be adjusted within the browser.

  • min_step (float, default=0.033) – Though time is represented continuously in SNUB. Some analyses and GUI elements require time to be discretized. In such cases min_step determines the size of each discrete time unit.

  • animation_fps (float, default=30) – Sets the update rate during playback. If you have video data, it is recommended that animation_fps be set to the video framerate, though this is not required for realtime playback.

  • zoom_gain (float, default=0.003) – How fast the timeline zooms in response to scrolling.

  • min_range (float, default=0.01) – Smallest allowable range in seconds that can be displayed on the timeline. Lower values allow the timeline to be dilated more.

  • init_current_time (float, default=None) – SNUB contains a global current_time variable that sets the location of the playhead, the currently displayed video frame, etc. init_current_time sets the current_time when SNUB is first opened. If init_current_time=None, the current time is set the lower bound of the timeline.

  • initial_playspeed (float, default=1) – Initial setting for playback speed. The playback speed can be changed at any time within the browser using a slider.

  • track_playhead (bool, default=True) – Toggles the center-playhead setting. When this setting is True, the playhead stays fixed and the data scrolls by. Otherwise currently visible range rests while the playhead moves across the screen.

Returns

config – Config file for the newly created project

Return type

dict

snub.io.project.edit_dataview_properties(project_directory, dataview_type, name, **kwargs)[source]

Edit the properties of a data-view by inputting the desired key-value pairs as keyword arguments to this function.

Parameters
  • project_directory (str) – Project path containing the config.json file.

  • dataview_type ({'video', 'scatter', 'mesh', 'heatmap', 'spikeplot', 'traceplot'}) – The type of data-view to be edited.

  • name (str) – Name of the data-view to be edited.

snub.io.project.edit_global_config(project_directory, **kwargs)[source]

Edit the properties in an existing config file by inputting the desired key-value pairs as keyword arguments to this function. This function is for editing global properties. To change the properties of a specific data-view, use edit_dataview_properties().

Parameters

project_directory (str) – Project path containing the config.json file.

snub.io.project.generate_intervals(start_time, binsize, num_intervals)[source]

Generate an array of start/end times for non-overlapping time intervals.

Parameters
  • start_time (float) – Left bound of the first time interval

  • binsize (float) – Width of each interval

  • num_intervals (int) –

Returns

time_intervals – Array of shape (num_intervals,2) containing the start and end times for each interval.

Return type

ndarray

snub.io.project.list_dataviews(project_directory)[source]

List of the names of each type of dataview

snub.io.project.load_config(project_directory)[source]

Load the config file in a given project directory

snub.io.project.modify_annotator_label(project_directory, name, new_label, old_label=None)[source]

Add a new label to an annotator or rename an existing label.

Parameters
  • project_directory (str) – Project that the annotator should be modified in.

  • name (str) – Name of the annotator to modify.

  • new_label (str) – New label name to add.

  • old_label (str, default=None) – If given, the label with this name will be replaced with new_label. If not given, new_label will be added to the annotator.

snub.io.project.remove_annotator_label(project_directory, name, label)[source]

Remove a label from an annotator. All annotations for this label will be lost.

Parameters
  • project_directory (str) – Project that the annotator should be modified in.

  • name (str) – Name of the annotator to modify.

  • label (str) – Label name to remove.

snub.io.project.remove_dataview(project_directory, dataview_type, name, delete_data=False)[source]

Remove a data-view from the specified project and (optionally) delete its data.

Parameters
  • project_directory (str) – Path of the project to be edited.

  • dataview_type ({'video', 'scatter', 'mesh', 'heatmap', 'spikeplot', 'traceplot'}) – The type of data-view to be removed.

  • name (str) – Name of the data-view to be remove.

  • delete_data (bool, default=False) – Delete the data associated with the given data-view, which may be .avi, .npy or .hdf5 files depending on the type of data-view. This is automatically prevented if the data is located outside the project directory.

snub.io.project.save_config(project_directory, config)[source]

Save the config dict to the given project directory

snub.io.project.set_markers(project_directory, markers)[source]

Set the time markers for a project. Any existing time markers are overwritten and replaced by the contents of the markers argument.

Time markers are vertical lines over the SNUB timeline that can be used to marker key events such as trials.

Parameters
  • project_directory (str) – Project path containing the config.json file.

  • markers (dict) –

    Dictionary containing the desired time markers. Entries should consist of key-value pairs where the key is the name of the marker and the value is a dict of the form {'time': float, 'color': (int,int,int)}. For example to mark a pair of trials at 10sec and 20sec with white lines, set

    markers = {
        'trial 1': {'time': 10, 'color': (255,255,255)},
        'trial 2': {'time': 20, 'color': (255,255,255)}
    }
    

snub.io.manifold.bin_data(data, binsize, axis=- 1, return_intervals=False)[source]

Bin data using non-overlaping windows along axis

Returns

  • data_binned (ndarray)

  • bin_intervals (ndarray (returned if rerturn_intervals=True)) – (N,2) array with the start and end index of each bin

snub.io.manifold.firing_rates(spike_times, spike_labels, window_size=0.2, window_step=0.05)[source]

Convert spike tikes to firing rates using a sliding window

Parameters
  • spike_times (ndarray) – Spike times (in seconds) for all units. The source of each spike is input separately using spike_labels

  • spike_labels (ndarray) – The source/label for each spike in spike_times. The maximum value of this array determines the number of rows in the heatmap.

  • window_size (float, default=0.2) – Length (in seconds) of the sliding window used to calculate firing rates

  • window_step (float, default=0.05) – Step-size (in seconds) between each window used to calculate firing rates

Returns

  • firing_rates (ndarray) – Array of firing rates, where rows units and columns are sliding window locations. firing_rates has shape (N,M) where:

    N = max(spike_labels)+1
    
    M = (max(spike_times)-min(spike_times))/binsize
    
  • start_time, float – The time (in seconds) corresponding to the left-boundary of the first window in firing_rates.

snub.io.manifold.sort(data, method='rastermap', options={})[source]

Compute neuron ordering that groups neurons with similar activity

Parameters
  • data (ndarray) – Data matrix where rows are neurons and columns are time points

  • method ({'rastermap'}) – Method to use for sorting (currently only rastermap is implemented)

  • options (dict, default={}) –

    Sorting method-specific options.

    ’rastermap’

    options will be passed as keyword arguments when initializing rastermap.mapping.Rastermap

Returns

ordering – Ordering index that can be used for sorting (see numpy.argsort)

Return type

ndarray

snub.io.manifold.umap_embedding(data, standardize=True, n_pcs=20, n_components=2, n_neighbors=100, **kwargs)[source]

Generate a 2D embedding of neural activity using UMAP. The function generates the embedding in three steps:

  1. (Optionally) standardize (Z-score) the activity of each neuron

  2. Perform initial dimensionality reduction using PCA

  3. Run UMAP on the output of PCA

Parameters
  • data (ndarray) – Array of neural activity where rows are neurons and columns are time points

  • standardize (bool, default=True) – Whether to standardize (Z-score) the data prior to PCA

  • n_pcs (int, default=20) – Number of principal components to use during PCA. If n_pcs=None, the binned data will be passed directly to UMAP

  • n_components (int, default=2) – Dimensionality of the embedding

  • n_neighbors (int, default=100) – Passed to UMAP (see explanation of UMAP parameters).

  • **kwargs – Any other UMAP parameters can also be passed as keyword arguments

Returns

coodinates – (N,2) array containing UMAP coordinates

Return type

ndarray

snub.io.manifold.zscore(data, axis=0, eps=1e-10)[source]

Z-score standardize the data along axis

snub.io.video.azure_ir_transform(input_image)[source]

Apply the following transformation to better visualize high-dynamic-range infrared video (used in snub.io.video.transform_azure_ir_stream()) This method is useful for viewing the infrared stream output by an Azure Kinect (K4A) depth sensor.

transformed_image = log(input_image)*322 - 350
Parameters

input_image (ndarray) – Input image as a numpy array (can be any shape)

Returns

transformed_image

Return type

ndarray

snub.io.video.detrend_video(videopath_in, videopath_out, window_length=150, window_step=10, pctl=20, clipping_bounds=(- 20, 45), quality=6)[source]

Detrend a video by subtracting a pixel-wise running percentile.

Parameters
  • videopath_in (str) – Path to the input video

  • videopath_out (str) – Path to write the detrended video

  • window_length (int, default=75) – Window over which to calculate the running percentile.

  • window_step (int, default=5) – Downsampling factor for computing running percentile. For frame i, the frames used to compute the percetile will be [i, i-window_step, i-2*window_step,…,i-window_length]

  • pctl (int, default=20) – Percentile used to calculate background

  • clipping_bounds (tuple(float,float), default=(-20,45)) – Clipping bounds for normalizing detrended video. The interval defined by clip is rescaled to [0,255] in the final video.

  • quality (int, defaut=6) – Quality of output video (passed to imageio writer).

snub.io.video.fast_prct_filt(input_data, level=8, frames_window=3000)[source]

Fast approximate percentage filtering Borrowed from CaImAn

snub.io.video.generate_video_timestamps(videopath, fps=None, start_time=0)[source]

Generate timestamps from a video file.

snub.io.video.transform_azure_ir_stream(inpath, outpath=None, num_frames=None, quality=7)[source]

Convert a 16bit monochrome video to an 8bit mp4 video that can be viewed within SNUB. Each frame is transformed using snub.io.video.azure_ir_transform() and the output video is compressed using ffmpeg. This method is useful for viewing the infrared stream output by an Azure Kinect (K4A) depth sensor.

Parameters
  • inpath (str) – Path to the input video

  • outpath (str, default=None) – Path where the output video will be written (must end in ‘.mp4’). If outpath=None, then the output video will have the same location as inpath with the file extension switched to .mp4.

  • num_frames (int, default=None) – Number of frames to convert. By default the full video is converted.

  • quality (int, default=7) – Quality of output video (passed to imageio writer).