-
Notifications
You must be signed in to change notification settings - Fork 0
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
Adding computation and plotting of trial by trial metrics #26
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a90e462
set up of trial metrics
alexpiet 5867ea5
plot updates
alexpiet d742fbc
linting
alexpiet 79b1306
linting
alexpiet bcea8ed
adding ylabel ticks to metrics plots
alexpiet 8f56654
adding choose right rate
alexpiet afbf1b0
plotting baiting
alexpiet 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
61 changes: 61 additions & 0 deletions
61
src/aind_dynamic_foraging_basic_analysis/metrics/trial_metrics.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Tools for computing trial by trial metrics | ||
df_trials = compute_all_trial_metrics(nwb) | ||
|
||
""" | ||
|
||
import numpy as np | ||
|
||
# TODO, we might want to make these parameters metric specific | ||
WIN_DUR = 15 | ||
MIN_EVENTS = 2 | ||
|
||
|
||
def compute_all_trial_metrics(nwb): | ||
""" | ||
Computes all trial by trial metrics | ||
|
||
response_rate, fraction of trials with a response | ||
gocue_reward_rate, fraction of trials with a reward | ||
response_reward_rate, fraction of trials with a reward, | ||
computed only on trials with a response | ||
choose_right_rate, fraction of trials where chose right, | ||
computed only on trials with a response | ||
|
||
""" | ||
if not hasattr(nwb, "df_trials"): | ||
print("You need to compute df_trials: nwb_utils.create_trials_df(nwb)") | ||
return | ||
|
||
df = nwb.df_trials.copy() | ||
|
||
df["RESPONDED"] = [x in [0, 1] for x in df["animal_response"].values] | ||
# Rolling fraction of goCues with a response | ||
df["response_rate"] = ( | ||
df["RESPONDED"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean() | ||
) | ||
|
||
# Rolling fraction of goCues with a response | ||
df["gocue_reward_rate"] = ( | ||
df["earned_reward"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean() | ||
) | ||
|
||
# Rolling fraction of responses with a response | ||
df["RESPONSE_REWARD"] = [ | ||
x[0] if x[1] else np.nan for x in zip(df["earned_reward"], df["RESPONDED"]) | ||
] | ||
df["response_reward_rate"] = ( | ||
df["RESPONSE_REWARD"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean() | ||
) | ||
|
||
# Rolling fraction of choosing right | ||
df["WENT_RIGHT"] = [x if x in [0, 1] else np.nan for x in df["animal_response"]] | ||
df["choose_right_rate"] = ( | ||
df["WENT_RIGHT"].rolling(WIN_DUR, min_periods=MIN_EVENTS, center=True).mean() | ||
) | ||
|
||
# Clean up temp columns | ||
drop_cols = ["RESPONDED", "RESPONSE_REWARD", "WENT_RIGHT"] | ||
df = df.drop(columns=drop_cols) | ||
|
||
return df |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have more trial-based metrics here that can be migrated to this function.
Most of my metrics are not binary variables, so they may not need smoothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tracking them in this issue: #27