AllenInstitute / mindscope_utilities

Utilities for loading, manipulating and visualizing data from the Allen Institute Mindscope program
Other
2 stars 1 forks source link

event triggered response modifies the dataframe #14

Closed alexpiet closed 3 years ago

alexpiet commented 3 years ago

One thing I've noticed is the event_triggered_response function in mindscope utilities appears to change the dataframes you pass in

For example, if I make an event triggered response for the running_df, then it moves a column "timestamps" to the index

so if I compute a second etr, it throws an error

pupil_df = experiments[ophys_experiment_ids[0]].eye_tracking.reset_index()
pupil_df.head()

def make_event_triggered_plot(df, x, y, event_query, stimulus_table, ax, t_before=3, t_after=3):
    # Build event triggered response
    etr = mindscope_utilities.event_triggered_response(
        data = df,
        t = 'timestamps',
        y = y,
        event_times = stimulus_table.query(event_query)['start_time'],
        t_before=t_before,
        t_after=t_before,
        output_sampling_rate = 50,
    )
    # Plot event triggered response
    sns.lineplot(
        data=etr,
        x=x,
        y=y,
        n_boot=500,
        ax=ax
    )

fig, ax = plt.subplots()
make_event_triggered_plot(
    df = pupil_df,
    x = 'time',
    y = 'pupil_area',
    stimulus_table = stimulus_table,
    event_query = 'omitted',
    ax=ax
)
fig, ax = plt.subplots()
make_event_triggered_plot(
    df = pupil_df,
    x = 'time',
    y = 'pupil_area',
    stimulus_table = stimulus_table,
    event_query = 'omitted',
    ax=ax
)

that second make_event_triggered_plot throws an error

alexpiet commented 3 years ago

@dougollerenshaw thinks its this line, which changes the index "in place"

https://github.com/AllenInstitute/mindscope_utilities/blob/1814df81189677abed7c0116c5a5b37f5c617d8b/mindscope_utilities/general_utilities.py#L117

dougollerenshaw commented 3 years ago

Thanks @alexpiet. I think the solution is to simply make a copy of the dataframe in the function before operating on it. We could set the function signature to be:

def event_triggered_response(input_data, ...

Then

data = input_data.copy()
dougollerenshaw commented 3 years ago

Resolved in PR #17