Tierpsy / tierpsy-tracker

Multi-Worm Behaviour Tracker. README here:
https://github.com/Tierpsy/tierpsy-tracker/blob/development/README.md
MIT License
21 stars 19 forks source link

Getting timestamp from the hdf5 file #57

Closed loftusa closed 3 years ago

loftusa commented 3 years ago

Hey all.

I have an hdf5 file generated by tierpsy loaded in Python:

import pandas as pd
file_location = "<filename>.hdf5"
f = pd.HDFStore(file_location)

I'm looking to work directly with the timeseries data. In particular, I care about the worm index, timestamp, and motion modes:

motion_modes = f.get("features_timeseries")[["worm_index", "timestamp", "motion_modes"]]

I'd like to figure out the frames-per-second for the particular video that generated this HDF5 file. In particular, I'd like to average the motion-mode to some kind of "movement-per-second" metric, with the motion mode averaged for all rows for a particular worm in a given second.

Is there an easy way to pull out the length of the video, or even better, the frames-per-second? the frames used in "timestamp" aren't very useful to me if I don't have a time-conversion.

luigiferiani commented 3 years ago

Hi @loftusa,

this should work if you only want the framerate (as long as it was set correctly in the parameters json file):

from tierpsy.helper.params import read_unit_conversions
masked_file_location = "<masked file location>.hdf5"
fps, microns_per_pixel, is_light_background = read_unit_conversions(masked_filename)

Alternatively, if you only have the *_featuresN.hdf5 file, you can find the fps amongst the attributes to the /trajectories_data in the hdf5 file:

import h5py
with h5py.File(masked_file_location, 'r') as fid:
        fps = fid["/trajectories_data"].attrs["fps"]

In the /trajectories_data table inside the hdf5 you also have a few time-related columns. timestamp_time should be in seconds, provided that an fps value was given to Tierpsy in the parameters json file!

loftusa commented 3 years ago

Hi @loftusa,

this should work if you only want the framerate (as long as it was set correctly in the parameters json file):

from tierpsy.helper.params import read_unit_conversions
masked_file_location = "<masked file location>.hdf5"
fps, microns_per_pixel, is_light_background = read_unit_conversions(masked_filename)

Alternatively, if you only have the *_featuresN.hdf5 file, you can find the fps amongst the attributes to the /trajectories_data in the hdf5 file:

import h5py
with h5py.File(masked_file_location, 'r') as fid:
        fps = fid["/trajectories_data"].attrs["fps"]

In the /trajectories_data table inside the hdf5 you also have a few time-related columns. timestamp_time should be in seconds, provided that an fps value was given to Tierpsy in the parameters json file!

Excellent, this is so helpful, thanks so much!

luigiferiani commented 3 years ago

happy that helped, closing the issue now :)