%% degenerationAll % % This script analyses the muscles data obtained by contouring SS, IS, SC, and % TM. It returns an excel file (SpecificMusclesState_P###) for % 'specific' argument. In case of 'new' or 'all' arguments, the script % returns four DAT file (musclesState_SS, musclesState_IS, musclesState_SC, % musclesState_TM). They are automatically connected to the Excel file % "musclesStates". Don't forget to actualize the connections in Excel. % % Author: EPFL-LBO % Date: 2016-09-05 % %% % Source is 'all' or 'specific' (for only one case, the subject is required as second argument) % Subject is the patient number (only in case of 'specific') function [] = degenerationAll(source,subject) clc muscleName = cellstr(['SS';'IS';'SC';'TM']); switch source %% 'specific' Cases case {'specific'} if subject > 1000; subject = sprintf('N%03d',subject-1000); end subject = sprintf('P%03d',subject); Degen{1,1} = subject; Degen{1,2} = 'Atrophy'; Degen{1,3} = 'Infiltration'; Degen{1,4} = 'Osteo'; Degen{1,5} = 'Degeneration'; for j=1:4 HUThresholdMuscle = -29; HUThresholdOsteochondroma = 166.25; close all [Stot,Sm,Sa,Si,So,Ra,Ri,Ro,Rd] = muscleDegeneration(subject, char(muscleName(j)),HUThresholdMuscle,HUThresholdOsteochondroma,'image'); allData(j,:) = [Stot,Sm,Sa,Si,So,Ra,Ri,Ro,Rd]; Degen{j+1,1} = char(muscleName(j)); Degen{j+1,2} = cell2mat(Ra(i)); Degen{j+1,3} = cell2mat(Ri(i)); Degen{j+1,4} = cell2mat(Ro(i)); Degen{j+1,5} = cell2mat(Rd(i)); end % Load the reference 2D CT slice and associated muscle contour CTDatabaseLocation = 'Z://data'; % Location of the CT database listDirInCurrDir = dir([CTDatabaseLocation '/' subject(1) '/' subject(2) '/' subject(3)]); listDirInCurrDir = listDirInCurrDir(3:end); for i=1:length(listDirInCurrDir) if ~isempty(regexp(listDirInCurrDir(i).name,['^[PN]\d{0,2}[' subject(4) '][-]\w*'], 'once')) listDirInCaseFolder = dir([listDirInCurrDir(i).folder '\' listDirInCurrDir(i).name]); listDirInCaseFolder = listDirInCaseFolder(3:end); % In all CT folders in the current Case Folder, find CT #1 for j=1:length(listDirInCaseFolder) if ~isempty(regexp(listDirInCaseFolder(j).name,'^[C][T][-][PN]\d{1,3}[-]\d*[-][1]', 'once')) % If 'muscles' exists, save iput path if exist([listDirInCaseFolder(j).folder '\' listDirInCaseFolder(j).name '\muscles'],'dir') == 7 inputPath = [listDirInCaseFolder(j).folder '\' listDirInCaseFolder(j).name '\muscles\']; else error(['Missing "muscles" directory for CaseID: ' subject]); end end end end end filename = [inputPath 'SpecificMusclesState_' subject '.xls']; xlswrite(filename, Degen); %% 'all' Cases case {'all'} % Pfolders = dir('results\SS\*'); % for i = 3:numel(Pfolders) % Pcases(i-2,1) = str2double(Pfolders(i).name(2:4)); % if strcmp(Pfolders(i).name(1),'N') % Pcases(i-2,1) = Pcases(i-2,1) + 1000; % end % end % Pcases = sort(Pcases); location = 'N'; l=1; rootDir = '../../../../data/N'; for levelDir1 = 0:9 for levelDir2 = 0:9 curentDir = sprintf('%s/%d/%d/N*', rootDir, levelDir1, levelDir2); folderInfo = dir(curentDir); for i=1:1:numel(folderInfo) name = folderInfo(i).name; sCases_normal{l,1} = folderInfo(i).name; sCases_normal{l,2} = levelDir1; sCases_normal{l,3} = levelDir2; sCases_normal{l,4} = 'N'; l=l+1; end end end location = 'P'; l=1; rootDir = '../../../../data/P'; for levelDir1 = 0:9 for levelDir2 = 0:9 curentDir = sprintf('%s/%d/%d/P*', rootDir, levelDir1, levelDir2); folderInfo = dir(curentDir); for i=1:1:numel(folderInfo) name = folderInfo(i).name; sCases_pathologic{l,1} = folderInfo(i).name; sCases_pathologic{l,2} = levelDir1; sCases_pathologic{l,3} = levelDir2; sCases_pathologic{l,4} = 'P'; l=l+1; end end end sCases = [sCases_normal;sCases_pathologic]; patientList = sCases(:,1); for j=1:4 m=1; HUThresholdMuscle = -29; HUThresholdOsteochondroma = 166.25; for i = 1:length(sCases) patient = char(sCases(i,1)); levelDir1 = cell2mat(sCases(i,2)); levelDir2 = cell2mat(sCases(i,3)); location = char(sCases(i,4)); CTn = strtok(patient,'-'); CTn = strtok(CTn, location); CTn = str2num(CTn); CTn = sprintf('%s%03d',location,CTn); if exist(sprintf('results/%s/%s', char(muscleName(j)),CTn),'dir') == 7 close all % subject = sprintf('P%03d',Pcases(i)); % if Pcases(i) > 1000; subject = sprintf('N%03d',Pcases(i)-1000); end clc percentage = 100*i/length(sCases); %fprintf('%s %d, %3.0f%%\n', char(muscleName(j)), CTn, percentage); fprintf('%s %s, %3.0f%%\n', char(muscleName(j)), CTn, percentage); [Stot(m),Sm(m),Sa(m),Si(m),So(m),Ra(m),Ri(m),Ro(m),Rd(m)] = muscleDegeneration(CTn, char(muscleName(j)),HUThresholdMuscle,HUThresholdOsteochondroma,'image'); else Ra(m) = cellstr(' '); Ri(m) = cellstr(' '); Ro(m) = cellstr(' '); Rd(m) = cellstr(' '); end patientName{m} = strtok(patient,'-'); m=m+1; end % result=[patientName, num2cell(SS_results)]; % xlswrite('musclesTest', results); % filename = sprintf('musclesState2_%s.dat', char(muscleName(j))); % csvwrite(filename, [patientList(:) Ra(:) Ri(:) Ro(:) Rd(:)]) if j==1 SS_results = [Ra(:) Ri(:) Ro(:) Rd(:)] end if j==2 IS_results = [Ra(:) Ri(:) Ro(:) Rd(:)] end if j==3 SC_results = [Ra(:) Ri(:) Ro(:) Rd(:)] end if j==4 TM_results = [Ra(:) Ri(:) Ro(:) Rd(:)] end end % %% 'new' cases % case {'new'} % if ( exist('musclesState_SS.dat', 'file') || exist('musclesState_IS.dat', 'file') || exist('musclesState_SC.dat', 'file') || exist('musclesState_TM.dat', 'file') ) % % Pfolders = dir('results\SS\*'); % for i = 3:numel(Pfolders) % Pcases(i-2,1) = str2double(Pfolders(i).name(2:4)); % if strcmp(Pfolders(i).name(1),'N') % Pcases(i-2,1) = Pcases(i-2,1) + 1000; % end % end % Pcases = sort(Pcases); % % %Read the existing dat file % load 'musclesState_SS.dat'; % load 'musclesState_IS.dat'; % load 'musclesState_SC.dat'; % load 'musclesState_TM.dat'; % SSfile = musclesState_SS; % ISfile = musclesState_IS; % SCfile = musclesState_SC; % TMfile = musclesState_TM; % % missing = setdiff(Pcases,SSfile(:,1)); % % if ~isempty(missing) % for j=1:4 % % HUThresholdMuscle = -29; % HUThresholdOsteochondroma = 166.25; % % for i = 1:length(missing) % close all % % load 'musclesState_SS.dat'; % load 'musclesState_IS.dat'; % load 'musclesState_SC.dat'; % load 'musclesState_TM.dat'; % SSfile = musclesState_SS; % ISfile = musclesState_IS; % SCfile = musclesState_SC; % TMfile = musclesState_TM; % % subject = sprintf('P%03d',missing(i)); % if missing(i) > 1000; subject = sprintf('N%03d',missing(i)-1000); end % percentage = 100*i/length(missing); % clc % fprintf('%s %d, %3.0f%%\n', char(muscleName(j)), missing(i), percentage) % [Stot(i),Sm(i),Sa(i),Si(i),So(i),Ra(i),Ri(i),Ro(i),Rd(i)] = muscleDegeneration(subject, char(muscleName(j)),HUThresholdMuscle,HUThresholdOsteochondroma,'image'); % clc % fprintf('%d, %3.0f%%\n', missing(i), percentage) % output = eval(sprintf('%sfile', char(muscleName(j)))); % n=length(output); % % output(n+1,:) = [missing(i) Ra(i) Ri(i) Ro(i) Rd(i)]; % output = sortrows(output,1); % % filename = sprintf('musclesState_%s.dat', char(muscleName(j))); % csvwrite(filename, output) % end % end % else % disp('No new cases found!') % end % end result=[patientName, SS_results,IS_results, SC_results, TM_results]; xlswrite('muscles', result); otherwise disp('Unknown group name') end