ProcessRawTifFile. This loads the last tif file for each stim and trims the last tif to have equal number of frames for each channel and across planes. Runs as an isolated function.
Finds root directories and trim tif file
Usage: processRawTifFile('M25012', '20250210')
Output: animalname_session_stim_0000x_trimmed.tif
This will also create an archived folder with two copies of the tif file that was 'opened' and the other copy is untouched.
In Python, 'Data' repo to curate a single plane with the z-motion correction
Get all the session information, pripheral information and metadata
Get all the file paths in the session
Usage: [sessionFileInfo] = get2PsessionFilePaths(animal_name, session_name, stim_list, rerun_process, fileNameAddition)
Example: sessionFileInfo = get2PsessionFilePaths('M24048', '20240816')
Output: M24048_20240816_sessionFileInfo.mat
Get 2P metadata from tif file. This will include the imaging parameters.
Usage: [sessionFileInfo] = get2PMetadata(sessionFileInfo)
Example: sessionFileInfo = get2PMetadata(sessionFileInfo)
Output: M24048_20240816_2PMetaData_StimName.mat (one file for each stim)
Counts the length of tif files across all stim in one recording session
This is needed for the next steps, but it takes a long time to run.
Usage: [sessionFileInfo] = get2PFrameTimes_TwoChannels(sessionFileInfo, nPlanesPreZCorrection, nChannels);
Example: sessionFileInfo = get2PFrameTimes_TwoChannels(sessionFileInfo, 8, 1);
Output: Updates the stim frame run in the sessionFileInfo
Load Suite2P, 2P-FrameTimes (saved in Bonsai), Bonsai and Peripheral Data
Load the peripheral files that are common to all stimuli: Wheel, PD, Quad.
Usage: [sessionFileInfo] = processPeripheralFiles(sessionFileInfo)
Example: sessionFileInfo = processPeripheralFiles(sessionFileInfo)
Output: animalName_session_PeripheralData_stimName.mat
Get the bonsai VR mouse position and trial info tables and saves in a
Usage: [bonsaiData, sessionFileInfo] = getVRBonsaiFiles(sessionFileInfo, VRStimName)
Example: [bonsaiData, sessionFileInfo] = getVRBonsaiFiles(sessionFileInfo, 'M25041_VRCorr_20250413_00001')
Output: bonsaiData.mousePos; bonsaiData.trialInfo; bonsaiData.isVRStim
Suite2P raw and trimmed to match length across planes (if multi)
Usage: [sessionFileInfo] = mergeBonsaiSuite2pFiles(sessionFileInfo)
Example: sessionFileInfo = mergeBonsaiSuite2pFiles(sessionFileInfo)
Output: animalName_session_2pData_stimName.mat
[bonsaiData, sessionFileInfo] = findBonsaiPeripheralLag(sessionFileInfo, method, sampling_rate)
Usage: [bonsaiData, sessionFileInfo] = findBonsaiPeripheralLag(sessionFileInfo, method, samplingRate, StimName)
Example: [bonsaiData, sessionFileInfo] = findBonsaiPeripheralLag(sessionFileInfo, 1, 60)
Outputs: bonsaiData.LagInfo (depending on method used):
.method - Method used to find lag.
.xcorrBestLag - Frames to shift.
.samplingRate - Use as input in this function.
3.2 alignVRBonsaiToPeripheralData.m (Currently lag shifts only ArduinoTime) (vr) / temp include VRStim name
Usage: [bonsaiData, sessionFileInfo] = alignVRBonsaiToPeripheralData(sessionFileInfo,VRStimName,plotFlag)
Example: [bonsaiData, sessionFileInfo] = alignVRBonsaiToPeripheralData(sessionFileInfo, 'M25041_VRCorr_20250413_00001')
Outputs:
- bonsaiData (struct):
- .MousePos / .Quadstate / .TrialInfo:
- .correctedArduinoTime: lag-corrected raw time vector.
- .LagInfo
- .lagShift: Lag shift used (in seconds).
- .lagShift: Lag shift used (in seconds).
- .MousePos / .Quadstate / .TrialInfo:
4.1 resamplAndAlignVR_BonsaiPeripheralSuite2P.m (Resample all data streams to common time base) (vr)
resamplAndAlignVR_BonsaiPeripheralSuite2P.m
Usage: [processedTwoPData, bonsaiData, peripheralData, sessionFileInfo] = resamplAndAlignVR_BonsaiPeripheralSuite2P(sessionFileInfo, samplingRate, mainTimeToUse, VRStimName, plotFlag)
Example: [processedTwoPData, bonsaiData, peripheralData, sessionFileInfo] = resamplAndAlignVR_BonsaiPeripheralSuite2P(sessionFileInfo,60,'TwoPFrameTime', 'M25041_VRCorr_20250413_00001', true)
Outputs: processedTwoPData : struct
Contains resampled F, Fneu, spks, frame times, and ROI metadata.
bonsaiData : struct
Bonsai-tracked signals (e.g., mouse position, trial info, quadstate), all corrected for lag and resampled.
peripheralData : struct
Peripheral signals (e.g., photodiode, wheel), resampled to the same timebase.
Extracts wheel speed, virtual position, and lap-related info from aligned Bonsai and peripheral data during 2P-VR Aman's Classical Corridor. Handles lap classification (completed/aborted) and optionally plots lap timing.
Usage: [response, sessionFileInfo] = extractVRAndPeripheralData(sessionFileInfo, VRStimName, plotFlag)
Example:[response, sessionFileInfo] = extractVRAndPeripheralData(sessionFileInfo, 'M25041_VRCorr_20250413_00001', true)
response. : struct
% Contains key behavioral and timing variables for downstream lap-by-lap analysis:
% - wheelSpeed : real-time wheel speed (cm/s)
% - mouseVirtualPosition : virtual track position (1–140 cm)
% - trackIDFromMousePosition : track IDs inferred from mouse position (usually 1)
% - mouseRecordedPosition : raw position signal (-1141 to -1000)
% - trackIDs : track ID per lap (all 1s in classical VR corridor)
% - lapCount : unified lap index
% - blockIDs : cumulative index of track switches (e.g., block transitions; NA)
% - trialType : task/trial type ID per lap
% (0-NoTask; 1-Passive; 2-Hybrid)
% - completedLaps : indices of completed laps
% - abortedLaps : indices of aborted laps
% - startTimeAll : Bonsai start times for all laps
% - endTimeAll : parsed lap end time (based on position trace)
% - completedStartTimes : start times for completed laps
% - completedEndTimes : end times for completed laps
%
For each ROI, lap, and spatial bin (1 cm), this function finds the corresponding two-photon (2P) frame indices and relative times (from lap start). Only includes frames where wheel speed > 1 cm/s. Both cells and non-cells are included here
Usage: [response, sessionFileInfo] = get2PFrameLapPositionBins(sessionFileInfo, VRStimName)
Example: [response, sessionFileInfo] = get2PFrameLapPositionBins(sessionFileInfo, 'M25041_VRCorr_20250413_00001')
Output: Output:
response : struct (updated)
Adds:
- lapPosition2PFrameIdx{ROI, lap, bin} : 2P frame indices per ROI per lap per bin
- lapPositionRelativeTime{lap, bin} : time relative to lap start (only once per bin) (could exclude)
Extracts mean binned fluorescence/activity values per lap from 2P data. Only includes ROIs labeled as "cells" and only for completed laps. Optionally applies Gaussian temporal smoothing across position bins.
Usage: [response, sessionFileInfo] = getLapPositionActivity(sessionFileInfo, signalField, applySmoothing, VRStimName, onlyIncludeROIs)
Example: [response, sessionFileInfo] = getLapPositionActivity(sessionFileInfo, 'F', false, 'M25041_VRCorr_20250413_00001', true)
Outputs: response : struct (updated)
Adds the following fields:
- lapPositionActivity : [nCells x nLaps x nBins] mean activity per bin
- cellROIs : indices of cell ROIs
- signalUsed : which signal type was used
- smoothingApplied : whether smoothing was applied
plotSortedPopulationResponse(sessionFileInfo, response, applySmoothing)
Code for analysing 2 photon imaging data
get2PsessionFilePaths.m
Usage: [sessionFileInfo] = get2PsessionFilePaths(animal_name, session_name)
Example: sessionFileInfo = get2PsessionFilePaths('M24048', '20240816')
Output: M24048_20240816_sessionFileInfo.mat
get2PMetadata.m
Usage: [sessionFileInfo] = get2PMetadata(sessionFileInfo)
Example: sessionFileInfo = get2PMetadata(sessionFileInfo)
Output: M24048_20240816_2PMetaData_StimName.mat (one file for each stim)
This is needed for the next steps, but it takes a long time to run.
get2PFrameTimes.m
Usage: [sessionFileInfo] = get2PFrameTimes(sessionFileInfo);
Example: sessionFileInfo = get2PFrameTimes(sessionFileInfo);
Output: Updates the stim frame run in the sessionFileInfo
1.4 Load the peripheral files that are common to all stimuli: Wheel, PD, Quad, 2P. Saves as a new .m file for each stimulus
processPeripheralFiles.m
Usage: [sessionFileInfo] = processPeripheralFiles(sessionFileInfo)
Example: sessionFileInfo = processPeripheralFiles(sessionFileInfo)
Output: animalName_session_PeripheralData_stimName.m
2.1 Gets all the times for all planes, also getting the F/fneu/ops/spks/iscell from suite2p for each stimulus;
Trims bonsai time to match the suite2p plane lengths. Saves one .m file for each stimulus.
mergeBonsaiSuite2pFiles.m Usage: [sessionFileInfo] = mergeBonsaiSuite2pFiles(sessionFileInfo) Example: sessionFileInfo = mergeBonsaiSuite2pFiles(sessionFileInfo) Output: animalName_session_2pData_stimName.mat
2.1.1 Uses this function: [plane_data, plane_data_new] = get_bonsai_twopframetimes_by_planes(filepath, nplanes) to get the planetimes
getVRBonsaiFiles.m Usage: VRInfo = getVRBonsaiFiles(sessionFileInfo) Output: VRInfo.mousePos; VRInfo.trialInfo; VRInfo.isVRStim
For VR - interpolation is the first step