mlfpm / deepof

DeepLabCut based data analysis package including pose estimation and representation learning mediated behavior recognition
MIT License
37 stars 6 forks source link

Multiple ROI #35

Open jess255 opened 7 months ago

jess255 commented 7 months ago

Hello, I just found out about deepof and found it really great. I would like to use it but I was wondering if it was possible to do multiple ROI. Thank you 😊 !

lucasmiranda42 commented 7 months ago

Hi, @jess255!

And thank you for your interest in DeepOF. May I ask you for details about what you mean with multiple ROI? You mean quantifying the time the animals spend in multiple regions of interest in the arena?

Best, and happy to help as soon as I know a bit more! Lucas

jess255 commented 7 months ago

Hi @lucasmiranda42 ,

Yes exacly, I would like to use deepof to analyse a 3chamber test. So there's three chambers + 2 "interactions zones" inside the left and right chamber. I would also be interested in seeing if deepof can spot some individual behaviors in the different areas of the Arena. Thank you very much for your help.

Jessica

lucasmiranda42 commented 7 months ago

Dear Jessica,

Thank you for the details!

Unfortunately, there is no way currently to define regions of interest directly (although this was discussed for future releases), hence all annotations refer to the entire arena.

What you could do instead, if I understand correctly, is to:

1) Visually explore the spatial distribution of certain behaviors a posteriori. For both supervised and unsupervised annotations, you can easily plot heatmaps using the deepof.visuals.plot_heatmaps function. See this tutorial and the API documentation for details.

2) This will likely not be enough if you need to quantify spatial enrichment. One thing you could do, albeit a bit cumbersome, would be to define your regions of interest with point coordinates and then filter your trajectories (and / or annotations) to see whether they occur inside the defined region or not. Below a simple example:

import numpy as np
import deepof.data
from shapely.geometry import Point, Polygon

# Define the corners of a polygon delimiting your ROI
ROI_1 = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])

# Define a polygon with the provided corners
ROI_1_polygon = Polygon(ROI_1)

# Create a mask specifying whether the center of your desired animal (here I assume one animal per experiment) is within the polygon
# Here, we assume that deepof_coords is a coordinates object containing your DLC / SLEAP tracks
in_ROI_1 = {
    key: np.array(
        [ROI_1_polygon.contains(Point(n)) for n in deepof_coords[key].Center.values]
    )
    for key, value in deepof_coords.items()
}

# Filter your DeepOF coordinates to keep only those frames in which the selected animal is within the desired coordinates
coords_in_ROI_1 = {
    key: value.loc[in_ROI_1[key]] for key, value in deepof.coords.items()
}

I'll tag this as a good first issue to contribute! DeepOF is growing its maintenance staff from April, so we should have the manpower to add this soon! However, in case you would like to contribute and have the time, here are our community guidelines. We'd be more than happy to help!

Best, Lucas

jess255 commented 6 months ago

Hello Lucas, Thanks a lot for your reply! Indeed I would like to quantify spatial enrichment but maybe I can make it work as you are suggesting. Thanks again for the help.

Best, Jessica