Commit 96f0fdc0 authored by Mark Winter's avatar Mark Winter

Batch exporting for seg and track folders, also split preview functionality...

Batch exporting for seg and track folders, also split preview functionality into a preview figure handler and a dialog box for setting parameters.
parent a50517c6
function [subPaths,needsExport,renamable] = CheckFolderExport(rootDir)
[subPaths,needsExport,renamable] = recursiveCheckExport(rootDir,'');
end
function [subPaths,needsExport,renamable] = recursiveCheckExport(rootDir,subDir)
subPaths = {};
needsExport = false(0);
renamable = false(0);
dirList = dir(fullfile(rootDir,subDir));
bInvalidName = arrayfun(@(x)(strncmpi(x.name,'.',1) || strncmpi(x.name,'..',2)), dirList);
bValidDir = ~bInvalidName & (vertcat(dirList.isdir) > 0);
bValidFile = ~bInvalidName & (vertcat(dirList.isdir) == 0);
fileList = dirList(bValidFile);
pathList = dirList(bValidDir);
filenames = {fileList.name}.';
%% Get a list of valid JSON files
jsonMatch = regexpi(filenames, '(\.json$)','once');
bJSON = cellfun(@(x)(~isempty(x)),jsonMatch);
jsonNames = filenames(bJSON);
if ( ~isempty(jsonNames) )
bValid = cellfun(@(x)(~isempty(MicroscopeData.ReadMetadataFile(fullfile(rootDir,subDir,x)))),jsonNames);
% Remove files that have the same prefix (datasetName) as .json from the list
datasetNames = cellfun(@(x)(x(1:end-5)),jsonNames(bValid),'UniformOutput',false);
matchDatasets = cellfun(@(x)(strncmp(x,filenames.',length(x))),datasetNames,'UniformOutput',false);
bMatched = any(vertcat(matchDatasets{:}),1);
filenames = filenames(~bMatched);
subPaths = cellfun(@(x)(fullfile(subDir,x)),jsonNames(bValid),'UniformOutput',false);
needsExport = false(nnz(bValid),1);
renamable = false(nnz(bValid),1);
end
%% Handle folders of TIFs that don't require export or are renamable stacks
tifMatch = regexpi(filenames, '(\.tif$)|(\.tiff$)','once');
bTIF = cellfun(@(x)(~isempty(x)),tifMatch);
tifNames = filenames(bTIF);
if ( ~isempty(tifNames) )
% If these appear to be renameable tifs don't bother with subdirectories
[bNeedsExport,bWriteable,renameStruct] = Load.CheckExportImages(fullfile(rootDir,subDir),tifNames{1});
if ( ~isempty(renameStruct) )
subPaths = {fullfile(subDir,tifNames{1})};
needsExport = bNeedsExport;
renamable = bWriteable;
return;
end
end
%% Check any other files to see if they are supported and need export
if ( ~isempty(filenames) )
bCanExport = MicroscopeData.Original.CanExportFormat(filenames);
exportNames = filenames(bCanExport);
subPaths = [subPaths; cellfun(@(x)(fullfile(subDir,x)),exportNames,'UniformOutput',false)];
needsExport = [needsExport; true(length(exportNames),1)];
renamable = [renamable; false(length(exportNames),1)];
end
%% Deal with further subdirectories
for i=1:length(pathList)
nextSubDir = fullfile(subDir,pathList(i).name);
[newPaths,chkExport,chkRename] = recursiveCheckExport(rootDir,nextSubDir);
subPaths = [subPaths; newPaths];
needsExport = [needsExport; chkExport];
renamable = [renamable; chkRename];
end
end
function exportRoot = FolderExport(rootDir)
exportRoot = rootDir;
[subPaths,needsExport,renamable] = Load.CheckFolderExport(rootDir);
if ( isempty(subPaths) )
return;
end
bExport = any(needsExport);
if ( ~bExport )
return;
end
% Allow an inplace update if all exports are renamable
exportRenames = renamable(needsExport);
exportPaths = subPaths(needsExport);
bInplace = all(exportRenames);
exportRoot = Load.ExportLocationDialog(rootDir, bInplace);
if ( isempty(exportRoot) )
return;
end
%% Copy all entries that don't need an export if we aren't doing inplace
copyPaths = {};
if ( ~strcmp(exportRoot,rootDir) )
copyPaths = subPaths(~needsExport);
end
%% Copy all valid lever data to export directory
for i=1:length(copyPaths)
[subDir,filename] = fileparts(exportPaths{i});
exportDir = fullfile(exportRoot,subDir);
importDir = fullfile(rootDir,subDir);
if ( ~exist(exportDir,'dir') )
mkdir(exportDir);
end
copyfile(fullfile(importDir,[filename '*']),exportDir);
end
%% Export or rename/copy
for i=1:length(exportPaths)
[subDir,filename,fext] = fileparts(exportPaths{i});
inputName = [filename fext];
exportDir = fullfile(exportRoot,subDir);
importDir = fullfile(rootDir,subDir);
if ( ~exportRenames(i) )
Load.ExportImages(exportDir, importDir,inputName);
else
renameStruct = Load.BuildRenameStruct(inputName);
Load.RenameImages(exportDir, importDir,renameStruct,false);
end
end
end
This diff is collapsed.
This diff is collapsed.
......@@ -11,6 +11,17 @@ global CONSTANTS CellPhenotypes
CONSTANTS=[];
if ( ~exist('outputDir','var') )
outputDir = '';
end
% Trial run command
bTrialRun = false;
if ( strncmpi(outputDir,'-T',2) )
outputDir = outputDir(3:end);
bTrialRun = true;
end
softwareVersion = Dev.GetVersion();
if (isdeployed())
......@@ -20,58 +31,44 @@ end
directory_name = uigetdir('','Select Root Folder for Seg and Track');
if ( ~directory_name ),return,end
if ( ~exist('outputDir','var') )
outputDir = directory_name;
% Get initial cell segmentation parameters
Load.AddConstant('cellType', [], 1);
hDlg = UI.SegPropDialog();
uiwait(hDlg);
if ( isempty(CONSTANTS.cellType) )
return;
end
% Trial run command
bTrialRun = 0;
if ( strcmpi(outputDir(1:2),'-T') )
outputDir = outputDir(3:end);
if ( isempty(outputDir) )
outputDir = directory_name;
end
bTrialRun = 1;
%% Run export of subdirectories if necessary.
exportRoot = Load.FolderExport(directory_name);
if ( isempty(exportRoot) )
return;
end
%% Get a list of valid subdirectories
dirList = dir(directory_name);
if ( isempty(outputDir) )
outputDir = exportRoot;
end
bInvalidName = arrayfun(@(x)(strncmpi(x.name,'.',1) || strncmpi(x.name,'..',2)), dirList);
bValidDir = ~bInvalidName & (vertcat(dirList.isdir) > 0);
dirList = dirList(bValidDir);
%% Find valid images in exported folder.
[chkPaths,invalidFile] = Load.CheckFolderExport(exportRoot);
validJSON = chkPaths(~invalidFile);
validDirs = {};
for dirIdx=1:length(dirList)
if ( ~(dirList(dirIdx).isdir) )
continue
end
imageData = MicroscopeData.ReadMetadata([fullfile(directory_name,dirList(dirIdx).name) '\'],false);
if ( isempty(imageData) )
continue;
end
validDirs = [validDirs; dirList(dirIdx).name];
if ( isempty(validJSON) )
warning(['No valid data found at: ' exportRoot]);
return;
end
%% Use previewer on first valid directory to get cell type and segmentation parameters
Load.AddConstant('version',softwareVersion,1);
Load.AddConstant('cellType', [], 1);
[imageData,imagePath] = MicroscopeData.ReadMetadata([fullfile(directory_name, validDirs{1}) '\']);
[imageData,imagePath] = MicroscopeData.ReadMetadataFile(fullfile(exportRoot,validJSON{1}));
Metadata.SetMetadata(imageData);
Load.AddConstant('rootImageFolder', imagePath, 1);
Load.AddConstant('matFullFile', fullfile(outputDir, Metadata.GetDatasetName()),1);
UI.SegPreview();
if ( isempty(CONSTANTS.cellType) )
return;
end
cellType = CONSTANTS.cellType;
%% Use total number of processors or max from command-line
numProcessors = getenv('Number_of_processors');
numProcessors = str2double(numProcessors);
......@@ -88,12 +85,15 @@ end
%% Run segmentation for all valid directories
processedDatasets = {};
for dirIdx=1:length(validDirs)
[imageData,imagePath] = MicroscopeData.ReadMetadata([fullfile(directory_name, validDirs{dirIdx}) '\']);
for dirIdx=1:length(validJSON)
subDir = fileparts(validJSON{dirIdx});
dataDir = fileparts(subDir);
[imageData,imagePath] = MicroscopeData.ReadMetadataFile(fullfile(exportRoot,validJSON{dirIdx}));
Metadata.SetMetadata(imageData);
Load.AddConstant('rootImageFolder', imagePath, 1);
Load.AddConstant('matFullFile', fullfile(outputDir, [Metadata.GetDatasetName() '_LEVer.mat']),1);
Load.AddConstant('matFullFile', fullfile(outputDir,dataDir, [Metadata.GetDatasetName() '_LEVer.mat']),1);
if ( exist(CONSTANTS.matFullFile,'file') )
fprintf('%s - LEVer data already exists. Skipping\n', Metadata.GetDatasetName());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment