Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions @MappedTensor/arrayfun.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,13 @@
end

% handle early return with single chunk
if bEarlyReturn && ~isempty(tData) && any(tData)
mtNewVar = tData;
return;
if bEarlyReturn
if ~isempty(tData) && any(tData(:))
mtNewVar = tData;
return;
else
continue;
end
end

% - Write results
Expand Down
10 changes: 7 additions & 3 deletions @MappedTensor/arrayfun2.m
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,13 @@
end

% handle early return with single chunk
if bEarlyReturn && ~isempty(tData1) && any(tData1)
mtNewVar = tData1;
return;
if bEarlyReturn
if ~isempty(tData1) && any(tData1)
mtNewVar = tData1;
return;
else
continue;
end
end

% - Write results
Expand Down
37 changes: 23 additions & 14 deletions @MappedTensor/load.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
% | Extension | Description |
% |-------------------|---------------------------|
% | EDF | ESRF Data Format |
% | POS | Atom Probe Tomography |
% | NPY | Python NumPy array |
% | IMG SMV | ADSC X-ray detector image |
% | MAR MCCD | MAR CCD image |
% | MRC MAP CCP4 RES | MRC MRC/CCP4/MAP electronic density map |
% | MAR | MAR CCD image |
% | IMG MCCD | ADSC X-ray detector image |
% | NRRD | Nearly Raw Raster Data |
% | NPY | Python NumPy array |
% | POS | Atom Probe Tomography |
% | VOL (PAR) | PyHST2 volume (tomography)|
%
%
% Not implemented
% VOL + PAR PyHST2 volume reconstruction
Expand Down Expand Up @@ -77,20 +80,26 @@
args = {}; header = [];
switch upper(e)
case '.EDF'
[Descr,args] = private_load_edf(filename);
case '.POS'
[Descr,args] = private_load_pos(filename);
case '.NPY'
[Descr,args,header] = private_load_npy(filename);
case {'.MRC','.MAP','.CCP4','.RES'}
[Descr,args,header] = private_load_mrc(filename);
[Descr,args,header] = private_load_edf(filename);
case '.IMG'
% ADSC (IMG/SMV)
[Descr,args,header] = private_load_smv(filename);
% could be a Mayo Clinic Analyze when supported...
case {'.MAR','.MCCD'}
[Descr,args,header] = private_load_mar(filename);
case {'.MRC','.MAP','.CCP4','.RES'}
[Descr,args,header] = private_load_mrc(filename);
case '.NPY'
[Descr,args,header] = private_load_npy(filename);
case '.NRRD'
[Descr,args,header] = private_load_nrrd(filename);
case '.POS'
[Descr,args,header] = private_load_pos(filename);
case '.SMV' % ADSC (IMG/SMV)
[Descr,args,header] = private_load_smv(filename);
case '.IMG'
% ADSC (IMG/SMV)
[Descr,args,header] = private_load_smv(filename);
case '.VOL'
[Descr,args,header] = private_load_vol(filename);

otherwise
error([ mfilename ': unsupported file type ' upper(e) ' for ' filename ]);
end
Expand Down
22 changes: 18 additions & 4 deletions @MappedTensor/plot.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
% H = PLOT(M) plots the array M as a vector, matrix/surface, or volume.
% Higher dimension tensors plot a projection.
%
% PLOT(...,'log') transforms the signal in log-scale before plotting.
%
% PLOT(..., 'PROP1',VALUE1,...) uses specified plot properties.
%
% Example: h=plot(MappedTensor(rand(100))); close(gcf); 1

h = [];
Expand All @@ -22,6 +26,16 @@
end
signal = squeeze(double(signal));

% handle special 'log' option
for index=1:numel(varargin)
if ischar(varargin{index}) && strcmp(varargin{index}, 'log')
varargin(index) = [];
signal = signal - min(signal(:));
signal(signal <=0) = nan;
signal = log10(signal);
end
end

if ~isempty(inputname(1))
iname = inputname(1);
else
Expand All @@ -39,9 +53,9 @@

% call the single plot method
try
h = iFunc_plot(name, signal);
h = single_plot(name, signal, varargin{:});
if ~isempty(h)
h = iFunc_plot_menu(h, a, name);
h = single_plot_menu(h, a, name);
end
catch ME
warning(getReport(ME))
Expand All @@ -51,7 +65,7 @@

% ------------------------------------------------------------------------------
% simple plot of the array
function h=iFunc_plot(name, signal, varargin)
function h=single_plot(name, signal, varargin)
% this internal function plots a single model, 1D, 2D or 3D.
h=[];
if isempty(signal) || isscalar(signal)
Expand All @@ -74,7 +88,7 @@
set(h, 'DisplayName', name);

%-------------------------------------------------------------------------------
function h=iFunc_plot_menu(h, a, name)
function h=single_plot_menu(h, a, name)
% contextual menu for the single object being displayed
% internal functions must be avoided as it uses LOTS of memory

Expand Down
214 changes: 214 additions & 0 deletions @MappedTensor/private/private_load_nrrd.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
function [Descr, args, header] = private_load_nrrd(filename)
% PRIVATE_LOAD_NRRD Read NRRD Nearly Raw Raster Data.
%
% A NRRD file is a simple binary block, preceeded by a text header until an
% empty line.
%
% See http://teem.sourceforge.net/nrrd/format.html

Descr=''; args = [];

header = nrrdread(filename);

Descr= 'Nearly Raw Raster Data';
args = {
'Offset', header.Offset, ...
'Format', header.Format, ...
'MachineFormat', header.MachineFormat, ...
'Dimension' , header.Dimension' };

% ------------------------------------------------------------------------------
% https://www.mathworks.com/matlabcentral/fileexchange/34653-nrrd-format-file-reader
% ------------------------------------------------------------------------------

% Copyright (c) 2016, The MathWorks, Inc.
% All rights reserved.

% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:

% * Redistributions of source code must retain the above copyright notice, this
% list of conditions and the following disclaimer.

% * Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
% * In all cases, the software is, and all modifications and derivatives of the
% software shall be, licensed to you solely for use in conjunction with
% MathWorks products and service offerings.

% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function [meta] = nrrdread(filename)
%NRRDREAD Import NRRD imagery and metadata.
% [X, META] = NRRDREAD(FILENAME) reads the image volume and associated
% metadata from the NRRD-format file specified by FILENAME.
%
% Current limitations/caveats:
% * "Block" datatype is not supported.
% * Only tested with "gzip" and "raw" file encodings.
% * Very limited testing on actual files.
% * I only spent a couple minutes reading the NRRD spec.
%
% See the format specification online:
% http://teem.sourceforge.net/nrrd/format.html
% Copyright 2012 The MathWorks, Inc.
% Open file.
fid = fopen(filename, 'rb');
assert(fid > 0, 'Could not open file.');
cleaner = onCleanup(@() fclose(fid));
% Magic line.
theLine = fgetl(fid);
assert(numel(theLine) >= 4, 'Bad signature in file.')
assert(isequal(theLine(1:4), 'NRRD'), 'Bad signature in file.')
% The general format of a NRRD file (with attached header) is:
%
% NRRD000X
% <field>: <desc>
% <field>: <desc>
% # <comment>
% ...
% <field>: <desc>
% <key>:=<value>
% <key>:=<value>
% <key>:=<value>
% # <comment>
%
% <data><data><data><data><data><data>...
meta = struct([]);
% Parse the file a line at a time.
while (true)
theLine = fgetl(fid);

if (isempty(theLine) || feof(fid))
% End of the header.
break;
end

if (isequal(theLine(1), '#'))
% Comment line.
continue;
end

% "fieldname:= value" or "fieldname: value" or "fieldname:value"
parsedLine = regexp(theLine, ':=?\s*', 'split','once');

assert(numel(parsedLine) == 2, 'Parsing error')

field = lower(parsedLine{1});
value = parsedLine{2};

field(isspace(field)) = '';
meta(1).(field) = value;

end
datatype = getDatatype(meta.type);
% Get the size of the data.
assert(isfield(meta, 'sizes') && ...
isfield(meta, 'encoding') , ...
'Missing required metadata fields.')
dims = sscanf(meta.sizes, '%d');
if isfield(meta,'dimension')
ndims = sscanf(meta.dimension, '%d');
assert(numel(dims) == ndims);
end
meta.Offset = ftell(fid);
%data = readData(fid, meta, datatype);
%data = adjustEndian(data, meta);
% Reshape and get into MATLAB's order.
meta.Dimension = dims;
%X = reshape(data, dims');
%X = permute(X, [2 1 3]);

% prepare for MappedTensor
meta.Format = datatype;
meta.MachineFormat = '';
if isfield(meta, 'endian')
switch meta.endian
case {'little','L','le'}
meta.MachineFormat = 'ieee-le';
case {'big','B','be'}
meta.MachineFormat = 'ieee-be';
end
end

% ------------------------------------------------------------------------------

function datatype = getDatatype(metaType)
% Determine the datatype
switch (metaType)
case {'signed char', 'int8', 'int8_t'}
datatype = 'int8';

case {'uchar', 'unsigned char', 'uint8', 'uint8_t'}
datatype = 'uint8';
case {'short', 'short int', 'signed short', 'signed short int', ...
'int16', 'int16_t'}
datatype = 'int16';

case {'ushort', 'unsigned short', 'unsigned short int', 'uint16', ...
'uint16_t'}
datatype = 'uint16';

case {'int', 'signed int', 'int32', 'int32_t'}
datatype = 'int32';

case {'uint', 'unsigned int', 'uint32', 'uint32_t'}
datatype = 'uint32';

case {'longlong', 'long long', 'long long int', 'signed long long', ...
'signed long long int', 'int64', 'int64_t'}
datatype = 'int64';

case {'ulonglong', 'unsigned long long', 'unsigned long long int', ...
'uint64', 'uint64_t'}
datatype = 'uint64';

case {'float'}
datatype = 'single';

case {'double'}
datatype = 'double';

otherwise
assert(false, 'Unknown datatype')
end


function data = readData(fidIn, meta, datatype)
switch (meta.encoding)
case {'raw'}

data = fread(fidIn, inf, [datatype '=>' datatype]);

case {'gzip', 'gz'}
error('Compressed NRRD is not supported')

case {'txt', 'text', 'ascii'}

data = fscanf(fidIn, '%f');
data = cast(data, datatype);

otherwise
assert(false, 'Unsupported encoding')
end


function data = adjustEndian(data, meta)
if ~isfield(meta, 'endian'), return; end
[~,~,endian] = computer();
needToSwap = (isequal(endian, 'B') && isequal(lower(meta.endian), 'little')) || ...
(isequal(endian, 'L') && isequal(lower(meta.endian), 'big'));

if (needToSwap)
data = swapbytes(data);
end
Loading