DissectionPhotoVolumes / data /code /Stats /UWphoto_script_extractStats.m
introvoyz041's picture
Migrated from GitHub
72417f2 verified
Raw
History Blame Contribute Delete
8.53 kB
% Script to load in the volume statistics generated by Samseg during the
% segmentation process.
%% list cases to exclude
% no mathcing, no matching, deformed image
% removecases = {'18-2102','18-2127','18-1343','18-2260','19-0019','19-0138'};
removecases = {'18-2102',... % missing volumes
'18-2127',...% missing volumes
'18-2260',...% hemorrhage
'19-0019',...% missing volumes & hemorrhage
'18-1705',...% poor segmentation ventricles
'18-1754',...% poor segmentation ventricles
'18-1343',...% poor segmentation deformation
'19-0138',...% poor segmentation cortex
'19-0100'... % very large ventricles (biasing)
};
removaltype = [1,1,1,1,2,2,3,3,4];
removalnotes = {'missing volumes',...
'missing volumes',...
'hemorrhage',...
'missing volumes & hemorrhage',...
'poor segmentation ventricles',...
'poor segmentation ventricles',...
'poor segmentation deformation',...
'poor segmentation cortex',...
'very large ventricles (biasing)'
};
%% setup directories
PHOTO_RECON_HOME=getenv('PHOTO_RECON_HOME');
% segmentation directories
MRIsDir = fullfile(PHOTO_RECON_HOME,'FLAIR_Scan_Data','SAMSEG');
hardDir = fullfile(PHOTO_RECON_HOME,'Results_hard','SAMSEG');
softDir = fullfile(PHOTO_RECON_HOME,'Results','SAMSEG');
% volume directories
MRI_volfolder = fullfile(PHOTO_RECON_HOME,'FLAIR_Scan_Data');
Hrd_rcnfolder = fullfile(PHOTO_RECON_HOME,'Results_hard');
% figure directory
figDir = fullfile(PHOTO_RECON_HOME,'figures');
%% locate files
dlist_mristats = dir(fullfile(MRIsDir,'*/samseg.stats'));
dlist_hrdstats = dir(fullfile(hardDir,'*/samseg.stats'));
dlist_sftstats = dir(fullfile(softDir,'*/samseg.stats'));
%% read in MRI stats
segVolumeInfo = struct();
segVolumeInfo(length(dlist_mristats)+length(dlist_hrdstats)...
+length(dlist_sftstats)) = struct();
for il=1:length(dlist_mristats)
[~,segID,~]=fileparts(dlist_mristats(il).folder);
seprtd = regexp(segID(3:end),'_','split');
caseID = [seprtd{1},'-',seprtd{2}];
intable = readtable(fullfile(dlist_mristats(il).folder,dlist_mristats(il).name),'FileType','text');
segVolumeInfo(il).caseID = caseID;
segVolumeInfo(il).segtype = 'MRI';
%% check if case might need removing
removeindex = ismember(removecases,caseID);
if any(removeindex)
segVolumeInfo(il).removaltype=removaltype(removeindex);
segVolumeInfo(il).removalnotes=removalnotes{removeindex};
else
segVolumeInfo(il).removaltype=0;
segVolumeInfo(il).removalnotes='';
end
%% read in volumes
for jl=1:size(intable,1)
varname = intable{jl,1};
varname = regexp(varname,'# Measure ','split');
varname = [varname{1}{1},varname{1}{2}];
varname = matlab.lang.makeValidName(varname);
segVolumeInfo(il).(varname) = intable.Var2(jl);
end
end
%% read in hard stats
for il=1:length(dlist_hrdstats)
structind = il+length(dlist_mristats);
[~,segID,~]=fileparts(dlist_hrdstats(il).folder);
seprtd = regexp(segID,'.hard','split');
caseID = seprtd{1};
intable = readtable(fullfile(dlist_hrdstats(il).folder,dlist_hrdstats(il).name),'FileType','text');
segVolumeInfo(structind).caseID = caseID;
segVolumeInfo(structind).segtype = 'Hard';
%% check if case might need removing
removeindex = ismember(removecases,caseID);
if any(removeindex)
segVolumeInfo(structind).removaltype=removaltype(removeindex);
segVolumeInfo(structind).removalnotes=removalnotes{removeindex};
else
segVolumeInfo(structind).removaltype=0;
segVolumeInfo(structind).removalnotes='';
end
%% read in volumes
for jl=1:size(intable,1)
varname = intable{jl,1};
varname = regexp(varname,'# Measure ','split');
varname = [varname{1}{1},varname{1}{2}];
varname = matlab.lang.makeValidName(varname);
segVolumeInfo(structind).(varname) = intable.Var2(jl);
end
end
%% read in soft stats
for il=1:length(dlist_hrdstats)
structind = il+length(dlist_mristats)+length(dlist_hrdstats);
[~,segID,~]=fileparts(dlist_sftstats(il).folder);
seprtd = regexp(segID,'_soft','split');
caseID = seprtd{1};
intable = readtable(fullfile(dlist_sftstats(il).folder,dlist_sftstats(il).name),'FileType','text');
segVolumeInfo(structind).caseID = caseID;
segVolumeInfo(structind).segtype = 'Soft';
%% check if case might need removing
removeindex = ismember(removecases,caseID);
if any(removeindex)
segVolumeInfo(structind).removaltype=removaltype(removeindex);
segVolumeInfo(structind).removalnotes=removalnotes{removeindex};
else
segVolumeInfo(structind).removaltype=0;
segVolumeInfo(structind).removalnotes='';
end
%% read in volumes
for jl=1:size(intable,1)
varname = intable{jl,1};
varname = regexp(varname,'# Measure ','split');
varname = [varname{1}{1},varname{1}{2}];
varname = matlab.lang.makeValidName(varname);
segVolumeInfo(structind).(varname) = intable.Var2(jl);
end
end
%% write out table
T = struct2table(segVolumeInfo);
writetable(T,fullfile(figDir,'UWphoto_fullSegmentationStats.xlsx'))
%% remove bad apples
strfields = fieldnames(segVolumeInfo);
removeindex = ismember({segVolumeInfo.caseID},removecases);
% processtruct = instructure(~removeindex);
%% load in spreadsheets
infoTable=readtable(fullfile(figDir,'NP data for reconstruction cohort.xlsx'));
crosswalk=readtable(fullfile(figDir,'crosswalk.xlsx'));
%% match up case IDs
caseID = cell(size(infoTable,1),1);
for il=1:size(infoTable,1)
caseID{il} = crosswalk.npNumber1(crosswalk.tissueCode==infoTable.TissueCode(il));
end
matchedInfo = [cell2table(caseID),infoTable];
%% get left right averages
% segVolumeInfo = processtruct;
flag_fieldsToAverage_left = startsWith(strfields,'Left_');
fieldsToAverage_left=strfields(flag_fieldsToAverage_left);
fieldendings = regexp(strfields(flag_fieldsToAverage_left),'Left_','split');
for il=1:length(fieldendings)
fieldendings{il}=fieldendings{il}{2};
end
for il = 1:length(fieldsToAverage_left)
fieldToAverage_right = strfields{startsWith(strfields,'Right_') &...
endsWith(strfields,fieldendings(il))};
for jl = 1:length(segVolumeInfo)
leftVal = segVolumeInfo(jl).(fieldsToAverage_left{il});
rightVal = segVolumeInfo(jl).(fieldToAverage_right);
segVolumeInfo(jl).(['Average_',fieldendings{il}]) = mean([leftVal,rightVal]);
end
end
%% get volume adjustment factor
% During reconstruction using a hard mask some affine deformation is
% applied to the MRI mask at the same time as the individual slices. This
% is to account for gross changes in the brain between scanning and
% dissection.
%
% This means volumes from the MRI segmentations should be adjusted to
% correlate properly with the photo segmentations. The correction factor is
%
% adjustment = prod(MRI.volres)/prod(photo.volres)
%
% giving the ratio between MR and photo voxel volume. The adjusted photo
% volumes are then
%
% vol_corrected = adjustment * volume_photo
for il=1:length(segVolumeInfo)
if strcmp(segVolumeInfo(il).segtype,'Hard')
MR_ID_parts = regexp(segVolumeInfo(il).caseID,'-','split');
MR_ID = ['NP',MR_ID_parts{1},'_',MR_ID_parts{2}];
dlist_mri = dir(fullfile(MRI_volfolder,[MR_ID,'.rotated.mgz']));
mri_vol = MRIread(fullfile(dlist_mri.folder,dlist_mri.name));
dlist_hrd = dir(fullfile(Hrd_rcnfolder,segVolumeInfo(il).caseID,...
'*warped_ref*'));
hrd_vol = MRIread(fullfile(dlist_hrd.folder,dlist_hrd.name));
tempadjustfactor = prod(mri_vol.volres)/prod(hrd_vol.volres);
segVolumeInfo(il).volumeAdjustmentFactor = tempadjustfactor;
else
segVolumeInfo(il).volumeAdjustmentFactor = 1;
end
end
%% save stuff
save(fullfile(figDir,'AdjustedCaseStats.mat'),'segVolumeInfo',...
'matchedInfo','removecases')