From c610c6368652bd722ca0498a45929385c4040d83 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Mar 2026 21:39:14 +0000 Subject: [PATCH 1/2] Initial plan From cd280439075e69efc24222b100f7cab01d6d955f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Mar 2026 22:00:08 +0000 Subject: [PATCH 2/2] Fix write function performance for filenames with underscores - Replace str2num with str2double in make_img_filename to avoid eval-based path searches that are extremely slow on clusters with large MATLAB paths. str2double handles non-numeric strings by returning NaN (fast) instead of triggering a full MATLAB function path search via eval. - Fix warning('off') to use warning('off', 'all') with proper save/restore state pattern, fixing deprecated usage in MATLAB R2020a+. - Fix O(N^2) char concatenation loop in slice write path to use cell array pre-allocation (O(N)) instead of repeated char() matrix building. Fixes slow write for filenames like TYPEB_FITHRF.nii where str2num called with the identifier would trigger expensive MATLAB path lookups. Co-authored-by: torwager <6262700+torwager@users.noreply.github.com> --- .../iimg_reconstruct_3dvol.m | 9 ++++++--- .../iimg_reconstruct_vols.m | 15 +++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CanlabCore/Index_image_manip_tools/iimg_reconstruct_3dvol.m b/CanlabCore/Index_image_manip_tools/iimg_reconstruct_3dvol.m index c65261f2..58a83c66 100644 --- a/CanlabCore/Index_image_manip_tools/iimg_reconstruct_3dvol.m +++ b/CanlabCore/Index_image_manip_tools/iimg_reconstruct_3dvol.m @@ -129,9 +129,9 @@ function write_file(voldata, volInfo, outname, descrip, varargin) % correct .private loaded from file by re-mapping volume. spm_write_plane(spm_vol(volInfo.fname), voldata, slice_number); else - warning('off'); % empty images return many warnings + warnstate = warning('off', 'all'); % empty images return many warnings spm_write_vol(volInfo, voldata); - warning('on'); + warning(warnstate); end end @@ -149,7 +149,10 @@ function write_file(voldata, volInfo, outname, descrip, varargin) t = t(1); n1 = ext((t+1):end); if ~isempty(n1), - n = str2num(n1); + n = str2double(n1); + if isnan(n) + n = 1; % Fall back to 1 for non-numeric values + end ext = ext(1:(t-1)); end end diff --git a/CanlabCore/Index_image_manip_tools/iimg_reconstruct_vols.m b/CanlabCore/Index_image_manip_tools/iimg_reconstruct_vols.m index 3e77c855..3d5770b0 100644 --- a/CanlabCore/Index_image_manip_tools/iimg_reconstruct_vols.m +++ b/CanlabCore/Index_image_manip_tools/iimg_reconstruct_vols.m @@ -232,18 +232,18 @@ function write_file(voldata, volInfo, outname, descrip, varargin) % write one slice for a series of images slice_number = varargin{1}; - filenames = []; + filenames_cell = cell(nimgs, 1); for i = 1:nimgs - filenames = char(filenames, make_img_filename(outname, i)); + filenames_cell{i} = make_img_filename(outname, i); end - filenames = filenames(2:end, :); + filenames = char(filenames_cell); % Note: pinfo is set to [1 0 0] for new images; no rescaling of % images. scn_write_plane(filenames, voldata, slice_number, volInfo); else - warning('off'); % empty images return many warnings + warnstate = warning('off', 'all'); % empty images return many warnings for i = 1:nimgs [volInfo.fname, volInfo.n(1)] = make_img_filename(outname, i); volInfo.descrip = descrip; @@ -254,7 +254,7 @@ function write_file(voldata, volInfo, outname, descrip, varargin) spm_write_vol(volInfo, squeeze(voldata(:, :, :, i))); end - warning('on'); + warning(warnstate); end end @@ -278,7 +278,10 @@ function write_file(voldata, volInfo, outname, descrip, varargin) t = t(1); n1 = ext((t+1):end); if ~isempty(n1), - n = str2num(n1); + n = str2double(n1); + if isnan(n) + n = imagenum; % Fall back to imagenum for non-numeric values + end ext = ext(1:(t-1)); end else