# Interpreting df/f trace data and finding stimulus onset

**URL:** <https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588>\
**Category:** Technical\
**Tags:** api, brain-observatory-visual-coding, analysis, how-to, allensdk\
**Created:** [June 8, 2020, 10:47pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588 "2020-06-08T22:47:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewchen37](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@matthewchen37](https://community.brain-map.org/u/matthewchen37)\
**Post date:** [June 8, 2020, 10:47pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/1 "2020-06-08T22:47:55Z")

</div>

I’m using the AllenSDK as part of my research and specifically want to use the Df/f trace data. When I plot out Df/f vs time (using get\_dff\_trace() and saving the timestamps and the trace data like the tutorial [https://allensdk.readthedocs.io/en/latest/\_static/examples/nb/brain\_observatory.html](https://allensdk.readthedocs.io/en/latest/_static/examples/nb/brain_observatory.html))  
I get a plot for a cell like this:

![dff_traces_cell_specimen_id_517406566](https://canada1.discourse-cdn.com/flex027/uploads/brainobservatory/original/1X/87cad75db10dea4f03643a38161b3658e3f1b08d.png)

Some data about this specific experiment and cell that I’m referring to from it’s metadata file if you’re wondering:

‘age\_days’: 108,  
‘cre\_line’: ‘Cux2-CreERT2/wt’,  
‘device’: ‘Nikon A1R-MP multiphoton microscope’,  
‘device\_name’: ‘CAM2P.2’,  
‘excitation\_lambda’: ‘910 nanometers’,  
‘experiment\_container\_id’: 511507650,  
‘fov’: ‘400x400 microns (512 x 512 pixels)’,  
‘genotype’: ‘Cux2-CreERT2/wt;Camk2a-tTA/wt;Ai93(TITL-GCaMP6f)/Ai93(TITL-GCaMP6f)’,  
‘imaging\_depth\_um’: 175,  
‘indicator’: ‘GCaMP6f’,  
‘ophys\_experiment\_id’: 501794235,  
‘pipeline\_version’: ‘3.0’,  
‘session\_start\_time’: datetime.datetime(2016, 2, 8, 14, 9, 15),  
‘session\_type’: ‘three\_session\_B’,  
‘sex’: ‘male’,  
‘specimen\_name’: ‘Cux2-CreERT2;Camk2a-tTA;Ai93-222424’,  
‘targeted\_structure’: ‘VISp’

I understand that this plot graphs out all of session B, but is there any way I can splice the data to only show the df/f for static gratings? The stimulus epoch table that I get from this experiment does not line up with the timestamps from the df/f data (this goes from 747 to 113623 while the dff\_trace timestamps go from around 0 to 4000)

stimulus epoch table:  
stimulus start end  
0 static\_gratings 747 15198  
1 natural\_scenes 16102 30550  
2 spontaneous 30700 39579  
3 natural\_scenes 39580 54028  
4 static\_gratings 54931 69378  
5 natural\_movie\_one 70281 79310  
6 natural\_scenes 80213 96091  
7 static\_gratings 97370 113623

Also is there a way to determine at what specific time a stimulus is given relative to the timestamps from df/f trace, specifically for static gradients? My research mentor recommended that I get approximately 3 seconds before the “static gradient stimulus onset” and 3 seconds after of df/f in a process called “epoching”.

I apologize if any of this sounds confusing. I’ve just been learning about this over the past week and any clarifications would be helpful!

---

<div class="post-metadata">

**Author:** ![saskiad](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/saskiad/32/1695_2.png) [@saskiad](https://community.brain-map.org/u/saskiad)\
**Post date:** [June 10, 2020, 11:02pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/2 "2020-06-10T23:02:14Z")

</div>

The stimulus epoch table and the stimulus table provide indexes into the trace itself. So when the start & end for static\_gratings is 747 and 15198, you can plot this epoch with

> plt.plot(dff\_trace[cell\_id, 747:15198])

The imaging was acquired at 30Hz, so to add 3 seconds before and after, you would add 900 frames to either end.

---

<div class="post-metadata">

**Author:** ![matthewchen37](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@matthewchen37](https://community.brain-map.org/u/matthewchen37)\
**Post date:** [June 11, 2020, 5:43pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/3 "2020-06-11T17:43:24Z")

</div>

Thank you so much for the reply! In that case, if I want to access mean df/f for each of the 50 trials like this graph from the Stimulus documentation:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/brainobservatory/original/1X/9d0b222761c3cd274fb82a15a298b45e3489d48e.png)

Are there any functions that can plot this graph? Or extract the pre-calculated mean df/f data? Or would I have to calculate the mean df/f myself?

EDIT: I’ve been trying to plot this graph using plot\_sg\_traces from: [https://alleninstitute.github.io/AllenSDK/allensdk.brain\_observatory.brain\_observatory\_plotting.html#allensdk.brain\_observatory.brain\_observatory\_plotting.plot\_sg\_traces](https://alleninstitute.github.io/AllenSDK/allensdk.brain_observatory.brain_observatory_plotting.html#allensdk.brain_observatory.brain_observatory_plotting.plot_sg_traces)

but seem to be getting an error whenever I run the code? Here is the relevant code:

```
sg = StaticGratings(expDataSet)
stim_table = sg.stim_table
print(stim_table)
observ_plot.plot_sg_traces(sg, "sg_traces")

```

where expDataSet is the data set from the experiment and observ\_plot is allensdk.brain\_observatory.brain\_observatory\_plotting

This is my error message:

```
TypeError: only size-1 arrays can be converted to Python scalars`

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "C:\Users\Matthew Chen\Anaconda3\lib\site-packages\pandas\core\nanops.py", line 69, in _f
    return f(*args, **kwargs)

  File "C:\Users\Matthew Chen\Anaconda3\lib\site-packages\pandas\core\nanops.py", line 125, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)

  File "C:\Users\Matthew Chen\Anaconda3\lib\site-packages\pandas\core\nanops.py", line 762, in nanvar
    avg = _ensure_numeric(values.sum(axis=axis, dtype=np.float64)) / count

  File "C:\Users\Matthew Chen\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 38, in _sum
    return umr_sum(a, axis, dtype, out, keepdims, initial, where)

ValueError: setting an array element with a sequence.

```

EDIT #2: Also, what does the variable in sg.interlength mean? Is this a time interval?

---

<div class="post-metadata">

**Author:** ![saskiad](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/saskiad/32/1695_2.png) [@saskiad](https://community.brain-map.org/u/saskiad)\
**Post date:** [July 2, 2020, 12:21am UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/4 "2020-07-02T00:21:55Z")

</div>

Hi - sorry for the delay!

The df/f per trial and the mean df/f trial are both pre-calculated for you and should be easy to access. Within the analysis object (sg) there is two dataframes called “sweep\_response” and “mean\_sweep\_response”.

> sg = StaticGratings(expDataSet)  
> sweep\_response = sg.sweep\_response  
> mean\_sweep\_response = sg.mean\_sweep\_response

The sweep response contains the DF/F for each trial for each cell in a session. It includes ~1 second prior to the trial start, and ~1 second after it ends. The columns are different cells, with the cell _index_ as the column key as a string. The rows are individual trials, with the same index as the stim table.

The mean sweep response is structured the same way, in terms of columns and rows, but contains the mean DFF during each trial as described in the white paper.

I’ll need to dig into the error that you’ve encountered above, but hopefully this helps you do what you’re trying to do.

---

<div class="post-metadata">

**Author:** ![matthewchen37](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@matthewchen37](https://community.brain-map.org/u/matthewchen37)\
**Post date:** [July 9, 2020, 11:04pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/5 "2020-07-09T23:04:19Z")

</div>

Hi, thank you for the reply!

When looking at the sweep response, for experiment 511507650 I see 154 columns meaning 154 cells, but when I view the experiment online, [https://observatory.brain-map.org/visualcoding/search/cell\_list?experiment\_container\_id=511507650&sort\_field=p\_sg&sort\_dir=asc](https://observatory.brain-map.org/visualcoding/search/cell_list?experiment_container_id=511507650&sort_field=p_sg&sort_dir=asc), filtering for the experiment, the cell list shows 305 cells. Why are there different cell numbers for the same experiment?

---

<div class="post-metadata">

**Author:** ![saskiad](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/saskiad/32/1695_2.png) [@saskiad](https://community.brain-map.org/u/saskiad)\
**Post date:** [July 9, 2020, 11:22pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/6 "2020-07-09T23:22:35Z")

</div>

When you look at the experiment online, you see all of the cells that were identified for any of the three sessions that make up that experiment container. If you recall, for each field of view, we imaged three different times using different stimuli. We match cells across the sessions, and some cells are found in all three sessions, some in any combination of 2 of the 3 sessions, and some just in one session. You should notice on the website that for some cells, for some stimuli, it says NA which means that cell wasn’t identified for the session that included that stimulus. The sweep response dataframe only includes cells that were identified during the session of that particular stimulus.

(Also, just in case you hadn’t noticed this yet, one column in the sweep response is the running speed, and is called “dx”).

---

<div class="post-metadata">

**Author:** ![matthewchen37](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@matthewchen37](https://community.brain-map.org/u/matthewchen37)\
**Post date:** [July 20, 2020, 8:50pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/7 "2020-07-20T20:50:42Z")

</div>

That makes sense, thanks for the response! In regards to the running speed, I noticed there were negative values included when I do data\_set.get\_running\_speed(). Though, I thought that speed was a scalar quantity so speed should always be positive? I noticed that the speed is measured via a wheel/disk the mouse is placed on. Is this true for all stimuli? If so, do the positive and negative values correspond to counterclockwise and clockwise directions in centimeters/sec based on the rotational speed of the wheel?

---

<div class="post-metadata">

**Author:** ![saskiad](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/saskiad/32/1695_2.png) [@saskiad](https://community.brain-map.org/u/saskiad)\
**Post date:** [July 21, 2020, 5:32pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/8 "2020-07-21T17:32:58Z")

</div>

Yeah, the wheel rotation as the mouse runs forward results in positive speeds. If the mouse pushes the wheel backwards, the speeds will be negative. Usually that happens when a mouse is rocking the wheel back and forth a bit, I don’t think I’ve seen a mouse actually run backwards in these data.

---

<div class="post-metadata">

**Author:** ![RodrigoSandon](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/rodrigosandon/32/486_2.png) [@RodrigoSandon](https://community.brain-map.org/u/RodrigoSandon)\
**Post date:** [July 31, 2020, 5:33pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/9 "2020-07-31T17:33:57Z")

</div>

Hello Dr. de Vries,

Question on the experiments that are available as of now in the Brain Observatory (456) versus the number of experiments mentioned in your paper “A large-scale standardized physiological survey reveals functional organization of the mouse visual cortex” (432). Were more experiments added since the publishing of the paper or were some experiments omitted? If some experiments were omitted from the paper, why?

Thank you,

Rodrigo

---

<div class="post-metadata">

**Author:** ![saskiad](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/saskiad/32/1695_2.png) [@saskiad](https://community.brain-map.org/u/saskiad)\
**Post date:** [August 3, 2020, 4:55pm UTC](https://community.brain-map.org/t/interpreting-df-f-trace-data-and-finding-stimulus-onset/588/10 "2020-08-03T16:55:12Z")

</div>

Hi Rodrigo,

It’s a little of both. New data was added between the initial paper submission and the final accepted version. Most of the new data was incorporated into the paper, but we chose not to include any of the GCaMP6s data and only analyze the GCaMP6f data in the paper.
