# Running the allensdk analyses on new data - time traces

**URL:** <https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318>\
**Category:** Technical\
**Tags:** brain-observatory-visual-coding, analysis, how-to, allensdk\
**Created:** [October 21, 2019, 12:50pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318 "2019-10-21T12:50:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamsc](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@adamsc](https://community.brain-map.org/u/adamsc)\
**Post date:** [October 21, 2019, 12:50pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/1 "2019-10-21T12:50:20Z")

</div>

Hi all,

I’m trying to run the analyses included as parts of the “drifting gratings” and “natural movies” analyses on new/updated time-traces. Basically I have data exactly in the same form as the data downloaded via the brain observatory package in the sdk:

boc = BrainObservatoryCache(manifest\_file=‘boc/manifest.json’)  
data\_set = boc.get\_ophys\_experiment\_data(exp\_id)  
dg = DriftingGratings(data\_set)

so rather than calling dg.get\_response() to get the precomputed values, I’d like to update the time traces in data\_set and recompute the analyses from scratch.

Is this possible and how can I do it?

Thanks!

-Adam

---

<div class="post-metadata">

**Author:** ![nileg](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/nileg/32/39_2.png) [@nileg](https://community.brain-map.org/u/nileg)\
**Post date:** [October 25, 2019, 10:11pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/2 "2019-10-25T22:11:17Z")

</div>

Hi Adam,

Welcome to the forum!

When you say “exactly in the same form” do you mean:

1. you have NWB files?
2. you have modified some of the data for existing experiments? e.g. calculated your own dff traces.
3. you have whole-cloth new data for entire experiments?
4. something else?

Thanks,  
Nile

---

<div class="post-metadata">

**Author:** ![adamsc](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@adamsc](https://community.brain-map.org/u/adamsc)\
**Post date:** [October 25, 2019, 11:40pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/3 "2019-10-25T23:40:17Z")

</div>

Thanks Nile!

For your questions, I mean 2. I have downloaded the dFF traces, and made modifications to them based on a new algorithm. For all intents and purposes, these can be considered an array in python of the same size and type as the dFF trace matrix I loaded. I want to re-incorporate those new dFF traces and redo the analysis to see if the tuning curves and other distributions are preserved.

Thanks!

-Adam

---

<div class="post-metadata">

**Author:** ![nileg](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/nileg/32/39_2.png) [@nileg](https://community.brain-map.org/u/nileg)\
**Post date:** [October 28, 2019, 6:20pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/4 "2019-10-28T18:20:11Z")

</div>

Gotcha.

[`StimulusAnalysis`](https://github.com/AllenInstitute/AllenSDK/blob/2a5b2be91966ff4a6da5f114aab569f5e611de04/allensdk/brain_observatory/stimulus_analysis.py#L64) objects (the base class of `DriftingGratings` & `NaturalMovie`) are constructed with a [`BrainObservatoryNwbDataSet`](https://github.com/AllenInstitute/AllenSDK/blob/2a5b2be91966ff4a6da5f114aab569f5e611de04/allensdk/core/brain_observatory_nwb_data_set.py#L105), which they use to access data. To access dff, they [call](https://github.com/AllenInstitute/AllenSDK/blob/2a5b2be91966ff4a6da5f114aab569f5e611de04/allensdk/brain_observatory/stimulus_analysis.py#L186) the [get\_dff\_traces](https://github.com/AllenInstitute/AllenSDK/blob/2a5b2be91966ff4a6da5f114aab569f5e611de04/allensdk/core/brain_observatory_nwb_data_set.py#L423) method on their `BrainObservatoryNwbDataSet`. What you need is an object that behaves almost exactly like a `BrainObservatoryNwbDataSet`, but whose `get_dff_traces` method returns your traces instead of the ones in the NWB file.

[Here is an example implementation](https://gist.github.com/NileGraddis/8f0d8781baf93460ddc6371fedde040b).

One caveat is that sweepwise stimuli (those with discrete presentations, like gratings) use the average fluorescence over the 1 second preceding stimulus onset as f\_0, rather than using a running window. The running window df/f is used when a continuous signal is desired, such as when analyzing the response to natural movies, or when analyzing running speed data. The upshot is that altering the way this running window df/f is calculated will not impact analysis metrics calculated from sweepwise stimuli. For more information, please see the [white paper](http://help.brain-map.org/download/attachments/10616846/VisualCoding_VisualStimuli.pdf?version=3&modificationDate=1497305590322&api=v2).

Good luck! Please let us know if you have further questions.  
Nile

---

<div class="post-metadata">

**Author:** ![adamsc](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@adamsc](https://community.brain-map.org/u/adamsc)\
**Post date:** [November 11, 2019, 7:57pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/5 "2019-11-11T19:57:14Z")

</div>

Thanks Nile,

I’ve been trying to get this working right, and I’m starting to get some annoying errors. So one thing is that it looks like there’s an error in line 18 (should that not be “if self.dff\_timestamps is None:” instead of “if self.dff\_timestamps is not None:”?). Second I’m trying to extract a specific neuron, and I’m getting the following issue:

1. If I try

> cache = DffPatchingCache()  
> exp\_id = 503109347  
> regular\_data\_set = cache.get\_ophys\_experiments(ids=[exp\_id])  
> timestamps, dff = regular\_data\_set.get\_dff\_traces()

I get the error:  
—\> 15 timestamps, dff = regular\_data\_set.get\_dff\_traces()  
AttributeError: ‘list’ object has no attribute ‘get\_dff\_traces’

1. If I try

> cache = DffPatchingCache()  
> exp\_id = 503109347  
> regular\_data\_set = cache.get\_ophys\_experiments(exp\_id)  
> timestamps, dff = regular\_data\_set.get\_dff\_traces()

I get the error:  
—\> 11 regular\_data\_set = cache.get\_ophys\_experiments(exp\_id)  
TypeError: expected str, bytes or os.PathLike object, not int

1. If I try

> cache = DffPatchingCache()  
> exp\_id = 503109347  
> regular\_data\_set = cache.get\_ophys\_experiments(exp\_id, dff\_traces=None)  
> timestamps, dff = regular\_data\_set.get\_dff\_traces()

I get the error:  
—\> 12 regular\_data\_set = cache.get\_ophys\_experiments(exp\_id, dff\_traces=None)  
TypeError: get\_ophys\_experiments() got an unexpected keyword argument ‘dff\_traces’

Interestingly, if I replace the dff traces, it seems to work OK:

> patched\_data\_set = cache.get\_ophys\_experiment\_data(exp\_id, dff\_traces=new\_dff)

Any idea what the issue is?

Thanks!

-Adam

---

<div class="post-metadata">

**Author:** ![adamsc](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@adamsc](https://community.brain-map.org/u/adamsc)\
**Post date:** [November 12, 2019, 4:01pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/6 "2019-11-12T16:01:11Z")

</div>

OK, update: somehow I confused “get\_ophys\_experiments” and " get\_ophys\_experiment\_data". I can seemingly create data objects now. I’m getting a second issue, though, in that the actual responses are not being recomputed. For example, using the above code I try:

> exp\_id = 503109347  
> regular\_data\_set = boc.get\_ophys\_experiment\_data(exp\_id)
> 
> # make a random array of the right shape
> 
> timestamps, dff = regular\_data\_set.get\_dff\_traces()  
> rand\_dff = np.random.rand(\*dff.shape)

> # … and patch it onto the dataset
> 
> rand\_data\_set = cache.get\_ophys\_experiment\_data(exp\_id, dff\_traces=rand\_dff)

> regular\_dg\_analysis = DriftingGratings(regular\_data\_set)  
> rand\_dg\_analysis = DriftingGratings(rand\_data\_set)

> dgreg = regular\_dg\_analysis.get\_response()  
> dgrand = rand\_dg\_analysis.get\_response()

> np.sum(np.sum((dgreg[:,1:,:,0] - dgrand[:,1:,:,0])\*\*2,1),0)

This returns all zeros, meaning that the responses are identical and the random time traces don’t seem to be effecting the calculations! Any idea how to fix that?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![nileg](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.brain-map.org/nileg/32/39_2.png) [@nileg](https://community.brain-map.org/u/nileg)\
**Post date:** [December 11, 2019, 7:48pm UTC](https://community.brain-map.org/t/running-the-allensdk-analyses-on-new-data-time-traces/318/7 "2019-12-11T19:48:35Z")

</div>

Hi @adamsc,

Thanks for the update!

I think you’re running into the sweepwise stimuli caveat I mentioned above. For stimuli like drifting gratings that are presented in discrete sweeps, the analysis uses the (neuropil-subtracted) fluorescence rather than df/f ([see here](https://github.com/AllenInstitute/AllenSDK/blob/835d11e212955138fcf1fdea928b0f42eebe0eb2/allensdk/brain_observatory/stimulus_analysis.py#L545)). As described in the whitepaper, the mean fluorescence during the pre-sweep period is used to normalize these values. The running df/f traces are used only when analyzing continuous stimuli.

If you want to get\_response to return values based on your df/f traces, you could hack get\_sweep\_response (linked above) so that it reports a running df/f as the sweep response rather than normalizing corrected\_fluorescence to the pre-sweep period.
