Commit a50517c6 authored by Mark Winter's avatar Mark Winter

Image dialog will export/rename old images or microscope data if necessary.

parent 4864ffe4
......@@ -4,7 +4,12 @@ settings = Load.ReadSettings();
bOpened = 0;
while ( ~bOpened )
[imageData, settings.imagePath] = MicroscopeData.ReadMetadata(settings.imagePath, true);
metadataPath = Load.ImageExportDialog(settings.imagePath,'Test');
if ( isempty(metadataPath) )
return;
end
[imageData, settings.imagePath] = MicroscopeData.ReadMetadataFile(metadataPath);
if ( isempty(imageData) )
return;
end
......
% [renameStruct,bNeedRename] = BuildRenameStruct(imageName)
function renameStruct = BuildRenameStruct(imageName)
renameStruct = [];
paramSet = {'c';'t';'z'};
paramSize = {2;4;4};
requireParam = [false,true,false];
paramPattern = cellfun(@(x)(['_(' x ')\d+']), paramSet, 'UniformOutput',false);
[~,fileName,fileExt] = fileparts(imageName);
[startMatch,tokMatch] = regexpi(fileName, paramPattern, 'start','tokens');
% If there are multiple matches to the parameter type, take only the one that's furthest in the name.
bFoundParams = cellfun(@(x)(~isempty(x)),startMatch);
validStarts = cellfun(@(x)(x(end)), startMatch(bFoundParams));
validParams = cellfun(@(x)(x{end}{1}), tokMatch(bFoundParams), 'UniformOutput',false);
if ( isempty(validStarts) )
return;
end
[~,validParamOrder] = sort(validStarts);
tokPattern = cellfun(@(x)(['_' x '(\d+)']), validParams(validParamOrder), 'UniformOutput',false);
buildPattern = ['^(.+)' tokPattern{:} '(.*?)$'];
tokMatch = regexp(fileName, buildPattern, 'tokens','once');
if ( isempty(tokMatch) )
return;
end
paramOrder = zeros(1,length(paramSet));
paramOrder(bFoundParams) = validParamOrder;
if ( ~all(paramOrder(requireParam)>0) )
return;
end
prefixStr = regexptranslate('escape',tokMatch{1});
postfixStr = regexptranslate('escape',tokMatch{end});
% Escape some possible file name issues
datasetName = tokMatch{1};
prefixName = strrep(tokMatch{1},'"','\"');
prefixName = strrep(prefixName,'''','\''');
prefixName = strrep(prefixName,'%','%%');
dirBlob = [datasetName '*' fileExt];
loadPattern = [prefixStr tokPattern{:} postfixStr '\' fileExt];
outParams = cellfun(@(x,y)(['_' x '%0' num2str(y) 'd']), paramSet,paramSize, 'UniformOutput',false);
outPattern = [prefixName outParams{:} '.tif'];
renameStruct = struct('datasetName',{datasetName}, 'dirBlob',{dirBlob}, 'loadPattern',{loadPattern}, 'outPattern',{outPattern}, 'paramOrder',{paramOrder});
end
% [bNeedsExport,bInplaceUpdate] = CheckExportImages(rootDir,fileName)
%
% Check if these specified image file needs to be exported for LEVER use or
% or if only an inplace rename and json generation is required.
function [bNeedsExport,bWriteable,renameStruct] = CheckExportImages(rootDir,fileName)
bNeedsExport = false;
bWriteable = false;
renameStruct = [];
[~,chkName,chkExt] = fileparts(fileName);
if ( any(strcmpi(chkExt,{'.tif','.tiff'})) )
jsonList = dir(fullfile(rootDir,[chkName '*.json']));
if ( ~isempty(jsonList) )
return;
end
renameStruct = Load.BuildRenameStruct(fileName);
end
bWriteable = ~isempty(renameStruct) && checkWriteable(rootDir);
bNeedsExport = true;
end
function bCanWrite = checkWriteable(rootDir)
bCanWrite = false;
fid = fopen(fullfile(rootDir,'testWriteFile'),'w');
if ( fid < 0 )
return;
end
fclose(fid);
delete(fullfile(rootDir,'testWriteFile'));
bCanWrite = true;
end
\ No newline at end of file
function jsonPath = ExportImages(exportDir, inRoot,inFilename)
[~,imD] = MicroscopeData.Original.Convert2Tiffs(inRoot,inFilename, exportDir, true, false);
if ( length(imD) > 1 )
[~,imName] = fileparts(inFilename);
exportDir = fullfile(exportDir,imName);
end
[~,chkIdx] = max(cellfun(@(x)((x.NumberOfFrames)),imD));
jsonPath = fullfile(exportDir,imD{chkIdx}.DatasetName,[imD{chkIdx}.DatasetName '.json']);
end
function exportLocation = ExportLocationDialog(rootDir,bInplace)
exportLocation = '';
% Offer to update the image names/json in place if possible.
if ( bInplace )
selAns = questdlg({'Selected images must be updated to conform to LEVER naming and metadata guidelines:',...
'Image name format: <DatasetName>_c%02d_t%04d_z%04d.tif',...
' ',...
'The images can be updated in place or exported to a new directory.'},...
'Image Export Required', 'Update','Export','Update');
if ( isempty(selAns) )
return;
end
if ( strcmpi(selAns,'Update') )
exportLocation = rootDir;
return;
end
end
while ( true )
chkDir = uigetdir(rootDir, 'Image Export Directory');
if ( ~chkDir )
return;
end
if ( strcmp(chkDir,rootDir) )
h = warndlg('Please select or create a different directory to export into.','Export Required');
uiwait(h);
else
exportLocation = chkDir;
return;
end
end
end
function jsonPath = ImageExportDialog(initialPath,promptTitle)
filterSpecs = {'*.json','LEVER Metadata (*.json)';
'*.lif;*.lei','Leica LAS (*.lif,*.lei)';
'*.czi;*.lsm;*.zvi','Zeiss (*.czi,*.lsm,*.zvi)';
'*.tif;*.tiff','TIFF Images (*.tif,*.tiff)';
'*.*','All Files (*.*)'};
jsonPath = '';
[fileName,rootDir,filterIndex] = uigetfile(filterSpecs,promptTitle,initialPath);
if (filterIndex==0)
return
end
if ( filterIndex == 1 )
jsonPath = fullfile(rootDir,fileName);
return
end
[~,chkName,chkExt] = fileparts(fileName);
if ( filterIndex == length(filterSpecs) && strcmpi(chkExt,'.json') )
jsonPath = fullfile(rootDir,fileName);
return;
end
[bNeedsExport,bWriteable,renameStruct] = Load.CheckExportImages(rootDir,fileName);
if ( ~bNeedsExport )
jsonList = dir(fullfile(rootDir,[chkName '*.json']));
jsonPath = fullfile(rootDir,jsonList(1).name);
return;
end
exportDir = Load.ExportLocationDialog(rootDir, bWriteable);
if ( isempty(exportDir) )
return;
end
if ( isempty(renameStruct) )
jsonPath = Load.ExportImages(exportDir, rootDir,fileName);
else
jsonPath = Load.RenameImages(exportDir, rootDir, renameStruct);
end
end
function jsonPath = RenameImages(exportDir,inRoot, renameStruct, bCreateDatasetDir)
if ( ~exist('bCreateDatasetDir','var') )
bCreateDatasetDir = true;
end
fileOp = @(src,dst)(movefile(src,dst,'f'));
if ( ~strcmp(exportDir,inRoot) )
fileOp = @(src,dst)(copyfile(src,dst,'f'));
if ( bCreateDatasetDir )
exportDir = fullfile(exportDir,renameStruct.datasetName);
end
end
% fileOp = @(src,dst)(fprintf('%s -> %s\n', src,dst));
if ( ~exist(exportDir,'dir') )
mkdir(exportDir);
end
flist = dir(fullfile(inRoot,renameStruct.dirBlob));
nameList = {flist.name}.';
loadTok = regexp(nameList, renameStruct.loadPattern, 'tokens','once');
bValidParams = (renameStruct.paramOrder > 0);
paramIdx = find(bValidParams);
validOrder = paramIdx(renameStruct.paramOrder(bValidParams));
bValidNames = cellfun(@(x)(length(x)==nnz(bValidParams)), loadTok);
paramTok = vertcat(loadTok{bValidNames});
paramVals = ones(size(paramTok,1),length(renameStruct.paramOrder));
paramVals(:,validOrder) = cellfun(@(x)(str2double(x)), paramTok);
paramMin = min(paramVals,[],1);
paramOffset = 1 - paramMin;
validNames = nameList(bValidNames);
for i=1:length(validNames)
paramCell = num2cell(paramVals(i,:)+paramOffset);
outName = sprintf(renameStruct.outPattern,paramCell{:});
fileOp(fullfile(inRoot,validNames{i}),fullfile(exportDir,outName));
end
imageData = MicroscopeData.MakeMetadataFromFolder(exportDir,renameStruct.datasetName);
MicroscopeData.CreateMetadata(exportDir,imageData,true);
jsonPath = fullfile(exportDir,[renameStruct.datasetName '.json']);
end
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