Visualising how much of an environment has been visited multiple times

194 views
Skip to first unread message

Elizabeth Allison

unread,
Dec 12, 2017, 6:05:58 AM12/12/17
to Bonsai Users
We would like to have a plot which shows how much of the environment has been visited at least twice. We were trying to split the environment up into 5cm square bins and have a plot of whether each bin has been visited, and if it has been visited multiple separate times. Ideally we'd have a plot of the 1m square environment divided into 20*20 bins which counts how many times each bin has been visited, and changes the bin's colour when it's visited once and again once it's been visited more than once. To count as a visit, the animal has to go to a different bin in between two visits. (So staying in a bin does not count as another visit).

Any ideas how we should do this?
Thanks very much,
Tizzy & Klara

Gonçalo Lopes

unread,
Dec 12, 2017, 11:22:34 AM12/12/17
to Elizabeth Allison, Bonsai Users
Hi Tizzy,

I am attaching a modified version of an earlier histogram example that should solve this. For reference, the original example looked like this:



The problem with this workflow is that every single frame contributes to bin counts. In order to count just "visits" to a bin, you need to change it so that you only count frames when the animal switches bins. You can do this by further filtering the centroid before sending it to histogram.


The modified workflow would look like this (also attached):


Basically only three nodes were added:
 - ValidPoint: this is a Condition where we take only points that are valid (i.e. non-NaN). This is just to make the next python transform easier.
 - CentroidToBinPosition: this is a custom PythonTransform that basically converts the centroid to bin coordinates by dividing the X and Y components by each bin size:

import clr
clr
.AddReference("OpenCV.Net")
from OpenCV.Net import *

nbins
= 20
binSizeX
= 640 / nbins
binSizeY
= 480 / nbins

@returns(Point2f)
def process(value):
  binX
= int(value.X / binSizeX)
  binY
= int(value.Y / binSizeY)
 
return Point2f(binX, binY)

 - DistinctUntilChanged: this is the important bit. The DistinctUntilChanged node only emits a value when the input changes. This means that if the animal stays in bin (5,5) for 20 frames, the node will only fire when the bin changes to the next one, e.g. (6, 5).

After this transformation you can just visualize the histogram like normal using Histogram2D. Make sure the bin sizes lineup between this node and the python script. There are ways to make it more convenient and robust in case this is what you want.

Hope this helps.



--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/332b2869-c9ef-406d-9bba-9067252aa0e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

histogram2d.bonsai

Elizabeth Allison

unread,
Dec 13, 2017, 10:43:47 AM12/13/17
to Bonsai Users
Thanks very much. I think this will work.
I think if I'm understanding how it's working currently, the shade of white/grey on the 2d histogram is relative to the maximum value in the histogram, i.e. 2 visits to one bin will be a different shade depending on what the maximum number of visits is in the most visited bin. Is there a way to make it max out at 3 visits and not keep adding to the visit count beyond that?
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.

Gonçalo Lopes

unread,
Dec 13, 2017, 4:37:57 PM12/13/17
to Elizabeth Allison, Bonsai Users
Hi Tizzy,

For that purpose you can probably just use a Threshold on the histogram output, with ThresholdType set to Truncate, and Value set to 3.
This will should make all pixels above 3 to be stuck at the maximum.

Hope this helps.

To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages