This commit is contained in:
Karthik 2025-08-27 21:30:03 +02:00
parent 4971e488c4
commit 90f7d2b520
2 changed files with 101 additions and 21 deletions

View File

@ -46,8 +46,11 @@ function [od_imgs, scan_parameter_values, file_list] = collectODImages(options)
end
end
% --- Determine RawData folder ---
raw_data_folder = fullfile(options.saveDirectory,'RawData');
% --- General path to raw data folders ---
raw_data_folders = dir(fullfile(options.saveDirectory,'RawData_*'));
% --- Specific sequence, data and run ---
dataSource = makeDataSourceStruct(options.folderPath);
% --- Check if workspace full dataset exists ---
fullDataExists = evalin('base', 'exist(''full_od_imgs'', ''var'')') && ...
@ -56,9 +59,11 @@ function [od_imgs, scan_parameter_values, file_list] = collectODImages(options)
evalin('base', 'exist(''raw_file_list'', ''var'')') && ...
evalin('base', 'exist(''prior_options'',''var'')');
if ~isfield(options,'SAVE_TO_WORKSPACE') || ~options.SAVE_TO_WORKSPACE
fullDataExists = false;
if ~isfield(options,'SAVE_TO_WORKSPACE')
[options.SAVE_TO_WORKSPACE, ~] = Helper.estimateDatasetMemory(dataSource, options);
end
raw_data_folder = [];
% --- Prepare full_od_imgs, full_bkg_imgs, scan values, file list ---
if fullDataExists
@ -67,28 +72,57 @@ function [od_imgs, scan_parameter_values, file_list] = collectODImages(options)
full_bkg_imgs = evalin('base', 'full_bkg_imgs');
raw_scan_parameter_values = evalin('base', 'raw_scan_parameter_values');
raw_file_list = evalin('base', 'raw_file_list');
nFiles = size(full_od_imgs,3);
nFiles = size(full_od_imgs,3);
fprintf('\n[INFO] Cropping and subtracting background from images...\n');
elseif exist(raw_data_folder,'dir')
mat_files = dir(fullfile(raw_data_folder,'*.mat'));
nFiles = numel(mat_files);
elseif ~options.SAVE_TO_WORKSPACE && ~isempty(raw_data_folders)
matched = false;
for r = 1:numel(raw_data_folders)
metaPath = fullfile(raw_data_folders(r).folder, raw_data_folders(r).name, 'metadata.mat');
if ~isfile(metaPath), continue; end
S = load(metaPath,'metadata');
if isequaln(S.metadata.options, options)
fprintf('\n[INFO] Found matching raw data folder: %s\n', raw_data_folders(r).name);
raw_data_folder = fullfile(raw_data_folders(r).folder, raw_data_folders(r).name);
matched = true;
break;
end
end
if ~matched
fprintf('\n[INFO] No matching raw data folder for this run found.\n');
[~, ~, ~, ~] = Helper.processRawData(options);
fprintf('\n[INFO] Completed computing OD images. Images will be stored on disk for reuse.\n');
end
mat_files = dir(fullfile(raw_data_folder,'*.mat'));
mat_files = mat_files(~strcmp({mat_files.name}, 'metadata.mat')); % Exclude metadata.mat
nFiles = numel(mat_files);
raw_scan_parameter_values = zeros(1,nFiles);
raw_file_list = strings(1,nFiles);
raw_file_list = strings(1,nFiles);
fprintf('\n[INFO] Cropping and subtracting background from images in raw data folder on disk...\n');
else
fprintf('\n[INFO] No raw data found in either the workspace or on disk.\n');
[full_od_imgs, full_bkg_imgs, raw_scan_parameter_values, raw_file_list] = Helper.processRawData(options);
if isfield(options,'SAVE_TO_WORKSPACE') && options.SAVE_TO_WORKSPACE
if options.SAVE_TO_WORKSPACE
nFiles = size(full_od_imgs,3);
assignin('base', 'full_od_imgs', full_od_imgs);
assignin('base', 'full_bkg_imgs', full_bkg_imgs);
assignin('base', 'raw_scan_parameter_values', raw_scan_parameter_values);
assignin('base', 'raw_file_list', raw_file_list);
fprintf('\n[INFO] Completed computing OD images. Images will be stored in workspace for reuse.\n');
fprintf('\n[INFO] Completed computing OD images. Images stored in workspace for reuse.\n');
fprintf('\n[INFO] Cropping and subtracting background from images...\n');
else
fprintf('\n[INFO] Completed computing OD images. Images will be stored on disk for reuse.\n');
fprintf('\n[INFO] Completed computing OD images. Images stored on disk for reuse.\n');
runID = sprintf('%s_%s_Run%04d', ...
dataSource{1}.sequence, ...
strrep(dataSource{1}.date,'/','-'), ...
dataSource{1}.runs);
raw_data_folder = fullfile(options.saveDirectory, ['RawData_' runID]);
mat_files = dir(fullfile(raw_data_folder,'*.mat'));
mat_files = mat_files(~strcmp({mat_files.name}, 'metadata.mat')); % Exclude metadata.mat
nFiles = numel(mat_files);
raw_scan_parameter_values = zeros(1,nFiles);
raw_file_list = strings(1,nFiles);
fprintf('\n[INFO] Cropping and subtracting background from images in raw data folder on disk...\n');
end
nFiles = size(full_od_imgs,3);
fprintf('\n[INFO] Cropping and subtracting background from images...\n');
end
% --- Unified cropping & background subtraction ---
@ -102,7 +136,7 @@ function [od_imgs, scan_parameter_values, file_list] = collectODImages(options)
end
for k = 1:nFiles
if fullDataExists || exist(raw_data_folder,'dir')
if fullDataExists || ~isempty(raw_data_folder)
if fullDataExists
od_mat = full_od_imgs(:,:,k);
bkg_mat = full_bkg_imgs(:,:,k);
@ -254,7 +288,7 @@ function saveODFigures(od_imgs, saveDirectory)
fprintf('[INFO] OD figures saved successfully.\n');
end
function dataSources = makeDataSource(folderPath)
function dataSource = makeDataSourceStruct(folderPath)
% Split by file separators (handles / or \)
parts = regexp(folderPath, '[\\/]', 'split');
@ -277,7 +311,7 @@ function dataSources = makeDataSource(folderPath)
runNum = str2double(runStr);
% Construct struct inside a cell array
dataSources = {
dataSource = {
struct('sequence', sequence, ...
'date', dateStr, ...
'runs', runNum)

View File

@ -27,16 +27,31 @@ function [full_od_imgs, full_bkg_imgs, raw_scan_parameter_values, raw_file_list]
raw_scan_parameter_values = zeros(1, nFiles);
raw_file_list = string(zeros(1,nFiles)); % always string array
if options.SAVE_TO_WORKSPACE
fprintf('\n[INFO] Creating in-memory arrays of raw data...\n');
full_od_imgs = nan(ny, nx, nFiles, 'single');
full_bkg_imgs = nan(ny, nx, nFiles, 'single');
else
fprintf('\n[INFO] Creating folder of raw data on disk (per-image MAT files)...\n');
rawFolder = fullfile(options.saveDirectory, 'RawData');
% --- Create uniquely identified RawData folder ---
dataSource = makeDataSourceStruct(options.folderPath);
runID = sprintf('%s_%s_Run%04d', ...
dataSource{1}.sequence, ...
strrep(dataSource{1}.date,'/','-'), ...
dataSource{1}.runs);
rawFolder = fullfile(options.saveDirectory, ['RawData_' runID]);
if ~exist(rawFolder,'dir'), mkdir(rawFolder); end
fprintf('\n[INFO] Creating folder of raw data on disk: %s\n', rawFolder);
% --- Save metadata for this run ---
metadata.options = options;
metadata.timestamp = datetime; % still record analysis time
metadata.runID = runID; % traceable to experiment run
metadata.imageSize = [ny, nx];
metadata.fileList = string(arrayfun(@(f) fullfile(f.folder, f.name), files, 'UniformOutput', false));
save(fullfile(rawFolder,'metadata.mat'),'metadata','-v7.3');
full_od_imgs = rawFolder;
full_bkg_imgs = rawFolder;
end
@ -92,6 +107,7 @@ function [full_od_imgs, full_bkg_imgs, raw_scan_parameter_values, raw_file_list]
end
end
%% --- Local helper functions ---
function [od_img, bkg_img, val] = readAndComputeOD(fullFileName, options, groupList, ny, nx)
try
atm_img = double(imrotate(h5read(fullFileName, append(groupList(options.cam), "/atoms")), options.angle, 'bilinear', 'crop'));
@ -130,3 +146,33 @@ function writeRawImagesToDisk(rawFolder, od_img, bkg_img, scan_val, file_name, i
File = string(file_name);
save(matFilePath, 'OD','BKG','Scan','File','-v7.3');
end
function dataSource = makeDataSourceStruct(folderPath)
% Split by file separators (handles / or \)
parts = regexp(folderPath, '[\\/]', 'split');
% Remove empty parts caused by leading slashes
parts = parts(~cellfun('isempty', parts));
% Extract sequence, date, and run number
% Now the indices are correct:
% parts = {'DyLabNAS', 'Data', 'StructuralPhaseTransition', '2025', '08', '13', '0062'}
sequence = parts{3}; % "StructuralPhaseTransition"
year = parts{4}; % "2025"
month = parts{5}; % "08"
day = parts{6}; % "13"
runStr = parts{7}; % "0062"
% Build date string
dateStr = sprintf('%s/%s/%s', year, month, day);
% Convert run string to number
runNum = str2double(runStr);
% Construct struct inside a cell array
dataSource = {
struct('sequence', sequence, ...
'date', dateStr, ...
'runs', runNum)
};
end