juliencarponcy / trialexp

MIT License
2 stars 4 forks source link

Motion correction window #36

Closed kouichi-c-nakamura closed 1 year ago

kouichi-c-nakamura commented 1 year ago

This Pull Request includes:

1. a New Method of Motion Correction

motion_correction_win(photometry_dict: dict) will replace motion_correction(photometry_dict: dict)

  1. The function cut the photometry data (analog_1_filt) into 30 s-long chunks with 50% overlap.
  2. Compute linregress() for each chunk separately, so the regression can follow the slow changes in data
  3. Combine the predicted data for each chunk
    • When two chunks are overlapping, use the average of the two chunks for the same data points
    • When not overlapping (i.e. the first half of the first chunk and the last chunk), just use the data from the one existing chunk
      1. Use this as analog_1_est_motion

2. Compute $\Delta F / F$ for analog_2

Using the same method (simple subtraction of the low-pass filtered data)

3. Compute last_bar_off

def get_last_bar_off_before_first_spout(df_trial):
    # Find the last bar_off before the first spout in trial
    # You can prepare df_trial for debugging/development as below:
    #
    # for i in range(1, np.max(df_event['trial_nb'])):
    #     df_trial = df_event[df_event['trial_nb'] == i]
    # df_trial = the row for event of interest

    bar_off =  df_trial[df_trial['name']=='bar_off']

    spout =  df_trial[df_trial['name']=='spout']
    if spout.shape[0] > 0 and bar_off.shape[0] > 0:
        spout1 = spout.iloc[0]

        filtered_df = bar_off[bar_off['trial_time'] < spout1['trial_time']]
        max_time = filtered_df['trial_time'].max()
        result = filtered_df[filtered_df['trial_time'] == max_time]

        if len(result) >0:
            return result.iloc[0]
juliencarponcy commented 1 year ago

I guess we should approve this PR before the spike_sorting one

kouichi-c-nakamura commented 1 year ago

Yeah, I wanted to do this for Rasha's presentation, but I was a bit too slow!

kouichi-c-nakamura commented 1 year ago

changed accordingly