-
Notifications
You must be signed in to change notification settings - Fork 0
added get_average_signal_window. #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c43f409
added get_average_signal_window. linted
rachelstephlee 29239c8
documentation
rachelstephlee 93fe48c
shortened the name of the average signal column
rachelstephlee d6eb368
debugged, added check for nwb.trials
rachelstephlee 0353df6
fixed linting. there's still something buggy/off about this average s…
rachelstephlee a4d618f
found and fixed bug for now
rachelstephlee bb60bd0
linting
rachelstephlee 737ac73
Merge branch 'main' into avg-signal-window
rachelstephlee 1bdc40c
addressed alex's concerns. linted. need to test.
rachelstephlee 2658acd
checked functionality. added example.
rachelstephlee a4fbcd0
added multiple NWB version as well
rachelstephlee fafcd05
added checks
rachelstephlee 49daa8a
tested
rachelstephlee 4ab80d3
linting
rachelstephlee f5d31c4
returning original nwb.df_trials if channel not there.
rachelstephlee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| import aind_dynamic_foraging_data_utils.nwb_utils as nu | ||
| import aind_dynamic_foraging_models.logistic_regression.model as model | ||
| import aind_dynamic_foraging_basic_analysis.plot.plot_fip as pf | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
|
|
@@ -234,3 +235,69 @@ def add_intertrial_licking(df_trials, df_licks): | |
| df_trials["intertrial_choice"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean() | ||
| ) | ||
| return df_trials | ||
|
|
||
|
|
||
| def get_average_signal_window( | ||
| nwb, | ||
| alignment_event, | ||
| offsets, | ||
| channel, | ||
| data_col='data_z', | ||
| output_col=None, | ||
| censor=True | ||
| ): | ||
| """ | ||
| Returns a Series with the mean signal in a window around an alignment event, | ||
| for each trial, for each session and a specific signal (event). | ||
|
|
||
| Parameters | ||
| ---------- | ||
| nwb : nwb object (or nwb-like object) | ||
| nwb object with df_fip and df_trials attributes | ||
| alignment_event : str | ||
| The event column in df_trials to align to. must be given in_session, not in_trial | ||
| offsets : list or tuple of float | ||
| [start, end] offsets (in seconds) relative to alignment_event. | ||
| channel : str | ||
| The value in df_fip['event'] to filter for. | ||
| data_col : str | ||
| Column in df_fip to extract (default 'data_z'). | ||
| output_col : str or None | ||
| Name for the new column. If None, will be generated as | ||
| '<data_col>_<channel>_<start>_<end>_<alignment_event>'. | ||
|
|
||
|
|
||
| Returns | ||
| ------- | ||
| df_trial: pd.DataFrame | ||
| DataFrame with a new column containing the mean signal | ||
| in the specified window for each trial. | ||
| """ | ||
|
|
||
| # Check alignment_event ends with 'in_session' | ||
| if not alignment_event.endswith('in_session'): | ||
| raise ValueError(f"alignment_event '{alignment_event}' must end with 'in_session'.") | ||
|
|
||
| # Check alignment_event is in df_trials columns | ||
| if alignment_event not in nwb.df_trials.columns: | ||
| raise ValueError(f"alignment_event '{alignment_event}' not found in df_trials columns.") | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a check that data_column is in the dataframe
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| # Get output column name | ||
| if output_col is None: | ||
| output_col = f"{data_col}_{channel}_{offsets[0]}_{offsets[1]}_{alignment_event.replace('_in_session','')}" | ||
|
|
||
| df_trials = nwb.df_trials.copy() | ||
|
|
||
| # get event triggered response | ||
| etr = pf.fip_psth_inner_compute(nwb, nwb.df_trials[alignment_event].values, | ||
| channel=channel, average=False, tw=offsets, | ||
| censor=censor, data_column=data_col) | ||
|
|
||
| avg_activity = etr.groupby("event_number").mean() | ||
| avg_activity['trial'] = df_trials.trial.values | ||
| avg_activity = avg_activity.rename(columns={data_col: output_col}) | ||
|
|
||
| # Merge on 'trial' | ||
| df_trials = df_trials.merge(avg_activity[['trial', output_col]], on='trial', how='left') | ||
|
|
||
| return df_trials | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.