plateform
stringclasses
1 value
repo_name
stringlengths
13
113
name
stringlengths
3
74
ext
stringclasses
1 value
path
stringlengths
12
229
size
int64
23
843k
source_encoding
stringclasses
9 values
md5
stringlengths
32
32
text
stringlengths
23
843k
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/5 bias vs variance linear regression/ex5/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/5 bias vs variance linear regression/ex5/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/submit.m
1,567
utf_8
1dba733a05282b2db9f2284548483b81
function submit() addpath('./lib'); conf.assignmentSlug = 'multi-class-classification-and-neural-networks'; conf.itemName = 'Multi-class Classification and Neural Networks'; conf.partArrays = { ... { ... '1', ... { 'lrCostFunction.m' }, ... 'Regularized Logistic Regression', ... }, ... { ... '2', ... { 'oneVsAll.m' }, ... 'One-vs-All Classifier Training', ... }, ... { ... '3', ... { 'predictOneVsAll.m' }, ... 'One-vs-All Classifier Prediction', ... }, ... { ... '4', ... { 'predict.m' }, ... 'Neural Network Prediction Function' ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxdata) % Random Test Cases X = [ones(20,1) (exp(1) * sin(1:1:20))' (exp(0.5) * cos(1:1:20))']; y = sin(X(:,1) + X(:,2)) > 0; Xm = [ -1 -1 ; -1 -2 ; -2 -1 ; -2 -2 ; ... 1 1 ; 1 2 ; 2 1 ; 2 2 ; ... -1 1 ; -1 2 ; -2 1 ; -2 2 ; ... 1 -1 ; 1 -2 ; -2 -1 ; -2 -2 ]; ym = [ 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 ]'; t1 = sin(reshape(1:2:24, 4, 3)); t2 = cos(reshape(1:2:40, 4, 5)); if partId == '1' [J, grad] = lrCostFunction([0.25 0.5 -0.5]', X, y, 0.1); out = sprintf('%0.5f ', J); out = [out sprintf('%0.5f ', grad)]; elseif partId == '2' out = sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1)); elseif partId == '3' out = sprintf('%0.5f ', predictOneVsAll(t1, Xm)); elseif partId == '4' out = sprintf('%0.5f ', predict(t1, t2, Xm)); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/3 neural nets/ex3/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/submit.m
2,064
utf_8
7c4fcf60df3a7e09d05a74f7772fed3b
function submit() addpath('./lib'); conf.assignmentSlug = 'anomaly-detection-and-recommender-systems'; conf.itemName = 'Anomaly Detection and Recommender Systems'; conf.partArrays = { ... { ... '1', ... { 'estimateGaussian.m' }, ... 'Estimate Gaussian Parameters', ... }, ... { ... '2', ... { 'selectThreshold.m' }, ... 'Select Threshold', ... }, ... { ... '3', ... { 'cofiCostFunc.m' }, ... 'Collaborative Filtering Cost', ... }, ... { ... '4', ... { 'cofiCostFunc.m' }, ... 'Collaborative Filtering Gradient', ... }, ... { ... '5', ... { 'cofiCostFunc.m' }, ... 'Regularized Cost', ... }, ... { ... '6', ... { 'cofiCostFunc.m' }, ... 'Regularized Gradient', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxstring) % Random Test Cases n_u = 3; n_m = 4; n = 5; X = reshape(sin(1:n_m*n), n_m, n); Theta = reshape(cos(1:n_u*n), n_u, n); Y = reshape(sin(1:2:2*n_m*n_u), n_m, n_u); R = Y > 0.5; pval = [abs(Y(:)) ; 0.001; 1]; yval = [R(:) ; 1; 0]; params = [X(:); Theta(:)]; if partId == '1' [mu sigma2] = estimateGaussian(X); out = sprintf('%0.5f ', [mu(:); sigma2(:)]); elseif partId == '2' [bestEpsilon bestF1] = selectThreshold(yval, pval); out = sprintf('%0.5f ', [bestEpsilon(:); bestF1(:)]); elseif partId == '3' [J] = cofiCostFunc(params, Y, R, n_u, n_m, ... n, 0); out = sprintf('%0.5f ', J(:)); elseif partId == '4' [J, grad] = cofiCostFunc(params, Y, R, n_u, n_m, ... n, 0); out = sprintf('%0.5f ', grad(:)); elseif partId == '5' [J] = cofiCostFunc(params, Y, R, n_u, n_m, ... n, 1.5); out = sprintf('%0.5f ', J(:)); elseif partId == '6' [J, grad] = cofiCostFunc(params, Y, R, n_u, n_m, ... n, 1.5); out = sprintf('%0.5f ', grad(:)); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/8 Anomaly detection and recommender system/ex8/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/submit.m
1,318
utf_8
bfa0b4ffb8a7854d8e84276e91818107
function submit() addpath('./lib'); conf.assignmentSlug = 'support-vector-machines'; conf.itemName = 'Support Vector Machines'; conf.partArrays = { ... { ... '1', ... { 'gaussianKernel.m' }, ... 'Gaussian Kernel', ... }, ... { ... '2', ... { 'dataset3Params.m' }, ... 'Parameters (C, sigma) for Dataset 3', ... }, ... { ... '3', ... { 'processEmail.m' }, ... 'Email Preprocessing', ... }, ... { ... '4', ... { 'emailFeatures.m' }, ... 'Email Feature Extraction', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxstring) % Random Test Cases x1 = sin(1:10)'; x2 = cos(1:10)'; ec = 'the quick brown fox jumped over the lazy dog'; wi = 1 + abs(round(x1 * 1863)); wi = [wi ; wi]; if partId == '1' sim = gaussianKernel(x1, x2, 2); out = sprintf('%0.5f ', sim); elseif partId == '2' load('ex6data3.mat'); [C, sigma] = dataset3Params(X, y, Xval, yval); out = sprintf('%0.5f ', C); out = [out sprintf('%0.5f ', sigma)]; elseif partId == '3' word_indices = processEmail(ec); out = sprintf('%d ', word_indices); elseif partId == '4' x = emailFeatures(wi); out = sprintf('%d ', x); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
porterStemmer.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/porterStemmer.m
9,902
utf_8
7ed5acd925808fde342fc72bd62ebc4d
function stem = porterStemmer(inString) % Applies the Porter Stemming algorithm as presented in the following % paper: % Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14, % no. 3, pp 130-137 % Original code modeled after the C version provided at: % http://www.tartarus.org/~martin/PorterStemmer/c.txt % The main part of the stemming algorithm starts here. b is an array of % characters, holding the word to be stemmed. The letters are in b[k0], % b[k0+1] ending at b[k]. In fact k0 = 1 in this demo program (since % matlab begins indexing by 1 instead of 0). k is readjusted downwards as % the stemming progresses. Zero termination is not in fact used in the % algorithm. % To call this function, use the string to be stemmed as the input % argument. This function returns the stemmed word as a string. % Lower-case string inString = lower(inString); global j; b = inString; k = length(b); k0 = 1; j = k; % With this if statement, strings of length 1 or 2 don't go through the % stemming process. Remove this conditional to match the published % algorithm. stem = b; if k > 2 % Output displays per step are commented out. %disp(sprintf('Word to stem: %s', b)); x = step1ab(b, k, k0); %disp(sprintf('Steps 1A and B yield: %s', x{1})); x = step1c(x{1}, x{2}, k0); %disp(sprintf('Step 1C yields: %s', x{1})); x = step2(x{1}, x{2}, k0); %disp(sprintf('Step 2 yields: %s', x{1})); x = step3(x{1}, x{2}, k0); %disp(sprintf('Step 3 yields: %s', x{1})); x = step4(x{1}, x{2}, k0); %disp(sprintf('Step 4 yields: %s', x{1})); x = step5(x{1}, x{2}, k0); %disp(sprintf('Step 5 yields: %s', x{1})); stem = x{1}; end % cons(j) is TRUE <=> b[j] is a consonant. function c = cons(i, b, k0) c = true; switch(b(i)) case {'a', 'e', 'i', 'o', 'u'} c = false; case 'y' if i == k0 c = true; else c = ~cons(i - 1, b, k0); end end % mseq() measures the number of consonant sequences between k0 and j. If % c is a consonant sequence and v a vowel sequence, and <..> indicates % arbitrary presence, % <c><v> gives 0 % <c>vc<v> gives 1 % <c>vcvc<v> gives 2 % <c>vcvcvc<v> gives 3 % .... function n = measure(b, k0) global j; n = 0; i = k0; while true if i > j return end if ~cons(i, b, k0) break; end i = i + 1; end i = i + 1; while true while true if i > j return end if cons(i, b, k0) break; end i = i + 1; end i = i + 1; n = n + 1; while true if i > j return end if ~cons(i, b, k0) break; end i = i + 1; end i = i + 1; end % vowelinstem() is TRUE <=> k0,...j contains a vowel function vis = vowelinstem(b, k0) global j; for i = k0:j, if ~cons(i, b, k0) vis = true; return end end vis = false; %doublec(i) is TRUE <=> i,(i-1) contain a double consonant. function dc = doublec(i, b, k0) if i < k0+1 dc = false; return end if b(i) ~= b(i-1) dc = false; return end dc = cons(i, b, k0); % cvc(j) is TRUE <=> j-2,j-1,j has the form consonant - vowel - consonant % and also if the second c is not w,x or y. this is used when trying to % restore an e at the end of a short word. e.g. % % cav(e), lov(e), hop(e), crim(e), but % snow, box, tray. function c1 = cvc(i, b, k0) if ((i < (k0+2)) || ~cons(i, b, k0) || cons(i-1, b, k0) || ~cons(i-2, b, k0)) c1 = false; else if (b(i) == 'w' || b(i) == 'x' || b(i) == 'y') c1 = false; return end c1 = true; end % ends(s) is TRUE <=> k0,...k ends with the string s. function s = ends(str, b, k) global j; if (str(length(str)) ~= b(k)) s = false; return end % tiny speed-up if (length(str) > k) s = false; return end if strcmp(b(k-length(str)+1:k), str) s = true; j = k - length(str); return else s = false; end % setto(s) sets (j+1),...k to the characters in the string s, readjusting % k accordingly. function so = setto(s, b, k) global j; for i = j+1:(j+length(s)) b(i) = s(i-j); end if k > j+length(s) b((j+length(s)+1):k) = ''; end k = length(b); so = {b, k}; % rs(s) is used further down. % [Note: possible null/value for r if rs is called] function r = rs(str, b, k, k0) r = {b, k}; if measure(b, k0) > 0 r = setto(str, b, k); end % step1ab() gets rid of plurals and -ed or -ing. e.g. % caresses -> caress % ponies -> poni % ties -> ti % caress -> caress % cats -> cat % feed -> feed % agreed -> agree % disabled -> disable % matting -> mat % mating -> mate % meeting -> meet % milling -> mill % messing -> mess % meetings -> meet function s1ab = step1ab(b, k, k0) global j; if b(k) == 's' if ends('sses', b, k) k = k-2; elseif ends('ies', b, k) retVal = setto('i', b, k); b = retVal{1}; k = retVal{2}; elseif (b(k-1) ~= 's') k = k-1; end end if ends('eed', b, k) if measure(b, k0) > 0; k = k-1; end elseif (ends('ed', b, k) || ends('ing', b, k)) && vowelinstem(b, k0) k = j; retVal = {b, k}; if ends('at', b, k) retVal = setto('ate', b(k0:k), k); elseif ends('bl', b, k) retVal = setto('ble', b(k0:k), k); elseif ends('iz', b, k) retVal = setto('ize', b(k0:k), k); elseif doublec(k, b, k0) retVal = {b, k-1}; if b(retVal{2}) == 'l' || b(retVal{2}) == 's' || ... b(retVal{2}) == 'z' retVal = {retVal{1}, retVal{2}+1}; end elseif measure(b, k0) == 1 && cvc(k, b, k0) retVal = setto('e', b(k0:k), k); end k = retVal{2}; b = retVal{1}(k0:k); end j = k; s1ab = {b(k0:k), k}; % step1c() turns terminal y to i when there is another vowel in the stem. function s1c = step1c(b, k, k0) global j; if ends('y', b, k) && vowelinstem(b, k0) b(k) = 'i'; end j = k; s1c = {b, k}; % step2() maps double suffices to single ones. so -ization ( = -ize plus % -ation) maps to -ize etc. note that the string before the suffix must give % m() > 0. function s2 = step2(b, k, k0) global j; s2 = {b, k}; switch b(k-1) case {'a'} if ends('ational', b, k) s2 = rs('ate', b, k, k0); elseif ends('tional', b, k) s2 = rs('tion', b, k, k0); end; case {'c'} if ends('enci', b, k) s2 = rs('ence', b, k, k0); elseif ends('anci', b, k) s2 = rs('ance', b, k, k0); end; case {'e'} if ends('izer', b, k) s2 = rs('ize', b, k, k0); end; case {'l'} if ends('bli', b, k) s2 = rs('ble', b, k, k0); elseif ends('alli', b, k) s2 = rs('al', b, k, k0); elseif ends('entli', b, k) s2 = rs('ent', b, k, k0); elseif ends('eli', b, k) s2 = rs('e', b, k, k0); elseif ends('ousli', b, k) s2 = rs('ous', b, k, k0); end; case {'o'} if ends('ization', b, k) s2 = rs('ize', b, k, k0); elseif ends('ation', b, k) s2 = rs('ate', b, k, k0); elseif ends('ator', b, k) s2 = rs('ate', b, k, k0); end; case {'s'} if ends('alism', b, k) s2 = rs('al', b, k, k0); elseif ends('iveness', b, k) s2 = rs('ive', b, k, k0); elseif ends('fulness', b, k) s2 = rs('ful', b, k, k0); elseif ends('ousness', b, k) s2 = rs('ous', b, k, k0); end; case {'t'} if ends('aliti', b, k) s2 = rs('al', b, k, k0); elseif ends('iviti', b, k) s2 = rs('ive', b, k, k0); elseif ends('biliti', b, k) s2 = rs('ble', b, k, k0); end; case {'g'} if ends('logi', b, k) s2 = rs('log', b, k, k0); end; end j = s2{2}; % step3() deals with -ic-, -full, -ness etc. similar strategy to step2. function s3 = step3(b, k, k0) global j; s3 = {b, k}; switch b(k) case {'e'} if ends('icate', b, k) s3 = rs('ic', b, k, k0); elseif ends('ative', b, k) s3 = rs('', b, k, k0); elseif ends('alize', b, k) s3 = rs('al', b, k, k0); end; case {'i'} if ends('iciti', b, k) s3 = rs('ic', b, k, k0); end; case {'l'} if ends('ical', b, k) s3 = rs('ic', b, k, k0); elseif ends('ful', b, k) s3 = rs('', b, k, k0); end; case {'s'} if ends('ness', b, k) s3 = rs('', b, k, k0); end; end j = s3{2}; % step4() takes off -ant, -ence etc., in context <c>vcvc<v>. function s4 = step4(b, k, k0) global j; switch b(k-1) case {'a'} if ends('al', b, k) end; case {'c'} if ends('ance', b, k) elseif ends('ence', b, k) end; case {'e'} if ends('er', b, k) end; case {'i'} if ends('ic', b, k) end; case {'l'} if ends('able', b, k) elseif ends('ible', b, k) end; case {'n'} if ends('ant', b, k) elseif ends('ement', b, k) elseif ends('ment', b, k) elseif ends('ent', b, k) end; case {'o'} if ends('ion', b, k) if j == 0 elseif ~(strcmp(b(j),'s') || strcmp(b(j),'t')) j = k; end elseif ends('ou', b, k) end; case {'s'} if ends('ism', b, k) end; case {'t'} if ends('ate', b, k) elseif ends('iti', b, k) end; case {'u'} if ends('ous', b, k) end; case {'v'} if ends('ive', b, k) end; case {'z'} if ends('ize', b, k) end; end if measure(b, k0) > 1 s4 = {b(k0:j), j}; else s4 = {b(k0:k), k}; end % step5() removes a final -e if m() > 1, and changes -ll to -l if m() > 1. function s5 = step5(b, k, k0) global j; j = k; if b(k) == 'e' a = measure(b, k0); if (a > 1) || ((a == 1) && ~cvc(k-1, b, k0)) k = k-1; end end if (b(k) == 'l') && doublec(k, b, k0) && (measure(b, k0) > 1) k = k-1; end s5 = {b(k0:k), k};
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/6 support vector machines/ex6/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/submit.m
1,635
utf_8
ae9c236c78f9b5b09db8fbc2052990fc
function submit() addpath('./lib'); conf.assignmentSlug = 'neural-network-learning'; conf.itemName = 'Neural Networks Learning'; conf.partArrays = { ... { ... '1', ... { 'nnCostFunction.m' }, ... 'Feedforward and Cost Function', ... }, ... { ... '2', ... { 'nnCostFunction.m' }, ... 'Regularized Cost Function', ... }, ... { ... '3', ... { 'sigmoidGradient.m' }, ... 'Sigmoid Gradient', ... }, ... { ... '4', ... { 'nnCostFunction.m' }, ... 'Neural Network Gradient (Backpropagation)', ... }, ... { ... '5', ... { 'nnCostFunction.m' }, ... 'Regularized Gradient', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxstring) % Random Test Cases X = reshape(3 * sin(1:1:30), 3, 10); Xm = reshape(sin(1:32), 16, 2) / 5; ym = 1 + mod(1:16,4)'; t1 = sin(reshape(1:2:24, 4, 3)); t2 = cos(reshape(1:2:40, 4, 5)); t = [t1(:) ; t2(:)]; if partId == '1' [J] = nnCostFunction(t, 2, 4, 4, Xm, ym, 0); out = sprintf('%0.5f ', J); elseif partId == '2' [J] = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5); out = sprintf('%0.5f ', J); elseif partId == '3' out = sprintf('%0.5f ', sigmoidGradient(X)); elseif partId == '4' [J, grad] = nnCostFunction(t, 2, 4, 4, Xm, ym, 0); out = sprintf('%0.5f ', J); out = [out sprintf('%0.5f ', grad)]; elseif partId == '5' [J, grad] = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5); out = sprintf('%0.5f ', J); out = [out sprintf('%0.5f ', grad)]; end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/4 neural nets/ex4/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/2 Logistic Regression/ex2/submit.m
1,605
utf_8
9b63d386e9bd7bcca66b1a3d2fa37579
function submit() addpath('./lib'); conf.assignmentSlug = 'logistic-regression'; conf.itemName = 'Logistic Regression'; conf.partArrays = { ... { ... '1', ... { 'sigmoid.m' }, ... 'Sigmoid Function', ... }, ... { ... '2', ... { 'costFunction.m' }, ... 'Logistic Regression Cost', ... }, ... { ... '3', ... { 'costFunction.m' }, ... 'Logistic Regression Gradient', ... }, ... { ... '4', ... { 'predict.m' }, ... 'Predict', ... }, ... { ... '5', ... { 'costFunctionReg.m' }, ... 'Regularized Logistic Regression Cost', ... }, ... { ... '6', ... { 'costFunctionReg.m' }, ... 'Regularized Logistic Regression Gradient', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxstring) % Random Test Cases X = [ones(20,1) (exp(1) * sin(1:1:20))' (exp(0.5) * cos(1:1:20))']; y = sin(X(:,1) + X(:,2)) > 0; if partId == '1' out = sprintf('%0.5f ', sigmoid(X)); elseif partId == '2' out = sprintf('%0.5f ', costFunction([0.25 0.5 -0.5]', X, y)); elseif partId == '3' [cost, grad] = costFunction([0.25 0.5 -0.5]', X, y); out = sprintf('%0.5f ', grad); elseif partId == '4' out = sprintf('%0.5f ', predict([0.25 0.5 -0.5]', X)); elseif partId == '5' out = sprintf('%0.5f ', costFunctionReg([0.25 0.5 -0.5]', X, y, 0.1)); elseif partId == '6' [cost, grad] = costFunctionReg([0.25 0.5 -0.5]', X, y, 0.1); out = sprintf('%0.5f ', grad); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/submit.m
1,876
utf_8
8d1c467b830a89c187c05b121cb8fbfd
function submit() addpath('./lib'); conf.assignmentSlug = 'linear-regression'; conf.itemName = 'Linear Regression with Multiple Variables'; conf.partArrays = { ... { ... '1', ... { 'warmUpExercise.m' }, ... 'Warm-up Exercise', ... }, ... { ... '2', ... { 'computeCost.m' }, ... 'Computing Cost (for One Variable)', ... }, ... { ... '3', ... { 'gradientDescent.m' }, ... 'Gradient Descent (for One Variable)', ... }, ... { ... '4', ... { 'featureNormalize.m' }, ... 'Feature Normalization', ... }, ... { ... '5', ... { 'computeCostMulti.m' }, ... 'Computing Cost (for Multiple Variables)', ... }, ... { ... '6', ... { 'gradientDescentMulti.m' }, ... 'Gradient Descent (for Multiple Variables)', ... }, ... { ... '7', ... { 'normalEqn.m' }, ... 'Normal Equations', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId) % Random Test Cases X1 = [ones(20,1) (exp(1) + exp(2) * (0.1:0.1:2))']; Y1 = X1(:,2) + sin(X1(:,1)) + cos(X1(:,2)); X2 = [X1 X1(:,2).^0.5 X1(:,2).^0.25]; Y2 = Y1.^0.5 + Y1; if partId == '1' out = sprintf('%0.5f ', warmUpExercise()); elseif partId == '2' out = sprintf('%0.5f ', computeCost(X1, Y1, [0.5 -0.5]')); elseif partId == '3' out = sprintf('%0.5f ', gradientDescent(X1, Y1, [0.5 -0.5]', 0.01, 10)); elseif partId == '4' out = sprintf('%0.5f ', featureNormalize(X2(:,2:4))); elseif partId == '5' out = sprintf('%0.5f ', computeCostMulti(X2, Y2, [0.1 0.2 0.3 0.4]')); elseif partId == '6' out = sprintf('%0.5f ', gradientDescentMulti(X2, Y2, [-0.1 -0.2 -0.3 -0.4]', 0.01, 10)); elseif partId == '7' out = sprintf('%0.5f ', normalEqn(X2, Y2)); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/1 Linear Regression with Multiple Variables/ex1/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submit.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/submit.m
1,438
utf_8
665ea5906aad3ccfd94e33a40c58e2ce
function submit() addpath('./lib'); conf.assignmentSlug = 'k-means-clustering-and-pca'; conf.itemName = 'K-Means Clustering and PCA'; conf.partArrays = { ... { ... '1', ... { 'findClosestCentroids.m' }, ... 'Find Closest Centroids (k-Means)', ... }, ... { ... '2', ... { 'computeCentroids.m' }, ... 'Compute Centroid Means (k-Means)', ... }, ... { ... '3', ... { 'pca.m' }, ... 'PCA', ... }, ... { ... '4', ... { 'projectData.m' }, ... 'Project Data (PCA)', ... }, ... { ... '5', ... { 'recoverData.m' }, ... 'Recover Data (PCA)', ... }, ... }; conf.output = @output; submitWithConfiguration(conf); end function out = output(partId, auxstring) % Random Test Cases X = reshape(sin(1:165), 15, 11); Z = reshape(cos(1:121), 11, 11); C = Z(1:5, :); idx = (1 + mod(1:15, 3))'; if partId == '1' idx = findClosestCentroids(X, C); out = sprintf('%0.5f ', idx(:)); elseif partId == '2' centroids = computeCentroids(X, idx, 3); out = sprintf('%0.5f ', centroids(:)); elseif partId == '3' [U, S] = pca(X); out = sprintf('%0.5f ', abs([U(:); S(:)])); elseif partId == '4' X_proj = projectData(X, Z, 5); out = sprintf('%0.5f ', X_proj(:)); elseif partId == '5' X_rec = recoverData(X(:,1:5), Z, 5); out = sprintf('%0.5f ', X_rec(:)); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
submitWithConfiguration.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/lib/submitWithConfiguration.m
3,926
utf_8
f889a7cf3dc6c1c2877566d38df1bec8
function submitWithConfiguration(conf) % Note: has the "certificate" patch from Liran for Windows-like systems addpath('./lib/jsonlab'); %keyboard parts = parts(conf); fprintf('== Submitting solutions | %s...\n', conf.itemName); tokenFile = 'token.mat'; if exist(tokenFile, 'file') load(tokenFile); [email token] = promptToken(email, token, tokenFile); else [email token] = promptToken('', '', tokenFile); end if isempty(token) fprintf('!! Submission Cancelled\n'); return end try response = submitParts(conf, email, token, parts); catch e = lasterror(); fprintf( ... '!! Submission failed: unexpected error: %s\n', ... e.message); fprintf('!! Please try again later.\n'); return end if isfield(response, 'errorMessage') fprintf('!! Submission failed: %s\n', response.errorMessage); else showFeedback(parts, response); save(tokenFile, 'email', 'token'); end end function [email token] = promptToken(email, existingToken, tokenFile) if (~isempty(email) && ~isempty(existingToken)) prompt = sprintf( ... 'Use token from last successful submission (%s)? (Y/n): ', ... email); reenter = input(prompt, 's'); if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y') token = existingToken; return; else delete(tokenFile); end end email = input('Login (email address): ', 's'); token = input('Token: ', 's'); end function isValid = isValidPartOptionIndex(partOptions, i) isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions)); end function response = submitParts(conf, email, token, parts) body = makePostBody(conf, email, token, parts); submissionUrl = submissionUrl(); params = {'jsonBody', body}; %responseBody = urlread(submissionUrl, 'post', params); [code, responseBody] = system(sprintf('echo jsonBody=%s | curl -k -X POST -d @- %s', body, submissionUrl)); response = loadjson(responseBody); end function body = makePostBody(conf, email, token, parts) bodyStruct.assignmentSlug = conf.assignmentSlug; bodyStruct.submitterEmail = email; bodyStruct.secret = token; bodyStruct.parts = makePartsStruct(conf, parts); opt.Compact = 1; body = savejson('', bodyStruct, opt); end function partsStruct = makePartsStruct(conf, parts) for part = parts partId = part{:}.id; fieldName = makeValidFieldName(partId); outputStruct.output = conf.output(partId); partsStruct.(fieldName) = outputStruct; end end function [parts] = parts(conf) parts = {}; for partArray = conf.partArrays part.id = partArray{:}{1}; part.sourceFiles = partArray{:}{2}; part.name = partArray{:}{3}; parts{end + 1} = part; end end function showFeedback(parts, response) fprintf('== \n'); fprintf('== %43s | %9s | %-s\n', 'Part Name', 'Score', 'Feedback'); fprintf('== %43s | %9s | %-s\n', '---------', '-----', '--------'); for part = parts score = ''; partFeedback = ''; partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id)); partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id)); score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore); fprintf('== %43s | %9s | %-s\n', part{:}.name, score, partFeedback); end evaluation = response.evaluation; totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore); fprintf('== --------------------------------\n'); fprintf('== %43s | %9s | %-s\n', '', totalScore, ''); fprintf('== \n'); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Service configuration % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function submissionUrl = submissionUrl() submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1'; end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
savejson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/lib/jsonlab/savejson.m
17,462
utf_8
861b534fc35ffe982b53ca3ca83143bf
function json=savejson(rootname,obj,varargin) % % json=savejson(rootname,obj,filename) % or % json=savejson(rootname,obj,opt) % json=savejson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a JSON (JavaScript % Object Notation) string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09 % % $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array). % filename: a string for the file name to save the output JSON data. % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.FloatFormat ['%.10g'|string]: format to show each numeric element % of a 1D/2D array; % opt.ArrayIndent [1|0]: if 1, output explicit data array with % precedent indentation; if 0, no indentation % opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [0|1]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.Inf ['"$1_Inf_"'|string]: a customized regular expression pattern % to represent +/-Inf. The matched pattern is '([-+]*)Inf' % and $1 represents the sign. For those who want to use % 1e999 to represent Inf, they can set opt.Inf to '$11e999' % opt.NaN ['"_NaN_"'|string]: a customized regular expression pattern % to represent NaN % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSONP='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode. % opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs) % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a string in the JSON format (see http://json.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % savejson('jmesh',jsonmesh) % savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\t%.5g') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end whitespaces=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); if(jsonopt('Compact',0,opt)==1) whitespaces=struct('tab','','newline','','sep',','); end if(~isfield(opt,'whitespaces_')) opt.whitespaces_=whitespaces; end nl=whitespaces.newline; json=obj2json(rootname,obj,rootlevel,opt); if(rootisarray) json=sprintf('%s%s',json,nl); else json=sprintf('{%s%s%s}\n',nl,json,nl); end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=sprintf('%s(%s);%s',jsonp,json,nl); end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) if(jsonopt('SaveBinary',0,opt)==1) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); else fid = fopen(opt.FileName, 'wt'); fwrite(fid,json,'char'); end fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2json(name,item,level,varargin) if(iscell(item)) txt=cell2json(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2json(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2json(name,item,level,varargin{:}); else txt=mat2json(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2json(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=jsonopt('whitespaces_',struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')),varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); nl=ws.newline; if(len>1) if(~isempty(name)) txt=sprintf('%s"%s": [%s',padding0, checkname(name,varargin{:}),nl); name=''; else txt=sprintf('%s[%s',padding0,nl); end elseif(len==0) if(~isempty(name)) txt=sprintf('%s"%s": []',padding0, checkname(name,varargin{:})); name=''; else txt=sprintf('%s[]',padding0); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:})); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=struct2json(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding0=repmat(ws.tab,1,level); padding2=repmat(ws.tab,1,level+1); padding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1)); nl=ws.newline; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding0,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding0,nl); end end for j=1:dim(2) if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=sprintf('%s%s"%s": {%s',txt,padding1, checkname(name,varargin{:}),nl); else txt=sprintf('%s%s{%s',txt,padding1,nl); end if(~isempty(names)) for e=1:length(names) txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})); if(e<length(names)) txt=sprintf('%s%s',txt,','); end txt=sprintf('%s%s',txt,nl); end end txt=sprintf('%s%s}',txt,padding1); if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end end if(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end %%------------------------------------------------------------------------- function txt=str2json(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(~isempty(name)) if(len>1) txt=sprintf('%s"%s": [%s',padding1,checkname(name,varargin{:}),nl); end else if(len>1) txt=sprintf('%s[%s',padding1,nl); end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len if(isoct) val=regexprep(item(e,:),'\\','\\'); val=regexprep(val,'"','\"'); val=regexprep(val,'^"','\"'); else val=regexprep(item(e,:),'\\','\\\\'); val=regexprep(val,'"','\\"'); val=regexprep(val,'^"','\\"'); end val=escapejsonstring(val); if(len==1) obj=['"' checkname(name,varargin{:}) '": ' '"',val,'"']; if(isempty(name)) obj=['"',val,'"']; end txt=sprintf('%s%s%s%s',txt,padding1,obj); else txt=sprintf('%s%s%s%s',txt,padding0,['"',val,'"']); end if(e==len) sep=''; end txt=sprintf('%s%s',txt,sep); end if(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end %%------------------------------------------------------------------------- function txt=mat2json(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); padding1=repmat(ws.tab,1,level); padding0=repmat(ws.tab,1,level+1); nl=ws.newline; sep=ws.sep; if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:})) if(isempty(name)) txt=sprintf('%s{%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); else txt=sprintf('%s"%s": {%s%s"_ArrayType_": "%s",%s%s"_ArraySize_": %s,%s',... padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\s+',','),nl); end else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0) numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\[',''),']',''); else numtxt=matdata2json(item,level+1,varargin{:}); end if(isempty(name)) txt=sprintf('%s%s',padding1,numtxt); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); else txt=sprintf('%s"%s": %s',padding1,checkname(name,varargin{:}),numtxt); end end return; end dataformat='%s%s%s%s%s'; if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); end txt=sprintf(dataformat,txt,padding0,'"_ArrayIsSparse_": ','1', sep); if(size(item,1)==1) % Row vector, store only column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([iy(:),data'],level+2,varargin{:}), nl); elseif(size(item,2)==1) % Column vector, store only row indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,data],level+2,varargin{:}), nl); else % General case, store row and column indices. txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([ix,iy,data],level+2,varargin{:}), nl); end else if(isreal(item)) txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json(item(:)',level+2,varargin{:}), nl); else txt=sprintf(dataformat,txt,padding0,'"_ArrayIsComplex_": ','1', sep); txt=sprintf(dataformat,txt,padding0,'"_ArrayData_": ',... matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl); end end txt=sprintf('%s%s%s',txt,padding1,'}'); %%------------------------------------------------------------------------- function txt=matdata2json(mat,level,varargin) ws=struct('tab',sprintf('\t'),'newline',sprintf('\n'),'sep',sprintf(',\n')); ws=jsonopt('whitespaces_',ws,varargin{:}); tab=ws.tab; nl=ws.newline; if(size(mat,1)==1) pre=''; post=''; level=level-1; else pre=sprintf('[%s',nl); post=sprintf('%s%s]',nl,repmat(tab,1,level-1)); end if(isempty(mat)) txt='null'; return; end floatformat=jsonopt('FloatFormat','%.10g',varargin{:}); %if(numel(mat)>1) formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]]; %else % formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\n')]]; %end if(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1) formatstr=[repmat(tab,1,level) formatstr]; end txt=sprintf(formatstr,mat'); txt(end-length(nl):end)=[]; if(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1) txt=regexprep(txt,'1','true'); txt=regexprep(txt,'0','false'); end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],\n[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end txt=[pre txt post]; if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function newstr=escapejsonstring(str) newstr=str; isoct=exist('OCTAVE_VERSION','builtin'); if(isoct) vv=sscanf(OCTAVE_VERSION,'%f'); if(vv(1)>=3.8) isoct=0; end end if(isoct) escapechars={'\a','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},escapechars{i}); end else escapechars={'\a','\b','\f','\n','\r','\t','\v'}; for i=1:length(escapechars); newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\','\\\\')); end end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/lib/jsonlab/loadjson.m
18,884
ibm852
d21f0844f91f2dbb9ea8df00eda346ca
function data = loadjson(fname,varargin) % % data=loadjson(fname,opt) % or % data=loadjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2011/09/09, including previous works from % % Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713 % created on 2009/11/02 % François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393 % created on 2009/03/22 % Joel Feenstra: % http://www.mathworks.com/matlabcentral/fileexchange/20565 % created on 2008/07/03 % % $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a JSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.FastArrayParser [1|0 or integer]: if set to 1, use a % speed-optimized array parser when loading an % array object. The fast array parser may % collapse block arrays into a single large % array similar to rules defined in cell2mat; 0 to % use a legacy parser; if set to a larger-than-1 % value, this option will specify the minimum % dimension to enable the fast array parser. For % example, if the input is a 3D array, setting % FastArrayParser to 1 will return a 3D array; % setting to 2 will return a cell array of 2D % arrays; setting to 3 will return to a 2D cell % array of 1D vectors; setting to 4 will return a % 3D cell array. % opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % dat=loadjson('{"obj":{"string":"value","array":[1,2,3]}}') % dat=loadjson(['examples' filesep 'example1.json']) % dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); if(jsonopt('ShowProgress',0,opt)==1) opt.progressbar_=waitbar(0,'loading ...'); end jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end if(isfield(opt,'progressbar_')) close(opt.progressbar_); end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=data(j).x0x5F_ArraySize_; if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; if next_char ~= '}' while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end parse_char(':'); val = parse_value(varargin{:}); eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' break; end parse_char(','); end end parse_char('}'); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim2=[]; arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:}); pbar=jsonopt('progressbar_',-1,varargin{:}); if next_char ~= ']' if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:})) [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos); arraystr=['[' inStr(pos:endpos)]; arraystr=regexprep(arraystr,'"_NaN_"','NaN'); arraystr=regexprep(arraystr,'"([-+]*)_Inf_"','$1Inf'); arraystr(arraystr==sprintf('\n'))=[]; arraystr(arraystr==sprintf('\r'))=[]; %arraystr=regexprep(arraystr,'\s*,',','); % this is slow,sometimes needed if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D astr=inStr((e1l+1):(e1r-1)); astr=regexprep(astr,'"_NaN_"','NaN'); astr=regexprep(astr,'"([-+]*)_Inf_"','$1Inf'); astr(astr==sprintf('\n'))=[]; astr(astr==sprintf('\r'))=[]; astr(astr==' ')=''; if(isempty(find(astr=='[', 1))) % array is 2D dim2=length(sscanf(astr,'%f,',[1 inf])); end else % array is 1D astr=arraystr(2:end-1); astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]); if(nextidx>=length(astr)-1) object=obj; pos=endpos; parse_char(']'); return; end end if(~isempty(dim2)) astr=arraystr; astr(astr=='[')=''; astr(astr==']')=''; astr(astr==' ')=''; [obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf); if(nextidx>=length(astr)-1) object=reshape(obj,dim2,numel(obj)/dim2)'; pos=endpos; parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end return; end end arraystr=regexprep(arraystr,'\]\s*,','];'); else arraystr='['; end try if(isoct && regexp(arraystr,'"','once')) error('Octave eval can produce empty cells for JSON-like input'); end object=eval(arraystr); pos=endpos; catch while 1 newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1); val = parse_value(newopt); object{end+1} = val; if next_char == ']' break; end parse_char(','); end end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end parse_char(']'); if(pbar>0) waitbar(pos/length(inStr),pbar,'loading ...'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr len esc index_esc len_esc % len, ns = length(inStr), keyboard if inStr(pos) ~= '"' error_pos('String starting with " expected at position %d'); else pos = pos + 1; end str = ''; while pos <= len while index_esc <= len_esc && esc(index_esc) < pos index_esc = index_esc + 1; end if index_esc > len_esc str = [str inStr(pos:len)]; pos = len + 1; break; else str = [str inStr(pos:esc(index_esc)-1)]; pos = esc(index_esc); end nstr = length(str); switch inStr(pos) case '"' pos = pos + 1; if(~isempty(str)) if(strcmp(str,'_Inf_')) str=Inf; elseif(strcmp(str,'-_Inf_')) str=-Inf; elseif(strcmp(str,'_NaN_')) str=NaN; end end return; case '\' if pos+1 > len error_pos('End of file reached right after escape character'); end pos = pos + 1; switch inStr(pos) case {'"' '\' '/'} str(nstr+1) = inStr(pos); pos = pos + 1; case {'b' 'f' 'n' 'r' 't'} str(nstr+1) = sprintf(['\' inStr(pos)]); pos = pos + 1; case 'u' if pos+4 > len error_pos('End of file reached in escaped unicode character'); end str(nstr+(1:6)) = inStr(pos-1:pos+4); pos = pos + 5; end otherwise % should never happen str(nstr+1) = inStr(pos), keyboard pos = pos + 1; end end error_pos('End of file while expecting end of inStr'); %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct currstr=inStr(pos:end); numstr=0; if(isoct~=0) numstr=regexp(currstr,'^\s*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+\-]?\d+)?','end'); [num, one] = sscanf(currstr, '%f', 1); delta=numstr+1; else [num, one, err, delta] = sscanf(currstr, '%f', 1); if ~isempty(err) error_pos('Error reading number at position %d'); end end pos = pos + delta-1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; pbar=jsonopt('progressbar_',-1,varargin{:}); if(pbar>0) waitbar(pos/len,pbar,'loading ...'); end switch(inStr(pos)) case '"' val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'-','0','1','2','3','4','5','6','7','8','9'} val = parse_number(varargin{:}); return; case 't' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true') val = true; pos = pos + 4; return; end case 'f' if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false') val = false; pos = pos + 5; return; end case 'n' if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null') val = []; pos = pos + 4; return; end end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else % Hard to believe but sprintf("%X") is broken in Octave 4.0.0 % str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); str=sprintf('x0x%s_%s',xxNumToHexStr(char(str(1))),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
loadubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/lib/jsonlab/loadubjson.m
15,574
utf_8
5974e78e71b81b1e0f76123784b951a4
function data = loadubjson(fname,varargin) % % data=loadubjson(fname,opt) % or % data=loadubjson(fname,'param1',value1,'param2',value2,...) % % parse a JSON (JavaScript Object Notation) file or string % % authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/01 % % $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % fname: input file name, if fname contains "{}" or "[]", fname % will be interpreted as a UBJSON string % opt: a struct to store parsing options, opt can be replaced by % a list of ('param',value) pairs - the param string is equivallent % to a field in opt. opt can have the following % fields (first in [.|.] is the default) % % opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat % for each element of the JSON data, and group % arrays based on the cell2mat rules. % opt.IntEndian [B|L]: specify the endianness of the integer fields % in the UBJSON input data. B - Big-Endian format for % integers (as required in the UBJSON specification); % L - input integer fields are in Little-Endian order. % % output: % dat: a cell array, where {...} blocks are converted into cell arrays, % and [...] are converted to arrays % % examples: % obj=struct('string','value','array',[1 2 3]); % ubjdata=saveubjson('obj',obj); % dat=loadubjson(ubjdata) % dat=loadubjson(['examples' filesep 'example1.ubj']) % dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1) % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % global pos inStr len esc index_esc len_esc isoct arraytoken fileendian systemendian if(regexp(fname,'[\{\}\]\[]','once')) string=fname; elseif(exist(fname,'file')) fid = fopen(fname,'rb'); string = fread(fid,inf,'uint8=>char')'; fclose(fid); else error('input file does not exist'); end pos = 1; len = length(string); inStr = string; isoct=exist('OCTAVE_VERSION','builtin'); arraytoken=find(inStr=='[' | inStr==']' | inStr=='"'); jstr=regexprep(inStr,'\\\\',' '); escquote=regexp(jstr,'\\"'); arraytoken=sort([arraytoken escquote]); % String delimiters and escape chars identified to improve speed: esc = find(inStr=='"' | inStr=='\' ); % comparable to: regexp(inStr, '["\\]'); index_esc = 1; len_esc = length(esc); opt=varargin2struct(varargin{:}); fileendian=upper(jsonopt('IntEndian','B',opt)); [os,maxelem,systemendian]=computer; jsoncount=1; while pos <= len switch(next_char) case '{' data{jsoncount} = parse_object(opt); case '[' data{jsoncount} = parse_array(opt); otherwise error_pos('Outer level structure must be an object or an array'); end jsoncount=jsoncount+1; end % while jsoncount=length(data); if(jsoncount==1 && iscell(data)) data=data{1}; end if(~isempty(data)) if(isstruct(data)) % data can be a struct array data=jstruct2array(data); elseif(iscell(data)) data=jcell2array(data); end end %% function newdata=parse_collection(id,data,obj) if(jsoncount>0 && exist('data','var')) if(~iscell(data)) newdata=cell(1); newdata{1}=data; data=newdata; end end %% function newdata=jcell2array(data) len=length(data); newdata=data; for i=1:len if(isstruct(data{i})) newdata{i}=jstruct2array(data{i}); elseif(iscell(data{i})) newdata{i}=jcell2array(data{i}); end end %%------------------------------------------------------------------------- function newdata=jstruct2array(data) fn=fieldnames(data); newdata=data; len=length(data); for i=1:length(fn) % depth-first for j=1:len if(isstruct(getfield(data(j),fn{i}))) newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i}))); end end end if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn))) newdata=cell(len,1); for j=1:len ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_); iscpx=0; if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn))) if(data(j).x0x5F_ArrayIsComplex_) iscpx=1; end end if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn))) if(data(j).x0x5F_ArrayIsSparse_) if(~isempty(strmatch('x0x5F_ArraySize_',fn))) dim=double(data(j).x0x5F_ArraySize_); if(iscpx && size(ndata,2)==4-any(dim==1)) ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end)); end if isempty(ndata) % All-zeros sparse ndata=sparse(dim(1),prod(dim(2:end))); elseif dim(1)==1 % Sparse row vector ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end))); elseif dim(2)==1 % Sparse column vector ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end))); else % Generic sparse array. ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end))); end else if(iscpx && size(ndata,2)==4) ndata(:,3)=complex(ndata(:,3),ndata(:,4)); end ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3)); end end elseif(~isempty(strmatch('x0x5F_ArraySize_',fn))) if(iscpx && size(ndata,2)==2) ndata=complex(ndata(:,1),ndata(:,2)); end ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_); end newdata{j}=ndata; end if(len==1) newdata=newdata{1}; end end %%------------------------------------------------------------------------- function object = parse_object(varargin) parse_char('{'); object = []; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); % TODO pos=pos+2; end if(next_char == '#') pos=pos+1; count=double(parse_number()); end if next_char ~= '}' num=0; while 1 str = parseStr(varargin{:}); if isempty(str) error_pos('Name of value at position %d cannot be empty'); end %parse_char(':'); val = parse_value(varargin{:}); num=num+1; eval( sprintf( 'object.%s = val;', valid_field(str) ) ); if next_char == '}' || (count>=0 && num>=count) break; end %parse_char(','); end end if(count==-1) parse_char('}'); end %%------------------------------------------------------------------------- function [cid,len]=elem_info(type) id=strfind('iUIlLdD',type); dataclass={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; if(id>0) cid=dataclass{id}; len=bytelen(id); else error_pos('unsupported type at position %d'); end %%------------------------------------------------------------------------- function [data adv]=parse_block(type,count,varargin) global pos inStr isoct fileendian systemendian [cid,len]=elem_info(type); datastr=inStr(pos:pos+len*count-1); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end id=strfind('iUIlLdD',type); if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,cid)); end data=typecast(newdata,cid); adv=double(len*count); %%------------------------------------------------------------------------- function object = parse_array(varargin) % JSON array is written in row-major order global pos inStr isoct parse_char('['); object = cell(0, 1); dim=[]; type=''; count=-1; if(next_char == '$') type=inStr(pos+1); pos=pos+2; end if(next_char == '#') pos=pos+1; if(next_char=='[') dim=parse_array(varargin{:}); count=prod(double(dim)); else count=double(parse_number()); end end if(~isempty(type)) if(count>=0) [object adv]=parse_block(type,count,varargin{:}); if(~isempty(dim)) object=reshape(object,dim); end pos=pos+adv; return; else endpos=matching_bracket(inStr,pos); [cid,len]=elem_info(type); count=(endpos-pos)/len; [object adv]=parse_block(type,count,varargin{:}); pos=pos+adv; parse_char(']'); return; end end if next_char ~= ']' while 1 val = parse_value(varargin{:}); object{end+1} = val; if next_char == ']' break; end %parse_char(','); end end if(jsonopt('SimplifyCell',0,varargin{:})==1) try oldobj=object; object=cell2mat(object')'; if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0) object=oldobj; elseif(size(object,1)>1 && ndims(object)==2) object=object'; end catch end end if(count==-1) parse_char(']'); end %%------------------------------------------------------------------------- function parse_char(c) global pos inStr len skip_whitespace; if pos > len || inStr(pos) ~= c error_pos(sprintf('Expected %c at position %%d', c)); else pos = pos + 1; skip_whitespace; end %%------------------------------------------------------------------------- function c = next_char global pos inStr len skip_whitespace; if pos > len c = []; else c = inStr(pos); end %%------------------------------------------------------------------------- function skip_whitespace global pos inStr len while pos <= len && isspace(inStr(pos)) pos = pos + 1; end %%------------------------------------------------------------------------- function str = parseStr(varargin) global pos inStr esc index_esc len_esc % len, ns = length(inStr), keyboard type=inStr(pos); if type ~= 'S' && type ~= 'C' && type ~= 'H' error_pos('String starting with S expected at position %d'); else pos = pos + 1; end if(type == 'C') str=inStr(pos); pos=pos+1; return; end bytelen=double(parse_number()); if(length(inStr)>=pos+bytelen-1) str=inStr(pos:pos+bytelen-1); pos=pos+bytelen; else error_pos('End of file while expecting end of inStr'); end %%------------------------------------------------------------------------- function num = parse_number(varargin) global pos inStr len isoct fileendian systemendian id=strfind('iUIlLdD',inStr(pos)); if(isempty(id)) error_pos('expecting a number at position %d'); end type={'int8','uint8','int16','int32','int64','single','double'}; bytelen=[1,1,2,4,8,4,8]; datastr=inStr(pos+1:pos+bytelen(id)); if(isoct) newdata=int8(datastr); else newdata=uint8(datastr); end if(id<=5 && fileendian~=systemendian) newdata=swapbytes(typecast(newdata,type{id})); end num=typecast(newdata,type{id}); pos = pos + bytelen(id)+1; %%------------------------------------------------------------------------- function val = parse_value(varargin) global pos inStr len true = 1; false = 0; switch(inStr(pos)) case {'S','C','H'} val = parseStr(varargin{:}); return; case '[' val = parse_array(varargin{:}); return; case '{' val = parse_object(varargin{:}); if isstruct(val) if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact'))) val=jstruct2array(val); end elseif isempty(val) val = struct; end return; case {'i','U','I','l','L','d','D'} val = parse_number(varargin{:}); return; case 'T' val = true; pos = pos + 1; return; case 'F' val = false; pos = pos + 1; return; case {'Z','N'} val = []; pos = pos + 1; return; end error_pos('Value expected at position %d'); %%------------------------------------------------------------------------- function error_pos(msg) global pos inStr len poShow = max(min([pos-15 pos-1 pos pos+20],len),1); if poShow(3) == poShow(2) poShow(3:4) = poShow(2)+[0 -1]; % display nothing after end msg = [sprintf(msg, pos) ': ' ... inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ]; error( ['JSONparser:invalidFormat: ' msg] ); %%------------------------------------------------------------------------- function str = valid_field(str) global isoct % From MATLAB doc: field names must begin with a letter, which may be % followed by any combination of letters, digits, and underscores. % Invalid characters will be converted to underscores, and the prefix % "x0x[Hex code]_" will be added if the first character is not a letter. pos=regexp(str,'^[^A-Za-z]','once'); if(~isempty(pos)) if(~isoct) str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once'); else str=sprintf('x0x%X_%s',char(str(1)),str(2:end)); end end if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end if(~isoct) str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_'); else pos=regexp(str,'[^0-9A-Za-z_]'); if(isempty(pos)) return; end str0=str; pos0=[0 pos(:)' length(str)]; str=''; for i=1:length(pos) str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))]; end if(pos(end)~=length(str)) str=[str str0(pos0(end-1)+1:pos0(end))]; end end %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_'; %%------------------------------------------------------------------------- function endpos = matching_quote(str,pos) len=length(str); while(pos<len) if(str(pos)=='"') if(~(pos>1 && str(pos-1)=='\')) endpos=pos; return; end end pos=pos+1; end error('unmatched quotation mark'); %%------------------------------------------------------------------------- function [endpos e1l e1r maxlevel] = matching_bracket(str,pos) global arraytoken level=1; maxlevel=level; endpos=0; bpos=arraytoken(arraytoken>=pos); tokens=str(bpos); len=length(tokens); pos=1; e1l=[]; e1r=[]; while(pos<=len) c=tokens(pos); if(c==']') level=level-1; if(isempty(e1r)) e1r=bpos(pos); end if(level==0) endpos=bpos(pos); return end end if(c=='[') if(isempty(e1l)) e1l=bpos(pos); end level=level+1; maxlevel=max(maxlevel,level); end if(c=='"') pos=matching_quote(tokens,pos+1); end pos=pos+1; end if(endpos==0) error('unmatched "]"'); end
github
kartik-nighania/Coursera-Machine-Learning-Course-by-Stanford-master
saveubjson.m
.m
Coursera-Machine-Learning-Course-by-Stanford-master/7 K-means clustering and PCA/ex7/lib/jsonlab/saveubjson.m
16,123
utf_8
61d4f51010aedbf97753396f5d2d9ec0
function json=saveubjson(rootname,obj,varargin) % % json=saveubjson(rootname,obj,filename) % or % json=saveubjson(rootname,obj,opt) % json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...) % % convert a MATLAB object (cell, struct or array) into a Universal % Binary JSON (UBJSON) binary string % % author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu) % created on 2013/08/17 % % $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $ % % input: % rootname: the name of the root-object, when set to '', the root name % is ignored, however, when opt.ForceRootName is set to 1 (see below), % the MATLAB variable name will be used as the root name. % obj: a MATLAB object (array, cell, cell array, struct, struct array) % filename: a string for the file name to save the output UBJSON data % opt: a struct for additional options, ignore to use default values. % opt can have the following fields (first in [.|.] is the default) % % opt.FileName [''|string]: a file name to save the output JSON data % opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D % array in JSON array format; if sets to 1, an % array will be shown as a struct with fields % "_ArrayType_", "_ArraySize_" and "_ArrayData_"; for % sparse arrays, the non-zero elements will be % saved to _ArrayData_ field in triplet-format i.e. % (ix,iy,val) and "_ArrayIsSparse_" will be added % with a value of 1; for a complex array, the % _ArrayData_ array will include two columns % (4 for sparse) to record the real and imaginary % parts, and also "_ArrayIsComplex_":1 is added. % opt.ParseLogical [1|0]: if this is set to 1, logical array elem % will use true/false rather than 1/0. % opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single % numerical element will be shown without a square % bracket, unless it is the root object; if 0, square % brackets are forced for any numerical arrays. % opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson % will use the name of the passed obj variable as the % root object name; if obj is an expression and % does not have a name, 'root' will be used; if this % is set to 0 and rootname is empty, the root level % will be merged down to the lower level. % opt.JSONP [''|string]: to generate a JSONP output (JSON with padding), % for example, if opt.JSON='foo', the JSON data is % wrapped inside a function call as 'foo(...);' % opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson % back to the string form % % opt can be replaced by a list of ('param',value) pairs. The param % string is equivallent to a field in opt and is case sensitive. % output: % json: a binary string in the UBJSON format (see http://ubjson.org) % % examples: % jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... % 'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],... % 'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;... % 2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],... % 'MeshCreator','FangQ','MeshTitle','T6 Cube',... % 'SpecialData',[nan, inf, -inf]); % saveubjson('jsonmesh',jsonmesh) % saveubjson('jsonmesh',jsonmesh,'meshdata.ubj') % % license: % BSD, see LICENSE_BSD.txt files for details % % -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab) % if(nargin==1) varname=inputname(1); obj=rootname; if(isempty(varname)) varname='root'; end rootname=varname; else varname=inputname(2); end if(length(varargin)==1 && ischar(varargin{1})) opt=struct('FileName',varargin{1}); else opt=varargin2struct(varargin{:}); end opt.IsOctave=exist('OCTAVE_VERSION','builtin'); rootisarray=0; rootlevel=1; forceroot=jsonopt('ForceRootName',0,opt); if((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0) rootisarray=1; rootlevel=0; else if(isempty(rootname)) rootname=varname; end end if((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot) rootname='root'; end json=obj2ubjson(rootname,obj,rootlevel,opt); if(~rootisarray) json=['{' json '}']; end jsonp=jsonopt('JSONP','',opt); if(~isempty(jsonp)) json=[jsonp '(' json ')']; end % save to a file if FileName is set, suggested by Patrick Rapin if(~isempty(jsonopt('FileName','',opt))) fid = fopen(opt.FileName, 'wb'); fwrite(fid,json); fclose(fid); end %%------------------------------------------------------------------------- function txt=obj2ubjson(name,item,level,varargin) if(iscell(item)) txt=cell2ubjson(name,item,level,varargin{:}); elseif(isstruct(item)) txt=struct2ubjson(name,item,level,varargin{:}); elseif(ischar(item)) txt=str2ubjson(name,item,level,varargin{:}); else txt=mat2ubjson(name,item,level,varargin{:}); end %%------------------------------------------------------------------------- function txt=cell2ubjson(name,item,level,varargin) txt=''; if(~iscell(item)) error('input is not a cell'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); % let's handle 1D cell first if(len>1) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) '[']; name=''; else txt='['; end elseif(len==0) if(~isempty(name)) txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; else txt='Z'; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})]; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=struct2ubjson(name,item,level,varargin) txt=''; if(~isstruct(item)) error('input is not a struct'); end dim=size(item); if(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now item=reshape(item,dim(1),numel(item)/dim(1)); dim=size(item); end len=numel(item); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end for j=1:dim(2) if(dim(1)>1) txt=[txt '[']; end for i=1:dim(1) names = fieldnames(item(i,j)); if(~isempty(name) && len==1) txt=[txt S_(checkname(name,varargin{:})) '{']; else txt=[txt '{']; end if(~isempty(names)) for e=1:length(names) txt=[txt obj2ubjson(names{e},getfield(item(i,j),... names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})]; end end txt=[txt '}']; end if(dim(1)>1) txt=[txt ']']; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=str2ubjson(name,item,level,varargin) txt=''; if(~ischar(item)) error('input is not a string'); end item=reshape(item, max(size(item),[1 0])); len=size(item,1); if(~isempty(name)) if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end else if(len>1) txt='['; end end isoct=jsonopt('IsOctave',0,varargin{:}); for e=1:len val=item(e,:); if(len==1) obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),'']; if(isempty(name)) obj=['',S_(val),'']; end txt=[txt,'',obj]; else txt=[txt,'',['',S_(val),'']]; end end if(len>1) txt=[txt ']']; end %%------------------------------------------------------------------------- function txt=mat2ubjson(name,item,level,varargin) if(~isnumeric(item) && ~islogical(item)) error('input is not an array'); end if(length(size(item))>2 || issparse(item) || ~isreal(item) || ... isempty(item) || jsonopt('ArrayToStruct',0,varargin{:})) cid=I_(uint32(max(size(item)))); if(isempty(name)) txt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ]; else if(isempty(item)) txt=[S_(checkname(name,varargin{:})),'Z']; return; else txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))]; end end else if(isempty(name)) txt=matdata2ubjson(item,level+1,varargin{:}); else if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1) numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\[',''),']',''); txt=[S_(checkname(name,varargin{:})) numtxt]; else txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})]; end end return; end if(issparse(item)) [ix,iy]=find(item); data=full(item(find(item))); if(~isreal(item)) data=[real(data(:)),imag(data(:))]; if(size(item,1)==1) % Kludge to have data's 'transposedness' match item's. % (Necessary for complex row vector handling below.) data=data'; end txt=[txt,S_('_ArrayIsComplex_'),'T']; end txt=[txt,S_('_ArrayIsSparse_'),'T']; if(size(item,1)==1) % Row vector, store only column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([iy(:),data'],level+2,varargin{:})]; elseif(size(item,2)==1) % Column vector, store only row indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,data],level+2,varargin{:})]; else % General case, store row and column indices. txt=[txt,S_('_ArrayData_'),... matdata2ubjson([ix,iy,data],level+2,varargin{:})]; end else if(isreal(item)) txt=[txt,S_('_ArrayData_'),... matdata2ubjson(item(:)',level+2,varargin{:})]; else txt=[txt,S_('_ArrayIsComplex_'),'T']; txt=[txt,S_('_ArrayData_'),... matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})]; end end txt=[txt,'}']; %%------------------------------------------------------------------------- function txt=matdata2ubjson(mat,level,varargin) if(isempty(mat)) txt='Z'; return; end if(size(mat,1)==1) level=level-1; end type=''; hasnegtive=(mat<0); if(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0))) if(isempty(hasnegtive)) if(max(mat(:))<=2^8) type='U'; end end if(isempty(type)) % todo - need to consider negative ones separately id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]); if(isempty(find(id))) error('high-precision data is not yet supported'); end key='iIlL'; type=key(find(id)); end txt=[I_a(mat(:),type,size(mat))]; elseif(islogical(mat)) logicalval='FT'; if(numel(mat)==1) txt=logicalval(mat+1); else txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')]; end else if(numel(mat)==1) txt=['[' D_(mat) ']']; else txt=D_a(mat(:),'D',size(mat)); end end %txt=regexprep(mat2str(mat),'\s+',','); %txt=regexprep(txt,';',sprintf('],[')); % if(nargin>=2 && size(mat,1)>1) % txt=regexprep(txt,'\[',[repmat(sprintf('\t'),1,level) '[']); % end if(any(isinf(mat(:)))) txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','"$1_Inf_"',varargin{:})); end if(any(isnan(mat(:)))) txt=regexprep(txt,'NaN',jsonopt('NaN','"_NaN_"',varargin{:})); end %%------------------------------------------------------------------------- function newname=checkname(name,varargin) isunpack=jsonopt('UnpackHex',1,varargin{:}); newname=name; if(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once'))) return end if(isunpack) isoct=jsonopt('IsOctave',0,varargin{:}); if(~isoct) newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}'); else pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start'); pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end'); if(isempty(pos)) return; end str0=name; pos0=[0 pend(:)' length(name)]; newname=''; for i=1:length(pos) newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))]; end if(pos(end)~=length(name)) newname=[newname str0(pos0(end-1)+1:pos0(end))]; end end end %%------------------------------------------------------------------------- function val=S_(str) if(length(str)==1) val=['C' str]; else val=['S' I_(int32(length(str))) str]; end %%------------------------------------------------------------------------- function val=I_(num) if(~isinteger(num)) error('input is not an integer'); end if(num>=0 && num<255) val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')]; return; end key='iIlL'; cid={'int8','int16','int32','int64'}; for i=1:4 if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1))) val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')]; return; end end error('unsupported integer'); %%------------------------------------------------------------------------- function val=D_(num) if(~isfloat(num)) error('input is not a float'); end if(isa(num,'single')) val=['d' data2byte(num,'uint8')]; else val=['D' data2byte(num,'uint8')]; end %%------------------------------------------------------------------------- function data=I_a(num,type,dim,format) id=find(ismember('iUIlL',type)); if(id==0) error('unsupported integer array'); end % based on UBJSON specs, all integer types are stored in big endian format if(id==1) data=data2byte(swapbytes(int8(num)),'uint8'); blen=1; elseif(id==2) data=data2byte(swapbytes(uint8(num)),'uint8'); blen=1; elseif(id==3) data=data2byte(swapbytes(int16(num)),'uint8'); blen=2; elseif(id==4) data=data2byte(swapbytes(int32(num)),'uint8'); blen=4; elseif(id==5) data=data2byte(swapbytes(int64(num)),'uint8'); blen=8; end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/blen)) data(:)']; end data=['[' data(:)']; else data=reshape(data,blen,numel(data)/blen); data(2:blen+1,:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function data=D_a(num,type,dim,format) id=find(ismember('dD',type)); if(id==0) error('unsupported float array'); end if(id==1) data=data2byte(single(num),'uint8'); elseif(id==2) data=data2byte(double(num),'uint8'); end if(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2)) format='opt'; end if((nargin<4 || strcmp(format,'opt')) && numel(num)>1) if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2)))) cid=I_(uint32(max(dim))); data=['$' type '#' I_a(dim,cid(1)) data(:)']; else data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)']; end data=['[' data]; else data=reshape(data,(id*4),length(data)/(id*4)); data(2:(id*4+1),:)=data; data(1,:)=type; data=data(:)'; data=['[' data(:)' ']']; end %%------------------------------------------------------------------------- function bytes=data2byte(varargin) bytes=typecast(varargin{:}); bytes=bytes(:)';
github
nagadomi/caffe-master
classification_demo.m
.m
caffe-master/matlab/demo/classification_demo.m
5,412
utf_8
8f46deabe6cde287c4759f3bc8b7f819
function [scores, maxlabel] = classification_demo(im, use_gpu) % [scores, maxlabel] = classification_demo(im, use_gpu) % % Image classification demo using BVLC CaffeNet. % % IMPORTANT: before you run this demo, you should download BVLC CaffeNet % from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html) % % **************************************************************************** % For detailed documentation and usage on Caffe's Matlab interface, please % refer to Caffe Interface Tutorial at % http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab % **************************************************************************** % % input % im color image as uint8 HxWx3 % use_gpu 1 to use the GPU, 0 to use the CPU % % output % scores 1000-dimensional ILSVRC score vector % maxlabel the label of the highest score % % You may need to do the following before you start matlab: % $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64 % $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6 % Or the equivalent based on where things are installed on your system % % Usage: % im = imread('../../examples/images/cat.jpg'); % scores = classification_demo(im, 1); % [score, class] = max(scores); % Five things to be aware of: % caffe uses row-major order % matlab uses column-major order % caffe uses BGR color channel order % matlab uses RGB color channel order % images need to have the data mean subtracted % Data coming in from matlab needs to be in the order % [width, height, channels, images] % where width is the fastest dimension. % Here is the rough matlab for putting image data into the correct % format in W x H x C with BGR channels: % % permute channels from RGB to BGR % im_data = im(:, :, [3, 2, 1]); % % flip width and height to make width the fastest dimension % im_data = permute(im_data, [2, 1, 3]); % % convert from uint8 to single % im_data = single(im_data); % % reshape to a fixed size (e.g., 227x227). % im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % % subtract mean_data (already in W x H x C with BGR channels) % im_data = im_data - mean_data; % If you have multiple images, cat them with cat(4, ...) % Add caffe/matlab to you Matlab search PATH to use matcaffe if exist('../+caffe', 'dir') addpath('..'); else error('Please run this demo from caffe/matlab/demo'); end % Set caffe mode if exist('use_gpu', 'var') && use_gpu caffe.set_mode_gpu(); gpu_id = 0; % we will use the first gpu in this demo caffe.set_device(gpu_id); else caffe.set_mode_cpu(); end % Initialize the network using BVLC CaffeNet for image classification % Weights (parameter) file needs to be downloaded from Model Zoo. model_dir = '../../models/bvlc_reference_caffenet/'; net_model = [model_dir 'deploy.prototxt']; net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel']; phase = 'test'; % run with phase test (so that dropout isn't applied) if ~exist(net_weights, 'file') error('Please download CaffeNet from Model Zoo before you run this demo'); end % Initialize a network net = caffe.Net(net_model, net_weights, phase); if nargin < 1 % For demo purposes we will use the cat image fprintf('using caffe/examples/images/cat.jpg as input image\n'); im = imread('../../examples/images/cat.jpg'); end % prepare oversampled input % input_data is Height x Width x Channel x Num tic; input_data = {prepare_image(im)}; toc; % do forward pass to get scores % scores are now Channels x Num, where Channels == 1000 tic; % The net forward function. It takes in a cell array of N-D arrays % (where N == 4 here) containing data of input blob(s) and outputs a cell % array containing data from output blob(s) scores = net.forward(input_data); toc; scores = scores{1}; scores = mean(scores, 2); % take average scores over 10 crops [~, maxlabel] = max(scores); % call caffe.reset_all() to reset caffe caffe.reset_all(); % ------------------------------------------------------------------------ function crops_data = prepare_image(im) % ------------------------------------------------------------------------ % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that % is already in W x H x C with BGR channels d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat'); mean_data = d.mean_data; IMAGE_DIM = 256; CROPPED_DIM = 227; % Convert an image returned by Matlab's imread to im_data in caffe's data % format: W x H x C with BGR channels im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR im_data = permute(im_data, [2, 1, 3]); % flip width and height im_data = single(im_data); % convert from uint8 to single im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data im_data = im_data - mean_data; % subtract mean_data (already in W x H x C, BGR) % oversample (4 corners, center, and their x-axis flips) crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single'); indices = [0 IMAGE_DIM-CROPPED_DIM] + 1; n = 1; for i = indices for j = indices crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :); crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n); n = n + 1; end end center = floor(indices(2) / 2) + 1; crops_data(:,:,:,5) = ... im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:); crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);
github
gylee1103/ELDNet-master
classification_demo.m
.m
ELDNet-master/caffe/matlab/demo/classification_demo.m
5,412
utf_8
8f46deabe6cde287c4759f3bc8b7f819
function [scores, maxlabel] = classification_demo(im, use_gpu) % [scores, maxlabel] = classification_demo(im, use_gpu) % % Image classification demo using BVLC CaffeNet. % % IMPORTANT: before you run this demo, you should download BVLC CaffeNet % from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html) % % **************************************************************************** % For detailed documentation and usage on Caffe's Matlab interface, please % refer to Caffe Interface Tutorial at % http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab % **************************************************************************** % % input % im color image as uint8 HxWx3 % use_gpu 1 to use the GPU, 0 to use the CPU % % output % scores 1000-dimensional ILSVRC score vector % maxlabel the label of the highest score % % You may need to do the following before you start matlab: % $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64 % $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6 % Or the equivalent based on where things are installed on your system % % Usage: % im = imread('../../examples/images/cat.jpg'); % scores = classification_demo(im, 1); % [score, class] = max(scores); % Five things to be aware of: % caffe uses row-major order % matlab uses column-major order % caffe uses BGR color channel order % matlab uses RGB color channel order % images need to have the data mean subtracted % Data coming in from matlab needs to be in the order % [width, height, channels, images] % where width is the fastest dimension. % Here is the rough matlab for putting image data into the correct % format in W x H x C with BGR channels: % % permute channels from RGB to BGR % im_data = im(:, :, [3, 2, 1]); % % flip width and height to make width the fastest dimension % im_data = permute(im_data, [2, 1, 3]); % % convert from uint8 to single % im_data = single(im_data); % % reshape to a fixed size (e.g., 227x227). % im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % % subtract mean_data (already in W x H x C with BGR channels) % im_data = im_data - mean_data; % If you have multiple images, cat them with cat(4, ...) % Add caffe/matlab to you Matlab search PATH to use matcaffe if exist('../+caffe', 'dir') addpath('..'); else error('Please run this demo from caffe/matlab/demo'); end % Set caffe mode if exist('use_gpu', 'var') && use_gpu caffe.set_mode_gpu(); gpu_id = 0; % we will use the first gpu in this demo caffe.set_device(gpu_id); else caffe.set_mode_cpu(); end % Initialize the network using BVLC CaffeNet for image classification % Weights (parameter) file needs to be downloaded from Model Zoo. model_dir = '../../models/bvlc_reference_caffenet/'; net_model = [model_dir 'deploy.prototxt']; net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel']; phase = 'test'; % run with phase test (so that dropout isn't applied) if ~exist(net_weights, 'file') error('Please download CaffeNet from Model Zoo before you run this demo'); end % Initialize a network net = caffe.Net(net_model, net_weights, phase); if nargin < 1 % For demo purposes we will use the cat image fprintf('using caffe/examples/images/cat.jpg as input image\n'); im = imread('../../examples/images/cat.jpg'); end % prepare oversampled input % input_data is Height x Width x Channel x Num tic; input_data = {prepare_image(im)}; toc; % do forward pass to get scores % scores are now Channels x Num, where Channels == 1000 tic; % The net forward function. It takes in a cell array of N-D arrays % (where N == 4 here) containing data of input blob(s) and outputs a cell % array containing data from output blob(s) scores = net.forward(input_data); toc; scores = scores{1}; scores = mean(scores, 2); % take average scores over 10 crops [~, maxlabel] = max(scores); % call caffe.reset_all() to reset caffe caffe.reset_all(); % ------------------------------------------------------------------------ function crops_data = prepare_image(im) % ------------------------------------------------------------------------ % caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that % is already in W x H x C with BGR channels d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat'); mean_data = d.mean_data; IMAGE_DIM = 256; CROPPED_DIM = 227; % Convert an image returned by Matlab's imread to im_data in caffe's data % format: W x H x C with BGR channels im_data = im(:, :, [3, 2, 1]); % permute channels from RGB to BGR im_data = permute(im_data, [2, 1, 3]); % flip width and height im_data = single(im_data); % convert from uint8 to single im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data im_data = im_data - mean_data; % subtract mean_data (already in W x H x C, BGR) % oversample (4 corners, center, and their x-axis flips) crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single'); indices = [0 IMAGE_DIM-CROPPED_DIM] + 1; n = 1; for i = indices for j = indices crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :); crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n); n = n + 1; end end center = floor(indices(2) / 2) + 1; crops_data(:,:,:,5) = ... im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:); crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);
github
fasiha/cython-demo-master
numpyToMat.m
.m
cython-demo-master/numpyToMat.m
731
utf_8
00738f0f052d887028420010b8a9316e
% Taken from answer by Jim Hokanson % http://www.mathworks.com/matlabcentral/answers/157347-convert-python-numpy-array-to-double % if a simple list would be like this: % means = cellfun( @double, cell(ms)) function data = numpyToMat(x) data_size = cell2mat(cell(x.shape)); % if empty array if data_size == 0 data = []; return end data_row = double(py.array.array('d', py.numpy.nditer(x, pyargs('order', 'F')))); % Add order='F' to get data in column-major order (as in Fortran 'F' and Matlab if length(data_size) > 1 data = reshape(data_row, data_size); % No need for transpose, since we're retrieving the data in column major order else data = data_row; end
github
golnazghiasi/cofw68-benchmark-master
VisualizeLocalizationRes.m
.m
cofw68-benchmark-master/VisualizeLocalizationRes.m
3,332
utf_8
20529c5f9a701993d9c0971e566211bd
function [] = VisualizeLocalizationRes( ... boxes, pts_name, occ_name, test, testname, figdir, ... crop_images, show_groundtruth, show_keypoint_num, errors, ... draw_line_between_gt_det, method_name, max_to_show) if(~exist('max_to_show', 'var') || max_to_show > length(test)) max_to_show = length(test); end fprintf(['Visualizing landmark localization predication of first %d images' ... ' for %s method ...\n'], max_to_show, method_name); save_res = fullfile(figdir, method_name); if(~exist(save_res, 'dir')) mkdir(save_res); end for i = 1 : max_to_show if(isempty(boxes{i})) im = imread(test(i).im); clf; imagesc(im); axis off; axis image; hold on; if isfield(test(i), 'bbox') bbox = test(i).bbox; [im, offset] = CropImage(im, bbox, 0); clf; imagesc(im); axis off; axis image; hold on; end continue; end pts_gt = test(i).pts; if isfield(test(i), 'occ') && ~isempty(test(i).occ) occ_gt = test(i).occ; else occ_gt = zeros(1, size(pts_gt, 1)); end im = imread(test(i).im); b = boxes{i}(1); pts_det = getfield(b, pts_name); occ_det = getfield(b, occ_name); if crop_images if isfield(test(i), 'bbox') bbox = test(i).bbox; [im, offset] = CropImage(im, bbox, 0.3); else bbox = [min(pts_gt(:, 1)), min(pts_gt(:, 2)), ... max(pts_gt(:, 1)), max(pts_gt(:, 2))]; [im, offset] = CropImage(im, bbox, 0.3); end pts_gt(:, 1) = pts_gt(:, 1) - offset(1); pts_gt(:, 2) = pts_gt(:, 2) - offset(2); pts_det(:, 1) = pts_det(:, 1) - offset(1); pts_det(:, 2) = pts_det(:, 2) - offset(2); end clf; imagesc(im); axis off; axis image; hold on; if(size(im, 3) == 1) colormap(gray); end plot(pts_det(occ_det == 0, 1), pts_det(occ_det == 0, 2), '.g', ... 'MarkerSize', 20); plot(pts_det(occ_det == 1, 1), pts_det(occ_det == 1, 2), '.r', ... 'MarkerSize', 20); if show_keypoint_num ShowKeypointNums(pts_det, 'k'); end if show_groundtruth plot(pts_gt(occ_gt == 0, 1), pts_gt(occ_gt == 0, 2), '.b', ... 'MarkerSize', 20); plot(pts_gt(occ_gt == 1, 1), pts_gt(occ_gt == 1, 2), '.m', ... 'MarkerSize', 20); if show_keypoint_num ShowKeypointNums(pts_gt, 'm'); end end if draw_line_between_gt_det for k = 1 : size(pts_gt, 1) plot([pts_gt(k, 1) pts_det(k, 1)], [pts_gt(k, 2) pts_det(k, 2)], 'g'); end end title(sprintf('%.3f', errors(i))); pause; %export_fig(fullfile(save_res, num2str(i)), '-pdf'); end function [im, offset] = CropImage(im, box, pad_ratio) % Crops image around the bounding box. pad = pad_ratio * ((box(3) - box(1) + 1) + (box(4) - box(2) + 1)); x1 = max(1, round(box(1) - pad)); y1 = max(1, round(box(2) - pad)); x2 = min(size(im, 2), round(box(3) + pad)); y2 = min(size(im, 1), round(box(4) + pad)); im = im(y1:y2, x1:x2, :); offset(1) = x1 -1; offset(2) = y1 -1; function ShowKeypointNums(pts, color) for i = 1 : size(pts, 1) text(pts(i, 1), pts(i, 2), num2str(i), 'Color', color, 'FontSize', 10); end
github
golnazghiasi/cofw68-benchmark-master
distinguishable_colors.m
.m
cofw68-benchmark-master/distinguishable_colors.m
5,753
utf_8
57960cf5d13cead2f1e291d1288bccb2
function colors = distinguishable_colors(n_colors,bg,func) % DISTINGUISHABLE_COLORS: pick colors that are maximally perceptually distinct % % When plotting a set of lines, you may want to distinguish them by color. % By default, Matlab chooses a small set of colors and cycles among them, % and so if you have more than a few lines there will be confusion about % which line is which. To fix this problem, one would want to be able to % pick a much larger set of distinct colors, where the number of colors % equals or exceeds the number of lines you want to plot. Because our % ability to distinguish among colors has limits, one should choose these % colors to be "maximally perceptually distinguishable." % % This function generates a set of colors which are distinguishable % by reference to the "Lab" color space, which more closely matches % human color perception than RGB. Given an initial large list of possible % colors, it iteratively chooses the entry in the list that is farthest (in % Lab space) from all previously-chosen entries. While this "greedy" % algorithm does not yield a global maximum, it is simple and efficient. % Moreover, the sequence of colors is consistent no matter how many you % request, which facilitates the users' ability to learn the color order % and avoids major changes in the appearance of plots when adding or % removing lines. % % Syntax: % colors = distinguishable_colors(n_colors) % Specify the number of colors you want as a scalar, n_colors. This will % generate an n_colors-by-3 matrix, each row representing an RGB % color triple. If you don't precisely know how many you will need in % advance, there is no harm (other than execution time) in specifying % slightly more than you think you will need. % % colors = distinguishable_colors(n_colors,bg) % This syntax allows you to specify the background color, to make sure that % your colors are also distinguishable from the background. Default value % is white. bg may be specified as an RGB triple or as one of the standard % "ColorSpec" strings. You can even specify multiple colors: % bg = {'w','k'} % or % bg = [1 1 1; 0 0 0] % will only produce colors that are distinguishable from both white and % black. % % colors = distinguishable_colors(n_colors,bg,rgb2labfunc) % By default, distinguishable_colors uses the image processing toolbox's % color conversion functions makecform and applycform. Alternatively, you % can supply your own color conversion function. % % Example: % c = distinguishable_colors(25); % figure % image(reshape(c,[1 size(c)])) % % Example using the file exchange's 'colorspace': % func = @(x) colorspace('RGB->Lab',x); % c = distinguishable_colors(25,'w',func); % Copyright 2010-2011 by Timothy E. Holy % Parse the inputs if (nargin < 2) bg = [1 1 1]; % default white background else if iscell(bg) % User specified a list of colors as a cell aray bgc = bg; for i = 1:length(bgc) bgc{i} = parsecolor(bgc{i}); end bg = cat(1,bgc{:}); else % User specified a numeric array of colors (n-by-3) bg = parsecolor(bg); end end % Generate a sizable number of RGB triples. This represents our space of % possible choices. By starting in RGB space, we ensure that all of the % colors can be generated by the monitor. n_grid = 30; % number of grid divisions along each axis in RGB space x = linspace(0,1,n_grid); [R,G,B] = ndgrid(x,x,x); rgb = [R(:) G(:) B(:)]; if (n_colors > size(rgb,1)/3) error('You can''t readily distinguish that many colors'); end % Convert to Lab color space, which more closely represents human % perception if (nargin > 2) lab = func(rgb); bglab = func(bg); else C = makecform('srgb2lab'); lab = applycform(rgb,C); bglab = applycform(bg,C); end % If the user specified multiple background colors, compute distances % from the candidate colors to the background colors mindist2 = inf(size(rgb,1),1); for i = 1:size(bglab,1)-1 dX = bsxfun(@minus,lab,bglab(i,:)); % displacement all colors from bg dist2 = sum(dX.^2,2); % square distance mindist2 = min(dist2,mindist2); % dist2 to closest previously-chosen color end % Iteratively pick the color that maximizes the distance to the nearest % already-picked color colors = zeros(n_colors,3); lastlab = bglab(end,:); % initialize by making the "previous" color equal to background for i = 1:n_colors dX = bsxfun(@minus,lab,lastlab); % displacement of last from all colors on list dist2 = sum(dX.^2,2); % square distance mindist2 = min(dist2,mindist2); % dist2 to closest previously-chosen color [~,index] = max(mindist2); % find the entry farthest from all previously-chosen colors colors(i,:) = rgb(index,:); % save for output lastlab = lab(index,:); % prepare for next iteration end end function c = parsecolor(s) if ischar(s) c = colorstr2rgb(s); elseif isnumeric(s) && size(s,2) == 3 c = s; else error('MATLAB:InvalidColorSpec','Color specification cannot be parsed.'); end end function c = colorstr2rgb(c) % Convert a color string to an RGB value. % This is cribbed from Matlab's whitebg function. % Why don't they make this a stand-alone function? rgbspec = [1 0 0;0 1 0;0 0 1;1 1 1;0 1 1;1 0 1;1 1 0;0 0 0]; cspec = 'rgbwcmyk'; k = find(cspec==c(1)); if isempty(k) error('MATLAB:InvalidColorString','Unknown color string.'); end if k~=3 || length(c)==1, c = rgbspec(k,:); elseif length(c)>2, if strcmpi(c(1:3),'bla') c = [0 0 0]; elseif strcmpi(c(1:3),'blu') c = [0 0 1]; else error('MATLAB:UnknownColorString', 'Unknown color string.'); end end end
github
sunhongfu/scripts-master
load_nii_ext.m
.m
scripts-master/cs-phase/_src/_nii/load_nii_ext.m
5,544
utf_8
09a2960b9d48f4b0363d5065f1780cbd
% Load NIFTI header extension after its header is loaded using load_nii_hdr. % % Usage: ext = load_nii_ext(filename) % % filename - NIFTI file name. % % Returned values: % % ext - Structure of NIFTI header extension, which includes num_ext, % and all the extended header sections in the header extension. % Each extended header section will have its esize, ecode, and % edata, where edata can be plain text, xml, or any raw data % that was saved in the extended header section. % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function ext = load_nii_ext(filename) if ~exist('filename','var'), error('Usage: ext = load_nii_ext(filename)'); end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end machine = 'ieee-le'; new_ext = 0; if findstr('.nii',filename) & strcmp(filename(end-3:end), '.nii') new_ext = 1; filename(end-3:end)=''; end if findstr('.hdr',filename) & strcmp(filename(end-3:end), '.hdr') filename(end-3:end)=''; end if findstr('.img',filename) & strcmp(filename(end-3:end), '.img') filename(end-3:end)=''; end if new_ext fn = sprintf('%s.nii',filename); if ~exist(fn) msg = sprintf('Cannot find file "%s.nii".', filename); error(msg); end else fn = sprintf('%s.hdr',filename); if ~exist(fn) msg = sprintf('Cannot find file "%s.hdr".', filename); error(msg); end end fid = fopen(fn,'r',machine); vox_offset = 0; if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); if fread(fid,1,'int32') == 348 if new_ext fseek(fid,108,'bof'); vox_offset = fread(fid,1,'float32'); end ext = read_extension(fid, vox_offset); fclose(fid); else fclose(fid); % first try reading the opposite endian to 'machine' % switch machine, case 'ieee-le', machine = 'ieee-be'; case 'ieee-be', machine = 'ieee-le'; end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); if fread(fid,1,'int32') ~= 348 % Now throw an error % msg = sprintf('File "%s" is corrupted.',fn); error(msg); end if new_ext fseek(fid,108,'bof'); vox_offset = fread(fid,1,'float32'); end ext = read_extension(fid, vox_offset); fclose(fid); end end end % Clean up after gunzip % if exist('gzFileName', 'var') rmdir(tmpDir,'s'); end return % load_nii_ext %--------------------------------------------------------------------- function ext = read_extension(fid, vox_offset) ext = []; if vox_offset end_of_ext = vox_offset; else fseek(fid, 0, 'eof'); end_of_ext = ftell(fid); end if end_of_ext > 352 fseek(fid, 348, 'bof'); ext.extension = fread(fid,4)'; end if isempty(ext) | ext.extension(1) == 0 ext = []; return; end i = 1; while(ftell(fid) < end_of_ext) ext.section(i).esize = fread(fid,1,'int32'); ext.section(i).ecode = fread(fid,1,'int32'); ext.section(i).edata = char(fread(fid,ext.section(i).esize-8)'); i = i + 1; end ext.num_ext = length(ext.section); return % read_extension
github
sunhongfu/scripts-master
rri_orient.m
.m
scripts-master/cs-phase/_src/_nii/rri_orient.m
2,357
utf_8
e1b7cfcaf2517b7887ac6e02d9ab504d
% Convert image of different orientations to standard Analyze orientation % % Usage: nii = rri_orient(nii); % Jimmy Shen ([email protected]), 26-APR-04 %___________________________________________________________________ function [nii, orient, pattern] = rri_orient(nii, varargin) if nargin > 1 pattern = varargin{1}; else pattern = []; end if(nargin > 2) orient = varargin{2}; if(length(find(orient>6)) || length(find(orient<1))) %value checking orient=[1 2 3]; %set to default if bogus values set end else orient = [1 2 3]; end dim = double(nii.hdr.dime.dim([2:4])); if ~isempty(pattern) & ~isequal(length(pattern), prod(dim)) return; end % get orient of the current image % if isequal(orient, [1 2 3]) orient = rri_orient_ui; pause(.1); end % no need for conversion % if isequal(orient, [1 2 3]) return; end if isempty(pattern) pattern = 1:prod(dim); end pattern = reshape(pattern, dim); img = nii.img; % calculate after flip orient % rot_orient = mod(orient + 2, 3) + 1; % do flip: % flip_orient = orient - rot_orient; for i = 1:3 if flip_orient(i) pattern = flipdim(pattern, i); img = flipdim(img, i); end end % get index of orient (do inverse) % [tmp rot_orient] = sort(rot_orient); % do rotation: % pattern = permute(pattern, rot_orient); img = permute(img, [rot_orient 4 5 6]); % rotate resolution, or 'dim' % new_dim = nii.hdr.dime.dim([2:4]); new_dim = new_dim(rot_orient); nii.hdr.dime.dim([2:4]) = new_dim; % rotate voxel_size, or 'pixdim' % tmp = nii.hdr.dime.pixdim([2:4]); tmp = tmp(rot_orient); nii.hdr.dime.pixdim([2:4]) = tmp; % re-calculate originator % tmp = nii.hdr.hist.originator([1:3]); tmp = tmp(rot_orient); flip_orient = flip_orient(rot_orient); for i = 1:3 if flip_orient(i) & ~isequal(double(tmp(i)), 0) tmp(i) = int16(double(new_dim(i)) - double(tmp(i)) + 1); end end nii.hdr.hist.originator([1:3]) = tmp; nii.img = img; pattern = pattern(:); return; % rri_orient
github
sunhongfu/scripts-master
save_untouch0_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/save_untouch0_nii_hdr.m
8,813
utf_8
a0a201073cb18f09b62842e94094c451
% internal function % - Jimmy Shen ([email protected]) function save_nii_hdr(hdr, fid) if ~isequal(hdr.hk.sizeof_hdr,348), error('hdr.hk.sizeof_hdr must be 348.'); end write_header(hdr, fid); return; % save_nii_hdr %--------------------------------------------------------------------- function write_header(hdr, fid) % Original header structures % struct dsr /* dsr = hdr */ % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ header_key(fid, hdr.hk); image_dimension(fid, hdr.dime); data_history(fid, hdr.hist); % check the file size is 348 bytes % fbytes = ftell(fid); if ~isequal(fbytes,348), msg = sprintf('Header size is not 348 bytes.'); warning(msg); end return; % write_header %--------------------------------------------------------------------- function header_key(fid, hk) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ fwrite(fid, hk.sizeof_hdr(1), 'int32'); % must be 348. % data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars from left % fwrite(fid, data_type(1:10), 'uchar'); pad = zeros(1, 10-length(hk.data_type)); hk.data_type = [hk.data_type char(pad)]; fwrite(fid, hk.data_type(1:10), 'uchar'); % db_name = sprintf('%-18s', hk.db_name); % ensure it is 18 chars from left % fwrite(fid, db_name(1:18), 'uchar'); pad = zeros(1, 18-length(hk.db_name)); hk.db_name = [hk.db_name char(pad)]; fwrite(fid, hk.db_name(1:18), 'uchar'); fwrite(fid, hk.extents(1), 'int32'); fwrite(fid, hk.session_error(1), 'int16'); fwrite(fid, hk.regular(1), 'uchar'); fwrite(fid, hk.hkey_un0(1), 'uchar'); return; % header_key %--------------------------------------------------------------------- function image_dimension(fid, dime) %struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % char vox_units[4]; /* 16 + 4 */ % char cal_units[8]; /* 20 + 8 */ % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width % pixdim[2] - voxel height % pixdim[3] - interslice distance % ..etc % */ % float vox_offset; /* 68 + 4 */ % float roi_scale; /* 72 + 4 */ % float funused1; /* 76 + 4 */ % float funused2; /* 80 + 4 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % int compressed; /* 92 + 4 */ % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ fwrite(fid, dime.dim(1:8), 'int16'); pad = zeros(1, 4-length(dime.vox_units)); dime.vox_units = [dime.vox_units char(pad)]; fwrite(fid, dime.vox_units(1:4), 'uchar'); pad = zeros(1, 8-length(dime.cal_units)); dime.cal_units = [dime.cal_units char(pad)]; fwrite(fid, dime.cal_units(1:8), 'uchar'); fwrite(fid, dime.unused1(1), 'int16'); fwrite(fid, dime.datatype(1), 'int16'); fwrite(fid, dime.bitpix(1), 'int16'); fwrite(fid, dime.dim_un0(1), 'int16'); fwrite(fid, dime.pixdim(1:8), 'float32'); fwrite(fid, dime.vox_offset(1), 'float32'); fwrite(fid, dime.roi_scale(1), 'float32'); fwrite(fid, dime.funused1(1), 'float32'); fwrite(fid, dime.funused2(1), 'float32'); fwrite(fid, dime.cal_max(1), 'float32'); fwrite(fid, dime.cal_min(1), 'float32'); fwrite(fid, dime.compressed(1), 'int32'); fwrite(fid, dime.verified(1), 'int32'); fwrite(fid, dime.glmax(1), 'int32'); fwrite(fid, dime.glmin(1), 'int32'); return; % image_dimension %--------------------------------------------------------------------- function data_history(fid, hist) % Original header structures - ANALYZE 7.5 %struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % char orient; /* 104 + 1 */ % char originator[10]; /* 105 + 10 */ % char generated[10]; /* 115 + 10 */ % char scannum[10]; /* 125 + 10 */ % char patient_id[10]; /* 135 + 10 */ % char exp_date[10]; /* 145 + 10 */ % char exp_time[10]; /* 155 + 10 */ % char hist_un0[3]; /* 165 + 3 */ % int views /* 168 + 4 */ % int vols_added; /* 172 + 4 */ % int start_field; /* 176 + 4 */ % int field_skip; /* 180 + 4 */ % int omax; /* 184 + 4 */ % int omin; /* 188 + 4 */ % int smax; /* 192 + 4 */ % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ % descrip = sprintf('%-80s', hist.descrip); % 80 chars from left % fwrite(fid, descrip(1:80), 'uchar'); pad = zeros(1, 80-length(hist.descrip)); hist.descrip = [hist.descrip char(pad)]; fwrite(fid, hist.descrip(1:80), 'uchar'); % aux_file = sprintf('%-24s', hist.aux_file); % 24 chars from left % fwrite(fid, aux_file(1:24), 'uchar'); pad = zeros(1, 24-length(hist.aux_file)); hist.aux_file = [hist.aux_file char(pad)]; fwrite(fid, hist.aux_file(1:24), 'uchar'); fwrite(fid, hist.orient(1), 'uchar'); fwrite(fid, hist.originator(1:5), 'int16'); pad = zeros(1, 10-length(hist.generated)); hist.generated = [hist.generated char(pad)]; fwrite(fid, hist.generated(1:10), 'uchar'); pad = zeros(1, 10-length(hist.scannum)); hist.scannum = [hist.scannum char(pad)]; fwrite(fid, hist.scannum(1:10), 'uchar'); pad = zeros(1, 10-length(hist.patient_id)); hist.patient_id = [hist.patient_id char(pad)]; fwrite(fid, hist.patient_id(1:10), 'uchar'); pad = zeros(1, 10-length(hist.exp_date)); hist.exp_date = [hist.exp_date char(pad)]; fwrite(fid, hist.exp_date(1:10), 'uchar'); pad = zeros(1, 10-length(hist.exp_time)); hist.exp_time = [hist.exp_time char(pad)]; fwrite(fid, hist.exp_time(1:10), 'uchar'); pad = zeros(1, 3-length(hist.hist_un0)); hist.hist_un0 = [hist.hist_un0 char(pad)]; fwrite(fid, hist.hist_un0(1:3), 'uchar'); fwrite(fid, hist.views(1), 'int32'); fwrite(fid, hist.vols_added(1), 'int32'); fwrite(fid, hist.start_field(1),'int32'); fwrite(fid, hist.field_skip(1), 'int32'); fwrite(fid, hist.omax(1), 'int32'); fwrite(fid, hist.omin(1), 'int32'); fwrite(fid, hist.smax(1), 'int32'); fwrite(fid, hist.smin(1), 'int32'); return; % data_history
github
sunhongfu/scripts-master
rri_zoom_menu.m
.m
scripts-master/cs-phase/_src/_nii/rri_zoom_menu.m
770
utf_8
f0bae2b3d88fd719c47fd467e867e19f
% Imbed a zoom menu to any figure. % % Usage: rri_zoom_menu(fig); % % - Jimmy Shen ([email protected]) % %-------------------------------------------------------------------- function menu_hdl = rri_zoom_menu(fig) if isnumeric(fig) menu_hdl = uimenu('Parent',fig, ... 'Label','Zoom on', ... 'Userdata', 1, ... 'Callback','rri_zoom_menu(''zoom'');'); return; end zoom_on_state = get(gcbo,'Userdata'); if (zoom_on_state == 1) zoom on; set(gcbo,'Userdata',0,'Label','Zoom off'); set(gcbf,'pointer','crosshair'); else zoom off; set(gcbo,'Userdata',1,'Label','Zoom on'); set(gcbf,'pointer','arrow'); end return % rri_zoom_menu
github
sunhongfu/scripts-master
rri_select_file.m
.m
scripts-master/cs-phase/_src/_nii/rri_select_file.m
17,235
utf_8
0e0b14435a670dd8805aa514f7dbb6bb
function [selected_file, selected_path] = rri_select_file(varargin) % % USAGE: [selected_file, selected_path] = ... % rri_select_file(dir_name, fig_title) % % Allow user to select a file from a list of Matlab competible % file format % % Example: % % [selected_file, selected_path] = ... % rri_select_file('/usr','Select Data File'); % % See Also RRI_GETFILES % -- Created June 2001 by Wilkin Chau, Rotman Research Institute % % use rri_select_file to open & save Matlab recognized format % -- Modified Dec 2002 by Jimmy Shen, Rotman Research Institute % if nargin == 0 | ischar(varargin{1}) % create rri_select_file figure dir_name = ''; fig_title = 'Select a File'; if nargin > 0 dir_name = varargin{1}; end if nargin > 1 fig_title = varargin{2}; end Init(fig_title,dir_name); uiwait; % wait for user finish selected_path = getappdata(gcf,'SelectedDirectory'); selected_file = getappdata(gcf,'SelectedFile'); cd (getappdata(gcf,'StartDirectory')); close(gcf); return; end; % clear the message line, % h = findobj(gcf,'Tag','MessageLine'); set(h,'String',''); action = varargin{1}{1}; % change 'File format': % update 'Files' & 'File selection' based on file pattern % if strcmp(action,'EditFilter'), EditFilter; % run delete_fig when figure is closing % elseif strcmp(action,'delete_fig'), delete_fig; % select 'Directories': % go into the selected dir % update 'Files' & 'File selection' based on file pattern % elseif strcmp(action,'select_dir'), select_dir; % select 'Files': % update 'File selection' % elseif strcmp(action,'select_file'), select_file; % change 'File selection': % if it is a file, select that, % if it is more than a file (*), select those, % if it is a directory, select based on file pattern % elseif strcmp(action,'EditSelection'), EditSelection; % clicked 'Select' % elseif strcmp(action,'DONE_BUTTON_PRESSED'), h = findobj(gcf,'Tag','SelectionEdit'); [filepath,filename,fileext] = fileparts(get(h,'String')); if isempty(filepath) | isempty(filename) | isempty(fileext) setappdata(gcf,'SelectedDirectory',[]); setappdata(gcf,'SelectedFile',[]); else if ~strcmp(filepath(end),filesep) % not end with filesep filepath = [filepath filesep]; % add a filesep to filepath end setappdata(gcf,'SelectedDirectory',filepath); setappdata(gcf,'SelectedFile',[filename fileext]); end if getappdata(gcf,'ready') % ready to exit uiresume; end % clicked 'cancel' % elseif strcmp(action,'CANCEL_BUTTON_PRESSED'), setappdata(gcf,'SelectedDirectory',[]); setappdata(gcf,'SelectedFile',[]); set(findobj(gcf,'Tag','FileList'),'String',''); uiresume; end; return; % -------------------------------------------------------------------- function Init(fig_title,dir_name), StartDirectory = pwd; if isempty(StartDirectory), StartDirectory = filesep; end; filter_disp = {'JPEG image (*.jpg)', ... 'TIFF image, compressed (*.tif)', ... 'EPS Level 1 (*.eps)', ... 'Adobe Illustrator 88 (*.ai)', ... 'Enhanced metafile (*.emf)', ... 'Matlab Figure (*.fig)', ... 'Matlab M-file (*.m)', ... 'Portable bitmap (*.pbm)', ... 'Paintbrush 24-bit (*.pcx)', ... 'Portable Graymap (*.pgm)', ... 'Portable Network Graphics (*.png)', ... 'Portable Pixmap (*.ppm)', ... }; filter_string = {'*.jpg', ... '*.tif', ... '*.eps', ... '*.ai', ... '*.emf', ... '*.fig', ... '*.m', ... '*.pbm', ... '*.pcx', ... '*.pgm', ... '*.png', ... '*.ppm', ... }; % filter_disp = char(filter_disp); filter_string = char(filter_string); margine = 0.05; line_height = 0.07; char_height = line_height*0.8; save_setting_status = 'on'; rri_select_file_pos = []; try load('pls_profile'); catch end if ~isempty(rri_select_file_pos) & strcmp(save_setting_status,'on') pos = rri_select_file_pos; else w = 0.4; h = 0.6; x = (1-w)/2; y = (1-h)/2; pos = [x y w h]; end h0 = figure('parent',0, 'Color',[0.8 0.8 0.8], ... 'Units','normal', ... 'Name',fig_title, ... 'NumberTitle','off', ... 'MenuBar','none', ... 'Position', pos, ... 'deleteFcn','rri_select_file({''delete_fig''});', ... 'WindowStyle', 'modal', ... 'Tag','GetFilesFigure', ... 'ToolBar','none'); x = margine; y = 1 - 1*line_height - margine; w = 1-2*x; h = char_height; pos = [x y w h]; h1 = uicontrol('Parent',h0, ... % Filter Label 'Style','text', ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'Position', pos, ... 'String','Choose one of the file format:', ... 'Tag','FilterLabel'); y = 1 - 2*line_height - margine + line_height*0.2; w = 1-2*x; pos = [x y w h]; h_filter = uicontrol('Parent',h0, ... % Filter list 'Style','popupmenu', ... 'Units','normal', ... 'BackgroundColor',[1 1 1], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'Position', pos, ... 'String', filter_disp, ... 'user', filter_string, ... 'value', 1, ... 'Callback','rri_select_file({''EditFilter''});', ... 'Tag','FilterEdit'); y = 1 - 3*line_height - margine; w = 0.5 - x - margine/2; pos = [x y w h]; h1 = uicontrol('Parent',h0, ... % Directory Label 'Style','text', ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'ListboxTop',0, ... 'Position', pos, ... 'String','Directories', ... 'Tag','DirectoryLabel'); x = 0.5; y = 1 - 3*line_height - margine; w = 0.5 - margine; pos = [x y w h]; h1 = uicontrol('Parent',h0, ... % File Label 'Style','text', ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'ListboxTop',0, ... 'Position', pos, ... 'String','Files', ... 'Tag','FileLabel'); x = margine; y = 4*line_height + margine; w = 0.5 - x - margine/2; h = 1 - 7*line_height - 2*margine; pos = [x y w h]; h_dir = uicontrol('Parent',h0, ... % Directory Listbox 'Style','listbox', ... 'Units','normal', ... 'fontunit','normal', ... 'FontSize',0.08, ... 'HorizontalAlignment','left', ... 'Interruptible', 'off', ... 'ListboxTop',1, ... 'Position', pos, ... 'String', '', ... 'Callback','rri_select_file({''select_dir''});', ... 'Tag','DirectoryList'); x = 0.5; y = 4*line_height + margine; w = 0.5 - margine; h = 1 - 7*line_height - 2*margine; pos = [x y w h]; h_file = uicontrol('Parent',h0, ... % File Listbox 'Style','listbox', ... 'Units','normal', ... 'fontunit','normal', ... 'FontSize',0.08, ... 'HorizontalAlignment','left', ... 'ListboxTop',1, ... 'Position', pos, ... 'String', '', ... 'Callback','rri_select_file({''select_file''});', ... 'Tag','FileList'); x = margine; y = 3*line_height + margine - line_height*0.2; w = 1-2*x; h = char_height; pos = [x y w h]; h1 = uicontrol('Parent',h0, ... % Selection Label 'Style','text', ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'Position', pos, ... 'String','File you selected:', ... 'Tag','SelectionLabel'); y = 2*line_height + margine; w = 1-2*x; pos = [x y w h]; h_select = uicontrol('Parent',h0, ... % Selection Edit 'Style','edit', ... 'Units','normal', ... 'BackgroundColor',[1 1 1], ... 'fontunit','normal', ... 'FontSize',0.5, ... 'HorizontalAlignment','left', ... 'Position', pos, ... 'String', '', ... 'Callback','rri_select_file({''EditSelection''});', ... 'Tag','SelectionEdit'); x = 2*margine; y = line_height/2 + margine; w = 0.2; h = line_height; pos = [x y w h]; h_done = uicontrol('Parent',h0, ... % DONE 'Units','normal', ... 'fontunit','normal', ... 'FontSize',0.5, ... 'ListboxTop',0, ... 'Position', pos, ... 'HorizontalAlignment','center', ... 'String','Save', ... % 'Select', ... 'Callback','rri_select_file({''DONE_BUTTON_PRESSED''});', ... 'Tag','DONEButton'); x = 1 - x - w; pos = [x y w h]; h_cancel = uicontrol('Parent',h0, ... % CANCEL 'Units','normal', ... 'fontunit','normal', ... 'FontSize',0.5, ... 'ListboxTop',0, ... 'Position', pos, ... 'HorizontalAlignment','center', ... 'String','Cancel', ... 'Callback','rri_select_file({''CANCEL_BUTTON_PRESSED''});', ... 'Tag','CANCELButton'); if isempty(dir_name) dir_name = StartDirectory; end set(h_select,'string',dir_name); filter_select = get(h_filter,'value'); filter_pattern = filter_string(filter_select,:); setappdata(gcf,'FilterPattern',deblank(filter_pattern)); setappdata(gcf,'filter_string',filter_string); setappdata(gcf,'h_filter', h_filter); setappdata(gcf,'h_dir', h_dir); setappdata(gcf,'h_file', h_file); setappdata(gcf,'h_select', h_select); setappdata(gcf,'h_done', h_done); setappdata(gcf,'h_cancel', h_cancel); setappdata(gcf,'StartDirectory',StartDirectory); EditSelection; h_file = getappdata(gcf,'h_file'); if isempty(get(h_file,'string')) setappdata(gcf,'ready',0); else setappdata(gcf,'ready',1); end return; % Init % called by all the actions, to update 'Directories' or 'Files' % based on filter_pattern. Select first file in filelist. % % -------------------------------------------------------------------- function update_dirlist; filter_path = getappdata(gcf,'curr_dir'); filter_pattern = getappdata(gcf,'FilterPattern'); if exist(filter_pattern) == 2 % user input specific filename is_single_file = 1; % need manually take path out later else is_single_file = 0; end % take the file path out from filter_pattern % [fpath fname fext] = fileparts(filter_pattern); filter_pattern = [fname fext]; dir_struct = dir(filter_path); if isempty(dir_struct) msg = 'ERROR: Directory not found!'; uiwait(msgbox(msg,'File Selection Error','modal')); return; end; old_pointer = get(gcf,'Pointer'); set(gcf,'Pointer','watch'); dir_list = dir_struct(find([dir_struct.isdir] == 1)); [sorted_dir_names,sorted_dir_index] = sortrows({dir_list.name}'); dir_struct = dir([filter_path filesep filter_pattern]); if isempty(dir_struct) sorted_file_names = []; else file_list = dir_struct(find([dir_struct.isdir] == 0)); if is_single_file % take out path tmp = file_list.name; [fpath fname fext] = fileparts(tmp); file_list.name = [fname fext]; end [sorted_file_names,sorted_file_index] = sortrows({file_list.name}'); end; disp_dir_names = []; % if need full path, use this % instead of sorted_dir_names for i=1:length(sorted_dir_names) tmp = [filter_path filesep sorted_dir_names{i}]; disp_dir_names = [disp_dir_names {tmp}]; end h = findobj(gcf,'Tag','DirectoryList'); set(h,'String',sorted_dir_names,'Value',1); h = findobj(gcf,'Tag','FileList'); set(h,'String',sorted_file_names,'value',1); h_select = getappdata(gcf,'h_select'); if strcmp(filter_path(end),filesep) % filepath end with filesep filter_path = filter_path(1:end-1); % take filesep out end if isempty(sorted_file_names) set(h_select,'string',[filter_path filesep]); else set(h_select,'string',[filter_path filesep sorted_file_names{1}]); end set(gcf,'Pointer',old_pointer); return; % update_dirlist % change 'File format': % update 'Files' & 'File selection' based on file pattern % % -------------------------------------------------------------------- function EditFilter() filter_select = get(gcbo,'value'); filter_string = getappdata(gcf,'filter_string'); filter_pattern = filter_string(filter_select,:); filter_path = getappdata(gcf,'curr_dir'); % update filter_pattern setappdata(gcf,'FilterPattern',deblank(filter_pattern)); if isempty(filter_path), filter_path = filesep; end; update_dirlist; h_file = getappdata(gcf,'h_file'); if isempty(get(h_file,'string')) setappdata(gcf,'ready',0); else setappdata(gcf,'ready',1); end return; % EditFilter % select 'Directories': % go into the selected dir % update 'Files' & 'File selection' based on file pattern % % -------------------------------------------------------------------- function select_dir() listed_dir = get(gcbo,'String'); selected_dir_idx = get(gcbo,'Value'); selected_dir = listed_dir{selected_dir_idx}; curr_dir = getappdata(gcf,'curr_dir'); % update the selection box % try cd ([curr_dir filesep selected_dir]); catch msg = 'ERROR: Cannot access directory'; uiwait(msgbox(msg,'File Selection Error','modal')); return; end; if isempty(pwd) curr_dir = filesep; else curr_dir = pwd; end; setappdata(gcf,'curr_dir',curr_dir); update_dirlist; h_file = getappdata(gcf,'h_file'); if isempty(get(h_file,'string')) setappdata(gcf,'ready',0); else setappdata(gcf,'ready',1); end return; % select_dir % select 'Files': % update 'File selection' % % -------------------------------------------------------------------- function select_file() setappdata(gcf,'ready',1); listed_file = get(gcbo,'String'); selected_file_idx = get(gcbo,'Value'); selected_file = listed_file{selected_file_idx}; curr_dir = getappdata(gcf,'curr_dir'); if strcmp(curr_dir(end),filesep) % filepath end with filesep curr_dir = curr_dir(1:end-1); % take filesep out end h_select = getappdata(gcf,'h_select'); set(h_select,'string',[curr_dir filesep selected_file]); return; % select_file % change 'File selection': % if it is a file, select that, % if it is more than a file (*), select those, % if it is a directory, select based on file pattern % % -------------------------------------------------------------------- function EditSelection() filter_string = getappdata(gcf,'filter_string'); h_select = getappdata(gcf,'h_select'); selected_file = get(h_select,'string'); if exist(selected_file) == 7 % if user enter a dir setappdata(gcf,'ready',0); setappdata(gcf,'curr_dir',selected_file); % get new dir update_dirlist; else setappdata(gcf,'ready',1); [fpath fname fext]= fileparts(selected_file); if exist(fpath) ~=7 % fpath is not a dir setappdata(gcf,'ready',0); msg = 'ERROR: Cannot access directory'; uiwait(msgbox(msg,'File Selection Error','modal')); end % if the file format user entered is not supported by matlab if isempty(strmatch(['*',fext],filter_string,'exact')) setappdata(gcf,'ready',0); msg = 'ERROR: File format is not supported by Matlab.'; uiwait(msgbox(msg,'File Selection Error','modal')); end end return; % EditSelection % -------------------------------------------------------------------- function delete_fig() try load('pls_profile'); pls_profile = which('pls_profile.mat'); rri_select_file_pos = get(gcbf,'position'); save(pls_profile, '-append', 'rri_select_file_pos'); catch end return;
github
sunhongfu/scripts-master
clip_nii.m
.m
scripts-master/cs-phase/_src/_nii/clip_nii.m
3,421
utf_8
19da887808bddae362df38b0e9f35076
% CLIP_NII: Clip the NIfTI volume from any of the 6 sides % % Usage: nii = clip_nii(nii, [option]) % % Inputs: % % nii - NIfTI volume. % % option - struct instructing how many voxel to be cut from which side. % % option.cut_from_L = ( number of voxel ) % option.cut_from_R = ( number of voxel ) % option.cut_from_P = ( number of voxel ) % option.cut_from_A = ( number of voxel ) % option.cut_from_I = ( number of voxel ) % option.cut_from_S = ( number of voxel ) % % Options description in detail: % ============================== % % cut_from_L: Number of voxels from Left side will be clipped. % % cut_from_R: Number of voxels from Right side will be clipped. % % cut_from_P: Number of voxels from Posterior side will be clipped. % % cut_from_A: Number of voxels from Anterior side will be clipped. % % cut_from_I: Number of voxels from Inferior side will be clipped. % % cut_from_S: Number of voxels from Superior side will be clipped. % % NIfTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function nii = clip_nii(nii, opt) dims = abs(nii.hdr.dime.dim(2:4)); origin = abs(nii.hdr.hist.originator(1:3)); if isempty(origin) | all(origin == 0) % according to SPM origin = round((dims+1)/2); end cut_from_L = 0; cut_from_R = 0; cut_from_P = 0; cut_from_A = 0; cut_from_I = 0; cut_from_S = 0; if nargin > 1 & ~isempty(opt) if ~isstruct(opt) error('option argument should be a struct'); end if isfield(opt,'cut_from_L') cut_from_L = round(opt.cut_from_L); if cut_from_L >= origin(1) | cut_from_L < 0 error('cut_from_L cannot be negative or cut beyond originator'); end end if isfield(opt,'cut_from_P') cut_from_P = round(opt.cut_from_P); if cut_from_P >= origin(2) | cut_from_P < 0 error('cut_from_P cannot be negative or cut beyond originator'); end end if isfield(opt,'cut_from_I') cut_from_I = round(opt.cut_from_I); if cut_from_I >= origin(3) | cut_from_I < 0 error('cut_from_I cannot be negative or cut beyond originator'); end end if isfield(opt,'cut_from_R') cut_from_R = round(opt.cut_from_R); if cut_from_R > dims(1)-origin(1) | cut_from_R < 0 error('cut_from_R cannot be negative or cut beyond originator'); end end if isfield(opt,'cut_from_A') cut_from_A = round(opt.cut_from_A); if cut_from_A > dims(2)-origin(2) | cut_from_A < 0 error('cut_from_A cannot be negative or cut beyond originator'); end end if isfield(opt,'cut_from_S') cut_from_S = round(opt.cut_from_S); if cut_from_S > dims(3)-origin(3) | cut_from_S < 0 error('cut_from_S cannot be negative or cut beyond originator'); end end end nii = make_nii(nii.img( (cut_from_L+1) : (dims(1)-cut_from_R), ... (cut_from_P+1) : (dims(2)-cut_from_A), ... (cut_from_I+1) : (dims(3)-cut_from_S), ... :,:,:,:,:), nii.hdr.dime.pixdim(2:4), ... [origin(1)-cut_from_L origin(2)-cut_from_P origin(3)-cut_from_I], ... nii.hdr.dime.datatype, nii.hdr.hist.descrip); return;
github
sunhongfu/scripts-master
affine.m
.m
scripts-master/cs-phase/_src/_nii/affine.m
16,664
utf_8
419b609560eb98534c0e32cc4506cc7f
% Using 2D or 3D affine matrix to rotate, translate, scale, reflect and % shear a 2D image or 3D volume. 2D image is represented by a 2D matrix, % 3D volume is represented by a 3D matrix, and data type can be real % integer or floating-point. % % You may notice that MATLAB has a function called 'imtransform.m' for % 2D spatial transformation. However, keep in mind that 'imtransform.m' % assumes y for the 1st dimension, and x for the 2nd dimension. They are % equivalent otherwise. % % In addition, if you adjust the 'new_elem_size' parameter, this 'affine.m' % is equivalent to 'interp2.m' for 2D image, and equivalent to 'interp3.m' % for 3D volume. % % Usage: [new_img new_M] = ... % affine(old_img, old_M, [new_elem_size], [verbose], [bg], [method]); % % old_img - original 2D image or 3D volume. We assume x for the 1st % dimension, y for the 2nd dimension, and z for the 3rd % dimension. % % old_M - a 3x3 2D affine matrix for 2D image, or a 4x4 3D affine % matrix for 3D volume. We assume x for the 1st dimension, % y for the 2nd dimension, and z for the 3rd dimension. % % new_elem_size (optional) - size of voxel along x y z direction for % a transformed 3D volume, or size of pixel along x y for % a transformed 2D image. We assume x for the 1st dimension % y for the 2nd dimension, and z for the 3rd dimension. % 'new_elem_size' is 1 if it is default or empty. % % You can increase its value to decrease the resampling rate, % and make the 2D image or 3D volume more coarse. It works % just like 'interp3'. % % verbose (optional) - 1, 0 % 1: show transforming progress in percentage % 2: progress will not be displayed % 'verbose' is 1 if it is default or empty. % % bg (optional) - background voxel intensity in any extra corner that % is caused by the interpolation. 0 in most cases. If it is % default or empty, 'bg' will be the average of two corner % voxel intensities in original data. % % method (optional) - 1, 2, or 3 % 1: for Trilinear interpolation % 2: for Nearest Neighbor interpolation % 3: for Fischer's Bresenham interpolation % 'method' is 1 if it is default or empty. % % new_img - transformed 2D image or 3D volume % % new_M - transformed affine matrix % % Example 1 (3D rotation): % load mri.mat; old_img = double(squeeze(D)); % old_M = [0.88 0.5 3 -90; -0.5 0.88 3 -126; 0 0 2 -72; 0 0 0 1]; % new_img = affine(old_img, old_M, 2); % [x y z] = meshgrid(1:128,1:128,1:27); % sz = size(new_img); % [x1 y1 z1] = meshgrid(1:sz(2),1:sz(1),1:sz(3)); % figure; slice(x, y, z, old_img, 64, 64, 13.5); % shading flat; colormap(map); view(-66, 66); % figure; slice(x1, y1, z1, new_img, sz(1)/2, sz(2)/2, sz(3)/2); % shading flat; colormap(map); view(-66, 66); % % Example 2 (2D interpolation): % load mri.mat; old_img=D(:,:,1,13)'; % old_M = [1 0 0; 0 1 0; 0 0 1]; % new_img = affine(old_img, old_M, [.2 .4]); % figure; image(old_img); colormap(map); % figure; image(new_img); colormap(map); % % This program is inspired by: % SPM5 Software from Wellcome Trust Centre for Neuroimaging % http://www.fil.ion.ucl.ac.uk/spm/software % Fischer, J., A. del Rio (2004). A Fast Method for Applying Rigid % Transformations to Volume Data, WSCG2004 Conference. % http://wscg.zcu.cz/wscg2004/Papers_2004_Short/M19.pdf % % - Jimmy Shen ([email protected]) % function [new_img, new_M] = affine(old_img, old_M, new_elem_size, verbose, bg, method) if ~exist('old_img','var') | ~exist('old_M','var') error('Usage: [new_img new_M] = affine(old_img, old_M, [new_elem_size], [verbose], [bg], [method]);'); end if ndims(old_img) == 3 if ~isequal(size(old_M),[4 4]) error('old_M should be a 4x4 affine matrix for 3D volume.'); end elseif ndims(old_img) == 2 if ~isequal(size(old_M),[3 3]) error('old_M should be a 3x3 affine matrix for 2D image.'); end else error('old_img should be either 2D image or 3D volume.'); end if ~exist('new_elem_size','var') | isempty(new_elem_size) new_elem_size = [1 1 1]; elseif length(new_elem_size) < 2 new_elem_size = new_elem_size(1)*ones(1,3); elseif length(new_elem_size) < 3 new_elem_size = [new_elem_size(:); 1]'; end if ~exist('method','var') | isempty(method) method = 1; elseif ~exist('bresenham_line3d.m','file') & method == 3 error([char(10) char(10) 'Please download 3D Bresenham''s line generation program from:' char(10) char(10) 'http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=21057' char(10) char(10) 'to test Fischer''s Bresenham interpolation method.' char(10) char(10)]); end % Make compatible to MATLAB earlier than version 7 (R14), which % can only perform arithmetic on double data type % old_img = double(old_img); old_dim = size(old_img); if ~exist('bg','var') | isempty(bg) bg = mean([old_img(1) old_img(end)]); end if ~exist('verbose','var') | isempty(verbose) verbose = 1; end if ndims(old_img) == 2 old_dim(3) = 1; old_M = old_M(:, [1 2 3 3]); old_M = old_M([1 2 3 3], :); old_M(3,:) = [0 0 1 0]; old_M(:,3) = [0 0 1 0]'; end % Vertices of img in voxel % XYZvox = [ 1 1 1 1 1 old_dim(3) 1 old_dim(2) 1 1 old_dim(2) old_dim(3) old_dim(1) 1 1 old_dim(1) 1 old_dim(3) old_dim(1) old_dim(2) 1 old_dim(1) old_dim(2) old_dim(3) ]'; old_R = old_M(1:3,1:3); old_T = old_M(1:3,4); % Vertices of img in millimeter % XYZmm = old_R*(XYZvox-1) + repmat(old_T, [1, 8]); % Make scale of new_M according to new_elem_size % new_M = diag([new_elem_size 1]); % Make translation so minimum vertex is moved to [1,1,1] % new_M(1:3,4) = round( min(XYZmm,[],2) ); % New dimensions will be the maximum vertices in XYZ direction (dim_vox) % i.e. compute dim_vox via dim_mm = R*(dim_vox-1)+T % where, dim_mm = round(max(XYZmm,[],2)); % new_dim = ceil(new_M(1:3,1:3) \ ( round(max(XYZmm,[],2))-new_M(1:3,4) )+1)'; % Initialize new_img with new_dim % new_img = zeros(new_dim(1:3)); % Mask out any changes from Z axis of transformed volume, since we % will traverse it voxel by voxel below. We will only apply unit % increment of mask_Z(3,4) to simulate the cursor movement % % i.e. we will use mask_Z * new_XYZvox to replace new_XYZvox % mask_Z = diag(ones(1,4)); mask_Z(3,3) = 0; % It will be easier to do the interpolation if we invert the process % by not traversing the original volume. Instead, we traverse the % transformed volume, and backproject each voxel in the transformed % volume back into the original volume. If the backprojected voxel % in original volume is within its boundary, the intensity of that % voxel can be used by the cursor location in the transformed volume. % % First, we traverse along Z axis of transformed volume voxel by voxel % for z = 1:new_dim(3) if verbose & ~mod(z,10) fprintf('%.2f percent is done.\n', 100*z/new_dim(3)); end % We need to find out the mapping from voxel in the transformed % volume (new_XYZvox) to voxel in the original volume (old_XYZvox) % % The following equation works, because they all equal to XYZmm: % new_R*(new_XYZvox-1) + new_T == old_R*(old_XYZvox-1) + old_T % % We can use modified new_M1 & old_M1 to substitute new_M & old_M % new_M1 * new_XYZvox == old_M1 * old_XYZvox % % where: M1 = M; M1(:,4) = M(:,4) - sum(M(:,1:3),2); % and: M(:,4) == [T; 1] == sum(M1,2) % % Therefore: old_XYZvox = old_M1 \ new_M1 * new_XYZvox; % % Since we are traverse Z axis, and new_XYZvox is replaced % by mask_Z * new_XYZvox, the above formula can be rewritten % as: old_XYZvox = old_M1 \ new_M1 * mask_Z * new_XYZvox; % % i.e. we find the mapping from new_XYZvox to old_XYZvox: % M = old_M1 \ new_M1 * mask_Z; % % First, compute modified old_M1 & new_M1 % old_M1 = old_M; old_M1(:,4) = old_M(:,4) - sum(old_M(:,1:3),2); new_M1 = new_M; new_M1(:,4) = new_M(:,4) - sum(new_M(:,1:3),2); % Then, apply unit increment of mask_Z(3,4) to simulate the % cursor movement % mask_Z(3,4) = z; % Here is the mapping from new_XYZvox to old_XYZvox % M = old_M1 \ new_M1 * mask_Z; switch method case 1 new_img(:,:,z) = trilinear(old_img, new_dim, old_dim, M, bg); case 2 new_img(:,:,z) = nearest_neighbor(old_img, new_dim, old_dim, M, bg); case 3 new_img(:,:,z) = bresenham(old_img, new_dim, old_dim, M, bg); end end; % for z if ndims(old_img) == 2 new_M(3,:) = []; new_M(:,3) = []; end return; % affine %-------------------------------------------------------------------- function img_slice = trilinear(img, dim1, dim2, M, bg) img_slice = zeros(dim1(1:2)); TINY = 5e-2; % tolerance % Dimension of transformed 3D volume % xdim1 = dim1(1); ydim1 = dim1(2); % Dimension of original 3D volume % xdim2 = dim2(1); ydim2 = dim2(2); zdim2 = dim2(3); % initialize new_Y accumulation % Y2X = 0; Y2Y = 0; Y2Z = 0; for y = 1:ydim1 % increment of new_Y accumulation % Y2X = Y2X + M(1,2); % new_Y to old_X Y2Y = Y2Y + M(2,2); % new_Y to old_Y Y2Z = Y2Z + M(3,2); % new_Y to old_Z % backproject new_Y accumulation and translation to old_XYZ % old_X = Y2X + M(1,4); old_Y = Y2Y + M(2,4); old_Z = Y2Z + M(3,4); for x = 1:xdim1 % accumulate the increment of new_X, and apply it % to the backprojected old_XYZ % old_X = M(1,1) + old_X ; old_Y = M(2,1) + old_Y ; old_Z = M(3,1) + old_Z ; % within boundary of original image % if ( old_X > 1-TINY & old_X < xdim2+TINY & ... old_Y > 1-TINY & old_Y < ydim2+TINY & ... old_Z > 1-TINY & old_Z < zdim2+TINY ) % Calculate distance of old_XYZ to its neighbors for % weighted intensity average % dx = old_X - floor(old_X); dy = old_Y - floor(old_Y); dz = old_Z - floor(old_Z); x000 = floor(old_X); x100 = x000 + 1; if floor(old_X) < 1 x000 = 1; x100 = x000; elseif floor(old_X) > xdim2-1 x000 = xdim2; x100 = x000; end x010 = x000; x001 = x000; x011 = x000; x110 = x100; x101 = x100; x111 = x100; y000 = floor(old_Y); y010 = y000 + 1; if floor(old_Y) < 1 y000 = 1; y100 = y000; elseif floor(old_Y) > ydim2-1 y000 = ydim2; y010 = y000; end y100 = y000; y001 = y000; y101 = y000; y110 = y010; y011 = y010; y111 = y010; z000 = floor(old_Z); z001 = z000 + 1; if floor(old_Z) < 1 z000 = 1; z001 = z000; elseif floor(old_Z) > zdim2-1 z000 = zdim2; z001 = z000; end z100 = z000; z010 = z000; z110 = z000; z101 = z001; z011 = z001; z111 = z001; x010 = x000; x001 = x000; x011 = x000; x110 = x100; x101 = x100; x111 = x100; v000 = double(img(x000, y000, z000)); v010 = double(img(x010, y010, z010)); v001 = double(img(x001, y001, z001)); v011 = double(img(x011, y011, z011)); v100 = double(img(x100, y100, z100)); v110 = double(img(x110, y110, z110)); v101 = double(img(x101, y101, z101)); v111 = double(img(x111, y111, z111)); img_slice(x,y) = v000*(1-dx)*(1-dy)*(1-dz) + ... v010*(1-dx)*dy*(1-dz) + ... v001*(1-dx)*(1-dy)*dz + ... v011*(1-dx)*dy*dz + ... v100*dx*(1-dy)*(1-dz) + ... v110*dx*dy*(1-dz) + ... v101*dx*(1-dy)*dz + ... v111*dx*dy*dz; else img_slice(x,y) = bg; end % if boundary end % for x end % for y return; % trilinear %-------------------------------------------------------------------- function img_slice = nearest_neighbor(img, dim1, dim2, M, bg) img_slice = zeros(dim1(1:2)); % Dimension of transformed 3D volume % xdim1 = dim1(1); ydim1 = dim1(2); % Dimension of original 3D volume % xdim2 = dim2(1); ydim2 = dim2(2); zdim2 = dim2(3); % initialize new_Y accumulation % Y2X = 0; Y2Y = 0; Y2Z = 0; for y = 1:ydim1 % increment of new_Y accumulation % Y2X = Y2X + M(1,2); % new_Y to old_X Y2Y = Y2Y + M(2,2); % new_Y to old_Y Y2Z = Y2Z + M(3,2); % new_Y to old_Z % backproject new_Y accumulation and translation to old_XYZ % old_X = Y2X + M(1,4); old_Y = Y2Y + M(2,4); old_Z = Y2Z + M(3,4); for x = 1:xdim1 % accumulate the increment of new_X and apply it % to the backprojected old_XYZ % old_X = M(1,1) + old_X ; old_Y = M(2,1) + old_Y ; old_Z = M(3,1) + old_Z ; xi = round(old_X); yi = round(old_Y); zi = round(old_Z); % within boundary of original image % if ( xi >= 1 & xi <= xdim2 & ... yi >= 1 & yi <= ydim2 & ... zi >= 1 & zi <= zdim2 ) img_slice(x,y) = img(xi,yi,zi); else img_slice(x,y) = bg; end % if boundary end % for x end % for y return; % nearest_neighbor %-------------------------------------------------------------------- function img_slice = bresenham(img, dim1, dim2, M, bg) img_slice = zeros(dim1(1:2)); % Dimension of transformed 3D volume % xdim1 = dim1(1); ydim1 = dim1(2); % Dimension of original 3D volume % xdim2 = dim2(1); ydim2 = dim2(2); zdim2 = dim2(3); for y = 1:ydim1 start_old_XYZ = round(M*[0 y 0 1]'); end_old_XYZ = round(M*[xdim1 y 0 1]'); [X Y Z] = bresenham_line3d(start_old_XYZ, end_old_XYZ); % line error correction % % del = end_old_XYZ - start_old_XYZ; % del_dom = max(del); % idx_dom = find(del==del_dom); % idx_dom = idx_dom(1); % idx_other = [1 2 3]; % idx_other(idx_dom) = []; %del_x1 = del(idx_other(1)); % del_x2 = del(idx_other(2)); % line_slope = sqrt((del_x1/del_dom)^2 + (del_x2/del_dom)^2 + 1); % line_error = line_slope - 1; % line error correction removed because it is too slow for x = 1:xdim1 % rescale ratio % i = round(x * length(X) / xdim1); if i < 1 i = 1; elseif i > length(X) i = length(X); end xi = X(i); yi = Y(i); zi = Z(i); % within boundary of the old XYZ space % if ( xi >= 1 & xi <= xdim2 & ... yi >= 1 & yi <= ydim2 & ... zi >= 1 & zi <= zdim2 ) img_slice(x,y) = img(xi,yi,zi); % if line_error > 1 % x = x + 1; % if x <= xdim1 % img_slice(x,y) = img(xi,yi,zi); % line_error = line_slope - 1; % end % end % if line_error % line error correction removed because it is too slow else img_slice(x,y) = bg; end % if boundary end % for x end % for y return; % bresenham
github
sunhongfu/scripts-master
load_untouch_nii_img.m
.m
scripts-master/cs-phase/_src/_nii/load_untouch_nii_img.m
15,224
utf_8
46fb6696904467f1848e2882cd7a72f6
% internal function % - Jimmy Shen ([email protected]) function [img,hdr] = load_untouch_nii_img(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx) if ~exist('hdr','var') | ~exist('filetype','var') | ~exist('fileprefix','var') | ~exist('machine','var') error('Usage: [img,hdr] = load_nii_img(hdr,filetype,fileprefix,machine,[img_idx],[dim5_idx],[dim6_idx],[dim7_idx],[old_RGB],[slice_idx]);'); end if ~exist('img_idx','var') | isempty(img_idx) | hdr.dime.dim(5)<1 img_idx = []; end if ~exist('dim5_idx','var') | isempty(dim5_idx) | hdr.dime.dim(6)<1 dim5_idx = []; end if ~exist('dim6_idx','var') | isempty(dim6_idx) | hdr.dime.dim(7)<1 dim6_idx = []; end if ~exist('dim7_idx','var') | isempty(dim7_idx) | hdr.dime.dim(8)<1 dim7_idx = []; end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end if ~exist('slice_idx','var') | isempty(slice_idx) | hdr.dime.dim(4)<1 slice_idx = []; end % check img_idx % if ~isempty(img_idx) & ~isnumeric(img_idx) error('"img_idx" should be a numerical array.'); end if length(unique(img_idx)) ~= length(img_idx) error('Duplicate image index in "img_idx"'); end if ~isempty(img_idx) & (min(img_idx) < 1 | max(img_idx) > hdr.dime.dim(5)) max_range = hdr.dime.dim(5); if max_range == 1 error(['"img_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"img_idx" should be an integer within the range of [' range '].']); end end % check dim5_idx % if ~isempty(dim5_idx) & ~isnumeric(dim5_idx) error('"dim5_idx" should be a numerical array.'); end if length(unique(dim5_idx)) ~= length(dim5_idx) error('Duplicate index in "dim5_idx"'); end if ~isempty(dim5_idx) & (min(dim5_idx) < 1 | max(dim5_idx) > hdr.dime.dim(6)) max_range = hdr.dime.dim(6); if max_range == 1 error(['"dim5_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim5_idx" should be an integer within the range of [' range '].']); end end % check dim6_idx % if ~isempty(dim6_idx) & ~isnumeric(dim6_idx) error('"dim6_idx" should be a numerical array.'); end if length(unique(dim6_idx)) ~= length(dim6_idx) error('Duplicate index in "dim6_idx"'); end if ~isempty(dim6_idx) & (min(dim6_idx) < 1 | max(dim6_idx) > hdr.dime.dim(7)) max_range = hdr.dime.dim(7); if max_range == 1 error(['"dim6_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim6_idx" should be an integer within the range of [' range '].']); end end % check dim7_idx % if ~isempty(dim7_idx) & ~isnumeric(dim7_idx) error('"dim7_idx" should be a numerical array.'); end if length(unique(dim7_idx)) ~= length(dim7_idx) error('Duplicate index in "dim7_idx"'); end if ~isempty(dim7_idx) & (min(dim7_idx) < 1 | max(dim7_idx) > hdr.dime.dim(8)) max_range = hdr.dime.dim(8); if max_range == 1 error(['"dim7_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim7_idx" should be an integer within the range of [' range '].']); end end % check slice_idx % if ~isempty(slice_idx) & ~isnumeric(slice_idx) error('"slice_idx" should be a numerical array.'); end if length(unique(slice_idx)) ~= length(slice_idx) error('Duplicate index in "slice_idx"'); end if ~isempty(slice_idx) & (min(slice_idx) < 1 | max(slice_idx) > hdr.dime.dim(4)) max_range = hdr.dime.dim(4); if max_range == 1 error(['"slice_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"slice_idx" should be an integer within the range of [' range '].']); end end [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx); return % load_nii_img %--------------------------------------------------------------------- function [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx) switch filetype case {0, 1} fn = [fileprefix '.img']; case 2 fn = [fileprefix '.nii']; end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); end % Set bitpix according to datatype % % /*Acceptable values for datatype are*/ % % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN % 1 Binary (ubit1, bitpix=1) % DT_BINARY % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8 % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16 % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32 % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32 % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64 % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64 % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24 % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8 % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96 % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16 % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32 % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64 % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64 % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128 % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % switch hdr.dime.datatype case 1, hdr.dime.bitpix = 1; precision = 'ubit1'; case 2, hdr.dime.bitpix = 8; precision = 'uint8'; case 4, hdr.dime.bitpix = 16; precision = 'int16'; case 8, hdr.dime.bitpix = 32; precision = 'int32'; case 16, hdr.dime.bitpix = 32; precision = 'float32'; case 32, hdr.dime.bitpix = 64; precision = 'float32'; case 64, hdr.dime.bitpix = 64; precision = 'float64'; case 128, hdr.dime.bitpix = 24; precision = 'uint8'; case 256 hdr.dime.bitpix = 8; precision = 'int8'; case 511 hdr.dime.bitpix = 96; precision = 'float32'; case 512 hdr.dime.bitpix = 16; precision = 'uint16'; case 768 hdr.dime.bitpix = 32; precision = 'uint32'; case 1024 hdr.dime.bitpix = 64; precision = 'int64'; case 1280 hdr.dime.bitpix = 64; precision = 'uint64'; case 1792, hdr.dime.bitpix = 128; precision = 'float64'; otherwise error('This datatype is not supported'); end tmp = hdr.dime.dim(2:end); tmp(find(tmp < 1)) = 1; hdr.dime.dim(2:end) = tmp; % move pointer to the start of image block % switch filetype case {0, 1} fseek(fid, 0, 'bof'); case 2 fseek(fid, hdr.dime.vox_offset, 'bof'); end % Load whole image block for old Analyze format or binary image; % otherwise, load images that are specified in img_idx, dim5_idx, % dim6_idx, and dim7_idx % % For binary image, we have to read all because pos can not be % seeked in bit and can not be calculated the way below. % if hdr.dime.datatype == 1 | isequal(hdr.dime.dim(4:8),ones(1,5)) | ... (isempty(img_idx) & isempty(dim5_idx) & isempty(dim6_idx) & isempty(dim7_idx) & isempty(slice_idx)) % For each frame, precision of value will be read % in img_siz times, where img_siz is only the % dimension size of an image, not the byte storage % size of an image. % img_siz = prod(hdr.dime.dim(2:8)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end img = fread(fid, img_siz, sprintf('*%s',precision)); d1 = hdr.dime.dim(2); d2 = hdr.dime.dim(3); d3 = hdr.dime.dim(4); d4 = hdr.dime.dim(5); d5 = hdr.dime.dim(6); d6 = hdr.dime.dim(7); d7 = hdr.dime.dim(8); if isempty(slice_idx) slice_idx = 1:d3; end if isempty(img_idx) img_idx = 1:d4; end if isempty(dim5_idx) dim5_idx = 1:d5; end if isempty(dim6_idx) dim6_idx = 1:d6; end if isempty(dim7_idx) dim7_idx = 1:d7; end else d1 = hdr.dime.dim(2); d2 = hdr.dime.dim(3); d3 = hdr.dime.dim(4); d4 = hdr.dime.dim(5); d5 = hdr.dime.dim(6); d6 = hdr.dime.dim(7); d7 = hdr.dime.dim(8); if isempty(slice_idx) slice_idx = 1:d3; end if isempty(img_idx) img_idx = 1:d4; end if isempty(dim5_idx) dim5_idx = 1:d5; end if isempty(dim6_idx) dim6_idx = 1:d6; end if isempty(dim7_idx) dim7_idx = 1:d7; end %ROMAN: begin roman = 1; if(roman) % compute size of one slice % img_siz = prod(hdr.dime.dim(2:3)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end % preallocate img img = zeros(img_siz, length(slice_idx)*length(img_idx)*length(dim5_idx)*length(dim6_idx)*length(dim7_idx) ); currentIndex = 1; else img = []; end; %if(roman) % ROMAN: end for i7=1:length(dim7_idx) for i6=1:length(dim6_idx) for i5=1:length(dim5_idx) for t=1:length(img_idx) for s=1:length(slice_idx) % Position is seeked in bytes. To convert dimension size % to byte storage size, hdr.dime.bitpix/8 will be % applied. % pos = sub2ind([d1 d2 d3 d4 d5 d6 d7], 1, 1, slice_idx(s), ... img_idx(t), dim5_idx(i5),dim6_idx(i6),dim7_idx(i7)) -1; pos = pos * hdr.dime.bitpix/8; % ROMAN: begin if(roman) % do nothing else img_siz = prod(hdr.dime.dim(2:3)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end end; % if (roman) % ROMAN: end if filetype == 2 fseek(fid, pos + hdr.dime.vox_offset, 'bof'); else fseek(fid, pos, 'bof'); end % For each frame, fread will read precision of value % in img_siz times % % ROMAN: begin if(roman) img(:,currentIndex) = fread(fid, img_siz, sprintf('*%s',precision)); currentIndex = currentIndex +1; else img = [img fread(fid, img_siz, sprintf('*%s',precision))]; end; %if(roman) % ROMAN: end end end end end end end % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img = reshape(img, [2, length(img)/2]); img = complex(img(1,:)', img(2,:)'); end fclose(fid); % Update the global min and max values % hdr.dime.glmax = double(max(img(:))); hdr.dime.glmin = double(min(img(:))); % old_RGB treat RGB slice by slice, now it is treated voxel by voxel % if old_RGB & hdr.dime.datatype == 128 & hdr.dime.bitpix == 24 % remove squeeze img = (reshape(img, [hdr.dime.dim(2:3) 3 length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [1 2 4 3 5 6 7 8]); elseif hdr.dime.datatype == 128 & hdr.dime.bitpix == 24 % remove squeeze img = (reshape(img, [3 hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [2 3 4 1 5 6 7 8]); elseif hdr.dime.datatype == 511 & hdr.dime.bitpix == 96 img = double(img(:)); img = single((img - min(img))/(max(img) - min(img))); % remove squeeze img = (reshape(img, [3 hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [2 3 4 1 5 6 7 8]); else % remove squeeze img = (reshape(img, [hdr.dime.dim(2:3) length(slice_idx) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); end if ~isempty(slice_idx) hdr.dime.dim(4) = length(slice_idx); end if ~isempty(img_idx) hdr.dime.dim(5) = length(img_idx); end if ~isempty(dim5_idx) hdr.dime.dim(6) = length(dim5_idx); end if ~isempty(dim6_idx) hdr.dime.dim(7) = length(dim6_idx); end if ~isempty(dim7_idx) hdr.dime.dim(8) = length(dim7_idx); end return % read_image
github
sunhongfu/scripts-master
load_untouch_nii.m
.m
scripts-master/cs-phase/_src/_nii/load_untouch_nii.m
6,373
utf_8
303eb6438d7d37e2144d554504fbdf54
% Load NIFTI or ANALYZE dataset, but not applying any appropriate affine % geometric transform or voxel intensity scaling. % % Although according to NIFTI website, all those header information are % supposed to be applied to the loaded NIFTI image, there are some % situations that people do want to leave the original NIFTI header and % data untouched. They will probably just use MATLAB to do certain image % processing regardless of image orientation, and to save data back with % the same NIfTI header. % % Since this program is only served for those situations, please use it % together with "save_untouch_nii.m", and do not use "save_nii.m" or % "view_nii.m" for the data that is loaded by "load_untouch_nii.m". For % normal situation, you should use "load_nii.m" instead. % % Usage: nii = load_untouch_nii(filename, [img_idx], [dim5_idx], [dim6_idx], ... % [dim7_idx], [old_RGB], [slice_idx]) % % filename - NIFTI or ANALYZE file name. % % img_idx (optional) - a numerical array of image volume indices. % Only the specified volumes will be loaded. All available image % volumes will be loaded, if it is default or empty. % % The number of images scans can be obtained from get_nii_frame.m, % or simply: hdr.dime.dim(5). % % dim5_idx (optional) - a numerical array of 5th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % dim6_idx (optional) - a numerical array of 6th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % dim7_idx (optional) - a numerical array of 7th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % old_RGB (optional) - a scale number to tell difference of new RGB24 % from old RGB24. New RGB24 uses RGB triple sequentially for each % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for % each slices. If the image that you view is garbled, try to set % old_RGB variable to 1 and try again, because it could be in % old RGB24. It will be set to 0, if it is default or empty. % % slice_idx (optional) - a numerical array of image slice indices. % Only the specified slices will be loaded. All available image % slices will be loaded, if it is default or empty. % % Returned values: % % nii structure: % % hdr - struct with NIFTI header fields. % % filetype - Analyze format .hdr/.img (0); % NIFTI .hdr/.img (1); % NIFTI .nii (2) % % fileprefix - NIFTI filename without extension. % % machine - machine string variable. % % img - 3D (or 4D) matrix of NIFTI data. % % - Jimmy Shen ([email protected]) % function nii = load_untouch_nii(filename, img_idx, dim5_idx, dim6_idx, dim7_idx, ... old_RGB, slice_idx) if ~exist('filename','var') error('Usage: nii = load_untouch_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [slice_idx])'); end if ~exist('img_idx','var') | isempty(img_idx) img_idx = []; end if ~exist('dim5_idx','var') | isempty(dim5_idx) dim5_idx = []; end if ~exist('dim6_idx','var') | isempty(dim6_idx) dim6_idx = []; end if ~exist('dim7_idx','var') | isempty(dim7_idx) dim7_idx = []; end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end if ~exist('slice_idx','var') | isempty(slice_idx) slice_idx = []; end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end % Read the dataset header % [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename); if nii.filetype == 0 nii.hdr = load_untouch0_nii_hdr(nii.fileprefix,nii.machine); nii.ext = []; else nii.hdr = load_untouch_nii_hdr(nii.fileprefix,nii.machine,nii.filetype); % Read the header extension % nii.ext = load_nii_ext(filename); end % Read the dataset body % [nii.img,nii.hdr] = load_untouch_nii_img(nii.hdr,nii.filetype,nii.fileprefix, ... nii.machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB,slice_idx); % Perform some of sform/qform transform % % nii = xform_nii(nii, tolerance, preferredForm); nii.untouch = 1; % Clean up after gunzip % if exist('gzFileName', 'var') % fix fileprefix so it doesn't point to temp location % nii.fileprefix = gzFileName(1:end-7); rmdir(tmpDir,'s'); end return % load_untouch_nii
github
sunhongfu/scripts-master
collapse_nii_scan.m
.m
scripts-master/cs-phase/_src/_nii/collapse_nii_scan.m
7,038
utf_8
2d30d10b884719503df2974ff39b7093
% Collapse multiple single-scan NIFTI files into a multiple-scan NIFTI file % % Usage: collapse_nii_scan(scan_file_pattern, [collapsed_fileprefix], [scan_file_folder]) % % Here, scan_file_pattern should look like: 'myscan_0*.img' % If collapsed_fileprefix is omit, 'multi_scan' will be used % If scan_file_folder is omit, current file folder will be used % % The order of volumes in the collapsed file will be the order of % corresponding filenames for those selected scan files. % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function collapse_nii_scan(scan_pattern, fileprefix, scan_path) if ~exist('fileprefix','var') fileprefix = 'multi_scan'; else [tmp fileprefix] = fileparts(fileprefix); end if ~exist('scan_path','var'), scan_path = pwd; end pnfn = fullfile(scan_path, scan_pattern); file_lst = dir(pnfn); flist = {file_lst.name}; flist = flist(:); filename = flist{1}; v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); else gzFile = 1; end else if ~strcmp(filename(end-3:end), '.img') & ... ~strcmp(filename(end-3:end), '.hdr') & ... ~strcmp(filename(end-3:end), '.nii') error('Please check filename.'); end end nii = load_untouch_nii(fullfile(scan_path,filename)); nii.hdr.dime.dim(5) = length(flist); if nii.hdr.dime.dim(1) < 4 nii.hdr.dime.dim(1) = 4; end hdr = nii.hdr; filetype = nii.filetype; if isfield(nii,'ext') & ~isempty(nii.ext) ext = nii.ext; [ext, esize_total] = verify_nii_ext(ext); else ext = []; end switch double(hdr.dime.datatype), case 1, hdr.dime.bitpix = int16(1 ); precision = 'ubit1'; case 2, hdr.dime.bitpix = int16(8 ); precision = 'uint8'; case 4, hdr.dime.bitpix = int16(16); precision = 'int16'; case 8, hdr.dime.bitpix = int16(32); precision = 'int32'; case 16, hdr.dime.bitpix = int16(32); precision = 'float32'; case 32, hdr.dime.bitpix = int16(64); precision = 'float32'; case 64, hdr.dime.bitpix = int16(64); precision = 'float64'; case 128, hdr.dime.bitpix = int16(24); precision = 'uint8'; case 256 hdr.dime.bitpix = int16(8 ); precision = 'int8'; case 512 hdr.dime.bitpix = int16(16); precision = 'uint16'; case 768 hdr.dime.bitpix = int16(32); precision = 'uint32'; case 1024 hdr.dime.bitpix = int16(64); precision = 'int64'; case 1280 hdr.dime.bitpix = int16(64); precision = 'uint64'; case 1792, hdr.dime.bitpix = int16(128); precision = 'float64'; otherwise error('This datatype is not supported'); end if filetype == 2 fid = fopen(sprintf('%s.nii',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.nii.',fileprefix); error(msg); end hdr.dime.vox_offset = 352; if ~isempty(ext) hdr.dime.vox_offset = hdr.dime.vox_offset + esize_total; end hdr.hist.magic = 'n+1'; save_untouch_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end elseif filetype == 1 fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end hdr.dime.vox_offset = 0; hdr.hist.magic = 'ni1'; save_untouch_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end fclose(fid); fid = fopen(sprintf('%s.img',fileprefix),'w'); else fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end save_untouch0_nii_hdr(hdr, fid); fclose(fid); fid = fopen(sprintf('%s.img',fileprefix),'w'); end if filetype == 2 & isempty(ext) skip_bytes = double(hdr.dime.vox_offset) - 348; else skip_bytes = 0; end if skip_bytes fwrite(fid, zeros(1,skip_bytes), 'uint8'); end glmax = -inf; glmin = inf; for i = 1:length(flist) nii = load_untouch_nii(fullfile(scan_path,flist{i})); if double(hdr.dime.datatype) == 128 % RGB planes are expected to be in the 4th dimension of nii.img % if(size(nii.img,4)~=3) error(['The NII structure does not appear to have 3 RGB color planes in the 4th dimension']); end nii.img = permute(nii.img, [4 1 2 3 5 6 7 8]); end % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 real_img = real(nii.img(:))'; nii.img = imag(nii.img(:))'; nii.img = [real_img; nii.img]; end if nii.hdr.dime.glmax > glmax glmax = nii.hdr.dime.glmax; end if nii.hdr.dime.glmin < glmin glmin = nii.hdr.dime.glmin; end fwrite(fid, nii.img, precision); end hdr.dime.glmax = round(glmax); hdr.dime.glmin = round(glmin); if filetype == 2 fseek(fid, 140, 'bof'); fwrite(fid, hdr.dime.glmax, 'int32'); fwrite(fid, hdr.dime.glmin, 'int32'); elseif filetype == 1 fid2 = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid2 < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end save_untouch_nii_hdr(hdr, fid2); if ~isempty(ext) save_nii_ext(ext, fid2); end fclose(fid2); else fid2 = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid2 < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end save_untouch0_nii_hdr(hdr, fid2); fclose(fid2); end fclose(fid); % gzip output file if requested % if exist('gzFile', 'var') if filetype == 1 gzip([fileprefix, '.img']); delete([fileprefix, '.img']); gzip([fileprefix, '.hdr']); delete([fileprefix, '.hdr']); elseif filetype == 2 gzip([fileprefix, '.nii']); delete([fileprefix, '.nii']); end; end; return; % collapse_nii_scan
github
sunhongfu/scripts-master
rri_orient_ui.m
.m
scripts-master/cs-phase/_src/_nii/rri_orient_ui.m
5,635
utf_8
3361ce417798ffe2c6b53cf194b2a146
% Return orientation of the current image: % orient is orientation 1x3 matrix, in that: % Three elements represent: [x y z] % Element value: 1 - Left to Right; 2 - Posterior to Anterior; % 3 - Inferior to Superior; 4 - Right to Left; % 5 - Anterior to Posterior; 6 - Superior to Inferior; % e.g.: % Standard RAS Orientation: [1 2 3] % Standard RHOS Orientation: [2 4 3] % Jimmy Shen ([email protected]), 26-APR-04 % function orient = rri_orient_ui(varargin) if nargin == 0 init; orient_ui_fig = gcf; uiwait; % wait for user finish orient = getappdata(gcf, 'orient'); if isempty(orient) orient = [1 2 3]; end if ishandle(orient_ui_fig) close(gcf); end return; end action = varargin{1}; if strcmp(action, 'done') click_done; elseif strcmp(action, 'cancel') uiresume; end return; % rri_orient_ui %---------------------------------------------------------------------- function init save_setting_status = 'on'; rri_orient_pos = []; try load('pls_profile'); catch end try load('rri_pos_profile'); catch end if ~isempty(rri_orient_pos) & strcmp(save_setting_status,'on') pos = rri_orient_pos; else w = 0.35; h = 0.4; x = (1-w)/2; y = (1-h)/2; pos = [x y w h]; end handles.figure = figure('Color',[0.8 0.8 0.8], ... 'Units','normal', ... 'Name', 'Convert to standard RAS orientation', ... 'NumberTitle','off', ... 'MenuBar','none', ... 'Position',pos, ... 'WindowStyle', 'normal', ... 'ToolBar','none'); h0 = handles.figure; Font.FontUnits = 'point'; Font.FontSize = 12; margin = .1; line_num = 6; line_ht = (1 - margin*2) / line_num; x = margin; y = 1 - margin - line_ht; w = 1 - margin * 2; h = line_ht * .7; pos = [x y w h]; handles.Ttit = uicontrol('parent', h0, ... 'style','text', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'background', [0.8 0.8 0.8], ... 'string', 'Please input orientation of the current image:'); y = y - line_ht; w = .2; pos = [x y w h]; handles.Tx_orient = uicontrol('parent', h0, ... 'style','text', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'background', [0.8 0.8 0.8], ... 'string', 'X Axes:'); y = y - line_ht; pos = [x y w h]; handles.Ty_orient = uicontrol('parent', h0, ... 'style','text', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'background', [0.8 0.8 0.8], ... 'string', 'Y Axes:'); y = y - line_ht; pos = [x y w h]; handles.Tz_orient = uicontrol('parent', h0, ... 'style','text', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'background', [0.8 0.8 0.8], ... 'string', 'Z Axes:'); choice = { 'From Left to Right', 'From Posterior to Anterior', ... 'From Inferior to Superior', 'From Right to Left', ... 'From Anterior to Posterior', 'From Superior to Inferior' }; y = 1 - margin - line_ht; y = y - line_ht; w = 1 - margin - x - w; x = 1 - margin - w; pos = [x y w h]; handles.x_orient = uicontrol('parent', h0, ... 'style','popupmenu', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'string', choice, ... 'value', 1, ... 'background', [1 1 1]); y = y - line_ht; pos = [x y w h]; handles.y_orient = uicontrol('parent', h0, ... 'style','popupmenu', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'string', choice, ... 'value', 2, ... 'background', [1 1 1]); y = y - line_ht; pos = [x y w h]; handles.z_orient = uicontrol('parent', h0, ... 'style','popupmenu', ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','left',... 'string', choice, ... 'value', 3, ... 'background', [1 1 1]); x = margin; y = y - line_ht * 1.5; w = .3; pos = [x y w h]; handles.done = uicontrol('parent', h0, ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','center',... 'callback', 'rri_orient_ui(''done'');', ... 'string', 'Done'); x = 1 - margin - w; pos = [x y w h]; handles.cancel = uicontrol('parent', h0, ... 'unit', 'normal', ... Font, ... 'Position',pos, ... 'HorizontalAlignment','center',... 'callback', 'rri_orient_ui(''cancel'');', ... 'string', 'Cancel'); setappdata(h0, 'handles', handles); setappdata(h0, 'orient', [1 2 3]); return; % init %---------------------------------------------------------------------- function click_done handles = getappdata(gcf, 'handles'); x_orient = get(handles.x_orient, 'value'); y_orient = get(handles.y_orient, 'value'); z_orient = get(handles.z_orient, 'value'); orient = [x_orient y_orient z_orient]; test_orient = [orient, orient + 3]; test_orient = mod(test_orient, 3); if length(unique(test_orient)) ~= 3 msgbox('Please don''t choose same or opposite direction','Error','modal'); return; end setappdata(gcf, 'orient', [x_orient y_orient z_orient]); uiresume; return; % click_done
github
sunhongfu/scripts-master
load_untouch0_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/load_untouch0_nii_hdr.m
8,293
utf_8
d823050e9ba931a2ba7f9d9a3893d2d1
% internal function % - Jimmy Shen ([email protected]) function hdr = load_nii_hdr(fileprefix, machine) fn = sprintf('%s.hdr',fileprefix); fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); hdr = read_header(fid); fclose(fid); end return % load_nii_hdr %--------------------------------------------------------------------- function [ dsr ] = read_header(fid) % Original header structures % struct dsr % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ dsr.hk = header_key(fid); dsr.dime = image_dimension(fid); dsr.hist = data_history(fid); return % read_header %--------------------------------------------------------------------- function [ hk ] = header_key(fid) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ % % int sizeof_header Should be 348. % char regular Must be 'r' to indicate that all images and % volumes are the same size. v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hk.sizeof_hdr = fread(fid, 1,'int32')'; % should be 348! hk.data_type = deblank(fread(fid,10,directchar)'); hk.db_name = deblank(fread(fid,18,directchar)'); hk.extents = fread(fid, 1,'int32')'; hk.session_error = fread(fid, 1,'int16')'; hk.regular = fread(fid, 1,directchar)'; hk.hkey_un0 = fread(fid, 1,directchar)'; return % header_key %--------------------------------------------------------------------- function [ dime ] = image_dimension(fid) %struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % /* % dim[0] Number of dimensions in database; usually 4. % dim[1] Image X dimension; number of *pixels* in an image row. % dim[2] Image Y dimension; number of *pixel rows* in slice. % dim[3] Volume Z dimension; number of *slices* in a volume. % dim[4] Time points; number of volumes in database % */ % char vox_units[4]; /* 16 + 4 */ % char cal_units[8]; /* 20 + 8 */ % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width, mm % pixdim[2] - voxel height, mm % pixdim[3] - slice thickness, mm % pixdim[4] - volume timing, in msec % ..etc % */ % float vox_offset; /* 68 + 4 */ % float roi_scale; /* 72 + 4 */ % float funused1; /* 76 + 4 */ % float funused2; /* 80 + 4 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % int compressed; /* 92 + 4 */ % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end dime.dim = fread(fid,8,'int16')'; dime.vox_units = deblank(fread(fid,4,directchar)'); dime.cal_units = deblank(fread(fid,8,directchar)'); dime.unused1 = fread(fid,1,'int16')'; dime.datatype = fread(fid,1,'int16')'; dime.bitpix = fread(fid,1,'int16')'; dime.dim_un0 = fread(fid,1,'int16')'; dime.pixdim = fread(fid,8,'float32')'; dime.vox_offset = fread(fid,1,'float32')'; dime.roi_scale = fread(fid,1,'float32')'; dime.funused1 = fread(fid,1,'float32')'; dime.funused2 = fread(fid,1,'float32')'; dime.cal_max = fread(fid,1,'float32')'; dime.cal_min = fread(fid,1,'float32')'; dime.compressed = fread(fid,1,'int32')'; dime.verified = fread(fid,1,'int32')'; dime.glmax = fread(fid,1,'int32')'; dime.glmin = fread(fid,1,'int32')'; return % image_dimension %--------------------------------------------------------------------- function [ hist ] = data_history(fid) %struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % char orient; /* 104 + 1 */ % char originator[10]; /* 105 + 10 */ % char generated[10]; /* 115 + 10 */ % char scannum[10]; /* 125 + 10 */ % char patient_id[10]; /* 135 + 10 */ % char exp_date[10]; /* 145 + 10 */ % char exp_time[10]; /* 155 + 10 */ % char hist_un0[3]; /* 165 + 3 */ % int views /* 168 + 4 */ % int vols_added; /* 172 + 4 */ % int start_field; /* 176 + 4 */ % int field_skip; /* 180 + 4 */ % int omax; /* 184 + 4 */ % int omin; /* 188 + 4 */ % int smax; /* 192 + 4 */ % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hist.descrip = deblank(fread(fid,80,directchar)'); hist.aux_file = deblank(fread(fid,24,directchar)'); hist.orient = fread(fid, 1,'char')'; hist.originator = fread(fid, 5,'int16')'; hist.generated = deblank(fread(fid,10,directchar)'); hist.scannum = deblank(fread(fid,10,directchar)'); hist.patient_id = deblank(fread(fid,10,directchar)'); hist.exp_date = deblank(fread(fid,10,directchar)'); hist.exp_time = deblank(fread(fid,10,directchar)'); hist.hist_un0 = deblank(fread(fid, 3,directchar)'); hist.views = fread(fid, 1,'int32')'; hist.vols_added = fread(fid, 1,'int32')'; hist.start_field = fread(fid, 1,'int32')'; hist.field_skip = fread(fid, 1,'int32')'; hist.omax = fread(fid, 1,'int32')'; hist.omin = fread(fid, 1,'int32')'; hist.smax = fread(fid, 1,'int32')'; hist.smin = fread(fid, 1,'int32')'; return % data_history
github
sunhongfu/scripts-master
load_nii.m
.m
scripts-master/cs-phase/_src/_nii/load_nii.m
7,006
utf_8
71beffc9e2b0c7e14c2f8dc8adbadbf1
% Load NIFTI or ANALYZE dataset. Support both *.nii and *.hdr/*.img % file extension. If file extension is not provided, *.hdr/*.img will % be used as default. % % A subset of NIFTI transform is included. For non-orthogonal rotation, % shearing etc., please use 'reslice_nii.m' to reslice the NIFTI file. % It will not cause negative effect, as long as you remember not to do % slice time correction after reslicing the NIFTI file. Output variable % nii will be in RAS orientation, i.e. X axis from Left to Right, % Y axis from Posterior to Anterior, and Z axis from Inferior to % Superior. % % Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], ... % [dim7_idx], [old_RGB], [tolerance], [preferredForm]) % % filename - NIFTI or ANALYZE file name. % % img_idx (optional) - a numerical array of 4th dimension indices, % which is the indices of image scan volume. The number of images % scan volumes can be obtained from get_nii_frame.m, or simply % hdr.dime.dim(5). Only the specified volumes will be loaded. % All available image volumes will be loaded, if it is default or % empty. % % dim5_idx (optional) - a numerical array of 5th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % dim6_idx (optional) - a numerical array of 6th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % dim7_idx (optional) - a numerical array of 7th dimension indices. % Only the specified range will be loaded. All available range % will be loaded, if it is default or empty. % % old_RGB (optional) - a scale number to tell difference of new RGB24 % from old RGB24. New RGB24 uses RGB triple sequentially for each % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for % each slices. If the image that you view is garbled, try to set % old_RGB variable to 1 and try again, because it could be in % old RGB24. It will be set to 0, if it is default or empty. % % tolerance (optional) - distortion allowed in the loaded image for any % non-orthogonal rotation or shearing of NIfTI affine matrix. If % you set 'tolerance' to 0, it means that you do not allow any % distortion. If you set 'tolerance' to 1, it means that you do % not care any distortion. The image will fail to be loaded if it % can not be tolerated. The tolerance will be set to 0.1 (10%), if % it is default or empty. % % preferredForm (optional) - selects which transformation from voxels % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate % "prefer sform or qform, but use others if preferred not present". % Upper case indicate the program is forced to use the specificied % tranform or fail loading. 'preferredForm' will be 's', if it is % default or empty. - Jeff Gunter % % Returned values: % % nii structure: % % hdr - struct with NIFTI header fields. % % filetype - Analyze format .hdr/.img (0); % NIFTI .hdr/.img (1); % NIFTI .nii (2) % % fileprefix - NIFTI filename without extension. % % machine - machine string variable. % % img - 3D (or 4D) matrix of NIFTI data. % % original - the original header before any affine transform. % % Part of this file is copied and modified from: % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function nii = load_nii(filename, img_idx, dim5_idx, dim6_idx, dim7_idx, ... old_RGB, tolerance, preferredForm) if ~exist('filename','var') error('Usage: nii = load_nii(filename, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx], [old_RGB], [tolerance], [preferredForm])'); end if ~exist('img_idx','var') | isempty(img_idx) img_idx = []; end if ~exist('dim5_idx','var') | isempty(dim5_idx) dim5_idx = []; end if ~exist('dim6_idx','var') | isempty(dim6_idx) dim6_idx = []; end if ~exist('dim7_idx','var') | isempty(dim7_idx) dim7_idx = []; end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end if ~exist('tolerance','var') | isempty(tolerance) tolerance = 0.1; % 10 percent end if ~exist('preferredForm','var') | isempty(preferredForm) preferredForm= 's'; % Jeff end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end % Read the dataset header % [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename); % Read the header extension % % nii.ext = load_nii_ext(filename); % Read the dataset body % [nii.img,nii.hdr] = load_nii_img(nii.hdr,nii.filetype,nii.fileprefix, ... nii.machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB); % Perform some of sform/qform transform % nii = xform_nii(nii, tolerance, preferredForm); % Clean up after gunzip % if exist('gzFileName', 'var') % fix fileprefix so it doesn't point to temp location % nii.fileprefix = gzFileName(1:end-7); rmdir(tmpDir,'s'); end return % load_nii
github
sunhongfu/scripts-master
unxform_nii.m
.m
scripts-master/cs-phase/_src/_nii/unxform_nii.m
1,221
utf_8
ff8be64760837046b931857d59ca304e
% Undo the flipping and rotations performed by xform_nii; spit back only % the raw img data block. Initial cut will only deal with 3D volumes % strongly assume we have called xform_nii to write down the steps used % in xform_nii. % % Usage: a = load_nii('original_name'); % manipulate a.img to make array b; % % if you use unxform_nii to un-tranform the image (img) data % block, then nii.original.hdr is the corresponding header. % % nii.original.img = unxform_nii(a, b); % save_nii(nii.original,'newname'); % % Where, 'newname' is created with data in the same space as the % original_name data % % - Jeff Gunter, 26-JUN-06 % function outblock = unxform_nii(nii, inblock) if isempty(nii.hdr.hist.rot_orient) outblock=inblock; else [dummy unrotate_orient] = sort(nii.hdr.hist.rot_orient); outblock = permute(inblock, unrotate_orient); end if ~isempty(nii.hdr.hist.flip_orient) flip_orient = nii.hdr.hist.flip_orient(unrotate_orient); for i = 1:3 if flip_orient(i) outblock = flipdim(outblock, i); end end end; return;
github
sunhongfu/scripts-master
load_untouch_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/load_untouch_nii_hdr.m
8,739
utf_8
eb068c88e2b7bb518ea557d0734bc65d
% internal function % - Jimmy Shen ([email protected]) function hdr = load_nii_hdr(fileprefix, machine, filetype) if filetype == 2 fn = sprintf('%s.nii',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.nii".', fileprefix); error(msg); end else fn = sprintf('%s.hdr',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.hdr".', fileprefix); error(msg); end end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); hdr = read_header(fid); fclose(fid); end return % load_nii_hdr %--------------------------------------------------------------------- function [ dsr ] = read_header(fid) % Original header structures % struct dsr % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ dsr.hk = header_key(fid); dsr.dime = image_dimension(fid); dsr.hist = data_history(fid); % For Analyze data format % if ~strcmp(dsr.hist.magic, 'n+1') & ~strcmp(dsr.hist.magic, 'ni1') dsr.hist.qform_code = 0; dsr.hist.sform_code = 0; end return % read_header %--------------------------------------------------------------------- function [ hk ] = header_key(fid) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char dim_info; % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ % % int sizeof_header Should be 348. % char regular Must be 'r' to indicate that all images and % volumes are the same size. v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hk.sizeof_hdr = fread(fid, 1,'int32')'; % should be 348! hk.data_type = deblank(fread(fid,10,directchar)'); hk.db_name = deblank(fread(fid,18,directchar)'); hk.extents = fread(fid, 1,'int32')'; hk.session_error = fread(fid, 1,'int16')'; hk.regular = fread(fid, 1,directchar)'; hk.dim_info = fread(fid, 1,'uchar')'; return % header_key %--------------------------------------------------------------------- function [ dime ] = image_dimension(fid) % Original header structures % struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % /* % dim[0] Number of dimensions in database; usually 4. % dim[1] Image X dimension; number of *pixels* in an image row. % dim[2] Image Y dimension; number of *pixel rows* in slice. % dim[3] Volume Z dimension; number of *slices* in a volume. % dim[4] Time points; number of volumes in database % */ % float intent_p1; % char vox_units[4]; /* 16 + 4 */ % float intent_p2; % char cal_units[8]; /* 20 + 4 */ % float intent_p3; % char cal_units[8]; /* 24 + 4 */ % short int intent_code; % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int slice_start; % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width, mm % pixdim[2] - voxel height, mm % pixdim[3] - slice thickness, mm % pixdim[4] - volume timing, in msec % ..etc % */ % float vox_offset; /* 68 + 4 */ % float scl_slope; % float roi_scale; /* 72 + 4 */ % float scl_inter; % float funused1; /* 76 + 4 */ % short slice_end; % float funused2; /* 80 + 2 */ % char slice_code; % float funused2; /* 82 + 1 */ % char xyzt_units; % float funused2; /* 83 + 1 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % float slice_duration; % int compressed; /* 92 + 4 */ % float toffset; % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ dime.dim = fread(fid,8,'int16')'; dime.intent_p1 = fread(fid,1,'float32')'; dime.intent_p2 = fread(fid,1,'float32')'; dime.intent_p3 = fread(fid,1,'float32')'; dime.intent_code = fread(fid,1,'int16')'; dime.datatype = fread(fid,1,'int16')'; dime.bitpix = fread(fid,1,'int16')'; dime.slice_start = fread(fid,1,'int16')'; dime.pixdim = fread(fid,8,'float32')'; dime.vox_offset = fread(fid,1,'float32')'; dime.scl_slope = fread(fid,1,'float32')'; dime.scl_inter = fread(fid,1,'float32')'; dime.slice_end = fread(fid,1,'int16')'; dime.slice_code = fread(fid,1,'uchar')'; dime.xyzt_units = fread(fid,1,'uchar')'; dime.cal_max = fread(fid,1,'float32')'; dime.cal_min = fread(fid,1,'float32')'; dime.slice_duration = fread(fid,1,'float32')'; dime.toffset = fread(fid,1,'float32')'; dime.glmax = fread(fid,1,'int32')'; dime.glmin = fread(fid,1,'int32')'; return % image_dimension %--------------------------------------------------------------------- function [ hist ] = data_history(fid) % Original header structures % struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % short int qform_code; /* 104 + 2 */ % short int sform_code; /* 106 + 2 */ % float quatern_b; /* 108 + 4 */ % float quatern_c; /* 112 + 4 */ % float quatern_d; /* 116 + 4 */ % float qoffset_x; /* 120 + 4 */ % float qoffset_y; /* 124 + 4 */ % float qoffset_z; /* 128 + 4 */ % float srow_x[4]; /* 132 + 16 */ % float srow_y[4]; /* 148 + 16 */ % float srow_z[4]; /* 164 + 16 */ % char intent_name[16]; /* 180 + 16 */ % char magic[4]; % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hist.descrip = deblank(fread(fid,80,directchar)'); hist.aux_file = deblank(fread(fid,24,directchar)'); hist.qform_code = fread(fid,1,'int16')'; hist.sform_code = fread(fid,1,'int16')'; hist.quatern_b = fread(fid,1,'float32')'; hist.quatern_c = fread(fid,1,'float32')'; hist.quatern_d = fread(fid,1,'float32')'; hist.qoffset_x = fread(fid,1,'float32')'; hist.qoffset_y = fread(fid,1,'float32')'; hist.qoffset_z = fread(fid,1,'float32')'; hist.srow_x = fread(fid,4,'float32')'; hist.srow_y = fread(fid,4,'float32')'; hist.srow_z = fread(fid,4,'float32')'; hist.intent_name = deblank(fread(fid,16,directchar)'); hist.magic = deblank(fread(fid,4,directchar)'); return % data_history
github
sunhongfu/scripts-master
save_nii_ext.m
.m
scripts-master/cs-phase/_src/_nii/save_nii_ext.m
1,015
utf_8
db919f3a7a4b2f64dae641b1e97fa4a0
% Save NIFTI header extension. % % Usage: save_nii_ext(ext, fid) % % ext - struct with NIFTI header extension fields. % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function save_nii_ext(ext, fid) if ~exist('ext','var') | ~exist('fid','var') error('Usage: save_nii_ext(ext, fid)'); end if ~isfield(ext,'extension') | ~isfield(ext,'section') | ~isfield(ext,'num_ext') error('Wrong header extension'); end write_ext(ext, fid); return; % save_nii_ext %--------------------------------------------------------------------- function write_ext(ext, fid) fwrite(fid, ext.extension, 'uchar'); for i=1:ext.num_ext fwrite(fid, ext.section(i).esize, 'int32'); fwrite(fid, ext.section(i).ecode, 'int32'); fwrite(fid, ext.section(i).edata, 'uchar'); end return; % write_ext
github
sunhongfu/scripts-master
view_nii_menu.m
.m
scripts-master/cs-phase/_src/_nii/view_nii_menu.m
14,895
utf_8
d81fb80884a14ae659630258fbc330bc
% Imbed Zoom, Interp, and Info menu to view_nii window. % % Usage: view_nii_menu(fig); % % - Jimmy Shen ([email protected]) % %-------------------------------------------------------------------- function menu_hdl = view_nii_menu(fig, varargin) if isnumeric(fig) menu_hdl = init(fig); return; end menu_hdl = []; switch fig case 'interp' if nargin > 1 fig = varargin{1}; else fig = gcbf; end nii_menu = getappdata(fig, 'nii_menu'); interp_on_state = get(nii_menu.Minterp,'Userdata'); if (interp_on_state == 1) opt.useinterp = 1; view_nii(fig,opt); set(nii_menu.Minterp,'Userdata',0,'Label','Interp off'); reset_zoom(fig); else opt.useinterp = 0; view_nii(fig,opt); set(nii_menu.Minterp,'Userdata',1,'Label','Interp on'); reset_zoom(fig); end case 'reset_zoom' if nargin > 1 fig = varargin{1}; else fig = gcbf; end reset_zoom(fig); case 'orient' orient; case 'editvox' editvox; case 'img_info' img_info; case 'img_hist' img_hist; case 'save_disp' save_disp; end return % view_nii_menu %-------------------------------------------------------------------- function menu_hdl = init(fig) % search for edit, view menu % nii_menu.Mfile = []; nii_menu.Medit = []; nii_menu.Mview = []; menuitems = findobj(fig, 'type', 'uimenu'); for i=1:length(menuitems) filelabel = get(menuitems(i),'label'); if strcmpi(strrep(filelabel, '&', ''), 'file') nii_menu.Mfile = menuitems(i); end editlabel = get(menuitems(i),'label'); if strcmpi(strrep(editlabel, '&', ''), 'edit') nii_menu.Medit = menuitems(i); end viewlabel = get(menuitems(i),'label'); if strcmpi(strrep(viewlabel, '&', ''), 'view') nii_menu.Mview = menuitems(i); end end set(fig, 'menubar', 'none'); if isempty(nii_menu.Mfile) nii_menu.Mfile = uimenu('Parent',fig, ... 'Label','File'); nii_menu.Mfile_save = uimenu('Parent',nii_menu.Mfile, ... 'Label','Save displayed image as ...', ... 'Callback','view_nii_menu(''save_disp'');'); else nii_menu.Mfile_save = uimenu('Parent',nii_menu.Mfile, ... 'Label','Save displayed image as ...', ... 'separator','on', ... 'Callback','view_nii_menu(''save_disp'');'); end if isempty(nii_menu.Medit) nii_menu.Medit = uimenu('Parent',fig, ... 'Label','Edit'); nii_menu.Medit_orient = uimenu('Parent',nii_menu.Medit, ... 'Label','Convert to RAS orientation', ... 'Callback','view_nii_menu(''orient'');'); nii_menu.Medit_editvox = uimenu('Parent',nii_menu.Medit, ... 'Label','Edit voxel value at crosshair', ... 'Callback','view_nii_menu(''editvox'');'); else nii_menu.Medit_orient = uimenu('Parent',nii_menu.Medit, ... 'Label','Convert to RAS orientation', ... 'separator','on', ... 'Callback','view_nii_menu(''orient'');'); nii_menu.Medit_editvox = uimenu('Parent',nii_menu.Medit, ... 'Label','Edit voxel value at crosshair', ... 'Callback','view_nii_menu(''editvox'');'); end if isempty(nii_menu.Mview) nii_menu.Mview = uimenu('Parent',fig, ... 'Label','View'); nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ... 'Label','Image Information', ... 'Callback','view_nii_menu(''img_info'');'); nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ... 'Label','Volume Histogram', ... 'Callback','view_nii_menu(''img_hist'');'); else nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ... 'Label','Image Information', ... 'separator','on', ... 'Callback','view_nii_menu(''img_info'');'); nii_menu.Mview_info = uimenu('Parent',nii_menu.Mview, ... 'Label','Volume Histogram', ... 'Callback','view_nii_menu(''img_hist'');'); end nii_menu.Mzoom = rri_zoom_menu(fig); nii_menu.Minterp = uimenu('Parent',fig, ... 'Label','Interp on', ... 'Userdata', 1, ... 'Callback','view_nii_menu(''interp'');'); setappdata(fig,'nii_menu',nii_menu); menu_hdl = nii_menu.Minterp; return % init %---------------------------------------------------------------- function reset_zoom(fig) old_handle_vis = get(fig, 'HandleVisibility'); set(fig, 'HandleVisibility', 'on'); nii_view = getappdata(fig, 'nii_view'); nii_menu = getappdata(fig, 'nii_menu'); set(nii_menu.Mzoom,'Userdata',1,'Label','Zoom on'); set(fig,'pointer','arrow'); zoom off; axes(nii_view.handles.axial_axes); setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ... [get(gca, 'xlim') get(gca, 'ylim')]) % zoom reset; % zoom getlimits; zoom out; axes(nii_view.handles.coronal_axes); setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ... [get(gca, 'xlim') get(gca, 'ylim')]) % zoom reset; % zoom getlimits; zoom out; axes(nii_view.handles.sagittal_axes); setappdata(get(gca,'zlabel'), 'ZOOMAxesData', ... [get(gca, 'xlim') get(gca, 'ylim')]) % zoom reset; % zoom getlimits; zoom out; set(fig, 'HandleVisibility', old_handle_vis); return; % reset_zoom %---------------------------------------------------------------- function img_info nii_view = getappdata(gcbf, 'nii_view'); hdr = nii_view.nii.hdr; max_value = num2str(double(max(nii_view.nii.img(:)))); min_value = num2str(double(min(nii_view.nii.img(:)))); dim = sprintf('%d %d %d', double(hdr.dime.dim(2:4))); vox = sprintf('%.3f %.3f %.3f', double(hdr.dime.pixdim(2:4))); if double(hdr.dime.datatype) == 1 type = '1-bit binary'; elseif double(hdr.dime.datatype) == 2 type = '8-bit unsigned integer'; elseif double(hdr.dime.datatype) == 4 type = '16-bit signed integer'; elseif double(hdr.dime.datatype) == 8 type = '32-bit signed integer'; elseif double(hdr.dime.datatype) == 16 type = '32-bit single float'; elseif double(hdr.dime.datatype) == 64 type = '64-bit double precision'; elseif double(hdr.dime.datatype) == 128 type = '24-bit RGB true color'; elseif double(hdr.dime.datatype) == 256 type = '8-bit signed integer'; elseif double(hdr.dime.datatype) == 511 type = '96-bit RGB true color'; elseif double(hdr.dime.datatype) == 512 type = '16-bit unsigned integer'; elseif double(hdr.dime.datatype) == 768 type = '32-bit unsigned integer'; elseif double(hdr.dime.datatype) == 1024 type = '64-bit signed integer'; elseif double(hdr.dime.datatype) == 1280 type = '64-bit unsigned integer'; end msg = {}; msg = [msg {''}]; msg = [msg {['Dimension: [', dim, ']']}]; msg = [msg {''}]; msg = [msg {['Voxel Size: [', vox, ']']}]; msg = [msg {''}]; msg = [msg {['Data Type: [', type, ']']}]; msg = [msg {''}]; msg = [msg {['Max Value: [', max_value, ']']}]; msg = [msg {''}]; msg = [msg {['Min Value: [', min_value, ']']}]; msg = [msg {''}]; if isfield(nii_view.nii, 'fileprefix') if isfield(nii_view.nii, 'filetype') & nii_view.nii.filetype == 2 msg = [msg {['File Name: [', nii_view.nii.fileprefix, '.nii]']}]; msg = [msg {''}]; elseif isfield(nii_view.nii, 'filetype') msg = [msg {['File Name: [', nii_view.nii.fileprefix, '.img]']}]; msg = [msg {''}]; else msg = [msg {['File Prefix: [', nii_view.nii.fileprefix, ']']}]; msg = [msg {''}]; end end h = msgbox(msg, 'Image Information', 'modal'); set(h,'color',[1 1 1]); return; % img_info %---------------------------------------------------------------- function orient fig = gcbf; nii_view = getappdata(fig, 'nii_view'); nii = nii_view.nii; if ~isempty(nii_view.bgimg) msg = 'You can not modify an overlay image'; h = msgbox(msg, 'Error', 'modal'); return; end old_pointer = get(fig,'Pointer'); set(fig,'Pointer','watch'); [nii orient] = rri_orient(nii); if isequal(orient, [1 2 3]) % do nothing set(fig,'Pointer',old_pointer); return; end oldopt = view_nii(fig); opt.command = 'updatenii'; opt.usecolorbar = oldopt.usecolorbar; opt.usepanel = oldopt.usepanel; opt.usecrosshair = oldopt.usecrosshair; opt.usestretch = oldopt.usestretch; opt.useimagesc = oldopt.useimagesc; opt.useinterp = oldopt.useinterp; opt.setarea = oldopt.area; opt.setunit = oldopt.unit; opt.setviewpoint = oldopt.viewpoint; opt.setscanid = oldopt.scanid; opt.setcbarminmax = oldopt.cbarminmax; opt.setcolorindex = oldopt.colorindex; opt.setcolormap = oldopt.colormap; opt.setcolorlevel = oldopt.colorlevel; if isfield(oldopt,'highcolor') opt.sethighcolor = oldopt.highcolor; end view_nii(fig, nii, opt); set(fig,'Pointer',old_pointer); reset_zoom(fig); return; % orient %---------------------------------------------------------------- function editvox fig = gcbf; nii_view = getappdata(fig, 'nii_view'); if ~isempty(nii_view.bgimg) msg = 'You can not modify an overlay image'; h = msgbox(msg, 'Error', 'modal'); return; end nii = nii_view.nii; oldopt = view_nii(fig); sag = nii_view.imgXYZ.vox(1); cor = nii_view.imgXYZ.vox(2); axi = nii_view.imgXYZ.vox(3); if nii_view.nii.hdr.dime.datatype == 128 imgvalue = [double(nii.img(sag,cor,axi,1,nii_view.scanid)) double(nii.img(sag,cor,axi,2,nii_view.scanid)) double(nii.img(sag,cor,axi,3,nii_view.scanid))]; init_val = sprintf('%7.4g %7.4g %7.4g',imgvalue); elseif nii_view.nii.hdr.dime.datatype == 511 R = double(nii.img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; G = double(nii.img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; B = double(nii.img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; imgvalue = [R G B]; init_val = sprintf('%7.4g %7.4g %7.4g',imgvalue); else imgvalue = double(nii.img(sag,cor,axi,nii_view.scanid)); init_val = sprintf('%.6g',imgvalue); end old_pointer = get(fig,'Pointer'); set(fig,'Pointer','watch'); repeat = 1; while repeat if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 init_val = inputdlg({'Replace the current voxel values with 3 new numbers:'}, ... 'Edit voxel value at crosshair', 1, {num2str(init_val)}); else init_val = inputdlg({'Replace the current voxel value with 1 new number:'}, ... 'Edit voxel value at crosshair', 1, {num2str(init_val)}); end if isempty(init_val) set(fig,'Pointer',old_pointer); return end imgvalue = str2num(init_val{1}); if ( (nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511) ... & length(imgvalue) ~= 3 ) | ... ( (nii_view.nii.hdr.dime.datatype ~= 128 & nii_view.nii.hdr.dime.datatype ~= 511) ... & length(imgvalue) ~= 1 ) % do nothing else repeat = 0; end end if nii_view.nii.hdr.dime.datatype == 128 nii.img(sag,cor,axi,1,nii_view.scanid) = imgvalue(1); nii.img(sag,cor,axi,2,nii_view.scanid) = imgvalue(2); nii.img(sag,cor,axi,3,nii_view.scanid) = imgvalue(3); elseif nii_view.nii.hdr.dime.datatype == 511 nii.img(sag,cor,axi,1,nii_view.scanid) = (imgvalue(1) - nii_view.nii.hdr.dime.glmin) ... / (nii_view.nii.hdr.dime.glmax - nii_view.nii.hdr.dime.glmin); nii.img(sag,cor,axi,2,nii_view.scanid) = (imgvalue(2) - nii_view.nii.hdr.dime.glmin) ... / (nii_view.nii.hdr.dime.glmax - nii_view.nii.hdr.dime.glmin); nii.img(sag,cor,axi,3,nii_view.scanid) = (imgvalue(3) - nii_view.nii.hdr.dime.glmin) ... / (nii_view.nii.hdr.dime.glmax - nii_view.nii.hdr.dime.glmin); else nii.img(sag,cor,axi,nii_view.scanid) = imgvalue; end opt.command = 'updatenii'; opt.usecolorbar = oldopt.usecolorbar; opt.usepanel = oldopt.usepanel; opt.usecrosshair = oldopt.usecrosshair; opt.usestretch = oldopt.usestretch; opt.useimagesc = oldopt.useimagesc; opt.useinterp = oldopt.useinterp; opt.setarea = oldopt.area; opt.setunit = oldopt.unit; opt.setviewpoint = oldopt.viewpoint; opt.setscanid = oldopt.scanid; opt.setcbarminmax = oldopt.cbarminmax; opt.setcolorindex = oldopt.colorindex; opt.setcolormap = oldopt.colormap; opt.setcolorlevel = oldopt.colorlevel; if isfield(oldopt,'highcolor') opt.sethighcolor = oldopt.highcolor; end view_nii(fig, nii, opt); set(fig,'Pointer',old_pointer); reset_zoom(fig); return; % editvox %---------------------------------------------------------------- function save_disp [filename pathname] = uiputfile('*.*', 'Save displayed image as (*.nii or *.img)'); if isequal(filename,0) | isequal(pathname,0) return; else out_imgfile = fullfile(pathname, filename); % original image file end old_pointer = get(gcbf,'Pointer'); set(gcbf,'Pointer','watch'); nii_view = getappdata(gcbf, 'nii_view'); nii = nii_view.nii; try save_nii(nii, out_imgfile); catch msg = 'File can not be saved.'; msgbox(msg, 'File write error', 'modal'); end set(gcbf,'Pointer',old_pointer); return; % save_disp %---------------------------------------------------------------- function img_hist nii_view = getappdata(gcbf, 'nii_view'); N = hist(double(nii_view.nii.img(:)),256); x = linspace(double(min(nii_view.nii.img(:))), double(max(nii_view.nii.img(:))), 256); figure;bar(x,N); set(gcf, 'number', 'off', 'name', 'Volume Histogram'); set(gcf, 'windowstyle', 'modal'); % no zoom ... xspan = max(x) - min(x) + 1; yspan = max(N) + 1; set(gca, 'xlim', [min(x)-xspan/20, max(x)+xspan/20]); set(gca, 'ylim', [-yspan/20, max(N)+yspan/20]); return; % img_hist
github
sunhongfu/scripts-master
save_untouch_header_only.m
.m
scripts-master/cs-phase/_src/_nii/save_untouch_header_only.m
2,203
utf_8
6622b1835d5ad8ce504298473ab7684f
% This function is only used to save Analyze or NIfTI header that is % ended with .hdr and loaded by load_untouch_header_only.m. If you % have NIfTI file that is ended with .nii and you want to change its % header only, you can use load_untouch_nii / save_untouch_nii pair. % % Usage: save_untouch_header_only(hdr, new_header_file_name) % % hdr - struct with NIfTI / Analyze header fields, which is obtained from: % hdr = load_untouch_header_only(original_header_file_name) % % new_header_file_name - NIfTI / Analyze header name ended with .hdr. % You can either copy original.img(.gz) to new.img(.gz) manually, % or simply input original.hdr(.gz) in save_untouch_header_only.m % to overwrite the original header. % % - Jimmy Shen ([email protected]) % function save_untouch_header_only(hdr, filename) if ~exist('hdr','var') | isempty(hdr) | ~exist('filename','var') | isempty(filename) error('Usage: save_untouch_header_only(hdr, filename)'); end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.hdr.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); else gzFile = 1; filename = filename(1:end-3); end end [p,f] = fileparts(filename); fileprefix = fullfile(p, f); write_hdr(hdr, fileprefix); % gzip output file if requested % if exist('gzFile', 'var') gzip([fileprefix, '.hdr']); delete([fileprefix, '.hdr']); end; return % save_untouch_header_only %----------------------------------------------------------------------------------- function write_hdr(hdr, fileprefix) fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if isfield(hdr.hist,'magic') save_untouch_nii_hdr(hdr, fid); else save_untouch0_nii_hdr(hdr, fid); end fclose(fid); return % write_hdr
github
sunhongfu/scripts-master
pad_nii.m
.m
scripts-master/cs-phase/_src/_nii/pad_nii.m
3,854
utf_8
a38d813f9f822362d873bc92725f565b
% PAD_NII: Pad the NIfTI volume from any of the 6 sides % % Usage: nii = pad_nii(nii, [option]) % % Inputs: % % nii - NIfTI volume. % % option - struct instructing how many voxel to be padded from which side. % % option.pad_from_L = ( number of voxel ) % option.pad_from_R = ( number of voxel ) % option.pad_from_P = ( number of voxel ) % option.pad_from_A = ( number of voxel ) % option.pad_from_I = ( number of voxel ) % option.pad_from_S = ( number of voxel ) % option.bg = [0] % % Options description in detail: % ============================== % % pad_from_L: Number of voxels from Left side will be padded. % % pad_from_R: Number of voxels from Right side will be padded. % % pad_from_P: Number of voxels from Posterior side will be padded. % % pad_from_A: Number of voxels from Anterior side will be padded. % % pad_from_I: Number of voxels from Inferior side will be padded. % % pad_from_S: Number of voxels from Superior side will be padded. % % bg: Background intensity, which is 0 by default. % % NIfTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function nii = pad_nii(nii, opt) dims = abs(nii.hdr.dime.dim(2:4)); origin = abs(nii.hdr.hist.originator(1:3)); if isempty(origin) | all(origin == 0) % according to SPM origin = round((dims+1)/2); end pad_from_L = 0; pad_from_R = 0; pad_from_P = 0; pad_from_A = 0; pad_from_I = 0; pad_from_S = 0; bg = 0; if nargin > 1 & ~isempty(opt) if ~isstruct(opt) error('option argument should be a struct'); end if isfield(opt,'pad_from_L') pad_from_L = round(opt.pad_from_L); if pad_from_L >= origin(1) | pad_from_L < 0 error('pad_from_L cannot be negative'); end end if isfield(opt,'pad_from_P') pad_from_P = round(opt.pad_from_P); if pad_from_P >= origin(2) | pad_from_P < 0 error('pad_from_P cannot be negative'); end end if isfield(opt,'pad_from_I') pad_from_I = round(opt.pad_from_I); if pad_from_I >= origin(3) | pad_from_I < 0 error('pad_from_I cannot be negative'); end end if isfield(opt,'pad_from_R') pad_from_R = round(opt.pad_from_R); if pad_from_R > dims(1)-origin(1) | pad_from_R < 0 error('pad_from_R cannot be negative'); end end if isfield(opt,'pad_from_A') pad_from_A = round(opt.pad_from_A); if pad_from_A > dims(2)-origin(2) | pad_from_A < 0 error('pad_from_A cannot be negative'); end end if isfield(opt,'pad_from_S') pad_from_S = round(opt.pad_from_S); if pad_from_S > dims(3)-origin(3) | pad_from_S < 0 error('pad_from_S cannot be negative'); end end if isfield(opt,'bg') bg = opt.bg; end end blk = bg * ones( pad_from_L, dims(2), dims(3) ); nii.img = cat(1, blk, nii.img); blk = bg * ones( pad_from_R, dims(2), dims(3) ); nii.img = cat(1, nii.img, blk); dims = size(nii.img); blk = bg * ones( dims(1), pad_from_P, dims(3) ); nii.img = cat(2, blk, nii.img); blk = bg * ones( dims(1), pad_from_A, dims(3) ); nii.img = cat(2, nii.img, blk); dims = size(nii.img); blk = bg * ones( dims(1), dims(2), pad_from_I ); nii.img = cat(3, blk, nii.img); blk = bg * ones( dims(1), dims(2), pad_from_S ); nii.img = cat(3, nii.img, blk); nii = make_nii(nii.img, nii.hdr.dime.pixdim(2:4), ... [origin(1)+pad_from_L origin(2)+pad_from_P origin(3)+pad_from_I], ... nii.hdr.dime.datatype, nii.hdr.hist.descrip); return;
github
sunhongfu/scripts-master
load_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/load_nii_hdr.m
10,311
utf_8
ef81f82b43da4fbd79a9de1787b5ae22
% internal function % - Jimmy Shen ([email protected]) function [hdr, filetype, fileprefix, machine] = load_nii_hdr(fileprefix) if ~exist('fileprefix','var'), error('Usage: [hdr, filetype, fileprefix, machine] = load_nii_hdr(filename)'); end machine = 'ieee-le'; new_ext = 0; if findstr('.nii',fileprefix) & strcmp(fileprefix(end-3:end), '.nii') new_ext = 1; fileprefix(end-3:end)=''; end if findstr('.hdr',fileprefix) & strcmp(fileprefix(end-3:end), '.hdr') fileprefix(end-3:end)=''; end if findstr('.img',fileprefix) & strcmp(fileprefix(end-3:end), '.img') fileprefix(end-3:end)=''; end if new_ext fn = sprintf('%s.nii',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.nii".', fileprefix); error(msg); end else fn = sprintf('%s.hdr',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.hdr".', fileprefix); error(msg); end end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); if fread(fid,1,'int32') == 348 hdr = read_header(fid); fclose(fid); else fclose(fid); % first try reading the opposite endian to 'machine' % switch machine, case 'ieee-le', machine = 'ieee-be'; case 'ieee-be', machine = 'ieee-le'; end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else fseek(fid,0,'bof'); if fread(fid,1,'int32') ~= 348 % Now throw an error % msg = sprintf('File "%s" is corrupted.',fn); error(msg); end hdr = read_header(fid); fclose(fid); end end end if strcmp(hdr.hist.magic, 'n+1') filetype = 2; elseif strcmp(hdr.hist.magic, 'ni1') filetype = 1; else filetype = 0; end return % load_nii_hdr %--------------------------------------------------------------------- function [ dsr ] = read_header(fid) % Original header structures % struct dsr % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ dsr.hk = header_key(fid); dsr.dime = image_dimension(fid); dsr.hist = data_history(fid); % For Analyze data format % if ~strcmp(dsr.hist.magic, 'n+1') & ~strcmp(dsr.hist.magic, 'ni1') dsr.hist.qform_code = 0; dsr.hist.sform_code = 0; end return % read_header %--------------------------------------------------------------------- function [ hk ] = header_key(fid) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char dim_info; % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ % % int sizeof_header Should be 348. % char regular Must be 'r' to indicate that all images and % volumes are the same size. v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hk.sizeof_hdr = fread(fid, 1,'int32')'; % should be 348! hk.data_type = deblank(fread(fid,10,directchar)'); hk.db_name = deblank(fread(fid,18,directchar)'); hk.extents = fread(fid, 1,'int32')'; hk.session_error = fread(fid, 1,'int16')'; hk.regular = fread(fid, 1,directchar)'; hk.dim_info = fread(fid, 1,'uchar')'; return % header_key %--------------------------------------------------------------------- function [ dime ] = image_dimension(fid) % Original header structures % struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % /* % dim[0] Number of dimensions in database; usually 4. % dim[1] Image X dimension; number of *pixels* in an image row. % dim[2] Image Y dimension; number of *pixel rows* in slice. % dim[3] Volume Z dimension; number of *slices* in a volume. % dim[4] Time points; number of volumes in database % */ % float intent_p1; % char vox_units[4]; /* 16 + 4 */ % float intent_p2; % char cal_units[8]; /* 20 + 4 */ % float intent_p3; % char cal_units[8]; /* 24 + 4 */ % short int intent_code; % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int slice_start; % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width, mm % pixdim[2] - voxel height, mm % pixdim[3] - slice thickness, mm % pixdim[4] - volume timing, in msec % ..etc % */ % float vox_offset; /* 68 + 4 */ % float scl_slope; % float roi_scale; /* 72 + 4 */ % float scl_inter; % float funused1; /* 76 + 4 */ % short slice_end; % float funused2; /* 80 + 2 */ % char slice_code; % float funused2; /* 82 + 1 */ % char xyzt_units; % float funused2; /* 83 + 1 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % float slice_duration; % int compressed; /* 92 + 4 */ % float toffset; % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ dime.dim = fread(fid,8,'int16')'; dime.intent_p1 = fread(fid,1,'float32')'; dime.intent_p2 = fread(fid,1,'float32')'; dime.intent_p3 = fread(fid,1,'float32')'; dime.intent_code = fread(fid,1,'int16')'; dime.datatype = fread(fid,1,'int16')'; dime.bitpix = fread(fid,1,'int16')'; dime.slice_start = fread(fid,1,'int16')'; dime.pixdim = fread(fid,8,'float32')'; dime.vox_offset = fread(fid,1,'float32')'; dime.scl_slope = fread(fid,1,'float32')'; dime.scl_inter = fread(fid,1,'float32')'; dime.slice_end = fread(fid,1,'int16')'; dime.slice_code = fread(fid,1,'uchar')'; dime.xyzt_units = fread(fid,1,'uchar')'; dime.cal_max = fread(fid,1,'float32')'; dime.cal_min = fread(fid,1,'float32')'; dime.slice_duration = fread(fid,1,'float32')'; dime.toffset = fread(fid,1,'float32')'; dime.glmax = fread(fid,1,'int32')'; dime.glmin = fread(fid,1,'int32')'; return % image_dimension %--------------------------------------------------------------------- function [ hist ] = data_history(fid) % Original header structures % struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % short int qform_code; /* 104 + 2 */ % short int sform_code; /* 106 + 2 */ % float quatern_b; /* 108 + 4 */ % float quatern_c; /* 112 + 4 */ % float quatern_d; /* 116 + 4 */ % float qoffset_x; /* 120 + 4 */ % float qoffset_y; /* 124 + 4 */ % float qoffset_z; /* 128 + 4 */ % float srow_x[4]; /* 132 + 16 */ % float srow_y[4]; /* 148 + 16 */ % float srow_z[4]; /* 164 + 16 */ % char intent_name[16]; /* 180 + 16 */ % char magic[4]; % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ v6 = version; if str2num(v6(1))<6 directchar = '*char'; else directchar = 'uchar=>char'; end hist.descrip = deblank(fread(fid,80,directchar)'); hist.aux_file = deblank(fread(fid,24,directchar)'); hist.qform_code = fread(fid,1,'int16')'; hist.sform_code = fread(fid,1,'int16')'; hist.quatern_b = fread(fid,1,'float32')'; hist.quatern_c = fread(fid,1,'float32')'; hist.quatern_d = fread(fid,1,'float32')'; hist.qoffset_x = fread(fid,1,'float32')'; hist.qoffset_y = fread(fid,1,'float32')'; hist.qoffset_z = fread(fid,1,'float32')'; hist.srow_x = fread(fid,4,'float32')'; hist.srow_y = fread(fid,4,'float32')'; hist.srow_z = fread(fid,4,'float32')'; hist.intent_name = deblank(fread(fid,16,directchar)'); hist.magic = deblank(fread(fid,4,directchar)'); fseek(fid,253,'bof'); hist.originator = fread(fid, 5,'int16')'; return % data_history
github
sunhongfu/scripts-master
save_untouch_slice.m
.m
scripts-master/cs-phase/_src/_nii/save_untouch_slice.m
20,263
utf_8
833f175c0298d11697418454a03993db
% Save back to the original image with a portion of slices that was % loaded by "load_untouch_nii". You can process those slices matrix % in any way, as long as their dimension is not altered. % % Usage: save_untouch_slice(slice, filename, ... % slice_idx, [img_idx], [dim5_idx], [dim6_idx], [dim7_idx]) % % slice - a portion of slices that was loaded by "load_untouch_nii". % This should be a numeric matrix (i.e. only the .img field in the % loaded structure) % % filename - NIfTI or ANALYZE file name. % % slice_idx (depending on slice size) - a numerical array of image % slice indices, which should be the same as that you entered % in "load_untouch_nii" command. % % img_idx (depending on slice size) - a numerical array of image % volume indices, which should be the same as that you entered % in "load_untouch_nii" command. % % dim5_idx (depending on slice size) - a numerical array of 5th % dimension indices, which should be the same as that you entered % in "load_untouch_nii" command. % % dim6_idx (depending on slice size) - a numerical array of 6th % dimension indices, which should be the same as that you entered % in "load_untouch_nii" command. % % dim7_idx (depending on slice size) - a numerical array of 7th % dimension indices, which should be the same as that you entered % in "load_untouch_nii" command. % % Example: % nii = load_nii('avg152T1_LR_nifti.nii'); % save_nii(nii, 'test.nii'); % view_nii(nii); % nii = load_untouch_nii('test.nii','','','','','',[40 51:53]); % nii.img = ones(91,109,4)*122; % save_untouch_slice(nii.img, 'test.nii', [40 51:52]); % nii = load_nii('test.nii'); % view_nii(nii); % % - Jimmy Shen ([email protected]) % function save_untouch_slice(slice, filename, slice_idx, img_idx, dim5_idx, dim6_idx, dim7_idx) if ~exist('slice','var') | ~isnumeric(slice) msg = [char(10) '"slice" argument should be a portion of slices that was loaded' char(10)]; msg = [msg 'by "load_untouch_nii.m". This should be a numeric matrix (i.e.' char(10)]; msg = [msg 'only the .img field in the loaded structure).']; error(msg); end if ~exist('filename','var') | ~exist(filename,'file') error('In order to save back, original NIfTI or ANALYZE file must exist.'); end if ~exist('slice_idx','var') | isempty(slice_idx) | ~isequal(size(slice,3),length(slice_idx)) msg = [char(10) '"slice_idx" is a numerical array of image slice indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end if ~exist('img_idx','var') | isempty(img_idx) img_idx = []; if ~isequal(size(slice,4),1) msg = [char(10) '"img_idx" is a numerical array of image volume indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end elseif ~isequal(size(slice,4),length(img_idx)) msg = [char(10) '"img_idx" is a numerical array of image volume indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end if ~exist('dim5_idx','var') | isempty(dim5_idx) dim5_idx = []; if ~isequal(size(slice,5),1) msg = [char(10) '"dim5_idx" is a numerical array of 5th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end elseif ~isequal(size(slice,5),length(img_idx)) msg = [char(10) '"img_idx" is a numerical array of 5th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end if ~exist('dim6_idx','var') | isempty(dim6_idx) dim6_idx = []; if ~isequal(size(slice,6),1) msg = [char(10) '"dim6_idx" is a numerical array of 6th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end elseif ~isequal(size(slice,6),length(img_idx)) msg = [char(10) '"img_idx" is a numerical array of 6th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end if ~exist('dim7_idx','var') | isempty(dim7_idx) dim7_idx = []; if ~isequal(size(slice,7),1) msg = [char(10) '"dim7_idx" is a numerical array of 7th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end elseif ~isequal(size(slice,7),length(img_idx)) msg = [char(10) '"img_idx" is a numerical array of 7th dimension indices, which' char(10)]; msg = [msg 'should be the same as that you entered in "load_untouch_nii.m"' char(10)]; msg = [msg 'command.']; error(msg); end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end % Read the dataset header % [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename); if nii.filetype == 0 nii.hdr = load_untouch0_nii_hdr(nii.fileprefix,nii.machine); else nii.hdr = load_untouch_nii_hdr(nii.fileprefix,nii.machine,nii.filetype); end % Clean up after gunzip % if exist('gzFileName', 'var') % fix fileprefix so it doesn't point to temp location % nii.fileprefix = gzFileName(1:end-7); % rmdir(tmpDir,'s'); end [p,f] = fileparts(filename); fileprefix = fullfile(p, f); % fileprefix = nii.fileprefix; filetype = nii.filetype; if ~isequal( nii.hdr.dime.dim(2:3), [size(slice,1),size(slice,2)] ) msg = [char(10) 'The first two dimensions of slice matrix should be the same as' char(10)]; msg = [msg 'the first two dimensions of image loaded by "load_untouch_nii".']; error(msg); end % Save the dataset body % save_untouch_slice_img(slice, nii.hdr, filetype, fileprefix, ... nii.machine, slice_idx,img_idx,dim5_idx,dim6_idx,dim7_idx); % gzip output file if requested % if exist('gzFileName', 'var') [p,f] = fileparts(gzFileName); if filetype == 1 gzip([fileprefix, '.img']); delete([fileprefix, '.img']); movefile([fileprefix, '.img.gz']); gzip([fileprefix, '.hdr']); delete([fileprefix, '.hdr']); movefile([fileprefix, '.hdr.gz']); elseif filetype == 2 gzip([fileprefix, '.nii']); delete([fileprefix, '.nii']); movefile([fileprefix, '.nii.gz']); end; rmdir(tmpDir,'s'); end; return % save_untouch_slice %-------------------------------------------------------------------------- function save_untouch_slice_img(slice,hdr,filetype,fileprefix,machine,slice_idx,img_idx,dim5_idx,dim6_idx,dim7_idx) if ~exist('hdr','var') | ~exist('filetype','var') | ~exist('fileprefix','var') | ~exist('machine','var') error('Usage: save_untouch_slice_img(slice,hdr,filetype,fileprefix,machine,slice_idx,[img_idx],[dim5_idx],[dim6_idx],[dim7_idx]);'); end if ~exist('slice_idx','var') | isempty(slice_idx) | hdr.dime.dim(4)<1 slice_idx = []; end if ~exist('img_idx','var') | isempty(img_idx) | hdr.dime.dim(5)<1 img_idx = []; end if ~exist('dim5_idx','var') | isempty(dim5_idx) | hdr.dime.dim(6)<1 dim5_idx = []; end if ~exist('dim6_idx','var') | isempty(dim6_idx) | hdr.dime.dim(7)<1 dim6_idx = []; end if ~exist('dim7_idx','var') | isempty(dim7_idx) | hdr.dime.dim(8)<1 dim7_idx = []; end % check img_idx % if ~isempty(img_idx) & ~isnumeric(img_idx) error('"img_idx" should be a numerical array.'); end if length(unique(img_idx)) ~= length(img_idx) error('Duplicate image index in "img_idx"'); end if ~isempty(img_idx) & (min(img_idx) < 1 | max(img_idx) > hdr.dime.dim(5)) max_range = hdr.dime.dim(5); if max_range == 1 error(['"img_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"img_idx" should be an integer within the range of [' range '].']); end end % check dim5_idx % if ~isempty(dim5_idx) & ~isnumeric(dim5_idx) error('"dim5_idx" should be a numerical array.'); end if length(unique(dim5_idx)) ~= length(dim5_idx) error('Duplicate index in "dim5_idx"'); end if ~isempty(dim5_idx) & (min(dim5_idx) < 1 | max(dim5_idx) > hdr.dime.dim(6)) max_range = hdr.dime.dim(6); if max_range == 1 error(['"dim5_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim5_idx" should be an integer within the range of [' range '].']); end end % check dim6_idx % if ~isempty(dim6_idx) & ~isnumeric(dim6_idx) error('"dim6_idx" should be a numerical array.'); end if length(unique(dim6_idx)) ~= length(dim6_idx) error('Duplicate index in "dim6_idx"'); end if ~isempty(dim6_idx) & (min(dim6_idx) < 1 | max(dim6_idx) > hdr.dime.dim(7)) max_range = hdr.dime.dim(7); if max_range == 1 error(['"dim6_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim6_idx" should be an integer within the range of [' range '].']); end end % check dim7_idx % if ~isempty(dim7_idx) & ~isnumeric(dim7_idx) error('"dim7_idx" should be a numerical array.'); end if length(unique(dim7_idx)) ~= length(dim7_idx) error('Duplicate index in "dim7_idx"'); end if ~isempty(dim7_idx) & (min(dim7_idx) < 1 | max(dim7_idx) > hdr.dime.dim(8)) max_range = hdr.dime.dim(8); if max_range == 1 error(['"dim7_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim7_idx" should be an integer within the range of [' range '].']); end end % check slice_idx % if ~isempty(slice_idx) & ~isnumeric(slice_idx) error('"slice_idx" should be a numerical array.'); end if length(unique(slice_idx)) ~= length(slice_idx) error('Duplicate index in "slice_idx"'); end if ~isempty(slice_idx) & (min(slice_idx) < 1 | max(slice_idx) > hdr.dime.dim(4)) max_range = hdr.dime.dim(4); if max_range == 1 error(['"slice_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"slice_idx" should be an integer within the range of [' range '].']); end end write_image(slice,hdr,filetype,fileprefix,machine,slice_idx,img_idx,dim5_idx,dim6_idx,dim7_idx); return % save_untouch_slice_img %--------------------------------------------------------------------- function write_image(slice,hdr,filetype,fileprefix,machine,slice_idx,img_idx,dim5_idx,dim6_idx,dim7_idx) if filetype == 2 fid = fopen(sprintf('%s.nii',fileprefix),'r+'); if fid < 0, msg = sprintf('Cannot open file %s.nii.',fileprefix); error(msg); end else fid = fopen(sprintf('%s.img',fileprefix),'r+'); if fid < 0, msg = sprintf('Cannot open file %s.img.',fileprefix); error(msg); end end % Set bitpix according to datatype % % /*Acceptable values for datatype are*/ % % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN % 1 Binary (ubit1, bitpix=1) % DT_BINARY % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8 % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16 % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32 % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32 % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64 % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64 % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24 % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8 % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96 % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16 % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32 % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64 % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64 % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128 % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % switch hdr.dime.datatype case 2, hdr.dime.bitpix = 8; precision = 'uint8'; case 4, hdr.dime.bitpix = 16; precision = 'int16'; case 8, hdr.dime.bitpix = 32; precision = 'int32'; case 16, hdr.dime.bitpix = 32; precision = 'float32'; case 64, hdr.dime.bitpix = 64; precision = 'float64'; case 128, hdr.dime.bitpix = 24; precision = 'uint8'; case 256 hdr.dime.bitpix = 8; precision = 'int8'; case 511 hdr.dime.bitpix = 96; precision = 'float32'; case 512 hdr.dime.bitpix = 16; precision = 'uint16'; case 768 hdr.dime.bitpix = 32; precision = 'uint32'; case 1024 hdr.dime.bitpix = 64; precision = 'int64'; case 1280 hdr.dime.bitpix = 64; precision = 'uint64'; otherwise error('This datatype is not supported'); end hdr.dime.dim(find(hdr.dime.dim < 1)) = 1; % move pointer to the start of image block % switch filetype case {0, 1} fseek(fid, 0, 'bof'); case 2 fseek(fid, hdr.dime.vox_offset, 'bof'); end if hdr.dime.datatype == 1 | isequal(hdr.dime.dim(4:8),ones(1,5)) | ... (isempty(img_idx) & isempty(dim5_idx) & isempty(dim6_idx) & isempty(dim7_idx) & isempty(slice_idx)) msg = [char(10) char(10) ' "save_untouch_slice" is used to save back to the original image a' char(10)]; msg = [msg ' portion of slices that were loaded by "load_untouch_nii". You can' char(10)]; msg = [msg ' process those slices matrix in any way, as long as their dimension' char(10)]; msg = [msg ' is not changed.']; error(msg); else d1 = hdr.dime.dim(2); d2 = hdr.dime.dim(3); d3 = hdr.dime.dim(4); d4 = hdr.dime.dim(5); d5 = hdr.dime.dim(6); d6 = hdr.dime.dim(7); d7 = hdr.dime.dim(8); if isempty(slice_idx) slice_idx = 1:d3; end if isempty(img_idx) img_idx = 1:d4; end if isempty(dim5_idx) dim5_idx = 1:d5; end if isempty(dim6_idx) dim6_idx = 1:d6; end if isempty(dim7_idx) dim7_idx = 1:d7; end %ROMAN: begin roman = 1; if(roman) % compute size of one slice % img_siz = prod(hdr.dime.dim(2:3)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end end; %if(roman) % ROMAN: end for i7=1:length(dim7_idx) for i6=1:length(dim6_idx) for i5=1:length(dim5_idx) for t=1:length(img_idx) for s=1:length(slice_idx) % Position is seeked in bytes. To convert dimension size % to byte storage size, hdr.dime.bitpix/8 will be % applied. % pos = sub2ind([d1 d2 d3 d4 d5 d6 d7], 1, 1, slice_idx(s), ... img_idx(t), dim5_idx(i5),dim6_idx(i6),dim7_idx(i7)) -1; pos = pos * hdr.dime.bitpix/8; % ROMAN: begin if(roman) % do nothing else img_siz = prod(hdr.dime.dim(2:3)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end end; % if (roman) % ROMAN: end if filetype == 2 fseek(fid, pos + hdr.dime.vox_offset, 'bof'); else fseek(fid, pos, 'bof'); end % For each frame, fwrite will write precision of value % in img_siz times % fwrite(fid, slice(:,:,s,t,i5,i6,i7), sprintf('*%s',precision)); end end end end end end fclose(fid); return % write_image
github
sunhongfu/scripts-master
load_nii_img.m
.m
scripts-master/cs-phase/_src/_nii/load_nii_img.m
12,720
utf_8
5670adb84a76f241bd221003bee8187d
% internal function % - Jimmy Shen ([email protected]) function [img,hdr] = load_nii_img(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB) if ~exist('hdr','var') | ~exist('filetype','var') | ~exist('fileprefix','var') | ~exist('machine','var') error('Usage: [img,hdr] = load_nii_img(hdr,filetype,fileprefix,machine,[img_idx],[dim5_idx],[dim6_idx],[dim7_idx],[old_RGB]);'); end if ~exist('img_idx','var') | isempty(img_idx) | hdr.dime.dim(5)<1 img_idx = []; end if ~exist('dim5_idx','var') | isempty(dim5_idx) | hdr.dime.dim(6)<1 dim5_idx = []; end if ~exist('dim6_idx','var') | isempty(dim6_idx) | hdr.dime.dim(7)<1 dim6_idx = []; end if ~exist('dim7_idx','var') | isempty(dim7_idx) | hdr.dime.dim(8)<1 dim7_idx = []; end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end % check img_idx % if ~isempty(img_idx) & ~isnumeric(img_idx) error('"img_idx" should be a numerical array.'); end if length(unique(img_idx)) ~= length(img_idx) error('Duplicate image index in "img_idx"'); end if ~isempty(img_idx) & (min(img_idx) < 1 | max(img_idx) > hdr.dime.dim(5)) max_range = hdr.dime.dim(5); if max_range == 1 error(['"img_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"img_idx" should be an integer within the range of [' range '].']); end end % check dim5_idx % if ~isempty(dim5_idx) & ~isnumeric(dim5_idx) error('"dim5_idx" should be a numerical array.'); end if length(unique(dim5_idx)) ~= length(dim5_idx) error('Duplicate index in "dim5_idx"'); end if ~isempty(dim5_idx) & (min(dim5_idx) < 1 | max(dim5_idx) > hdr.dime.dim(6)) max_range = hdr.dime.dim(6); if max_range == 1 error(['"dim5_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim5_idx" should be an integer within the range of [' range '].']); end end % check dim6_idx % if ~isempty(dim6_idx) & ~isnumeric(dim6_idx) error('"dim6_idx" should be a numerical array.'); end if length(unique(dim6_idx)) ~= length(dim6_idx) error('Duplicate index in "dim6_idx"'); end if ~isempty(dim6_idx) & (min(dim6_idx) < 1 | max(dim6_idx) > hdr.dime.dim(7)) max_range = hdr.dime.dim(7); if max_range == 1 error(['"dim6_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim6_idx" should be an integer within the range of [' range '].']); end end % check dim7_idx % if ~isempty(dim7_idx) & ~isnumeric(dim7_idx) error('"dim7_idx" should be a numerical array.'); end if length(unique(dim7_idx)) ~= length(dim7_idx) error('Duplicate index in "dim7_idx"'); end if ~isempty(dim7_idx) & (min(dim7_idx) < 1 | max(dim7_idx) > hdr.dime.dim(8)) max_range = hdr.dime.dim(8); if max_range == 1 error(['"dim7_idx" should be 1.']); else range = ['1 ' num2str(max_range)]; error(['"dim7_idx" should be an integer within the range of [' range '].']); end end [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB); return % load_nii_img %--------------------------------------------------------------------- function [img,hdr] = read_image(hdr,filetype,fileprefix,machine,img_idx,dim5_idx,dim6_idx,dim7_idx,old_RGB) switch filetype case {0, 1} fn = [fileprefix '.img']; case 2 fn = [fileprefix '.nii']; end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); end % Set bitpix according to datatype % % /*Acceptable values for datatype are*/ % % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN % 1 Binary (ubit1, bitpix=1) % DT_BINARY % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8 % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16 % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32 % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32 % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64 % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64 % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24 % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8 % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96 % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16 % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32 % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64 % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64 % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128 % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % switch hdr.dime.datatype case 1, hdr.dime.bitpix = 1; precision = 'ubit1'; case 2, hdr.dime.bitpix = 8; precision = 'uint8'; case 4, hdr.dime.bitpix = 16; precision = 'int16'; case 8, hdr.dime.bitpix = 32; precision = 'int32'; case 16, hdr.dime.bitpix = 32; precision = 'float32'; case 32, hdr.dime.bitpix = 64; precision = 'float32'; case 64, hdr.dime.bitpix = 64; precision = 'float64'; case 128, hdr.dime.bitpix = 24; precision = 'uint8'; case 256 hdr.dime.bitpix = 8; precision = 'int8'; case 511 hdr.dime.bitpix = 96; precision = 'float32'; case 512 hdr.dime.bitpix = 16; precision = 'uint16'; case 768 hdr.dime.bitpix = 32; precision = 'uint32'; case 1024 hdr.dime.bitpix = 64; precision = 'int64'; case 1280 hdr.dime.bitpix = 64; precision = 'uint64'; case 1792, hdr.dime.bitpix = 128; precision = 'float64'; otherwise error('This datatype is not supported'); end hdr.dime.dim(find(hdr.dime.dim < 1)) = 1; % move pointer to the start of image block % switch filetype case {0, 1} fseek(fid, 0, 'bof'); case 2 fseek(fid, hdr.dime.vox_offset, 'bof'); end % Load whole image block for old Analyze format or binary image; % otherwise, load images that are specified in img_idx, dim5_idx, % dim6_idx, and dim7_idx % % For binary image, we have to read all because pos can not be % seeked in bit and can not be calculated the way below. % if hdr.dime.datatype == 1 | isequal(hdr.dime.dim(5:8),ones(1,4)) | ... (isempty(img_idx) & isempty(dim5_idx) & isempty(dim6_idx) & isempty(dim7_idx)) % For each frame, precision of value will be read % in img_siz times, where img_siz is only the % dimension size of an image, not the byte storage % size of an image. % img_siz = prod(hdr.dime.dim(2:8)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end img = fread(fid, img_siz, sprintf('*%s',precision)); d1 = hdr.dime.dim(2); d2 = hdr.dime.dim(3); d3 = hdr.dime.dim(4); d4 = hdr.dime.dim(5); d5 = hdr.dime.dim(6); d6 = hdr.dime.dim(7); d7 = hdr.dime.dim(8); if isempty(img_idx) img_idx = 1:d4; end if isempty(dim5_idx) dim5_idx = 1:d5; end if isempty(dim6_idx) dim6_idx = 1:d6; end if isempty(dim7_idx) dim7_idx = 1:d7; end else d1 = hdr.dime.dim(2); d2 = hdr.dime.dim(3); d3 = hdr.dime.dim(4); d4 = hdr.dime.dim(5); d5 = hdr.dime.dim(6); d6 = hdr.dime.dim(7); d7 = hdr.dime.dim(8); if isempty(img_idx) img_idx = 1:d4; end if isempty(dim5_idx) dim5_idx = 1:d5; end if isempty(dim6_idx) dim6_idx = 1:d6; end if isempty(dim7_idx) dim7_idx = 1:d7; end % compute size of one image % img_siz = prod(hdr.dime.dim(2:4)); % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img_siz = img_siz * 2; end %MPH: For RGB24, voxel values include 3 separate color planes % if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 img_siz = img_siz * 3; end % preallocate img img = zeros(img_siz, length(img_idx)*length(dim5_idx)*length(dim6_idx)*length(dim7_idx) ); currentIndex = 1; for i7=1:length(dim7_idx) for i6=1:length(dim6_idx) for i5=1:length(dim5_idx) for t=1:length(img_idx) % Position is seeked in bytes. To convert dimension size % to byte storage size, hdr.dime.bitpix/8 will be % applied. % pos = sub2ind([d1 d2 d3 d4 d5 d6 d7], 1, 1, 1, ... img_idx(t), dim5_idx(i5),dim6_idx(i6),dim7_idx(i7)) -1; pos = pos * hdr.dime.bitpix/8; if filetype == 2 fseek(fid, pos + hdr.dime.vox_offset, 'bof'); else fseek(fid, pos, 'bof'); end % For each frame, fread will read precision of value % in img_siz times % img(:,currentIndex) = fread(fid, img_siz, sprintf('*%s',precision)); currentIndex = currentIndex +1; end end end end end % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 img = reshape(img, [2, length(img)/2]); img = complex(img(1,:)', img(2,:)'); end fclose(fid); % Update the global min and max values % hdr.dime.glmax = double(max(img(:))); hdr.dime.glmin = double(min(img(:))); % old_RGB treat RGB slice by slice, now it is treated voxel by voxel % if old_RGB & hdr.dime.datatype == 128 & hdr.dime.bitpix == 24 % remove squeeze img = (reshape(img, [hdr.dime.dim(2:3) 3 hdr.dime.dim(4) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [1 2 4 3 5 6 7 8]); elseif hdr.dime.datatype == 128 & hdr.dime.bitpix == 24 % remove squeeze img = (reshape(img, [3 hdr.dime.dim(2:4) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [2 3 4 1 5 6 7 8]); elseif hdr.dime.datatype == 511 & hdr.dime.bitpix == 96 img = double(img(:)); img = single((img - min(img))/(max(img) - min(img))); % remove squeeze img = (reshape(img, [3 hdr.dime.dim(2:4) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); img = permute(img, [2 3 4 1 5 6 7 8]); else % remove squeeze img = (reshape(img, [hdr.dime.dim(2:4) length(img_idx) length(dim5_idx) length(dim6_idx) length(dim7_idx)])); end if ~isempty(img_idx) hdr.dime.dim(5) = length(img_idx); end if ~isempty(dim5_idx) hdr.dime.dim(6) = length(dim5_idx); end if ~isempty(dim6_idx) hdr.dime.dim(7) = length(dim6_idx); end if ~isempty(dim7_idx) hdr.dime.dim(8) = length(dim7_idx); end return % read_image
github
sunhongfu/scripts-master
bresenham_line3d.m
.m
scripts-master/cs-phase/_src/_nii/bresenham_line3d.m
4,682
utf_8
f2e52d1f3ac9779b22baf3bb4d2ac201
% Generate X Y Z coordinates of a 3D Bresenham's line between % two given points. % % A very useful application of this algorithm can be found in the % implementation of Fischer's Bresenham interpolation method in my % another program that can rotate three dimensional image volume % with an affine matrix: % http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=21080 % % Usage: [X Y Z] = bresenham_line3d(P1, P2, [precision]); % % P1 - vector for Point1, where P1 = [x1 y1 z1] % % P2 - vector for Point2, where P2 = [x2 y2 z2] % % precision (optional) - Although according to Bresenham's line % algorithm, point coordinates x1 y1 z1 and x2 y2 z2 should % be integer numbers, this program extends its limit to all % real numbers. If any of them are floating numbers, you % should specify how many digits of decimal that you would % like to preserve. Be aware that the length of output X Y % Z coordinates will increase in 10 times for each decimal % digit that you want to preserve. By default, the precision % is 0, which means that they will be rounded to the nearest % integer. % % X - a set of x coordinates on Bresenham's line % % Y - a set of y coordinates on Bresenham's line % % Z - a set of z coordinates on Bresenham's line % % Therefore, all points in XYZ set (i.e. P(i) = [X(i) Y(i) Z(i)]) % will constitute the Bresenham's line between P1 and P1. % % Example: % P1 = [12 37 6]; P2 = [46 3 35]; % [X Y Z] = bresenham_line3d(P1, P2); % figure; plot3(X,Y,Z,'s','markerface','b'); % % This program is ported to MATLAB from: % % B.Pendleton. line3d - 3D Bresenham's (a 3D line drawing algorithm) % ftp://ftp.isc.org/pub/usenet/comp.sources.unix/volume26/line3d, 1992 % % Which is also referenced by: % % Fischer, J., A. del Rio (2004). A Fast Method for Applying Rigid % Transformations to Volume Data, WSCG2004 Conference. % http://wscg.zcu.cz/wscg2004/Papers_2004_Short/M19.pdf % % - Jimmy Shen ([email protected]) % function [X,Y,Z] = bresenham_line3d(P1, P2, precision) if ~exist('precision','var') | isempty(precision) | round(precision) == 0 precision = 0; P1 = round(P1); P2 = round(P2); else precision = round(precision); P1 = round(P1*(10^precision)); P2 = round(P2*(10^precision)); end d = max(abs(P2-P1)+1); X = zeros(1, d); Y = zeros(1, d); Z = zeros(1, d); x1 = P1(1); y1 = P1(2); z1 = P1(3); x2 = P2(1); y2 = P2(2); z2 = P2(3); dx = x2 - x1; dy = y2 - y1; dz = z2 - z1; ax = abs(dx)*2; ay = abs(dy)*2; az = abs(dz)*2; sx = sign(dx); sy = sign(dy); sz = sign(dz); x = x1; y = y1; z = z1; idx = 1; if(ax>=max(ay,az)) % x dominant yd = ay - ax/2; zd = az - ax/2; while(1) X(idx) = x; Y(idx) = y; Z(idx) = z; idx = idx + 1; if(x == x2) % end break; end if(yd >= 0) % move along y y = y + sy; yd = yd - ax; end if(zd >= 0) % move along z z = z + sz; zd = zd - ax; end x = x + sx; % move along x yd = yd + ay; zd = zd + az; end elseif(ay>=max(ax,az)) % y dominant xd = ax - ay/2; zd = az - ay/2; while(1) X(idx) = x; Y(idx) = y; Z(idx) = z; idx = idx + 1; if(y == y2) % end break; end if(xd >= 0) % move along x x = x + sx; xd = xd - ay; end if(zd >= 0) % move along z z = z + sz; zd = zd - ay; end y = y + sy; % move along y xd = xd + ax; zd = zd + az; end elseif(az>=max(ax,ay)) % z dominant xd = ax - az/2; yd = ay - az/2; while(1) X(idx) = x; Y(idx) = y; Z(idx) = z; idx = idx + 1; if(z == z2) % end break; end if(xd >= 0) % move along x x = x + sx; xd = xd - az; end if(yd >= 0) % move along y y = y + sy; yd = yd - az; end z = z + sz; % move along z xd = xd + ax; yd = yd + ay; end end if precision ~= 0 X = X/(10^precision); Y = Y/(10^precision); Z = Z/(10^precision); end return; % bresenham_line3d
github
sunhongfu/scripts-master
make_nii.m
.m
scripts-master/cs-phase/_src/_nii/make_nii.m
7,105
utf_8
6b1565392965b164217621e71d213ddd
% Make NIfTI structure specified by an N-D matrix. Usually, N is 3 for % 3D matrix [x y z], or 4 for 4D matrix with time series [x y z t]. % Optional parameters can also be included, such as: voxel_size, % origin, datatype, and description. % % Once the NIfTI structure is made, it can be saved into NIfTI file % using "save_nii" command (for more detail, type: help save_nii). % % Usage: nii = make_nii(img, [voxel_size], [origin], [datatype], [description]) % % Where: % % img: Usually, img is a 3D matrix [x y z], or a 4D % matrix with time series [x y z t]. However, % NIfTI allows a maximum of 7D matrix. When the % image is in RGB format, make sure that the size % of 4th dimension is always 3 (i.e. [R G B]). In % that case, make sure that you must specify RGB % datatype, which is either 128 or 511. % % voxel_size (optional): Voxel size in millimeter for each % dimension. Default is [1 1 1]. % % origin (optional): The AC origin. Default is [0 0 0]. % % datatype (optional): Storage data type: % 2 - uint8, 4 - int16, 8 - int32, 16 - float32, % 32 - complex64, 64 - float64, 128 - RGB24, % 256 - int8, 511 - RGB96, 512 - uint16, % 768 - uint32, 1792 - complex128 % Default will use the data type of 'img' matrix % For RGB image, you must specify it to either 128 % or 511. % % description (optional): Description of data. Default is ''. % % e.g.: % origin = [33 44 13]; datatype = 64; % nii = make_nii(img, [], origin, datatype); % default voxel_size % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function nii = make_nii(varargin) nii.img = varargin{1}; dims = size(nii.img); dims = [length(dims) dims ones(1,8)]; dims = dims(1:8); voxel_size = [0 ones(1,7)]; origin = zeros(1,5); descrip = ''; switch class(nii.img) case 'uint8' datatype = 2; case 'int16' datatype = 4; case 'int32' datatype = 8; case 'single' if isreal(nii.img) datatype = 16; else datatype = 32; end case 'double' if isreal(nii.img) datatype = 64; else datatype = 1792; end case 'int8' datatype = 256; case 'uint16' datatype = 512; case 'uint32' datatype = 768; otherwise error('Datatype is not supported by make_nii.'); end if nargin > 1 & ~isempty(varargin{2}) voxel_size(2:4) = double(varargin{2}); end if nargin > 2 & ~isempty(varargin{3}) origin(1:3) = double(varargin{3}); end if nargin > 3 & ~isempty(varargin{4}) datatype = double(varargin{4}); if datatype == 128 | datatype == 511 dims(5) = []; dims(1) = dims(1) - 1; dims = [dims 1]; end end if nargin > 4 & ~isempty(varargin{5}) descrip = varargin{5}; end if ndims(nii.img) > 7 error('NIfTI only allows a maximum of 7 Dimension matrix.'); end maxval = round(double(max(nii.img(:)))); minval = round(double(min(nii.img(:)))); nii.hdr = make_header(dims, voxel_size, origin, datatype, ... descrip, maxval, minval); switch nii.hdr.dime.datatype case 2 nii.img = uint8(nii.img); case 4 nii.img = int16(nii.img); case 8 nii.img = int32(nii.img); case 16 nii.img = single(nii.img); case 32 nii.img = single(nii.img); case 64 nii.img = double(nii.img); case 128 nii.img = uint8(nii.img); case 256 nii.img = int8(nii.img); case 511 img = double(nii.img(:)); img = single((img - min(img))/(max(img) - min(img))); nii.img = reshape(img, size(nii.img)); nii.hdr.dime.glmax = double(max(img)); nii.hdr.dime.glmin = double(min(img)); case 512 nii.img = uint16(nii.img); case 768 nii.img = uint32(nii.img); case 1792 nii.img = double(nii.img); otherwise error('Datatype is not supported by make_nii.'); end return; % make_nii %--------------------------------------------------------------------- function hdr = make_header(dims, voxel_size, origin, datatype, ... descrip, maxval, minval) hdr.hk = header_key; hdr.dime = image_dimension(dims, voxel_size, datatype, maxval, minval); hdr.hist = data_history(origin, descrip); return; % make_header %--------------------------------------------------------------------- function hk = header_key hk.sizeof_hdr = 348; % must be 348! hk.data_type = ''; hk.db_name = ''; hk.extents = 0; hk.session_error = 0; hk.regular = 'r'; hk.dim_info = 0; return; % header_key %--------------------------------------------------------------------- function dime = image_dimension(dims, voxel_size, datatype, maxval, minval) dime.dim = dims; dime.intent_p1 = 0; dime.intent_p2 = 0; dime.intent_p3 = 0; dime.intent_code = 0; dime.datatype = datatype; switch dime.datatype case 2, dime.bitpix = 8; precision = 'uint8'; case 4, dime.bitpix = 16; precision = 'int16'; case 8, dime.bitpix = 32; precision = 'int32'; case 16, dime.bitpix = 32; precision = 'float32'; case 32, dime.bitpix = 64; precision = 'float32'; case 64, dime.bitpix = 64; precision = 'float64'; case 128 dime.bitpix = 24; precision = 'uint8'; case 256 dime.bitpix = 8; precision = 'int8'; case 511 dime.bitpix = 96; precision = 'float32'; case 512 dime.bitpix = 16; precision = 'uint16'; case 768 dime.bitpix = 32; precision = 'uint32'; case 1792, dime.bitpix = 128; precision = 'float64'; otherwise error('Datatype is not supported by make_nii.'); end dime.slice_start = 0; dime.pixdim = voxel_size; dime.vox_offset = 0; dime.scl_slope = 0; dime.scl_inter = 0; dime.slice_end = 0; dime.slice_code = 0; dime.xyzt_units = 0; dime.cal_max = 0; dime.cal_min = 0; dime.slice_duration = 0; dime.toffset = 0; dime.glmax = maxval; dime.glmin = minval; return; % image_dimension %--------------------------------------------------------------------- function hist = data_history(origin, descrip) hist.descrip = descrip; hist.aux_file = 'none'; hist.qform_code = 0; hist.sform_code = 0; hist.quatern_b = 0; hist.quatern_c = 0; hist.quatern_d = 0; hist.qoffset_x = 0; hist.qoffset_y = 0; hist.qoffset_z = 0; hist.srow_x = zeros(1,4); hist.srow_y = zeros(1,4); hist.srow_z = zeros(1,4); hist.intent_name = ''; hist.magic = ''; hist.originator = origin; return; % data_history
github
sunhongfu/scripts-master
verify_nii_ext.m
.m
scripts-master/cs-phase/_src/_nii/verify_nii_ext.m
1,721
utf_8
0339aeb8d7286e4f08165c9eeeb4c2cd
% Verify NIFTI header extension to make sure that each extension section % must be an integer multiple of 16 byte long that includes the first 8 % bytes of esize and ecode. If the length of extension section is not the % above mentioned case, edata should be padded with all 0. % % Usage: [ext, esize_total] = verify_nii_ext(ext) % % ext - Structure of NIFTI header extension, which includes num_ext, % and all the extended header sections in the header extension. % Each extended header section will have its esize, ecode, and % edata, where edata can be plain text, xml, or any raw data % that was saved in the extended header section. % % esize_total - Sum of all esize variable in all header sections. % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function [ext, esize_total] = verify_nii_ext(ext) if ~isfield(ext, 'section') error('Incorrect NIFTI header extension structure.'); elseif ~isfield(ext, 'num_ext') ext.num_ext = length(ext.section); elseif ~isfield(ext, 'extension') ext.extension = [1 0 0 0]; end esize_total = 0; for i=1:ext.num_ext if ~isfield(ext.section(i), 'ecode') | ~isfield(ext.section(i), 'edata') error('Incorrect NIFTI header extension structure.'); end ext.section(i).esize = ceil((length(ext.section(i).edata)+8)/16)*16; ext.section(i).edata = ... [ext.section(i).edata ... zeros(1,ext.section(i).esize-length(ext.section(i).edata)-8)]; esize_total = esize_total + ext.section(i).esize; end return % verify_nii_ext
github
sunhongfu/scripts-master
get_nii_frame.m
.m
scripts-master/cs-phase/_src/_nii/get_nii_frame.m
4,497
utf_8
cc9b1b92f34e5ae67dc34c35a5174c75
% Return time frame of a NIFTI dataset. Support both *.nii and % *.hdr/*.img file extension. If file extension is not provided, % *.hdr/*.img will be used as default. % % It is a lightweighted "load_nii_hdr", and is equivalent to % hdr.dime.dim(5) % % Usage: [ total_scan ] = get_nii_frame(filename) % % filename - NIFTI file name. % % Returned values: % % total_scan - total number of image scans for the time frame % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function [ total_scan ] = get_nii_frame(filename) if ~exist('filename','var'), error('Usage: [ total_scan ] = get_nii_frame(filename)'); end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end fileprefix = filename; machine = 'ieee-le'; new_ext = 0; if findstr('.nii',fileprefix) & strcmp(fileprefix(end-3:end), '.nii') new_ext = 1; fileprefix(end-3:end)=''; end if findstr('.hdr',fileprefix) & strcmp(fileprefix(end-3:end), '.hdr') fileprefix(end-3:end)=''; end if findstr('.img',fileprefix) & strcmp(fileprefix(end-3:end), '.img') fileprefix(end-3:end)=''; end if new_ext fn = sprintf('%s.nii',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.nii".', fileprefix); error(msg); end else fn = sprintf('%s.hdr',fileprefix); if ~exist(fn) msg = sprintf('Cannot find file "%s.hdr".', fileprefix); error(msg); end end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else hdr = read_header(fid); fclose(fid); end if hdr.sizeof_hdr ~= 348 % first try reading the opposite endian to 'machine' switch machine, case 'ieee-le', machine = 'ieee-be'; case 'ieee-be', machine = 'ieee-le'; end fid = fopen(fn,'r',machine); if fid < 0, msg = sprintf('Cannot open file %s.',fn); error(msg); else hdr = read_header(fid); fclose(fid); end end if hdr.sizeof_hdr ~= 348 % Now throw an error msg = sprintf('File "%s" is corrupted.',fn); error(msg); end total_scan = hdr.dim(5); % Clean up after gunzip % if exist('gzFileName', 'var') rmdir(tmpDir,'s'); end return; % get_nii_frame %--------------------------------------------------------------------- function [ dsr ] = read_header(fid) fseek(fid,0,'bof'); dsr.sizeof_hdr = fread(fid,1,'int32')'; % should be 348! fseek(fid,40,'bof'); dsr.dim = fread(fid,8,'int16')'; return; % read_header
github
sunhongfu/scripts-master
flip_lr.m
.m
scripts-master/cs-phase/_src/_nii/flip_lr.m
3,568
utf_8
d95b62698d44a65a3c2f02fbabc632ac
% When you load any ANALYZE or NIfTI file with 'load_nii.m', and view % it with 'view_nii.m', you may find that the image is L-R flipped. % This is because of the confusion of radiological and neurological % convention in the medical image before NIfTI format is adopted. You % can find more details from: % % http://www.rotman-baycrest.on.ca/~jimmy/UseANALYZE.htm % % Sometime, people even want to convert RAS (standard orientation) back % to LAS orientation to satisfy the legend programs or processes. This % program is only written for those purpose. So PLEASE BE VERY CAUTIOUS % WHEN USING THIS 'FLIP_LR.M' PROGRAM. % % With 'flip_lr.m', you can convert any ANALYZE or NIfTI (no matter % 3D or 4D) file to a flipped NIfTI file. This is implemented simply % by flipping the affine matrix in the NIfTI header. Since the L-R % orientation is determined there, so the image will be flipped. % % Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance],[preferredForm]) % % original_fn - filename of the original ANALYZE or NIfTI (3D or 4D) file % % flipped_fn - filename of the L-R flipped NIfTI file % % old_RGB (optional) - a scale number to tell difference of new RGB24 % from old RGB24. New RGB24 uses RGB triple sequentially for each % voxel, like [R1 G1 B1 R2 G2 B2 ...]. Analyze 6.0 from AnalyzeDirect % uses old RGB24, in a way like [R1 R2 ... G1 G2 ... B1 B2 ...] for % each slices. If the image that you view is garbled, try to set % old_RGB variable to 1 and try again, because it could be in % old RGB24. It will be set to 0, if it is default or empty. % % tolerance (optional) - distortion allowed for non-orthogonal rotation % or shearing in NIfTI affine matrix. It will be set to 0.1 (10%), % if it is default or empty. % % preferredForm (optional) - selects which transformation from voxels % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate % "prefer sform or qform, but use others if preferred not present". % Upper case indicate the program is forced to use the specificied % tranform or fail loading. 'preferredForm' will be 's', if it is % default or empty. - Jeff Gunter % % Example: flip_lr('avg152T1_LR_nifti.nii', 'flipped_lr.nii'); % flip_lr('avg152T1_RL_nifti.nii', 'flipped_rl.nii'); % % You will find that 'avg152T1_LR_nifti.nii' and 'avg152T1_RL_nifti.nii' % are the same, and 'flipped_lr.nii' and 'flipped_rl.nii' are also the % the same, but they are L-R flipped from 'avg152T1_*'. % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function flip_lr(original_fn, flipped_fn, old_RGB, tolerance, preferredForm) if ~exist('original_fn','var') | ~exist('flipped_fn','var') error('Usage: flip_lr(original_fn, flipped_fn, [old_RGB],[tolerance])'); end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end if ~exist('tolerance','var') | isempty(tolerance) tolerance = 0.1; end if ~exist('preferredForm','var') | isempty(preferredForm) preferredForm= 's'; % Jeff end nii = load_nii(original_fn, [], [], [], [], old_RGB, tolerance, preferredForm); M = diag(nii.hdr.dime.pixdim(2:5)); M(1:3,4) = -M(1:3,1:3)*(nii.hdr.hist.originator(1:3)-1)'; M(1,:) = -1*M(1,:); nii.hdr.hist.sform_code = 1; nii.hdr.hist.srow_x = M(1,:); nii.hdr.hist.srow_y = M(2,:); nii.hdr.hist.srow_z = M(3,:); save_nii(nii, flipped_fn); return; % flip_lr
github
sunhongfu/scripts-master
save_nii.m
.m
scripts-master/cs-phase/_src/_nii/save_nii.m
9,690
utf_8
ed292054cab74afaf953455bfbc200aa
% Save NIFTI dataset. Support both *.nii and *.hdr/*.img file extension. % If file extension is not provided, *.hdr/*.img will be used as default. % % Usage: save_nii(nii, filename, [old_RGB]) % % nii.hdr - struct with NIFTI header fields (from load_nii.m or make_nii.m) % % nii.img - 3D (or 4D) matrix of NIFTI data. % % filename - NIFTI file name. % % old_RGB - an optional boolean variable to handle special RGB data % sequence [R1 R2 ... G1 G2 ... B1 B2 ...] that is used only by % AnalyzeDirect (Analyze Software). Since both NIfTI and Analyze % file format use RGB triple [R1 G1 B1 R2 G2 B2 ...] sequentially % for each voxel, this variable is set to FALSE by default. If you % would like the saved image only to be opened by AnalyzeDirect % Software, set old_RGB to TRUE (or 1). It will be set to 0, if it % is default or empty. % % Tip: to change the data type, set nii.hdr.dime.datatype, % and nii.hdr.dime.bitpix to: % % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN % 1 Binary (ubit1, bitpix=1) % DT_BINARY % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8 % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16 % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32 % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32 % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64 % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64 % 128 uint RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24 % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8 % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96 % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16 % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32 % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64 % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64 % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128 % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % % Part of this file is copied and modified from: % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % - "old_RGB" related codes in "save_nii.m" are added by Mike Harms (2006.06.28) % function save_nii(nii, fileprefix, old_RGB) if ~exist('nii','var') | isempty(nii) | ~isfield(nii,'hdr') | ... ~isfield(nii,'img') | ~exist('fileprefix','var') | isempty(fileprefix) error('Usage: save_nii(nii, filename, [old_RGB])'); end if isfield(nii,'untouch') & nii.untouch == 1 error('Usage: please use ''save_untouch_nii.m'' for the untouched structure.'); end if ~exist('old_RGB','var') | isempty(old_RGB) old_RGB = 0; end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(fileprefix) > 2 & strcmp(fileprefix(end-2:end), '.gz') if ~strcmp(fileprefix(end-6:end), '.img.gz') & ... ~strcmp(fileprefix(end-6:end), '.hdr.gz') & ... ~strcmp(fileprefix(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); else gzFile = 1; fileprefix = fileprefix(1:end-3); end end filetype = 1; % Note: fileprefix is actually the filename you want to save % if findstr('.nii',fileprefix) & strcmp(fileprefix(end-3:end), '.nii') filetype = 2; fileprefix(end-3:end)=''; end if findstr('.hdr',fileprefix) & strcmp(fileprefix(end-3:end), '.hdr') fileprefix(end-3:end)=''; end if findstr('.img',fileprefix) & strcmp(fileprefix(end-3:end), '.img') fileprefix(end-3:end)=''; end write_nii(nii, filetype, fileprefix, old_RGB); % gzip output file if requested % if exist('gzFile', 'var') if filetype == 1 gzip([fileprefix, '.img']); delete([fileprefix, '.img']); gzip([fileprefix, '.hdr']); delete([fileprefix, '.hdr']); elseif filetype == 2 gzip([fileprefix, '.nii']); delete([fileprefix, '.nii']); end; end; if filetype == 1 % So earlier versions of SPM can also open it with correct originator % M=[[diag(nii.hdr.dime.pixdim(2:4)) -[nii.hdr.hist.originator(1:3).*nii.hdr.dime.pixdim(2:4)]'];[0 0 0 1]]; save([fileprefix '.mat'], 'M'); end return % save_nii %----------------------------------------------------------------------------------- function write_nii(nii, filetype, fileprefix, old_RGB) hdr = nii.hdr; if isfield(nii,'ext') & ~isempty(nii.ext) ext = nii.ext; [ext, esize_total] = verify_nii_ext(ext); else ext = []; end switch double(hdr.dime.datatype), case 1, hdr.dime.bitpix = int16(1 ); precision = 'ubit1'; case 2, hdr.dime.bitpix = int16(8 ); precision = 'uint8'; case 4, hdr.dime.bitpix = int16(16); precision = 'int16'; case 8, hdr.dime.bitpix = int16(32); precision = 'int32'; case 16, hdr.dime.bitpix = int16(32); precision = 'float32'; case 32, hdr.dime.bitpix = int16(64); precision = 'float32'; case 64, hdr.dime.bitpix = int16(64); precision = 'float64'; case 128, hdr.dime.bitpix = int16(24); precision = 'uint8'; case 256 hdr.dime.bitpix = int16(8 ); precision = 'int8'; case 511, hdr.dime.bitpix = int16(96); precision = 'float32'; case 512 hdr.dime.bitpix = int16(16); precision = 'uint16'; case 768 hdr.dime.bitpix = int16(32); precision = 'uint32'; case 1024 hdr.dime.bitpix = int16(64); precision = 'int64'; case 1280 hdr.dime.bitpix = int16(64); precision = 'uint64'; case 1792, hdr.dime.bitpix = int16(128); precision = 'float64'; otherwise error('This datatype is not supported'); end hdr.dime.glmax = round(double(max(nii.img(:)))); hdr.dime.glmin = round(double(min(nii.img(:)))); if filetype == 2 fid = fopen(sprintf('%s.nii',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.nii.',fileprefix); error(msg); end hdr.dime.vox_offset = 352; if ~isempty(ext) hdr.dime.vox_offset = hdr.dime.vox_offset + esize_total; end hdr.hist.magic = 'n+1'; save_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end else fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end hdr.dime.vox_offset = 0; hdr.hist.magic = 'ni1'; save_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end fclose(fid); fid = fopen(sprintf('%s.img',fileprefix),'w'); end ScanDim = double(hdr.dime.dim(5)); % t SliceDim = double(hdr.dime.dim(4)); % z RowDim = double(hdr.dime.dim(3)); % y PixelDim = double(hdr.dime.dim(2)); % x SliceSz = double(hdr.dime.pixdim(4)); RowSz = double(hdr.dime.pixdim(3)); PixelSz = double(hdr.dime.pixdim(2)); x = 1:PixelDim; if filetype == 2 & isempty(ext) skip_bytes = double(hdr.dime.vox_offset) - 348; else skip_bytes = 0; end if double(hdr.dime.datatype) == 128 % RGB planes are expected to be in the 4th dimension of nii.img % if(size(nii.img,4)~=3) error(['The NII structure does not appear to have 3 RGB color planes in the 4th dimension']); end if old_RGB nii.img = permute(nii.img, [1 2 4 3 5 6 7 8]); else nii.img = permute(nii.img, [4 1 2 3 5 6 7 8]); end end if double(hdr.dime.datatype) == 511 % RGB planes are expected to be in the 4th dimension of nii.img % if(size(nii.img,4)~=3) error(['The NII structure does not appear to have 3 RGB color planes in the 4th dimension']); end if old_RGB nii.img = permute(nii.img, [1 2 4 3 5 6 7 8]); else nii.img = permute(nii.img, [4 1 2 3 5 6 7 8]); end end % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 real_img = real(nii.img(:))'; nii.img = imag(nii.img(:))'; nii.img = [real_img; nii.img]; end if skip_bytes fwrite(fid, zeros(1,skip_bytes), 'uint8'); end fwrite(fid, nii.img, precision); % fwrite(fid, nii.img, precision, skip_bytes); % error using skip fclose(fid); return; % write_nii
github
sunhongfu/scripts-master
rri_file_menu.m
.m
scripts-master/cs-phase/_src/_nii/rri_file_menu.m
4,153
utf_8
c9faa3905c642854eeed98ab8b02998e
% Imbed a file menu to any figure. If file menu exist, it will append % to the existing file menu. This file menu includes: Copy to clipboard, % print, save, close etc. % % Usage: rri_file_menu(fig); % % rri_file_menu(fig,0) means no 'Close' menu. % % - Jimmy Shen ([email protected]) % %-------------------------------------------------------------------- function rri_file_menu(action, varargin) if isnumeric(action) fig = action; action = 'init'; end % clear the message line, % h = findobj(gcf,'Tag','MessageLine'); set(h,'String',''); if ~strcmp(action, 'init') set(gcbf, 'InvertHardcopy','off'); % set(gcbf, 'PaperPositionMode','auto'); end switch action case {'init'} if nargin > 1 init(fig, 1); % no 'close' menu else init(fig, 0); end case {'print_fig'} printdlg(gcbf); case {'copy_fig'} copy_fig; case {'export_fig'} export_fig; end return % rri_file_menu %------------------------------------------------ % % Create (or append) File menu % function init(fig, no_close) % search for file menu % h_file = []; menuitems = findobj(fig, 'type', 'uimenu'); for i=1:length(menuitems) filelabel = get(menuitems(i),'label'); if strcmpi(strrep(filelabel, '&', ''), 'file') h_file = menuitems(i); break; end end set(fig, 'menubar', 'none'); if isempty(h_file) if isempty(menuitems) h_file = uimenu('parent', fig, 'label', 'File'); else h_file = uimenu('parent', fig, 'label', 'Copy Figure'); end h1 = uimenu('parent', h_file, ... 'callback','rri_file_menu(''copy_fig'');', ... 'label','Copy to Clipboard'); else h1 = uimenu('parent', h_file, ... 'callback','rri_file_menu(''copy_fig'');', ... 'separator','on', ... 'label','Copy to Clipboard'); end h2 = uimenu(h_file, ... 'callback','pagesetupdlg(gcbf);', ... 'label','Page Setup...'); h2 = uimenu(h_file, ... 'callback','printpreview(gcbf);', ... 'label','Print Preview...'); h2 = uimenu('parent', h_file, ... 'callback','printdlg(gcbf);', ... 'label','Print Figure ...'); h2 = uimenu('parent', h_file, ... 'callback','rri_file_menu(''export_fig'');', ... 'label','Save Figure ...'); arch = computer; if ~strcmpi(arch(1:2),'PC') set(h1, 'enable', 'off'); end if ~no_close h1 = uimenu('parent', h_file, ... 'callback','close(gcbf);', ... 'separator','on', ... 'label','Close'); end return; % init %------------------------------------------------ % % Copy to clipboard % function copy_fig arch = computer; if(~strcmpi(arch(1:2),'PC')) error('copy to clipboard can only be used under MS Windows'); return; end print -noui -dbitmap; return % copy_fig %------------------------------------------------ % % Save as an image file % function export_fig curr = pwd; if isempty(curr) curr = filesep; end [selected_file, selected_path] = rri_select_file(curr,'Save As'); if isempty(selected_file) | isempty(selected_path) return; end filename = [selected_path selected_file]; if(exist(filename,'file')==2) % file exist dlg_title = 'Confirm File Overwrite'; msg = ['File ',filename,' exist. Are you sure you want to overwrite it?']; response = questdlg(msg,dlg_title,'Yes','No','Yes'); if(strcmp(response,'No')) return; end end old_pointer = get(gcbf,'pointer'); set(gcbf,'pointer','watch'); try saveas(gcbf,filename); catch msg = 'ERROR: Cannot save file'; set(findobj(gcf,'Tag','MessageLine'),'String',msg); end set(gcbf,'pointer',old_pointer); return; % export_fig
github
sunhongfu/scripts-master
reslice_nii.m
.m
scripts-master/cs-phase/_src/_nii/reslice_nii.m
10,138
utf_8
ea18d2f994fd5d9989449feaced1e4dd
% The basic application of the 'reslice_nii.m' program is to perform % any 3D affine transform defined by a NIfTI format image. % % In addition, the 'reslice_nii.m' program can also be applied to % generate an isotropic image from either a NIfTI format image or % an ANALYZE format image. % % The resliced NIfTI file will always be in RAS orientation. % % This program only supports real integer or floating-point data type. % For other data type, the program will exit with an error message % "Transform of this NIFTI data is not supported by the program". % % Usage: reslice_nii(old_fn, new_fn, [voxel_size], [verbose], [bg], ... % [method], [img_idx], [preferredForm]); % % old_fn - filename for original NIfTI file % % new_fn - filename for resliced NIfTI file % % voxel_size (optional) - size of a voxel in millimeter along x y z % direction for resliced NIfTI file. 'voxel_size' will use % the minimum voxel_size in original NIfTI header, % if it is default or empty. % % verbose (optional) - 1, 0 % 1: show transforming progress in percentage % 2: progress will not be displayed % 'verbose' is 1 if it is default or empty. % % bg (optional) - background voxel intensity in any extra corner that % is caused by 3D interpolation. 0 in most cases. 'bg' % will be the average of two corner voxel intensities % in original image volume, if it is default or empty. % % method (optional) - 1, 2, or 3 % 1: for Trilinear interpolation % 2: for Nearest Neighbor interpolation % 3: for Fischer's Bresenham interpolation % 'method' is 1 if it is default or empty. % % img_idx (optional) - a numerical array of image volume indices. Only % the specified volumes will be loaded. All available image % volumes will be loaded, if it is default or empty. % % The number of images scans can be obtained from get_nii_frame.m, % or simply: hdr.dime.dim(5). % % preferredForm (optional) - selects which transformation from voxels % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate % "prefer sform or qform, but use others if preferred not present". % Upper case indicate the program is forced to use the specificied % tranform or fail loading. 'preferredForm' will be 's', if it is % default or empty. - Jeff Gunter % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function reslice_nii(old_fn, new_fn, voxel_size, verbose, bg, method, img_idx, preferredForm) if ~exist('old_fn','var') | ~exist('new_fn','var') error('Usage: reslice_nii(old_fn, new_fn, [voxel_size], [verbose], [bg], [method], [img_idx])'); end if ~exist('method','var') | isempty(method) method = 1; end if ~exist('img_idx','var') | isempty(img_idx) img_idx = []; end if ~exist('verbose','var') | isempty(verbose) verbose = 1; end if ~exist('preferredForm','var') | isempty(preferredForm) preferredForm= 's'; % Jeff end nii = load_nii_no_xform(old_fn, img_idx, 0, preferredForm); if ~ismember(nii.hdr.dime.datatype, [2,4,8,16,64,256,512,768]) error('Transform of this NIFTI data is not supported by the program.'); end if ~exist('voxel_size','var') | isempty(voxel_size) voxel_size = abs(min(nii.hdr.dime.pixdim(2:4)))*ones(1,3); elseif length(voxel_size) < 3 voxel_size = abs(voxel_size(1))*ones(1,3); end if ~exist('bg','var') | isempty(bg) bg = mean([nii.img(1) nii.img(end)]); end old_M = nii.hdr.hist.old_affine; if nii.hdr.dime.dim(5) > 1 for i = 1:nii.hdr.dime.dim(5) if verbose fprintf('Reslicing %d of %d volumes.\n', i, nii.hdr.dime.dim(5)); end [img(:,:,:,i) M] = ... affine(nii.img(:,:,:,i), old_M, voxel_size, verbose, bg, method); end else [img M] = affine(nii.img, old_M, voxel_size, verbose, bg, method); end new_dim = size(img); nii.img = img; nii.hdr.dime.dim(2:4) = new_dim(1:3); nii.hdr.dime.datatype = 16; nii.hdr.dime.bitpix = 32; nii.hdr.dime.pixdim(2:4) = voxel_size(:)'; nii.hdr.dime.glmax = max(img(:)); nii.hdr.dime.glmin = min(img(:)); nii.hdr.hist.qform_code = 0; nii.hdr.hist.sform_code = 1; nii.hdr.hist.srow_x = M(1,:); nii.hdr.hist.srow_y = M(2,:); nii.hdr.hist.srow_z = M(3,:); nii.hdr.hist.new_affine = M; save_nii(nii, new_fn); return; % reslice_nii %-------------------------------------------------------------------- function [nii] = load_nii_no_xform(filename, img_idx, old_RGB, preferredForm) if ~exist('filename','var'), error('Usage: [nii] = load_nii(filename, [img_idx], [old_RGB])'); end if ~exist('img_idx','var'), img_idx = []; end if ~exist('old_RGB','var'), old_RGB = 0; end if ~exist('preferredForm','var'), preferredForm= 's'; end % Jeff v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end % Read the dataset header % [nii.hdr,nii.filetype,nii.fileprefix,nii.machine] = load_nii_hdr(filename); % Read the header extension % % nii.ext = load_nii_ext(filename); % Read the dataset body % [nii.img,nii.hdr] = ... load_nii_img(nii.hdr,nii.filetype,nii.fileprefix,nii.machine,img_idx,'','','',old_RGB); % Perform some of sform/qform transform % % nii = xform_nii(nii, preferredForm); % Clean up after gunzip % if exist('gzFileName', 'var') % fix fileprefix so it doesn't point to temp location % nii.fileprefix = gzFileName(1:end-7); rmdir(tmpDir,'s'); end hdr = nii.hdr; % NIFTI can have both sform and qform transform. This program % will check sform_code prior to qform_code by default. % % If user specifys "preferredForm", user can then choose the % priority. - Jeff % useForm=[]; % Jeff if isequal(preferredForm,'S') if isequal(hdr.hist.sform_code,0) error('User requires sform, sform not set in header'); else useForm='s'; end end % Jeff if isequal(preferredForm,'Q') if isequal(hdr.hist.qform_code,0) error('User requires sform, sform not set in header'); else useForm='q'; end end % Jeff if isequal(preferredForm,'s') if hdr.hist.sform_code > 0 useForm='s'; elseif hdr.hist.qform_code > 0 useForm='q'; end end % Jeff if isequal(preferredForm,'q') if hdr.hist.qform_code > 0 useForm='q'; elseif hdr.hist.sform_code > 0 useForm='s'; end end % Jeff if isequal(useForm,'s') R = [hdr.hist.srow_x(1:3) hdr.hist.srow_y(1:3) hdr.hist.srow_z(1:3)]; T = [hdr.hist.srow_x(4) hdr.hist.srow_y(4) hdr.hist.srow_z(4)]; nii.hdr.hist.old_affine = [ [R;[0 0 0]] [T;1] ]; elseif isequal(useForm,'q') b = hdr.hist.quatern_b; c = hdr.hist.quatern_c; d = hdr.hist.quatern_d; if 1.0-(b*b+c*c+d*d) < 0 if abs(1.0-(b*b+c*c+d*d)) < 1e-5 a = 0; else error('Incorrect quaternion values in this NIFTI data.'); end else a = sqrt(1.0-(b*b+c*c+d*d)); end qfac = hdr.dime.pixdim(1); i = hdr.dime.pixdim(2); j = hdr.dime.pixdim(3); k = qfac * hdr.dime.pixdim(4); R = [a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c 2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b 2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b]; T = [hdr.hist.qoffset_x hdr.hist.qoffset_y hdr.hist.qoffset_z]; nii.hdr.hist.old_affine = [ [R * diag([i j k]);[0 0 0]] [T;1] ]; elseif nii.filetype == 0 & exist([nii.fileprefix '.mat'],'file') load([nii.fileprefix '.mat']); % old SPM affine matrix R=M(1:3,1:3); T=M(1:3,4); T=R*ones(3,1)+T; M(1:3,4)=T; nii.hdr.hist.old_affine = M; else M = diag(hdr.dime.pixdim(2:5)); M(1:3,4) = -M(1:3,1:3)*(hdr.hist.originator(1:3)-1)'; M(4,4) = 1; nii.hdr.hist.old_affine = M; end return % load_nii_no_xform
github
sunhongfu/scripts-master
save_untouch_nii.m
.m
scripts-master/cs-phase/_src/_nii/save_untouch_nii.m
6,726
utf_8
cb98e2799abc112dca5b10078bde09bf
% Save NIFTI or ANALYZE dataset that is loaded by "load_untouch_nii.m". % The output image format and file extension will be the same as the % input one (NIFTI.nii, NIFTI.img or ANALYZE.img). Therefore, any file % extension that you specified will be ignored. % % Usage: save_untouch_nii(nii, filename) % % nii - nii structure that is loaded by "load_untouch_nii.m" % % filename - NIFTI or ANALYZE file name. % % - Jimmy Shen ([email protected]) % function save_untouch_nii(nii, filename) if ~exist('nii','var') | isempty(nii) | ~isfield(nii,'hdr') | ... ~isfield(nii,'img') | ~exist('filename','var') | isempty(filename) error('Usage: save_untouch_nii(nii, filename)'); end if ~isfield(nii,'untouch') | nii.untouch == 0 error('Usage: please use ''save_nii.m'' for the modified structure.'); end if isfield(nii.hdr.hist,'magic') & strcmp(nii.hdr.hist.magic(1:3),'ni1') filetype = 1; elseif isfield(nii.hdr.hist,'magic') & strcmp(nii.hdr.hist.magic(1:3),'n+1') filetype = 2; else filetype = 0; end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); else gzFile = 1; filename = filename(1:end-3); end end [p,f] = fileparts(filename); fileprefix = fullfile(p, f); write_nii(nii, filetype, fileprefix); % gzip output file if requested % if exist('gzFile', 'var') if filetype == 1 gzip([fileprefix, '.img']); delete([fileprefix, '.img']); gzip([fileprefix, '.hdr']); delete([fileprefix, '.hdr']); elseif filetype == 2 gzip([fileprefix, '.nii']); delete([fileprefix, '.nii']); end; end; % % So earlier versions of SPM can also open it with correct originator % % % if filetype == 0 % M=[[diag(nii.hdr.dime.pixdim(2:4)) -[nii.hdr.hist.originator(1:3).*nii.hdr.dime.pixdim(2:4)]'];[0 0 0 1]]; % save(fileprefix, 'M'); % elseif filetype == 1 % M=[]; % save(fileprefix, 'M'); %end return % save_untouch_nii %----------------------------------------------------------------------------------- function write_nii(nii, filetype, fileprefix) hdr = nii.hdr; if isfield(nii,'ext') & ~isempty(nii.ext) ext = nii.ext; [ext, esize_total] = verify_nii_ext(ext); else ext = []; end switch double(hdr.dime.datatype), case 1, hdr.dime.bitpix = int16(1 ); precision = 'ubit1'; case 2, hdr.dime.bitpix = int16(8 ); precision = 'uint8'; case 4, hdr.dime.bitpix = int16(16); precision = 'int16'; case 8, hdr.dime.bitpix = int16(32); precision = 'int32'; case 16, hdr.dime.bitpix = int16(32); precision = 'float32'; case 32, hdr.dime.bitpix = int16(64); precision = 'float32'; case 64, hdr.dime.bitpix = int16(64); precision = 'float64'; case 128, hdr.dime.bitpix = int16(24); precision = 'uint8'; case 256 hdr.dime.bitpix = int16(8 ); precision = 'int8'; case 512 hdr.dime.bitpix = int16(16); precision = 'uint16'; case 768 hdr.dime.bitpix = int16(32); precision = 'uint32'; case 1024 hdr.dime.bitpix = int16(64); precision = 'int64'; case 1280 hdr.dime.bitpix = int16(64); precision = 'uint64'; case 1792, hdr.dime.bitpix = int16(128); precision = 'float64'; otherwise error('This datatype is not supported'); end % hdr.dime.glmax = round(double(max(nii.img(:)))); % hdr.dime.glmin = round(double(min(nii.img(:)))); if filetype == 2 fid = fopen(sprintf('%s.nii',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.nii.',fileprefix); error(msg); end hdr.dime.vox_offset = 352; if ~isempty(ext) hdr.dime.vox_offset = hdr.dime.vox_offset + esize_total; end hdr.hist.magic = 'n+1'; save_untouch_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end elseif filetype == 1 fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end hdr.dime.vox_offset = 0; hdr.hist.magic = 'ni1'; save_untouch_nii_hdr(hdr, fid); if ~isempty(ext) save_nii_ext(ext, fid); end fclose(fid); fid = fopen(sprintf('%s.img',fileprefix),'w'); else fid = fopen(sprintf('%s.hdr',fileprefix),'w'); if fid < 0, msg = sprintf('Cannot open file %s.hdr.',fileprefix); error(msg); end save_untouch0_nii_hdr(hdr, fid); fclose(fid); fid = fopen(sprintf('%s.img',fileprefix),'w'); end ScanDim = double(hdr.dime.dim(5)); % t SliceDim = double(hdr.dime.dim(4)); % z RowDim = double(hdr.dime.dim(3)); % y PixelDim = double(hdr.dime.dim(2)); % x SliceSz = double(hdr.dime.pixdim(4)); RowSz = double(hdr.dime.pixdim(3)); PixelSz = double(hdr.dime.pixdim(2)); x = 1:PixelDim; if filetype == 2 & isempty(ext) skip_bytes = double(hdr.dime.vox_offset) - 348; else skip_bytes = 0; end if double(hdr.dime.datatype) == 128 % RGB planes are expected to be in the 4th dimension of nii.img % if(size(nii.img,4)~=3) error(['The NII structure does not appear to have 3 RGB color planes in the 4th dimension']); end nii.img = permute(nii.img, [4 1 2 3 5 6 7 8]); end % For complex float32 or complex float64, voxel values % include [real, imag] % if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 real_img = real(nii.img(:))'; nii.img = imag(nii.img(:))'; nii.img = [real_img; nii.img]; end if skip_bytes fwrite(fid, zeros(1,skip_bytes), 'uint8'); end fwrite(fid, nii.img, precision); % fwrite(fid, nii.img, precision, skip_bytes); % error using skip fclose(fid); return; % write_nii
github
sunhongfu/scripts-master
view_nii.m
.m
scripts-master/cs-phase/_src/_nii/view_nii.m
144,481
utf_8
8ea68ec34d3a6bec721497afb56cfb54
% VIEW_NII: Create or update a 3-View (Front, Top, Side) of the % brain data that is specified by nii structure % % Usage: status = view_nii([h], nii, [option]) or % status = view_nii(h, [option]) % % Where, h is the figure on which the 3-View will be plotted; % nii is the brain data in NIFTI format; % option is a struct that configures the view plotted, can be: % % option.command = 'init' % option.command = 'update' % option.command = 'clearnii' % option.command = 'updatenii' % option.command = 'updateimg' (nii is nii.img here) % % option.usecolorbar = 0 | [1] % option.usepanel = 0 | [1] % option.usecrosshair = 0 | [1] % option.usestretch = 0 | [1] % option.useimagesc = 0 | [1] % option.useinterp = [0] | 1 % % option.setarea = [x y w h] | [0.05 0.05 0.9 0.9] % option.setunit = ['vox'] | 'mm' % option.setviewpoint = [x y z] | [origin] % option.setscanid = [t] | [1] % option.setcrosshaircolor = [r g b] | [1 0 0] % option.setcolorindex = From 1 to 9 (default is 2 or 3) % option.setcolormap = (Mx3 matrix, 0 <= val <= 1) % option.setcolorlevel = No more than 256 (default 256) % option.sethighcolor = [] % option.setcbarminmax = [] % option.setvalue = [] % option.glblocminmax = [] % option.setbuttondown = '' % option.setcomplex = [0] | 1 | 2 % % Options description in detail: % ============================== % % 1. command: A char string that can control program. % % init: If option.command='init', the program will display % a 3-View plot on the figure specified by figure h % or on a new figure. If there is already a 3-View % plot on the figure, please use option.command = % 'updatenii' (see detail below); otherwise, the % new 3-View plot will superimpose on the old one. % If there is no option provided, the program will % assume that this is an initial plot. If the figure % handle is omitted, the program knows that it is % an initial plot. % % update: If there is no command specified, and a figure % handle of the existing 3-View plot is provided, % the program will choose option.command='update' % to update the 3-View plot with some new option % items. % % clearnii: Clear 3-View plot on specific figure % % updatenii: If a new nii is going to be loaded on a fig % that has already 3-View plot on it, use this % command to clear existing 3-View plot, and then % display with new nii. So, the new nii will not % superimpose on the existing one. All options % for 'init' can be used for 'updatenii'. % % updateimg: If a new 3D matrix with the same dimension % is going to be loaded, option.command='updateimg' % can be used as a light-weighted 'updatenii, since % it only updates the 3 slices with new values. % inputing argument nii should be a 3D matrix % (nii.img) instead of nii struct. No other option % should be used together with 'updateimg' to keep % this command as simple as possible. % % % 2. usecolorbar: If specified and usecolorbar=0, the program % will not include the colorbar in plot area; otherwise, % a colorbar will be included in plot area. % % 3. usepanel: If specified and usepanel=0, the control panel % at lower right cornor will be invisible; otherwise, % it will be visible. % % 4. usecrosshair: If specified and usecrosshair=0, the crosshair % will be invisible; otherwise, it will be visible. % % 5. usestretch: If specified and usestretch=0, the 3 slices will % not be stretched, and will be displayed according to % the actual voxel size; otherwise, the 3 slices will be % stretched to the edge. % % 6. useimagesc: If specified and useimagesc=0, images data will % be used directly to match the colormap (like 'image' % command); otherwise, image data will be scaled to full % colormap with 'imagesc' command in Matlab. % % 7. useinterp: If specified and useinterp=1, the image will be % displayed using interpolation. Otherwise, it will be % displayed like mosaic, and each tile stands for a % pixel. This option does not apply to 'setvalue' option % is set. % % % 8. setarea: 3-View plot will be displayed on this specific % region. If it is not specified, program will set the % plot area to [0.05 0.05 0.9 0.9]. % % 9. setunit: It can be specified to setunit='voxel' or 'mm' % and the view will change the axes unit of [X Y Z] % accordingly. % % 10. setviewpoint: If specified, [X Y Z] values will be used % to set the viewpoint of 3-View plot. % % 11. setscanid: If specified, [t] value will be used to display % the specified image scan in NIFTI data. % % 12. setcrosshaircolor: If specified, [r g b] value will be used % for Crosshair Color. Otherwise, red will be the default. % % 13. setcolorindex: If specified, the 3-View will choose the % following colormap: 2 - Bipolar; 3 - Gray; 4 - Jet; % 5 - Cool; 6 - Bone; 7 - Hot; 8 - Copper; 9 - Pink; % If not specified, it will choose 3 - Gray if all data % values are not less than 0; otherwise, it will choose % 2 - Bipolar if there is value less than 0. (Contrast % control can only apply to 3 - Gray colormap. % % 14. setcolormap: 3-View plot will use it as a customized colormap. % It is a 3-column matrix with value between 0 and 1. If % using MS-Windows version of Matlab, the number of rows % can not be more than 256, because of Matlab limitation. % When colormap is used, setcolorlevel option will be % disabled automatically. % % 15. setcolorlevel: If specified (must be no more than 256, and % cannot be used for customized colormap), row number of % colormap will be squeezed down to this level; otherwise, % it will assume that setcolorlevel=256. % % 16. sethighcolor: If specified, program will squeeze down the % colormap, and allocate sethighcolor (an Mx3 matrix) % to high-end portion of the colormap. The sum of M and % setcolorlevel should be less than 256. If setcolormap % option is used, sethighcolor will be inserted on top % of the setcolormap, and the setcolorlevel option will % be disabled automatically. % % 17. setcbarminmax: if specified, the [min max] will be used to % set the min and max of the colorbar, which does not % include any data for highcolor. % % 18. setvalue: If specified, setvalue.val (with the same size as % the source data on solution points) in the source area % setvalue.idx will be superimposed on the current nii % image. So, the size of setvalue.val should be equal to % the size of setvalue.idx. To use this feature, it needs % single or double nii structure for background image. % % 19. glblocminmax: If specified, pgm will use glblocminmax to % calculate the colormap, instead of minmax of image. % % 20. setbuttondown: If specified, pgm will evaluate the command % after a click or slide action is invoked to the new % view point. % % 21. setcomplex: This option will decide how complex data to be % displayed: 0 - Real part of complex data; 1 - Imaginary % part of complex data; 2 - Modulus (magnitude) of complex % data; If not specified, it will be set to 0 (Real part % of complex data as default option. This option only apply % when option.command is set to 'init or 'updatenii'. % % % Additional Options for 'update' command: % ======================================= % % option.enablecursormove = [1] | 0 % option.enableviewpoint = 0 | [1] % option.enableorigin = 0 | [1] % option.enableunit = 0 | [1] % option.enablecrosshair = 0 | [1] % option.enablehistogram = 0 | [1] % option.enablecolormap = 0 | [1] % option.enablecontrast = 0 | [1] % option.enablebrightness = 0 | [1] % option.enableslider = 0 | [1] % option.enabledirlabel = 0 | [1] % % % e.g.: % nii = load_nii('T1'); % T1.img/hdr % view_nii(nii); % % or % % h = figure('unit','normal','pos', [0.18 0.08 0.64 0.85]); % opt.setarea = [0.05 0.05 0.9 0.9]; % view_nii(h, nii, opt); % % % Part of this file is copied and modified from: % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function status = view_nii(varargin) if nargin < 1 error('Please check inputs using ''help view_nii'''); end; nii = ''; opt = ''; command = ''; usecolorbar = []; usepanel = []; usecrosshair = ''; usestretch = []; useimagesc = []; useinterp = []; setarea = []; setunit = ''; setviewpoint = []; setscanid = []; setcrosshaircolor = []; setcolorindex = ''; setcolormap = 'NA'; setcolorlevel = []; sethighcolor = 'NA'; setcbarminmax = []; setvalue = []; glblocminmax = []; setbuttondown = ''; setcomplex = 0; status = []; if ishandle(varargin{1}) % plot on top of this figure fig = varargin{1}; if nargin < 2 command = 'update'; % just to get 3-View status end if nargin == 2 if ~isstruct(varargin{2}) error('2nd parameter should be either nii struct or option struct'); end opt = varargin{2}; if isfield(opt,'hdr') & isfield(opt,'img') nii = opt; elseif isfield(opt, 'command') & (strcmpi(opt.command,'init') ... | strcmpi(opt.command,'updatenii') ... | strcmpi(opt.command,'updateimg') ) error('Option here cannot contain "init", "updatenii", or "updateimg" comand'); end end if nargin == 3 nii = varargin{2}; opt = varargin{3}; if ~isstruct(opt) error('3rd parameter should be option struct'); end if ~isfield(opt,'command') | ~strcmpi(opt.command,'updateimg') if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img') error('2nd parameter should be nii struct'); end if isfield(nii,'untouch') & nii.untouch == 1 error('Usage: please use ''load_nii.m'' to load the structure.'); end end end set(fig, 'menubar', 'none'); elseif ischar(varargin{1}) % call back by event command = lower(varargin{1}); fig = gcbf; else % start nii with a new figure nii = varargin{1}; if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img') error('1st parameter should be either a figure handle or nii struct'); end if isfield(nii,'untouch') & nii.untouch == 1 error('Usage: please use ''load_nii.m'' to load the structure.'); end if nargin > 1 opt = varargin{2}; if isfield(opt, 'command') & ~strcmpi(opt.command,'init') error('Option here must use "init" comand'); end end command = 'init'; fig = figure('unit','normal','position',[0.15 0.08 0.70 0.85]); view_nii_menu(fig); rri_file_menu(fig); end if ~isempty(opt) if isfield(opt,'command') command = lower(opt.command); end if isempty(command) command = 'update'; end if isfield(opt,'usecolorbar') usecolorbar = opt.usecolorbar; end if isfield(opt,'usepanel') usepanel = opt.usepanel; end if isfield(opt,'usecrosshair') usecrosshair = opt.usecrosshair; end if isfield(opt,'usestretch') usestretch = opt.usestretch; end if isfield(opt,'useimagesc') useimagesc = opt.useimagesc; end if isfield(opt,'useinterp') useinterp = opt.useinterp; end if isfield(opt,'setarea') setarea = opt.setarea; end if isfield(opt,'setunit') setunit = opt.setunit; end if isfield(opt,'setviewpoint') setviewpoint = opt.setviewpoint; end if isfield(opt,'setscanid') setscanid = opt.setscanid; end if isfield(opt,'setcrosshaircolor') setcrosshaircolor = opt.setcrosshaircolor; if ~isempty(setcrosshaircolor) & (~isnumeric(setcrosshaircolor) | ~isequal(size(setcrosshaircolor),[1 3]) | min(setcrosshaircolor(:))<0 | max(setcrosshaircolor(:))>1) error('Crosshair Color should be a 1x3 matrix with value between 0 and 1'); end end if isfield(opt,'setcolorindex') setcolorindex = round(opt.setcolorindex); if ~isnumeric(setcolorindex) | setcolorindex < 1 | setcolorindex > 9 error('Colorindex should be a number between 1 and 9'); end end if isfield(opt,'setcolormap') setcolormap = opt.setcolormap; if ~isempty(setcolormap) & (~isnumeric(setcolormap) | size(setcolormap,2) ~= 3 | min(setcolormap(:))<0 | max(setcolormap(:))>1) error('Colormap should be a Mx3 matrix with value between 0 and 1'); end end if isfield(opt,'setcolorlevel') setcolorlevel = round(opt.setcolorlevel); if ~isnumeric(setcolorlevel) | setcolorlevel > 256 | setcolorlevel < 1 error('Colorlevel should be a number between 1 and 256'); end end if isfield(opt,'sethighcolor') sethighcolor = opt.sethighcolor; if ~isempty(sethighcolor) & (~isnumeric(sethighcolor) | size(sethighcolor,2) ~= 3 | min(sethighcolor(:))<0 | max(sethighcolor(:))>1) error('Highcolor should be a Mx3 matrix with value between 0 and 1'); end end if isfield(opt,'setcbarminmax') setcbarminmax = opt.setcbarminmax; if isempty(setcbarminmax) | ~isnumeric(setcbarminmax) | length(setcbarminmax) ~= 2 error('Colorbar MinMax should contain 2 values: [min max]'); end end if isfield(opt,'setvalue') setvalue = opt.setvalue; if isempty(setvalue) | ~isstruct(setvalue) | ... ~isfield(opt.setvalue,'idx') | ~isfield(opt.setvalue,'val') error('setvalue should be a struct contains idx and val'); end if length(opt.setvalue.idx(:)) ~= length(opt.setvalue.val(:)) error('length of idx and val fields should be the same'); end if ~strcmpi(class(opt.setvalue.idx),'single') opt.setvalue.idx = single(opt.setvalue.idx); end if ~strcmpi(class(opt.setvalue.val),'single') opt.setvalue.val = single(opt.setvalue.val); end end if isfield(opt,'glblocminmax') glblocminmax = opt.glblocminmax; end if isfield(opt,'setbuttondown') setbuttondown = opt.setbuttondown; end if isfield(opt,'setcomplex') setcomplex = opt.setcomplex; end end switch command case {'init'} set(fig, 'InvertHardcopy','off'); set(fig, 'PaperPositionMode','auto'); fig = init(nii, fig, setarea, setunit, setviewpoint, setscanid, setbuttondown, ... setcolorindex, setcolormap, setcolorlevel, sethighcolor, setcbarminmax, ... usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, useinterp, ... setvalue, glblocminmax, setcrosshaircolor, setcomplex); % get status % status = get_status(fig); case {'update'} nii_view = getappdata(fig,'nii_view'); h = fig; if isempty(nii_view) error('The figure should already contain a 3-View plot.'); end if ~isempty(opt) % Order of the following update matters. % update_shape(h, setarea, usecolorbar, usestretch, useimagesc); update_useinterp(h, useinterp); update_useimagesc(h, useimagesc); update_usepanel(h, usepanel); update_colorindex(h, setcolorindex); update_colormap(h, setcolormap); update_highcolor(h, sethighcolor, setcolorlevel); update_cbarminmax(h, setcbarminmax); update_unit(h, setunit); update_viewpoint(h, setviewpoint); update_scanid(h, setscanid); update_buttondown(h, setbuttondown); update_crosshaircolor(h, setcrosshaircolor); update_usecrosshair(h, usecrosshair); % Enable/Disable object % update_enable(h, opt); end % get status % status = get_status(h); case {'updateimg'} if ~exist('nii','var') msg = sprintf('Please input a 3D matrix brain data'); error(msg); end % Note: nii is not nii, nii should be a 3D matrix here % if ~isnumeric(nii) msg = sprintf('2nd parameter should be a 3D matrix, not nii struct'); error(msg); end nii_view = getappdata(fig,'nii_view'); if isempty(nii_view) error('The figure should already contain a 3-View plot.'); end img = nii; update_img(img, fig, opt); % get status % status = get_status(fig); case {'updatenii'} nii_view = getappdata(fig,'nii_view'); if isempty(nii_view) error('The figure should already contain a 3-View plot.'); end if ~isstruct(nii) | ~isfield(nii,'hdr') | ~isfield(nii,'img') error('2nd parameter should be nii struct'); end if isfield(nii,'untouch') & nii.untouch == 1 error('Usage: please use ''load_nii.m'' to load the structure.'); end opt.command = 'clearnii'; view_nii(fig, opt); opt.command = 'init'; view_nii(fig, nii, opt); % get status % status = get_status(fig); case {'clearnii'} nii_view = getappdata(fig,'nii_view'); handles = struct2cell(nii_view.handles); for i=1:length(handles) if ishandle(handles{i}) % in case already del by parent delete(handles{i}); end end rmappdata(fig,'nii_view'); buttonmotion = get(fig,'windowbuttonmotion'); mymotion = '; view_nii(''move_cursor'');'; buttonmotion = strrep(buttonmotion, mymotion, ''); set(fig, 'windowbuttonmotion', buttonmotion); case {'axial_image','coronal_image','sagittal_image'} switch command case 'axial_image', view = 'axi'; axi = 0; cor = 1; sag = 1; case 'coronal_image', view = 'cor'; axi = 1; cor = 0; sag = 1; case 'sagittal_image', view = 'sag'; axi = 1; cor = 1; sag = 0; end nii_view = getappdata(fig,'nii_view'); nii_view = get_slice_position(nii_view,view); if isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end % CData must be double() for Matlab 6.5 for Windows % if axi, if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi)); set(nii_view.handles.axial_bg,'CData',double(Saxi)'); end if isfield(nii_view.handles,'axial_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid)); Saxi = permute(Saxi, [2 1 3]); else Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid)); Saxi = Saxi'; end set(nii_view.handles.axial_image,'CData',double(Saxi)); end if isfield(nii_view.handles,'axial_slider'), set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi); end; end if cor, if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:)); set(nii_view.handles.coronal_bg,'CData',double(Scor)'); end if isfield(nii_view.handles,'coronal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid)); Scor = permute(Scor, [2 1 3]); else Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid)); Scor = Scor'; end set(nii_view.handles.coronal_image,'CData',double(Scor)); end if isfield(nii_view.handles,'coronal_slider'), slider_val = nii_view.dims(2) - nii_view.slices.cor + 1; set(nii_view.handles.coronal_slider,'Value',slider_val); end; end; if sag, if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:)); set(nii_view.handles.sagittal_bg,'CData',double(Ssag)'); end if isfield(nii_view.handles,'sagittal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid)); Ssag = permute(Ssag, [2 1 3]); else Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid)); Ssag = Ssag'; end set(nii_view.handles.sagittal_image,'CData',double(Ssag)); end if isfield(nii_view.handles,'sagittal_slider'), set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag); end; end; update_nii_view(nii_view); if ~isempty(nii_view.buttondown) eval(nii_view.buttondown); end case {'axial_slider','coronal_slider','sagittal_slider'}, switch command case 'axial_slider', view = 'axi'; axi = 1; cor = 0; sag = 0; case 'coronal_slider', view = 'cor'; axi = 0; cor = 1; sag = 0; case 'sagittal_slider', view = 'sag'; axi = 0; cor = 0; sag = 1; end nii_view = getappdata(fig,'nii_view'); nii_view = get_slider_position(nii_view); if isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end if axi, if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi)); set(nii_view.handles.axial_bg,'CData',double(Saxi)'); end if isfield(nii_view.handles,'axial_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid)); Saxi = permute(Saxi, [2 1 3]); else Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid)); Saxi = Saxi'; end set(nii_view.handles.axial_image,'CData',double(Saxi)); end if isfield(nii_view.handles,'axial_slider'), set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi); end end if cor, if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:)); set(nii_view.handles.coronal_bg,'CData',double(Scor)'); end if isfield(nii_view.handles,'coronal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid)); Scor = permute(Scor, [2 1 3]); else Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid)); Scor = Scor'; end set(nii_view.handles.coronal_image,'CData',double(Scor)); end if isfield(nii_view.handles,'coronal_slider'), slider_val = nii_view.dims(2) - nii_view.slices.cor + 1; set(nii_view.handles.coronal_slider,'Value',slider_val); end end if sag, if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:)); set(nii_view.handles.sagittal_bg,'CData',double(Ssag)'); end if isfield(nii_view.handles,'sagittal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid)); Ssag = permute(Ssag, [2 1 3]); else Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid)); Ssag = Ssag'; end set(nii_view.handles.sagittal_image,'CData',double(Ssag)); end if isfield(nii_view.handles,'sagittal_slider'), set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag); end end update_nii_view(nii_view); if ~isempty(nii_view.buttondown) eval(nii_view.buttondown); end case {'impos_edit'} nii_view = getappdata(fig,'nii_view'); impos = str2num(get(nii_view.handles.impos,'string')); if isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end if isempty(impos) | ~all(size(impos) == [1 3]) msg = 'Please use 3 numbers to represent X,Y and Z'; msgbox(msg,'Error'); return; end slices.sag = round(impos(1)); slices.cor = round(impos(2)); slices.axi = round(impos(3)); nii_view = convert2voxel(nii_view,slices); nii_view = check_slices(nii_view); impos(1) = nii_view.slices.sag; impos(2) = nii_view.dims(2) - nii_view.slices.cor + 1; impos(3) = nii_view.slices.axi; if isfield(nii_view.handles,'sagittal_slider'), set(nii_view.handles.sagittal_slider,'Value',impos(1)); end if isfield(nii_view.handles,'coronal_slider'), set(nii_view.handles.coronal_slider,'Value',impos(2)); end if isfield(nii_view.handles,'axial_slider'), set(nii_view.handles.axial_slider,'Value',impos(3)); end nii_view = get_slider_position(nii_view); update_nii_view(nii_view); if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) & nii_view.useinterp Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi)); set(nii_view.handles.axial_bg,'CData',double(Saxi)'); end if isfield(nii_view.handles,'axial_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Saxi = squeeze(img(:,:,nii_view.slices.axi,:,nii_view.scanid)); Saxi = permute(Saxi, [2 1 3]); else Saxi = squeeze(img(:,:,nii_view.slices.axi,nii_view.scanid)); Saxi = Saxi'; end set(nii_view.handles.axial_image,'CData',double(Saxi)); end if isfield(nii_view.handles,'axial_slider'), set(nii_view.handles.axial_slider,'Value',nii_view.slices.axi); end if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) & nii_view.useinterp Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:)); set(nii_view.handles.coronal_bg,'CData',double(Scor)'); end if isfield(nii_view.handles,'coronal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Scor = squeeze(img(:,nii_view.slices.cor,:,:,nii_view.scanid)); Scor = permute(Scor, [2 1 3]); else Scor = squeeze(img(:,nii_view.slices.cor,:,nii_view.scanid)); Scor = Scor'; end set(nii_view.handles.coronal_image,'CData',double(Scor)); end if isfield(nii_view.handles,'coronal_slider'), slider_val = nii_view.dims(2) - nii_view.slices.cor + 1; set(nii_view.handles.coronal_slider,'Value',slider_val); end if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) & nii_view.useinterp Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:)); set(nii_view.handles.sagittal_bg,'CData',double(Ssag)'); end if isfield(nii_view.handles,'sagittal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Ssag = squeeze(img(nii_view.slices.sag,:,:,:,nii_view.scanid)); Ssag = permute(Ssag, [2 1 3]); else Ssag = squeeze(img(nii_view.slices.sag,:,:,nii_view.scanid)); Ssag = Ssag'; end set(nii_view.handles.sagittal_image,'CData',double(Ssag)); end if isfield(nii_view.handles,'sagittal_slider'), set(nii_view.handles.sagittal_slider,'Value',nii_view.slices.sag); end axes(nii_view.handles.axial_axes); axes(nii_view.handles.coronal_axes); axes(nii_view.handles.sagittal_axes); if ~isempty(nii_view.buttondown) eval(nii_view.buttondown); end case 'coordinates', nii_view = getappdata(fig,'nii_view'); set_image_value(nii_view); case 'crosshair', nii_view = getappdata(fig,'nii_view'); if get(nii_view.handles.xhair,'value') == 2 % off set(nii_view.axi_xhair.lx,'visible','off'); set(nii_view.axi_xhair.ly,'visible','off'); set(nii_view.cor_xhair.lx,'visible','off'); set(nii_view.cor_xhair.ly,'visible','off'); set(nii_view.sag_xhair.lx,'visible','off'); set(nii_view.sag_xhair.ly,'visible','off'); else set(nii_view.axi_xhair.lx,'visible','on'); set(nii_view.axi_xhair.ly,'visible','on'); set(nii_view.cor_xhair.lx,'visible','on'); set(nii_view.cor_xhair.ly,'visible','on'); set(nii_view.sag_xhair.lx,'visible','on'); set(nii_view.sag_xhair.ly,'visible','on'); set(nii_view.handles.axial_axes,'selected','on'); set(nii_view.handles.axial_axes,'selected','off'); set(nii_view.handles.coronal_axes,'selected','on'); set(nii_view.handles.coronal_axes,'selected','off'); set(nii_view.handles.sagittal_axes,'selected','on'); set(nii_view.handles.sagittal_axes,'selected','off'); end case 'xhair_color', old_color = get(gcbo,'user'); new_color = uisetcolor(old_color); update_crosshaircolor(fig, new_color); case {'color','contrast_def'} nii_view = getappdata(fig,'nii_view'); if nii_view.numscan == 1 if get(nii_view.handles.colorindex,'value') == 2 set(nii_view.handles.contrast,'value',128); elseif get(nii_view.handles.colorindex,'value') == 3 set(nii_view.handles.contrast,'value',1); end end [custom_color_map, custom_colorindex] = change_colormap(fig); if strcmpi(command, 'color') setcolorlevel = nii_view.colorlevel; if ~isempty(custom_color_map) % isfield(nii_view, 'color_map') setcolormap = custom_color_map; % nii_view.color_map; else setcolormap = []; end if isfield(nii_view, 'highcolor') sethighcolor = nii_view.highcolor; else sethighcolor = []; end redraw_cbar(fig, setcolorlevel, setcolormap, sethighcolor); if nii_view.numscan == 1 & ... (custom_colorindex < 2 | custom_colorindex > 3) contrastopt.enablecontrast = 0; else contrastopt.enablecontrast = 1; end update_enable(fig, contrastopt); end case {'neg_color','brightness','contrast'} change_colormap(fig); case {'brightness_def'} nii_view = getappdata(fig,'nii_view'); set(nii_view.handles.brightness,'value',0); change_colormap(fig); case 'hist_plot' hist_plot(fig); case 'hist_eq' hist_eq(fig); case 'move_cursor' move_cursor(fig); case 'edit_change_scan' change_scan('edit_change_scan'); case 'slider_change_scan' change_scan('slider_change_scan'); end return; % view_nii %---------------------------------------------------------------- function fig = init(nii, fig, area, setunit, setviewpoint, setscanid, buttondown, ... colorindex, color_map, colorlevel, highcolor, cbarminmax, ... usecolorbar, usepanel, usecrosshair, usestretch, useimagesc, ... useinterp, setvalue, glblocminmax, setcrosshaircolor, ... setcomplex) % Support data type COMPLEX64 & COMPLEX128 % if nii.hdr.dime.datatype == 32 | nii.hdr.dime.datatype == 1792 switch setcomplex, case 0, nii.img = real(nii.img); case 1, nii.img = imag(nii.img); case 2, if isa(nii.img, 'double') nii.img = abs(double(nii.img)); else nii.img = single(abs(double(nii.img))); end end end if isempty(area) area = [0.05 0.05 0.9 0.9]; end if isempty(setscanid) setscanid = 1; else setscanid = round(setscanid); if setscanid < 1 setscanid = 1; end if setscanid > nii.hdr.dime.dim(5) setscanid = nii.hdr.dime.dim(5); end end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 usecolorbar = 0; elseif isempty(usecolorbar) usecolorbar = 1; end if isempty(usepanel) usepanel = 1; end if isempty(usestretch) usestretch = 1; end if isempty(useimagesc) useimagesc = 1; end if isempty(useinterp) useinterp = 0; end if isempty(colorindex) tmp = min(nii.img(:,:,:,setscanid)); if min(tmp(:)) < 0 colorindex = 2; setcrosshaircolor = [1 1 0]; else colorindex = 3; end end if isempty(color_map) | ischar(color_map) color_map = []; else colorindex = 1; end bgimg = []; if ~isempty(glblocminmax) minvalue = glblocminmax(1); maxvalue = glblocminmax(2); else minvalue = nii.img(:,:,:,setscanid); minvalue = double(minvalue(:)); minvalue = min(minvalue(~isnan(minvalue))); maxvalue = nii.img(:,:,:,setscanid); maxvalue = double(maxvalue(:)); maxvalue = max(maxvalue(~isnan(maxvalue))); end if ~isempty(setvalue) if ~isempty(glblocminmax) minvalue = glblocminmax(1); maxvalue = glblocminmax(2); else minvalue = double(min(setvalue.val)); maxvalue = double(max(setvalue.val)); end bgimg = double(nii.img); minbg = double(min(bgimg(:))); maxbg = double(max(bgimg(:))); bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256 % 56 level for brain structure % % highcolor = [zeros(1,3);gray(55)]; highcolor = gray(56); cbarminmax = [minvalue maxvalue]; if useinterp % scale signal data to 1~200 % nii.img = repmat(nan, size(nii.img)); nii.img(setvalue.idx) = setvalue.val; % 200 level for source image % bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199)); else bgimg(setvalue.idx) = NaN; minbg = double(min(bgimg(:))); maxbg = double(max(bgimg(:))); bgimg(setvalue.idx) = minbg; % bgimg must be normalized to [201 256] % bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201; bgimg(setvalue.idx) = 0; % scale signal data to 1~200 % nii.img = zeros(size(nii.img)); nii.img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199); nii.img = nii.img + bgimg; bgimg = []; nii.img = scale_out(nii.img, cbarminmax(1), cbarminmax(2), 199); minvalue = double(nii.img(:)); minvalue = min(minvalue(~isnan(minvalue))); maxvalue = double(nii.img(:)); maxvalue = max(maxvalue(~isnan(maxvalue))); if ~isempty(glblocminmax) % maxvalue is gray minvalue = glblocminmax(1); end end colorindex = 2; setcrosshaircolor = [1 1 0]; end if isempty(highcolor) | ischar(highcolor) highcolor = []; num_highcolor = 0; else num_highcolor = size(highcolor,1); end if isempty(colorlevel) colorlevel = 256 - num_highcolor; end if usecolorbar cbar_area = area; cbar_area(1) = area(1) + area(3)*0.93; cbar_area(3) = area(3)*0.04; area(3) = area(3)*0.9; % 90% used for main axes else cbar_area = []; end % init color (gray) scaling to make sure the slice clim take the % global clim [min(nii.img(:)) max(nii.img(:))] % if isempty(bgimg) clim = [minvalue maxvalue]; else clim = [minvalue double(max(bgimg(:)))]; end if clim(1) == clim(2) clim(2) = clim(1) + 0.000001; end if isempty(cbarminmax) cbarminmax = [minvalue maxvalue]; end xdim = size(nii.img, 1); ydim = size(nii.img, 2); zdim = size(nii.img, 3); dims = [xdim ydim zdim]; voxel_size = abs(nii.hdr.dime.pixdim(2:4)); % vol in mm if any(voxel_size <= 0) voxel_size(find(voxel_size <= 0)) = 1; end origin = abs(nii.hdr.hist.originator(1:3)); if isempty(origin) | all(origin == 0) % according to SPM origin = (dims+1)/2; end; origin = round(origin); if any(origin > dims) % simulate fMRI origin(find(origin > dims)) = dims(find(origin > dims)); end if any(origin <= 0) origin(find(origin <= 0)) = 1; end nii_view.dims = dims; nii_view.voxel_size = voxel_size; nii_view.origin = origin; nii_view.slices.sag = 1; nii_view.slices.cor = 1; nii_view.slices.axi = 1; if xdim > 1, nii_view.slices.sag = origin(1); end if ydim > 1, nii_view.slices.cor = origin(2); end if zdim > 1, nii_view.slices.axi = origin(3); end nii_view.area = area; nii_view.fig = fig; nii_view.nii = nii; % image data nii_view.bgimg = bgimg; % background nii_view.setvalue = setvalue; nii_view.minvalue = minvalue; nii_view.maxvalue = maxvalue; nii_view.numscan = nii.hdr.dime.dim(5); nii_view.scanid = setscanid; Font.FontUnits = 'point'; Font.FontSize = 12; % create axes for colorbar % [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area); if isempty(cbar_area) nii_view.cbar_area = []; else nii_view.cbar_area = cbar_area; end % create axes for top/front/side view % vol_size = voxel_size .* dims; [top_ax, front_ax, side_ax] ... = create_ax(fig, area, vol_size, usestretch); top_pos = get(top_ax,'position'); front_pos = get(front_ax,'position'); side_pos = get(side_ax,'position'); % Sagittal Slider % x = side_pos(1); y = top_pos(2) + top_pos(4); w = side_pos(3); h = (front_pos(2) - y) / 2; y = y + h; pos = [x y w h]; if xdim > 1, slider_step(1) = 1/(xdim); slider_step(2) = 1.00001/(xdim); handles.sagittal_slider = uicontrol('Parent',fig, ... 'Style','slider','Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment','center',... 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],... 'BusyAction','queue',... 'TooltipString','Sagittal slice navigation',... 'Min',1,'Max',xdim,'SliderStep',slider_step, ... 'Value',nii_view.slices.sag,... 'Callback','view_nii(''sagittal_slider'');'); set(handles.sagittal_slider,'position',pos); % linux66 end % Coronal Slider % x = top_pos(1); y = top_pos(2) + top_pos(4); w = top_pos(3); h = (front_pos(2) - y) / 2; y = y + h; pos = [x y w h]; if ydim > 1, slider_step(1) = 1/(ydim); slider_step(2) = 1.00001/(ydim); slider_val = nii_view.dims(2) - nii_view.slices.cor + 1; handles.coronal_slider = uicontrol('Parent',fig, ... 'Style','slider','Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment','center',... 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],... 'BusyAction','queue',... 'TooltipString','Coronal slice navigation',... 'Min',1,'Max',ydim,'SliderStep',slider_step, ... 'Value',slider_val,... 'Callback','view_nii(''coronal_slider'');'); set(handles.coronal_slider,'position',pos); % linux66 end % Axial Slider % % x = front_pos(1) + front_pos(3); % y = front_pos(2); % w = side_pos(1) - x; % h = front_pos(4); x = top_pos(1); y = area(2); w = top_pos(3); h = top_pos(2) - y; pos = [x y w h]; if zdim > 1, slider_step(1) = 1/(zdim); slider_step(2) = 1.00001/(zdim); handles.axial_slider = uicontrol('Parent',fig, ... 'Style','slider','Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment','center',... 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],... 'BusyAction','queue',... 'TooltipString','Axial slice navigation',... 'Min',1,'Max',zdim,'SliderStep',slider_step, ... 'Value',nii_view.slices.axi,... 'Callback','view_nii(''axial_slider'');'); set(handles.axial_slider,'position',pos); % linux66 end % plot info view % % info_pos = [side_pos([1,3]); top_pos([2,4])]; % info_pos = info_pos(:); gap = side_pos(1)-(top_pos(1)+top_pos(3)); info_pos(1) = side_pos(1) + gap; info_pos(2) = area(2); info_pos(3) = side_pos(3) - gap; info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap; num_inputline = 10; inputline_space =info_pos(4) / num_inputline; % for any info_area change, update_usestretch should also be changed % Image Intensity Value at Cursor % x = info_pos(1); y = info_pos(2); w = info_pos(3)*0.5; h = inputline_space*0.6; pos = [x y w h]; handles.Timvalcur = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Value at cursor:'); if usepanel set(handles.Timvalcur, 'visible', 'on'); end x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; handles.imvalcur = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'right',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String',' '); if usepanel set(handles.imvalcur, 'visible', 'on'); end % Position at Cursor % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; handles.Timposcur = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','[X Y Z] at cursor:'); if usepanel set(handles.Timposcur, 'visible', 'on'); end x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; handles.imposcur = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'right',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String',' ','Value',[0 0 0]); if usepanel set(handles.imposcur, 'visible', 'on'); end % Image Intensity Value at Mouse Click % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; handles.Timval = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Value at crosshair:'); if usepanel set(handles.Timval, 'visible', 'on'); end x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; handles.imval = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'right',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String',' '); if usepanel set(handles.imval, 'visible', 'on'); end % Viewpoint Position at Mouse Click % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; handles.Timpos = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','[X Y Z] at crosshair:'); if usepanel set(handles.Timpos, 'visible', 'on'); end x = x + w + 0.005; y = y - 0.008; w = info_pos(3)*0.5; h = inputline_space*0.9; pos = [x y w h]; handles.impos = uicontrol('Parent',fig,'Style','edit', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'right',... 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'Callback','view_nii(''impos_edit'');', ... 'TooltipString','Viewpoint Location in Axes Unit', ... 'visible','off', ... 'String',' ','Value',[0 0 0]); if usepanel set(handles.impos, 'visible', 'on'); end % Origin Position % x = info_pos(1); y = y + inputline_space*1.2; w = info_pos(3)*0.5; h = inputline_space*0.6; pos = [x y w h]; handles.Torigin = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','[X Y Z] at origin:'); if usepanel set(handles.Torigin, 'visible', 'on'); end x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; handles.origin = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'right',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String',' ','Value',[0 0 0]); if usepanel set(handles.origin, 'visible', 'on'); end if 0 % Voxel Unit % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; handles.Tcoord = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Axes Unit:'); if usepanel set(handles.Tcoord, 'visible', 'on'); end x = x + w + 0.005; w = info_pos(3)*0.5 - 0.005; pos = [x y w h]; Font.FontSize = 8; handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ... 'Units','Normalized', Font, ... 'Position',pos, ... 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'TooltipString','Choose Voxel or Millimeter',... 'String',{'Voxel','Millimeter'},... 'visible','off', ... 'Callback','view_nii(''coordinates'');'); % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',... % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},... Font.FontSize = 12; if usepanel set(handles.coord, 'visible', 'on'); end end % Crosshair % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.4; pos = [x y w h]; handles.Txhair = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Crosshair:'); if usepanel set(handles.Txhair, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.2; h = inputline_space*0.7; pos = [x y w h]; Font.FontSize = 8; handles.xhair_color = uicontrol('Parent',fig,'Style','push', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Crosshair Color',... 'User',[1 0 0],... 'String','Color',... 'visible','off', ... 'Callback','view_nii(''xhair_color'');'); if usepanel set(handles.xhair_color, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.7; w = info_pos(3)*0.3; pos = [x y w h]; handles.xhair = uicontrol('Parent',fig,'Style','popupmenu', ... 'Units','Normalized', Font, ... 'Position',pos, ... 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'TooltipString','Display or Hide Crosshair',... 'String',{'On','Off'},... 'visible','off', ... 'Callback','view_nii(''crosshair'');'); if usepanel set(handles.xhair, 'visible', 'on'); end % Histogram & Color % x = info_pos(1); w = info_pos(3)*0.45; h = inputline_space * 1.5; pos = [x, y+inputline_space*0.9, w, h]; handles.hist_frame = uicontrol('Parent',fig, ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'Position',pos, ... 'visible','off', ... 'Style','frame'); if usepanel % set(handles.hist_frame, 'visible', 'on'); end handles.coord_frame = uicontrol('Parent',fig, ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'Position',pos, ... 'visible','off', ... 'Style','frame'); if usepanel set(handles.coord_frame, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.475; w = info_pos(3)*0.525; h = inputline_space * 1.5; pos = [x, y+inputline_space*0.9, w, h]; handles.color_frame = uicontrol('Parent',fig, ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'Position',pos, ... 'visible','off', ... 'Style','frame'); if usepanel set(handles.color_frame, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space*1.2; w = info_pos(3)*0.2; h = inputline_space*0.7; pos = [x y w h]; Font.FontSize = 8; handles.hist_eq = uicontrol('Parent',fig,'Style','toggle', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Histogram Equalization',... 'String','Hist EQ',... 'visible','off', ... 'Callback','view_nii(''hist_eq'');'); if usepanel % set(handles.hist_eq, 'visible', 'on'); end x = x + w; w = info_pos(3)*0.2; pos = [x y w h]; handles.hist_plot = uicontrol('Parent',fig,'Style','push', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Histogram Plot',... 'String','Hist Plot',... 'visible','off', ... 'Callback','view_nii(''hist_plot'');'); if usepanel % set(handles.hist_plot, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.025; w = info_pos(3)*0.4; pos = [x y w h]; handles.coord = uicontrol('Parent',fig,'Style','popupmenu', ... 'Units','Normalized', Font, ... 'Position',pos, ... 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'TooltipString','Choose Voxel or Millimeter',... 'String',{'Voxel','Millimeter'},... 'visible','off', ... 'Callback','view_nii(''coordinates'');'); % 'TooltipString','Choose Voxel, MNI or Talairach Coordinates',... % 'String',{'Voxel','MNI (mm)','Talairach (mm)'},... if usepanel set(handles.coord, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.2; pos = [x y w h]; handles.neg_color = uicontrol('Parent',fig,'Style','toggle', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Negative Colormap',... 'String','Negative',... 'visible','off', ... 'Callback','view_nii(''neg_color'');'); if usepanel set(handles.neg_color, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.neg_color, 'enable', 'off'); end x = info_pos(1) + info_pos(3)*0.7; w = info_pos(3)*0.275; pos = [x y w h]; handles.colorindex = uicontrol('Parent',fig,'Style','popupmenu', ... 'Units','Normalized', Font, ... 'Position',pos, ... 'BackgroundColor', [1 1 1], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'TooltipString','Change Colormap',... 'String',{'Custom','Bipolar','Gray','Jet','Cool','Bone','Hot','Copper','Pink'},... 'value', colorindex, ... 'visible','off', ... 'Callback','view_nii(''color'');'); if usepanel set(handles.colorindex, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.colorindex, 'enable', 'off'); end x = info_pos(1) + info_pos(3)*0.1; y = y + inputline_space; w = info_pos(3)*0.28; h = inputline_space*0.6; pos = [x y w h]; Font.FontSize = 8; handles.Thist = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Histogram'); handles.Tcoord = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Axes Unit'); if usepanel % set(handles.Thist, 'visible', 'on'); set(handles.Tcoord, 'visible', 'on'); end x = info_pos(1) + info_pos(3)*0.60; w = info_pos(3)*0.28; pos = [x y w h]; handles.Tcolor = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Colormap'); if usepanel set(handles.Tcolor, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.Tcolor, 'enable', 'off'); end % Contrast Frame % x = info_pos(1); w = info_pos(3)*0.45; h = inputline_space * 2; pos = [x, y+inputline_space*0.8, w, h]; handles.contrast_frame = uicontrol('Parent',fig, ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'Position',pos, ... 'visible','off', ... 'Style','frame'); if usepanel set(handles.contrast_frame, 'visible', 'on'); end if colorindex < 2 | colorindex > 3 set(handles.contrast_frame, 'visible', 'off'); end % Brightness Frame % x = info_pos(1) + info_pos(3)*0.475; w = info_pos(3)*0.525; pos = [x, y+inputline_space*0.8, w, h]; handles.brightness_frame = uicontrol('Parent',fig, ... 'Units','normal', ... 'BackgroundColor',[0.8 0.8 0.8], ... 'Position',pos, ... 'visible','off', ... 'Style','frame'); if usepanel set(handles.brightness_frame, 'visible', 'on'); end % Contrast % x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space; w = info_pos(3)*0.4; h = inputline_space*0.6; pos = [x y w h]; Font.FontSize = 12; slider_step(1) = 5/255; slider_step(2) = 5.00001/255; handles.contrast = uicontrol('Parent',fig, ... 'Style','slider','Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],... 'BusyAction','queue',... 'TooltipString','Change contrast',... 'Min',1,'Max',256,'SliderStep',slider_step, ... 'Value',1, ... 'visible','off', ... 'Callback','view_nii(''contrast'');'); if usepanel set(handles.contrast, 'visible', 'on'); end if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1 set(handles.contrast, 'enable', 'off'); end if nii_view.numscan > 1 set(handles.contrast, 'min', 1, 'max', nii_view.numscan, ... 'sliderstep',[1/(nii_view.numscan-1) 1.00001/(nii_view.numscan-1)], ... 'Callback', 'view_nii(''slider_change_scan'');'); elseif colorindex < 2 | colorindex > 3 set(handles.contrast, 'visible', 'off'); elseif colorindex == 2 set(handles.contrast,'value',128); end set(handles.contrast,'position',pos); % linux66 % Brightness % x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.475; pos = [x y w h]; Font.FontSize = 12; slider_step(1) = 1/50; slider_step(2) = 1.00001/50; handles.brightness = uicontrol('Parent',fig, ... 'Style','slider','Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor',[0.5 0.5 0.5],'ForegroundColor',[0 0 0],... 'BusyAction','queue',... 'TooltipString','Change brightness',... 'Min',-1,'Max',1,'SliderStep',slider_step, ... 'Value',0, ... 'visible','off', ... 'Callback','view_nii(''brightness'');'); if usepanel set(handles.brightness, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.brightness, 'enable', 'off'); end set(handles.brightness,'position',pos); % linux66 % Contrast text/def % x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space; w = info_pos(3)*0.22; pos = [x y w h]; handles.Tcontrast = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Contrast:'); if usepanel set(handles.Tcontrast, 'visible', 'on'); end if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1 set(handles.Tcontrast, 'enable', 'off'); end if nii_view.numscan > 1 set(handles.Tcontrast, 'string', 'Scan ID:'); set(handles.contrast, 'TooltipString', 'Change Scan ID'); elseif colorindex < 2 | colorindex > 3 set(handles.Tcontrast, 'visible', 'off'); end x = x + w; w = info_pos(3)*0.18; pos = [x y w h]; Font.FontSize = 8; handles.contrast_def = uicontrol('Parent',fig,'Style','push', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Restore initial contrast',... 'String','Reset',... 'visible','off', ... 'Callback','view_nii(''contrast_def'');'); if usepanel set(handles.contrast_def, 'visible', 'on'); end if (nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511) & nii_view.numscan <= 1 set(handles.contrast_def, 'enable', 'off'); end if nii_view.numscan > 1 set(handles.contrast_def, 'style', 'edit', 'background', 'w', ... 'TooltipString','Scan (or volume) index in the time series',... 'string', '1', 'Callback', 'view_nii(''edit_change_scan'');'); elseif colorindex < 2 | colorindex > 3 set(handles.contrast_def, 'visible', 'off'); end % Brightness text/def % x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.295; pos = [x y w h]; Font.FontSize = 12; handles.Tbrightness = uicontrol('Parent',fig,'Style','text', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'left',... 'BackgroundColor', [0.8 0.8 0.8], 'ForegroundColor', [0 0 0],... 'BusyAction','queue',... 'visible','off', ... 'String','Brightness:'); if usepanel set(handles.Tbrightness, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.Tbrightness, 'enable', 'off'); end x = x + w; w = info_pos(3)*0.18; pos = [x y w h]; Font.FontSize = 8; handles.brightness_def = uicontrol('Parent',fig,'Style','push', ... 'Units','Normalized', Font, ... 'Position',pos, 'HorizontalAlignment', 'center',... 'TooltipString','Restore initial brightness',... 'String','Reset',... 'visible','off', ... 'Callback','view_nii(''brightness_def'');'); if usepanel set(handles.brightness_def, 'visible', 'on'); end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(handles.brightness_def, 'enable', 'off'); end % init image handles % handles.axial_image = []; handles.coronal_image = []; handles.sagittal_image = []; % plot axial view % if ~isempty(nii_view.bgimg) bg_slice = squeeze(bgimg(:,:,nii_view.slices.axi)); h1 = plot_view(fig, xdim, ydim, top_ax, bg_slice', clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); handles.axial_bg = h1; else handles.axial_bg = []; end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,:,setscanid)); img_slice = permute(img_slice, [2 1 3]); else img_slice = squeeze(nii.img(:,:,nii_view.slices.axi,setscanid)); img_slice = img_slice'; end h1 = plot_view(fig, xdim, ydim, top_ax, img_slice, clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); set(h1,'buttondown','view_nii(''axial_image'');'); handles.axial_image = h1; handles.axial_axes = top_ax; if size(img_slice,1) == 1 | size(img_slice,2) == 1 set(top_ax,'visible','off'); if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider) set(handles.sagittal_slider, 'visible', 'off'); end if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider) set(handles.coronal_slider, 'visible', 'off'); end if isfield(handles,'axial_slider') & ishandle(handles.axial_slider) set(handles.axial_slider, 'visible', 'off'); end end % plot coronal view % if ~isempty(nii_view.bgimg) bg_slice = squeeze(bgimg(:,nii_view.slices.cor,:)); h1 = plot_view(fig, xdim, zdim, front_ax, bg_slice', clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); handles.coronal_bg = h1; else handles.coronal_bg = []; end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,:,setscanid)); img_slice = permute(img_slice, [2 1 3]); else img_slice = squeeze(nii.img(:,nii_view.slices.cor,:,setscanid)); img_slice = img_slice'; end h1 = plot_view(fig, xdim, zdim, front_ax, img_slice, clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); set(h1,'buttondown','view_nii(''coronal_image'');'); handles.coronal_image = h1; handles.coronal_axes = front_ax; if size(img_slice,1) == 1 | size(img_slice,2) == 1 set(front_ax,'visible','off'); if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider) set(handles.sagittal_slider, 'visible', 'off'); end if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider) set(handles.coronal_slider, 'visible', 'off'); end if isfield(handles,'axial_slider') & ishandle(handles.axial_slider) set(handles.axial_slider, 'visible', 'off'); end end % plot sagittal view % if ~isempty(nii_view.bgimg) bg_slice = squeeze(bgimg(nii_view.slices.sag,:,:)); h1 = plot_view(fig, ydim, zdim, side_ax, bg_slice', clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); handles.sagittal_bg = h1; else handles.sagittal_bg = []; end if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,:,setscanid)); img_slice = permute(img_slice, [2 1 3]); else img_slice = squeeze(nii.img(nii_view.slices.sag,:,:,setscanid)); img_slice = img_slice'; end h1 = plot_view(fig, ydim, zdim, side_ax, img_slice, clim, cbarminmax, ... handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, nii_view.numscan); set(h1,'buttondown','view_nii(''sagittal_image'');'); set(side_ax,'Xdir', 'reverse'); handles.sagittal_image = h1; handles.sagittal_axes = side_ax; if size(img_slice,1) == 1 | size(img_slice,2) == 1 set(side_ax,'visible','off'); if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider) set(handles.sagittal_slider, 'visible', 'off'); end if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider) set(handles.coronal_slider, 'visible', 'off'); end if isfield(handles,'axial_slider') & ishandle(handles.axial_slider) set(handles.axial_slider, 'visible', 'off'); end end [top1_label, top2_label, side1_label, side2_label] = ... dir_label(fig, top_ax, front_ax, side_ax); % store label handles % handles.top1_label = top1_label; handles.top2_label = top2_label; handles.side1_label = side1_label; handles.side2_label = side2_label; % plot colorbar % if ~isempty(cbar_axes) & ~isempty(cbarminmax_axes) if 0 if isempty(color_map) level = colorlevel + num_highcolor; else level = size([color_map; highcolor], 1); end end if isempty(color_map) level = colorlevel; else level = size([color_map], 1); end niiclass = class(nii.img); h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ... level, handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, niiclass, nii_view.numscan); handles.cbar_image = h1; handles.cbar_axes = cbar_axes; handles.cbarminmax_axes = cbarminmax_axes; end nii_view.handles = handles; % store handles nii_view.usepanel = usepanel; % whole panel at low right cornor nii_view.usestretch = usestretch; % stretch display of voxel_size nii_view.useinterp = useinterp; % use interpolation nii_view.colorindex = colorindex; % store colorindex variable nii_view.buttondown = buttondown; % command after button down click nii_view.cbarminmax = cbarminmax; % store min max value for colorbar set_coordinates(nii_view,useinterp); % coord unit if ~isfield(nii_view, 'axi_xhair') | ... ~isfield(nii_view, 'cor_xhair') | ... ~isfield(nii_view, 'sag_xhair') nii_view.axi_xhair = []; % top cross hair nii_view.cor_xhair = []; % front cross hair nii_view.sag_xhair = []; % side cross hair end if ~isempty(color_map) nii_view.color_map = color_map; end if ~isempty(colorlevel) nii_view.colorlevel = colorlevel; end if ~isempty(highcolor) nii_view.highcolor = highcolor; end update_nii_view(nii_view); if ~isempty(setunit) update_unit(fig, setunit); end if ~isempty(setviewpoint) update_viewpoint(fig, setviewpoint); end if ~isempty(setcrosshaircolor) update_crosshaircolor(fig, setcrosshaircolor); end if ~isempty(usecrosshair) update_usecrosshair(fig, usecrosshair); end nii_menu = getappdata(fig, 'nii_menu'); if ~isempty(nii_menu) if nii.hdr.dime.datatype == 128 | nii.hdr.dime.datatype == 511 set(nii_menu.Minterp,'Userdata',1,'Label','Interp on','enable','off'); elseif useinterp set(nii_menu.Minterp,'Userdata',0,'Label','Interp off'); else set(nii_menu.Minterp,'Userdata',1,'Label','Interp on'); end end windowbuttonmotion = get(fig, 'windowbuttonmotion'); windowbuttonmotion = [windowbuttonmotion '; view_nii(''move_cursor'');']; set(fig, 'windowbuttonmotion', windowbuttonmotion); return; % init %---------------------------------------------------------------- function fig = update_img(img, fig, opt) nii_menu = getappdata(fig,'nii_menu'); if ~isempty(nii_menu) set(nii_menu.Mzoom,'Userdata',1,'Label','Zoom on'); set(fig,'pointer','arrow'); zoom off; end nii_view = getappdata(fig,'nii_view'); change_interp = 0; if isfield(opt, 'useinterp') & opt.useinterp ~= nii_view.useinterp nii_view.useinterp = opt.useinterp; change_interp = 1; end setscanid = 1; if isfield(opt, 'setscanid') setscanid = round(opt.setscanid); if setscanid < 1 setscanid = 1; end if setscanid > nii_view.numscan setscanid = nii_view.numscan; end end if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax) minvalue = opt.glblocminmax(1); maxvalue = opt.glblocminmax(2); else minvalue = img(:,:,:,setscanid); minvalue = double(minvalue(:)); minvalue = min(minvalue(~isnan(minvalue))); maxvalue = img(:,:,:,setscanid); maxvalue = double(maxvalue(:)); maxvalue = max(maxvalue(~isnan(maxvalue))); end if isfield(opt, 'setvalue') setvalue = opt.setvalue; if isfield(opt, 'glblocminmax') & ~isempty(opt.glblocminmax) minvalue = opt.glblocminmax(1); maxvalue = opt.glblocminmax(2); else minvalue = double(min(setvalue.val)); maxvalue = double(max(setvalue.val)); end bgimg = double(img); minbg = double(min(bgimg(:))); maxbg = double(max(bgimg(:))); bgimg = scale_in(bgimg, minbg, maxbg, 55) + 200; % scale to 201~256 cbarminmax = [minvalue maxvalue]; if nii_view.useinterp % scale signal data to 1~200 % img = repmat(nan, size(img)); img(setvalue.idx) = setvalue.val; % 200 level for source image % bgimg = single(scale_out(bgimg, cbarminmax(1), cbarminmax(2), 199)); else bgimg(setvalue.idx) = NaN; minbg = double(min(bgimg(:))); maxbg = double(max(bgimg(:))); bgimg(setvalue.idx) = minbg; % bgimg must be normalized to [201 256] % bgimg = 55 * (bgimg-min(bgimg(:))) / (max(bgimg(:))-min(bgimg(:))) + 201; bgimg(setvalue.idx) = 0; % scale signal data to 1~200 % img = zeros(size(img)); img(setvalue.idx) = scale_in(setvalue.val, minvalue, maxvalue, 199); img = img + bgimg; bgimg = []; img = scale_out(img, cbarminmax(1), cbarminmax(2), 199); minvalue = double(min(img(:))); maxvalue = double(max(img(:))); if isfield(opt,'glblocminmax') & ~isempty(opt.glblocminmax) minvalue = opt.glblocminmax(1); end end nii_view.bgimg = bgimg; nii_view.setvalue = setvalue; else cbarminmax = [minvalue maxvalue]; end update_cbarminmax(fig, cbarminmax); nii_view.cbarminmax = cbarminmax; nii_view.nii.img = img; nii_view.minvalue = minvalue; nii_view.maxvalue = maxvalue; nii_view.scanid = setscanid; change_colormap(fig); % init color (gray) scaling to make sure the slice clim take the % global clim [min(nii.img(:)) max(nii.img(:))] % if isempty(nii_view.bgimg) clim = [minvalue maxvalue]; else clim = [minvalue double(max(nii_view.bgimg(:)))]; end if clim(1) == clim(2) clim(2) = clim(1) + 0.000001; end if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct') useimagesc = 0; else useimagesc = 1; end if ~isempty(nii_view.bgimg) % with interpolation Saxi = squeeze(nii_view.bgimg(:,:,nii_view.slices.axi)); if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) set(nii_view.handles.axial_bg,'CData',double(Saxi)'); else axes(nii_view.handles.axial_axes); if useimagesc nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'edgecolor','none','facecolor','interp'); else nii_view.handles.axial_bg = surface(zeros(size(Saxi')),double(Saxi'),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end order = get(gca,'child'); order(find(order == nii_view.handles.axial_bg)) = []; order = [order; nii_view.handles.axial_bg]; set(gca, 'child', order); end end if isfield(nii_view.handles,'axial_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,:,setscanid)); Saxi = permute(Saxi, [2 1 3]); else Saxi = squeeze(nii_view.nii.img(:,:,nii_view.slices.axi,setscanid)); Saxi = Saxi'; end set(nii_view.handles.axial_image,'CData',double(Saxi)); end set(nii_view.handles.axial_axes,'CLim',clim); if ~isempty(nii_view.bgimg) Scor = squeeze(nii_view.bgimg(:,nii_view.slices.cor,:)); if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) set(nii_view.handles.coronal_bg,'CData',double(Scor)'); else axes(nii_view.handles.coronal_axes); if useimagesc nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'edgecolor','none','facecolor','interp'); else nii_view.handles.coronal_bg = surface(zeros(size(Scor')),double(Scor'),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end order = get(gca,'child'); order(find(order == nii_view.handles.coronal_bg)) = []; order = [order; nii_view.handles.coronal_bg]; set(gca, 'child', order); end end if isfield(nii_view.handles,'coronal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,:,setscanid)); Scor = permute(Scor, [2 1 3]); else Scor = squeeze(nii_view.nii.img(:,nii_view.slices.cor,:,setscanid)); Scor = Scor'; end set(nii_view.handles.coronal_image,'CData',double(Scor)); end set(nii_view.handles.coronal_axes,'CLim',clim); if ~isempty(nii_view.bgimg) Ssag = squeeze(nii_view.bgimg(nii_view.slices.sag,:,:)); if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) set(nii_view.handles.sagittal_bg,'CData',double(Ssag)'); else axes(nii_view.handles.sagittal_axes); if useimagesc nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'edgecolor','none','facecolor','interp'); else nii_view.handles.sagittal_bg = surface(zeros(size(Ssag')),double(Ssag'),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end order = get(gca,'child'); order(find(order == nii_view.handles.sagittal_bg)) = []; order = [order; nii_view.handles.sagittal_bg]; set(gca, 'child', order); end end if isfield(nii_view.handles,'sagittal_image'), if nii_view.nii.hdr.dime.datatype == 128 | nii_view.nii.hdr.dime.datatype == 511 Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,:,setscanid)); Ssag = permute(Ssag, [2 1 3]); else Ssag = squeeze(nii_view.nii.img(nii_view.slices.sag,:,:,setscanid)); Ssag = Ssag'; end set(nii_view.handles.sagittal_image,'CData',double(Ssag)); end set(nii_view.handles.sagittal_axes,'CLim',clim); update_nii_view(nii_view); if isfield(opt, 'setvalue') if ~isfield(nii_view,'highcolor') | ~isequal(size(nii_view.highcolor),[56 3]) % 55 level for brain structure (paded 0 for highcolor level 1, i.e. normal level 201, to make 56 highcolor) % update_highcolor(fig, [zeros(1,3);gray(55)], []); end if nii_view.colorindex ~= 2 update_colorindex(fig, 2); end old_color = get(nii_view.handles.xhair_color,'user'); if isequal(old_color, [1 0 0]) update_crosshaircolor(fig, [1 1 0]); end % if change_interp % update_useinterp(fig, nii_view.useinterp); % end end if change_interp update_useinterp(fig, nii_view.useinterp); end return; % update_img %---------------------------------------------------------------- function [top_pos, front_pos, side_pos] = ... axes_pos(fig,area,vol_size,usestretch) set(fig,'unit','pixel'); fig_pos = get(fig,'position'); gap_x = 15/fig_pos(3); % width of vertical scrollbar gap_y = 15/fig_pos(4); % width of horizontal scrollbar a = (area(3) - gap_x * 1.3) * fig_pos(3) / (vol_size(1) + vol_size(2)); % no crosshair lost in zoom b = (area(4) - gap_y * 3) * fig_pos(4) / (vol_size(2) + vol_size(3)); c = min([a b]); % make sure 'ax' is inside 'area' top_w = vol_size(1) * c / fig_pos(3); side_w = vol_size(2) * c / fig_pos(3); top_h = vol_size(2) * c / fig_pos(4); side_h = vol_size(3) * c / fig_pos(4); side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom side_y = area(2) + top_h + gap_y * 3; if usestretch if a > b % top touched ceiling, use b d = (area(3) - gap_x * 1.3) / (top_w + side_w); % no crosshair lost in zoom top_w = top_w * d; side_w = side_w * d; side_x = area(1) + top_w + gap_x * 1.3; % no crosshair lost in zoom else d = (area(4) - gap_y * 3) / (top_h + side_h); top_h = top_h * d; side_h = side_h * d; side_y = area(2) + top_h + gap_y * 3; end end top_pos = [area(1) area(2)+gap_y top_w top_h]; front_pos = [area(1) side_y top_w side_h]; side_pos = [side_x side_y side_w side_h]; set(fig,'unit','normal'); return; % axes_pos %---------------------------------------------------------------- function [top_ax, front_ax, side_ax] ... = create_ax(fig, area, vol_size, usestretch) cur_fig = gcf; % save h_wait fig figure(fig); [top_pos, front_pos, side_pos] = ... axes_pos(fig,area,vol_size,usestretch); nii_view = getappdata(fig, 'nii_view'); if isempty(nii_view) top_ax = axes('position', top_pos); front_ax = axes('position', front_pos); side_ax = axes('position', side_pos); else top_ax = nii_view.handles.axial_axes; front_ax = nii_view.handles.coronal_axes; side_ax = nii_view.handles.sagittal_axes; set(top_ax, 'position', top_pos); set(front_ax, 'position', front_pos); set(side_ax, 'position', side_pos); end figure(cur_fig); return; % create_ax %---------------------------------------------------------------- function [cbar_axes, cbarminmax_axes] = create_cbar_axes(fig, cbar_area, nii_view) if isempty(cbar_area) % without_cbar cbar_axes = []; cbarminmax_axes = []; return; end cur_fig = gcf; % save h_wait fig figure(fig); if ~exist('nii_view', 'var') nii_view = getappdata(fig, 'nii_view'); end if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_axes') | isempty(nii_view.handles.cbar_axes) cbarminmax_axes = axes('position', cbar_area); cbar_axes = axes('position', cbar_area); else cbarminmax_axes = nii_view.handles.cbarminmax_axes; cbar_axes = nii_view.handles.cbar_axes; set(cbarminmax_axes, 'position', cbar_area); set(cbar_axes, 'position', cbar_area); end figure(cur_fig); return; % create_cbar_axes %---------------------------------------------------------------- function h1 = plot_view(fig, x, y, img_ax, img_slice, clim, ... cbarminmax, handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, useinterp, numscan) h1 = []; if x > 1 & y > 1, axes(img_ax); nii_view = getappdata(fig, 'nii_view'); if isempty(nii_view) % set colormap first % nii.handles = handles; nii.handles.axial_axes = img_ax; nii.colorindex = colorindex; nii.color_map = color_map; nii.colorlevel = colorlevel; nii.highcolor = highcolor; nii.numscan = numscan; change_colormap(fig, nii, colorindex, cbarminmax); if useinterp if useimagesc h1 = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp'); else h1 = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end set(gca,'clim',clim); else if useimagesc h1 = imagesc(img_slice,clim); else h1 = image(img_slice); end set(gca,'clim',clim); end else h1 = nii_view.handles.axial_image; if ~isequal(get(h1,'parent'), img_ax) h1 = nii_view.handles.coronal_image; end if ~isequal(get(h1,'parent'), img_ax) h1 = nii_view.handles.sagittal_image; end set(h1, 'cdata', double(img_slice)); set(h1, 'xdata', 1:size(img_slice,2)); set(h1, 'ydata', 1:size(img_slice,1)); end set(img_ax,'YDir','normal','XLimMode','manual','YLimMode','manual',... 'ClimMode','manual','visible','off', ... 'xtick',[],'ytick',[], 'clim', clim); end return; % plot_view %---------------------------------------------------------------- function h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, cbarminmax, ... level, handles, useimagesc, colorindex, color_map, ... colorlevel, highcolor, niiclass, numscan, nii_view) cbar_image = [1:level]'; % In a uint8 or uint16 indexed image, 0 points to the first row % in the colormap % if 0 % strcmpi(niiclass,'uint8') | strcmpi(niiclass,'uint16') % we use single for display anyway ylim = [0, level-1]; else ylim = [1, level]; end axes(cbarminmax_axes); plot([0 0], cbarminmax, 'w'); axis tight; set(cbarminmax_axes,'YDir','normal', ... 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ... 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right'); ylimb = get(cbarminmax_axes,'ylim'); ytickb = get(cbarminmax_axes,'ytick'); ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1); axes(cbar_axes); if ~exist('nii_view', 'var') nii_view = getappdata(fig, 'nii_view'); end if isempty(nii_view) | ~isfield(nii_view.handles,'cbar_image') | isempty(nii_view.handles.cbar_image) % set colormap first % nii.handles = handles; nii.colorindex = colorindex; nii.color_map = color_map; nii.colorlevel = colorlevel; nii.highcolor = highcolor; nii.numscan = numscan; change_colormap(fig, nii, colorindex, cbarminmax); h1 = image([0,1], [ylim(1),ylim(2)], cbar_image); else h1 = nii_view.handles.cbar_image; set(h1, 'cdata', double(cbar_image)); end set(cbar_axes,'YDir','normal','XLimMode','manual', ... 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ... 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel',''); return; % plot_cbar %---------------------------------------------------------------- function set_coordinates(nii_view,useinterp) imgPlim.vox = nii_view.dims; imgNlim.vox = [1 1 1]; if useinterp xdata_ax = [imgNlim.vox(1) imgPlim.vox(1)]; ydata_ax = [imgNlim.vox(2) imgPlim.vox(2)]; zdata_ax = [imgNlim.vox(3) imgPlim.vox(3)]; else xdata_ax = [imgNlim.vox(1)-0.5 imgPlim.vox(1)+0.5]; ydata_ax = [imgNlim.vox(2)-0.5 imgPlim.vox(2)+0.5]; zdata_ax = [imgNlim.vox(3)-0.5 imgPlim.vox(3)+0.5]; end if isfield(nii_view.handles,'axial_image') & ~isempty(nii_view.handles.axial_image) set(nii_view.handles.axial_axes,'Xlim',xdata_ax); set(nii_view.handles.axial_axes,'Ylim',ydata_ax); end; if isfield(nii_view.handles,'coronal_image') & ~isempty(nii_view.handles.coronal_image) set(nii_view.handles.coronal_axes,'Xlim',xdata_ax); set(nii_view.handles.coronal_axes,'Ylim',zdata_ax); end; if isfield(nii_view.handles,'sagittal_image') & ~isempty(nii_view.handles.sagittal_image) set(nii_view.handles.sagittal_axes,'Xlim',ydata_ax); set(nii_view.handles.sagittal_axes,'Ylim',zdata_ax); end; return % set_coordinates %---------------------------------------------------------------- function set_image_value(nii_view), % get coordinates of selected voxel and the image intensity there % sag = round(nii_view.slices.sag); cor = round(nii_view.slices.cor); axi = round(nii_view.slices.axi); if 0 % isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end if nii_view.nii.hdr.dime.datatype == 128 imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))]; set(nii_view.handles.imval,'Value',imgvalue); set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue)); elseif nii_view.nii.hdr.dime.datatype == 511 R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))]; set(nii_view.handles.imval,'Value',imgvalue); imgvalue = [R G B]; set(nii_view.handles.imval,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue)); else imgvalue = double(img(sag,cor,axi,nii_view.scanid)); set(nii_view.handles.imval,'Value',imgvalue); if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2) imgvalue = 0; end set(nii_view.handles.imval,'String',sprintf('%.6g',imgvalue)); end % Now update the coordinates of the selected voxel nii_view = update_imgXYZ(nii_view); if get(nii_view.handles.coord,'value') == 1, sag = nii_view.imgXYZ.vox(1); cor = nii_view.imgXYZ.vox(2); axi = nii_view.imgXYZ.vox(3); org = nii_view.origin; elseif get(nii_view.handles.coord,'value') == 2, sag = nii_view.imgXYZ.mm(1); cor = nii_view.imgXYZ.mm(2); axi = nii_view.imgXYZ.mm(3); org = [0 0 0]; elseif get(nii_view.handles.coord,'value') == 3, sag = nii_view.imgXYZ.tal(1); cor = nii_view.imgXYZ.tal(2); axi = nii_view.imgXYZ.tal(3); org = [0 0 0]; end set(nii_view.handles.impos,'Value',[sag,cor,axi]); if get(nii_view.handles.coord,'value') == 1, string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi); org_str = sprintf('%7.0f %7.0f %7.0f', org(1), org(2), org(3)); else string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi); org_str = sprintf('%7.1f %7.1f %7.1f', org(1), org(2), org(3)); end; set(nii_view.handles.impos,'String',string); set(nii_view.handles.origin, 'string', org_str); return % set_image_value %---------------------------------------------------------------- function nii_view = get_slice_position(nii_view,view), % obtain slices that is in correct unit, then update slices % slices = nii_view.slices; switch view, case 'sag', currentpoint = get(nii_view.handles.sagittal_axes,'CurrentPoint'); slices.cor = currentpoint(1,1); slices.axi = currentpoint(1,2); case 'cor', currentpoint = get(nii_view.handles.coronal_axes,'CurrentPoint'); slices.sag = currentpoint(1,1); slices.axi = currentpoint(1,2); case 'axi', currentpoint = get(nii_view.handles.axial_axes,'CurrentPoint'); slices.sag = currentpoint(1,1); slices.cor = currentpoint(1,2); end % update nii_view.slices with the updated slices % nii_view.slices.axi = round(slices.axi); nii_view.slices.cor = round(slices.cor); nii_view.slices.sag = round(slices.sag); return % get_slice_position %---------------------------------------------------------------- function nii_view = get_slider_position(nii_view), [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi] = deal(0); if isfield(nii_view.handles,'sagittal_slider'), if ishandle(nii_view.handles.sagittal_slider), nii_view.slices.sag = ... round(get(nii_view.handles.sagittal_slider,'Value')); end end if isfield(nii_view.handles,'coronal_slider'), if ishandle(nii_view.handles.coronal_slider), nii_view.slices.cor = ... round(nii_view.dims(2) - ... get(nii_view.handles.coronal_slider,'Value') + 1); end end if isfield(nii_view.handles,'axial_slider'), if ishandle(nii_view.handles.axial_slider), nii_view.slices.axi = ... round(get(nii_view.handles.axial_slider,'Value')); end end nii_view = check_slices(nii_view); return % get_slider_position %---------------------------------------------------------------- function nii_view = update_imgXYZ(nii_view), nii_view.imgXYZ.vox = ... [nii_view.slices.sag,nii_view.slices.cor,nii_view.slices.axi]; nii_view.imgXYZ.mm = ... (nii_view.imgXYZ.vox - nii_view.origin) .* nii_view.voxel_size; % nii_view.imgXYZ.tal = mni2tal(nii_view.imgXYZ.mni); return % update_imgXYZ %---------------------------------------------------------------- function nii_view = convert2voxel(nii_view,slices), if get(nii_view.handles.coord,'value') == 1, % [slices.axi, slices.cor, slices.sag] are in vox % nii_view.slices.axi = round(slices.axi); nii_view.slices.cor = round(slices.cor); nii_view.slices.sag = round(slices.sag); elseif get(nii_view.handles.coord,'value') == 2, % [slices.axi, slices.cor, slices.sag] are in mm % xpix = nii_view.voxel_size(1); ypix = nii_view.voxel_size(2); zpix = nii_view.voxel_size(3); nii_view.slices.axi = round(slices.axi / zpix + nii_view.origin(3)); nii_view.slices.cor = round(slices.cor / ypix + nii_view.origin(2)); nii_view.slices.sag = round(slices.sag / xpix + nii_view.origin(1)); elseif get(nii_view.handles.coord,'value') == 3, % [slices.axi, slices.cor, slices.sag] are in talairach % xpix = nii_view.voxel_size(1); ypix = nii_view.voxel_size(2); zpix = nii_view.voxel_size(3); xyz_tal = [slices.sag, slices.cor, slices.axi]; xyz_mni = tal2mni(xyz_tal); nii_view.slices.axi = round(xyz_mni(3) / zpix + nii_view.origin(3)); nii_view.slices.cor = round(xyz_mni(2) / ypix + nii_view.origin(2)); nii_view.slices.sag = round(xyz_mni(1) / xpix + nii_view.origin(1)); end return % convert2voxel %---------------------------------------------------------------- function nii_view = check_slices(nii_view), img = nii_view.nii.img; [ SagSize, CorSize, AxiSize, TimeSize ] = size(img); if nii_view.slices.sag > SagSize, nii_view.slices.sag = SagSize; end; if nii_view.slices.sag < 1, nii_view.slices.sag = 1; end; if nii_view.slices.cor > CorSize, nii_view.slices.cor = CorSize; end; if nii_view.slices.cor < 1, nii_view.slices.cor = 1; end; if nii_view.slices.axi > AxiSize, nii_view.slices.axi = AxiSize; end; if nii_view.slices.axi < 1, nii_view.slices.axi = 1; end; if nii_view.scanid > TimeSize, nii_view.scanid = TimeSize; end; if nii_view.scanid < 1, nii_view.scanid = 1; end; return % check_slices %---------------------------------------------------------------- % % keep this function small, since it will be called for every click % function nii_view = update_nii_view(nii_view) % add imgXYZ into nii_view struct % nii_view = check_slices(nii_view); nii_view = update_imgXYZ(nii_view); % update xhair % p_axi = nii_view.imgXYZ.vox([1 2]); p_cor = nii_view.imgXYZ.vox([1 3]); p_sag = nii_view.imgXYZ.vox([2 3]); nii_view.axi_xhair = ... rri_xhair(p_axi, nii_view.axi_xhair, nii_view.handles.axial_axes); nii_view.cor_xhair = ... rri_xhair(p_cor, nii_view.cor_xhair, nii_view.handles.coronal_axes); nii_view.sag_xhair = ... rri_xhair(p_sag, nii_view.sag_xhair, nii_view.handles.sagittal_axes); setappdata(nii_view.fig, 'nii_view', nii_view); set_image_value(nii_view); return; % update_nii_view %---------------------------------------------------------------- function hist_plot(fig) nii_view = getappdata(fig,'nii_view'); if isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end img = double(img(:)); if length(unique(round(img))) == length(unique(img)) is_integer = 1; range = max(img) - min(img) + 1; figure; hist(img, range); set(gca, 'xlim', [-range/5, max(img)]); else is_integer = 0; figure; hist(img); end xlabel('Voxel Intensity'); ylabel('Voxel Numbers for Each Intensity'); set(gcf, 'NumberTitle','off','Name','Histogram Plot'); return; % hist_plot %---------------------------------------------------------------- function hist_eq(fig) nii_view = getappdata(fig,'nii_view'); old_pointer = get(fig,'Pointer'); set(fig,'Pointer','watch'); if get(nii_view.handles.hist_eq,'value') max_img = double(max(nii_view.nii.img(:))); tmp = double(nii_view.nii.img) / max_img; % normalize for histeq tmp = histeq(tmp(:)); nii_view.disp = reshape(tmp, size(nii_view.nii.img)); min_disp = min(nii_view.disp(:)); nii_view.disp = (nii_view.disp - min_disp); % range having eq hist nii_view.disp = nii_view.disp * max_img / max(nii_view.disp(:)); nii_view.disp = single(nii_view.disp); else if isfield(nii_view, 'disp') nii_view.disp = nii_view.nii.img; else set(fig,'Pointer',old_pointer); return; end end % update axial view % img_slice = squeeze(double(nii_view.disp(:,:,nii_view.slices.axi))); h1 = nii_view.handles.axial_image; set(h1, 'cdata', double(img_slice)'); % update coronal view % img_slice = squeeze(double(nii_view.disp(:,nii_view.slices.cor,:))); h1 = nii_view.handles.coronal_image; set(h1, 'cdata', double(img_slice)'); % update sagittal view % img_slice = squeeze(double(nii_view.disp(nii_view.slices.sag,:,:))); h1 = nii_view.handles.sagittal_image; set(h1, 'cdata', double(img_slice)'); % remove disp field if un-check 'histeq' button % if ~get(nii_view.handles.hist_eq,'value') & isfield(nii_view, 'disp') nii_view = rmfield(nii_view, 'disp'); end update_nii_view(nii_view); set(fig,'Pointer',old_pointer); return; % hist_eq %---------------------------------------------------------------- function [top1_label, top2_label, side1_label, side2_label] = ... dir_label(fig, top_ax, front_ax, side_ax) nii_view = getappdata(fig,'nii_view'); top_pos = get(top_ax,'position'); front_pos = get(front_ax,'position'); side_pos = get(side_ax,'position'); top_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*top_pos(3)); top_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*top_pos(4)); side_gap_x = (side_pos(1)-top_pos(1)-top_pos(3)) / (2*side_pos(3)); side_gap_y = (front_pos(2)-top_pos(2)-top_pos(4)) / (2*side_pos(4)); top1_label_pos = [0, 1]; % rot0 top2_label_pos = [1, 0]; % rot90 side1_label_pos = [1, - side_gap_y]; % rot0 side2_label_pos = [0, 0]; % rot90 if isempty(nii_view) axes(top_ax); top1_label = text(double(top1_label_pos(1)),double(top1_label_pos(2)), ... '== X =>', ... 'vertical', 'bottom', ... 'unit', 'normal', 'fontsize', 8); axes(top_ax); top2_label = text(double(top2_label_pos(1)),double(top2_label_pos(2)), ... '== Y =>', ... 'rotation', 90, 'vertical', 'top', ... 'unit', 'normal', 'fontsize', 8); axes(side_ax); side1_label = text(double(side1_label_pos(1)),double(side1_label_pos(2)), ... '<= Y ==', ... 'horizontal', 'right', 'vertical', 'top', ... 'unit', 'normal', 'fontsize', 8); axes(side_ax); side2_label = text(double(side2_label_pos(1)),double(side2_label_pos(2)), ... '== Z =>', ... 'rotation', 90, 'vertical', 'bottom', ... 'unit', 'normal', 'fontsize', 8); else top1_label = nii_view.handles.top1_label; top2_label = nii_view.handles.top2_label; side1_label = nii_view.handles.side1_label; side2_label = nii_view.handles.side2_label; set(top1_label, 'position', [top1_label_pos 0]); set(top2_label, 'position', [top2_label_pos 0]); set(side1_label, 'position', [side1_label_pos 0]); set(side2_label, 'position', [side2_label_pos 0]); end return; % dir_label %---------------------------------------------------------------- function update_enable(h, opt); nii_view = getappdata(h,'nii_view'); handles = nii_view.handles; if isfield(opt,'enablecursormove') if opt.enablecursormove v = 'on'; else v = 'off'; end set(handles.Timposcur, 'visible', v); set(handles.imposcur, 'visible', v); set(handles.Timvalcur, 'visible', v); set(handles.imvalcur, 'visible', v); end if isfield(opt,'enableviewpoint') if opt.enableviewpoint v = 'on'; else v = 'off'; end set(handles.Timpos, 'visible', v); set(handles.impos, 'visible', v); set(handles.Timval, 'visible', v); set(handles.imval, 'visible', v); end if isfield(opt,'enableorigin') if opt.enableorigin v = 'on'; else v = 'off'; end set(handles.Torigin, 'visible', v); set(handles.origin, 'visible', v); end if isfield(opt,'enableunit') if opt.enableunit v = 'on'; else v = 'off'; end set(handles.Tcoord, 'visible', v); set(handles.coord_frame, 'visible', v); set(handles.coord, 'visible', v); end if isfield(opt,'enablecrosshair') if opt.enablecrosshair v = 'on'; else v = 'off'; end set(handles.Txhair, 'visible', v); set(handles.xhair_color, 'visible', v); set(handles.xhair, 'visible', v); end if isfield(opt,'enablehistogram') if opt.enablehistogram v = 'on'; vv = 'off'; else v = 'off'; vv = 'on'; end set(handles.Tcoord, 'visible', vv); set(handles.coord_frame, 'visible', vv); set(handles.coord, 'visible', vv); set(handles.Thist, 'visible', v); set(handles.hist_frame, 'visible', v); set(handles.hist_eq, 'visible', v); set(handles.hist_plot, 'visible', v); end if isfield(opt,'enablecolormap') if opt.enablecolormap v = 'on'; else v = 'off'; end set(handles.Tcolor, 'visible', v); set(handles.color_frame, 'visible', v); set(handles.neg_color, 'visible', v); set(handles.colorindex, 'visible', v); end if isfield(opt,'enablecontrast') if opt.enablecontrast v = 'on'; else v = 'off'; end set(handles.Tcontrast, 'visible', v); set(handles.contrast_frame, 'visible', v); set(handles.contrast_def, 'visible', v); set(handles.contrast, 'visible', v); end if isfield(opt,'enablebrightness') if opt.enablebrightness v = 'on'; else v = 'off'; end set(handles.Tbrightness, 'visible', v); set(handles.brightness_frame, 'visible', v); set(handles.brightness_def, 'visible', v); set(handles.brightness, 'visible', v); end if isfield(opt,'enabledirlabel') if opt.enabledirlabel v = 'on'; else v = 'off'; end set(handles.top1_label, 'visible', v); set(handles.top2_label, 'visible', v); set(handles.side1_label, 'visible', v); set(handles.side2_label, 'visible', v); end if isfield(opt,'enableslider') if opt.enableslider v = 'on'; else v = 'off'; end if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider) set(handles.sagittal_slider, 'visible', v); end if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider) set(handles.coronal_slider, 'visible', v); end if isfield(handles,'axial_slider') & ishandle(handles.axial_slider) set(handles.axial_slider, 'visible', v); end end return; % update_enable %---------------------------------------------------------------- function update_usepanel(fig, usepanel) if isempty(usepanel) return; end if usepanel opt.enablecursormove = 1; opt.enableviewpoint = 1; opt.enableorigin = 1; opt.enableunit = 1; opt.enablecrosshair = 1; % opt.enablehistogram = 1; opt.enablecolormap = 1; opt.enablecontrast = 1; opt.enablebrightness = 1; else opt.enablecursormove = 0; opt.enableviewpoint = 0; opt.enableorigin = 0; opt.enableunit = 0; opt.enablecrosshair = 0; % opt.enablehistogram = 0; opt.enablecolormap = 0; opt.enablecontrast = 0; opt.enablebrightness = 0; end update_enable(fig, opt); nii_view = getappdata(fig,'nii_view'); nii_view.usepanel = usepanel; setappdata(fig,'nii_view',nii_view); return; % update_usepanel %---------------------------------------------------------------- function update_usecrosshair(fig, usecrosshair) if isempty(usecrosshair) return; end if usecrosshair v=1; else v=2; end nii_view = getappdata(fig,'nii_view'); set(nii_view.handles.xhair,'value',v); opt.command = 'crosshair'; view_nii(fig, opt); return; % update_usecrosshair %---------------------------------------------------------------- function update_usestretch(fig, usestretch) nii_view = getappdata(fig,'nii_view'); handles = nii_view.handles; fig = nii_view.fig; area = nii_view.area; vol_size = nii_view.voxel_size .* nii_view.dims; % Three Axes & label % [top_ax, front_ax, side_ax] = ... create_ax(fig, area, vol_size, usestretch); dir_label(fig, top_ax, front_ax, side_ax); top_pos = get(top_ax,'position'); front_pos = get(front_ax,'position'); side_pos = get(side_ax,'position'); % Sagittal Slider % x = side_pos(1); y = top_pos(2) + top_pos(4); w = side_pos(3); h = (front_pos(2) - y) / 2; y = y + h; pos = [x y w h]; if isfield(handles,'sagittal_slider') & ishandle(handles.sagittal_slider) set(handles.sagittal_slider,'position',pos); end % Coronal Slider % x = top_pos(1); y = top_pos(2) + top_pos(4); w = top_pos(3); h = (front_pos(2) - y) / 2; y = y + h; pos = [x y w h]; if isfield(handles,'coronal_slider') & ishandle(handles.coronal_slider) set(handles.coronal_slider,'position',pos); end % Axial Slider % x = top_pos(1); y = area(2); w = top_pos(3); h = top_pos(2) - y; pos = [x y w h]; if isfield(handles,'axial_slider') & ishandle(handles.axial_slider) set(handles.axial_slider,'position',pos); end % plot info view % % info_pos = [side_pos([1,3]); top_pos([2,4])]; % info_pos = info_pos(:); gap = side_pos(1)-(top_pos(1)+top_pos(3)); info_pos(1) = side_pos(1) + gap; info_pos(2) = area(2); info_pos(3) = side_pos(3) - gap; info_pos(4) = top_pos(2) + top_pos(4) - area(2) - gap; num_inputline = 10; inputline_space =info_pos(4) / num_inputline; % Image Intensity Value at Cursor % x = info_pos(1); y = info_pos(2); w = info_pos(3)*0.5; h = inputline_space*0.6; pos = [x y w h]; set(handles.Timvalcur,'position',pos); x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.imvalcur,'position',pos); % Position at Cursor % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.Timposcur,'position',pos); x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.imposcur,'position',pos); % Image Intensity Value at Mouse Click % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.Timval,'position',pos); x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.imval,'position',pos); % Viewpoint Position at Mouse Click % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.Timpos,'position',pos); x = x + w + 0.005; y = y - 0.008; w = info_pos(3)*0.5; h = inputline_space*0.9; pos = [x y w h]; set(handles.impos,'position',pos); % Origin Position % x = info_pos(1); y = y + inputline_space*1.2; w = info_pos(3)*0.5; h = inputline_space*0.6; pos = [x y w h]; set(handles.Torigin,'position',pos); x = x + w; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.origin,'position',pos); if 0 % Axes Unit % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.5; pos = [x y w h]; set(handles.Tcoord,'position',pos); x = x + w + 0.005; w = info_pos(3)*0.5 - 0.005; pos = [x y w h]; set(handles.coord,'position',pos); end % Crosshair % x = info_pos(1); y = y + inputline_space; w = info_pos(3)*0.4; pos = [x y w h]; set(handles.Txhair,'position',pos); x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.2; h = inputline_space*0.7; pos = [x y w h]; set(handles.xhair_color,'position',pos); x = info_pos(1) + info_pos(3)*0.7; w = info_pos(3)*0.3; pos = [x y w h]; set(handles.xhair,'position',pos); % Histogram & Color % x = info_pos(1); w = info_pos(3)*0.45; h = inputline_space * 1.5; pos = [x, y+inputline_space*0.9, w, h]; set(handles.hist_frame,'position',pos); set(handles.coord_frame,'position',pos); x = info_pos(1) + info_pos(3)*0.475; w = info_pos(3)*0.525; h = inputline_space * 1.5; pos = [x, y+inputline_space*0.9, w, h]; set(handles.color_frame,'position',pos); x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space*1.2; w = info_pos(3)*0.2; h = inputline_space*0.7; pos = [x y w h]; set(handles.hist_eq,'position',pos); x = x + w; w = info_pos(3)*0.2; pos = [x y w h]; set(handles.hist_plot,'position',pos); x = info_pos(1) + info_pos(3)*0.025; w = info_pos(3)*0.4; pos = [x y w h]; set(handles.coord,'position',pos); x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.2; pos = [x y w h]; set(handles.neg_color,'position',pos); x = info_pos(1) + info_pos(3)*0.7; w = info_pos(3)*0.275; pos = [x y w h]; set(handles.colorindex,'position',pos); x = info_pos(1) + info_pos(3)*0.1; y = y + inputline_space; w = info_pos(3)*0.28; h = inputline_space*0.6; pos = [x y w h]; set(handles.Thist,'position',pos); set(handles.Tcoord,'position',pos); x = info_pos(1) + info_pos(3)*0.60; w = info_pos(3)*0.28; pos = [x y w h]; set(handles.Tcolor,'position',pos); % Contrast Frame % x = info_pos(1); w = info_pos(3)*0.45; h = inputline_space * 2; pos = [x, y+inputline_space*0.8, w, h]; set(handles.contrast_frame,'position',pos); % Brightness Frame % x = info_pos(1) + info_pos(3)*0.475; w = info_pos(3)*0.525; pos = [x, y+inputline_space*0.8, w, h]; set(handles.brightness_frame,'position',pos); % Contrast % x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space; w = info_pos(3)*0.4; h = inputline_space*0.6; pos = [x y w h]; set(handles.contrast,'position',pos); % Brightness % x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.475; pos = [x y w h]; set(handles.brightness,'position',pos); % Contrast text/def % x = info_pos(1) + info_pos(3)*0.025; y = y + inputline_space; w = info_pos(3)*0.22; pos = [x y w h]; set(handles.Tcontrast,'position',pos); x = x + w; w = info_pos(3)*0.18; pos = [x y w h]; set(handles.contrast_def,'position',pos); % Brightness text/def % x = info_pos(1) + info_pos(3)*0.5; w = info_pos(3)*0.295; pos = [x y w h]; set(handles.Tbrightness,'position',pos); x = x + w; w = info_pos(3)*0.18; pos = [x y w h]; set(handles.brightness_def,'position',pos); return; % update_usestretch %---------------------------------------------------------------- function update_useinterp(fig, useinterp) if isempty(useinterp) return; end nii_menu = getappdata(fig, 'nii_menu'); if ~isempty(nii_menu) if get(nii_menu.Minterp,'user') set(nii_menu.Minterp,'Userdata',0,'Label','Interp off'); else set(nii_menu.Minterp,'Userdata',1,'Label','Interp on'); end end nii_view = getappdata(fig, 'nii_view'); nii_view.useinterp = useinterp; if ~isempty(nii_view.handles.axial_image) if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct') useimagesc = 0; else useimagesc = 1; end elseif ~isempty(nii_view.handles.coronal_image) if strcmpi(get(nii_view.handles.coronal_image,'cdatamapping'), 'direct') useimagesc = 0; else useimagesc = 1; end else if strcmpi(get(nii_view.handles.sagittal_image,'cdatamapping'), 'direct') useimagesc = 0; else useimagesc = 1; end end if ~isempty(nii_view.handles.axial_image) img_slice = get(nii_view.handles.axial_image, 'cdata'); delete(nii_view.handles.axial_image); axes(nii_view.handles.axial_axes); clim = get(gca,'clim'); if useinterp if useimagesc nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp'); else nii_view.handles.axial_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end else if useimagesc nii_view.handles.axial_image = imagesc('cdata',img_slice); else nii_view.handles.axial_image = image('cdata',img_slice); end end set(gca,'clim',clim); order = get(gca,'child'); order(find(order == nii_view.handles.axial_image)) = []; order = [order; nii_view.handles.axial_image]; if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) order(find(order == nii_view.handles.axial_bg)) = []; order = [order; nii_view.handles.axial_bg]; end set(gca, 'child', order); if ~useinterp if isfield(nii_view.handles,'axial_bg') & ~isempty(nii_view.handles.axial_bg) delete(nii_view.handles.axial_bg); nii_view.handles.axial_bg = []; end end set(nii_view.handles.axial_image,'buttondown','view_nii(''axial_image'');'); end if ~isempty(nii_view.handles.coronal_image) img_slice = get(nii_view.handles.coronal_image, 'cdata'); delete(nii_view.handles.coronal_image); axes(nii_view.handles.coronal_axes); clim = get(gca,'clim'); if useinterp if useimagesc nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp'); else nii_view.handles.coronal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end else if useimagesc nii_view.handles.coronal_image = imagesc('cdata',img_slice); else nii_view.handles.coronal_image = image('cdata',img_slice); end end set(gca,'clim',clim); order = get(gca,'child'); order(find(order == nii_view.handles.coronal_image)) = []; order = [order; nii_view.handles.coronal_image]; if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) order(find(order == nii_view.handles.coronal_bg)) = []; order = [order; nii_view.handles.coronal_bg]; end set(gca, 'child', order); if ~useinterp if isfield(nii_view.handles,'coronal_bg') & ~isempty(nii_view.handles.coronal_bg) delete(nii_view.handles.coronal_bg); nii_view.handles.coronal_bg = []; end end set(nii_view.handles.coronal_image,'buttondown','view_nii(''coronal_image'');'); end if ~isempty(nii_view.handles.sagittal_image) img_slice = get(nii_view.handles.sagittal_image, 'cdata'); delete(nii_view.handles.sagittal_image); axes(nii_view.handles.sagittal_axes); clim = get(gca,'clim'); if useinterp if useimagesc nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'edgecolor','none','facecolor','interp'); else nii_view.handles.sagittal_image = surface(zeros(size(img_slice)),double(img_slice),'cdatamapping','direct','edgecolor','none','facecolor','interp'); end else if useimagesc nii_view.handles.sagittal_image = imagesc('cdata',img_slice); else nii_view.handles.sagittal_image = image('cdata',img_slice); end end set(gca,'clim',clim); order = get(gca,'child'); order(find(order == nii_view.handles.sagittal_image)) = []; order = [order; nii_view.handles.sagittal_image]; if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) order(find(order == nii_view.handles.sagittal_bg)) = []; order = [order; nii_view.handles.sagittal_bg]; end set(gca, 'child', order); if ~useinterp if isfield(nii_view.handles,'sagittal_bg') & ~isempty(nii_view.handles.sagittal_bg) delete(nii_view.handles.sagittal_bg); nii_view.handles.sagittal_bg = []; end end set(nii_view.handles.sagittal_image,'buttondown','view_nii(''sagittal_image'');'); end if ~useinterp nii_view.bgimg = []; end set_coordinates(nii_view,useinterp); setappdata(fig, 'nii_view', nii_view); return; % update_useinterp %---------------------------------------------------------------- function update_useimagesc(fig, useimagesc) if isempty(useimagesc) return; end if useimagesc v='scaled'; else v='direct'; end nii_view = getappdata(fig,'nii_view'); handles = nii_view.handles; if isfield(handles,'cbar_image') & ishandle(handles.cbar_image) % set(handles.cbar_image,'cdatamapping',v); end set(handles.axial_image,'cdatamapping',v); set(handles.coronal_image,'cdatamapping',v); set(handles.sagittal_image,'cdatamapping',v); return; % update_useimagesc %---------------------------------------------------------------- function update_shape(fig, area, usecolorbar, usestretch, useimagesc) nii_view = getappdata(fig,'nii_view'); if isempty(usestretch) % no change, get usestretch stretchchange = 0; usestretch = nii_view.usestretch; else % change, set usestretch stretchchange = 1; nii_view.usestretch = usestretch; end if isempty(area) % no change, get area areachange = 0; area = nii_view.area; elseif ~isempty(nii_view.cbar_area) % change, set area & cbar_area areachange = 1; cbar_area = area; cbar_area(1) = area(1) + area(3)*0.93; cbar_area(3) = area(3)*0.04; area(3) = area(3)*0.9; % 90% used for main axes [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area); nii_view.area = area; nii_view.cbar_area = cbar_area; else % change, set area only areachange = 1; nii_view.area = area; end % Add colorbar % if ~isempty(usecolorbar) & usecolorbar & isempty(nii_view.cbar_area) colorbarchange = 1; cbar_area = area; cbar_area(1) = area(1) + area(3)*0.93; cbar_area(3) = area(3)*0.04; area(3) = area(3)*0.9; % 90% used for main axes % create axes for colorbar % [cbar_axes cbarminmax_axes] = create_cbar_axes(fig, cbar_area); nii_view.area = area; nii_view.cbar_area = cbar_area; % useimagesc follows axial image % if isempty(useimagesc) if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled') useimagesc = 1; else useimagesc = 0; end end if isfield(nii_view, 'highcolor') & ~isempty(highcolor) num_highcolor = size(nii_view.highcolor,1); else num_highcolor = 0; end if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel) colorlevel = nii_view.colorlevel; else colorlevel = 256 - num_highcolor; end if isfield(nii_view, 'color_map') color_map = nii_view.color_map; else color_map = []; end if isfield(nii_view, 'highcolor') highcolor = nii_view.highcolor; else highcolor = []; end % plot colorbar % if 0 if isempty(color_map) level = colorlevel + num_highcolor; else level = size([color_map; highcolor], 1); end end if isempty(color_map) level = colorlevel; else level = size([color_map], 1); end cbar_image = [1:level]'; niiclass = class(nii_view.nii.img); h1 = plot_cbar(fig, cbar_axes, cbarminmax_axes, nii_view.cbarminmax, ... level, nii_view.handles, useimagesc, nii_view.colorindex, ... color_map, colorlevel, highcolor, niiclass, nii_view.numscan); nii_view.handles.cbar_image = h1; nii_view.handles.cbar_axes = cbar_axes; nii_view.handles.cbarminmax_axes = cbar_axes; % remove colorbar % elseif ~isempty(usecolorbar) & ~usecolorbar & ~isempty(nii_view.cbar_area) colorbarchange = 1; area(3) = area(3) / 0.9; nii_view.area = area; nii_view.cbar_area = []; nii_view.handles = rmfield(nii_view.handles,'cbar_image'); delete(nii_view.handles.cbarminmax_axes); nii_view.handles = rmfield(nii_view.handles,'cbarminmax_axes'); delete(nii_view.handles.cbar_axes); nii_view.handles = rmfield(nii_view.handles,'cbar_axes'); else colorbarchange = 0; end if colorbarchange | stretchchange | areachange setappdata(fig,'nii_view',nii_view); update_usestretch(fig, usestretch); end return; % update_shape %---------------------------------------------------------------- function update_unit(fig, setunit) if isempty(setunit) return; end if strcmpi(setunit,'mm') | strcmpi(setunit,'millimeter') | strcmpi(setunit,'mni') v = 2; % elseif strcmpi(setunit,'tal') | strcmpi(setunit,'talairach') % v = 3; elseif strcmpi(setunit,'vox') | strcmpi(setunit,'voxel') v = 1; else v = 1; end nii_view = getappdata(fig,'nii_view'); set(nii_view.handles.coord, 'value', v); set_image_value(nii_view); return; % update_unit %---------------------------------------------------------------- function update_viewpoint(fig, setviewpoint) if isempty(setviewpoint) return; end nii_view = getappdata(fig,'nii_view'); if length(setviewpoint) ~= 3 error('Viewpoint position should contain [x y z]'); end set(nii_view.handles.impos,'string',num2str(setviewpoint)); opt.command = 'impos_edit'; view_nii(fig, opt); set(nii_view.handles.axial_axes,'selected','on'); set(nii_view.handles.axial_axes,'selected','off'); set(nii_view.handles.coronal_axes,'selected','on'); set(nii_view.handles.coronal_axes,'selected','off'); set(nii_view.handles.sagittal_axes,'selected','on'); set(nii_view.handles.sagittal_axes,'selected','off'); return; % update_viewpoint %---------------------------------------------------------------- function update_scanid(fig, setscanid) if isempty(setscanid) return; end nii_view = getappdata(fig,'nii_view'); if setscanid < 1 setscanid = 1; end if setscanid > nii_view.numscan setscanid = nii_view.numscan; end set(nii_view.handles.contrast_def,'string',num2str(setscanid)); set(nii_view.handles.contrast,'value',setscanid); opt.command = 'updateimg'; opt.setscanid = setscanid; view_nii(fig, nii_view.nii.img, opt); return; % update_scanid %---------------------------------------------------------------- function update_crosshaircolor(fig, new_color) if isempty(new_color) return; end nii_view = getappdata(fig,'nii_view'); xhair_color = nii_view.handles.xhair_color; set(xhair_color,'user',new_color); set(nii_view.axi_xhair.lx,'color',new_color); set(nii_view.axi_xhair.ly,'color',new_color); set(nii_view.cor_xhair.lx,'color',new_color); set(nii_view.cor_xhair.ly,'color',new_color); set(nii_view.sag_xhair.lx,'color',new_color); set(nii_view.sag_xhair.ly,'color',new_color); return; % update_crosshaircolor %---------------------------------------------------------------- function update_colorindex(fig, colorindex) if isempty(colorindex) return; end nii_view = getappdata(fig,'nii_view'); nii_view.colorindex = colorindex; setappdata(fig, 'nii_view', nii_view); set(nii_view.handles.colorindex,'value',colorindex); opt.command = 'color'; view_nii(fig, opt); return; % update_colorindex %---------------------------------------------------------------- function redraw_cbar(fig, colorlevel, color_map, highcolor) nii_view = getappdata(fig,'nii_view'); if isempty(nii_view.cbar_area) return; end colorindex = nii_view.colorindex; if isempty(highcolor) num_highcolor = 0; else num_highcolor = size(highcolor,1); end if isempty(colorlevel) colorlevel=256; end if colorindex == 1 colorlevel = size(color_map, 1); end % level = colorlevel + num_highcolor; level = colorlevel; cbar_image = [1:level]'; cbar_area = nii_view.cbar_area; % useimagesc follows axial image % if strcmpi(get(nii_view.handles.axial_image,'cdatamap'),'scaled') useimagesc = 1; else useimagesc = 0; end niiclass = class(nii_view.nii.img); delete(nii_view.handles.cbar_image); delete(nii_view.handles.cbar_axes); delete(nii_view.handles.cbarminmax_axes); [nii_view.handles.cbar_axes nii_view.handles.cbarminmax_axes] = ... create_cbar_axes(fig, cbar_area, []); nii_view.handles.cbar_image = plot_cbar(fig, ... nii_view.handles.cbar_axes, nii_view.handles.cbarminmax_axes, ... nii_view.cbarminmax, level, nii_view.handles, useimagesc, ... colorindex, color_map, colorlevel, highcolor, niiclass, ... nii_view.numscan, []); setappdata(fig, 'nii_view', nii_view); return; % redraw_cbar %---------------------------------------------------------------- function update_buttondown(fig, setbuttondown) if isempty(setbuttondown) return; end nii_view = getappdata(fig,'nii_view'); nii_view.buttondown = setbuttondown; setappdata(fig, 'nii_view', nii_view); return; % update_buttondown %---------------------------------------------------------------- function update_cbarminmax(fig, cbarminmax) if isempty(cbarminmax) return; end nii_view = getappdata(fig, 'nii_view'); if ~isfield(nii_view.handles, 'cbarminmax_axes') return; end nii_view.cbarminmax = cbarminmax; setappdata(fig, 'nii_view', nii_view); axes(nii_view.handles.cbarminmax_axes); plot([0 0], cbarminmax, 'w'); axis tight; set(nii_view.handles.cbarminmax_axes,'YDir','normal', ... 'XLimMode','manual','YLimMode','manual','YColor',[0 0 0], ... 'XColor',[0 0 0],'xtick',[],'YAxisLocation','right'); ylim = get(nii_view.handles.cbar_axes,'ylim'); ylimb = get(nii_view.handles.cbarminmax_axes,'ylim'); ytickb = get(nii_view.handles.cbarminmax_axes,'ytick'); ytick=(ylim(2)-ylim(1))*(ytickb-ylimb(1))/(ylimb(2)-ylimb(1))+ylim(1); axes(nii_view.handles.cbar_axes); set(nii_view.handles.cbar_axes,'YDir','normal','XLimMode','manual', ... 'YLimMode','manual','YColor',[0 0 0],'XColor',[0 0 0],'xtick',[], ... 'YAxisLocation','right','ylim',ylim,'ytick',ytick,'yticklabel',''); return; % update_cbarminmax %---------------------------------------------------------------- function update_highcolor(fig, highcolor, colorlevel) nii_view = getappdata(fig,'nii_view'); if ischar(highcolor) & (isempty(colorlevel) | nii_view.colorindex == 1) return; end if ~ischar(highcolor) nii_view.highcolor = highcolor; if isempty(highcolor) nii_view = rmfield(nii_view, 'highcolor'); end else highcolor = []; end if isempty(colorlevel) | nii_view.colorindex == 1 nii_view.colorlevel = nii_view.colorlevel - size(highcolor,1); else nii_view.colorlevel = colorlevel; end setappdata(fig, 'nii_view', nii_view); if isfield(nii_view,'color_map') color_map = nii_view.color_map; else color_map = []; end redraw_cbar(fig, nii_view.colorlevel, color_map, highcolor); change_colormap(fig); return; % update_highcolor %---------------------------------------------------------------- function update_colormap(fig, color_map) if ischar(color_map) return; end nii_view = getappdata(fig,'nii_view'); nii = nii_view.nii; minvalue = nii_view.minvalue; if isempty(color_map) if minvalue < 0 colorindex = 2; else colorindex = 3; end nii_view = rmfield(nii_view, 'color_map'); setappdata(fig,'nii_view',nii_view); update_colorindex(fig, colorindex); return; else colorindex = 1; nii_view.color_map = color_map; nii_view.colorindex = colorindex; setappdata(fig,'nii_view',nii_view); set(nii_view.handles.colorindex,'value',colorindex); end colorlevel = nii_view.colorlevel; if isfield(nii_view, 'highcolor') highcolor = nii_view.highcolor; else highcolor = []; end redraw_cbar(fig, colorlevel, color_map, highcolor); change_colormap(fig); opt.enablecontrast = 0; update_enable(fig, opt); return; % update_colormap %---------------------------------------------------------------- function status = get_status(h); nii_view = getappdata(h,'nii_view'); status.fig = h; status.area = nii_view.area; if isempty(nii_view.cbar_area) status.usecolorbar = 0; else status.usecolorbar = 1; width = status.area(3) / 0.9; status.area(3) = width; end if strcmpi(get(nii_view.handles.imval,'visible'), 'on') status.usepanel = 1; else status.usepanel = 0; end if get(nii_view.handles.xhair,'value') == 1 status.usecrosshair = 1; else status.usecrosshair = 0; end status.usestretch = nii_view.usestretch; if strcmpi(get(nii_view.handles.axial_image,'cdatamapping'), 'direct') status.useimagesc = 0; else status.useimagesc = 1; end status.useinterp = nii_view.useinterp; if get(nii_view.handles.coord,'value') == 1 status.unit = 'vox'; elseif get(nii_view.handles.coord,'value') == 2 status.unit = 'mm'; elseif get(nii_view.handles.coord,'value') == 3 status.unit = 'tal'; end status.viewpoint = get(nii_view.handles.impos,'value'); status.scanid = nii_view.scanid; status.intensity = get(nii_view.handles.imval,'value'); status.colorindex = get(nii_view.handles.colorindex,'value'); if isfield(nii_view,'color_map') status.colormap = nii_view.color_map; else status.colormap = []; end status.colorlevel = nii_view.colorlevel; if isfield(nii_view,'highcolor') status.highcolor = nii_view.highcolor; else status.highcolor = []; end status.cbarminmax = nii_view.cbarminmax; status.buttondown = nii_view.buttondown; return; % get_status %---------------------------------------------------------------- function [custom_color_map, colorindex] ... = change_colormap(fig, nii, colorindex, cbarminmax) custom_color_map = []; if ~exist('nii', 'var') nii_view = getappdata(fig,'nii_view'); else nii_view = nii; end if ~exist('colorindex', 'var') colorindex = get(nii_view.handles.colorindex,'value'); end if ~exist('cbarminmax', 'var') cbarminmax = nii_view.cbarminmax; end if isfield(nii_view, 'highcolor') & ~isempty(nii_view.highcolor) highcolor = nii_view.highcolor; num_highcolor = size(highcolor,1); else highcolor = []; num_highcolor = 0; end % if isfield(nii_view, 'colorlevel') & ~isempty(nii_view.colorlevel) if nii_view.colorlevel < 256 num_color = nii_view.colorlevel; else num_color = 256 - num_highcolor; end contrast = []; if colorindex == 3 % for gray if nii_view.numscan > 1 contrast = 1; else contrast = (num_color-1)*(get(nii_view.handles.contrast,'value')-1)/255+1; contrast = floor(contrast); end elseif colorindex == 2 % for bipolar if nii_view.numscan > 1 contrast = 128; else contrast = get(nii_view.handles.contrast,'value'); end end if isfield(nii_view,'color_map') & ~isempty(nii_view.color_map) color_map = nii_view.color_map; custom_color_map = color_map; elseif colorindex == 1 [f p] = uigetfile('*.txt', 'Input colormap text file'); if p==0 colorindex = nii_view.colorindex; set(nii_view.handles.colorindex,'value',colorindex); return; end; try custom_color_map = load(fullfile(p,f)); loadfail = 0; catch loadfail = 1; end if loadfail | isempty(custom_color_map) | size(custom_color_map,2)~=3 ... | min(custom_color_map(:)) < 0 | max(custom_color_map(:)) > 1 msg = 'Colormap should be a Mx3 matrix with value between 0 and 1'; msgbox(msg,'Error in colormap file'); colorindex = nii_view.colorindex; set(nii_view.handles.colorindex,'value',colorindex); return; end color_map = custom_color_map; nii_view.color_map = color_map; end switch colorindex case {2} color_map = bipolar(num_color, cbarminmax(1), cbarminmax(2), contrast); case {3} color_map = gray(num_color - contrast + 1); case {4} color_map = jet(num_color); case {5} color_map = cool(num_color); case {6} color_map = bone(num_color); case {7} color_map = hot(num_color); case {8} color_map = copper(num_color); case {9} color_map = pink(num_color); end nii_view.colorindex = colorindex; if ~exist('nii', 'var') setappdata(fig,'nii_view',nii_view); end if colorindex == 3 color_map = [zeros(contrast,3); color_map(2:end,:)]; end if get(nii_view.handles.neg_color,'value') & isempty(highcolor) color_map = flipud(color_map); elseif get(nii_view.handles.neg_color,'value') & ~isempty(highcolor) highcolor = flipud(highcolor); end brightness = get(nii_view.handles.brightness,'value'); color_map = brighten(color_map, brightness); color_map = [color_map; highcolor]; set(fig, 'colormap', color_map); return; % change_colormap %---------------------------------------------------------------- function move_cursor(fig) nii_view = getappdata(fig, 'nii_view'); if isempty(nii_view) return; end axi = get(nii_view.handles.axial_axes, 'pos'); cor = get(nii_view.handles.coronal_axes, 'pos'); sag = get(nii_view.handles.sagittal_axes, 'pos'); curr = get(fig, 'currentpoint'); if curr(1) >= axi(1) & curr(1) <= axi(1)+axi(3) & ... curr(2) >= axi(2) & curr(2) <= axi(2)+axi(4) curr = get(nii_view.handles.axial_axes, 'current'); sag = curr(1,1); cor = curr(1,2); axi = nii_view.slices.axi; elseif curr(1) >= cor(1) & curr(1) <= cor(1)+cor(3) & ... curr(2) >= cor(2) & curr(2) <= cor(2)+cor(4) curr = get(nii_view.handles.coronal_axes, 'current'); sag = curr(1,1); cor = nii_view.slices.cor; axi = curr(1,2); elseif curr(1) >= sag(1) & curr(1) <= sag(1)+sag(3) & ... curr(2) >= sag(2) & curr(2) <= sag(2)+sag(4) curr = get(nii_view.handles.sagittal_axes, 'current'); sag = nii_view.slices.sag; cor = curr(1,1); axi = curr(1,2); else set(nii_view.handles.imvalcur,'String',' '); set(nii_view.handles.imposcur,'String',' '); return; end sag = round(sag); cor = round(cor); axi = round(axi); if sag < 1 sag = 1; elseif sag > nii_view.dims(1) sag = nii_view.dims(1); end if cor < 1 cor = 1; elseif cor > nii_view.dims(2) cor = nii_view.dims(2); end if axi < 1 axi = 1; elseif axi > nii_view.dims(3) axi = nii_view.dims(3); end if 0 % isfield(nii_view, 'disp') img = nii_view.disp; else img = nii_view.nii.img; end if nii_view.nii.hdr.dime.datatype == 128 imgvalue = [double(img(sag,cor,axi,1,nii_view.scanid)) double(img(sag,cor,axi,2,nii_view.scanid)) double(img(sag,cor,axi,3,nii_view.scanid))]; set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue)); elseif nii_view.nii.hdr.dime.datatype == 511 R = double(img(sag,cor,axi,1,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; G = double(img(sag,cor,axi,2,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; B = double(img(sag,cor,axi,3,nii_view.scanid)) * (nii_view.nii.hdr.dime.glmax - ... nii_view.nii.hdr.dime.glmin) + nii_view.nii.hdr.dime.glmin; imgvalue = [R G B]; set(nii_view.handles.imvalcur,'String',sprintf('%7.4g %7.4g %7.4g',imgvalue)); else imgvalue = double(img(sag,cor,axi,nii_view.scanid)); if isnan(imgvalue) | imgvalue > nii_view.cbarminmax(2) imgvalue = 0; end set(nii_view.handles.imvalcur,'String',sprintf('%.6g',imgvalue)); end nii_view.slices.sag = sag; nii_view.slices.cor = cor; nii_view.slices.axi = axi; nii_view = update_imgXYZ(nii_view); if get(nii_view.handles.coord,'value') == 1, sag = nii_view.imgXYZ.vox(1); cor = nii_view.imgXYZ.vox(2); axi = nii_view.imgXYZ.vox(3); elseif get(nii_view.handles.coord,'value') == 2, sag = nii_view.imgXYZ.mm(1); cor = nii_view.imgXYZ.mm(2); axi = nii_view.imgXYZ.mm(3); elseif get(nii_view.handles.coord,'value') == 3, sag = nii_view.imgXYZ.tal(1); cor = nii_view.imgXYZ.tal(2); axi = nii_view.imgXYZ.tal(3); end if get(nii_view.handles.coord,'value') == 1, string = sprintf('%7.0f %7.0f %7.0f',sag,cor,axi); else string = sprintf('%7.1f %7.1f %7.1f',sag,cor,axi); end; set(nii_view.handles.imposcur,'String',string); return; % move_cursor %---------------------------------------------------------------- function change_scan(hdl_str) fig = gcbf; nii_view = getappdata(fig,'nii_view'); if strcmpi(hdl_str, 'edit_change_scan') % edit hdl = nii_view.handles.contrast_def; setscanid = round(str2num(get(hdl, 'string'))); else % slider hdl = nii_view.handles.contrast; setscanid = round(get(hdl, 'value')); end update_scanid(fig, setscanid); return; % change_scan %---------------------------------------------------------------- function val = scale_in(val, minval, maxval, range) % scale value into range % val = range*(double(val)-double(minval))/(double(maxval)-double(minval))+1; return; % scale_in %---------------------------------------------------------------- function val = scale_out(val, minval, maxval, range) % according to [minval maxval] and range of color levels (e.g. 199) % scale val back from any thing between 1~256 to a small number that % is corresonding to [minval maxval]. % val = (double(val)-1)*(double(maxval)-double(minval))/range+double(minval); return; % scale_out
github
sunhongfu/scripts-master
mat_into_hdr.m
.m
scripts-master/cs-phase/_src/_nii/mat_into_hdr.m
2,691
utf_8
847d96698f45f7c5e7decbb3a0c3187f
%MAT_INTO_HDR The old versions of SPM (any version before SPM5) store % an affine matrix of the SPM Reoriented image into a matlab file % (.mat extension). The file name of this SPM matlab file is the % same as the SPM Reoriented image file (.img/.hdr extension). % % This program will convert the ANALYZE 7.5 SPM Reoriented image % file into NIfTI format, and integrate the affine matrix in the % SPM matlab file into its header file (.hdr extension). % % WARNING: Before you run this program, please save the header % file (.hdr extension) into another file name or into another % folder location, because all header files (.hdr extension) % will be overwritten after they are converted into NIfTI % format. % % Usage: mat_into_hdr(filename); % % filename: file name(s) with .hdr or .mat file extension, like: % '*.hdr', or '*.mat', or a single .hdr or .mat file. % e.g. mat_into_hdr('T1.hdr') % mat_into_hdr('*.mat') % % - Jimmy Shen ([email protected]) % %------------------------------------------------------------------------- function mat_into_hdr(files) pn = fileparts(files); file_lst = dir(files); file_lst = {file_lst.name}; file1 = file_lst{1}; [p n e]= fileparts(file1); for i=1:length(file_lst) [p n e]= fileparts(file_lst{i}); disp(['working on file ', num2str(i) ,' of ', num2str(length(file_lst)), ': ', n,e]); process=1; if isequal(e,'.hdr') mat=fullfile(pn, [n,'.mat']); hdr=fullfile(pn, file_lst{i}); if ~exist(mat,'file') warning(['Cannot find file "',mat , '". File "', n, e, '" will not be processed.']); process=0; end elseif isequal(e,'.mat') hdr=fullfile(pn, [n,'.hdr']); mat=fullfile(pn, file_lst{i}); if ~exist(hdr,'file') warning(['Can not find file "',hdr , '". File "', n, e, '" will not be processed.']); process=0; end else warning(['Input file must have .mat or .hdr extension. File "', n, e, '" will not be processed.']); process=0; end if process load(mat); R=M(1:3,1:3); T=M(1:3,4); T=R*ones(3,1)+T; M(1:3,4)=T; [h filetype fileprefix machine]=load_nii_hdr(hdr); h.hist.qform_code=0; h.hist.sform_code=1; h.hist.srow_x=M(1,:); h.hist.srow_y=M(2,:); h.hist.srow_z=M(3,:); h.hist.magic='ni1'; fid = fopen(hdr,'w',machine); save_nii_hdr(h,fid); fclose(fid); end end return; % mat_into_hdr
github
sunhongfu/scripts-master
xform_nii.m
.m
scripts-master/cs-phase/_src/_nii/xform_nii.m
18,628
utf_8
e39c421e7f117cbc81c56e9d023774a3
% internal function % 'xform_nii.m' is an internal function called by "load_nii.m", so % you do not need run this program by yourself. It does simplified % NIfTI sform/qform affine transform, and supports some of the % affine transforms, including translation, reflection, and % orthogonal rotation (N*90 degree). % % For other affine transforms, e.g. any degree rotation, shearing % etc. you will have to use the included 'reslice_nii.m' program % to reslice the image volume. 'reslice_nii.m' is not called by % any other program, and you have to run 'reslice_nii.m' explicitly % for those NIfTI files that you want to reslice them. % % Since 'xform_nii.m' does not involve any interpolation or any % slice change, the original image volume is supposed to be % untouched, although it is translated, reflected, or even % orthogonally rotated, based on the affine matrix in the % NIfTI header. % % However, the affine matrix in the header of a lot NIfTI files % contain slightly non-orthogonal rotation. Therefore, optional % input parameter 'tolerance' is used to allow some distortion % in the loaded image for any non-orthogonal rotation or shearing % of NIfTI affine matrix. If you set 'tolerance' to 0, it means % that you do not allow any distortion. If you set 'tolerance' to % 1, it means that you do not care any distortion. The image will % fail to be loaded if it can not be tolerated. The tolerance will % be set to 0.1 (10%), if it is default or empty. % % Because 'reslice_nii.m' has to perform 3D interpolation, it can % be slow depending on image size and affine matrix in the header. % % After you perform the affine transform, the 'nii' structure % generated from 'xform_nii.m' or new NIfTI file created from % 'reslice_nii.m' will be in RAS orientation, i.e. X axis from % Left to Right, Y axis from Posterior to Anterior, and Z axis % from Inferior to Superior. % % NOTE: This function should be called immediately after load_nii. % % Usage: [ nii ] = xform_nii(nii, [tolerance], [preferredForm]) % % nii - NIFTI structure (returned from load_nii) % % tolerance (optional) - distortion allowed for non-orthogonal rotation % or shearing in NIfTI affine matrix. It will be set to 0.1 (10%), % if it is default or empty. % % preferredForm (optional) - selects which transformation from voxels % to RAS coordinates; values are s,q,S,Q. Lower case s,q indicate % "prefer sform or qform, but use others if preferred not present". % Upper case indicate the program is forced to use the specificied % tranform or fail loading. 'preferredForm' will be 's', if it is % default or empty. - Jeff Gunter % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function nii = xform_nii(nii, tolerance, preferredForm) % save a copy of the header as it was loaded. This is the % header before any sform, qform manipulation is done. % nii.original.hdr = nii.hdr; if ~exist('tolerance','var') | isempty(tolerance) tolerance = 0.1; elseif(tolerance<=0) tolerance = eps; end if ~exist('preferredForm','var') | isempty(preferredForm) preferredForm= 's'; % Jeff end % if scl_slope field is nonzero, then each voxel value in the % dataset should be scaled as: y = scl_slope * x + scl_inter % I bring it here because hdr will be modified by change_hdr. % if nii.hdr.dime.scl_slope ~= 0 & ... ismember(nii.hdr.dime.datatype, [2,4,8,16,64,256,512,768]) & ... (nii.hdr.dime.scl_slope ~= 1 | nii.hdr.dime.scl_inter ~= 0) nii.img = ... nii.hdr.dime.scl_slope * double(nii.img) + nii.hdr.dime.scl_inter; if nii.hdr.dime.datatype == 64 nii.hdr.dime.datatype = 64; nii.hdr.dime.bitpix = 64; else nii.img = single(nii.img); nii.hdr.dime.datatype = 16; nii.hdr.dime.bitpix = 32; end nii.hdr.dime.glmax = max(double(nii.img(:))); nii.hdr.dime.glmin = min(double(nii.img(:))); % set scale to non-use, because it is applied in xform_nii % nii.hdr.dime.scl_slope = 0; end % However, the scaling is to be ignored if datatype is DT_RGB24. % If datatype is a complex type, then the scaling is to be applied % to both the real and imaginary parts. % if nii.hdr.dime.scl_slope ~= 0 & ... ismember(nii.hdr.dime.datatype, [32,1792]) nii.img = ... nii.hdr.dime.scl_slope * double(nii.img) + nii.hdr.dime.scl_inter; if nii.hdr.dime.datatype == 32 nii.img = single(nii.img); end nii.hdr.dime.glmax = max(double(nii.img(:))); nii.hdr.dime.glmin = min(double(nii.img(:))); % set scale to non-use, because it is applied in xform_nii % nii.hdr.dime.scl_slope = 0; end % There is no need for this program to transform Analyze data % if nii.filetype == 0 & exist([nii.fileprefix '.mat'],'file') load([nii.fileprefix '.mat']); % old SPM affine matrix R=M(1:3,1:3); T=M(1:3,4); T=R*ones(3,1)+T; M(1:3,4)=T; nii.hdr.hist.qform_code=0; nii.hdr.hist.sform_code=1; nii.hdr.hist.srow_x=M(1,:); nii.hdr.hist.srow_y=M(2,:); nii.hdr.hist.srow_z=M(3,:); elseif nii.filetype == 0 nii.hdr.hist.rot_orient = []; nii.hdr.hist.flip_orient = []; return; % no sform/qform for Analyze format end hdr = nii.hdr; [hdr,orient]=change_hdr(hdr,tolerance,preferredForm); % flip and/or rotate image data % if ~isequal(orient, [1 2 3]) old_dim = hdr.dime.dim([2:4]); % More than 1 time frame % if ndims(nii.img) > 3 pattern = 1:prod(old_dim); else pattern = []; end if ~isempty(pattern) pattern = reshape(pattern, old_dim); end % calculate for rotation after flip % rot_orient = mod(orient + 2, 3) + 1; % do flip: % flip_orient = orient - rot_orient; for i = 1:3 if flip_orient(i) if ~isempty(pattern) pattern = flipdim(pattern, i); else nii.img = flipdim(nii.img, i); end end end % get index of orient (rotate inversely) % [tmp rot_orient] = sort(rot_orient); new_dim = old_dim; new_dim = new_dim(rot_orient); hdr.dime.dim([2:4]) = new_dim; new_pixdim = hdr.dime.pixdim([2:4]); new_pixdim = new_pixdim(rot_orient); hdr.dime.pixdim([2:4]) = new_pixdim; % re-calculate originator % tmp = hdr.hist.originator([1:3]); tmp = tmp(rot_orient); flip_orient = flip_orient(rot_orient); for i = 1:3 if flip_orient(i) & ~isequal(tmp(i), 0) tmp(i) = new_dim(i) - tmp(i) + 1; end end hdr.hist.originator([1:3]) = tmp; hdr.hist.rot_orient = rot_orient; hdr.hist.flip_orient = flip_orient; % do rotation: % if ~isempty(pattern) pattern = permute(pattern, rot_orient); pattern = pattern(:); if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 | ... hdr.dime.datatype == 128 | hdr.dime.datatype == 511 tmp = reshape(nii.img(:,:,:,1), [prod(new_dim) hdr.dime.dim(5:8)]); tmp = tmp(pattern, :); nii.img(:,:,:,1) = reshape(tmp, [new_dim hdr.dime.dim(5:8)]); tmp = reshape(nii.img(:,:,:,2), [prod(new_dim) hdr.dime.dim(5:8)]); tmp = tmp(pattern, :); nii.img(:,:,:,2) = reshape(tmp, [new_dim hdr.dime.dim(5:8)]); if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 tmp = reshape(nii.img(:,:,:,3), [prod(new_dim) hdr.dime.dim(5:8)]); tmp = tmp(pattern, :); nii.img(:,:,:,3) = reshape(tmp, [new_dim hdr.dime.dim(5:8)]); end else nii.img = reshape(nii.img, [prod(new_dim) hdr.dime.dim(5:8)]); nii.img = nii.img(pattern, :); nii.img = reshape(nii.img, [new_dim hdr.dime.dim(5:8)]); end else if hdr.dime.datatype == 32 | hdr.dime.datatype == 1792 | ... hdr.dime.datatype == 128 | hdr.dime.datatype == 511 nii.img(:,:,:,1) = permute(nii.img(:,:,:,1), rot_orient); nii.img(:,:,:,2) = permute(nii.img(:,:,:,2), rot_orient); if hdr.dime.datatype == 128 | hdr.dime.datatype == 511 nii.img(:,:,:,3) = permute(nii.img(:,:,:,3), rot_orient); end else nii.img = permute(nii.img, rot_orient); end end else hdr.hist.rot_orient = []; hdr.hist.flip_orient = []; end nii.hdr = hdr; return; % xform_nii %----------------------------------------------------------------------- function [hdr, orient] = change_hdr(hdr, tolerance, preferredForm) orient = [1 2 3]; affine_transform = 1; % NIFTI can have both sform and qform transform. This program % will check sform_code prior to qform_code by default. % % If user specifys "preferredForm", user can then choose the % priority. - Jeff % useForm=[]; % Jeff if isequal(preferredForm,'S') if isequal(hdr.hist.sform_code,0) error('User requires sform, sform not set in header'); else useForm='s'; end end % Jeff if isequal(preferredForm,'Q') if isequal(hdr.hist.qform_code,0) error('User requires qform, qform not set in header'); else useForm='q'; end end % Jeff if isequal(preferredForm,'s') if hdr.hist.sform_code > 0 useForm='s'; elseif hdr.hist.qform_code > 0 useForm='q'; end end % Jeff if isequal(preferredForm,'q') if hdr.hist.qform_code > 0 useForm='q'; elseif hdr.hist.sform_code > 0 useForm='s'; end end % Jeff if isequal(useForm,'s') R = [hdr.hist.srow_x(1:3) hdr.hist.srow_y(1:3) hdr.hist.srow_z(1:3)]; T = [hdr.hist.srow_x(4) hdr.hist.srow_y(4) hdr.hist.srow_z(4)]; if det(R) == 0 | ~isequal(R(find(R)), sum(R)') hdr.hist.old_affine = [ [R;[0 0 0]] [T;1] ]; R_sort = sort(abs(R(:))); R( find( abs(R) < tolerance*min(R_sort(end-2:end)) ) ) = 0; hdr.hist.new_affine = [ [R;[0 0 0]] [T;1] ]; if det(R) == 0 | ~isequal(R(find(R)), sum(R)') msg = [char(10) char(10) ' Non-orthogonal rotation or shearing ']; msg = [msg 'found inside the affine matrix' char(10)]; msg = [msg ' in this NIfTI file. You have 3 options:' char(10) char(10)]; msg = [msg ' 1. Using included ''reslice_nii.m'' program to reslice the NIfTI' char(10)]; msg = [msg ' file. I strongly recommand this, because it will not cause' char(10)]; msg = [msg ' negative effect, as long as you remember not to do slice' char(10)]; msg = [msg ' time correction after using ''reslice_nii.m''.' char(10) char(10)]; msg = [msg ' 2. Using included ''load_untouch_nii.m'' program to load image' char(10)]; msg = [msg ' without applying any affine geometric transformation or' char(10)]; msg = [msg ' voxel intensity scaling. This is only for people who want' char(10)]; msg = [msg ' to do some image processing regardless of image orientation' char(10)]; msg = [msg ' and to save data back with the same NIfTI header.' char(10) char(10)]; msg = [msg ' 3. Increasing the tolerance to allow more distortion in loaded' char(10)]; msg = [msg ' image, but I don''t suggest this.' char(10) char(10)]; msg = [msg ' To get help, please type:' char(10) char(10) ' help reslice_nii.m' char(10)]; msg = [msg ' help load_untouch_nii.m' char(10) ' help load_nii.m']; error(msg); end end elseif isequal(useForm,'q') b = hdr.hist.quatern_b; c = hdr.hist.quatern_c; d = hdr.hist.quatern_d; if 1.0-(b*b+c*c+d*d) < 0 if abs(1.0-(b*b+c*c+d*d)) < 1e-5 a = 0; else error('Incorrect quaternion values in this NIFTI data.'); end else a = sqrt(1.0-(b*b+c*c+d*d)); end qfac = hdr.dime.pixdim(1); if qfac==0, qfac = 1; end i = hdr.dime.pixdim(2); j = hdr.dime.pixdim(3); k = qfac * hdr.dime.pixdim(4); R = [a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c 2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b 2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b]; T = [hdr.hist.qoffset_x hdr.hist.qoffset_y hdr.hist.qoffset_z]; % qforms are expected to generate rotation matrices R which are % det(R) = 1; we'll make sure that happens. % % now we make the same checks as were done above for sform data % BUT we do it on a transform that is in terms of voxels not mm; % after we figure out the angles and squash them to closest % rectilinear direction. After that, the voxel sizes are then % added. % % This part is modified by Jeff Gunter. % if det(R) == 0 | ~isequal(R(find(R)), sum(R)') % det(R) == 0 is not a common trigger for this --- % R(find(R)) is a list of non-zero elements in R; if that % is straight (not oblique) then it should be the same as % columnwise summation. Could just as well have checked the % lengths of R(find(R)) and sum(R)' (which should be 3) % hdr.hist.old_affine = [ [R * diag([i j k]);[0 0 0]] [T;1] ]; R_sort = sort(abs(R(:))); R( find( abs(R) < tolerance*min(R_sort(end-2:end)) ) ) = 0; R = R * diag([i j k]); hdr.hist.new_affine = [ [R;[0 0 0]] [T;1] ]; if det(R) == 0 | ~isequal(R(find(R)), sum(R)') msg = [char(10) char(10) ' Non-orthogonal rotation or shearing ']; msg = [msg 'found inside the affine matrix' char(10)]; msg = [msg ' in this NIfTI file. You have 3 options:' char(10) char(10)]; msg = [msg ' 1. Using included ''reslice_nii.m'' program to reslice the NIfTI' char(10)]; msg = [msg ' file. I strongly recommand this, because it will not cause' char(10)]; msg = [msg ' negative effect, as long as you remember not to do slice' char(10)]; msg = [msg ' time correction after using ''reslice_nii.m''.' char(10) char(10)]; msg = [msg ' 2. Using included ''load_untouch_nii.m'' program to load image' char(10)]; msg = [msg ' without applying any affine geometric transformation or' char(10)]; msg = [msg ' voxel intensity scaling. This is only for people who want' char(10)]; msg = [msg ' to do some image processing regardless of image orientation' char(10)]; msg = [msg ' and to save data back with the same NIfTI header.' char(10) char(10)]; msg = [msg ' 3. Increasing the tolerance to allow more distortion in loaded' char(10)]; msg = [msg ' image, but I don''t suggest this.' char(10) char(10)]; msg = [msg ' To get help, please type:' char(10) char(10) ' help reslice_nii.m' char(10)]; msg = [msg ' help load_untouch_nii.m' char(10) ' help load_nii.m']; error(msg); end else R = R * diag([i j k]); end % 1st det(R) else affine_transform = 0; % no sform or qform transform end if affine_transform == 1 voxel_size = abs(sum(R,1)); inv_R = inv(R); originator = inv_R*(-T)+1; orient = get_orient(inv_R); % modify pixdim and originator % hdr.dime.pixdim(2:4) = voxel_size; hdr.hist.originator(1:3) = originator; % set sform or qform to non-use, because they have been % applied in xform_nii % hdr.hist.qform_code = 0; hdr.hist.sform_code = 0; end % apply space_unit to pixdim if not 1 (mm) % space_unit = get_units(hdr); if space_unit ~= 1 hdr.dime.pixdim(2:4) = hdr.dime.pixdim(2:4) * space_unit; % set space_unit of xyzt_units to millimeter, because % voxel_size has been re-scaled % hdr.dime.xyzt_units = char(bitset(hdr.dime.xyzt_units,1,0)); hdr.dime.xyzt_units = char(bitset(hdr.dime.xyzt_units,2,1)); hdr.dime.xyzt_units = char(bitset(hdr.dime.xyzt_units,3,0)); end hdr.dime.pixdim = abs(hdr.dime.pixdim); return; % change_hdr %----------------------------------------------------------------------- function orient = get_orient(R) orient = []; for i = 1:3 switch find(R(i,:)) * sign(sum(R(i,:))) case 1 orient = [orient 1]; % Left to Right case 2 orient = [orient 2]; % Posterior to Anterior case 3 orient = [orient 3]; % Inferior to Superior case -1 orient = [orient 4]; % Right to Left case -2 orient = [orient 5]; % Anterior to Posterior case -3 orient = [orient 6]; % Superior to Inferior end end return; % get_orient %----------------------------------------------------------------------- function [space_unit, time_unit] = get_units(hdr) switch bitand(hdr.dime.xyzt_units, 7) % mask with 0x07 case 1 space_unit = 1e+3; % meter, m case 3 space_unit = 1e-3; % micrometer, um otherwise space_unit = 1; % millimeter, mm end switch bitand(hdr.dime.xyzt_units, 56) % mask with 0x38 case 16 time_unit = 1e-3; % millisecond, ms case 24 time_unit = 1e-6; % microsecond, us otherwise time_unit = 1; % second, s end return; % get_units
github
sunhongfu/scripts-master
make_ana.m
.m
scripts-master/cs-phase/_src/_nii/make_ana.m
5,665
utf_8
37d574b277823f941138c9548127d720
% Make ANALYZE 7.5 data structure specified by a 3D or 4D matrix. % Optional parameters can also be included, such as: voxel_size, % origin, datatype, and description. % % Once the ANALYZE structure is made, it can be saved into ANALYZE 7.5 % format data file using "save_untouch_nii" command (for more detail, % type: help save_untouch_nii). % % Usage: ana = make_ana(img, [voxel_size], [origin], [datatype], [description]) % % Where: % % img: a 3D matrix [x y z], or a 4D matrix with time % series [x y z t]. When image is in RGB format, % make sure that the size of 4th dimension is % always 3 (i.e. [R G B]). In that case, make % sure that you must specify RGB datatype to 128. % % voxel_size (optional): Voxel size in millimeter for each % dimension. Default is [1 1 1]. % % origin (optional): The AC origin. Default is [0 0 0]. % % datatype (optional): Storage data type: % 2 - uint8, 4 - int16, 8 - int32, 16 - float32, % 64 - float64, 128 - RGB24 % Default will use the data type of 'img' matrix % For RGB image, you must specify it to 128. % % description (optional): Description of data. Default is ''. % % e.g.: % origin = [33 44 13]; datatype = 64; % ana = make_ana(img, [], origin, datatype); % default voxel_size % % ANALYZE 7.5 format: http://www.rotman-baycrest.on.ca/~jimmy/ANALYZE75.pdf % % - Jimmy Shen ([email protected]) % function ana = make_ana(varargin) ana.img = varargin{1}; dims = size(ana.img); dims = [4 dims ones(1,8)]; dims = dims(1:8); voxel_size = [0 ones(1,3) zeros(1,4)]; origin = zeros(1,5); descrip = ''; switch class(ana.img) case 'uint8' datatype = 2; case 'int16' datatype = 4; case 'int32' datatype = 8; case 'single' datatype = 16; case 'double' datatype = 64; otherwise error('Datatype is not supported by make_ana.'); end if nargin > 1 & ~isempty(varargin{2}) voxel_size(2:4) = double(varargin{2}); end if nargin > 2 & ~isempty(varargin{3}) origin(1:3) = double(varargin{3}); end if nargin > 3 & ~isempty(varargin{4}) datatype = double(varargin{4}); if datatype == 128 | datatype == 511 dims(5) = []; dims = [dims 1]; end end if nargin > 4 & ~isempty(varargin{5}) descrip = varargin{5}; end if ndims(ana.img) > 4 error('NIfTI only allows a maximum of 4 Dimension matrix.'); end maxval = round(double(max(ana.img(:)))); minval = round(double(min(ana.img(:)))); ana.hdr = make_header(dims, voxel_size, origin, datatype, ... descrip, maxval, minval); ana.filetype = 0; ana.ext = []; ana.untouch = 1; switch ana.hdr.dime.datatype case 2 ana.img = uint8(ana.img); case 4 ana.img = int16(ana.img); case 8 ana.img = int32(ana.img); case 16 ana.img = single(ana.img); case 64 ana.img = double(ana.img); case 128 ana.img = uint8(ana.img); otherwise error('Datatype is not supported by make_ana.'); end return; % make_ana %--------------------------------------------------------------------- function hdr = make_header(dims, voxel_size, origin, datatype, ... descrip, maxval, minval) hdr.hk = header_key; hdr.dime = image_dimension(dims, voxel_size, datatype, maxval, minval); hdr.hist = data_history(origin, descrip); return; % make_header %--------------------------------------------------------------------- function hk = header_key hk.sizeof_hdr = 348; % must be 348! hk.data_type = ''; hk.db_name = ''; hk.extents = 0; hk.session_error = 0; hk.regular = 'r'; hk.hkey_un0 = '0'; return; % header_key %--------------------------------------------------------------------- function dime = image_dimension(dims, voxel_size, datatype, maxval, minval) dime.dim = dims; dime.vox_units = 'mm'; dime.cal_units = ''; dime.unused1 = 0; dime.datatype = datatype; switch dime.datatype case 2, dime.bitpix = 8; precision = 'uint8'; case 4, dime.bitpix = 16; precision = 'int16'; case 8, dime.bitpix = 32; precision = 'int32'; case 16, dime.bitpix = 32; precision = 'float32'; case 64, dime.bitpix = 64; precision = 'float64'; case 128 dime.bitpix = 24; precision = 'uint8'; otherwise error('Datatype is not supported by make_ana.'); end dime.dim_un0 = 0; dime.pixdim = voxel_size; dime.vox_offset = 0; dime.roi_scale = 1; dime.funused1 = 0; dime.funused2 = 0; dime.cal_max = 0; dime.cal_min = 0; dime.compressed = 0; dime.verified = 0; dime.glmax = maxval; dime.glmin = minval; return; % image_dimension %--------------------------------------------------------------------- function hist = data_history(origin, descrip) hist.descrip = descrip; hist.aux_file = 'none'; hist.orient = 0; hist.originator = origin; hist.generated = ''; hist.scannum = ''; hist.patient_id = ''; hist.exp_date = ''; hist.exp_time = ''; hist.hist_un0 = ''; hist.views = 0; hist.vols_added = 0; hist.start_field = 0; hist.field_skip = 0; hist.omax = 0; hist.omin = 0; hist.smax = 0; hist.smin = 0; return; % data_history
github
sunhongfu/scripts-master
extra_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/extra_nii_hdr.m
8,085
utf_8
4f76a8a66736025a0acf3efa15a2d2aa
% Decode extra NIFTI header information into hdr.extra % % Usage: hdr = extra_nii_hdr(hdr) % % hdr can be obtained from load_nii_hdr % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function hdr = extra_nii_hdr(hdr) switch hdr.dime.datatype case 1 extra.NIFTI_DATATYPES = 'DT_BINARY'; case 2 extra.NIFTI_DATATYPES = 'DT_UINT8'; case 4 extra.NIFTI_DATATYPES = 'DT_INT16'; case 8 extra.NIFTI_DATATYPES = 'DT_INT32'; case 16 extra.NIFTI_DATATYPES = 'DT_FLOAT32'; case 32 extra.NIFTI_DATATYPES = 'DT_COMPLEX64'; case 64 extra.NIFTI_DATATYPES = 'DT_FLOAT64'; case 128 extra.NIFTI_DATATYPES = 'DT_RGB24'; case 256 extra.NIFTI_DATATYPES = 'DT_INT8'; case 512 extra.NIFTI_DATATYPES = 'DT_UINT16'; case 768 extra.NIFTI_DATATYPES = 'DT_UINT32'; case 1024 extra.NIFTI_DATATYPES = 'DT_INT64'; case 1280 extra.NIFTI_DATATYPES = 'DT_UINT64'; case 1536 extra.NIFTI_DATATYPES = 'DT_FLOAT128'; case 1792 extra.NIFTI_DATATYPES = 'DT_COMPLEX128'; case 2048 extra.NIFTI_DATATYPES = 'DT_COMPLEX256'; otherwise extra.NIFTI_DATATYPES = 'DT_UNKNOWN'; end switch hdr.dime.intent_code case 2 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CORREL'; case 3 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST'; case 4 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST'; case 5 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ZSCORE'; case 6 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ'; case 7 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BETA'; case 8 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BINOM'; case 9 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GAMMA'; case 10 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POISSON'; case 11 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NORMAL'; case 12 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST_NONC'; case 13 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ_NONC'; case 14 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGISTIC'; case 15 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LAPLACE'; case 16 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_UNIFORM'; case 17 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST_NONC'; case 18 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_WEIBULL'; case 19 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHI'; case 20 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_INVGAUSS'; case 21 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_EXTVAL'; case 22 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_PVAL'; case 23 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGPVAL'; case 24 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOG10PVAL'; case 1001 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ESTIMATE'; case 1002 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LABEL'; case 1003 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NEURONAME'; case 1004 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GENMATRIX'; case 1005 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_SYMMATRIX'; case 1006 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DISPVECT'; case 1007 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_VECTOR'; case 1008 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POINTSET'; case 1009 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TRIANGLE'; case 1010 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_QUATERNION'; case 1011 extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DIMLESS'; otherwise extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NONE'; end extra.NIFTI_INTENT_NAMES = hdr.hist.intent_name; if hdr.hist.sform_code > 0 switch hdr.hist.sform_code case 1 extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT'; case 2 extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT'; case 3 extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_TALAIRACH'; case 4 extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_MNI_152'; otherwise extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; end extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; elseif hdr.hist.qform_code > 0 extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; switch hdr.hist.qform_code case 1 extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT'; case 2 extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT'; case 3 extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_TALAIRACH'; case 4 extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_MNI_152'; otherwise extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; end else extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN'; end switch bitand(hdr.dime.xyzt_units, 7) % mask with 0x07 case 1 extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_METER'; case 2 extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MM'; % millimeter case 3 extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MICRO'; otherwise extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_UNKNOWN'; end switch bitand(hdr.dime.xyzt_units, 56) % mask with 0x38 case 8 extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_SEC'; case 16 extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_MSEC'; case 24 extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_USEC'; % microsecond otherwise extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_UNKNOWN'; end switch hdr.dime.xyzt_units case 32 extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_HZ'; case 40 extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_PPM'; % part per million case 48 extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_RADS'; % radians per second otherwise extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_UNKNOWN'; end % MRI-specific spatial and temporal information % dim_info = hdr.hk.dim_info; extra.NIFTI_FREQ_DIM = bitand(dim_info, 3); extra.NIFTI_PHASE_DIM = bitand(bitshift(dim_info, -2), 3); extra.NIFTI_SLICE_DIM = bitand(bitshift(dim_info, -4), 3); % Check slice code % switch hdr.dime.slice_code case 1 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_INC'; % sequential increasing case 2 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_DEC'; % sequential decreasing case 3 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC'; % alternating increasing case 4 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC'; % alternating decreasing case 5 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC2'; % ALT_INC # 2 case 6 extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC2'; % ALT_DEC # 2 otherwise extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_UNKNOWN'; end % Check NIFTI version % if ~isempty(hdr.hist.magic) & strcmp(hdr.hist.magic(1),'n') & ... ( strcmp(hdr.hist.magic(2),'i') | strcmp(hdr.hist.magic(2),'+') ) & ... str2num(hdr.hist.magic(3)) >= 1 & str2num(hdr.hist.magic(3)) <= 9 extra.NIFTI_VERSION = str2num(hdr.hist.magic(3)); else extra.NIFTI_VERSION = 0; end % Check if data stored in the same file (*.nii) or separate % files (*.hdr/*.img) % if isempty(hdr.hist.magic) extra.NIFTI_ONEFILE = 0; else extra.NIFTI_ONEFILE = strcmp(hdr.hist.magic(2), '+'); end % Swap has been taken care of by checking whether sizeof_hdr is % 348 (machine is 'ieee-le' or 'ieee-be' etc) % % extra.NIFTI_NEEDS_SWAP = (hdr.dime.dim(1) < 0 | hdr.dime.dim(1) > 7); % Check NIFTI header struct contains a 5th (vector) dimension % if hdr.dime.dim(1) > 4 & hdr.dime.dim(6) > 1 extra.NIFTI_5TH_DIM = hdr.dime.dim(6); else extra.NIFTI_5TH_DIM = 0; end hdr.extra = extra; return; % extra_nii_hdr
github
sunhongfu/scripts-master
rri_xhair.m
.m
scripts-master/cs-phase/_src/_nii/rri_xhair.m
2,300
utf_8
95954b8cd43e01fba5c4b2f335be1780
% rri_xhair: create a pair of full_cross_hair at point [x y] in % axes h_ax, and return xhair struct % % Usage: xhair = rri_xhair([x y], xhair, h_ax); % % If omit xhair, rri_xhair will create a pair of xhair; otherwise, % rri_xhair will update the xhair. If omit h_ax, current axes will % be used. % % 24-nov-2003 jimmy ([email protected]) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function xhair = rri_xhair(varargin) if nargin == 0 error('Please enter a point position as first argument'); return; end if nargin > 0 p = varargin{1}; if ~isnumeric(p) | length(p) ~= 2 error('Invalid point position'); return; else xhair = []; end end if nargin > 1 xhair = varargin{2}; if ~isempty(xhair) if ~isstruct(xhair) error('Invalid xhair struct'); return; elseif ~isfield(xhair,'lx') | ~isfield(xhair,'ly') error('Invalid xhair struct'); return; elseif ~ishandle(xhair.lx) | ~ishandle(xhair.ly) error('Invalid xhair struct'); return; end lx = xhair.lx; ly = xhair.ly; else lx = []; ly = []; end end if nargin > 2 h_ax = varargin{3}; if ~ishandle(h_ax) error('Invalid axes handle'); return; elseif ~strcmp(lower(get(h_ax,'type')), 'axes') error('Invalid axes handle'); return; end else h_ax = gca; end x_range = get(h_ax,'xlim'); y_range = get(h_ax,'ylim'); if ~isempty(xhair) set(lx, 'ydata', [p(2) p(2)]); set(ly, 'xdata', [p(1) p(1)]); set(h_ax, 'selected', 'on'); set(h_ax, 'selected', 'off'); else figure(get(h_ax,'parent')); axes(h_ax); xhair.lx = line('xdata', x_range, 'ydata', [p(2) p(2)], ... 'zdata', [11 11], 'color', [1 0 0], 'hittest', 'off'); xhair.ly = line('xdata', [p(1) p(1)], 'ydata', y_range, ... 'zdata', [11 11], 'color', [1 0 0], 'hittest', 'off'); end set(h_ax,'xlim',x_range); set(h_ax,'ylim',y_range); return;
github
sunhongfu/scripts-master
save_untouch_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/save_untouch_nii_hdr.m
8,721
utf_8
0d396eaeebb6114f24d56ab74a8299cf
% internal function % - Jimmy Shen ([email protected]) function save_nii_hdr(hdr, fid) if ~isequal(hdr.hk.sizeof_hdr,348), error('hdr.hk.sizeof_hdr must be 348.'); end write_header(hdr, fid); return; % save_nii_hdr %--------------------------------------------------------------------- function write_header(hdr, fid) % Original header structures % struct dsr /* dsr = hdr */ % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ header_key(fid, hdr.hk); image_dimension(fid, hdr.dime); data_history(fid, hdr.hist); % check the file size is 348 bytes % fbytes = ftell(fid); if ~isequal(fbytes,348), msg = sprintf('Header size is not 348 bytes.'); warning(msg); end return; % write_header %--------------------------------------------------------------------- function header_key(fid, hk) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char dim_info; % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ fwrite(fid, hk.sizeof_hdr(1), 'int32'); % must be 348. % data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars from left % fwrite(fid, data_type(1:10), 'uchar'); pad = zeros(1, 10-length(hk.data_type)); hk.data_type = [hk.data_type char(pad)]; fwrite(fid, hk.data_type(1:10), 'uchar'); % db_name = sprintf('%-18s', hk.db_name); % ensure it is 18 chars from left % fwrite(fid, db_name(1:18), 'uchar'); pad = zeros(1, 18-length(hk.db_name)); hk.db_name = [hk.db_name char(pad)]; fwrite(fid, hk.db_name(1:18), 'uchar'); fwrite(fid, hk.extents(1), 'int32'); fwrite(fid, hk.session_error(1), 'int16'); fwrite(fid, hk.regular(1), 'uchar'); % might be uint8 % fwrite(fid, hk.hkey_un0(1), 'uchar'); % fwrite(fid, hk.hkey_un0(1), 'uint8'); fwrite(fid, hk.dim_info(1), 'uchar'); return; % header_key %--------------------------------------------------------------------- function image_dimension(fid, dime) % Original header structures % struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % float intent_p1; % char vox_units[4]; /* 16 + 4 */ % float intent_p2; % char cal_units[8]; /* 20 + 4 */ % float intent_p3; % char cal_units[8]; /* 24 + 4 */ % short int intent_code; % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int slice_start; % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width % pixdim[2] - voxel height % pixdim[3] - interslice distance % pixdim[4] - volume timing, in msec % ..etc % */ % float vox_offset; /* 68 + 4 */ % float scl_slope; % float roi_scale; /* 72 + 4 */ % float scl_inter; % float funused1; /* 76 + 4 */ % short slice_end; % float funused2; /* 80 + 2 */ % char slice_code; % float funused2; /* 82 + 1 */ % char xyzt_units; % float funused2; /* 83 + 1 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % float slice_duration; % int compressed; /* 92 + 4 */ % float toffset; % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ fwrite(fid, dime.dim(1:8), 'int16'); fwrite(fid, dime.intent_p1(1), 'float32'); fwrite(fid, dime.intent_p2(1), 'float32'); fwrite(fid, dime.intent_p3(1), 'float32'); fwrite(fid, dime.intent_code(1), 'int16'); fwrite(fid, dime.datatype(1), 'int16'); fwrite(fid, dime.bitpix(1), 'int16'); fwrite(fid, dime.slice_start(1), 'int16'); fwrite(fid, dime.pixdim(1:8), 'float32'); fwrite(fid, dime.vox_offset(1), 'float32'); fwrite(fid, dime.scl_slope(1), 'float32'); fwrite(fid, dime.scl_inter(1), 'float32'); fwrite(fid, dime.slice_end(1), 'int16'); fwrite(fid, dime.slice_code(1), 'uchar'); fwrite(fid, dime.xyzt_units(1), 'uchar'); fwrite(fid, dime.cal_max(1), 'float32'); fwrite(fid, dime.cal_min(1), 'float32'); fwrite(fid, dime.slice_duration(1), 'float32'); fwrite(fid, dime.toffset(1), 'float32'); fwrite(fid, dime.glmax(1), 'int32'); fwrite(fid, dime.glmin(1), 'int32'); return; % image_dimension %--------------------------------------------------------------------- function data_history(fid, hist) % Original header structures %struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % short int qform_code; /* 104 + 2 */ % short int sform_code; /* 106 + 2 */ % float quatern_b; /* 108 + 4 */ % float quatern_c; /* 112 + 4 */ % float quatern_d; /* 116 + 4 */ % float qoffset_x; /* 120 + 4 */ % float qoffset_y; /* 124 + 4 */ % float qoffset_z; /* 128 + 4 */ % float srow_x[4]; /* 132 + 16 */ % float srow_y[4]; /* 148 + 16 */ % float srow_z[4]; /* 164 + 16 */ % char intent_name[16]; /* 180 + 16 */ % char magic[4]; % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ % descrip = sprintf('%-80s', hist.descrip); % 80 chars from left % fwrite(fid, descrip(1:80), 'uchar'); pad = zeros(1, 80-length(hist.descrip)); hist.descrip = [hist.descrip char(pad)]; fwrite(fid, hist.descrip(1:80), 'uchar'); % aux_file = sprintf('%-24s', hist.aux_file); % 24 chars from left % fwrite(fid, aux_file(1:24), 'uchar'); pad = zeros(1, 24-length(hist.aux_file)); hist.aux_file = [hist.aux_file char(pad)]; fwrite(fid, hist.aux_file(1:24), 'uchar'); fwrite(fid, hist.qform_code, 'int16'); fwrite(fid, hist.sform_code, 'int16'); fwrite(fid, hist.quatern_b, 'float32'); fwrite(fid, hist.quatern_c, 'float32'); fwrite(fid, hist.quatern_d, 'float32'); fwrite(fid, hist.qoffset_x, 'float32'); fwrite(fid, hist.qoffset_y, 'float32'); fwrite(fid, hist.qoffset_z, 'float32'); fwrite(fid, hist.srow_x(1:4), 'float32'); fwrite(fid, hist.srow_y(1:4), 'float32'); fwrite(fid, hist.srow_z(1:4), 'float32'); % intent_name = sprintf('%-16s', hist.intent_name); % 16 chars from left % fwrite(fid, intent_name(1:16), 'uchar'); pad = zeros(1, 16-length(hist.intent_name)); hist.intent_name = [hist.intent_name char(pad)]; fwrite(fid, hist.intent_name(1:16), 'uchar'); % magic = sprintf('%-4s', hist.magic); % 4 chars from left % fwrite(fid, magic(1:4), 'uchar'); pad = zeros(1, 4-length(hist.magic)); hist.magic = [hist.magic char(pad)]; fwrite(fid, hist.magic(1:4), 'uchar'); return; % data_history
github
sunhongfu/scripts-master
expand_nii_scan.m
.m
scripts-master/cs-phase/_src/_nii/expand_nii_scan.m
1,381
utf_8
0715d668d046bcc608ea78cd0c2089bd
% Expand a multiple-scan NIFTI file into multiple single-scan NIFTI files % % Usage: expand_nii_scan(multi_scan_filename, [img_idx], [path_to_save]) % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function expand_nii_scan(filename, img_idx, newpath) v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); else gzFile = 1; end end if ~exist('newpath','var') | isempty(newpath), newpath = pwd; end if ~exist('img_idx','var') | isempty(img_idx), img_idx = 1:get_nii_frame(filename); end for i=img_idx nii_i = load_untouch_nii(filename, i); fn = [nii_i.fileprefix '_' sprintf('%04d',i)]; pnfn = fullfile(newpath, fn); if exist('gzFile', 'var') pnfn = [pnfn '.nii.gz']; end save_untouch_nii(nii_i, pnfn); end return; % expand_nii_scan
github
sunhongfu/scripts-master
load_untouch_header_only.m
.m
scripts-master/cs-phase/_src/_nii/load_untouch_header_only.m
7,255
utf_8
f1210f851ab6610e7656121194cb5c8b
% Load NIfTI / Analyze header without applying any appropriate affine % geometric transform or voxel intensity scaling. It is equivalent to % hdr field when using load_untouch_nii to load dataset. Support both % *.nii and *.hdr file extension. If file extension is not provided, % *.hdr will be used as default. % % Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename) % % filename - NIfTI / Analyze file name. % % Returned values: % % header - struct with NIfTI / Analyze header fields. % % ext - NIfTI extension if it is not empty. % % filetype - 0 for Analyze format (*.hdr/*.img); % 1 for NIFTI format in 2 files (*.hdr/*.img); % 2 for NIFTI format in 1 file (*.nii). % % machine - a string, see below for details. The default here is 'ieee-le'. % % 'native' or 'n' - local machine format - the default % 'ieee-le' or 'l' - IEEE floating point with little-endian % byte ordering % 'ieee-be' or 'b' - IEEE floating point with big-endian % byte ordering % 'vaxd' or 'd' - VAX D floating point and VAX ordering % 'vaxg' or 'g' - VAX G floating point and VAX ordering % 'cray' or 'c' - Cray floating point with big-endian % byte ordering % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian % byte ordering and 64 bit long data type % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte % ordering and 64 bit long data type. % % Part of this file is copied and modified from: % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools % % NIFTI data format can be found on: http://nifti.nimh.nih.gov % % - Jimmy Shen ([email protected]) % function [hdr, ext, filetype, machine] = load_untouch_header_only(filename) if ~exist('filename','var') error('Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename)'); end v = version; % Check file extension. If .gz, unpack it into temp folder % if length(filename) > 2 & strcmp(filename(end-2:end), '.gz') if ~strcmp(filename(end-6:end), '.img.gz') & ... ~strcmp(filename(end-6:end), '.hdr.gz') & ... ~strcmp(filename(end-6:end), '.nii.gz') error('Please check filename.'); end if str2num(v(1:3)) < 7.1 | ~usejava('jvm') error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.'); elseif strcmp(filename(end-6:end), '.img.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.hdr.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.hdr.gz') filename1 = filename; filename2 = filename; filename2(end-6:end) = ''; filename2 = [filename2, '.img.gz']; tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename1 = gunzip(filename1, tmpDir); filename2 = gunzip(filename2, tmpDir); filename = char(filename1); % convert from cell to string elseif strcmp(filename(end-6:end), '.nii.gz') tmpDir = tempname; mkdir(tmpDir); gzFileName = filename; filename = gunzip(filename, tmpDir); filename = char(filename); % convert from cell to string end end % Read the dataset header % [hdr, filetype, fileprefix, machine] = load_nii_hdr(filename); if filetype == 0 hdr = load_untouch0_nii_hdr(fileprefix, machine); ext = []; else hdr = load_untouch_nii_hdr(fileprefix, machine, filetype); % Read the header extension % ext = load_nii_ext(filename); end % Set bitpix according to datatype % % /*Acceptable values for datatype are*/ % % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN % 1 Binary (ubit1, bitpix=1) % DT_BINARY % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8 % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16 % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32 % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32 % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64 % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64 % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24 % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8 % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96 % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16 % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32 % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64 % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64 % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128 % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128 % switch hdr.dime.datatype case 1, hdr.dime.bitpix = 1; precision = 'ubit1'; case 2, hdr.dime.bitpix = 8; precision = 'uint8'; case 4, hdr.dime.bitpix = 16; precision = 'int16'; case 8, hdr.dime.bitpix = 32; precision = 'int32'; case 16, hdr.dime.bitpix = 32; precision = 'float32'; case 32, hdr.dime.bitpix = 64; precision = 'float32'; case 64, hdr.dime.bitpix = 64; precision = 'float64'; case 128, hdr.dime.bitpix = 24; precision = 'uint8'; case 256 hdr.dime.bitpix = 8; precision = 'int8'; case 511 hdr.dime.bitpix = 96; precision = 'float32'; case 512 hdr.dime.bitpix = 16; precision = 'uint16'; case 768 hdr.dime.bitpix = 32; precision = 'uint32'; case 1024 hdr.dime.bitpix = 64; precision = 'int64'; case 1280 hdr.dime.bitpix = 64; precision = 'uint64'; case 1792, hdr.dime.bitpix = 128; precision = 'float64'; otherwise error('This datatype is not supported'); end tmp = hdr.dime.dim(2:end); tmp(find(tmp < 1)) = 1; hdr.dime.dim(2:end) = tmp; % Clean up after gunzip % if exist('gzFileName', 'var') rmdir(tmpDir,'s'); end return % load_untouch_header_only
github
sunhongfu/scripts-master
bipolar.m
.m
scripts-master/cs-phase/_src/_nii/bipolar.m
2,239
utf_8
c860ec93d96b6ab636c985280d79958d
%BIPOLAR returns an M-by-3 matrix containing a blue-red colormap, in % in which red stands for positive, blue stands for negative, % and white stands for 0. % % Usage: cmap = bipolar(M, lo, hi, contrast); or cmap = bipolar; % % cmap: output M-by-3 matrix for BIPOLAR colormap. % M: number of shades in the colormap. By default, it is the % same length as the current colormap. % lo: the lowest value to represent. % hi: the highest value to represent. % % Inspired from the LORETA PASCAL program: % http://www.unizh.ch/keyinst/NewLORETA % % [email protected] % %---------------------------------------------------------------- function cmap = bipolar(M, lo, hi, contrast) if ~exist('contrast','var') contrast = 128; end if ~exist('lo','var') lo = -1; end if ~exist('hi','var') hi = 1; end if ~exist('M','var') cmap = colormap; M = size(cmap,1); end steepness = 10 ^ (1 - (contrast-1)/127); pos_infs = 1e-99; neg_infs = -1e-99; doubleredc = []; doublebluec = []; if lo >= 0 % all positive if lo == 0 lo = pos_infs; end for i=linspace(hi/M, hi, M) t = exp(log(i/hi)*steepness); doubleredc = [doubleredc; [(1-t)+t,(1-t)+0,(1-t)+0]]; end cmap = doubleredc; elseif hi <= 0 % all negative if hi == 0 hi = neg_infs; end for i=linspace(abs(lo)/M, abs(lo), M) t = exp(log(i/abs(lo))*steepness); doublebluec = [doublebluec; [(1-t)+0,(1-t)+0,(1-t)+t]]; end cmap = flipud(doublebluec); else if hi > abs(lo) maxc = hi; else maxc = abs(lo); end for i=linspace(maxc/M, hi, round(M*hi/(hi-lo))) t = exp(log(i/maxc)*steepness); doubleredc = [doubleredc; [(1-t)+t,(1-t)+0,(1-t)+0]]; end for i=linspace(maxc/M, abs(lo), round(M*abs(lo)/(hi-lo))) t = exp(log(i/maxc)*steepness); doublebluec = [doublebluec; [(1-t)+0,(1-t)+0,(1-t)+t]]; end cmap = [flipud(doublebluec); doubleredc]; end return; % bipolar
github
sunhongfu/scripts-master
save_nii_hdr.m
.m
scripts-master/cs-phase/_src/_nii/save_nii_hdr.m
9,497
utf_8
66a99df0cb0f3c1f44c6e36dcd13cddf
% internal function % - Jimmy Shen ([email protected]) function save_nii_hdr(hdr, fid) if ~exist('hdr','var') | ~exist('fid','var') error('Usage: save_nii_hdr(hdr, fid)'); end if ~isequal(hdr.hk.sizeof_hdr,348), error('hdr.hk.sizeof_hdr must be 348.'); end if hdr.hist.qform_code == 0 & hdr.hist.sform_code == 0 hdr.hist.sform_code = 1; hdr.hist.srow_x(1) = hdr.dime.pixdim(2); hdr.hist.srow_x(2) = 0; hdr.hist.srow_x(3) = 0; hdr.hist.srow_y(1) = 0; hdr.hist.srow_y(2) = hdr.dime.pixdim(3); hdr.hist.srow_y(3) = 0; hdr.hist.srow_z(1) = 0; hdr.hist.srow_z(2) = 0; hdr.hist.srow_z(3) = hdr.dime.pixdim(4); hdr.hist.srow_x(4) = (1-hdr.hist.originator(1))*hdr.dime.pixdim(2); hdr.hist.srow_y(4) = (1-hdr.hist.originator(2))*hdr.dime.pixdim(3); hdr.hist.srow_z(4) = (1-hdr.hist.originator(3))*hdr.dime.pixdim(4); end write_header(hdr, fid); return; % save_nii_hdr %--------------------------------------------------------------------- function write_header(hdr, fid) % Original header structures % struct dsr /* dsr = hdr */ % { % struct header_key hk; /* 0 + 40 */ % struct image_dimension dime; /* 40 + 108 */ % struct data_history hist; /* 148 + 200 */ % }; /* total= 348 bytes*/ header_key(fid, hdr.hk); image_dimension(fid, hdr.dime); data_history(fid, hdr.hist); % check the file size is 348 bytes % fbytes = ftell(fid); if ~isequal(fbytes,348), msg = sprintf('Header size is not 348 bytes.'); warning(msg); end return; % write_header %--------------------------------------------------------------------- function header_key(fid, hk) fseek(fid,0,'bof'); % Original header structures % struct header_key /* header key */ % { /* off + size */ % int sizeof_hdr /* 0 + 4 */ % char data_type[10]; /* 4 + 10 */ % char db_name[18]; /* 14 + 18 */ % int extents; /* 32 + 4 */ % short int session_error; /* 36 + 2 */ % char regular; /* 38 + 1 */ % char dim_info; % char hkey_un0; /* 39 + 1 */ % }; /* total=40 bytes */ fwrite(fid, hk.sizeof_hdr(1), 'int32'); % must be 348. % data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars from left % fwrite(fid, data_type(1:10), 'uchar'); pad = zeros(1, 10-length(hk.data_type)); hk.data_type = [hk.data_type char(pad)]; fwrite(fid, hk.data_type(1:10), 'uchar'); % db_name = sprintf('%-18s', hk.db_name); % ensure it is 18 chars from left % fwrite(fid, db_name(1:18), 'uchar'); pad = zeros(1, 18-length(hk.db_name)); hk.db_name = [hk.db_name char(pad)]; fwrite(fid, hk.db_name(1:18), 'uchar'); fwrite(fid, hk.extents(1), 'int32'); fwrite(fid, hk.session_error(1), 'int16'); fwrite(fid, hk.regular(1), 'uchar'); % might be uint8 % fwrite(fid, hk.hkey_un0(1), 'uchar'); % fwrite(fid, hk.hkey_un0(1), 'uint8'); fwrite(fid, hk.dim_info(1), 'uchar'); return; % header_key %--------------------------------------------------------------------- function image_dimension(fid, dime) % Original header structures % struct image_dimension % { /* off + size */ % short int dim[8]; /* 0 + 16 */ % float intent_p1; % char vox_units[4]; /* 16 + 4 */ % float intent_p2; % char cal_units[8]; /* 20 + 4 */ % float intent_p3; % char cal_units[8]; /* 24 + 4 */ % short int intent_code; % short int unused1; /* 28 + 2 */ % short int datatype; /* 30 + 2 */ % short int bitpix; /* 32 + 2 */ % short int slice_start; % short int dim_un0; /* 34 + 2 */ % float pixdim[8]; /* 36 + 32 */ % /* % pixdim[] specifies the voxel dimensions: % pixdim[1] - voxel width % pixdim[2] - voxel height % pixdim[3] - interslice distance % pixdim[4] - volume timing, in msec % ..etc % */ % float vox_offset; /* 68 + 4 */ % float scl_slope; % float roi_scale; /* 72 + 4 */ % float scl_inter; % float funused1; /* 76 + 4 */ % short slice_end; % float funused2; /* 80 + 2 */ % char slice_code; % float funused2; /* 82 + 1 */ % char xyzt_units; % float funused2; /* 83 + 1 */ % float cal_max; /* 84 + 4 */ % float cal_min; /* 88 + 4 */ % float slice_duration; % int compressed; /* 92 + 4 */ % float toffset; % int verified; /* 96 + 4 */ % int glmax; /* 100 + 4 */ % int glmin; /* 104 + 4 */ % }; /* total=108 bytes */ fwrite(fid, dime.dim(1:8), 'int16'); fwrite(fid, dime.intent_p1(1), 'float32'); fwrite(fid, dime.intent_p2(1), 'float32'); fwrite(fid, dime.intent_p3(1), 'float32'); fwrite(fid, dime.intent_code(1), 'int16'); fwrite(fid, dime.datatype(1), 'int16'); fwrite(fid, dime.bitpix(1), 'int16'); fwrite(fid, dime.slice_start(1), 'int16'); fwrite(fid, dime.pixdim(1:8), 'float32'); fwrite(fid, dime.vox_offset(1), 'float32'); fwrite(fid, dime.scl_slope(1), 'float32'); fwrite(fid, dime.scl_inter(1), 'float32'); fwrite(fid, dime.slice_end(1), 'int16'); fwrite(fid, dime.slice_code(1), 'uchar'); fwrite(fid, dime.xyzt_units(1), 'uchar'); fwrite(fid, dime.cal_max(1), 'float32'); fwrite(fid, dime.cal_min(1), 'float32'); fwrite(fid, dime.slice_duration(1), 'float32'); fwrite(fid, dime.toffset(1), 'float32'); fwrite(fid, dime.glmax(1), 'int32'); fwrite(fid, dime.glmin(1), 'int32'); return; % image_dimension %--------------------------------------------------------------------- function data_history(fid, hist) % Original header structures %struct data_history % { /* off + size */ % char descrip[80]; /* 0 + 80 */ % char aux_file[24]; /* 80 + 24 */ % short int qform_code; /* 104 + 2 */ % short int sform_code; /* 106 + 2 */ % float quatern_b; /* 108 + 4 */ % float quatern_c; /* 112 + 4 */ % float quatern_d; /* 116 + 4 */ % float qoffset_x; /* 120 + 4 */ % float qoffset_y; /* 124 + 4 */ % float qoffset_z; /* 128 + 4 */ % float srow_x[4]; /* 132 + 16 */ % float srow_y[4]; /* 148 + 16 */ % float srow_z[4]; /* 164 + 16 */ % char intent_name[16]; /* 180 + 16 */ % char magic[4]; % int smin; /* 196 + 4 */ % }; /* total=200 bytes */ % descrip = sprintf('%-80s', hist.descrip); % 80 chars from left % fwrite(fid, descrip(1:80), 'uchar'); pad = zeros(1, 80-length(hist.descrip)); hist.descrip = [hist.descrip char(pad)]; fwrite(fid, hist.descrip(1:80), 'uchar'); % aux_file = sprintf('%-24s', hist.aux_file); % 24 chars from left % fwrite(fid, aux_file(1:24), 'uchar'); pad = zeros(1, 24-length(hist.aux_file)); hist.aux_file = [hist.aux_file char(pad)]; fwrite(fid, hist.aux_file(1:24), 'uchar'); fwrite(fid, hist.qform_code, 'int16'); fwrite(fid, hist.sform_code, 'int16'); fwrite(fid, hist.quatern_b, 'float32'); fwrite(fid, hist.quatern_c, 'float32'); fwrite(fid, hist.quatern_d, 'float32'); fwrite(fid, hist.qoffset_x, 'float32'); fwrite(fid, hist.qoffset_y, 'float32'); fwrite(fid, hist.qoffset_z, 'float32'); fwrite(fid, hist.srow_x(1:4), 'float32'); fwrite(fid, hist.srow_y(1:4), 'float32'); fwrite(fid, hist.srow_z(1:4), 'float32'); % intent_name = sprintf('%-16s', hist.intent_name); % 16 chars from left % fwrite(fid, intent_name(1:16), 'uchar'); pad = zeros(1, 16-length(hist.intent_name)); hist.intent_name = [hist.intent_name char(pad)]; fwrite(fid, hist.intent_name(1:16), 'uchar'); % magic = sprintf('%-4s', hist.magic); % 4 chars from left % fwrite(fid, magic(1:4), 'uchar'); pad = zeros(1, 4-length(hist.magic)); hist.magic = [hist.magic char(pad)]; fwrite(fid, hist.magic(1:4), 'uchar'); return; % data_history
github
sunhongfu/scripts-master
pocs.m
.m
scripts-master/cs-phase/_src/_PF/pocs.m
32,075
utf_8
7902088f7cb0a941557fee7fc7ec700d
function [im, kspFull] = pocs( ksp, iter, watchProgress ) %Partial-Fourier Reconstruction with POCS % % [im, kspFull] = pocs( kspIn, iter, watchProgr ) % % === Input === % % kspIn: Reduced Cartesian MRI Data-Set % Any dimension may be reduced, % but only one reduction dim. is allowed due to Physics/Math. % % Allowed shapes for kspIn are... % ... Ny x Nx % ... Nc x Ny x Nx % ... Nc x Ny x Nx x Nz % % With Nc == number of receive Channels / Coils. % % kspIn can either be a zero-padded array, so the partial Fourier property is obvious. % Or kspIn can be the measured data only, then we try to find k-space centre automagically % and create a zero-padded array with the full size, first. % Errors are however more likely to occur in the latter case. % % % iter: No. of iterations % (optional) default: iter = 20 % Try on your own if larger iter improves your results! % % watchProgr: true/false; Whether the progress of the reconstruction should % (optional) be monitored in an image window. % In 3D data, only the central partition will be shown. % % % === Output === % % im: Reconstructed Images (channels not combined) % % kspFull: Reconstructed full k-space data % % % % === About the code === % % (1) We find out whether input data is % a) already zero-filled or % b) the pure asymmetric dataset, only % % If b) is true, we zero-fill the data ourselves, which means we have to % determine the dimension first, in which the partial Fourier reduction was done. % We therefor find the position of the max. intensity in k-space which should % be identical to k-space centre. If the k-space centre is different from the % centre of the matrix, we know the partial Fourier dimension. % We then enlarge the matrix to its desired full size and fill the new part % with zeros. % If a) was true, finding the partial Fourier dimension is easy: % It is the dimension with all the zeros. :-) % % (2) We create one low resolution image per channel/coil: % % We need a symmetrically sampled part around the central k-space. Think of a % small stripe of phase encoding lines in the central k-space. % We only use these symmetric data (setting the rest zero) to reconstruct % low-resolution images. In order to avoid Gibbs-Ringing, a Hamming-filter % with the width of the stripe is multiplied with the data. % Additionally, all the fully sampled dimensions get a Hamming filter, too, % since we increase SNR, reduce further Gibbs-ringing and do not lose much % resolution. % % (3) The phase of the low-resolution images is saved % % POCS uses the fact that k-space data of real objects (no imaginary part) % have a point symmetry: % S(-k) = S*(k) with k = (kx, ky, kz) % Our MRI objects are always complex, but we assume that phase variations % are due to coil sensitivities and B0-inhomgeneities, % which are both slowly varying (no high res. required). % Small-scale phase pertubations will decrease the reconstruction quality. % % (4) Reference phase is applied in image space % % We... % ... transform our zero-filled data to image space (IFFT) % ... remove the phase --> abs(image) % ... set the phase of our reference phase map --> image .* exp(1i.*phase) % ... transform back to k-space (FFT) % ... re-insert the measured data (self-consistency!) % ... goto "We..." % % Iterating through the above steps fills the missing k-space points % with reasonable values. % If the phase varies slowly and there is no aliasing, this works very well. % % Aliasing artifacts are very challenging for POCS. % So try to prevent aliasing in the first place (sufficient Field of View). % ========================================================================= % Original code by Martin Blaimer % * changed by Uvo Hoelscher % * changed by Michael Völker % -- auto-detect PF dimension % -- auto-find centre point/line/partition % -- accept zerofilled or "pure" data % -- for multichannel or plain 2D data (single-channel) % -- 2D and 3D % -- error handling % -- comments, comments, comments % -- added option to monitor progress % -- moved code to seperate functions % -- smooth transition between acquired signal and % reconstructed data % % Problems? Suggestions? % --> [email protected] % ========================================================================= % ( =================================================================== % Input Handling % if ~exist( 'ksp', 'var' ) || isempty(ksp) || ~isnumeric(ksp) error('pocs:input', 'First input must be Cartesian k-space data.') end if ~exist('iter','var') || isempty(iter) || numel(iter) ~= 1 || ~isnumeric(iter) iter = 20; end if ~exist('watchProgress','var') || isempty(watchProgress) || numel(watchProgress) ~= 1 || ~isfinite(watchProgress) watchProgress = false; else watchProgress = logical( watchProgress ); end Ndim = ndims( ksp ); if Ndim > 4 || Ndim < 2 error('pocs:shape','First input ''kspace'' should have one of these shapes:\n\n\t... Ny x Nx\n\t... Nc x Ny x Nx\n\t... Nc x Ny x Nx x Nz') end if Ndim == 2 % Ny x Nx ksp = reshape( ksp, [1 size(ksp)] ); % 1 x Ny x Nx --> now we have one channel... wasAddedCoilDim = true; Ndim = 3; else wasAddedCoilDim = false; end % read the properties of the data sz = size( ksp ); sz = sz(2:end); % the (k-)spatial size of the array (i.e. without channels) prec = class( ksp ); % single or double precision? % ) =================================================================== % First: Check the sampling pattern (which parts of input are actually data?) smplPtrn = reshape( sum(abs(ksp),1) ~= 0, sz); % Ny x Nx x Nz % ( =================================================================== % If input data is not yet zero-filled, do it here % if nnz(smplPtrn) == numel(smplPtrn) % only the sampled data were passed / |N|umber of |N|on |Z|ero elements [ ksp, pfDim, isUpper, isLower, Nsmp ] = zerofillPFdim( ksp, wasAddedCoilDim ); sz = size( ksp ); sz = sz(2:end); % ignore channels else [ pfDim, isUpper, isLower, Nsmp ] = detectPFdim( smplPtrn, wasAddedCoilDim ); end clear smplPtrn % ) =================================================================== if numel(sz) < 3 sz(3) = 1; end Ny = sz(1); Nx = sz(2); Nz = sz(3); % ( =================================================================== % Handle ugly problems. % if ~isUpper && ~isLower error('pocs:UnknownErrorFound', 'I thought we are partial Fourier, but things seem to make no sense... :-(') end % ) =================================================================== % ===================================================================== % % We can now be sure to operate with zero-padded data. % % ===================================================================== % initialize a cell of subscripts subs = { ':', ':', ':', ':' }; % all channels / all Ny / all Nx / all Nz % If the first entries are zero-filled (instead of the trailing ones), % flip the entries so we can treat them as if we pf'ed the first half of kspace. if isLower subs{pfDim+1} = sz(pfDim):-1:1; % ...esreveR ksp = ksp(subs{:}); % !ecaps-k si sihT subs{pfDim+1} = 1:sz(pfDim); % lalala, we didn't do anything... end % Find out which point is in the centre and which indices belong to the % symmetrically sampled part of k-space. [ centreLine, idxSym ] = findSymSampled( ksp, pfDim, Nsmp ); szSym = numel( idxSym ); % 2 * (Nsmp - centreLine) + 1 if isUpper % fprintf('Using %g points around point %g\n', szSym, centreLine ); else % fprintf('Using %g points around point %g\n', szSym, sz(pfDim)-centreLine+1 ); end % ( =================================================================== % build up a symmetric low-pass filter % filter = cast( 1, prec ); for d = 1:Ndim-1 reshRule = ones(1,Ndim); % how the filter will be reshaped if d ~= pfDim % Each standard dimension gets a simple low-pass filter filt1D = hamming( sz(d), 'periodic' ); else % our partial Fourier dimension gets an extra nice filter % create a narrow filter and remove everything else filt1D = zeros(sz(d), 1, prec); % full-size filter tmp = hann( szSym + 2, 'symmetric' ); % a very narrow window filt1D(idxSym) = tmp(2:end-1); % cut out the zeros at the edges (we have data there!) % take a look: %figure, plot(filt1D) end % reshape the filter according to the dimension it represents reshRule(d+1) = sz(d); filt1D = reshape( filt1D, reshRule ); filter = bsxfun( @times, filter, filt1D ); % iteratively build up a multidimensional filter end % ) =================================================================== % Apply the low-pass filter kspLowRes = bsxfun( @times, filter, ksp); clear filt1D filter reshRule idxSym % ( =================================================================== % prerequisites prior to the iteration loop % % Set everything up here, do computations that you don't have % to do in the loop, remove no longer needed variables... % % fftshift everything once before and after for-looping % => less overhead during iteration ksp = cmshiftnd( ksp, [0 sz/2] ); kspLowRes = cmshiftnd( kspLowRes, [0 sz/2] ); % reorder arrays such that the fft-dimensions come first % => faster memory access ksp = permute( ksp, [2 3 4 1] ); % Ny x Nx x Nz x Nc kspLowRes = permute( kspLowRes, [2 3 4 1] ); % subs = { subs{2}, subs{3}, subs{4}, subs{1} }; % calc. initial image and the reference phase map im = fft( fft( fft( conj(ksp), [], 1), [], 2), [], 3); % im's phase is wrong now, but we only want it's abs() to be correct phase = ifft(ifft(ifft( kspLowRes, [], 1), [], 2), [], 3); phase = exp(1i * angle(phase)); % We use a trick in the loop to avoid using ifft (fft is faster). % We only need to calculate the factor 1/N ourselves, with N = prod(sz) phase = phase ./ prod(sz); % 1/N is absorbed inside the phase array, once % create image with calculated phasemap from low res image im = abs(im) .* phase; % In the loop, we want to know where we have to copy the % measured data to, so we set the subscript of the pf dimension % accordingly. % We have to do this due to the ifftshift'ing above. tmp = false( 1, sz(pfDim)); tmp(1:Nsmp) = true; subs{pfDim} = find(ifftshift(tmp)); % release RAM clear tmp kspLowRes % only keep the acquired data in memory ksp = ksp(subs{:}); % ) =================================================================== % Helpers for pretty-printing: % Such a mess for such beautiful output! b = repmat('=',1,80); progress_str = 'starting POCS loop...'; % fprintf( '%s\n%s\n%s %s', b, b(1), b(1), progress_str ) edging = sprintf( '\n%s\n%s', b(1), b ); % fprintf( edging ) % ( =================================================================== % iterative reconstruction POCS % tic for ii = double(~watchProgress) : iter if ii > 0 % Fourier transform the image to k-space im = fft(fft(fft( im ,[],1),[],2),[],3); % "im" is a really bad variable name now % but we save a lot of RAM with this % Data Consistency: % insert original data where we have them im(subs{:}) = ksp; % "im" is still our reconstructed k-space signal % Fourier transform into image domain im = conj( im ); im = fft(fft(fft( im ,[],1),[],2),[],3); % Now, "im" is an image again. % create image with calculated phasemap from low res image im = abs(im) .* phase; prevLength = numel(progress_str) + numel(edging); t = toc; ETA = (t./ii) * iter - t; progress_str = sprintf( 'Iteration %g/%g, in %g s, ETA: %g s...', ii, iter, t, ETA ); % fprintf([repmat('\b',1,prevLength) '%s' '%s'], progress_str, edging ); end % if ii > 0 % a rough way to monitor the progress % if watchProgress tmp = ifftshift(sqrt(sum(abs(im(:,:,1,:).^2),4))); % due to fftshift(), the 1st partition is the central one maxRange = sort( tmp(:), 'descend' ); maxRange = maxRange( ceil(0.05 * numel(maxRange)) ); % ignore the "hottest" 5% if ~exist('pic','var') pic = [tmp tmp zeros(size(tmp),prec)]; diffScale = 1; else delta = abs( pic(:,Nx+(1:Nx)) - tmp ); diffScale = 0.5 * maxRange / median( delta(:) ); pic(:, Nx+(1:Nx)) = tmp; pic(:,2*Nx+(1:Nx)) = diffScale * delta; clear delta end figure(999) imagesc( pic, [0 maxRange ] ) title(sprintf('\\bfiteration %g\ninitial | current | abs(previous - current) × %g', ii, diffScale )) axis image colormap(gray(256)) drawnow clear tmp %if Nz == 1 % little pause for 2D (too fast otherwise) % pause(2 / iter) %end end end % for ii = 1:iter % fprintf([repmat('\b',1, numel(progress_str) + numel(edging)) 'POCS done! (%g s)' '%s\n\n'], t, edging ); % ) =================================================================== clear phase pic % ( =================================================================== % The main part is over. Time for some thoughts. % % We began with a dataset that had fewer data samples than would be % necessary for an unambiguous image reconstruction. As a consequence, % an infinite number of images corresponds to the acquired data. % The above iteration picks that single image whose abs() fits the data % AND whose phase corresponds to the low-resolution phase, obtained % using the symmetric part of the data. % % Viewed in k-space, there is almost always a severe edge at the border % between acquired and interpolated data, which is due to imperfections % in the assumptions made. % Namely, phase often has some high frequency components which cannot be % accounted for in the low-resolution map. Additionally, there is noise % and we may have changing contrast or trajectory errors in our MRI % sequence. % % ^ % | A A A A A A A A \ % | A A A A A A A A % | A A A A A A A A acquired signal % k2 | A A A A A A A A % | A A A A A A A A / % | I I I I I I I I \ % | I I I I I I I I interpolated data % | I I I I I I I I / % -----------------> % k1 % % Empirically, it should be wise to create a smoother transition from % the acquired part of the signal to the interpolated data. % Ntrans = floor( (szSym-1)/3 ); % width of the transition zone % Create subscripts where we intend to keep the measured data, only. tmp = false( 1, sz(pfDim)); tmp(1:Nsmp-Ntrans) = true; subsPure = subs; subsPure{pfDim} = find(ifftshift(tmp)); % Create subscripts where we want to have a smooth transition between % measured and phase-corrected data. subsTrans = subs; subsTrans{pfDim} = setdiff( subs{pfDim}, subsPure{pfDim} ); % build a filter for the transition: tmp = hann( 2*Ntrans+3, 'symmetric'); filterTrans = tmp( Ntrans+3 : end-1 ); filterTrans = reshape( filterTrans, [ ones(1,pfDim-1) Ntrans 1] ); % Seperate data in unfiltered part and transition zone. tmp = zeros( size(im), prec ); tmp(subs{:}) = ksp; kspPure = tmp(subsPure{:}); kspTrans = tmp(subsTrans{:}); clear tmp ksp im = fft(fft(fft( im ,[],1),[],2),[],3); % "im" becomes k-space signal, again im(subsPure{:}) = kspPure; % strict data consistency for Nsmp-Ntrans samples im(subsTrans{:}) = bsxfun( @times, filterTrans, kspTrans ) ... + bsxfun( @times, 1-filterTrans, im(subsTrans{:}) ); clear subsPure subsTrans filterTrans kspPure kspTrans if nargout > 1 kspFull = im; else kspFull = double.empty([sz 0]); % kspFull exists, but no memory required end im = ifft(ifft(ifft( im ,[],1),[],2),[],3); % "im" is an image, again % ) =================================================================== % ( =================================================================== % Undo the prerequisites (--> postrequisites???) % % undo the permutations im = permute( im, [4 1 2 3] ); kspFull = permute( kspFull, [4 1 2 3] ); subs = { subs{4}, subs{1}, subs{2}, subs{3} }; % undo the fftshifts im = cmshiftnd( im, [0 sz/2] ); kspFull = cmshiftnd( kspFull, [0 sz/2] ); % undo flipping if isLower subs{pfDim+1} = sz(pfDim):-1:1; im = im(subs{:}); kspFull = kspFull(subs{:}); end % ) =================================================================== if wasAddedCoilDim % we initially reshaped a simple 2D raw data matrix to be of size 1 x Ny x Nx im = reshape( im, Ny, Nx, [] ); kspFull = reshape( kspFull, Ny, Nx, [] ); end end % of pocs() % ========================================================================= % = % SWAPPED CODE = % = % ========================================================================= function [ ksp, pfDim, isUpper, isLower, Nsmp ] = zerofillPFdim( ksp, wasAddedCoilDim ) % Only the acquired data were passed and we have to find the asymmetric % dimension. Then we increase the size along this dimension and pad with 0. Ndim = ndims( ksp ) - 1; % one dimension was for the channels sz = size( ksp ); sz = sz( 2:end ); % ignore channel dimension Nc = size( ksp, 1 ); prec = class( ksp ); % init some helper variables pfDim = 0; % partial Fourier reduction dimension isUpper = false; isLower = false; isPartialFourier = false(Ndim,1); % ( =============================================================== % autodetect the Partial Fourier dimension % for d = 1:Ndim centre = floor( sz(d)/2 ) + 1; tmp = squeeze( sum(abs(ksp),1) ); for d2 = 1:Ndim if d2 ~= d tmp = max(tmp,[],d2); % keep only the maximum of non-partial data points end end [ dummy, maxPos(d) ] = max( tmp(:) ); %#ok <-- don't use "~", for compatibility if abs(maxPos(d) - centre) >= 2 % significant asymmetry ==> partial Fourier acquisition isPartialFourier(d) = true; pfDim = d; Nsmp = sz(d); isUpper = maxPos(d) > centre; % Did we sample the upper matrix part, so the lower part is missing... isLower = maxPos(d) < centre; % ... or are the first data points missing (e.g. asymmetric echo)? end end % for d = 1:Ndim % % ) ===== (PF dim detection) ====================================== switch nnz(isPartialFourier) % |N|umber of |N|on |Z|ero elements case 0 error( 'pocs:NoPfDim', 'No partial Fourier dimension found.' ) case 1 % fprintf( 'Found partial Fourier along array dimension %d\n', pfDim + ~wasAddedCoilDim ) otherwise error( 'pocs:TooManyPfDims', 'Partial Fourier only allowed in 1 dimension, but %g were found!', nnz(isPartialFourier) ) end if pfDim == 0 % our init value above error('zerofillPF:NoPF','No partial Fourier property found!') end % initialize a cell of subscripts subs = { ':', ':', ':', ':' }; % all channels / all Ny / all Nx / all Nz c = maxPos(pfDim); if isUpper sz(pfDim) = 2 * (c - mod(c,2)); % determine the blown-up size we want to achieve subs{pfDim+1} = 1:Nsmp; elseif isLower sz(pfDim) = 2 * (Nsmp - c + 1); c = floor( sz(pfDim)/2 ) + 1; sz(pfDim) = sz(pfDim) + 2*~mod(c,2); % A hack for Stefan's data... keep an eye on this! subs{pfDim+1} = (1:Nsmp) + (sz(pfDim)-Nsmp); else error( 'zerofillPF:PFdimNotClassified', 'Could not tell how partial Fourier was implemented.' ) end % do the zerofilling tmp = zeros( [Nc sz], prec ); tmp(subs{:}) = ksp; ksp = tmp; end % of zerofillPFdim() function [ pfDim, isUpper, isLower, Nsmp ] = detectPFdim( smplPtrn, wasAddedCoilDim ) % User passed already zero-padded data. This was nice, now it's easy % to find the partial Fourier dimension! Ndim = ndims( smplPtrn ); sz = size( smplPtrn ); % init some helper variables pfDim = 0; % partial Fourier reduction dimension isUpper = false; isLower = false; isPartialFourier = false( Ndim, 1 ); % ( =============================================================== % Determine if this is a zerofilled partial Fourier measurement % and along which dimension the data is reduced. % % smplPtrn in Partial Fourier looks like this: % % ^ % | 1 1 1 1 1 1 1 1 ---> sampling pattern is the same % | 1 1 1 1 1 1 1 1 for all k1 points % | 1 1 1 1 1 1 1 1 % k2 | 1 1 1 1 1 1 1 1 i.e. for programming: % | 1 1 1 1 1 1 1 1 smplPtrn == repmat( smplPtrn(:,1,1), [1 Nx Nz] ) % | O O O O O 0 0 0 % | O O O O O 0 0 0 % | O O O O O 0 0 0 % -----------------> % k1 % for d = 1:Ndim subs = { ones(1,sz(d)), ... % initialize a cell of subscripts we might be interested in ones(1,sz(d)), ... ones(1,sz(d)) }; subs{d} = 1:sz(d); % we ask for all entries in the d'th dimension idx_d = sub2ind( sz, subs{:} ); % convert to linear array indices oneCol = smplPtrn( idx_d ); % one column of the d'th dimension % create a rule how to reshape oneCol reshRule = ones(1,Ndim); reshRule(d) = sz(d); % e.g. reshRule = [ 1 1 128 ] oneCol = reshape( oneCol, reshRule); % create a rule how to replicate oneCol repRule = sz; repRule(d) = 1; % e.g. repRule = [ 256 256 1 ] % Check if we get the sampling pattern again % just by replicating oneCol along the other dimensions isPartialFourier(d) = isequal( smplPtrn, repmat( oneCol, repRule ) ); if isPartialFourier(d) pfDim = d; Nsmp = nnz( oneCol ); % how many fully sampled lines do we have? % Sampled upper or lower part of k-space matrix? isUpper = isequal( oneCol(:).', [ true( 1,Nsmp) false(1,sz(d)-Nsmp) ]); isLower = isequal( oneCol(:).', [ false(1,sz(d)-Nsmp) true( 1,Nsmp) ]); end end % ) =============================================================== switch nnz(isPartialFourier) % |N|umber of |N|on |Z|ero elements case 0 error( 'pocs:NoPfDim', 'No partial Fourier dimension found.' ) case 1 % fprintf( 'Found partial Fourier along array dimension %d\n', pfDim + ~wasAddedCoilDim ) otherwise error( 'pocs:TooManyPfDims', 'Partial Fourier only allowed in 1 dimension!' ) end end % of detectPFdim() function [ centreLine, idxSym ] = findSymSampled( ksp, pfDim, Nsmp ) Ndim = ndims( ksp ) - 1; % one for channels sz = size( ksp ); sz = sz(2:end); % autodetect the central k-space line %if ~exist('centreLine', 'var') || isempty(centreLine) tmp = squeeze( sum(abs(ksp),1) ); for d = 1:Ndim if d ~= pfDim tmp = max(tmp,[],d); % keep only the maximum of non-partial data points end end [ dummy, centreLine] = max( tmp(:) ); %#ok the central line has the max intensity %end % calculate the size of the symmetric part and the full dataset startSym = centreLine - (Nsmp - centreLine); % start of our symmetric sampling endSym = centreLine + (Nsmp - centreLine); % end of symmetric part idxSym = startSym : endSym; if any(idxSym < 1) || any(idxSym > sz(pfDim)) error( 'pocs:BadDataProperty' , 'Symmetric part of k-space out of bounds.\nThe maximum k-space intensity is at index %g whereas it should be centred => near %g.\nThe way, zerofilling was done is probably wrong.\nCheck your input k-space.', centreLine, round(sz(pfDim)/2) ) end end % of findSymmetricSampled() function x = cmshiftnd( x, shifts) %Function to circularly shift N-D arrays if nargin < 2 || all(shifts(:) == 0) return % no shift end sz = size( x ); numDims = ndims(x); % number of dimensions idx = cell(1, numDims); % creates cell array of empty matrices, % one cell for each dimension for k = 1:numDims m = sz(k); p = ceil(shifts(k)); if p < 0 p = m + p; end idx{k} = [p+1:m 1:p]; end % Use comma-separated list syntax for N-D indexing. x = x(idx{:}); end % of cmshiftnd() % Avoid the need for the signal toolbox and implement % hamming() and hann() manually: % function w = hamming( N, symFlag ) %Hamming window % % w = hamming(L) returns an L-point symmetric Hamming window in the column vector w. % L should be a positive integer. % % The coefficients of a Hamming window are computed from the following equation: % % w(n) = 0.54 + 0.46 * cos(2*pi*n/N), 0 <= n <= N % % % w = hamming( L, 'symFlag') returns an L-point Hamming window using the window sampling % specified by 'symFlag', which can be either 'periodic' or 'symmetric' (the default). % The 'periodic' flag is useful for DFT/FFT purposes, such as in spectral analysis. % The DFT/FFT contains an implicit periodic extension and the periodic flag enables a signal % windowed with a periodic window to have perfect periodic extension. % When 'periodic' is specified, hamming computes a length L+1 window and returns the first L points. % When using windows for filter design, the 'symmetric' flag should be used. % % --> http://www.mathworks.de/de/help/signal/ref/hamming.html % --> https://de.wikipedia.org/wiki/Hamming-Fenster % implemented by [email protected], 2012 if ~exist( 'N', 'var' ) || isempty(N) || numel(N) ~= 1 || ~isnumeric(N) || ~isfinite(N) || N < 1 || floor(N) ~= N error( 'hamming:badSize', 'Window lenght must be a positive integer.' ) end if ~exist( 'symFlag', 'var' ) || isempty(symFlag) symFlag = 'symmetric'; end if N == 1 w = 1; return end switch symFlag case 'symmetric' L = N-1; case 'periodic' L = N; otherwise error('hamming:symFlag', 'Unknown symmetry flag. Try ''symmetric'' (default) or ''periodic''.') end w = (0:N-1) - L/2; w = 0.54 + 0.46 * cos(2*pi * w(:)./L); end % of hamming() function w = hann( N, symFlag ) %von-Hann (Hanning) window % % w = hann(L) returns an L-point symmetric Hann window in the column vector w. % L must be a positive integer. % % The coefficients of a Hann window are computed from the following equation: % % w(n) = 0.5 * (1 + cos(2*pi*n/N)), 0 <= n <= N % % The window length is L = N+1. % % w = hann(L,'sflag') returns an L-point Hann window using the window sampling specified by 'sflag', % which can be either 'periodic' or 'symmetric' (the default). The 'periodic' flag is useful for DFT/FFT purposes, % such as in spectral analysis. % The DFT/FFT contains an implicit periodic extension and the periodic flag enables a signal windowed % with a periodic window to have perfect periodic extension. % When 'periodic' is specified, hann computes a length L+1 window and returns the first L points. % When using windows for filter design, the 'symmetric' flag should be used. % % --> http://www.mathworks.de/de/help/signal/ref/hann.html % --> https://de.wikipedia.org/wiki/Hann-Fenster % implemented by [email protected], 2012 if ~exist( 'N', 'var' ) || isempty(N) || numel(N) ~= 1 || ~isnumeric(N) || ~isfinite(N) || N < 1 || floor(N) ~= N error( 'hann:badSize', 'Window lenght must be a positive integer.' ) end if ~exist( 'symFlag', 'var' ) || isempty(symFlag) symFlag = 'symmetric'; end if N == 1 w = 1; return end switch symFlag case 'symmetric' L = N-1; case 'periodic' L = N; otherwise error('hann:symFlag', 'Unknown symmetry flag. Try ''symmetric'' (default) or ''periodic''.') end w = (0:N-1) - L/2; w = 0.5 * ( 1 + cos(2*pi * w(:)./L) ); end % of hann()
github
sunhongfu/scripts-master
grappa.m
.m
scripts-master/cs-phase/_src/_grappa/grappa.m
2,384
utf_8
ed282c80f1a0002da0cc2c40499631bd
% grappa.m % [email protected] % % inputs: % data - (nc, nx, ny, nz, m]) complex undersampled k-space data % will also loop across extra dimension m % calib - (nc, cx, cy, cz) complex calibration k-space data % R - [Rx, Ry] or [Rx, Ry, Rz] acceleration factors % kernel - [kx, ky] or [kx, ky, kz] kernel size % tol - singular value cutoff threshold for kernel weight % training, relative to s(1), defaults to pinv default % % output: % recon - (nc, nx, ny, nz) complex reconstructed k-space data function data = grappa(data, calib, ARG) data = permute(data ,[3,1,2,4]); calib = permute(calib,[3,1,2]); R = ARG.iPAT.factor; kernel = ARG.iPAT.kernel; tol = ARG.iPAT.tol; %% Use default pinv tolerance if not supplied if tol > 0 pinv_reg = @(A)pinv(A, tol*norm(A,2)); else pinv_reg = @pinv; end %% Determine whether this is a 1D or 2D GRAPPA problem if numel(R) == 2 R(3) = 1; end if numel(kernel) == 2 kernel(3) = 1; end for iMe = 1:size(data,4) %% extract one measument tmp = squeeze(data(:,:,:,iMe)); %% Prepare masks and zero-pad data if iMe == 1 pad = floor(R.*kernel/2); mask = padarray(tmp~=0, [0 pad]); end tmp = padarray(tmp, [0 pad]); %% Loop over all possible kernel types for type = 1:prod(R(2:end))-1 if iMe == 1 % Collect source and target calibration points for weight estimation [src, trg] = grappa_get_indices(kernel, true(size(calib)), pad, R, type); % Perform weight estimation weights(:,:,type) = calib(trg)*pinv_reg(calib(src)); end % Collect source points in under-sampled data for weight application [src, trg] = grappa_get_indices(kernel, mask, pad, R, type); % Apply weights to reconstruct missing data tmp(trg) = squeeze(weights(:,:,type))*tmp(src); end %% Un-pad reconstruction to get original image size back data(:,:,:,iMe) = tmp(:,pad(1)+1:size(tmp,2)-pad(1), pad(2)+1:size(tmp,3)-pad(2), pad(3)+1:size(tmp,4)-pad(3),:); clear tmp end %% Permute for compatibility with other code data = permute(data,[2,3,1,4]);
github
sunhongfu/scripts-master
grappa_get_indices.m
.m
scripts-master/cs-phase/_src/_grappa/grappa_get_indices.m
2,774
utf_8
0692576896f6fc13f24d3be9e43c8002
% grappa_get_indices.m % [email protected] % % inputs: % kernel - [sx, sy, sz] kernel size in each dimension % samp - (c, nx, ny, nz) sampling mask, true(size(calib)) % pad - [pad_x, pad_y, pad_z] size of padding in each direction % type - (scalar, must be < R) indicates which of the R(2)*R(3)-1 kernels % you are trying to index over % offset - additional index offset that gets added to src,trg % % output: % src - linear indices for all source points (c*sx*sy*sz, all possible targets) % trg - linear indices for all the target points (c, all possible targets) function [src, trg] = grappa_get_indices(kernel, samp, pad, R, type, offset) % Offset is optional, 0 by default if nargin < 6 offset = 0; end % Get dimensions [nc,dx,dy,dz] = size(samp); % Make sure the under-sampling is in y and z only % There are a few things here that require that assumption if R(1) > 1 error('x-direction must be fully sampled'); end % Make sure the type parameter makes sense % It should be between 1 and R(2)*R(3)-1 (inclusive) if type > prod(R(2:3))-1 error('Type parameter is inconsistent with R'); end % Find the limits of all possible target points given padding kx = 1+pad(1):dx-pad(1); ky = 1+pad(2):dy-pad(2); kz = 1+pad(3):dz-pad(3); %% Compute indices for a single coil % Find relative indices for kernel SOURCE points mask = false(dx,dy,dz); mask(1:R(1):R(1)*kernel(1), 1:R(2):R(2)*kernel(2), 1:R(3):R(3)*kernel(3)) = true; k_idx = reshape(find(mask),[],1); % Find the index for the desired TARGET point (depends on type parameter) mask = false(dx,dy,dz); [yy,zz] = ind2sub(R(2:3),type+1); mask(R(1)*ceil(kernel(1)/2), R(2)*(ceil(kernel(2)/2)-1)+yy, R(3)*(ceil(kernel(3)/2)-1)+zz) = true; k_trg = reshape(find(mask),[],1); % Subtract the target index from source indices % to get relative linear indices for all source points % relative to the target point (index 0, target position) k_idx = k_idx - k_trg; % Find all possible target indices mask = false(dx,dy,dz); mask(kx,ky,kz) = squeeze(circshift(samp(1,kx,ky,kz),[0 0 yy-1 zz-1])); trg = reshape(find(mask),1,[]); % Find all source indices associated with the target points in trg src = bsxfun(@plus, k_idx, trg); %% Now replicate indexing over all coils % Final shape of trg should be (#coils, all possible target points) trg = bsxfun(@plus, (trg-1)*nc+1, (0:nc-1)') + offset; % Final shape of src should be (#coils*sx*sy, all possible target points) src = bsxfun(@plus, (src(:)'-1)*nc+1, (0:nc-1)'); src = reshape(src,[], size(trg,2)) + offset;
github
sunhongfu/scripts-master
Gsparse.m
.m
scripts-master/cs-phase/_src/_NUFFT/Gsparse.m
7,162
utf_8
313f033569655fb4925e3490b7ae7f5a
function ob = Gsparse(arg1, varargin) %function ob = Gsparse(file.wtf | sparse | cell, options) % % Construct Gsparse object, either from a sparse matrix itself, % or from the arguments that would be passed to matlab's sparse() command, % or from an Aspire binary .wtf file. % % The purpose of this object is to overcome some annoying limitations of % matlab's sparse() function and sparse datatype. In particular, this function % allows single precision arguments (that are converted silently to doubles, % as long as matlab continues to insist on that) instead of the useless % error message provided by our good friends at Mathworks. And you can do % multiplication of a Gsparse object times a non-double-precision vector % (which is silently upgraded to doubles), which Matlab does not support. % % More substantively, this object also supports the "multidimensional" % constructs needed in imaging problems. (support mask, subsets, etc.) % % This just uses an ordinary Matlab sparse matrix for the core! % See Gsparse_test.m for example usage. % % You create an system object by calling: % G = Gsparse(file) % and then you can use it thereafter by typing commands like % y = G * x. % % in % arg1 char | cell | sparse sparse matrix (usual case) % or cell array of sparse() arguments % or filename of an aspire .wtf % options % mask [idim] logical support mask % idim [1,ndim_in] input dimensions: nx,ny,nz etc. % odim [1,ndim_out] output dimensions: nb,na etc. % chat verbosity % % out % ob [nd,np] nd = prod(odim), np = sum(mask(:)) % so it is already "masked" % % Copyright 2005-6-16, Jeff Fessler, The University of Michigan if nargin == 1 & streq(arg1, 'test'), Gsparse_test, return, end if nargin < 1, help(mfilename), error(mfilename), end % defaults arg.mask = []; arg.idim = []; arg.odim = []; arg.chat = 0; arg.blocks = {}; % place to store blocks of G for subset algorithms arg = vararg_pair(arg, varargin); if ~isempty(arg.mask) && ~islogical(arg.mask) error 'mask must be logical' end % % cell array of arguments to sparse() % if iscell(arg1) if length(arg1) >= 3 % i, j, s ... arg1{3} = double(arg1{3}); % trick: double values for s end arg1 = sparse(arg1{:}); end % % if input is an Aspire .wtf file % if ischar(arg1) arg.file = arg1; if ~isempty(arg.idim) | ~isempty(arg.odim) error 'idim / odim should not be given for .wtf' end [arg.G arg.idim(1) arg.idim(2) arg.odim(1) arg.odim(2)] = ... wtfmex('load', arg.file); % default mask from .wtf if isempty(arg.mask) tmp = full(sum(arg.G) > 0); arg.mask = reshape(tmp, arg.idim); end arg.G = arg.G(:,arg.mask(:)); % % if input is a sparse matrix % elseif issparse(arg1) arg.G = arg1; if isempty(arg.idim) if ~isempty(arg.mask) arg.idim = size(arg.mask); else warning 'idim not given for sparse matrix!' arg.idim = [size(arg.G,2) 1]; end end if isempty(arg.mask) arg.mask = true(arg.idim); % default mask is all elseif length(arg.idim) ~= ndims(arg.mask) | ... any(arg.idim ~= size(arg.mask)) disp(arg), error 'bad mask size' end if isempty(arg.odim) warning 'odim not given for sparse matrix!' arg.odim = [size(arg.G,1) 1]; elseif prod(arg.odim) ~= size(arg.G,1) error 'bad row dimension' end if sum(arg.mask(:)) ~= size(arg.G,2) if size(arg.G,2) == numel(arg.mask) arg.G = arg.G(:, arg.mask(:)); % trick: compact size else disp(arg), error 'bad G size' end end else error 'input must be cell or filename or sparse matrix' end % % build Fatrix object % arg.nd = prod(arg.odim); arg.np = sum(arg.mask(:)); ob = Fatrix([arg.nd arg.np], arg, 'caller', mfilename, ... 'forw', @Gsparse_forw, 'back', @Gsparse_back, ... 'block_setup', @Gsparse_block_setup, ... 'mtimes_block', @Gsparse_mtimes_block, ... 'abs', @Gsparse_abs, 'power', @Gsparse_power); % % Gsparse_forw(): y = G * x % function y = Gsparse_forw(arg, x) % if needed, convert array to concise column flag_array = 0; if size(x,1) ~= arg.np flag_array = 1; x = reshape(x, numel(arg.mask), []); % [*N,*L] x = x(arg.mask(:),:); % [np, *L] end if isa(x, 'double') y = arg.G * x; % [nd, *L] else y = arg.G * double(x); % [nd, *L] if ~issparse(y) y = single(y); end end if flag_array y = reshaper(y, arg.odim); % [(M),*L] end % % Gsparse_back(): x = G' * y % function x = Gsparse_back(arg, y) flag_array = 0; if size(y,1) ~= arg.nd flag_array = 1; y = reshape(y, arg.nd, []); % [nd,*L] end if isa(y, 'double') x = (y' * arg.G)'; % [np,*L] trick: runs faster this way! else x = (double(y)' * arg.G)'; % [np,*L] if ~issparse(x) x = single(x); end end if flag_array x = embed(x, arg.mask); % [(N),*L] end % % Gsparse_abs() % function ob = Gsparse_abs(ob) ob.arg.G = abs(ob.arg.G); % % Gsparse_block_setup() % Pre-construct blocks of sparse matrix so that it need not be done % for every block access. Doubles memory. % function ob = Gsparse_block_setup(ob) nb = prod(ob.arg.odim(1:end-1)); na = ob.arg.odim(end); ob.arg.blocks = cell(ob.nblock,1); for iblock=1:ob.nblock ia = iblock:ob.nblock:na; ii = outer_sum(1:nb, (ia-1)*nb); ii = ii(:); t = ob.arg.G(ii,:); % fix: sparse, but nzmax is too large! [i j s] = find(t); if iblock == 1 & length(s) ~= nzmax(t) % persistent warned warning 'stupid matlab sparse too big' end t = sparse(i, j, s, length(ii), size(ob.arg.G, 2)); ob.arg.blocks{iblock} = t; end % % Gsparse_mtimes_block() % note: this is not incredibly efficient, but it is mostly for testing anyway. % function y = Gsparse_mtimes_block(arg, is_transpose, x, istart, nblock) if is_transpose y = Gsparse_mtimes_back(arg, x, istart, nblock); else y = Gsparse_mtimes_forw(arg, x, istart, nblock); end % old slow way %nb = prod(arg.odim(1:end-1)); %ii = outer_sum(1:nb, (ia-1)*nb); % trick: just reuse almost everything in arg %arg.G = arg.G(ii(:),:); % % Gsparse_mtimes_forw() % function y = Gsparse_mtimes_forw(arg, x, istart, nblock); ia = istart:nblock:arg.odim(end); % subset over last dim % if needed, convert array to concise column flag_array = 0; if size(x,1) ~= arg.np flag_array = 1; x = reshape(x, numel(arg.mask), []); % [*N,*L] x = x(arg.mask(:),:); % [np,*L] end if isa(x, 'double') y = arg.blocks{istart} * x; % [nd, *L] else y = arg.blocks{istart} * double(x); % [nd, *L] y = single(full(y)); end if flag_array y = reshaper(y, [arg.odim(1:end-1) length(ia)]); end % % Gsparse_mtimes_back() % function x = Gsparse_mtimes_back(arg, y, istart, nblock); ia = istart:nblock:arg.odim(end); % subset over last dim nd1 = arg.nd * length(ia) / arg.odim(end); flag_array = 0; if size(y,1) ~= nd1 flag_array = 1; y = reshape(y, nd1, []); % [nd1,*L] end if isa(y, 'double') x = full(y' * arg.blocks{istart})'; % [np,*L] trick: runs faster else x = full(double(y)' * arg.blocks{istart})'; % [np,*L] x = single(x); end if flag_array x = embed(x, arg.mask); % [(N),*L] end % % Gsparse_power() % function ob = Gsparse_power(ob, sup) ob.arg.G = ob.arg.G .^ sup; % fix: this is inefficient to be working with both G and its blocks if ~isempty(ob.nblock) for ii=1:ob.nblock ob.arg.blocks{ii} = ob.arg.blocks{ii} .^ sup; end end
github
sunhongfu/scripts-master
ifft_sym.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/ifft_sym.m
1,181
utf_8
01ebf2f01e0379ebb2d3ae4792f996f7
function y = ifft_sym(varargin) %function y = ifft_sym(varargin) % matlab 7.0 introduced a 'symmetric' option to ifft to handle % spectra that are (circularly) hermitian symmetric (real signal). % this glue routine is to provide backward compatibility for matlab 6.5. % Caution: v7 ifft with 'symmetric' just uses the first half of the spectrum % along whichever dimension is requested. Here, for pre v7, I just take % the real part. The difference is neglible in the cases where this % routine is expected to be used, where the spectrum should be exactly % symmetric but has slight asymmetry due to numerical precision. % If the spectrum is severely asymmetric, then "real(ifft())" and % ifft(..., 'symmetric') will differ substantially. (But one should % not call this routine in such cases.) if ~nargin, help(mfilename), error(mfilename), end if nargin == 1 && streq(varargin{1}, 'test'), ifft_sym_test, return, end if is_pre_v7 y = ifft(varargin{:}); y = reale(y, 1e-11, 'prompt'); else y = ifft(varargin{:}, 'symmetric'); end function y = ifft_sym_test del = 10^5*eps; format compact x1 = [4 2+0i*del 8 2-1i*del] y1 = ifft(x1) y2 = ifft_sym(x1) x2 = fft(y2) y1 - y2
github
sunhongfu/scripts-master
jf_protected_names.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/jf_protected_names.m
2,298
utf_8
9430f87fc9730771e794a957c0e20697
function pn = jf_protected_names %|function pn = jf_protected_names %| %| A serious drawback of the matlab language is that it lacks %| a protected or local namespace. Every m-file that is in the path %| is available to all functions (except those in "private" subdirectories). %| Users who have their own m-files that happen to have the same names as %| any of the routines in a toolbox like this one will have problems. %| %| To try to overcome this limitation, I created this function in late 2009 %| to serve as a repository of simple functions. %| To use any of these functions, one types something like %| pn = jf_protected_names; %| and then one can call the functions using %| out = pn.fun(arg1, arg2, ...); %| %| Copyright 2009-11-21, Jeff Fessler, University of Michigan pn = strum(struct, { ... 'struct_recurse', @jf_struct_recurse, '()'; 'color_order', @jf_color_order, '()'; 'diary', @jf_diary, '(file)'; 'has_hct2', @jf_has_hct2, '()'; 'hct_arg', @jf_hct_arg, '(cg, ig)'; 'ind2sub', @jf_ind2sub, '(siz, ind)'; 'mid3', @jf_mid3, '(im_3d, [dim])'; 'normcdf', @jf_normcdf, '(x, mu, sigma)'; 'prctile', @jf_prctile, '(x, p, [dim])'; 'case', @jf_case, '(x, v0, v1, ...)'; 'test', @jf_test, '()'; }); end % jf_protected_names() % jf_case() % its purpose is to act like the '?' operator in C % jf_case(x, 'value if x is 0', 'value if x is 1', ...) function out = jf_case(st, x, varargin) if x+1 > numel(varargin) fail('x=%d but num=%d', x, numel(varargin)) end out = varargin{x+1}; end % jf_case() % jf_diary() % this version prompts if file exists! function jf_diary(st, file) if streq(file, 'off') diary('off'); return end if exist(file, 'file') fail 'file exists' else printm('starting diary for "%s"', file) diary(file); end end % jf_diary() % jf_ind2sub() % version with a single matrix output, one dimension per column function subs = jf_ind2sub(st, Nd, ind) ind = ind(:); subs = zeros(numel(ind), numel(Nd)); switch numel(Nd) case 2 [subs(:,1) subs(:,2)] = ind2sub(Nd, ind); case 3 [subs(:,1) subs(:,2) subs(:,3)] = ind2sub(Nd, ind); case 4 [subs(:,1) subs(:,2) subs(:,3) subs(:,4)] = ind2sub(Nd, ind); otherwise fail 'not done' end end % jf_ind2sub() % jf_test() function jf_test(st, varargin) jf_equal(st.case(1, 2, 3), 3) end % jf_test()
github
sunhongfu/scripts-master
os_run.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/os_run.m
496
utf_8
b86b9948f9e383a3f5f8d047d0ec9ae0
function out = os_run(str) %|function out = os_run(str) %| call OS (unix only of course), check for error, optionally return output if nargin < 1, help(mfilename), error(mfilename), end if streq(str, 'test'), os_run_test, return, end [s out1] = unix(str); if s fail('unix call failed:\n%s', str) end if nargout out = out1; end function os_run_test printm 'os_run test' if ~isunix warn 'os_run works only on unix' return end out = os_run('echo 1+2 | bc'); jf_equal(out, sprintf('3\n'))
github
sunhongfu/scripts-master
interp1_jump.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/interp1_jump.m
2,280
utf_8
b183193c9190a72ea28565ea8def6dba
function yi = interp1_jump(xj, yj, xi, varargin) %function yi = interp1_jump(xj, yj, xi, {arguments for interp1}) % Generalization of matlab's "interp1" to allow xj with repeated values, % for interpolation of a function that has "jumps" (discontinuities), % such as is caused by k-edges for mass attenuation coefficients. % If the first option is 'monodown' then the function is expected to % be monotone decreasing except for certain jumps that may not correspond % to xj's with equal values. % The (remaining) options are passed to 'interp1'. % Copyright 2004-5-2, Jeff Fessler, The University of Michigan if nargin == 1 && streq(xj, 'test'), interp1_jump_test, return, end if nargin < 3, help(mfilename), error(mfilename), end if length(xj) ~= length(yj), error 'xj and yj have different lengths', end jjump = find(diff(xj) == 0); if length(varargin) && streq(varargin{1}, 'monodown') varargin = {varargin{2:end}}; yjump = find(diff(yj) > 0); jjump = unique([jjump; yjump]); end npiece = 1 + length(jjump); if npiece == 1 yi = interp1(xj, yj, xi, varargin{:}); return end yi = zeros(size(xi)); done = zeros(size(xi)); for ip=1:npiece if ip == 1 jlist = [1:jjump(1)]; elseif ip == npiece jlist = [(1+jjump(npiece-1)):length(xj)]; else jlist = [(1+jjump(ip-1)):jjump(ip)]; end x = xj(jlist); y = yj(jlist); if ip == 1 ilist = find(xi <= max(x)); elseif ip == npiece ilist = find(min(x) <= xi); else ilist = find(min(x) <= xi & xi <= max(x)); end if isempty(ilist), continue, end if length(x) > 1 yi(ilist) = interp1(x, y, xi(ilist), varargin{:}); done(ilist) = 1; elseif length(x) == 1 if any(x == xi(ilist)) yi(ilist) = y(x == xi(ilist)); else warning 'bug?' keyboard end end end % for anything left over, use linear interpolation if any(~done) [xj jj] = unique(xj); yi(~done) = interp1(xj, yj(jj), xi(~done), 'linear'); end function interp1_jump_test x = [0 0.5 1 1 2 3 3 4 5]; %x = [0 0.5 1 1.1 2 3 3.1 4 5]; y = [1 0 0 1 1 2 1 2 2]; t = linspace(-0.5,0.5+max(x),1001); f = interp1_jump(x, y, t, 'cubic', 'extrap'); if im clf, subplot(211) plot(x, y, 'o', t, f, '-') end x = [0:5]; y = [4 2 1 2 1 0]; f = interp1_jump(x, y, t, 'monodown', 'cubic', 'extrap'); if im subplot(212) plot(x, y, 'o', t, f, '-') end
github
sunhongfu/scripts-master
jf_histn.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/jf_histn.m
2,106
utf_8
8a5e6756705f9e4c239f592b17967eed
function [hist center] = jf_histn(data, varargin) %|function [hist center] = jf_histn(data, varargin) %| %| Fast histogram of multidimensional data for equally-spaced bins. %| todo: use accumarray? %| %| in %| data [N M] data values to be binned (M-dimensional) %| %| option %| 'min' [M] minimum bin values for each dimension (left side) %| 'max' [M] maximum bin values for each dimension (right side) %| 'nbin' [M] # of bins for each dimension (default: 100) %| %| out %| hist [[ncent]] histogram values: sum(hist(:)) = N %| center {ncent} cell array of bin centers for each dimension %| %| Copyright 2010-07-31, Jeff Fessler, University of Michigan if nargin == 1 && streq(data, 'test'), jf_histn_test, return, end if nargin < 1, help(mfilename), error(mfilename), end arg.min = []; arg.max = []; arg.nbin = []; arg.chat = 0; arg = vararg_pair(arg, varargin); M = size(data,2); if isempty(arg.nbin) arg.nbin = 100; end if numel(arg.nbin) == 1 arg.nbin = arg.nbin * ones(M,1); end for id=1:M tmp = data(:,id); if isempty(arg.min) xmin = min(tmp); else xmin = arg.min(id); end if isempty(arg.max) xmax = max(tmp); else xmax = arg.max(id); end if xmin == xmax if xmin == 0 xmin = -0.5; xmax = +0.5; else xmin = 0.5 * xmin; xmax = 1.5 * xmin; end end K = arg.nbin(id); tmp = (tmp - xmin) / (xmax - xmin); % [0,1] tmp(tmp < 0) = 0; tmp(tmp > 1) = 1; tmp = 1 + tmp * (K-1); % [1 K] data(:,id) = round(tmp); if K == 1 center{id} = (xmin + xmax) / 2; else center{id} = linspace(xmin, xmax, K); end end [hist hcent] = hist_bin_int(data); for id=1:M tmp = hcent{id}; K = arg.nbin(id); if min(tmp) ~= 1 || max(tmp) ~= K minmax(tmp) fail 'todo' end end % test routine function jf_histn_test randn('state', 0) n = 1000; sig = 5; rho = 0.6; % correlated gaussian Cov = sig * [1 rho; rho 1]; tmp = sqrtm(Cov) data = randn(n, 2) * sqrtm(Cov); nbin = [30 30] [hist cent] = jf_histn(data, 'nbin', nbin); [xs ys] = deal(cent{:}); if im im plc 1 2 im(1, xs, ys, hist) axis equal im subplot 2 plot(data(:,1), data(:,2), '.') axis equal end
github
sunhongfu/scripts-master
jf_assert.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/jf_assert.m
871
utf_8
444b53c5605424b42508cd26fa0da635
function jf_assert(varargin) %function jf_assert(command) % verify that the command (evaluated within caller) returns true. % if not, print error message. if nargin < 1, help(mfilename), error(mfilename), end if nargin == 1 && streq(varargin{1}, 'test'), jf_assert_test, return, end arg = [varargin{:}]; % handle cases with spaces like 'jf_assert x == y' [name line] = caller_name; if isempty(name) str = ''; else str = sprintf(' at %d in "%s"', line, name); end try tmp = evalin('caller', arg); catch error(['%s was unable to evaluate "%s"' str], mfilename, arg) end % note: use isequal, not issame! if isscalar(tmp) && islogical(tmp) if ~tmp fail(['jf_assert of "%s" was untrue' str], arg) % dbup % dbstack % keyboard end else tmp whos error(['jf_assert of "%s" did not return logical scalar' str], arg) end function jf_assert_test jf_assert 7 == 7
github
sunhongfu/scripts-master
gaussian_kernel.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/gaussian_kernel.m
760
utf_8
119655fccf91567673b1e3739af2b637
function kern = gaussian_kernel(fwhm, nk_half) %function kern = gaussian_kernel(fwhm, nk_half) % samples of a gaussian kernel at [-nk_half:nk_half] % with given FWHM in pixels % uses integral over each sample bin so that sum is very close to unity % % Copyright 2001-9-18, Jeff Fessler, The University of Michigan if nargin < 1, help(mfilename), return, end if streq(fwhm, 'test'), gaussian_kernel_test, return, end if nargin < 2, nk_half = 2 * ceil(fwhm); end if fwhm == 0 kern = zeros(nk_half*2+1, 1); kern(nk_half+1) = 1; else sig = fwhm / sqrt(log(256)); x = [-nk_half:nk_half]'; pn = jf_protected_names; kern = pn.normcdf(x+1/2, 0, sig) - pn.normcdf(x-1/2, 0, sig); end function gaussian_kernel_test kern = gaussian_kernel(3); plot(kern, '-o')
github
sunhongfu/scripts-master
fwhm_match.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/fwhm_match.m
1,878
utf_8
8ae6568a77739be15c5ac4afea48b4d8
function [fwhm_best, costs, im_best] = ... fwhm_match(true_image, blurred_image, fwhms) %|function [fwhm_best, costs, im_best] = ... %| fwhm_match(true_image, blurred_image, fwhms) %| %| given a blurred_image of a true_image, find the FHWM of a Gaussian kernel %| that, when convolved to the true_image, yields the smoothed image %| that best matches blurred_image. %| %| the set of FWHM values given in the array fwhms is tried. %| %| Copyright 2001-8-30, Jeff Fessler, University of Michigan if nargin == 1 && streq(true_image, 'test'), fwhm_match_test, return, end if nargin < 2, help(mfilename), error(' '), end if nargin < 3 fwhms = 0:0.5:4; end costs = zeros(size(fwhms)); cost_min = Inf; for ii=1:length(fwhms) fwhm = fwhms(ii); kern = gaussian_kernel(fwhm); psf = kern * kern'; tmp = conv2(true_image, psf, 'same'); costs(ii) = norm(tmp(:) - blurred_image(:)) / norm(true_image(:)); if costs(ii) < cost_min im_best = tmp; end end [dummy ibest] = min(costs); if ibest == 1 | ibest == length(fwhms) warning 'need wider range of fwhms' end fwhm_best = fwhms(ibest); % fwhm_match_test function fwhm_match_test % pyramidal PSF to stress the approach psf1 = [0:5 4:-1:0]; psf1 = psf1 / sum(psf1); psf = psf1' * psf1; true_image = zeros(128); true_image(64:96,64:96) = 1; blurred_image = conv2(true_image, psf, 'same'); im plc 2 2 im(1, true_image, 'True Image') im(2, blurred_image, 'Blurred Image') fwhms = [2:0.25:8]; [fwhm_best, costs] = fwhm_match(true_image, blurred_image, fwhms); np = length(psf); ip = -(np-1)/2:(np-1)/2; kern = gaussian_kernel(fwhm_best); nk = length(kern); ik = -(nk-1)/2:(nk-1)/2; if im im subplot 3 plot(fwhms, costs, 'c-o', fwhm_best, min(costs), 'yx') xlabel FWHM, ylabel Cost, title 'Cost vs FWHM' im subplot 4 plot(ip, psf1, '-o', ik, kern(:), '-+') xlabel pixel, title 'PSF profile: actual and Gaussian fit' end
github
sunhongfu/scripts-master
fractional_delay.m
.m
scripts-master/cs-phase/_src/_NUFFT/utilities/fractional_delay.m
2,438
utf_8
09fad587ef6cfbb84e8a06617919b92f
function y = fractional_delay(x, delay) %function y = fractional_delay(x, delay) % % given N samples x[n] of a real, periodic, band-limited signal x(t), % compute sinc interpolated samples of delayed signal y(t) = x(t - delay) % each column of x can be shifted by a different amount if delay is a vector. % in % x [N,L] % delay [L] % out % y [N,L] % % see laakso:96:stu for more ideas. % % Copyright 2003-11-1, Jeff Fessler, The University of Michigan % Extend to allow x to have multiple columns, 2003-11-2, Yingying Zhang. if nargin < 1, help(mfilename), disp('Need Inputs'), error(mfilename), end if streq(x, 'test'), fractional_delay_test, return, end if size(x,2) ~= length(delay) error 'Need size(x) = [N,L]; length(delay) = L' end dims = size(x); N = dims(1); L = dims(2); if length(dims) > 2, error 'x must be 1d or 2d', end X = fft(x); % fft of each column % it is important to choose the k indices appropriately! if rem(N,2) % odd k = [-(N-1)/2:(N-1)/2]'; else k = [-N/2:(N/2-1)]'; end c = exp(-1i * 2*pi/N * k * delay(:)'); % [N,L] outer product if ~rem(N,2) % even mid = 1; c(mid,:) = real(c(mid,:)); % this is the other key trick! end c = ifftshift1(c); % ifftshift differs from fftshift for odd N! Y = X .* c; y = ifft(Y); % % 1D ifftshift for each column % function c = ifftshift1(c) [N,L] = size(c); if L == 1 c = ifftshift(c); % ifftshift differs from fftshift for odd N! else % multiple input signals if ~rem(N,2) % even c = c([N/2+1:end 1:N/2],:); else % odd c = c([(N-1)/2+1:end 1:(N-1)/2],:); end end % % self test % function fractional_delay_test Nlist = [5 6]; im clf, pl=240; for ii=1:2 N = Nlist(ii); n = [0:(N-1)]'; xt = inline('sinc_periodic(t, N)', 't', 'N'); x = xt(n, N); xx = [x x]; delay = [3.7; -2.2]; y = fractional_delay(xx, delay); t = linspace(0,2*N,401); yt1 = xt(t-delay(1),N); yt2 = xt(t-delay(2),N); if im subplot(pl+0+ii), plot(t, xt(t,N), '-', n, xx(:,1), 'o') axis([0 2*N -0.4 1.1]), title(sprintf('N=%d 1st input', N)) subplot(pl+2+ii), plot(t, xt(t,N), '-', n, xx(:,2), 'o') axis([0 2*N -0.4 1.1]), title(sprintf('N=%d 2nd input', N)) subplot(pl+4+ii) plot(t, yt1, '-', n, real(y(:,1)), 's', n, imag(y(:,1)), '.') axis([0 2*N -0.4 1.1]), title(sprintf('delay=%g 1st input',delay(1))) subplot(pl+6+ii) plot(t, yt2, '-', n, real(y(:,2)), 's', n, imag(y(:,2)), '.') axis([0 2*N -0.4 1.1]), title(sprintf('delay=%g 2nd input',delay(2))) end end