Skip to content

Map Annotations To ISXD Data

Compute Credits

This tool uses 1.0 compute credits per hour.

Overview

flowchart LR A[ISXD files] --> C; B[Experiment Annotations file.
Each row is a time point.] --> C[Mapped Experiment Annotations file
parquet format];

This tool maps frames from one or more experiment annotations files to frames in one or more .isxd files, and generates a single annotations file in a .parquet format that can be used with the following tools:

This tool does two things:

  • maps frames from input annotations to an isxd file
  • convert those annotations to a format usable by other tools (into a .parquet file)

Which tool to use?

  • Use the Map Boris Annotations To ISXD Data tool if you have a TSV file where each row specifies the start and stop of some bout.
  • Use this tool if you have a CSV file where each row corresponds to a single time point
  • Use this tool if you have an IDEAS experiment annotations file generated from an .isxb file which is synchronized to an .isxd file (using the nVision system).

Parameters

Parameter Required? Default Description
ISXD File(s) True N/A ISXD files. Can be a single file, or a series.
Annotations File(s) True N/A Annotations in IDEAS format. File(s) with time column and a column specifying annotations at each time point.
Time Column True time Name of column in input annotations files specifying timestamps to use for mapping to ISXD data.
State Column True state Name of column in input annotations files specifying state annotations

Input Data

Valid Inputs

Source Parameter File Type File Format
ISXD File(s) cell_set, miniscope_movie isxd, isxd
Annotations File(s) unknown, experiment_annotations, experiment_annotations csv, csv, parquet

ISXD File(s)

The ISXD input can be one of the following types:

  • Movie: The file contains a time series of movie frames and metadata acquired by an Inscopix system.
  • Cell set: The file contains time series traces for each cell in the field-of-view of the recording along with the corresponding footprints.

A series of ISXD files can also be used as an input.

Input CSV Annotations File(s)

The annotations file can be either a .csv or .parquet file that must satisfy the following:

  • It may have any number of columns
  • There exists a column that contains timestamps in seconds relative to the start time of the recording. These timestamps are numbers convertible to floats
  • There exists a column that contains values representing the event types, and must be of a type convertible to a string
  • The number of annotations files should match the number of ISXD files. Alternatively, a single annotations file with timestamps relative to the start time of the first ISXD file may be used.

Uploading input annotations file

When you upload an input annotations file for mapping and conversion using this tool, add this to a data column that has data type "Any".

nVision Timing information

For annotations generated by an nVision system (i.e., generated from an .isxb movie): The annotations should contain a column named Hardware counter (us) representing hardware counter timestamps generated by nVision, which can be compared directly to hardware counter timestamps in the corresponding synchronized .isxd files. This column will be used to precisely map frames from the annotations to the isxd files.

Third-Party Timing information

For annotations not generated by an nVision system (i.e., not generated from an .isxb movie): If possible, use values in the time column that correspond to the time values in the cell set. If these values don't match, this tool will map them as best as possible, by finding the closest annotations in time.

Un-mappable Frames

For both nVision and third-party timing information, the tool will find the annotation frame with a timestamp closest in time to an ISXD frame. If the time difference between the annotation frame and ISXD frame is greater than the average frame period of the annotations, then the annotation frame is not mapped to the ISXD frame, since it's deemed to have occurred to far away from the ISXD frame. The mapped annotation frame for that ISXD frame is left undefined (i.e., empty cell in the output).

The content of a sample CSV annotation file is shown below, what could be used to encode behavioral information:

time state Position
3.54 rearing 123.5
3.90 sniffing 343.1
5.67 sniffing 345.1

You may also use such a file to specify other sorts of annotations. For example:

time drug
0 vehicle
0.1 vehicle
0.2 vehicle
0.3 DrugA

For annotations files generated from .isxb files, the contents of the file will have some specific columns containing timing metadata necessary for mapping. For example, here's an annotations file generated by the Compute Locomotion Metrics tool that can be used as input to this tool:

Frame number Time since start (s) Hardware counter (us) Average keypoint x Average keypoint y Displacement (px) Speed (px/s) Smoothed Speed (px/s) Displacement (cm) Speed (cm/s) Smoothed Speed (cm/s) State
0 0.0 247903537725.0 494.0040043422154 415.5110539027623 29.005753815209786 2.9005753815209787 rest
1 0.052012 247903589737.0 494.3198699951172 417.4007219587053 1.9158852972801972 36.835447536726086 75.02198242991568 0.1915885297280197 3.6835447536726087 7.502198242991568 move
2 0.100012 247903637737.0 493.5560368129185 424.491446358817 7.131746886182038 148.57806012879243 103.43969501356901 0.7131746886182038 14.857806012879243 10.3439695013569 move
3 0.15201 247903689735.0 491.2410409109933 429.7851126534598 5.777725232733714 111.1143742592737 96.02808974478721 0.5777725232733715 11.11143742592737 9.602808974478721 move
4 0.200012 247903737737.0 488.97420610700334 430.5046822684152 2.37830201180324 49.54589416697721 61.577474410340926 0.237830201180324 4.954589416697721 6.157747441034092 move
... ... ... ... ... ... ... ... ... ... ...

The Time since start (s) and State columns will be specified as the column names for the input to the tool. Additionally, the Hardware counter (us) column will be used to precisely map frames from the annotations to the corresponding synchronized ISXD files.

Parameters

Parameter Required? Description
Cell Set File Yes Name of ISXD file(s)
Annotations File Yes Name of the annotations file(s)
Column name: Time No Label of the column for the time value. Default = "time"
Column name: State No Label of the column for the state value. Default = "state"

Algorithm details

This tool will map frames in the annotations file(s) for every frame in the .isxd file(s), by comparing timestamps using the following procedure.

Step 1: Detect hardware counter timestamps

The tool first checks if a column named Hardware counter (us) exists within the annotations file. This column contains hardware counter timestamps (also known as tsc values) extracted from the .isxb file which generated the annotations file.

What are hardware counter timestamps?

On nVision and miniscope systems, there is a very precise, hardware counter, storing the time overlap in microseconds, and is constantly incremented over time. For every frame captured by either system, the value of the hardware counter is recorded and saved to the per-frame metadata of the .isxd or .isxb file.

For nVision and miniscope systems which are synchronized, the systems share the same hardware counter. This means that the hardware counter timestamps in synchronized .isxd and .isxb files can be directly compared, because they share the same reference time (i.e., the shared hardware counter).

Info

Tools which generate annotations files from .isxb movies (e.g., Average DeepLabCut Keypoints, nVision Animal Tracking Model) will write this column to the annotations files so that it can be used by this tool.

Step 2: Compute annotations timestamps relative to .isxd timestamps

If hardware counter timestamps are present, then they are used to compute accurate timestamps of the samples in the annotations file, relative to the start of the reference .isxd file.

Start time of synchronized .isxd and .isxb files

Normally, synchronized .isxd and .isxb files do not start at the exact same time. This is because when a synchronized recording is started, there will always be a small (unavoidable) amount of latency in sending a signal to the nVision and miniscope systems to start acquisition. Moreover, the signal to start recording will likely arrive at slightly different times between the nVision and miniscope systems. Often times, the nVision recordings start a few hundred ms after the miniscope recording.

Since the start of the reference .isxd file is not the same as the annotations .isxb file, the tool computes the time delta between the first frame of the reference .isxd file and the first frame of the annotations .isxb file, and then adds this time delta to every frame timestamp of the annotations .isxb file.

For example, consider the following .isxd and .isxb files. Each file has four frames, and is recorded at a frequency of 1 Hz, however the .isxb file starts one second after the .isxd file.

>>> isxd_times = [0, 1, 2, 3] # timestamps in s of isxd file, relative to start of isxd file
>>> isxd_tsc = [0, 1000000, 2000000, 3000000] # tsc in us of isxd file

>>> isxb_times = [0, 1, 2, 3] # timestamps in s of isxb file, relative to start of isxb file
>>> isxb_tsc = [1000000, 2000000, 3000000, 4000000] # tsc in us of isxb file

>>> time_delta = (isxb_tsc[0] - isxd_tsc[0]) / 1e6 # compute time delta in s
>>> isxb_times += time_delta # add time delta to every isxb timestamp
>>> isxb_times # timestamps in s of isxb file, relative to start of isxd file
    [1, 2, 3, 4]

No hardware counter timestamps

If there is no Hardware counter (us) column in the annotations file, then the tool assumes the reference .isxd file and annotations file start at the same time. For synchronized recordings, always ensure the annotations file has this column so that this tool can map frames accurately.

Series inputs

For series inputs, the tsc time delta is computed for every pair of .isxd and annotations files.

Moreover, the timestamps for .isxd and annotations files are actually computed using the Unix Epoch start time stored in the .isxd file metadata. This is to ensure that time differences between each .isxd file is factored into the timestamps computed for the annotations file. For series inputs, the Unix Epoch start time of each .isxd file is used as the reference start timestamp for the corresponding annotations file.

Step 3: Map annotations frames using timestamps

For every .isxd frame, the tool will find the annotation frame with the closest timestamp. If two annotation frames are equally close to the .isxd frame, then the annotation frame with the nearest even frame number will be selected.

The following pseudocode, demonstrates the steps of this mapping with an .isxd file containing 8 frames recorded at 1 Hz, and an .isxb file containing 4 frames recorded at 0.5 Hz, starting 1 s after the .isxd file:

>>> isxd_times = [0, 1, 2, 3, 4, 5, 6, 7] # timestamps in s of isxd file, relative to start of isxd file
>>> annotations_times = [1, 3, 5, 7] # timestamps in s of isxb file, relative to start of isxd file
>>> annotations_frames = [0, 1, 2, 3] # frame number of annotations file to map to isxd file (i.e., 0, 1, ... n)

# for every frame of the isxd file, find the closest frame in the annotations file
>>> mapped_annotations_frames = np.interp(isxd_times, annotations_times, annotations_frames)
>>> mapped_annotations_frames
array([0. , 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. ])

# need to round to whole integer frame number
>>> mapped_annotations_frames = np.round(mapped_annotations_frames).astype(int)
>>> mapped_annotations_frames
array([0, 0, 0, 1, 2, 2, 2, 3])

# map annotation frames to isxd frames
>>> np.array(annotations_times)[mapped_annotations_frames]
array([1, 1, 1, 3, 5, 5, 5, 7])
Here are links to the documentation for details on the interpolation algorithm and rounding algorithm.

Note

If the time delta between an .isxd frame and its corresponding mapped annotation frame is greater than the average frame period of the .isxd file, then the mapped annotation frame is set to NaN. This is for cases where there are significant gaps in time between the .isxd and annotations files.

Step 4: Map all other columns of the annotations file(s)

In the final step, the tool creates output annotations files with every other column in the annotations files, mapped to the .isxd files. Some additional columns are also included in the mapped annotations files, described in the next section.

Output Data

The tool will output a parquet file with at least the following columns:

  • frame: The frame number in the input ISXD file
  • series: The file number in the input ISXD series
  • time The timestamps, in seconds, from the input ISXD file. This is in recording time, meaning time delays are not factored between files in an ISXD series.
  • time since start (s) The timestamps, in seconds, from the input ISXD file. This is relative to the start of the first ISXD file (i.e., start of experiment), meaning time delays are factored between files in an ISXD series.
  • mapped frame: The frame number in the annotations file that maps to the corresponding ISXD file. This frame will have to closest timestamp to the corresponding ISXD frame.
  • mapped time since start (s) The timestamps, in seconds, from the input annotations file. This is relative to the start of the first ISXD file (i.e., start of experiment), meaning time delays are factored between files in an annotations series.
  • state labels for the state mapped from the input annotations

An example table is shown below:

frame series time time since start (s) mapped frame mapped time since start (s) state
0 0 0.0 0.0
1 0 0.09991 0.9991
2 0 0.1998 0.1998
3 0 0.2997 0.2997
4 0 0.3996 0.3996 0 0.387 moving
5 0 0.4996 0.4996 0 0.487 moving
... ... ... ... ... ...
6000 1 600.0001 1200.0001
6001 1 600.09991 1200.9991
6002 1 600.1998 1200.1998
6003 1 600.2997 1200.2997
6004 1 600.3996 1200.3996 18237 1200.387 moving
6005 1 600.4996 1200.4996 18240 1200.487 moving
... ... ... ... ... ...

Other columns in input annotations

Other columns in the input annotations are mapped as well to the output annotations.

Downloading and inspecting annotations file

The output .parquet file can be downloaded and used for downstream analysis using any tool that can read .parquet files, see here for examples.