Skip to content

Commit

Permalink
iData: fix in save images (ndims=2 'data' get raw image/no axes)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhi committed Mar 21, 2017
1 parent 7e7e331 commit 0d2e1c6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 19 deletions.
17 changes: 9 additions & 8 deletions Objects/@iData/private/iData_private_cleannaninf.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
function s=iData_private_cleannaninf(s)
function s=iData_private_cleannaninf(s, ratio)
% iData_private_cleannaninf: clean NaNs and Infs from a numerical field
%

if nargin < 2, ratio=10; end
if isa(s,'iData')
if numel(s) > 1
a = [];
for index=1:numel(s)
a = [ a ; feval(mfilename, s(index)) ];
a = [ a ; feval(mfilename, s(index), ratio) ];
end
s = a;
else
s=set(s,'Signal',iData_private_cleannaninf(get(s,'Signal')));
s=set(s,'Error', iData_private_cleannaninf(get(s,'Error')));
s=set(s,'Signal',iData_private_cleannaninf(get(s,'Signal'), ratio));
s=set(s,'Error', iData_private_cleannaninf(get(s,'Error'), ratio));
end
return
end
Expand All @@ -26,12 +27,12 @@

S(isnan(S)) = 0;
if ~isempty(mins)
if mins<0, S(find(S == -Inf)) = mins*100;
else S(find(S == -Inf)) = mins/100; end
if mins<0, S(find(S == -Inf)) = mins*ratio;
else S(find(S == -Inf)) = mins/ratio; end
end
if ~isempty(maxs)
if maxs>0, S(find(S == +Inf)) = maxs*100;
else S(find(S == +Inf)) = maxs/100; end
if maxs>0, S(find(S == +Inf)) = maxs*ratio;
else S(find(S == +Inf)) = maxs/ratio; end
end
s = double(reshape(S, size(s)));
end
1 change: 1 addition & 0 deletions Objects/@iData/private/iData_private_saveas_analyze.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
dim=size(a);
siz=zeros(size(dim));

a = iData_private_cleannaninf(a);
x=getaxis(a,1);
y=getaxis(a,2);
z=getaxis(a,3);
Expand Down
23 changes: 19 additions & 4 deletions Objects/@iData/private/iData_private_saveas_html.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@
% create output from the figure: png pdf fig
basename = fullfile(target, 'img', [ 'iFit_DataSet_' a.Tag ]);
basename_img = fullfile('img', [ 'iFit_DataSet_' a.Tag ]);
saveas(f, basename, 'png');
if ndims(a) == 2
saveas(a, basename, 'png data'); % just the image for 2D data sets, else getframe
elseif ndims(a) == 3
saveas(a, basename, 'png', 'tight view3');
else
saveas(a, basename, 'png', 'tight');
end

% only export when not in 'data' mode (flat HTML with minimal content)
if isempty(strfind(format, 'data')) && isempty(strfind(format, 'flat'))
Expand All @@ -132,18 +138,19 @@

% export object into a number of usable formats
% 1D: mat dat/data hdf5/mantid png json xml yaml nc fig pdf svg
% 2D: mat dat/data hdf5/mantid png json xml yaml nc fig pdf svg
% 2D: mat dat/data hdf5/mantid png json xml yaml nc fig pdf svg fits
% 3D: mat dat/data hdf5/mantid png json/data xml/data yaml/data nc fig pdf vtk mrc nc
% nD: mat dat/data hdf5/mantid png json/data xml/data yaml/data nc
export = {'mat','dat data','hdf mantid', 'json','xml','yaml','nc' };
export = {'mat','dat data','hdf mantid', 'json','xml','yaml','nc','tiff' };
export_label = { ...
'Matlab binary file. Open with Matlab or <a href="http://ifit.mccode.org">iFit</a>.', ...
'Flat text file which contains axes and the data set. You will have to reshape the matrix after reading the contents. View with any text editor.', ...
'<a href="http://www.hdfgroup.org/">NeXus/HDF5</a> data file, to be opened with e.g. <a href="http://www.mantidproject.org/Main_Page">Mantid</a>, <a href="http://www.hdfgroup.org/hdf-java-html/hdfview">hdfview</a> or <a href="http://ifit.mccode.org">iFit</a>.', ...
'<a href="http://en.wikipedia.org/wiki/JSON">JavaScript Object Notation</a>, to be opened with e.g. JSONView Chrome/Firefox plugin and text editors.', ...
'<a href="http://www.w3.org/XML/">Extensible Markup Language</a> file, to be opened with e.g. Chrome/Firefox and text editors.', ...
'<a href="http://en.wikipedia.org/wiki/YAML">YAML</a> interchange format, to be viewed with e.g. text editors.', ...
'<a href="http://www.unidata.ucar.edu/software/netcdf/">NetCDF</a> binary file, to be viewed with <a href="http://meteora.ucsd.edu/~pierce/ncview_home_page.html">ncview</a> and <a href="http://www.hdfgroup.org/hdf-java-html/hdfview">hdfview</a>.'};
'<a href="http://www.unidata.ucar.edu/software/netcdf/">NetCDF</a> binary file, to be viewed with <a href="http://meteora.ucsd.edu/~pierce/ncview_home_page.html">ncview</a> and <a href="http://www.hdfgroup.org/hdf-java-html/hdfview">hdfview</a>.', ...
'<a href="https://en.wikipedia.org/wiki/TIFF">TIFF</a> image file, to be viewed with e.g. <a href="http://rsb.info.nih.gov/ij/">ImageJ</a>, <a href="http://www.gimp.org/">GIMP.</a>.'};

% add 'data' keyword when the object memory size is larger than 10 Mb
w = whos('a');
Expand All @@ -165,6 +172,11 @@
'Extensible Web page with embeded viewer (X3DOM), to be viewed with Chrome/Firefox.', ...
'X3D Geometry Scene for <a href="http://castle-engine.sourceforge.net/view3dscene.php">view3dscene</a>, <a href="http://www.instantreality.org/">InstantPlayer</a>, <a href="http://freewrl.sourceforge.net/">FreeWRL</a>' ];
end
if ndims(a) == 2
export = [ export 'fits' ];
export_label = [ export_label ...
'Flexible Image Transport System (<a href="https://fits.gsfc.nasa.gov/fits_home.html">FITS</a>) image, which can be viewed with e.g. <a href="http://rsb.info.nih.gov/ij/">ImageJ</a>, <a href="http://www.gimp.org/">GIMP.</a>.' ];
end
if isempty(strfind(format, 'data')) && isempty(strfind(format, 'flat'))
for index=1:numel(export)
f = export{index};
Expand All @@ -190,6 +202,9 @@
fprintf(fid, '<div style="text-align: center;"><a href="%s"><img src="%s" align="middle"></a><br>\n<i>Data: %s</i><br></div>\n', ...
[ basename_img '.png' ], ...
[ basename_img '.png' ], titl);
if ndims(a) == 2
fprintf(fid, '(try the <a href="%s">TIFF file</a> in case the axes are not shown)<br>\n', [ basename_img '.tiff' ]);
end
if ~isempty(m)
fprintf(fid, '<div style="text-align: center;"><i>Model: %s</i><br></div>\n', m.Name);
end
Expand Down
2 changes: 1 addition & 1 deletion Objects/@iData/private/iData_private_saveas_nii.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
% Jimmy Shen 23 Oct 2005 (Updated 22 Jan 2014)
% make_nii, save_nii


a = iData_private_cleannaninf(a);
nii = make_nii(getaxis(a, 0),[], [], [], char(a));
% nii.hdr.hist.descrip = char(a);
save_nii(nii, filename);
Expand Down
1 change: 1 addition & 0 deletions Objects/@iData/private/iData_private_saveas_stl.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
if ndims(a) == 1 iData_private_warning(mfilename,[ 'Object ' inputname(1) ' ' a.Tag ' 1D does not seem to be exportatble as a ' format ' file. Ignoring.' ]);
return
else
a = iData_private_cleannaninf(a);
if ndims(a) == 2
[x, xlab] = getaxis(a,2); x=double(x);
[y, ylab] = getaxis(a,1); y=double(y);
Expand Down
1 change: 1 addition & 0 deletions Objects/@iData/private/iData_private_saveas_vtk.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
return
end

a = iData_private_cleannaninf(a);
str=char(a);

if ndims(a) <= 2 && isvector(a) % line, 1D or 2D
Expand Down
2 changes: 2 additions & 0 deletions Objects/@iData/private/iData_private_saveas_x3d.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
% export as a X3D/XHTML file
% option can contain: axes (display axes), auto to rescale axes in range [0,1]
% for aspect ratio 1.

titl = char(a);
titl(titl=='<')='[';
titl(titl=='>')=']';
desc = evalc('disp(a)');
desc(desc=='<')='[';
desc(desc=='>')=']';
a = reducevolume(a);
a = iData_private_cleannaninf(a);
x1=min(a); x2=max(a);
if ndims(a) <= 2
f=figure('visible','off');
Expand Down
13 changes: 7 additions & 6 deletions Objects/@iData/saveas.m
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@
case 'hdr' % Analyze volume
filename = iData_private_saveas_analyze(a, filename);
case 'mrc' % MRC map file
WriteMRC(getaxis(a,0),1,filename); % in private
WriteMRC(getaxis(iData_private_cleannaninf(a),0),1,filename); % in private
case {'fits','fit','fts'} % FITS image
if ndims(a) == 2
a = double(a);
fitswrite(a, filename);
b = real(double(iData_private_cleannaninf(a,1))); % Signal/Monitor
fitswrite(b', filename);
else
disp([ mfilename ': Export into ' format ' is only possible for 2D objects, not for ' num2str(ndims(a)) 'D. Use resize to change dimensionality. Ignoring.' ])
end
Expand All @@ -340,9 +340,10 @@
case 'csv' % Spreadsheet comma separated values file format
csvwrite(filename, double(a));
case {'gif','bmp','pbm','pcx','pgm','pnm','ppm','ras','xwd','hdf4','tiff','png','art'} % bitmap images
if ndims(a) == 2
b=getaxis(a,0); % Signal/Monitor
b=round((b-min(b(:)))/(max(b(:))-min(b(:)))*256);
if ndims(a) == 2 && ~isempty(root) % 'data' option
b = real(double(iData_private_cleannaninf(a,1)));
b = round((b-min(b(:)))/(max(b(:))-min(b(:)))*256);
b = flipud(b);
else
f = getframe(a,[],options);
b = f.cdata;
Expand Down

0 comments on commit 0d2e1c6

Please sign in to comment.