ETHZ-INS / DLCAnalyzer

R Scripts to analyze deep lab cut point data and related data
GNU General Public License v3.0
59 stars 16 forks source link

[Query] Equivalent python package ? #3

Open basicvisual opened 3 years ago

basicvisual commented 3 years ago

Hi @lukasvonziegler , first of all thank you for the post analysis scripts for Deeplabcut. I had two questions

  1. Is there an equivalent python package for analysis ?
  2. Can it be used for any animal skeleton as long as it has a set number of keypoints ?

/B

lukasvonziegler commented 3 years ago

Hi there

Thank you for your interest in this package. here are the answers:

  1. to my knowledge there is no similar package for python to do this type of analysis and we do not have the capacity to port ours at this moment unfortunately
  2. yes! you can use any number of points that can be part of either a skeleton of any type of any animal or of a arena of any type and it will also work with it. for some functions you might have to specify the name of the points that have to be analyzed (i.e. in our OFT analysis we have preset "bodycentre" as the default point for which we analyze zone visits etc, but the functions can be called with the name of any other point)

Best, Lukas

lukasvonziegler commented 3 years ago

Also, if you are interested in using the machine learning part of it you might have to define new functions (i.e. such as our CreateSkeletonData_OFT() ) for your personal skeleton description so they contain distances/angles/areas/accelerations that are defined with your points and the corresponding names. best is to copy paste an already existing function and redefine all these features so they fit your personal skeleton.

basicvisual commented 3 years ago

Thank you , could you briefly explain the conversion from pixel/s to physical unit m/s? How did you do it and what is the conversion factor and is it the same for any video format ?

Just out of curiosity , was there a reason for implementing the analysis on R instead of python ?

lukasvonziegler commented 3 years ago

The conversion is a simple linear conversion and only works if: a) the tracked subject is equidistant to the camera all the time (i.e. as in a top recording where it is moving in a non-tilted plane) and b) the distance of at least a tracked point pair in this setup is static and known in metric units (i.e. an edge of the arena which does not change and we can measure the length in whatever unit we want to transform to). The process is described in the documentation. by using the CalibrateTrackingData() function the measured distance between a point pair can be added and the coordinates will be transformed from pixel space to the new unit space. however, if the recording is more complex and has depth a new calibration function would have to be devised so the current package can not solve this.

The reason for sticking with R is that it ha excellent data analysis and plotting packages that we were familiar with.

basicvisual commented 3 years ago

Thank you , I am more focused on used case , that is an object moving from left to right . Camera point is fixed. You mentioned that you knew the area of where the object was moving is that right , for taking into consideration ?

Ok, now i understand why you went for R implementation . Its just that I am personally not familiar with R . I guess this package can be used as a black box ? Do you have some suggestions if one can convert R scripts in python scripts ?

GrantFolkert commented 3 years ago

In case you're still interested, I've successfully used rpy2 to run multi-file OFT and EPM analyses in a Python Colab notebook (their actual R runtime can't mount a Drive at the moment). The results can then easily be assigned from R to Pandas dataframes using "line magic". Create a folder with both DLCAnalyzer_Functions_final.R and a new Python notebook in it and try:

%load_ext rpy2.ipython

import rpy2.robjects.packages as rpackages

utils = rpackages.importr('utils')

utils.chooseCRANmirror(ind=1) # select the first mirror in the list

packnames = ('sp', 'imputeTS', 'ggplot2', 'ggmap', 'data.table', 'cowplot', 'corrplot')

from rpy2.robjects.vectors import StrVector

names_to_install = [x for x in packnames if not rpackages.isinstalled(x)] if len(names_to_install) > 0: utils.install_packages(StrVector(names_to_install))

(Cell break)

%%R

library(sp) #tested with v1.3-2 library(imputeTS) #tested with v3.0 library(ggplot2) #tested with v3.1.0 library(ggmap) #tested with v3.0.0 library(data.table) #tested with v1.12.8 library(cowplot) #tested with v0.9.4 library(corrplot) #tested with v0.84

And proceed according to the normal instructions with the caveat of adding %%R at the top of your cells. Not really a conversion to Python, but the upshot is that you can use these great functions from @lukasvonziegler alongside/within your normal Python workflow. The R code itself is understandable without necessarily "knowing" R.