Skip to content

Commit

Permalink
Merge branch 'master' into hotfix/fixTestEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
wahln authored May 26, 2023
2 parents 2f42625 + 689eabe commit beaee4e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
50 changes: 26 additions & 24 deletions IO/matRad_readNRRD.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

matRad_cfg = MatRad_Config.instance();

% Open file.
hFile = fopen(filename, 'r');
if hFile <= 0
error('Could not open NRRD file!');
matRad_cfg.dispError('Could not open NRRD file!');
end
cleaner = onCleanup(@() fclose(hFile));

%% Determine NRRD Version
nrrdLine = fgetl(hFile);
regTokens = regexp(nrrdLine,'NRRD00(?:\.)?0([1-5])','tokens');
if isempty(regTokens)
error('Invalid Header line! Could not identify NRRD version!');
matRad_cfg.dispError('Invalid Header line! Could not identify NRRD version!');
end
nrrdVersion = str2num(regTokens{1}{1});

if nrrdVersion > 5
error('NRRD version > 5 not supported!');
matRad_cfg.dispError('NRRD version > 5 not supported!');
end

%% Read header
Expand All @@ -60,15 +62,15 @@
%Parse the line
lineContent = regexp(currentLine, '(.+):(=|\s)(.+)', 'tokens');
if isempty(lineContent)
warning(['Could not parse line: "' lineContent '"']);
matRad_cfg.dispWarning(['Could not parse line: "' currentLine '"']);
elseif isequal(lineContent{1}{2},' ') %space after colon refers to "field"
nrrdMetaData.fields{end+1,1} = lineContent{1}{1}; %Fieldname
nrrdMetaData.fields{end,2} = lineContent{1}{3}; %Information
elseif isequal(lineContent{1}{2},'=') %= after colon refers to key-value pair
nrrdMetaData.keys{end+1,1} = lineContent{1}{1}; %Key
nrrdMetaData.keys{end,2} = lineContent{1}{3}; %Value
else
warning(['Could not parse line: "' lineContent '"']);
matRad_cfg.dispWarning(['Could not parse line: "' currentLine '"']);
end
end
currentLine = fgetl(hFile);
Expand All @@ -85,7 +87,7 @@
endianFieldIx = find(ismember(nrrdMetaData.fields(:,1),'endian'));
if ~isempty(endianFieldIx)
if ~isequal(nrrdMetaData.fields{endianFieldIx,2},'little') && ~isequal(nrrdMetaData.fields{endianFieldIx,2},'big')
error(['Datatype is ' datatype ', thus endian information is required but could not be interpreted!']);
matRad_cfg.dispError(['Datatype is ' datatype ', thus endian information is required but could not be interpreted!']);
end;
%Now we compare the file endian to the system endian
%First acquire system endian
Expand All @@ -101,36 +103,36 @@
doSwapBytes = true;
end
else
error(['Datatype is ' datatype ', thus endian information is required but could not be found!']);
matRad_cfg.dispError(['Datatype is ' datatype ', thus endian information is required but could not be found!']);
end
end
metadata.datatype = datatype;

else
error('Could not find required "type" field!');
matRad_cfg.dispError('Could not find required "type" field!');
end

%Check for the always required image dimension
dimFieldIx = find(ismember(nrrdMetaData.fields(:,1), 'dimension'));
if ~isempty(dimFieldIx)
[metadata.dimension,success] = str2num(nrrdMetaData.fields{dimFieldIx,2});
if ~success
error('Could not read required dimension field');
matRad_cfg.dispError('Could not read required dimension field');
end
else
error('Could not find required "dimension" field!');
matRad_cfg.dispError('Could not find required "dimension" field!');
end

%Check for size / dim length
sizeFieldIx = find(ismember(nrrdMetaData.fields(:,1), 'sizes'));
if ~isempty(sizeFieldIx)
sizes = textscan(nrrdMetaData.fields{sizeFieldIx,2},'%d');
if numel(sizes{1}) ~= metadata.dimension || ~all(sizes{1} > 0)
error('Incorrect size definition!');
matRad_cfg.dispError('Incorrect size definition!');
end
metadata.cubeDim = sizes{1}';
else
error('Could not find required "dimension" field!');
matRad_cfg.dispError('Could not find required "dimension" field!');
end

%Check for resolution
Expand All @@ -140,7 +142,7 @@
if ~isempty(spacingFieldIx)
resolutions = textscan(nrrdMetaData.fields{spacingFieldIx,2},'%f');
if numel(resolutions{1}) ~= metadata.dimension
error('Incorrect spacings definition');
matRad_cfg.dispError('Incorrect spacings definition');
end
metadata.resolution = resolutions{1}';

Expand All @@ -167,14 +169,14 @@
currentAxis = find(vectors{c});

if numel(find(vectors{c})) ~= 1
error('Sorry! We currently only support spaces with cartesian basis!');
matRad_cfg.dispError('Sorry! We currently only support spaces with cartesian basis!');
end
metadata.axisPermutation(c) = currentAxis*sign(vectors{c}(currentAxis));
metadata.resolution(c) = vectors{c}(currentAxis);
end

else
warning('No Resolution Information available');
matRad_cfg.dispWarning('No Resolution Information available');
end

%find the origin if we have one
Expand Down Expand Up @@ -202,7 +204,7 @@
data_fileFieldIx = find(ismember(nrrdMetaData.fields(:,1), 'data file'));
datafileFieldIx = find(ismember(nrrdMetaData.fields(:,1), 'datafile'));
if ~isempty(data_fileFieldIx) || ~isempty(datafileFieldIx)
error('Sorry! We currently do not support detached data files!');
matRad_cfg.dispError('Sorry! We currently do not support detached data files!');
%Proposed workflow:
%check for data file
%close file
Expand All @@ -215,15 +217,15 @@
%Check for encoding
encodingFieldIx = find(ismember(nrrdMetaData.fields(:,1), 'encoding'));
if isempty(encodingFieldIx)
error('Could not find required "encoding" field!');
matRad_cfg.dispError('Could not find required "encoding" field!');
end
switch nrrdMetaData.fields{encodingFieldIx,2}
case 'raw'
cube = fread(hFile,prod(metadata.cubeDim), metadata.datatype);
case {'txt','text','ascii'}
cube = cast(fscanf(hFile,'%f'),metadata.datatype);
case 'hex'
error('Sorry: NRRD hex file not yet supported!');
matRad_cfg.dispError('Sorry: NRRD hex file not yet supported!');
case {'gz','gzip'}
compressedByteArray = fread(hFile, inf, 'uint8');

Expand All @@ -242,7 +244,7 @@
javaByteOutputStream.close();
javaUnpackSuccessful = true;
catch
warning('Java unpacking failed... using temporary files!');
matRad_cfg.dispWarning('Java unpacking failed... using temporary files!');
end
end
if ~javaUnpackSuccessful
Expand All @@ -252,7 +254,7 @@
hFileTmp = fopen(tmpFile, 'wb');

if hFileTmp <= 0
error('Could not open temporary file for GZIP!');
matRad_cfg.dispError('Could not open temporary file for GZIP!');
end

fwrite(hFileTmp, compressedByteArray, 'uint8');
Expand All @@ -264,16 +266,16 @@
%Read the uncompressed file
hFileTmp = fopen(tmpName, 'rb');
if hFileTmp <= 0
error('Could not open unpacked file!');
matRad_cfg.dispError('Could not open unpacked file!');
end
cleanTmpFile = onCleanup(@() fclose(hFileTmp));

cube = fread(hFileTmp, prod(metadata.cubeDim), metadata.datatype);
end
case {'bz2','bzip2'}
error('Sorry: bzip compression not yet supported!');
matRad_cfg.dispError('Sorry: bzip compression not yet supported!');
otherwise
error(['Undefined NRRD encoding scheme: ' nrrdMetaData.encoding]);
matRad_cfg.dispError(['Undefined NRRD encoding scheme: ' nrrdMetaData.encoding]);
end

%maybe we need to correct the byte ordering (endian)
Expand Down Expand Up @@ -327,7 +329,7 @@
case 'double'
datatype = 'double';
otherwise
error('Could not identify datatype for NRRD data');
matRad_cfg.dispError('Could not identify datatype for NRRD data');
end

end
Expand Down
5 changes: 4 additions & 1 deletion dicom/matRad_scanDicomImportFolder.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
%% get all files in search directory

% dicom import needs image processing toolbox -> check if available
v = ver;
if ~license('checkout','image_toolbox')
matRad_cfg.dispError('image processing toolbox and/or corresponding licence not available');
matRad_cfg.dispError('Image Processing Toolbox and/or corresponding license not available');
elseif ~any(strcmp('Image Processing Toolbox', {v.Name}))
matRad_cfg.dispError('Image Processing Toolbox not installed');
end

fileList = matRad_listAllFiles(patDir);
Expand Down
4 changes: 4 additions & 0 deletions matRad_resizeCstToGrid.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
cst{i,4}{j} = find(matRad_interp3(vXgridOld,vYgridOld,vZgridOld, ...
tmpCube, ...
vXgridNew,vYgridNew',vZgridNew,'nearest'));
if isempty(cst{i,4}{j})
matRad_cfg = MatRad_Config.instance();
matRad_cfg.dispWarning('Resizing the cst to the dose grid created an empty structure %s in scenario %d (cst{%d,4}{%d})!',cst{i,2},j,i,j);
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
for i = 1:size(cst,1)

% Only take OAR or target VOI.
if ~isempty(cst{i,4}) && ( isequal(cst{i,3},'OAR') || isequal(cst{i,3},'TARGET') )
if ~any(cellfun(@isempty,cst{i,4})) && ( isequal(cst{i,3},'OAR') || isequal(cst{i,3},'TARGET') )

% loop over the number of constraints for the current VOI
for j = 1:numel(cst{i,6})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
% compute objective function for every VOI.
for i = 1:size(cst,1)
% Only take OAR or target VOI.
if ~isempty(cst{i,4}{1}) && ( isequal(cst{i,3},'OAR') || isequal(cst{i,3},'TARGET') )
if ~any(cellfun(@isempty,cst{i,4})) && ( isequal(cst{i,3},'OAR') || isequal(cst{i,3},'TARGET') )
% loop over the number of constraints for the current VOI
for j = 1:numel(cst{i,6})

Expand All @@ -48,7 +48,7 @@
% get the jacobian structure depending on dose
jacobDoseStruct = obj.getDoseConstraintJacobianStructure(numel(cst{i,4}{1}));
nRows = size(jacobDoseStruct,2);
jacobStruct = [jacobStruct; repmat(spones(mean(dij.physicalDose{1}(cst{i,4}{1},:))),nRows,1)];
jacobStruct = [jacobStruct; repmat(spones(mean(dij.physicalDose{1}(cst{i,4}{1},:),1)),nRows,1)];

end
end
Expand Down
2 changes: 1 addition & 1 deletion plotting/matRad_plotVoiContourSlice.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
C = cst{s,7}{slice,plane};
else
%If we do not have precomputed contours available, then compute them
mask = zeros(size(ct{ctIndex}));
mask = zeros(ct.cubeDim);
mask(cst{s,4}{ctIndex}) = 1;

if plane == 1 && any(any(mask(slice,:,:) > 0))
Expand Down
2 changes: 1 addition & 1 deletion tools/matRad_plotSliceWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

%plot VOI contours
if ~isempty(cst)
hContour = matRad_plotVoiContourSlice(axesHandle,cst,ct.cubeHU,cubeIdx,voiSelection,plane,slice,contourColorMap,varargin{:});
hContour = matRad_plotVoiContourSlice(axesHandle,cst,ct,cubeIdx,voiSelection,plane,slice,contourColorMap,varargin{:});

if boolPlotLegend
visibleOnSlice = (~cellfun(@isempty,hContour));
Expand Down

0 comments on commit beaee4e

Please sign in to comment.