diff --git a/workflow/apps/matlab/easy/EASYconsole.m b/workflow/apps/matlab/easy/EASYconsole.m index cf96ff6e..42f0a146 100644 --- a/workflow/apps/matlab/easy/EASYconsole.m +++ b/workflow/apps/matlab/easy/EASYconsole.m @@ -3,6 +3,7 @@ function varargout = EASYconsole(varargin) global easyDir + global projectName global projectScansDir global easyResultsDir global easyProjectName @@ -40,23 +41,32 @@ function varargout = EASYconsole(varargin) fprintf('This script name: %s\n', easyFileName); - % Set projectScansDir (project scans directory) intelligently + % Allow EASY_DIR env to override hardcoded default EASY directory + if ~isempty(getenv('EASY_DIR')) + ed=fullfile(getenv('EASY_DIR')); + if ~strcmp(easyDir, ed) % sanity check + disp("WARNING: EASY_DIR does not match this script's hardcoded EASY location"); + disp("This is probably OK but this pathway is not well-tested"); + easyDir=ed; + end + fprintf('Using EASY script directory: %s from environment variable EASY_DIR\n', easyDir); + else + fprintf('Using EASY script directory: %s from hardcoded default\n', easyDir); + end + + + % Set projectScansDir intelligently if ~isempty(getenv('PROJECT_SCANS_DIR')) projectScansDir=fullfile(getenv('PROJECT_SCANS_DIR')); if exist(projectScansDir, 'dir') fprintf('Using scans directory: %s from environment variable PROJECT_SCANS_DIR\n', projectScansDir); disp('This usually indicates that we are in module mode'); - if isempty(getenv('PROJECT')) - dirInfo=fileparts(projectScansDir); - project=dirInfo.name; - end else disp('WARNING: PROJECT_SCANS_DIR does not exist'); end - elseif ~isempty(getenv('PROJECT')) % for standalone mode - % projectScansDir=fullfile(parentDir, '..', '..', 'scans', getenv('PROJECT') 'out', 'easy', getenv('PROJECT')); - project=getenv('PROJECT'); - projectScansDir=fullfile(parentDir, '..', '..', 'scans', project); + elseif ~isempty(getenv('PROJECT_NAME')) % for standalone mode + projectName=getenv('PROJECT_NAME'); + projectScansDir=fullfile(parentDir, '..', '..', 'scans', projectName); fprintf('Using project path: %s from environment variable PROJECT\n', projectScansDir); disp('This usually indicates that we are in standalone mode'); else @@ -85,18 +95,41 @@ function varargout = EASYconsole(varargin) end whos if ~isempty(matchedDirs) - fprintf('Found a non-empty scans directory in our list: %s\n', d); - fprintf('Setting projectScansDir to %s\n', char(d)); - projectScansDir=fullfile(d); + fprintf('Using scansDir %s\n', char(d)); end fprintf('Scanning inside %s for a project directory\n', d); sortedMatchedDirs=sortrows(matchedDirs); - project=sortedMatchedDirs{1}; % select the latest dir (by date prefix) - fprintf('Selected latest project directory %s\n', char(project)); + projectScansDir=fullfile(sortedMatchedDirs{end}); % select the latest dir (by date prefix) + fprintf('Using project scans directory: %s\n', char(projectScansDir)); + break end end end + % Get the name of the project + projectName=basename(projectScansDir); + + % Set the run-specific EASY output directory + if ~isempty(getenv('EASY_RESULTS_DIR')) + easyResultsDir=fullfile(getenv('EASY_RESULTS_DIR')); + fprintf('Using output directory: %s from environment variable EASY_RESULTS_DIR\n', easyResultsDir); + else + easyProjectName=strcat(todayStr, '_',userName, '_', project); + easyResultsDir=fullfile(parentDir, '..', '..', 'out', project, 'easy', easyProjectName); + if exist(easyResultsDir, 'dir') + fprintf('WARNING: EASY results dir %s already exists\n', easyResultsDir); + disp('Files in this directory may be overwritten') + fprintf('Using output directory: %s\n', easyResultsDir); + end + end + + % Get the name of the EASY project + if ~isempty(getenv('EASY_PROJECT_NAME')) + easyProjectName=getenv('EASY_PROJECT_NAME'); + else + easyProjectName=basename(easyResultsDir); + end + % User sanity check and warning if ~isempty(getenv('PROJECT_USER')) if ~strcmp(getenv('PROJECT_USER'), userName) @@ -104,44 +137,18 @@ function varargout = EASYconsole(varargin) end end - % Allow module to override hardcoded default EASY directory - if ~isempty(getenv('EASY_DIR')) - ed=fullfile(getenv('EASY_DIR')); - if ~strcmp(easyDir, ed) % sanity check - disp("WARNING: EASY_DIR does not match this script's hardcoded EASY location"); - disp("This is probably OK but this pathway is not well-tested"); - easyDir=ed; - end - fprintf('Using EASY script directory: %s from environment variable EASY_DIR\n', easyDir); - else - fprintf('Using EASY script directory: %s from hardcoded default\n', easyDir); - end - - if ~isempty(getenv('EASY_RESULTS_DIR')) - easyResultsDir=fullfile(getenv('EASY_RESULTS_DIR')); - fprintf('Using output directory: %s from environment variable EASY_RESULTS_DIR\n', easyResultsDir); - else - easyProjectName=strcat(todayStr,'_',userName,'_', project); - easyResultsDir=fullfile(parentDir, '..', '..', 'out', project, 'easy', easyProjectName); - if exist(easyResultsDir, 'dir') - fprintf('WARNING: EASY results dir %s already exists\n', easyResultsDir); - disp('Files in this directory may be overwritten') - fprintf('Using output directory: %s\n', easyResultsDir); - end - end - - if ~isempty(getenv('MASTER_PLATE_FILE')) masterPlateFile=fullfile(getenv('MASTER_PLATE_FILE')); fprintf('Using drug media file: %s from environment variable MASTER_PLATE_FILE\n', masterPlateFile); else % Try to find MasterPlate_ file on our own - mp=fullfile(projectScansDir, strcat('MasterPlate_', project,'.xlsx')); + mp=fullfile(easyResultsDir, strcat('MasterPlate_', projectName,'.xlsx')); if exist(mp, 'file') masterPlateFile=mp; - fprintf('Using drug media file: %s from internal logic\n', masterPlateFile); + fprintf('Using master plate file: %s from internal logic\n', masterPlateFile); else - fprintf('WARNING: Have you created a MasterPlate_ file in %s/easy_in/?\n', projectScansDir); + fprintf('WARNING: Could not find MasterPlate file: %s\n', mp); + fprintf('WARNING: Have you created a MasterPlate_ file in %s', easyResultsDir); end end @@ -149,17 +156,17 @@ function varargout = EASYconsole(varargin) drugMediaFile=fullfile(getenv('DRUG_MEDIA_FILE')); fprintf('Using drug media file: %s from environment variable DRUG_MEDIA_FILE\n', drugMediaFile); else - dm=fullfile(projectScansDir,'easy_in',strcat('DrugMedia_', project,'.xlsx')); - if exist(mp, 'file') + dm=fullfile(easyResultsDir, strcat('DrugMedia_', projectName,'.xlsx')); + if exist(dm, 'file') drugMediaFile=dm; fprintf('Using drug media file: %s from internal logic\n', drugMediaFile); else - fprintf('WARNING: Have you created a DrugMedia_ file in %s/MasterPlateFiles/?\n', projectScansDir); + fprintf('WARNING: Have you created a DrugMedia_ file in %s?\n', easyResultsDir); end end - matDir=fullfile(easyResultsDir); - matFile=fullfile(matDir,easyResultsDirName,'.mat'); + matDir=fullfile(easyResultsDir, 'matResults'); + matFile=fullfile(matDir,easyProjectName,'.mat'); mpdmFile=fullfile(matDir,'MPDM.mat'); printResultsDir=fullfile(easyResultsDir,'PrintResults'); fotosResultsDir=fullfile(easyResultsDir,'Fotos'); @@ -265,7 +272,7 @@ function NewExpDat_Callback(~, ~, ~) global matDir global matFile global easyResultsDir - global easyResultsDirName + global easyProjectName global fhconsole global scan global userName @@ -276,12 +283,11 @@ function NewExpDat_Callback(~, ~, ~) questdlg('\fontsize{20} NAME the file and NAVIGATE to the directory with the image folders.','File Creation','OK', struct('Default','OK','Interpreter','tex')); [matfile,easyResultsDir]=uiputfile('.mat'); inputFileName=strrep(inputFile,'.mat',''); - % easyResultsDirName=strcat('Results_',todayStr,'_',userName,'_',inputFileName); - project=basename(easyResultsDir); + + projectName=basename(easyResultsDir); % Set paths matDir=fullfile(easyResultsDir,'matResults'); - easyProjectName=strcat(todayStr,'_',userName,'_', project); - matFile=fullfile(matDir,strcat(todayStr,'_',userName,'_',inputFile)); + matFile=fullfile(matDir, projectName, '.mat'); % Added for 'parfor global' to preallocate 'scan' structure nlist=dir(fullfile(projectScansDir,'*')); @@ -338,19 +344,19 @@ end function LoadDatFile_Callback(~, ~, ~) global matDir global matFile + global projectName global easyResultsDir + global easyProjectName global easyPath global fhconsole try questdlg('Load results .mat from ../out/project/','File Creation','OK', struct('Default','OK','Interpreter','tex')); - [inputFile,inputPath]=uigetfile('.mat','Open Experiment folder and data storage .mat file name','MultiSelect','off'); - matDir=fullfile(inputPath); - matFile=fullfile(inputPath,inputFile); + [matDir,matFile]=uigetfile('.mat','Open Experiment folder and data storage .mat file name','MultiSelect','off'); load(matFile); easyResultsDir=fullfile(matDir,'..'); - project=basename(easyResultsDir); - projectScansDir=fullfile(easyResultsDir, '..', '..', 'scans', project); + projectName=basename(easyResultsDir); + projectScansDir=fullfile(easyResultsDir, '..', '..', 'scans', projectName); % TODO this is pretty hacky and needs something more explicit if isfolder(fullfile(matDir, '..','..','1')) % If Inovation Vrobot Then diff --git a/workflow/qhtcp-workflow b/workflow/qhtcp-workflow index dd835b3f..dde4bb04 100755 --- a/workflow/qhtcp-workflow +++ b/workflow/qhtcp-workflow @@ -592,10 +592,14 @@ easy() { script="$EASY_DIR/EASYconsole.m" # Prompt user for suffix - echo "Default EASY results directory: $EASY_RESULTS_DIR" + echo "Using default EASY results directory: $EASY_RESULTS_DIR" ((YES)) || read -r -p "Enter a custom suffix and/or hit enter to use the default (no suffix): " EASY_SUFFIX [[ -n $EASY_SUFFIX ]] && EASY_RESULTS_DIR+="_$EASY_SUFFIX" + # This dirname is separate from the project's so multiple EASY results can be generated + declare -gx EASY_PROJECT_NAME="${EASY_RESULTS_DIR##*/}" + debug "EASY results output name: $EASY_PROJECT_NAME" + # Backup and create EASY results dirs [[ -d $EASY_RESULTS_DIR ]] && backup "$EASY_RESULTS_DIR" [[ -d $EASY_RESULTS_DIR ]] || mkdir -p "$EASY_RESULTS_DIR" @@ -1566,22 +1570,23 @@ main() { done # Loop over projects - for PROJECT in "${PROJECTS[@]}"; do - declare -gx PROJECT_SCANS_DIR="$SCANS_DIR/$PROJECT" - declare -gx PROJECT_DATE="${PROJECT%"${PROJECT#????????}"}" # e.g. 20240723 - declare -gx PROJECT_SUFFIX="${PROJECT#????????_*_}" - declare -gx PROJECT_USER="${PROJECT#????????_}"; PROJECT_USER="${PROJECT_USER%%_*}" + for PROJECT_NAME in "${PROJECTS[@]}"; do + declare -gx PROJECT_NAME + declare -gx PROJECT_SCANS_DIR="$SCANS_DIR/$PROJECT_NAME" + declare -gx PROJECT_DATE="${PROJECT_NAME%"${PROJECT_NAME#????????}"}" # e.g. 20240723 + declare -gx PROJECT_SUFFIX="${PROJECT_NAME#????????_*_}" + declare -gx PROJECT_USER="${PROJECT_NAME#????????_}"; PROJECT_USER="${PROJECT_USER%%_*}" declare -gx STUDIES_ARCHIVE_FILE="$OUT_DIR/StudiesDataArchive.txt" - declare -gx QHTCP_PROJECT_DIR="$OUT_DIR/$PROJECT" + declare -gx QHTCP_PROJECT_DIR="$OUT_DIR/$PROJECT_NAME" declare -gx QHTCP_TEMPLATE_DIR="$TEMPLATES_DIR/qhtcp" declare -gx STUDY_TEMPLATE_DIR="$TEMPLATES_DIR/exp" declare -gx STUDY_INFO_FILE="$QHTCP_PROJECT_DIR/StudyInfo.csv" declare -gx EASY_OUT_DIR="$QHTCP_PROJECT_DIR/easy" if ((DEBUG)); then declare -p SCANS_DIR OUT_DIR TEMPLATES_DIR APPS_DIR \ - PROJECTS PROJECT \ - PROJECT_SCANS_DIR PROJECT_DATE PROJECT_SUFFIX \ - PROJECT_USER STUDIES_ARCHIVE_FILE QHTCP_PROJECT_DIR QHTCP_TEMPLATE_DIR \ + PROJECTS PROJECT_NAME \ + PROJECT_SCANS_DIR PROJECT_DATE PROJECT_SUFFIX PROJECT_USER \ + STUDIES_ARCHIVE_FILE QHTCP_PROJECT_DIR QHTCP_TEMPLATE_DIR \ STUDY_TEMPLATE_DIR STUDY_INFO_FILE fi