diff --git a/SCaseDB/@SCaseDB/SCaseDB.m b/SCaseDB/@SCaseDB/SCaseDB.m index cae3fc0..bca69ce 100644 --- a/SCaseDB/@SCaseDB/SCaseDB.m +++ b/SCaseDB/@SCaseDB/SCaseDB.m @@ -1,150 +1,209 @@ classdef SCaseDB < handle % SCaseDB object all the functions for SCase measurements and handling. % Author: Matthieu Boubat, EPFL-LBO % Creation: 2020-01-13 % properties cases - chosen + pool end properties ( Hidden = true, Access = private ) logFid startTime dataRoot end methods ( Hidden = true, Access = private ) function fid = startLog(obj) % inputs: % outputs: log file id (fid) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% definitions fid = []; logDir = 'log'; MAX_LOG_FILE = 5; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% if isempty(dir(logDir)) - error(sprintf('no "%s" directory here',logDir)); + error(sprintf(['No "%s" directory here.\nCreate one or move to ',... + 'a directory containing one.'],logDir)); end %%%%%%%%%%%%%% cleaning of 'log' directory if max log file number exceeded logFileList = dir([logDir '\SCaseDB_*.log']); if length(logFileList) > MAX_LOG_FILE for i = 1:length(logFileList)-MAX_LOG_FILE+1 delete(fullfile(logDir,logFileList(i).name)); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% create log file obj.startTime.Format = '_yyyy_MM_dd_HHmmss'; fid = fopen(fullfile(logDir,['SCaseDB' char(obj.startTime) '.log']),'w'); obj.logFid = fid; - obj.log('SCaseDB.log\n') + obj.logn('SCaseDB.log') obj.startTime.Format = 'default'; - obj.log('Date: %s\n\n',char(obj.startTime)) + obj.logn('Date: %s',char(obj.startTime)) + obj.logn(''); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end function log(obj,str,varargin) % to be used like sprintf(). % write arguments in current log file. + if strfind(str,'\') + str = strrep([' [backslashes replaced by slashes]' str],'\','/'); + end str = sprintf(str,varargin{:}); fprintf(obj.logFid,str); end function logn(obj,str,varargin) % to be used like sprintf(). % write arguments in current log file. - obj.log([str '\n'],varargin{:}); + obj.log(str,varargin{:}); + fprintf(obj.logFid,'\n'); end end methods function obj = SCaseDB(varargin) % class constructor % input: data root directory path (e.g: shoulder/data, Z:\\dataDev) obj.startTime = datetime('now'); obj.logFid = obj.startLog; - obj.chosen = {}; if nargin if exist(varargin{1},'dir') obj.dataRoot = varargin{1}; end else - obj.dataRoot = '/shoulder/dataDev' + obj.dataRoot = '/shoulder/dataDev'; end - obj.cases = containers.Map; + tic + if isempty(gcp('nocreate')) + localCluster = parallel.cluster.Local; + obj.pool = parpool(localCluster.NumWorkers); + else + obj.pool = gcp; + end + toc + + obj.cases = containers.Map; obj.listSCase end function listSCase(obj,varargin) % fulfill 'cases' property tic list = dir([obj.dataRoot '/*/*/*/*/CT-*']); for i = 1:length(list) + SCase = regexp(list(i).folder,'[NP]\d{1,3}','match'); + subList = dir(fullfile(list(i).folder,'CT-*')) ; + chosen = fullfile(subList(1).folder,subList(1).name); + for j = 1:length(subList) + if ~isempty(dir(fullfile(subList(j).folder,subList(j).name,'amira'))) + chosen = fullfile(subList(j).folder,subList(j).name); + break; + end + end + obj.cases(SCase{1}) = chosen; + end + SCaseList = obj.cases.keys; + + + for i = 1:length(list) + SCase = regexp(list(i).folder,'[NP]\d{1,3}','match'); + if ~isempty(obj.cases(SCase{1})) + continue + end subList = dir(fullfile(list(i).folder,'CT-*')) ; chosen = fullfile(subList(1).folder,subList(1).name); for j = 1:length(subList) if ~isempty(dir(fullfile(subList(j).folder,subList(j).name,'amira'))) chosen = fullfile(subList(j).folder,subList(j).name); break; end end - obj.chosen{end+1,1} = chosen; + SCase = regexp(list(i).folder,'[NP]\d{1,3}','match'); if isempty(dir(fullfile(chosen,'matlab','SCase.mat'))) obj.cases(SCase{1}) = ShoulderCase(SCase); else obj.cases(SCase{1}) = load(fullfile(chosen,'matlab','SCase.mat')); obj.cases(SCase{1}) = obj.cases(SCase{1}).SCase; end end + % for i = 1:length(list) + % subList = dir(fullfile(list(i).folder,'CT-*')) ; + % chosen = fullfile(subList(1).folder,subList(1).name); + % for j = 1:length(subList) + % if ~isempty(dir(fullfile(subList(j).folder,subList(j).name,'amira'))) + % chosen = fullfile(subList(j).folder,subList(j).name); + % break; + % end + % end + % SCase = regexp(list(i).folder,'[NP]\d{1,3}','match'); + % if isempty(dir(fullfile(chosen,'matlab','SCase.mat'))) + % obj.cases(SCase{1}) = ShoulderCase(SCase); + % else + % obj.cases(SCase{1}) = load(fullfile(chosen,'matlab','SCase.mat')); + % obj.cases(SCase{1}) = obj.cases(SCase{1}).SCase; + % end + % end toc end + function comment(obj,str,varargin) + % to be used like sprintf(). + % write arguments in current log file. + obj.log('COMMENT'); + obj.logn([': ' str],varargin{:}); + end + + + function delete(obj) % class destructor fclose(obj.logFid); clear obj end end end