From 33da2f518f3b59166e121fc824fff348ff9e05bc Mon Sep 17 00:00:00 2001 From: weiglszonja Date: Sun, 1 Dec 2024 17:41:17 +0100 Subject: [PATCH 1/3] add notes --- .../schierek_embargo_2024_notes.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md b/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md index bcfacf2..41c8b79 100644 --- a/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md +++ b/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md @@ -31,6 +31,71 @@ The "SU" struct is a cell array of all individual cells simultaneously recorded - `AP` – anterior/posterior neuropixels probe location relative to Bregma - `ML` – medial/lateral neuropixels probe location relative to Bregma +#### MATLAB `SU` `location` field converter + +The "location" field in some of the processed ephys data files (e.g. `E003_2022-08-01.mat`) when reading in Python shows up as a `MatlabOpaque` object. +To make the location field readable in Python, please use the `schierek_embargo_2024/mat_utils/convertSULocationToString.m` utility script to convert the location field to a string. + +This script processes .mat files containing the location field within `SU` structures to ensure compatibility with Python. +The script: +1) Recursively searches through a specified directory for .mat files, +2) Loads each file and processes the 'SU' struct if present (skipping files without 'SU' struct) +3) Converts location fields from MATLAB data types (cell arrays, strings) to character arrays to ensure compatibility with Python +4) Preserves the original 'S' structure while saving the modified data ('SU' and 'S') back to the .mat file + +```matlab +% MATLAB script to process all .mat files in a specified directory, +% checking for 'SU' structure and converting 'location' to a plain string if it exists + +% Specify the path to your directory containing the .mat files +folderPath = '/Volumes/T9/Constantinople/Ephys Data/'; % Replace with your actual folder path + +% Get a list of all .mat files in the directory +files = dir(fullfile(folderPath, '**', '*.mat')); +matFiles = fullfile({files.folder}, {files.name}); + +% Loop through each .mat file in the directory +for k = 1:length(matFiles) + matFilePath = fullfile(folderPath, matFiles(k)); + matFilePath = char(matFiles{k}); % Ensure it is a character array + % Load the .mat file + try + data = load(matFilePath); + catch ME + % Handle the error + disp(['An error occurred: ', ME.message]); + continue + end + + % Check if 'SU' structure exists in the loaded data + if isfield(data, 'SU') + SU = data.SU; % Get the SU structure + numUnits = length(SU); + + % Iterate over each unit in the SU structure + for i = 1:numUnits + if isfield(SU{i}, 'location') + % Check and convert location if it is a cell array + if iscell(SU{i}.location) && ~isempty(SU{i}.location) + locationStr = SU{i}.location{1}; % Extract first element if it is a cell + else + locationStr = SU{i}.location; + end + SU{i}.location = locationStr; + end + end + + S = data.S; + save(matFilePath, 'S', 'SU'); + disp(['Processed and saved: ', matFilePath]); + % Clear variables to free up workspace + clear SU; + clear S; + clear data; + end +end +``` + ### Processed behavior data The processed behavior data is stored in custom .mat files (e.g. `J076_2023-12-12.mat`) with the following fields: From 94f7c715012c6b9bcdc6ed5911dbfa2ab9cedbd3 Mon Sep 17 00:00:00 2001 From: weiglszonja Date: Sun, 1 Dec 2024 17:41:36 +0100 Subject: [PATCH 2/3] Add MATLAB script to convert SU locations to strings This script processes .mat files, checking for the 'SU' structure and converting 'location' fields to plain strings if they exist. It iterates through each file in the specified directory, handling errors, and saves the updated data. --- .../mat_utils/convertSULocationToString.m | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/constantinople_lab_to_nwb/schierek_embargo_2024/mat_utils/convertSULocationToString.m diff --git a/src/constantinople_lab_to_nwb/schierek_embargo_2024/mat_utils/convertSULocationToString.m b/src/constantinople_lab_to_nwb/schierek_embargo_2024/mat_utils/convertSULocationToString.m new file mode 100644 index 0000000..9459843 --- /dev/null +++ b/src/constantinople_lab_to_nwb/schierek_embargo_2024/mat_utils/convertSULocationToString.m @@ -0,0 +1,50 @@ +% MATLAB script to process all .mat files in a specified directory, +% checking for 'SU' structure and converting 'location' to a plain string if it exists + +% Specify the path to your directory containing the .mat files +folderPath = '/Volumes/T9/Constantinople/Ephys Data/'; % Replace with your actual folder path + +% Get a list of all .mat files in the directory +files = dir(fullfile(folderPath, '**', '*.mat')); +matFiles = fullfile({files.folder}, {files.name}); + +% Loop through each .mat file in the directory +for k = 1:length(matFiles) + matFilePath = fullfile(folderPath, matFiles(k)); + matFilePath = char(matFiles{k}); % Ensure it is a character array + % Load the .mat file + try + data = load(matFilePath); + catch ME + % Handle the error + disp(['An error occurred: ', ME.message]); + continue + end + + % Check if 'SU' structure exists in the loaded data + if isfield(data, 'SU') + SU = data.SU; % Get the SU structure + numUnits = length(SU); + + % Iterate over each unit in the SU structure + for i = 1:numUnits + if isfield(SU{i}, 'location') + % Check and convert location if it is a cell array + if iscell(SU{i}.location) && ~isempty(SU{i}.location) + locationStr = SU{i}.location{1}; % Extract first element if it is a cell + else + locationStr = SU{i}.location; + end + SU{i}.location = locationStr; + end + end + + S = data.S; + save(matFilePath, 'S', 'SU'); + disp(['Processed and saved: ', matFilePath]); + % Clear variables to free up workspace + clear SU; + clear S; + clear data; + end +end From db50bc448612abdd4c6907f9ab6fadb21ac2b718 Mon Sep 17 00:00:00 2001 From: weiglszonja Date: Sun, 1 Dec 2024 17:47:05 +0100 Subject: [PATCH 3/3] extend note --- .../schierek_embargo_2024/schierek_embargo_2024_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md b/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md index 41c8b79..b643329 100644 --- a/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md +++ b/src/constantinople_lab_to_nwb/schierek_embargo_2024/schierek_embargo_2024_notes.md @@ -43,6 +43,8 @@ The script: 3) Converts location fields from MATLAB data types (cell arrays, strings) to character arrays to ensure compatibility with Python 4) Preserves the original 'S' structure while saving the modified data ('SU' and 'S') back to the .mat file +Run this script in MATLAB to process all ephys .mat data files **before** converting to NWB. + ```matlab % MATLAB script to process all .mat files in a specified directory, % checking for 'SU' structure and converting 'location' to a plain string if it exists