Skip to content

Updating FIP PSTH tools to accept multiple NWB files#71

Merged
alexpiet merged 8 commits into
mainfrom
alex_multiple
Aug 20, 2025
Merged

Updating FIP PSTH tools to accept multiple NWB files#71
alexpiet merged 8 commits into
mainfrom
alex_multiple

Conversation

@alexpiet

@alexpiet alexpiet commented Aug 19, 2025

Copy link
Copy Markdown
Collaborator

Updates the tools in plot.plot_fip to operate on either a single NWB or a list of NWB files. This is a re-write and thus replaces #70. This also closes #48 (but does not Z-score each session as proposed in that issue)

Examples:
This original example still works as expected:

from aind_dynamic_foraging_basic_analysis.plot import plot_fip as pf
channel = 'G_1_dff-poly'
rewarded_go_cues = nwb.df_trials.query('earned_reward == 1')['goCue_start_time_in_session'].values
unrewarded_go_cues = nwb.df_trials.query('earned_reward == 0')['goCue_start_time_in_session'].values
pf.plot_fip_psth_compare_alignments(
    nwb, 
    {'rewarded goCue':rewarded_go_cues,'unrewarded goCue':unrewarded_go_cues}, 
    channel, 
    censor=True
    )

But now we can analyze and combine multiple sessions

alignments = []
for nwb in nwb_list:
    rewarded_go_cues = nwb.df_trials.query('earned_reward == 1')['goCue_start_time_in_session'].values
    unrewarded_go_cues = nwb.df_trials.query('earned_reward == 0')['goCue_start_time_in_session'].values
    alignments.append( {'rewarded goCue':rewarded_go_cues,'unrewarded goCue':unrewarded_go_cues})
pf.plot_fip_psth_compare_alignments(
    nwb_list, 
    alignments, 
    channel, 
    censor=True,
    error_type='sem'
    )

We can also use columns names in df_events:

pf.plot_fip_psth_compare_alignments(
    nwb_list, 
    ['goCue_start_time'], 
    channel, 
    censor=True,
    error_type='sem'
    )

The error_type keyword lets the user select between:

  • sem the standard error of the mean over all samples
  • sem_over_sessions the standard error of the mean over sessions.

Comment thread src/aind_dynamic_foraging_basic_analysis/plot/plot_fip.py
Comment thread src/aind_dynamic_foraging_basic_analysis/plot/plot_fip.py
Compare the same FIP channel aligned to multiple event types
nwb, nwb object for the session
nwb, nwb object for the session, or a list of nwbs
alignments, either a list of event types in df_events, or a dictionary

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

documentation needs to be updated

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe the comment wasn't at the correct point-- the update should be in alignments-- this was the info i wanted here:

if single nwb - can pass list, or dictionary
if list of nwbs - can pass a single list, or list of dictionaries

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread src/aind_dynamic_foraging_basic_analysis/plot/plot_fip.py
Comment thread src/aind_dynamic_foraging_basic_analysis/plot/plot_fip.py
********************
plot_fip_psth(nwb, 'goCue_start_time')
plot_fip_psth(nwb_list, 'goCue_start_time')
plot_fip_psth(nwb_list, ['goCue_start_time','goCue_start_time'])

@rachelstephlee rachelstephlee Aug 20, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

for compare_alignment, we should not allow for different alignment names for mutliple NWBs. this makes for confusing plots. the x axis is showing one label when there were two alignments given.

image

we rarely have the same event named differently in an NWB.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

agreed. fixed

@rachelstephlee

rachelstephlee commented Aug 20, 2025

Copy link
Copy Markdown
Collaborator

Looks good. Thank you for the refactor.

Some minor things--some checks, some functionality differences etc. I really think we shouldn't allow for different alignments for different NWBs in compare_channels. It'll make things confusing.

align = [align]

# First check that each session has an events table and fip table
for nwb_i in nwb_list:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

missing check for channel names in all NWBs:
channel_exists = all(c in nwb_i.df_fip["event"].values for nwb_i in nwb_to_check)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I check for missing channels below when computing the ETR:
include = [c in nwb.df_fip["event"].values for nwb in nwb_list]
If a channel is missing for a session, that session is skipped just for that channel.

I'll add a warning though if this happens

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks great thanks.

Compare the same FIP channel aligned to multiple event types
nwb, nwb object for the session
nwb, nwb object for the session, or a list of nwbs
alignments, either a list of event types in df_events, or a dictionary

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe the comment wasn't at the correct point-- the update should be in alignments-- this was the info i wanted here:

if single nwb - can pass list, or dictionary
if list of nwbs - can pass a single list, or list of dictionaries

Comment thread src/aind_dynamic_foraging_basic_analysis/plot/plot_fip.py

@rachelstephlee rachelstephlee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

sorry it took a bit-- this looks good. thank you!

@alexpiet alexpiet merged commit 314d311 into main Aug 20, 2025
3 checks passed
@alexpiet alexpiet deleted the alex_multiple branch August 20, 2025 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How do we plot PSTHs pooled across multiple sessions?

2 participants