Skip to content

Conversation

@pickles-bread-and-butter
Copy link
Contributor

@pickles-bread-and-butter pickles-bread-and-butter commented Jun 15, 2020

Overview:

Currently Omitted Stimuli exist in Behavior Ophys experiments, these stimuli represent when a experiment subject is being shown a gray screen. These stimuli by design have no ending frame or ending timestamp, they always last for 750ms however. Currently these cannot be written as an NWB interval because the datatype requires an ending timestamp, leaving this information out is not a good idea.

Solution:

The proposed solution that the Visual Behavior team agrees with is to take the starting timestamp, which is present in the omitted stimuli data, and add the standard duration time to get the stop time. This fixes the error where NWB intervals require a stop time, the stop_time is now created and saved in the stimulus table row.

Validation:

The solution has been validated by running through four Behavior Ophys experiments and successfully running the function add_stimulus_presentations, with the inclusion of #1619. The test cases cover the cases where stop_time is not found in a row, any additional testing coverage suggestions would be much appreciated.

Notes:

  • This does not fully fix the writing of Behavior Ophys NWB files but contributes to eliminating the errors in writing those files!
  • This PR also adds some documentation to stimulus_processing.py as work was done in this file understanding the omitted stimuli data.

Keys for the name of stimulus are different between eceephys and behavior ophys.
This adds in a small function to get the key represented in each data type and
raise an exception if one key is not found. This was added to allow ophys
behavior experiments to write to nwb files.
I updated the function to be more generic and removed specific case verbaige. I also fixed the failing tests on Linux and Mac. The regex for the tests was returning an error on Linux and Mac.
This function had to be added as the omitted stimuli did not by design
have an ending timestamp. These stimuli therfore could not be written to
NWB files as the interval data object in PyNWB requires an ending time.
This commit adds a function that adds a stop_time to a omitted row that
is based off the known time that omitted stimuli are shown. This allows
for omitted stimuli types to be written to NWB intervals.
I added documentation for the functions in stimulus_presentations.py that I have touched during investigation. These functions are get_stimulus_presentations and get_visual_stimuli_df. I added doc strings to better explain what is happening in the code.
Corrected import name problem, function name changed and I didn't change it.
@codecov-commenter
Copy link

codecov-commenter commented Jun 15, 2020

Codecov Report

Merging #1623 into address-behavior-ophys-NWB-failures will increase coverage by 0.03%.
The diff coverage is 95.45%.

Impacted file tree graph

@@                           Coverage Diff                           @@
##           address-behavior-ophys-NWB-failures    #1623      +/-   ##
=======================================================================
+ Coverage                                35.58%   35.61%   +0.03%     
=======================================================================
  Files                                      344      345       +1     
  Lines                                    33568    33586      +18     
=======================================================================
+ Hits                                     11946    11963      +17     
- Misses                                   21622    21623       +1     
Impacted Files Coverage Δ
allensdk/brain_observatory/nwb/__init__.py 88.14% <85.71%> (-0.13%) ⬇️
.../brain_observatory/behavior/stimulus_processing.py 26.19% <100.00%> (ø)
allensdk/brain_observatory/nwb/nwb_utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5200c74...7d1b06c. Read the comment docs.

@pickles-bread-and-butter pickles-bread-and-butter marked this pull request as ready for review June 15, 2020 16:33
@pickles-bread-and-butter pickles-bread-and-butter changed the base branch from master to address-behavior-ophys-NWB-failures June 16, 2020 18:04
@wbwakeman wbwakeman added the braintv relates to Insitute BrainTV program label Jun 16, 2020

# if there is no stop time in the stimuli it is an 'omitted' stimuli
# row and therefore by design has no stop_time, we must add this in.
if 'stop_time' not in row.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right place to set omitted stop times. The add_stimulus_presentations function is also used by the Ecephys Neuropixels project and it may not be correct (and probably wrong) to add a stop time if an Ecephys Neuropixels stimulus_presentation table row lacked a stop time.

A better place to set omitted stop times would be when loading the presentation table for ophys+behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the function call to the behavior_ophys_nwb_api module where whole stimulus table is now passed!

Moved logic of checking for omitted stimulus stop_times to behavior_ophys_nwb_api.
Added debug statements to the code.
Fixed small bug used pandas iterrows iterable
Changed debug statements
added further debug statements
added anohter debug statement.
Changed to work on indices of original dataframe and removed debug statements.
# All of the omitted stimuli have a duration of 250ms as defined
# by the Visual Behavior team. For questions about duration contact that
# team.
omitted_stimuli_duration = 0.250
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a default argument to set_omitted_stop_time. e.g.

def set_omitted_stop_time(stimulus_table_row: dict, omitted_stimuli_duration: float = 0.250):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set it to default and removed module global.

I changed the input for the omitted stimuli stop time setting function to be the whole DataFrame. I updated the tests and confirmed that the function passes the stimuli table writing.
Removed module global variable defining omitted stimuli time in favour of default function arguement.
@pickles-bread-and-butter
Copy link
Contributor Author

@njmei just an FYI the commits look ugly but it's just for you to see the history, I'm going to squash merge when you approve so they won't be carried into the feature branch.

def test_set_omitted_stop_time(stimulus_table, expected_stop_time):
stimulus_table = pd.DataFrame.from_dict(data=stimulus_table)
nwb_utils.set_omitted_stop_time(stimulus_table)
assert stimulus_table.iloc[0]['stop_time'] == expected_stop_time
Copy link
Contributor

@njmei njmei Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this test check that the entire stimulus table is 'correct' rather than checking that the row 0 stop_time is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to test if expected table is equal to created table.

Updated testting to test entire pd dataframe and not specific elements.
Copy link
Contributor

@njmei njmei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pickles-bread-and-butter pickles-bread-and-butter merged commit f489520 into address-behavior-ophys-NWB-failures Jun 23, 2020
@pickles-bread-and-butter pickles-bread-and-butter deleted the omitted-stimuli-bug branch June 24, 2020 18:08
pickles-bread-and-butter added a commit that referenced this pull request Jun 26, 2020
* Added function to set the stop_time for omitted stimuli for writing NWB

This function had to be added as the omitted stimuli did not by design
have an ending timestamp. These stimuli therfore could not be written to
NWB files as the interval data object in PyNWB requires an ending time.
This commit adds a function that adds a stop_time to a omitted row that
is based off the known time that omitted stimuli are shown. This allows
for omitted stimuli types to be written to NWB intervals.

* Added doc strings to stimulus_presentations.py

I added documentation for the functions in stimulus_presentations.py that I have touched during investigation. These functions are get_stimulus_presentations and get_visual_stimuli_df. I added doc strings to better explain what is happening in the code.
pickles-bread-and-butter added a commit that referenced this pull request Jun 26, 2020
* Added function to set the stop_time for omitted stimuli for writing NWB

This function had to be added as the omitted stimuli did not by design
have an ending timestamp. These stimuli therfore could not be written to
NWB files as the interval data object in PyNWB requires an ending time.
This commit adds a function that adds a stop_time to a omitted row that
is based off the known time that omitted stimuli are shown. This allows
for omitted stimuli types to be written to NWB intervals.

* Added doc strings to stimulus_presentations.py

I added documentation for the functions in stimulus_presentations.py that I have touched during investigation. These functions are get_stimulus_presentations and get_visual_stimuli_df. I added doc strings to better explain what is happening in the code.
pickles-bread-and-butter added a commit that referenced this pull request Jul 16, 2020
* Added function to set the stop_time for omitted stimuli for writing NWB

This function had to be added as the omitted stimuli did not by design
have an ending timestamp. These stimuli therfore could not be written to
NWB files as the interval data object in PyNWB requires an ending time.
This commit adds a function that adds a stop_time to a omitted row that
is based off the known time that omitted stimuli are shown. This allows
for omitted stimuli types to be written to NWB intervals.

* Added doc strings to stimulus_presentations.py

I added documentation for the functions in stimulus_presentations.py that I have touched during investigation. These functions are get_stimulus_presentations and get_visual_stimuli_df. I added doc strings to better explain what is happening in the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

braintv relates to Insitute BrainTV program

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants