Skip to content

About

Code for analysing 2 photon imaging data

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

2PAnalyses

Code for analysing 2 photon imaging data

Processing recordings in the Cylindrical Augmented Reality System (SCAR)

By: Jork de Kok

Last update: 10/04/2025

0. Extract async pulse, run compression and spike sorting with kilosort

For all these different steps use old script from Masa/Diao/Edd. These scripts are accessible here: (link to github repo)

0.1. main_LFP_preprocessing_and_saving.m (VR_NPX_analysis)

Run the first section (LFP -> Catgt) this extracts the async pulse and performs preprocessing steps on the LFP signal.

0.2. Run compression and spike sorting on a Linux computer

See https://github.com/SaleemLab/si_lab for documentation

1. importing packages and loading in data

Fill the mouse, session and so on and get all the quality metrics, ephys, bonsai, SLEAP and metadata.



2. extract and process async pulse

2.1. rename_cols_SLEAP (processing_async)

Usage: rename_cols_SLEAP(SLEAP_df)

Output: none

2.2. rename_cols_bonsai (processing_async)

Usage: rename_cols_bonsai(bonsai_df)

Output: none

2.3) process_async (processing_async)

Converts timestamps to seconds, binairize the async signal and determines timepoints when async went on This function will also plot the signal before binarization and after. The input must be a dataframe with a column called 'Timestamp'. The output of the function is a vector containing when the sync pulse went on in seconds

Usage: x_async_on = process_async(x_df)

Example: SLEAP_async_on = process_async(SLEAP_df)

Output: SLEAP_async_on

2.4) restructure_ephys_async (processing_async)

Converts previously extracted async pulse to a dataframe

Usage: ephys_async_on = restructure_ephys_async(ephys_async_on)

output: ephys_async_on



Section 3: preprocess SLEAP data

3.1) alignment_angle (circular_processing)

determines angle of rotation that is necessary to align with virtual environment based on if orthographic or cubemap view is used

Usage: alingment_angle = find_aligment_angle(session_meta_data)

Output: alingment_angle



3.2) process_trajectory_and_HD (circular_processing)

input are sleap data, the data containing location, alignment angle and a radius (r_lim). This function first plots the trajectory of the head of the animal. then applies radial restriction and plots the same trajectory after that. After this the trajectory is rotated to the align with the virtual environment and the trajectory is plotted again. Lastly, the head direction is calculated and plotted. (contains subfunctions in the same module)

Usage: process_trajectory_and_HD(SLEAP_df, columns_containing_trajectory_data, alignment_angle, r_lim)

Output: none

Section 4: dataframes on ephys time

4.1) align_and_interpolate (align_and_interpolation_functions)

performs crosscorrelation on 2 time vectors (ephys + bonsoi or SLEAP) that contain the time the async pulse went on then it determines the best lag and if one of the timevectors contains more time points, it will be cut to be the same length as the other timevector. After that linear interpolation is applied to the input dataframe based on the ephys time.

Usage: align_and_interpolate(x_async_on, ephys_async_on, x_df)

Example: align_and_interpolate(SLEAP_async_on, ephys_async_on, SLEAP_df)

Output: none

4.2) apply_interpolation_based_on_new_tvec (align_and_interpolation_functions)

creates a new time vector based on the start and end time of the SLEAP_df and bonsai_df and the sampling rate. After that columns in SLEAP_df containing trajectory data (contain X or Z) are linearly interpolated and other columns in SLEAP_df are interpolated based on the nearest value. Then for the bonsai_df the status_of_experiment column is converted from categorical value to numerical followed by interpolation based on the nearest value. After that the status_of_experiment column is converted back to a categorical value. Lastly, spikes are binned based on the new tvec. (contains subfuctions in the same module)

Usage: SLEAP_df, bonsai_df, spikes_df = apply_interpolation_based_on_new_tvec(SLEAP_df,bonsai_df,spikes_raw, sampling_rate)

output: SLEAP_df, bonsai_df, spikes_df

Section 5: processing trajectory and HD

5.1) interpolate_and_smooth (align_and_interpolation_functions)

first plots trajectory of the implantX and Z then it perform interpolation over the columns containing trajectory data. then it plots the trajectory again and then smoothing is applied followed by plotting the trajectory again. Lastly, the new head direction is calculated based on the interpolated and smoothed trajectory. By default the window size for the smoothing is set to 3.

Usage: interpolate_and_smooth(SLEAP_df, columns_containing_trajectory_data,window_size = 3)

Example: SLEAP_df = interpolate_and_smooth(SLEAP_df, columns_containing_trajectory_data)

output: SLEAP_df

5.2) calc_distance_and_speed (binning_spikes)

calculated the distance and speed of trajectory and also for the head direction.

Usage: calc_distance_and_speed(SLEAP_df, sampling_rate)

output: none

5.3) landmark_rotation (circular_processing)

Calculated the global direction by equating the first baseline to 0 degrees. Then plots the trajectory of the implantX and Z of SLEAP_df after which the trajectory is rotated relative to the global direction and then the trajectory is plotted again. After that a histogram of the HD is plotted followed by calculating the rotated head direction based on the landmark rotation. Lastly, the rotated HD is plotted. Bins can adjust the amount of bins in the histogram (contains subfunction in the same module)

Usage: landmark_rotation(SLEAP_df,global_direction, columns_containing_trajectory_data, bins =20)

Example: SLEAP_df, global_direction landmark_rotation(SLEAP_df,global_direction, columns_containing_trajectory_data)

output: SLEAP_df, global_direction

Section 6: binning spikes based on HD

6.1) create_HD_bins (binning_spikes)

creates a new dataframe based which can be based on specific time range (list with start and end time). The new dataframe contains columns that contian the witdh of the HD bins, the midle of bins in rad, middle of the bins in degrees, the occupancy of HD and the rotated (based on landmark) HD. Lastly, it plots the HD occupancy and the rotated HD occupancy.

Usage: create_HD_bins(SLEAP_df, sampling_rate, num_bins=20, time_range=False)

Example: HD_bins = create_HD_bins(SLEAP_df, sampling_rate=1/30, num_bins=20, time_range=[0,900])

output: HD_bins

6.2) apply_spikes_per_HD_bin (binning_spikes)

calculculates spikes per bin which were defined HD_bins dataframe. The binning can be based on a specfic time range. (contains subfuctions)

Usage: apply_spikes_per_HD_bin(HD_bins, SLEAP_df, spikes_df, sampling_rate, num_bins, time_range=False)

Example: binned_spikes_HD = apply_spikes_per_HD_bin(HD_bins, SLEAP_df, spikes_df, sampling_rate=1/30,num_bins=20, time_range=[0,900])

output: binned_spikes_HD

6.3) calc_neuron_characteristics (circular_processing)

calculates max firing, absolute vector lenght, rayleigh vector lenght and preferred firing direction of all neurons.

Usage: calc_neuron_characteristics(binned_spikes_HD,neurons)

Example: neuron_characteristics = calc_neuron_characteristics(binned_spikes_HD, neurons)

output: neuron_characteristics

Section 7: quality metrics

7.1) quality_metrics_thresholds (metrics)

applied threshold on quality metrics and outputs all neurons that adhere to the thresholds.

Usage: quality_metrics_thresholds(quality_metrics)

Example: valid_neurons_ids = metrics.quality_metrics_thresholds(quality_metrics=quality_metrics)

output: valid_neurons_ids

Section 8: plot binned HD spikes and heatmap

About

Code for analysing 2 photon imaging data

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages