Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add outlier functions to bypass matlab's isoutlier; adapt to r2017b #263

Merged
merged 9 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified tICA/scripts/Compiled_ComputeTICAFeatures/ComputeTICAFeatures
Binary file not shown.
12 changes: 4 additions & 8 deletions tICA/scripts/ComputeTICAFeatures.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
%Find strongest individual subjects for components
[~, I]=max(TCSVARS);
for i=1:size(TCSVARS,2)
%if ~isfile([OutputFolder '/tICA' num2str(i,'%02.f') '_SS.sdseries.nii']) && ~isfile([OutputFolder '/All' num2str(i,'%02.f') '_SS.sdseries.nii'])
if ~isfile([OutputFolder '/tICA' num2str(i,'%02.f') '_SS.sdseries.nii']) && ~isfile([OutputFolder '/All' num2str(i,'%02.f') '_SS.sdseries.nii'])
%Subjlist{I(i)}
SubjFolderlist=[StudyFolder '/' Subjlist{I(i)}];
tICAMaps_sub=ciftiopen([SubjFolderlist '/MNINonLinear/fsaverage_LR' LowResMesh 'k/' Subjlist{I(i)} '.' tICAFeaturesProcString '_SR' RegString '.' LowResMesh 'k_fs_LR.dscalar.nii'],'wb_command');
Expand Down Expand Up @@ -355,14 +355,10 @@
temp=tICAtcs_SS;
temp.cdata=zeros(size(rtICASingleComponentSpaceXTime,1),size(tICAtcs_SS.cdata,2),'single');
temp.cdata(:,1:size(rtICASingleComponentSpaceXTime,2))=squeeze(rtICASingleComponentSpaceXTime(:,:,i))./repmat(std(sum(rtICASingleComponentSpaceXTime,3),[],2),1,size(rtICASingleComponentSpaceXTime,2));
if ~isfile([OutputFolder '/tICA' num2str(i,'%02.f') '_SS.sdseries.nii'])
ciftisavereset(temp,[OutputFolder '/tICA' num2str(i,'%02.f') '_SS.sdseries.nii'],'wb_command');
end
ciftisavereset(temp,[OutputFolder '/tICA' num2str(i,'%02.f') '_SS.sdseries.nii'],'wb_command');
temp.cdata(:,1:size(rtICASingleComponentSpaceXTime,2))=squeeze(sum(rtICASingleComponentSpaceXTime(:,:,:),3))./repmat(std(sum(rtICASingleComponentSpaceXTime,3),[],2),1,size(rtICASingleComponentSpaceXTime,2));
if ~isfile([OutputFolder '/All' num2str(i,'%02.f') '_SS.sdseries.nii'])
ciftisavereset(temp,[OutputFolder '/All' num2str(i,'%02.f') '_SS.sdseries.nii'],'wb_command');
end
%end
ciftisavereset(temp,[OutputFolder '/All' num2str(i,'%02.f') '_SS.sdseries.nii'],'wb_command');
end
end
if ~isfile([OutputFolder '/tICA_Maps_' num2str(tICAdim) '_' nonlinear '_SS.dscalar.nii'])
ciftisave(tICAMaps_SS,[OutputFolder '/tICA_Maps_' num2str(tICAdim) '_tanhF_SS.dscalar.nii'],wbcommand);
Expand Down
12 changes: 12 additions & 0 deletions tICA/scripts/feature_helpers/findFivePercentageOutliers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function ret = findFivePercentageOutliers(flatdata)
%isoutlier(..., 'percentiles', [5 95]) written to treat all values at once
numelems = numel(flatdata);
ret = false(size(flatdata));
if numelems < 1
return
end
numexclude = floor((numelems + 9) / 20); %in R2021a, isoutlier excludes 0 for 10, 2 for 11...
[~, perm] = sort(flatdata(:));
ret([1:numexclude (end - numexclude + 1):end]) = true;
ret(perm) = ret; %un-sort the exclusion mask
end
7 changes: 7 additions & 0 deletions tICA/scripts/feature_helpers/findOutliers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function ret = findOutliers(data)
% must be 1d array
% outlier detection: identify data that do not pass the MAD-based test, while ensuring that it never detects more than 5% of the highest or lowest values.
madOutliers=findScaledMADOutliers(data);
perOutliers=findFivePercentageOutliers(data);
ret=and(madOutliers, perOutliers);
end
15 changes: 15 additions & 0 deletions tICA/scripts/feature_helpers/findScaledMADOutliers.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function ret = findScaledMADOutliers(data)
% Determine if an array contains values that are outside of three scaled MAD
numElems = numel(data);
if numElems < 1
return
end
deviations = data - median(data);
absDeviations = abs(deviations);
mad = median(absDeviations);
% Scale the MAD by -1/(sqrt(2)*erfcinv(3/2))
% from https://www.mathworks.com/help/matlab/ref/isoutlier.html#bvolffm
scaledMAD = 1.4826 * mad;
threshold = 3 * scaledMAD;
ret = absDeviations > threshold;
end
16 changes: 1 addition & 15 deletions tICA/scripts/feature_helpers/std_outlier.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if any(isnan(data_reshape))
error('found NaN in std_outlier');
end
isoutlier_stat = myoutliers(data_reshape); %TODO: test me
isoutlier_stat = findOutliers(data_reshape); %TODO: test me
if isempty(isoutlier_stat)
%outlier_stat=1; %original
outlier_stat=0;
Expand All @@ -18,17 +18,3 @@
outlier_stat=1-std_exclude_outlier/std_all;
end
end

%isoutlier(..., 'percentiles', [5 95]) written to treat all values at once
function ret = myoutliers(flatdata)
numelems = numel(flatdata);
ret = false(size(flatdata));
if numelems < 1
return
end
numexclude = floor((numelems + 9) / 20); %in R2021a, isoutlier excludes 0 for 10, 2 for 11...
[~, perm] = sort(flatdata(:));
ret([1:numexclude (end - numexclude + 1):end]) = true;
ret(perm) = ret; %un-sort the exclusion mask
end

3 changes: 2 additions & 1 deletion tICA/scripts/feature_helpers/sum_outlier.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function output = sum_outlier(T)
%Afill = filloutliers(T,'spline');
%Afill = filloutliers(T,'next');
Afill = filloutliers(T,'spline','percentiles', [5,95]);
outliers=findOutliers(T);

Afill = filloutliers(T,'spline','OutlierLocations', outliers);
Afill(find(isnan(Afill)))=T(find(isnan(Afill)));
% original
output = sqrt(sum((T-Afill).^2));
Expand Down