├── sleep.jpg ├── code ├── libsvm-mat-3.0.1 │ ├── svm.o │ ├── svm.obj │ ├── heart_scale.mat │ ├── svmtrain.mexa64 │ ├── svmtrain.mexw32 │ ├── svmtrain.mexw64 │ ├── libsvmread.mexa64 │ ├── libsvmread.mexw32 │ ├── libsvmread.mexw64 │ ├── libsvmwrite.mexa64 │ ├── libsvmwrite.mexw32 │ ├── libsvmwrite.mexw64 │ ├── svm_model_matlab.o │ ├── svmpredict.mexa64 │ ├── svmpredict.mexw32 │ ├── svmpredict.mexw64 │ ├── svm_model_matlab.obj │ ├── svm_model_matlab.h │ ├── make.m │ ├── COPYRIGHT │ ├── Makefile │ ├── libsvmwrite_org.c │ ├── libsvmwrite.c │ ├── svm_org.h │ ├── svm.h │ ├── libsvmread_org.c │ ├── libsvmread.c │ ├── svm_model_matlab_org.c │ ├── svm_model_matlab.c │ ├── README │ ├── svmpredict_org.c │ ├── svmpredict.c │ ├── svmtrain_org.c │ └── svmtrain.c ├── func │ ├── setdir.m │ ├── addBias.m │ ├── reArrangeDataStructure.m │ ├── uninorm.m │ ├── selectTopTvals.m │ ├── getFieldDef.m │ ├── reIndex.m │ ├── cpDetect.m │ ├── normFeature.m │ ├── performPerception2SleepDecoding.m │ ├── getTri.m │ ├── libsvm_h.m │ ├── readfMRIData.m │ ├── convertRawDatatoPreprocessedDataCell.m │ ├── readHDF5AsStruct.m │ ├── readHDF5AsStruct.m~ │ ├── writeHDF5FromStruct.m │ └── preprocfMRIData.m ├── Perception2SleepDecoding_BATCH.m └── convertRawDatatoPreprocessedData.m └── README.md /sleep.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/sleep.jpg -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svm.o -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svm.obj -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/heart_scale.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/heart_scale.mat -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmtrain.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmtrain.mexa64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmtrain.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmtrain.mexw32 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmtrain.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmtrain.mexw64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmread.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmread.mexa64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmread.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmread.mexw32 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmread.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmread.mexw64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmwrite.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmwrite.mexa64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmwrite.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmwrite.mexw32 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmwrite.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/libsvmwrite.mexw64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm_model_matlab.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svm_model_matlab.o -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmpredict.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmpredict.mexa64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmpredict.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmpredict.mexw32 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmpredict.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svmpredict.mexw64 -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm_model_matlab.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KamitaniLab/HumanDreamDecoding/HEAD/code/libsvm-mat-3.0.1/svm_model_matlab.obj -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm_model_matlab.h: -------------------------------------------------------------------------------- 1 | const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model); 2 | struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **error_message); 3 | -------------------------------------------------------------------------------- /code/func/setdir.m: -------------------------------------------------------------------------------- 1 | function [dpath,id]=setdir(dpath) 2 | % setdir - output dpath and make directory if the directory did not exist. 3 | % function [dpath,id]=setdir(dpath) 4 | % 5 | % 6 | % 7 | % 8 | % 9 | % Created by Tomoyasu Horikawa horikawa-t@atr.jp 2011/01/21 10 | % 11 | % 12 | id=exist(dpath,'dir'); 13 | if ~id 14 | mkdir(dpath) 15 | end 16 | 17 | -------------------------------------------------------------------------------- /code/func/addBias.m: -------------------------------------------------------------------------------- 1 | function X_1=addBias(X) 2 | % addBias - add the bias term 3 | % 4 | % [Input] 5 | % -X: training data [N x M] 6 | % 7 | % 8 | % [Output] 9 | % -X_1: training data with bias term [N x M+1] 10 | % 11 | % [Example] 12 | % X=randn(100,1); 13 | % X_1=addBias(X) 14 | % 15 | % 16 | % 17 | % 18 | % 19 | % [Related function] 20 | % 21 | % 22 | % Created By: Tomoyasu Horikawa horikawa-t@atr.jp 2009/10/11 23 | % 24 | % 25 | 26 | %% add the bias term 27 | X_1=[X,ones(size(X,1),1)]; 28 | -------------------------------------------------------------------------------- /code/func/reArrangeDataStructure.m: -------------------------------------------------------------------------------- 1 | function [D_new]=reArrangeDataStructure(D,params,fieldnames) 2 | 3 | % select voxels within the specified ROI 4 | D_new.data=D.data(:,D.metaData.(params.useRois)==1); 5 | 6 | % get label and label definition 7 | labelIdx=find(~cellfun(@isempty,strfind(fieldnames,'Synset_'))); 8 | for j=1:length(labelIdx) 9 | D_new.labels(:,j)=D.data(:,D.metaData.(fieldnames{labelIdx(j)})==1); 10 | end 11 | 12 | % get xyz coordinate 13 | D_new.labels_type=fieldnames(labelIdx)'; 14 | xyz=[D.metaData.X;D.metaData.Y;D.metaData.Z]; 15 | D_new.xyz=xyz(:,D.metaData.(params.useRois)==1); 16 | 17 | end 18 | 19 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/make.m: -------------------------------------------------------------------------------- 1 | % This make.m is used under Windows 2 | 3 | % add -largeArrayDims on 64-bit machines 4 | 5 | if ispc 6 | mex -largeArrayDims -O -c svm.cpp 7 | mex -largeArrayDims -O -c svm_model_matlab.c 8 | mex -largeArrayDims -O svmtrain.c svm.obj svm_model_matlab.obj 9 | mex -largeArrayDims -O svmpredict.c svm.obj svm_model_matlab.obj 10 | mex -largeArrayDims -O libsvmread.c 11 | mex -largeArrayDims -O libsvmwrite.c 12 | else 13 | mex -largeArrayDims -O -c svm.cpp 14 | mex -largeArrayDims -O -c svm_model_matlab.c 15 | mex -largeArrayDims -O svmtrain.c svm.o svm_model_matlab.o 16 | mex -largeArrayDims -O svmpredict.c svm.o svm_model_matlab.o 17 | mex -largeArrayDims -O libsvmread.c 18 | mex -largeArrayDims -O libsvmwrite.c 19 | end 20 | -------------------------------------------------------------------------------- /code/func/uninorm.m: -------------------------------------------------------------------------------- 1 | function n=uninorm(X,dir) 2 | % uninorm -- caclculate unit norm vector of input X 3 | % 4 | % [Inputs] 5 | % -X: data 6 | % -dir:direction [col=1(default), row=2] 7 | % 8 | % 9 | % [Outputs] 10 | % -n: unit norm vector matrix 11 | % 12 | % 13 | % 14 | % Written by Tomoyasu Horikawa horikawa-t@atr.jp 2011/10/07 15 | % 16 | % X=randn(10,5); 17 | % n=uninorm(X) 18 | % sum(n.*n,1) 19 | % n=uninorm(X,2) 20 | % sum(n.*n,2) 21 | % 22 | % 23 | %% 24 | if nargin < 2 25 | dir=1; 26 | end 27 | if ~isreal(X) 28 | warning('Computing distance table using imaginary inputs. Results may be off.'); 29 | end 30 | 31 | if dir==1 32 | X=X'; 33 | end 34 | % n=bsxfun(@rdivide,X,sqrt(diag(X*X'))); 35 | % unit norm 36 | n=X; 37 | for itr=1:size(n,1) 38 | n(itr,:)=X(itr,:)/sqrt(X(itr,:)*X(itr,:)'); 39 | end 40 | 41 | if dir==1 42 | n=n'; 43 | end 44 | 45 | %% 46 | 47 | -------------------------------------------------------------------------------- /code/func/selectTopTvals.m: -------------------------------------------------------------------------------- 1 | function [D,params]=selectTopTvals(D,params) 2 | conds = getFieldDef(params,'conds',[1,2]); 3 | num_comp = getFieldDef(params,'num_comp',size(D.data,2)); 4 | ind1=find(D.labels==conds(1)); 5 | ind2=find(D.labels==conds(2)); 6 | 7 | % class1 8 | n1=length(ind1); 9 | mu1=mean(D.data(ind1,:),1); 10 | var1=var(D.data(ind1,:),[],1); 11 | % class 2 12 | n2=length(ind2); 13 | mu2=mean(D.data(ind2,:),1); 14 | var2=var(D.data(ind2,:),[],1); 15 | 16 | % calc tvals 17 | s=sqrt((var1*(n1-1)+var2*(n2-1))/(n1+n2-2)); 18 | tvals=(mu1-mu2)./(s*sqrt(1/n1+1/n2)); 19 | tvals =abs(tvals)'; 20 | 21 | % Select top N within range 22 | [tvals, inds_tvals] = sort(tvals,'descend'); 23 | 24 | num_comp = min(abs(num_comp),length(inds_tvals)); 25 | inds_tvals = inds_tvals(1:abs(num_comp)); 26 | D.data = D.data(:,inds_tvals); 27 | D.xyz = D.xyz(:,inds_tvals); 28 | 29 | params.tvals = tvals(1:abs(num_comp)); 30 | params.inds_tvals = inds_tvals; 31 | 32 | end -------------------------------------------------------------------------------- /code/func/getFieldDef.m: -------------------------------------------------------------------------------- 1 | function out = getFieldDef(S, field, default) 2 | %getFieldDef - returns either S. or default 3 | %out = getFieldDef(S, field, default) 4 | % 5 | % If string 'field' is a field of structure S, it returns this value 6 | % otherwise, it returns default in out. 7 | % 8 | % Input: 9 | % S - any structure 10 | % field - string with possible field of S 11 | % default - default to assign to 'out', if 'field' doesn't exist 12 | % Output: 13 | % out - any variable output 14 | % 15 | % Created By: Alex Harner (1), alexh@atr.jp 06/07/05 16 | % Modified By: Alex Harner (1), alexh@atr.jp 06/07/12 17 | % Modified By: Satoshi MURATA (1), satoshi-m@atr.jp 08/08/21 18 | % (1) ATR Intl. Computational Neuroscience Labs, Decoding Group 19 | 20 | 21 | %% Check and get pars: 22 | if exist('default','var')==0 || isempty(default) 23 | default = []; 24 | end 25 | 26 | if exist('S','var')==0 || isempty(S) || exist('field','var')==0 || isempty(field) 27 | out = default; 28 | return; 29 | end 30 | 31 | 32 | %% Return value: 33 | out = default; 34 | if isfield(S,field) 35 | out = S.(field); 36 | end 37 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/COPYRIGHT: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2000-2010 Chih-Chung Chang and Chih-Jen Lin 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | 3. Neither name of copyright holders nor the names of its contributors 17 | may be used to endorse or promote products derived from this software 18 | without specific prior written permission. 19 | 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/Makefile: -------------------------------------------------------------------------------- 1 | # This Makefile is used under Linux 2 | 3 | #MATLABDIR ?= /usr/local/matlab 4 | MATLABDIR ?= /home/cns/matlab/r2007b 5 | # for Mac 6 | # MATLABDIR ?= /opt/local/matlab 7 | 8 | CXX ?= g++ 9 | #CXX = g++-4.1 10 | CFLAGS = -Wall -Wconversion -O3 -fPIC -I$(MATLABDIR)/extern/include 11 | 12 | MEX = $(MATLABDIR)/bin/mex 13 | MEX_OPTION = CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)" 14 | # comment the following line if you use MATLAB on 32-bit computer 15 | MEX_OPTION += -largeArrayDims 16 | MEX_EXT = $(shell $(MATLABDIR)/bin/mexext) 17 | 18 | OCTAVEDIR ?= /usr/include/octave 19 | OCTAVE_MEX = env CC=$(CXX) mkoctfile 20 | OCTAVE_MEX_OPTION = --mex 21 | OCTAVE_MEX_EXT = mex 22 | OCTAVE_CFLAGS = -Wall -O3 -fPIC -I$(OCTAVEDIR) 23 | 24 | all: matlab 25 | 26 | matlab: binary 27 | 28 | octave: 29 | @make MEX="$(OCTAVE_MEX)" MEX_OPTION="$(OCTAVE_MEX_OPTION)" \ 30 | MEX_EXT="$(OCTAVE_MEX_EXT)" CFLAGS="$(OCTAVE_CFLAGS)" \ 31 | binary 32 | 33 | binary: svmpredict.$(MEX_EXT) svmtrain.$(MEX_EXT) libsvmread.$(MEX_EXT) libsvmwrite.$(MEX_EXT) 34 | 35 | svmpredict.$(MEX_EXT): svmpredict.c svm.h svm.o svm_model_matlab.o 36 | $(MEX) $(MEX_OPTION) svmpredict.c svm.o svm_model_matlab.o 37 | 38 | svmtrain.$(MEX_EXT): svmtrain.c svm.h svm.o svm_model_matlab.o 39 | $(MEX) $(MEX_OPTION) svmtrain.c svm.o svm_model_matlab.o 40 | 41 | libsvmread.$(MEX_EXT): libsvmread.c 42 | $(MEX) $(MEX_OPTION) libsvmread.c 43 | 44 | libsvmwrite.$(MEX_EXT): libsvmwrite.c 45 | $(MEX) $(MEX_OPTION) libsvmwrite.c 46 | 47 | svm_model_matlab.o: svm_model_matlab.c svm.h 48 | $(CXX) $(CFLAGS) -c svm_model_matlab.c 49 | 50 | svm.o: svm.cpp svm.h 51 | $(CXX) $(CFLAGS) -c svm.cpp 52 | 53 | clean: 54 | rm -f *~ *.o *.mex* *.obj 55 | -------------------------------------------------------------------------------- /code/func/reIndex.m: -------------------------------------------------------------------------------- 1 | function [list_new, new_indx, old_indx] = reIndex(list, new_indx, old_indx) 2 | %reIndex - re-indexes a list, replacing old_indx items with new_indx items 3 | %[list_new, new_indx, old_indx] = reIndex(list, new_indx, old_indx) 4 | % 5 | % Replaces items of 'list' (any matrix) specified in old_indx with corresponding items in 6 | % new_indx. If old_indx is absent or [], items are found with with unique(list), which 7 | % sorts them. If new_indx is absent or [], then it uses [1 2 3 ...] as the new index. 8 | % 9 | % Exp: 10 | % >> list = [-1 0 153 0; 0 153 153 -1] 11 | % -1 0 153 0 12 | % 0 153 153 -1 13 | % >> reIndex(list,[0 3 2]) 14 | % 0 3 2 3 15 | % 3 2 2 0 16 | % >> reIndex(list) 17 | % 1 2 3 2 18 | % 2 3 3 1 19 | % 20 | % Created By: Alex Harner (1), alexh@atr.jp 06/10/27 21 | % Modified By: Alex Harner (1), alexh@atr.jp 06/10/30 22 | % Modified By: Satoshi MURATA (1), satoshi-m@atr.jp 08/10/09 23 | % (1) ATR Intl. Computational Neuroscience Labs, Decoding Group 24 | 25 | 26 | %% Check and get pars: 27 | if exist('list','var')==0 || isempty(list) 28 | list_new = []; 29 | new_indx = []; 30 | old_indx = []; 31 | return; 32 | end 33 | 34 | if exist('old_indx','var')==0 || isempty(old_indx) 35 | old_indx = unique(list)'; 36 | end 37 | if exist('new_indx','var')==0 || isempty(new_indx) 38 | new_indx = 1:length(old_indx); 39 | end 40 | 41 | if length(old_indx)~=length(new_indx) 42 | error('''new_indx'' and ''old_indx'' must be same length'); 43 | end 44 | 45 | 46 | %% Re-indexes: 47 | list_new = list; 48 | for iti=1:length(old_indx) 49 | list_new(list==old_indx(iti)) = new_indx(iti); 50 | end 51 | -------------------------------------------------------------------------------- /code/func/cpDetect.m: -------------------------------------------------------------------------------- 1 | function [cpm cpcomp cplen]=cpDetect(input) 2 | % cpDetect -- detect the changepoint of components in input vector 3 | % this function can be used for converting design matrix to ***_inds data/ 4 | % 5 | % [Input] 6 | % -input: vector 7 | % 8 | % [Output] 9 | % -cpm : change point index matrix 10 | % -cpcomp : top components of each cp interval 11 | % -cplen : lengh of each cp interval 12 | % 13 | % e.g. 14 | % input=[1,1,1,2,2,3,3,3,3,3,1,1,1,1,2] 15 | % [cpm cpcomp]=cpDetect(input) 16 | % cpm = 17 | % 18 | % 1 4 6 11 15 19 | % 3 5 10 14 15 20 | % cpcomp = 21 | % 1 2 3 1 2 22 | % 23 | % input=['r','r','r','b','b','b','r','r','r'] 24 | % cpm = 25 | % 1 4 7 26 | % 3 6 9 27 | % cpcomp = 28 | % rbr 29 | % 30 | % input=[{'ra'},{'ra'},{'rrr'},{'br'},{'br'},{'rb'},{'rb'}] 31 | % cpm = 32 | % 1 3 4 6 33 | % 2 3 5 7 34 | % cpcomp = 35 | % 'ra' 'rrr' 'br' 'rb' 36 | % 37 | % created by HORIKAWA tomoyasu 09/09/03 38 | % modified by HORIKAWA tomoyasu 09/10/22 add cpcomp 39 | a=input(1); 40 | cpm=zeros(2,1); 41 | count=1; 42 | if isnumeric(a)||islogical(a) 43 | for i=1:length(input) 44 | if input(i)~=a 45 | cpm(2,count)=i-1; 46 | cpm(1,count+1)=i; 47 | count=count+1; 48 | a=input(i); 49 | end 50 | end 51 | elseif ischar(a)||iscell(a) 52 | for i=1:length(input) 53 | if ~strcmp(input(i),a) 54 | cpm(2,count)=i-1; 55 | cpm(1,count+1)=i; 56 | count=count+1; 57 | a=input(i); 58 | end 59 | end 60 | 61 | end 62 | cpm(1,1)=1; 63 | cpm(end)=length(input); 64 | cpcomp=input(cpm(1,:)); 65 | cplen=cpm(2,:)-cpm(1,:)+1; 66 | -------------------------------------------------------------------------------- /code/func/normFeature.m: -------------------------------------------------------------------------------- 1 | function [data normMean normScale] = normFeature(data,meanMode,scaleMode,normMean,normScale) 2 | % 3 | % normFeature - normalize feature vector by specified mode 4 | % 5 | % [input] 6 | % - data: [samples x features] 7 | % - meanMode: mean normalization mode [all, feature, sample, or none] 8 | % - scaleMode: scale normalization mode [all, feature, sample, or none] 9 | % - normMean: if supplied, normalized by this value(s). should match 10 | % feature dim 11 | % - normScale: if supplied, normalized by this value(s). should match 12 | % feature dim 13 | % 14 | % [output] 15 | % - data: normalized data [samples x features] 16 | % - normMean: mean used for normalization [scalar, or vector] 17 | % - normScale: scale used for normalization [scalar, or vector] 18 | % 19 | % [note] 20 | % - mostly identical what normalize_feature.m in SLR toolbox does. 21 | % 22 | % 09/09/10 written by Yoichi Miyawaki (yoichi_m@atr.jp) 23 | % 2015/09/07 modified by Tomoyasu Horikawa (horikawa-t@atr.jp) 24 | 25 | if nargin < 3 || nargin > 5 26 | 27 | help normFeature; 28 | error; 29 | return; 30 | 31 | end 32 | 33 | 34 | if nargin < 5 35 | 36 | switch scaleMode 37 | case 'all' 38 | %normScale = max(abs(data(:))); 39 | normScale = std(data(:)); 40 | case 'feature' 41 | %normScale = max(abs(data),[],1); 42 | normScale = std(data,0,1); 43 | case 'sample' 44 | %normScale = max(abs(data),[],1); 45 | normScale = std(data,0,2); 46 | case 'none' 47 | normScale = 1; 48 | end 49 | 50 | end 51 | 52 | 53 | if nargin < 4 54 | 55 | switch meanMode 56 | case 'all' 57 | normMean = mean(data(:)); 58 | case 'feature' 59 | normMean = mean(data,1); 60 | case 'sample' 61 | normMean = mean(data,2); 62 | case 'none' 63 | normMean = 0; 64 | end 65 | 66 | end 67 | 68 | 69 | data = data - repmat(normMean,size(data,1),1); 70 | data = data ./repmat(normScale,size(data,1),1); 71 | 72 | 73 | 74 | % end -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Human Dream Decoding 2 | 3 | ![sleep.jpg](sleep.jpg) 4 | 5 | This work has been published in *Science*, 6 | 7 | Horikawa, T., Tamaki, M., Miyawaki, Y., & Kamitani, Y. (2013) "Neural decoding of visual imagery during sleep" Science 340, 639-642. 8 | 9 | ## Usage 10 | 11 | ### Download fMRI data 12 | 13 | First, download raw fMRI data from and put them in `data/raw/` directory. 14 | Data for all three subjects and all sessions are required. 15 | Because the raw fMRI data is very large (>30GB), if you want to skip the following preprocessing step and start from the decoding analyses, you can download preprocessed data from the Supplementary File of the above web page. 16 | 17 | ### fMRI data preprocessing 18 | 19 | To convert raw fMRI data to preprocessed data, change directory to the code/ and type: 20 | 21 | ``` matlab 22 | >> convertRawDatatoPreprocessedData 23 | ``` 24 | 25 | in the MATLAB terminal. 26 | The preprocessed data will be saved in `data/preproc/` directory. 27 | Because the preprocessing requires over 30 GB memory (and it takes about an hour), it may be better to run this code on your cluster machine, not on your local PC. 28 | 29 | ### Perform stimulus-to-dream decoding anlaysis 30 | 31 | To perform decoding analyses, type: 32 | 33 | ``` matlab 34 | >> Perception2SleepDecoding_BATCH 35 | ``` 36 | 37 | This script performs the pair-wise decoding for all categories for all the subjects, and produces histograms of decoding accuracy for all the pairs (cf. Figure 3B in the original paper). 38 | 39 | ## Copyright and License 40 | 41 | Copyright (c) 2015 ATR Department of Neuroinformatics 42 | 43 | The data provided here is released under the Open Data Commons Attribution License, ODC BY (). 44 | 45 | The scripts provided here are released under the MIT license (). 46 | 47 | Contact: brainliner-admin@atr.jp 48 | -------------------------------------------------------------------------------- /code/func/performPerception2SleepDecoding.m: -------------------------------------------------------------------------------- 1 | function results=performPerception2SleepDecoding(Ds,Dp,params) 2 | % 3 | % perform dream decoding analysis using perception-trained decoder 4 | % 5 | 6 | %% sample selection 7 | % sleep data 8 | if params.class1==params.class2 % 1 vs others 9 | label1_ind=Ds.labels(:,params.class1)==1; 10 | label2_ind=Ds.labels(:,params.class2)==0; 11 | else % 1 vs 1 12 | label1_ind=Ds.labels(:,params.class1)==1; 13 | label2_ind=Ds.labels(:,params.class2)==1; 14 | end 15 | % remove overlaps (= samples labeled with both class) 16 | overlap=label1_ind==label2_ind; 17 | label1_ind(overlap)=0; 18 | label2_ind(overlap)=0; 19 | Ds.labels=sum([label1_ind,label2_ind*2],2); 20 | results.sample=[sum(label1_ind),sum(label2_ind)]; 21 | 22 | % perception data 23 | if params.class1==params.class2 % 1 vs others 24 | label1_ind=Dp.labels(:,params.class1)==1; 25 | label2_ind=Dp.labels(:,params.class2)==0; 26 | else % 1 vs 1 27 | label1_ind=Dp.labels(:,params.class1)==1; 28 | label2_ind=Dp.labels(:,params.class2)==1; 29 | end 30 | Dp.labels=sum([label1_ind,label2_ind*2],2); 31 | 32 | % use samples labeled with either the pairs 33 | Dp.data=Dp.data(Dp.labels>0,:); 34 | Dp.labels=Dp.labels(Dp.labels>0); 35 | 36 | %% voxel selection using paired t-test 37 | params.conds=[1,2]; % used labels 38 | [Dp,params]=selectTopTvals(Dp,params); 39 | 40 | %% z-normalization for each sample 41 | Dp.data=zscore(Dp.data')'; 42 | Ds.data=zscore(Ds.data')'; 43 | 44 | %% classification 45 | P.mode=1; 46 | [r]=libsvm_h(Dp,P); 47 | 48 | [nouse,ord]=ismember(Dp.xyz',Ds.xyz','rows'); 49 | w=zeros(size(Ds.data,2),1); 50 | w_tmp=uninorm(r.weights); 51 | w(ord)=w_tmp; 52 | bias=r.bias/sqrt(sum(r.weights.^2)); 53 | w=[w;bias]; 54 | dcv=addBias(Ds.data)*w; 55 | if Dp.labels(1)==1 56 | r.preds=(dcv<0)+1; 57 | else 58 | r.preds=(dcv>0)+1; 59 | end 60 | r.labels=Ds.labels; 61 | 62 | results.preds=r.preds; 63 | results.labels=r.labels; 64 | results.decvals=dcv; 65 | results.weights=w; 66 | results.corrRate=mean(r.preds(r.labels>0)==r.labels(r.labels>0))*100; 67 | %% end 68 | end 69 | -------------------------------------------------------------------------------- /code/func/getTri.m: -------------------------------------------------------------------------------- 1 | function [vec,r,c]=getTri(mat,lu) 2 | % getTri -- extract upper triangle componet with diagonal from matrix 3 | % 4 | % [Input] 5 | % -mat: square matrix 6 | % -lu: 'lower' or 'upper', default: get lower components 7 | % 8 | % [Output] 9 | % -vec:vector 10 | % -r:row index 11 | % -c:column index 12 | % 13 | % 14 | % 15 | % [note] 16 | % each component was extracted in the ? direction, not -> direction 17 | % if mat is symmetry, use squareform(vec) to recover original matrix 18 | % (except diag. comp.) from vec 19 | % 20 | % 21 | % [e.g.,] 22 | % mat=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16] 23 | % [v,r,c]=getTri(mat) 24 | % squareform(v) 25 | % 26 | % Created By Tomoyasu Horikawa horikawa-t@atr.jp 2009/10/27 27 | % 28 | r=zeros(size(mat,2)*(size(mat,2)-1)/2,1); 29 | c=zeros(size(mat,2)*(size(mat,2)-1)/2,1); 30 | if iscell(mat(1)) 31 | vec=cell(size(mat,2)*(size(mat,2)-1)/2,1); 32 | counter=0; 33 | if ~exist('lu','var') 34 | lu='l'; 35 | end 36 | switch lu(1) 37 | case 'l' 38 | for i=1:(size(mat,2)-1) 39 | for j=(i+1):size(mat,2) 40 | counter=counter+1; 41 | vec{counter}=mat{j,i}; 42 | r(counter)=j; 43 | c(counter)=i; 44 | end 45 | end 46 | case 'u' 47 | for i=1:(size(mat,2)-1) 48 | for j=(i+1):size(mat,2) 49 | counter=counter+1; 50 | vec{counter}=mat{i,j}; 51 | r(counter)=j; 52 | c(counter)=i; 53 | end 54 | end 55 | otherwise 56 | error('Invalid 2nd inputs.') 57 | end 58 | else 59 | vec=zeros(size(mat,2)*(size(mat,2)-1)/2,1); 60 | counter=0; 61 | if ~exist('lu','var') 62 | lu='l'; 63 | end 64 | switch lu(1) 65 | case 'l' 66 | for i=1:(size(mat,2)-1) 67 | for j=(i+1):size(mat,2) 68 | counter=counter+1; 69 | vec(counter)=mat(j,i); 70 | r(counter)=j; 71 | c(counter)=i; 72 | end 73 | end 74 | case 'u' 75 | for i=1:(size(mat,2)-1) 76 | for j=(i+1):size(mat,2) 77 | counter=counter+1; 78 | vec(counter)=mat(i,j); 79 | r(counter)=j; 80 | c(counter)=i; 81 | end 82 | end 83 | otherwise 84 | error('Invalid 2nd inputs.') 85 | end 86 | end 87 | end 88 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmwrite_org.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "mex.h" 5 | 6 | #if MX_API_VER < 0x07030000 7 | typedef int mwIndex; 8 | #endif 9 | 10 | void exit_with_help() 11 | { 12 | mexPrintf( 13 | "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" 14 | ); 15 | } 16 | 17 | void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat) 18 | { 19 | FILE *fp = fopen(filename,"w"); 20 | int i, k, low, high, l; 21 | mwIndex *ir, *jc; 22 | int label_vector_row_num; 23 | double *samples, *labels; 24 | mxArray *instance_mat_col; // instance sparse matrix in column format 25 | 26 | if(fp ==NULL) 27 | { 28 | mexPrintf("can't open output file %s\n",filename); 29 | return; 30 | } 31 | 32 | // transpose instance matrix 33 | { 34 | mxArray *prhs[1], *plhs[1]; 35 | prhs[0] = mxDuplicateArray(instance_mat); 36 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 37 | { 38 | mexPrintf("Error: cannot transpose instance matrix\n"); 39 | return; 40 | } 41 | instance_mat_col = plhs[0]; 42 | mxDestroyArray(prhs[0]); 43 | } 44 | 45 | // the number of instance 46 | l = (int) mxGetN(instance_mat_col); 47 | label_vector_row_num = (int) mxGetM(label_vec); 48 | 49 | if(label_vector_row_num!=l) 50 | { 51 | mexPrintf("Length of label vector does not match # of instances.\n"); 52 | return; 53 | } 54 | 55 | // each column is one instance 56 | labels = mxGetPr(label_vec); 57 | samples = mxGetPr(instance_mat_col); 58 | ir = mxGetIr(instance_mat_col); 59 | jc = mxGetJc(instance_mat_col); 60 | 61 | for(i=0;i 2 | #include 3 | #include 4 | #include "mex.h" 5 | 6 | #if MX_API_VER < 0x07030000 7 | typedef int mwIndex; 8 | #endif 9 | 10 | void exit_with_help() 11 | { 12 | mexPrintf( 13 | "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" 14 | ); 15 | } 16 | 17 | void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat) 18 | { 19 | FILE *fp = fopen(filename,"w"); 20 | int i, k, low, high, l; 21 | mwIndex *ir, *jc; 22 | int label_vector_row_num; 23 | double *samples, *labels; 24 | mxArray *instance_mat_col; /* instance sparse matrix in column format */ 25 | 26 | if(fp ==NULL) 27 | { 28 | mexPrintf("can't open output file %s\n",filename); 29 | return; 30 | } 31 | 32 | /* transpose instance matrix */ 33 | { 34 | mxArray *prhs[1], *plhs[1]; 35 | prhs[0] = mxDuplicateArray(instance_mat); 36 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 37 | { 38 | mexPrintf("Error: cannot transpose instance matrix\n"); 39 | return; 40 | } 41 | instance_mat_col = plhs[0]; 42 | mxDestroyArray(prhs[0]); 43 | } 44 | 45 | /* the number of instance */ 46 | l = (int) mxGetN(instance_mat_col); 47 | label_vector_row_num = (int) mxGetM(label_vec); 48 | 49 | if(label_vector_row_num!=l) 50 | { 51 | mexPrintf("Length of label vector does not match # of instances.\n"); 52 | return; 53 | } 54 | 55 | /* each column is one instance */ 56 | labels = mxGetPr(label_vec); 57 | samples = mxGetPr(instance_mat_col); 58 | ir = mxGetIr(instance_mat_col); 59 | jc = mxGetJc(instance_mat_col); 60 | 61 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "mex.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | #define max(x,y) (((x)>(y))?(x):(y)) 13 | #define min(x,y) (((x)<(y))?(x):(y)) 14 | 15 | void exit_with_help() 16 | { 17 | mexPrintf( 18 | "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" 19 | ); 20 | } 21 | 22 | static void fake_answer(mxArray *plhs[]) 23 | { 24 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 25 | plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); 26 | } 27 | 28 | static char *line; 29 | static int max_line_len; 30 | 31 | static char* readline(FILE *input) 32 | { 33 | int len; 34 | 35 | if(fgets(line,max_line_len,input) == NULL) 36 | return NULL; 37 | 38 | while(strrchr(line,'\n') == NULL) 39 | { 40 | max_line_len *= 2; 41 | line = (char *) realloc(line, max_line_len); 42 | len = (int) strlen(line); 43 | if(fgets(line+len,max_line_len-len,input) == NULL) 44 | break; 45 | } 46 | return line; 47 | } 48 | 49 | // read in a problem (in libsvm format) 50 | void read_problem(const char *filename, mxArray *plhs[]) 51 | { 52 | int max_index, min_index, inst_max_index, i; 53 | long elements, k; 54 | FILE *fp = fopen(filename,"r"); 55 | int l = 0; 56 | char *endptr; 57 | mwIndex *ir, *jc; 58 | double *labels, *samples; 59 | 60 | if(fp == NULL) 61 | { 62 | mexPrintf("can't open input file %s\n",filename); 63 | fake_answer(plhs); 64 | return; 65 | } 66 | 67 | max_line_len = 1024; 68 | line = (char *) malloc(max_line_len*sizeof(char)); 69 | 70 | max_index = 0; 71 | min_index = 1; // our index starts from 1 72 | elements = 0; 73 | while(readline(fp) != NULL) 74 | { 75 | char *idx, *val; 76 | // features 77 | int index = 0; 78 | 79 | inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has start from 0 80 | strtok(line," \t"); // label 81 | while (1) 82 | { 83 | idx = strtok(NULL,":"); // index:value 84 | val = strtok(NULL," \t"); 85 | if(val == NULL) 86 | break; 87 | 88 | errno = 0; 89 | index = (int) strtol(idx,&endptr,10); 90 | if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) 91 | { 92 | mexPrintf("Wrong input format at line %d\n",l+1); 93 | fake_answer(plhs); 94 | return; 95 | } 96 | else 97 | inst_max_index = index; 98 | 99 | min_index = min(min_index, index); 100 | elements++; 101 | } 102 | max_index = max(max_index, inst_max_index); 103 | l++; 104 | } 105 | rewind(fp); 106 | 107 | // y 108 | plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); 109 | // x^T 110 | if (min_index <= 0) 111 | plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); 112 | else 113 | plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); 114 | 115 | labels = mxGetPr(plhs[0]); 116 | samples = mxGetPr(plhs[1]); 117 | ir = mxGetIr(plhs[1]); 118 | jc = mxGetJc(plhs[1]); 119 | 120 | k=0; 121 | for(i=0;i start from 0 146 | 147 | errno = 0; 148 | samples[k] = strtod(val,&endptr); 149 | if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 150 | { 151 | mexPrintf("Wrong input format at line %d\n",i+1); 152 | fake_answer(plhs); 153 | return; 154 | } 155 | ++k; 156 | } 157 | } 158 | jc[l] = k; 159 | 160 | fclose(fp); 161 | free(line); 162 | 163 | { 164 | mxArray *rhs[1], *lhs[1]; 165 | rhs[0] = plhs[1]; 166 | if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) 167 | { 168 | mexPrintf("Error: cannot transpose problem\n"); 169 | fake_answer(plhs); 170 | return; 171 | } 172 | plhs[1] = lhs[0]; 173 | } 174 | } 175 | 176 | void mexFunction( int nlhs, mxArray *plhs[], 177 | int nrhs, const mxArray *prhs[] ) 178 | { 179 | if(nrhs == 1) 180 | { 181 | char filename[256]; 182 | 183 | mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); 184 | 185 | if(filename == NULL) 186 | { 187 | mexPrintf("Error: filename is NULL\n"); 188 | return; 189 | } 190 | 191 | read_problem(filename, plhs); 192 | } 193 | else 194 | { 195 | exit_with_help(); 196 | fake_answer(plhs); 197 | return; 198 | } 199 | } 200 | 201 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/libsvmread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "mex.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | #define max(x,y) (((x)>(y))?(x):(y)) 13 | #define min(x,y) (((x)<(y))?(x):(y)) 14 | 15 | void exit_with_help() 16 | { 17 | mexPrintf( 18 | "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" 19 | ); 20 | } 21 | 22 | static void fake_answer(mxArray *plhs[]) 23 | { 24 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 25 | plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); 26 | } 27 | 28 | static char *line; 29 | static int max_line_len; 30 | 31 | static char* readline(FILE *input) 32 | { 33 | int len; 34 | 35 | if(fgets(line,max_line_len,input) == NULL) 36 | return NULL; 37 | 38 | while(strrchr(line,'\n') == NULL) 39 | { 40 | max_line_len *= 2; 41 | line = (char *) realloc(line, max_line_len); 42 | len = (int) strlen(line); 43 | if(fgets(line+len,max_line_len-len,input) == NULL) 44 | break; 45 | } 46 | return line; 47 | } 48 | 49 | /* read in a problem (in libsvm format) */ 50 | void read_problem(const char *filename, mxArray *plhs[]) 51 | { 52 | int max_index, min_index, inst_max_index, i; 53 | long elements, k; 54 | FILE *fp = fopen(filename,"r"); 55 | int l = 0; 56 | char *endptr; 57 | mwIndex *ir, *jc; 58 | double *labels, *samples; 59 | 60 | if(fp == NULL) 61 | { 62 | mexPrintf("can't open input file %s\n",filename); 63 | fake_answer(plhs); 64 | return; 65 | } 66 | 67 | max_line_len = 1024; 68 | line = (char *) malloc(max_line_len*sizeof(char)); 69 | 70 | max_index = 0; 71 | min_index = 1; /* our index starts from 1 */ 72 | elements = 0; 73 | while(readline(fp) != NULL) 74 | { 75 | char *idx, *val; 76 | /* features */ 77 | int index = 0; 78 | 79 | inst_max_index = -1; /* strtol gives 0 if wrong format, and precomputed kernel has start from 0 */ 80 | strtok(line," \t"); /* label */ 81 | while (1) 82 | { 83 | idx = strtok(NULL,":"); /* index:value */ 84 | val = strtok(NULL," \t"); 85 | if(val == NULL) 86 | break; 87 | 88 | errno = 0; 89 | index = (int) strtol(idx,&endptr,10); 90 | if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) 91 | { 92 | mexPrintf("Wrong input format at line %d\n",l+1); 93 | fake_answer(plhs); 94 | return; 95 | } 96 | else 97 | inst_max_index = index; 98 | 99 | min_index = min(min_index, index); 100 | elements++; 101 | } 102 | max_index = max(max_index, inst_max_index); 103 | l++; 104 | } 105 | rewind(fp); 106 | 107 | /* y */ 108 | plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); 109 | /* x^T */ 110 | if (min_index <= 0) 111 | plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); 112 | else 113 | plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); 114 | 115 | labels = mxGetPr(plhs[0]); 116 | samples = mxGetPr(plhs[1]); 117 | ir = mxGetIr(plhs[1]); 118 | jc = mxGetJc(plhs[1]); 119 | 120 | k=0; 121 | for(i=0;i start from 0 */ 146 | 147 | errno = 0; 148 | samples[k] = strtod(val,&endptr); 149 | if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 150 | { 151 | mexPrintf("Wrong input format at line %d\n",i+1); 152 | fake_answer(plhs); 153 | return; 154 | } 155 | ++k; 156 | } 157 | } 158 | jc[l] = k; 159 | 160 | fclose(fp); 161 | free(line); 162 | 163 | { 164 | mxArray *rhs[1], *lhs[1]; 165 | rhs[0] = plhs[1]; 166 | if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) 167 | { 168 | mexPrintf("Error: cannot transpose problem\n"); 169 | fake_answer(plhs); 170 | return; 171 | } 172 | plhs[1] = lhs[0]; 173 | } 174 | } 175 | 176 | void mexFunction( int nlhs, mxArray *plhs[], 177 | int nrhs, const mxArray *prhs[] ) 178 | { 179 | if(nrhs == 1) 180 | { 181 | char filename[256]; 182 | 183 | mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); 184 | 185 | if(filename == NULL) 186 | { 187 | mexPrintf("Error: filename is NULL\n"); 188 | return; 189 | } 190 | 191 | read_problem(filename, plhs); 192 | } 193 | else 194 | { 195 | exit_with_help(); 196 | fake_answer(plhs); 197 | return; 198 | } 199 | } 200 | 201 | -------------------------------------------------------------------------------- /code/func/libsvm_h.m: -------------------------------------------------------------------------------- 1 | function [results, pars] = libsvm_h(D, pars) 2 | %libsvm_h - performs multi SVM using libsvm-mat, either train or test 3 | %[results, pars] = libsvm_h(D, pars) 4 | % 5 | % Inputs: 6 | % D.data - 2D matrix of data 7 | % D.labels - labels matching samples of 'data'; only [] for test mode 8 | % 9 | % Optional: 10 | % pars.model- SVM 'model', including weights; optional for training 11 | % pars.mode - train=1 (make weights) or test=2 mode (use weights) 12 | % pars.conds - class label, which is necessary to relabel correctly 13 | % 14 | % 15 | % LibSVM pars fields: 16 | % .kernel - 0=linear, 1=poly, 2=rbf, 3=sigmoid; default=0 17 | % .cost - C of C-SVC, epsilon-SVR, and nu-SVR; default=1 18 | % .gamma - set gamma in kernel function; default 1/k 19 | % .coef - set coef0 in kernel function; default=0 20 | % .degree - set degree in kernel function; default=3 21 | % .prob - output probabilities as decVals? 0-no, 1-yes; default=1 22 | % 23 | % Outputs: 24 | % results - struct contain ANY result as a field, typically: 25 | % .mode - name of this function 26 | % .pars - parameters used (minus weights) 27 | % .decVals- decision values (raw classifier output) 28 | % .preds - labels predicted by the models 29 | % pars - modified pars, new weights will be added here 30 | % 31 | % Note: modelSwitch will make the remaining fields of results. 32 | % 33 | % Example: 34 | % >> % dataTrain, dataTest - [nSigs x nSamples] 35 | % >> pars.verbose = 0; % to avoid printing (new) 36 | % >> % 1 - train mode: 37 | % >> [weights, results, pars] = libsvm_h(dataTrain, pars, labelsTrain, 1); 38 | % >> % 2 - test mode: 39 | % >> [weights, results, pars] = libsvm_h(dataTest, pars, labelsTest, 2); 40 | % 41 | % Calls: svmtrain, svmpredict (help svmtrain for more info) 42 | % Requires: libsvm-mat-3.01, (c) 2000-2005 Chih-Chung Chang & Chih-Jen Lin 43 | % Info: http://www.csie.ntu.edu.tw/~cjlin/libsvm 44 | % Status: basic testing 45 | % 46 | 47 | 48 | %% Check and get pars: 49 | if exist('D','var')==0 || isempty(D), error('Wrong args'); end 50 | if exist('pars','var')==0 || isempty(pars), pars = []; end 51 | 52 | pars = getFieldDef(pars,mfilename,pars); % unnest, if need 53 | model = getFieldDef(pars,'model',[]); 54 | mode = getFieldDef(pars,'mode',1); 55 | conds = getFieldDef(pars,'conds',[]); 56 | normMeanMode = getFieldDef(pars,'normMeanMode','feature'); 57 | normScaleMode = getFieldDef(pars,'normScaleMode','feature'); 58 | normMean = getFieldDef(pars,'normMean',0); 59 | normScale = getFieldDef(pars,'normScale',1); 60 | normMode = getFieldDef(pars,'normMode','test'); 61 | 62 | 63 | if mode==1 && isempty(D.labels), error('must have ''labels'' for train'); 64 | elseif mode==2 && isempty(model), error('must have ''model'' for test'); end 65 | 66 | if ~isempty(conds) && max(conds)~=length(unique(conds)) || isempty(find(conds==0,1))==0 67 | fprintf('\nWarning: unrecommended format of ''labels'''); 68 | fprintf('\n rename ''labels'' in ''libsvm''\n'); 69 | 70 | [D.labels, labels_new, labels_old] = reIndex(D.labels,1:length(conds),conds); 71 | end 72 | 73 | %% SVM pars: 74 | kernel = getFieldDef(pars,'kernel',0); % linear 75 | gamma = getFieldDef(pars,'gamma',0); % NOTE: gamma=0 defaults to 1/k 76 | prob = getFieldDef(pars,'prob',1); 77 | cost = getFieldDef(pars,'cost',1); 78 | coef = getFieldDef(pars,'coef',0); 79 | degree = getFieldDef(pars,'degree',3); 80 | 81 | ops1 = sprintf('-t %d -c %g -r %g -d %g -q ', kernel, cost, coef, degree); 82 | if prob, ops1 = [ops1 ' -b 1']; ops2 = '-b 1'; end 83 | if gamma, ops1 = [ops1 ' -g ' num2str(gamma)]; end 84 | 85 | 86 | %% Test mode: 87 | if mode==2 88 | if strcmp(normMode,'test') 89 | if size(D.data,1) == 1 && strcmp(normMeanMode,'feature') 90 | fprintf('\nWARNINIG: data sample size is 1. this normalization convnert all features into 0.\n'); 91 | end 92 | data = normFeature(D.data,normMeanMode,normScaleMode); 93 | elseif strcmp(normMode,'training') 94 | data = normFeature(D.data,normMeanMode,normScaleMode,normMean,normScale); 95 | else 96 | error('normalization mode error'); 97 | end 98 | 99 | [preds, nouse, dec_vals] = svmpredict(D.labels,data,model,ops2); 100 | 101 | 102 | %% Train mode: 103 | else 104 | 105 | [data normMean normScale] = normFeature(D.data,normMeanMode,normScaleMode); 106 | 107 | model = svmtrain(D.labels,data,ops1); 108 | [preds, nouse, dec_vals] = svmpredict(D.labels,data,model,ops2); 109 | 110 | pars.model = model; 111 | end 112 | 113 | %% Retrun results: 114 | if exist('labels_old','var') && isempty(labels_old)==0 115 | D.labels = reIndex(D.labels,labels_old,labels_new); 116 | preds = reIndex(preds,labels_old,labels_new); 117 | end 118 | 119 | 120 | %% Return results: 121 | results.model = mfilename; 122 | results.preds = preds; 123 | results.labels = D.labels; 124 | results.dec_vals = dec_vals; 125 | results.weights = model.SVs' * model.sv_coef; 126 | results.bias = -model.rho; 127 | 128 | -------------------------------------------------------------------------------- /code/func/readfMRIData.m: -------------------------------------------------------------------------------- 1 | function [iDs,iDp,props]=readfMRIData(p) 2 | % 3 | % integrate fMRI data for the specified subject 4 | % 5 | 6 | %% set parameters 7 | fMRIdataGroupIdx=1; 8 | labelGroupIdx4SleepData=[2:7]; 9 | labelGroupIdx4PerceptionData=[2:4]; 10 | 11 | switch p.subjectID 12 | case 1 13 | nSleepDataSession=26; 14 | nPerceptionDataSession=32; 15 | case 2 16 | nSleepDataSession=14; 17 | nPerceptionDataSession=40; 18 | case 3 19 | nSleepDataSession=15; 20 | nPerceptionDataSession=40; 21 | end 22 | %% read data 23 | fprintf('Read files...\n') 24 | % sleep data 25 | Ds=cell(nSleepDataSession,1); 26 | for i=1:nSleepDataSession 27 | fprintf('Sleep data loading(%d/%d)\n',i,nSleepDataSession) 28 | Ds{i}=readHDF5AsStruct([p.datdir,'SleepDataSubject',num2str(p.subjectID),'Session',num2str(i)]); 29 | end 30 | % perception data 31 | Dp=cell(nPerceptionDataSession,1); 32 | for i=1:nPerceptionDataSession 33 | fprintf('Perception data loading(%d/%d)\n',i,nPerceptionDataSession) 34 | Dp{i}=readHDF5AsStruct([p.datdir,'PerceptionDataSubject',num2str(p.subjectID),'Session',num2str(i)]); 35 | end 36 | 37 | 38 | %% integrate fMRI data 39 | fprintf('Integrate fMRI data...\n') 40 | fMRIgroupName=['group',num2str(fMRIdataGroupIdx)]; 41 | thresVals=100; 42 | 43 | % integrate data 44 | data_tmp=shiftdim(Ds{1}.(fMRIgroupName).data,1); 45 | tmp_mask=data_tmp(:,:,:,1)>thresVals; 46 | tmp_mask=tmp_mask(:); 47 | tmp_nVox=sum(tmp_mask); 48 | clear data_tmp 49 | 50 | iDs=integrateData(Ds,nSleepDataSession,fMRIgroupName,tmp_mask); 51 | iDp=integrateData(Dp,nPerceptionDataSession,fMRIgroupName,tmp_mask); 52 | 53 | % extract ROI mask 54 | % the ROI masks are common across files [Ds(i) and Dp(i)] 55 | roiNames=fieldnames(Ds{1}.(fMRIgroupName).props.roi); 56 | nRois=length(roiNames); 57 | roiMask=zeros(nRois,tmp_nVox); 58 | for i=1:nRois 59 | tmp=Ds{1}.(fMRIgroupName).props.roi.(roiNames{i})(:); 60 | roiMask(i,:)=tmp(tmp_mask); 61 | end 62 | thresVox=any([iDs.data;iDp.data]>thresVals); 63 | props.roiMask=roiMask(:,thresVox); 64 | props.roiNames=roiNames; 65 | 66 | % reserve non-zero voxel data 67 | iDs.data=iDs.data(:,thresVox); 68 | iDp.data=iDp.data(:,thresVox); 69 | 70 | % reserve xyz coordinate 71 | iDs.xyz=Ds{1}.(fMRIgroupName).props.location(:,tmp_mask); 72 | iDp.xyz=Dp{1}.(fMRIgroupName).props.location(:,tmp_mask); 73 | iDs.xyz=iDs.xyz(:,thresVox); 74 | iDp.xyz=iDp.xyz(:,thresVox); 75 | 76 | %% integrate labels 77 | fprintf('Integrate labels...\n') 78 | % sleep data 79 | nlabelSleepData=length(labelGroupIdx4SleepData); 80 | nlabelPerceptionData=length(labelGroupIdx4PerceptionData); 81 | 82 | % sleep data 83 | cnt=0; 84 | iDs.labels=cell(1,nlabelSleepData); 85 | for j=1:nlabelSleepData 86 | label_tmp=cell(1,nSleepDataSession); 87 | labelGroupName=['group',num2str(labelGroupIdx4SleepData(j))]; 88 | for i=1:nSleepDataSession 89 | label_tmp{i}=Ds{i}.(labelGroupName).data'; 90 | end 91 | iDs.labels{j}=[label_tmp{:}]'; 92 | if strcmp(Ds{1}.(labelGroupName).props.title,'Base synset labels') 93 | for k=1:length(Ds{1}.(labelGroupName).props.labelNames) 94 | cnt=cnt+1; 95 | iDs.labels_type{cnt}=['Synset_',Ds{1}.(labelGroupName).props.labelNames{k}]; 96 | iDs.labels_def{cnt}=Ds{1}.(labelGroupName).props.description; 97 | end 98 | else 99 | cnt=cnt+1; 100 | iDs.labels_type{cnt}=Ds{1}.(labelGroupName).props.title; 101 | iDs.labels_def{cnt}=Ds{1}.(labelGroupName).props.description; 102 | end 103 | end 104 | iDs.labels=[iDs.labels{:}]; 105 | 106 | % perception data 107 | cnt=0; 108 | iDp.labels=cell(1,nlabelPerceptionData); 109 | for j=1:nlabelPerceptionData 110 | label_tmp=cell(1,nlabelPerceptionData); 111 | labelGroupName=['group',num2str(labelGroupIdx4PerceptionData(j))]; 112 | for i=1:nPerceptionDataSession 113 | label_tmp{i}=Dp{i}.(labelGroupName).data'; 114 | end 115 | iDp.labels{j}=[label_tmp{:}]'; 116 | if strcmp(Dp{1}.(labelGroupName).props.title,'Base synset labels') 117 | for k=1:length(Dp{1}.(labelGroupName).props.labelNames) 118 | cnt=cnt+1; 119 | iDp.labels_type{cnt}=['Synset_',Dp{1}.(labelGroupName).props.labelNames{k}]; 120 | iDp.labels_def{cnt}=Dp{1}.(labelGroupName).props.description; 121 | end 122 | else 123 | cnt=cnt+1; 124 | iDp.labels_type{cnt}=Dp{1}.(labelGroupName).props.title; 125 | iDp.labels_def{cnt}=Dp{1}.(labelGroupName).props.description; 126 | end 127 | end 128 | iDp.labels=[iDp.labels{:}]; 129 | 130 | end 131 | 132 | %% functions 133 | function iD=integrateData(D,nDataSession,fMRIgroupName,tmp_mask) 134 | iD.data=cell(nDataSession,1); 135 | for i=1:nDataSession 136 | data_tmp=shiftdim(D{i}.(fMRIgroupName).data,1); 137 | n=size(data_tmp,4); 138 | nVox=sum(tmp_mask); 139 | iD.data{i}=zeros(nVox,n); 140 | for j=1:n 141 | tmp=data_tmp(:,:,:,j); 142 | iD.data{i}(:,j)=tmp(tmp_mask); 143 | end 144 | end 145 | iD.data=[iD.data{:}]'; 146 | end 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /code/convertRawDatatoPreprocessedData.m: -------------------------------------------------------------------------------- 1 | % function convertRawDatatoPreprocessedData 2 | % 3 | % [note] 4 | % Run this script first to create and save preprocessed data. 5 | % it takes about 30 minutes to run this script. 6 | % 7 | % [main variables] 8 | % Ds - raw data structure of sleep experiment (all voxels with non-zero values) 9 | % Ds,data - 2D matrix of fMRI data ([time(sample) x space(voxel)] format) 10 | % Ds.xyz - xyz coordinate of each voxels the individual brain [3 x voxel] 11 | % Ds.labels - condition labels of each sample ([time x 1] format) 12 | % Ds.labels_type - label names. each corresponds to each column of .labels field 13 | % Ds.labels_def - definition of label value in .labels field 14 | % 15 | % Dp - raw data structure of perception experiment (all voxels with non-zero values) 16 | % Dp,data - 2D matrix of fMRI data ([time(sample) x space(voxel)] format) 17 | % Dp.xyz - xyz coordinate of each voxels the individual brain [3 x voxel] 18 | % Dp.labels - condition labels of each sample ([time x 1] format) 19 | % Dp.labels_type - label names. each corresponds to each column of .labels field 20 | % Dp.labels_def - definition of label value in .labels field 21 | % 22 | % props - property structure 23 | % props.synsetPairs - synsetPairs list for pairwise decoding analysis 24 | % 25 | % pDs - Preprocessed data structure of sleep experiment (only voxels within pre-defined ROIs) 26 | % pDs.data - 2D matrix of [fMRIdata, labels, design] 27 | % pDs.metaData - label index which is used to extract corresponding 28 | % data from the .data field 29 | % pDs.metaData.voxelData - index for voxelData-columns in .data field 30 | % pDs.metaData.(X,Y,Z) - xyz coordinates of corresponding voxeldata 31 | % pDs.metaData.(ROI:e.g., V1) - index for voxels within each ROI 32 | % pDs.metaData.label - index for label-columns in .data field 33 | % pDs.metaData.EEG_sleep_score - index for eeg score label in .data field 34 | % pDs.metaData.Synset_XXX - index for each object(synset) category in .data field 35 | % pDs.metaData.design - index for design-columns in .data field 36 | % pDs.metaData.session_number - index for session number of fMRI data in .data field 37 | % pDs.metaData.sample_number - index for sample number of fMRI data in .data field 38 | % pDs.metaDefinition - definition of label and design values 39 | % 40 | % pDp - Preprocessed data structure of perception experiment (only voxels within pre-defined ROIs) 41 | % pDp.data - 2D matrix of [fMRIdata, labels, design] 42 | % pDp.metaData - label index which is used to extract corresponding 43 | % data from the .data field 44 | % pDp.metaData.voxelData - index for voxelData-columns in .data field 45 | % pDp.metaData.(ROI:e.g., V1) - index for voxels within each ROI 46 | % pDp.metaData.(X,Y,Z) - xyz coordinates of corresponding voxeldata 47 | % pDp.metaData.label - index for label-columns in .data field 48 | % pDp.metaData.Synset_XXX - index for each object(synset) category in .data field 49 | % pDp.metaData.design - index for design-columns in .data field 50 | % pDp.metaData.session_number - index for session number of fMRI data in .data field 51 | % pDp.metaData.sample_number - index for sample number of fMRI data in .data field 52 | % pDp.metaDefinition - definition of label and design values 53 | % 54 | % [Example] 55 | % % To get fMRIdata, labels, or design in pDp structure 56 | % voxelData = pDp.data(:,pDp.metaData.voxelData==1); 57 | % labels = pDp.data(:,pDp.metaData.label==1); 58 | % design = pDp.data(:,pDp.metaData.design==1); 59 | % 60 | % 61 | % 62 | % [Note] 63 | % In the preprocessing stage the following procedures were applied 64 | % -voxel selection within pre-defined ROIs 65 | % -shift perception data for hemodynamic delay 66 | % -reduce outliers 67 | % -linear detrending 68 | % -feature normalization 69 | % -averaging to create samples 70 | % -select synset pairs for pairwise decoding analysis 71 | % -exclude awake samples (EEG sleep score = 0) from sleep data 72 | % -exclude irrevant labels 73 | % -match label order of sleep and perception data 74 | % 75 | % 76 | % 2015/09/07 Written by Tomoyasu Horikawa horikawa-t@atr.jp 77 | % 78 | %clear all 79 | %% initial settings 80 | % add path to functions 81 | addpath('./func/'); 82 | % location of the donwloaded data 83 | p.datdir = '../data/raw/'; 84 | % location of the save directory 85 | p.savdir = setdir('../data/preproc/'); 86 | % # of subject 87 | nSubjects = 3; 88 | 89 | %% Read & preprocess fmri data: create decoding sample (it takes about an hour) 90 | for i = 1:nSubjects 91 | p.subjectID=i; 92 | 93 | fprintf('Read data for Subject%d======\n',i);tic 94 | [Ds,Dp,props_org] = readfMRIData(p);toc 95 | % if you want whole brain data, save Ds and Dp 96 | 97 | fprintf('Preprocess data======\n');tic 98 | [pDs,pDp,props] = preprocfMRIData(Ds,Dp,props_org);toc 99 | 100 | fprintf('Save preprocessed data======\n') 101 | writeHDF5FromStruct([p.savdir,'PreprocessedSleepDataSubject',num2str(i),'.h5'],pDs); 102 | writeHDF5FromStruct([p.savdir,'PreprocessedPerceptionDataSubject',num2str(i),'.h5'],pDp); 103 | writeHDF5FromStruct([p.savdir,'propsSubject',num2str(i),'.h5'],props); 104 | end 105 | 106 | %% 107 | -------------------------------------------------------------------------------- /code/func/convertRawDatatoPreprocessedDataCell.m: -------------------------------------------------------------------------------- 1 | % function convertRawDatatoPreprocessedDataCell 2 | % 3 | % [note] 4 | % Run this script first to create and save preprocessed data. 5 | % it takes about 30 minutes to run this script. 6 | % 7 | % [main variables] 8 | % Ds - raw data structure of sleep experiment (all voxels with non-zero values) 9 | % Ds,data - 2D matrix of fMRI data ([time(sample) x space(voxel)] format) 10 | % Ds.xyz - xyz coordinate of each voxels the individual brain [3 x voxel] 11 | % Ds.labels - condition labels of each sample ([time x 1] format) 12 | % Ds.labels_type - label names. each corresponds to each column of .labels field 13 | % Ds.labels_def - definition of label value in .labels field 14 | % 15 | % Dp - raw data structure of perception experiment (all voxels with non-zero values) 16 | % Dp,data - 2D matrix of fMRI data ([time(sample) x space(voxel)] format) 17 | % Dp.xyz - xyz coordinate of each voxels the individual brain [3 x voxel] 18 | % Dp.labels - condition labels of each sample ([time x 1] format) 19 | % Dp.labels_type - label names. each corresponds to each column of .labels field 20 | % Dp.labels_def - definition of label value in .labels field 21 | % 22 | % props - property structure 23 | % props.synsetPairs - synsetPairs list for pairwise decoding analysis 24 | % 25 | % pDs - Preprocessed data structure of sleep experiment (only voxels within pre-defined ROIs) 26 | % pDs.data - 2D matrix of [fMRIdata, labels, design] 27 | % pDs.metaData - label index which is used to extract corresponding 28 | % data from the .data field 29 | % pDs.metaData.voxelData - index for voxelData-columns in .data field 30 | % pDs.metaData.(X,Y,Z) - xyz coordinates of corresponding voxeldata 31 | % pDs.metaData.(ROI:e.g., V1) - index for voxels within each ROI 32 | % pDs.metaData.label - index for label-columns in .data field 33 | % pDs.metaData.EEG_sleep_score - index for eeg score label in .data field 34 | % pDs.metaData.Synset_XXX - index for each object(synset) category in .data field 35 | % pDs.metaData.design - index for design-columns in .data field 36 | % pDs.metaData.session_number - index for session number of fMRI data in .data field 37 | % pDs.metaData.sample_number - index for sample number of fMRI data in .data field 38 | % pDs.metaDefinition - definition of label and design values 39 | % 40 | % pDp - Preprocessed data structure of perception experiment (only voxels within pre-defined ROIs) 41 | % pDp.data - 2D matrix of [fMRIdata, labels, design] 42 | % pDp.metaData - label index which is used to extract corresponding 43 | % data from the .data field 44 | % pDp.metaData.voxelData - index for voxelData-columns in .data field 45 | % pDp.metaData.(ROI:e.g., V1) - index for voxels within each ROI 46 | % pDp.metaData.(X,Y,Z) - xyz coordinates of corresponding voxeldata 47 | % pDp.metaData.label - index for label-columns in .data field 48 | % pDp.metaData.Synset_XXX - index for each object(synset) category in .data field 49 | % pDp.metaData.design - index for design-columns in .data field 50 | % pDp.metaData.session_number - index for session number of fMRI data in .data field 51 | % pDp.metaData.sample_number - index for sample number of fMRI data in .data field 52 | % pDp.metaDefinition - definition of label and design values 53 | % 54 | % [Example] 55 | % % To get fMRIdata, labels, or design in pDp structure 56 | % voxelData = pDp.data(:,pDp.metaData.voxelData==1); 57 | % labels = pDp.data(:,pDp.metaData.label==1); 58 | % design = pDp.data(:,pDp.metaData.design==1); 59 | % 60 | % 61 | % 62 | % [Note] 63 | % In the preprocessing stage the following procedures were applied 64 | % -voxel selection within pre-defined ROIs 65 | % -shift perception data for hemodynamic delay 66 | % -reduce outliers 67 | % -linear detrending 68 | % -feature normalization 69 | % -averaging to create samples 70 | % -select synset pairs for pairwise decoding analysis 71 | % -exclude awake samples (EEG sleep score = 0) from sleep data 72 | % -exclude irrevant labels 73 | % -match label order of sleep and perception data 74 | % 75 | % 76 | % Written by Tomoyasu Horikawa horikawa-t@atr.jp 77 | % 78 | clear all 79 | %% initial settings 80 | % cd('../science2013') 81 | cd('/home/mu/horikawa-t/toolbox/public/science2013/') 82 | % add path to functions 83 | addpath('./func/'); 84 | % location of the donwloaded data 85 | p.datdir = './data/'; 86 | % location of the save directory 87 | p.savdir = setdir('./preproc_data_cell/'); 88 | % # of subject 89 | nSubjects = 3; 90 | 91 | %% Read & preprocess fmri data: create decoding sample (it takes about 30 minutes) 92 | for i = 1:nSubjects 93 | p.subjectID=i; 94 | 95 | fprintf('Read data for Subject%d======\n',i);tic 96 | [Ds,Dp,props] = readfMRIData(p);toc 97 | % if you want whole brain data, save Ds and Dp 98 | 99 | fprintf('Preprocess data======\n');tic 100 | [pDs,pDp,props] = preprocfMRIData_test_cell(Ds,Dp,props);toc 101 | 102 | fprintf('Save preprocessed data======\n') 103 | writeHDF5FromStruct([p.savdir,'PreprocessedSleepDataSubject',num2str(i),'.h5'],pDs); 104 | writeHDF5FromStruct([p.savdir,'PreprocessedPerceptionDataSubject',num2str(i),'.h5'],pDp); 105 | writeHDF5FromStruct([p.savdir,'propsSubject',num2str(i),'.h5'],props); 106 | end 107 | 108 | %% 109 | -------------------------------------------------------------------------------- /code/func/readHDF5AsStruct.m: -------------------------------------------------------------------------------- 1 | function [ data, info ] = readHDF5AsStruct(hdf5Path, rowMajor) 2 | % 3 | % Read an HDF5 file and convert it to a Matlab struct. 4 | % 5 | % ex.) [ data, info ] = readHDF5AsStruct('sampleData.hdf5') 6 | % 7 | % Input: 8 | % hdf5Path - path to the HDF5 file. 9 | % rowMajor - if 1 (default), data is assumed to be saved in row-major 10 | % order ( C/C++ or python style), so dimension of matrix is permutated. 11 | % if 0, dimension of matrix does'nt change. 12 | % 13 | % Output: 14 | % data - < 1x1 struct > data converted from the HDF5 file 15 | % info - < 1x1 struct > Information about the HDF5 file 16 | % ( output of hdf5info(hdf5Path) ). 17 | % 18 | %% 19 | if ~exist( 'hdf5Path', 'var') || isempty(hdf5Path) 20 | help(mfilename) 21 | error('Specify a path to an HDF5 file.') 22 | end 23 | if ~exist('rowMajor', 'var')|| isempty(rowMajor) 24 | rowMajor = 1; 25 | end 26 | 27 | newVersion = 0 ; % 1 if MATLAB version >= ver. 2011a 28 | matlabInfo = ver('MATLAB'); 29 | [ verIdx1, remain ] = strtok( matlabInfo.Version, '.'); 30 | if str2num(verIdx1) >= 8 31 | newVersion = 1; 32 | else 33 | verIdx2 = strtok( remain, '.') ; 34 | if str2num(verIdx1) == 7 && str2num(verIdx2) >= 12 35 | newVersion = 1; 36 | end 37 | end 38 | 39 | 40 | % get info 41 | info = hdf5info(hdf5Path); 42 | groupHierarchy = info.GroupHierarchy ; 43 | 44 | % load data 45 | data = []; 46 | data = readDataSet(data, groupHierarchy, newVersion, rowMajor ); 47 | 48 | end 49 | 50 | 51 | %% readDataSet 52 | % read DataSet and Groups in a hierarchical way 53 | function data = readDataSet(data, groupHierarchy, newVersion, rowMajor) 54 | 55 | if ~isempty(groupHierarchy.Groups) 56 | nGroup = length(groupHierarchy.Groups); 57 | for groupIdx = 1:nGroup 58 | data = readDataSet(data, groupHierarchy.Groups(groupIdx), newVersion, rowMajor); 59 | end 60 | end 61 | 62 | % load dataset 63 | if ~isempty(groupHierarchy.Datasets) 64 | nDatasets = length(groupHierarchy.Datasets); 65 | for dataIdx = 1:nDatasets 66 | dataSetName = groupHierarchy.Datasets(dataIdx).Name; 67 | % replace '.' to '_' if exist 68 | dataSetName( ismember(dataSetName, '.') ) = '_'; 69 | % replace '/' to '.' 70 | dataSetName( ismember(dataSetName, '/') ) = '.'; 71 | 72 | dataSet = hdf5read(groupHierarchy.Datasets(dataIdx)) ; 73 | 74 | % check if the type of the dataSet is a cell or string array or not. 75 | if strcmp( groupHierarchy.Datasets(dataIdx).Datatype.Class, 'H5T_ARRAY') || strcmp(groupHierarchy.Datasets(dataIdx).Datatype.Class, 'H5T_STRING') 76 | 77 | nElements = numel(dataSet); 78 | cellDataTemp = cell(1,nElements); 79 | dataSetTemp = reshape(dataSet, 1, nElements); 80 | for idx = 1:nElements 81 | if rowMajor 82 | if ~isscalar(dataSetTemp(idx).Data) && ~ischar(dataSetTemp(idx).Data) 83 | nDim = sum(size(dataSetTemp(idx).Data) >= 1); 84 | cellDataTemp{idx} = permute(dataSetTemp(idx).Data, [ nDim:-1:1 ] ); 85 | else 86 | cellDataTemp{idx} = dataSetTemp(idx).Data; 87 | end 88 | else 89 | cellDataTemp{idx} = dataSetTemp(idx).Data; 90 | end 91 | end 92 | cellData = reshape(cellDataTemp, size(dataSet)); 93 | 94 | % write 95 | evalString = [ 'data', dataSetName, ' = ', 'cellData ;' ] ; 96 | eval(evalString); 97 | 98 | if numel(cellData) == 1 99 | evalString = [ 'data', dataSetName, ' = ', 'cell2mat(data', dataSetName, ') ;' ] ; 100 | eval(evalString); 101 | 102 | elseif rowMajor 103 | evalString = [ 'dataSize = size(data', dataSetName, ') ;' ]; 104 | eval(evalString); 105 | dataDim = length(dataSize); 106 | if dataDim > 2 || ( dataSize(1) > 1 && dataSize(2) > 1 ); 107 | evalString = [ 'data', dataSetName, ' = permute(data', dataSetName, ',[ ', num2str(dataDim:-1:1), '] );'] ; 108 | eval(evalString); 109 | end 110 | end 111 | 112 | else 113 | evalString = [ 'data', dataSetName, ' = ', 'dataSet ;' ]; 114 | eval(evalString); 115 | 116 | if rowMajor 117 | evalString = [ 'dataSize = size(data', dataSetName, ') ;' ]; 118 | eval(evalString); 119 | dataDim = length(dataSize) ; 120 | if dataDim > 2 || ( dataSize(1) > 1 && dataSize(2) > 1 ) || isnumeric(dataSet) 121 | evalString = [ 'data', dataSetName, ' = permute(data', dataSetName, ',[ ', num2str(dataDim:-1:1), '] );' ]; 122 | eval(evalString); 123 | end 124 | end 125 | 126 | end 127 | end 128 | 129 | end 130 | 131 | % load attribute 132 | if ~isempty(groupHierarchy.Attributes) 133 | nAttributes = length(groupHierarchy.Attributes); 134 | for attrIdx = 1:nAttributes 135 | attrLocation = groupHierarchy.Attributes(attrIdx).Location; 136 | attrName = groupHierarchy.Attributes(attrIdx).Shortname; 137 | % replace '/' to '.' 138 | attrLocation( ismember(attrLocation, '/') ) = '.'; 139 | if length(attrLocation) > 1 140 | attrLocation = [ attrLocation, '.Attributes.', attrName ]; 141 | else 142 | attrLocation = [ attrLocation, 'Attributes.', attrName ]; 143 | end 144 | attrLocation( ismember(attrLocation, ' ') ) = '_'; 145 | 146 | if isnumeric(groupHierarchy.Attributes(attrIdx).Value) 147 | attrData = groupHierarchy.Attributes(attrIdx).Value; 148 | else 149 | dataSize = length( groupHierarchy.Attributes(attrIdx).Value ); 150 | attrData = cell(dataSize, 1); 151 | for dataSizeIdx = 1:dataSize 152 | attrData{dataSizeIdx} = groupHierarchy.Attributes(attrIdx).Value(dataSizeIdx).Data; 153 | end 154 | end 155 | 156 | evalString = [ 'data', attrLocation, ' = ', 'attrData ;' ]; 157 | eval(evalString); 158 | end 159 | 160 | end 161 | 162 | end 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /code/func/readHDF5AsStruct.m~: -------------------------------------------------------------------------------- 1 | function [ data, info ] = readHDF5AsStruct(hdf5Path, rowMajor) 2 | % 3 | % Read an HDF5 file and convert it to a Matlab struct. 4 | % 5 | % ex.) [ data, info ] = readHDF5AsStruct('sampleData.hdf5') 6 | % 7 | % Input: 8 | % hdf5Path - path to the HDF5 file. 9 | % rowMajor - if 1 (default), data is assumed to be saved in row-major 10 | % order ( C/C++ or python style), so dimension of matrix is permutated. 11 | % if 0, dimension of matrix does'nt change. 12 | % 13 | % Output: 14 | % data - < 1x1 struct > data converted from the HDF5 file 15 | % info - < 1x1 struct > Information about the HDF5 file 16 | % ( output of hdf5info(hdf5Path) ). 17 | % 18 | %% 19 | if ~exist( 'hdf5Path', 'var') || isempty(hdf5Path) 20 | help(mfilename) 21 | error('Specify a path to an HDF5 file.') 22 | end 23 | if ~exist('rowMajor', 'var')|| isempty(rowMajor) 24 | rowMajor = 1; 25 | end 26 | 27 | newVersion = 0 ; % 1 if MATLAB version >= ver. 2011a 28 | matlabInfo = ver('MATLAB'); 29 | [ verIdx1, remain ] = strtok( matlabInfo.Version, '.'); 30 | if str2num(verIdx1) >= 8 31 | newVersion = 1; 32 | else 33 | verIdx2 = strtok( remain, '.') ; 34 | if str2num(verIdx1) == 7 && str2num(verIdx2) >= 12 35 | newVersion = 1; 36 | end 37 | end 38 | 39 | 40 | % get info 41 | info = hdf5info(hdf5Path); 42 | groupHierarchy = info.GroupHierarchy ; 43 | 44 | % load data 45 | data = []; 46 | data = readDataSet(data, groupHierarchy, newVersion, rowMajor ); 47 | 48 | end 49 | 50 | 51 | %% readDataSet 52 | % read DataSet and Groups in a hierarchical way 53 | function data = readDataSet(data, groupHierarchy, newVersion, rowMajor) 54 | 55 | if ~isempty(groupHierarchy.Groups) 56 | nGroup = length(groupHierarchy.Groups); 57 | for groupIdx = 1:nGroup 58 | data = readDataSet(data, groupHierarchy.Groups(groupIdx), newVersion, rowMajor); 59 | end 60 | end 61 | 62 | % load dataset 63 | if ~isempty(groupHierarchy.Datasets) 64 | nDatasets = length(groupHierarchy.Datasets); 65 | for dataIdx = 1:nDatasets 66 | dataSetName = groupHierarchy.Datasets(dataIdx).Name; 67 | % replace '.' to '_' if exist 68 | dataSetName( ismember(dataSetName, '.') ) = '_'; 69 | % replace '/' to '.' 70 | dataSetName( ismember(dataSetName, '/') ) = '.'; 71 | 72 | dataSet = hdf5read(groupHierarchy.Datasets(dataIdx)) ; 73 | 74 | % check if the type of the dataSet is a cell or string array or not. 75 | if strcmp( groupHierarchy.Datasets(dataIdx).Datatype.Class, 'H5T_ARRAY') || strcmp(groupHierarchy.Datasets(dataIdx).Datatype.Class, 'H5T_STRING') 76 | 77 | nElements = numel(dataSet); 78 | cellDataTemp = cell(1,nElements); 79 | dataSetTemp = reshape(dataSet, 1, nElements); 80 | for idx = 1:nElements 81 | if rowMajor 82 | if ~isscalar(dataSetTemp(idx).Data) && ~ischar(dataSetTemp(idx).Data) 83 | nDim = sum(size(dataSetTemp(idx).Data) >= 1); 84 | cellDataTemp{idx} = permute(dataSetTemp(idx).Data, [ nDim:-1:1 ] ); 85 | else 86 | cellDataTemp{idx} = dataSetTemp(idx).Data; 87 | end 88 | else 89 | cellDataTemp{idx} = dataSetTemp(idx).Data; 90 | end 91 | end 92 | cellData = reshape(cellDataTemp, size(dataSet)); 93 | 94 | % write 95 | evalString = [ 'data', dataSetName, ' = ', 'cellData ;' ] ; 96 | eval(evalString); 97 | 98 | if numel(cellData) == 1 99 | evalString = [ 'data', dataSetName, ' = ', 'cell2mat(data', dataSetName, ') ;' ] ; 100 | eval(evalString); 101 | 102 | elseif rowMajor 103 | evalString = [ 'dataSize = size(data', dataSetName, ') ;' ]; 104 | eval(evalString); 105 | dataDim = length(dataSize); 106 | if dataDim > 2 || ( dataSize(1) > 1 && dataSize(2) > 1 ); 107 | evalString = [ 'data', dataSetName, ' = permute(data', dataSetName, ',[ ', num2str(dataDim:-1:1), '] );'] ; 108 | eval(evalString); 109 | end 110 | end 111 | 112 | else 113 | evalString = [ 'data', dataSetName, ' = ', 'dataSet ;' ]; 114 | eval(evalString); 115 | 116 | if rowMajor 117 | evalString = [ 'dataSize = size(data', dataSetName, ') ;' ]; 118 | eval(evalString); 119 | dataDim = length(dataSize) ; 120 | if dataDim > 2 || ( dataSize(1) > 1 && dataSize(2) > 1 ) || isnumeric(dataSet) 121 | evalString = [ 'data', dataSetName, ' = permute(data', dataSetName, ',[ ', num2str(dataDim:-1:1), '] );' ]; 122 | eval(evalString); 123 | elseif dataDim == 2 && dataSize(1) == 1 && dataSize(2) > 1 && isnumeric(data) 124 | evalString = [ 'data', dataSetName, ' = permute(data', dataSetName, ',[ ', num2str(dataDim:-1:1), '] );' ]; 125 | eval(evalString); 126 | end 127 | end 128 | 129 | end 130 | end 131 | 132 | end 133 | 134 | % load attribute 135 | if ~isempty(groupHierarchy.Attributes) 136 | nAttributes = length(groupHierarchy.Attributes); 137 | for attrIdx = 1:nAttributes 138 | attrLocation = groupHierarchy.Attributes(attrIdx).Location; 139 | attrName = groupHierarchy.Attributes(attrIdx).Shortname; 140 | % replace '/' to '.' 141 | attrLocation( ismember(attrLocation, '/') ) = '.'; 142 | if length(attrLocation) > 1 143 | attrLocation = [ attrLocation, '.Attributes.', attrName ]; 144 | else 145 | attrLocation = [ attrLocation, 'Attributes.', attrName ]; 146 | end 147 | attrLocation( ismember(attrLocation, ' ') ) = '_'; 148 | 149 | if isnumeric(groupHierarchy.Attributes(attrIdx).Value) 150 | attrData = groupHierarchy.Attributes(attrIdx).Value; 151 | else 152 | dataSize = length( groupHierarchy.Attributes(attrIdx).Value ); 153 | attrData = cell(dataSize, 1); 154 | for dataSizeIdx = 1:dataSize 155 | attrData{dataSizeIdx} = groupHierarchy.Attributes(attrIdx).Value(dataSizeIdx).Data; 156 | end 157 | end 158 | 159 | evalString = [ 'data', attrLocation, ' = ', 'attrData ;' ]; 160 | eval(evalString); 161 | end 162 | 163 | end 164 | 165 | end 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /code/func/writeHDF5FromStruct.m: -------------------------------------------------------------------------------- 1 | function writeHDF5FromStruct(dataPath, mStruct, rowMajor ) 2 | % 3 | % function writeHDF5FromStruct(dataPath, mStruct, compress) 4 | % Input: 5 | % dataPath - a path to the output HDF file. 6 | % If file exists, data is overwritten. 7 | % mStruct - < 1x1 struct > 8 | % 9 | % rowMajor - If 1 (default), change order of storing 10 | % multidimensional arrays. 11 | % This is for compatibility with data format stored using 12 | % row-major based languages such as python, C/C++, ... 13 | % 14 | % 15 | % Note: If this script works on Matlab version >= 7.12 (2011a) and data size > 10MB, 16 | % numerical data will be compressed. 17 | % 18 | % ex.) 19 | % data.data1=1;data.group1.data2={'a','b'}; 20 | % writeHDF5FromStruct( 'sampleHDF5.h5', data ); 21 | % 22 | % datasets '/data1' and '/group1/data2' are created 23 | % and saved to 'sampleHDF5.h5' 24 | % 25 | % 26 | if ~exist('dataPath', 'var')||isempty(dataPath) 27 | error('Specify output HDF file'); 28 | end 29 | if ~exist('mStruct', 'var') 30 | error('Specify an input Matlab struct'); 31 | end 32 | if ~exist('rowMajor', 'var') || isempty(rowMajor) 33 | rowMajor = 1; 34 | end 35 | 36 | % If this script works on Matlab version >= 7.12 (2011a), 37 | % large data is compressed. 38 | compress = 0; 39 | matlabInfo = ver('MATLAB'); 40 | [ verIdx1, remain ] = strtok( matlabInfo.Version, '.'); 41 | if str2num(verIdx1) >= 8 42 | compress = 1; 43 | else 44 | verIdx2 = strtok( remain, '.') ; 45 | if str2num(verIdx1) == 7 && str2num(verIdx2) >= 12 46 | compress = 1; 47 | end 48 | end 49 | 50 | if exist(dataPath, 'file') 51 | delete(dataPath); 52 | end 53 | 54 | groupHierachy = []; 55 | if ~isstruct(mStruct) 56 | evalString = [ inputname(2) ' = mStruct ;' ]; 57 | eval(evalString); 58 | eval([ 'writeHDF5Hierachy(dataPath,' inputname(2) ', groupHierachy, compress, rowMajor) ;' ] ); 59 | else 60 | writeHDF5Hierachy(dataPath, mStruct, groupHierachy, compress, rowMajor); 61 | end 62 | 63 | end 64 | %% 65 | function writeHDF5Hierachy(dataPath, mStruct, groupHierachy, compress, rowMajor) 66 | 67 | if ~isstruct(mStruct) 68 | data = mStruct ; 69 | clear mStruct 70 | mStruct.(inputname(2)) = data; 71 | end 72 | fields = fieldnames(mStruct); 73 | nField = length(fields); 74 | for fieldIdx = 1:nField 75 | data = mStruct.(fields{fieldIdx}); 76 | gh = [ groupHierachy, '/', fields{fieldIdx} ]; 77 | if isstruct(data) 78 | 79 | createGroup(dataPath, gh); 80 | writeHDF5Hierachy(dataPath, data, gh, compress, rowMajor); 81 | else 82 | % check if data has empty element [] 83 | if ~isempty(data) 84 | if rowMajor 85 | dataSize = size(data); 86 | nDim = sum(dataSize >= 1); 87 | if ~ischar(data) 88 | data = permute(data, [ nDim:-1:1 ]); 89 | end 90 | end 91 | % check if data is cell array of cells 92 | if iscell(data) 93 | if ~isscalar(data{1}) && ~ischar(data{1}) 94 | if rowMajor 95 | dataSize = size(data); 96 | nElements = numel(data); 97 | 98 | dataTemp = reshape(data, 1, nElements); 99 | for idx = 1:nElements 100 | nDim = sum(dataSize >= 1) ; 101 | dataTemp{idx} = permute(dataTemp{idx}, [ nDim:-1:1 ]) ; 102 | end 103 | data = reshape(dataTemp, dataSize); 104 | 105 | end 106 | end 107 | end 108 | end 109 | 110 | dataInfo = whos('data'); 111 | % compress if data size > 10000000 bytes (10MB); 112 | if dataInfo.bytes > 10000000 && isnumeric(data) && compress 113 | chunkSize = floor(size(data)/10) + 1 ; 114 | h5create(dataPath, gh, size(data), 'Deflate',9,'ChunkSize',chunkSize); 115 | h5write(dataPath, gh, data); 116 | elseif numel(data) == 1 && isnumeric(data) 117 | writeScaler(dataPath, gh, data) ; 118 | elseif isnumeric(data) 119 | writeNumericData(dataPath, gh, data, rowMajor); 120 | elseif ischar(data) 121 | %if data is 1D string 122 | write1DString(dataPath, gh, data) ; 123 | else 124 | if exist(dataPath, 'file') 125 | hdf5write(dataPath, gh , data, 'WriteMode', 'append'); 126 | else 127 | hdf5write(dataPath, gh , data); 128 | end 129 | end 130 | 131 | end 132 | 133 | end 134 | 135 | end 136 | 137 | %% sub functions 138 | function writeScaler(dataPath, gh, data) 139 | opt1 = 'H5P_DEFAULT'; 140 | 141 | type_id = H5T.copy('H5T_NATIVE_DOUBLE'); 142 | space_id = H5S.create('H5S_SCALAR'); 143 | 144 | if exist(dataPath, 'file') 145 | fid = H5F.open(dataPath, 'H5F_ACC_RDWR', opt1); 146 | else 147 | fcpl = H5P.create('H5P_FILE_CREATE'); 148 | fapl = H5P.create('H5P_FILE_ACCESS'); 149 | fid = H5F.create( dataPath,'H5F_ACC_TRUNC',fcpl,fapl); 150 | end 151 | dset_id = H5D.create(fid, gh, type_id, space_id, opt1); 152 | H5D.write(dset_id,'H5ML_DEFAULT','H5S_ALL','H5S_ALL', opt1, data); 153 | 154 | H5S.close(space_id); 155 | H5T.close(type_id); 156 | H5D.close(dset_id); 157 | H5F.close(fid); 158 | 159 | end 160 | 161 | function writeNumericData(dataPath, gh, data, rowMajor) 162 | opt1 = 'H5P_DEFAULT'; 163 | dataSize = size(data); 164 | if rowMajor 165 | dataSize = fliplr(dataSize); 166 | end 167 | type_id = H5T.copy('H5T_NATIVE_DOUBLE'); 168 | %space_id = H5S.create('H5S_SCALAR'); 169 | space_id = H5S.create_simple( length(dataSize), dataSize, dataSize); 170 | 171 | if exist(dataPath, 'file') 172 | fid = H5F.open(dataPath, 'H5F_ACC_RDWR', opt1); 173 | else 174 | fcpl = H5P.create('H5P_FILE_CREATE'); 175 | fapl = H5P.create('H5P_FILE_ACCESS'); 176 | fid = H5F.create( dataPath,'H5F_ACC_TRUNC',fcpl,fapl); 177 | end 178 | dset_id = H5D.create(fid, gh, type_id, space_id, opt1); 179 | H5D.write(dset_id,'H5ML_DEFAULT','H5S_ALL','H5S_ALL', opt1, data); 180 | 181 | H5S.close(space_id); 182 | H5T.close(type_id); 183 | H5D.close(dset_id); 184 | H5F.close(fid); 185 | 186 | end 187 | 188 | 189 | function write1DString(dataPath, gh, data) 190 | 191 | stringLength = length(data); 192 | opt1 = 'H5P_DEFAULT'; 193 | 194 | type_id = H5T.copy('H5T_C_S1'); 195 | H5T.set_size(type_id, stringLength); 196 | 197 | space_id = H5S.create('H5S_SCALAR'); 198 | 199 | if exist(dataPath, 'file') 200 | fid = H5F.open(dataPath,'H5F_ACC_RDWR', opt1); 201 | else 202 | fcpl = H5P.create('H5P_FILE_CREATE'); 203 | fapl = H5P.create('H5P_FILE_ACCESS'); 204 | fid = H5F.create( dataPath,'H5F_ACC_TRUNC',fcpl,fapl); 205 | end 206 | dset_id = H5D.create(fid, gh, type_id, space_id, opt1); 207 | H5D.write(dset_id,'H5ML_DEFAULT','H5S_ALL','H5S_ALL', opt1, data); 208 | 209 | H5S.close(space_id); 210 | H5T.close(type_id); 211 | H5D.close(dset_id); 212 | H5F.close(fid); 213 | 214 | end 215 | 216 | function createGroup( dataPath, gh ) 217 | opt1 = 'H5P_DEFAULT'; 218 | if exist(dataPath, 'file') 219 | fid = H5F.open(dataPath,'H5F_ACC_RDWR',opt1); 220 | else 221 | fcpl = H5P.create('H5P_FILE_CREATE'); 222 | fapl = H5P.create('H5P_FILE_ACCESS'); 223 | fid = H5F.create( dataPath,'H5F_ACC_TRUNC',fcpl,fapl); 224 | end 225 | 226 | group_id = H5G.create(fid, gh, opt1,opt1,opt1); 227 | H5G.close(group_id); 228 | 229 | end 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm_model_matlab_org.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "svm.h" 4 | 5 | #include "mex.h" 6 | 7 | #if MX_API_VER < 0x07030000 8 | typedef int mwIndex; 9 | #endif 10 | 11 | #define NUM_OF_RETURN_FIELD 10 12 | 13 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 14 | 15 | static const char *field_names[] = { 16 | "Parameters", 17 | "nr_class", 18 | "totalSV", 19 | "rho", 20 | "Label", 21 | "ProbA", 22 | "ProbB", 23 | "nSV", 24 | "sv_coef", 25 | "SVs" 26 | }; 27 | 28 | const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model) 29 | { 30 | int i, j, n; 31 | double *ptr; 32 | mxArray *return_model, **rhs; 33 | int out_id = 0; 34 | 35 | rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); 36 | 37 | // Parameters 38 | rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL); 39 | ptr = mxGetPr(rhs[out_id]); 40 | ptr[0] = model->param.svm_type; 41 | ptr[1] = model->param.kernel_type; 42 | ptr[2] = model->param.degree; 43 | ptr[3] = model->param.gamma; 44 | ptr[4] = model->param.coef0; 45 | out_id++; 46 | 47 | // nr_class 48 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 49 | ptr = mxGetPr(rhs[out_id]); 50 | ptr[0] = model->nr_class; 51 | out_id++; 52 | 53 | // total SV 54 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 55 | ptr = mxGetPr(rhs[out_id]); 56 | ptr[0] = model->l; 57 | out_id++; 58 | 59 | // rho 60 | n = model->nr_class*(model->nr_class-1)/2; 61 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 62 | ptr = mxGetPr(rhs[out_id]); 63 | for(i = 0; i < n; i++) 64 | ptr[i] = model->rho[i]; 65 | out_id++; 66 | 67 | // Label 68 | if(model->label) 69 | { 70 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 71 | ptr = mxGetPr(rhs[out_id]); 72 | for(i = 0; i < model->nr_class; i++) 73 | ptr[i] = model->label[i]; 74 | } 75 | else 76 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 77 | out_id++; 78 | 79 | // probA 80 | if(model->probA != NULL) 81 | { 82 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 83 | ptr = mxGetPr(rhs[out_id]); 84 | for(i = 0; i < n; i++) 85 | ptr[i] = model->probA[i]; 86 | } 87 | else 88 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 89 | out_id ++; 90 | 91 | // probB 92 | if(model->probB != NULL) 93 | { 94 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 95 | ptr = mxGetPr(rhs[out_id]); 96 | for(i = 0; i < n; i++) 97 | ptr[i] = model->probB[i]; 98 | } 99 | else 100 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 101 | out_id++; 102 | 103 | // nSV 104 | if(model->nSV) 105 | { 106 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 107 | ptr = mxGetPr(rhs[out_id]); 108 | for(i = 0; i < model->nr_class; i++) 109 | ptr[i] = model->nSV[i]; 110 | } 111 | else 112 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 113 | out_id++; 114 | 115 | // sv_coef 116 | rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL); 117 | ptr = mxGetPr(rhs[out_id]); 118 | for(i = 0; i < model->nr_class-1; i++) 119 | for(j = 0; j < model->l; j++) 120 | ptr[(i*(model->l))+j] = model->sv_coef[i][j]; 121 | out_id++; 122 | 123 | // SVs 124 | { 125 | int ir_index, nonzero_element; 126 | mwIndex *ir, *jc; 127 | mxArray *pprhs[1], *pplhs[1]; 128 | 129 | if(model->param.kernel_type == PRECOMPUTED) 130 | { 131 | nonzero_element = model->l; 132 | num_of_feature = 1; 133 | } 134 | else 135 | { 136 | nonzero_element = 0; 137 | for(i = 0; i < model->l; i++) { 138 | j = 0; 139 | while(model->SV[i][j].index != -1) 140 | { 141 | nonzero_element++; 142 | j++; 143 | } 144 | } 145 | } 146 | 147 | // SV in column, easier accessing 148 | rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL); 149 | ir = mxGetIr(rhs[out_id]); 150 | jc = mxGetJc(rhs[out_id]); 151 | ptr = mxGetPr(rhs[out_id]); 152 | jc[0] = ir_index = 0; 153 | for(i = 0;i < model->l; i++) 154 | { 155 | if(model->param.kernel_type == PRECOMPUTED) 156 | { 157 | // make a (1 x model->l) matrix 158 | ir[ir_index] = 0; 159 | ptr[ir_index] = model->SV[i][0].value; 160 | ir_index++; 161 | jc[i+1] = jc[i] + 1; 162 | } 163 | else 164 | { 165 | int x_index = 0; 166 | while (model->SV[i][x_index].index != -1) 167 | { 168 | ir[ir_index] = model->SV[i][x_index].index - 1; 169 | ptr[ir_index] = model->SV[i][x_index].value; 170 | ir_index++, x_index++; 171 | } 172 | jc[i+1] = jc[i] + x_index; 173 | } 174 | } 175 | // transpose back to SV in row 176 | pprhs[0] = rhs[out_id]; 177 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 178 | return "cannot transpose SV matrix"; 179 | rhs[out_id] = pplhs[0]; 180 | out_id++; 181 | } 182 | 183 | /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ 184 | return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); 185 | 186 | /* Fill struct matrix with input arguments */ 187 | for(i = 0; i < NUM_OF_RETURN_FIELD; i++) 188 | mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); 189 | /* return */ 190 | plhs[0] = return_model; 191 | mxFree(rhs); 192 | 193 | return NULL; 194 | } 195 | 196 | struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg) 197 | { 198 | int i, j, n, num_of_fields; 199 | double *ptr; 200 | int id = 0; 201 | struct svm_node *x_space; 202 | struct svm_model *model; 203 | mxArray **rhs; 204 | 205 | num_of_fields = mxGetNumberOfFields(matlab_struct); 206 | if(num_of_fields != NUM_OF_RETURN_FIELD) 207 | { 208 | *msg = "number of return field is not correct"; 209 | return NULL; 210 | } 211 | rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); 212 | 213 | for(i=0;irho = NULL; 218 | model->probA = NULL; 219 | model->probB = NULL; 220 | model->label = NULL; 221 | model->nSV = NULL; 222 | model->free_sv = 1; // XXX 223 | 224 | ptr = mxGetPr(rhs[id]); 225 | model->param.svm_type = (int)ptr[0]; 226 | model->param.kernel_type = (int)ptr[1]; 227 | model->param.degree = (int)ptr[2]; 228 | model->param.gamma = ptr[3]; 229 | model->param.coef0 = ptr[4]; 230 | id++; 231 | 232 | ptr = mxGetPr(rhs[id]); 233 | model->nr_class = (int)ptr[0]; 234 | id++; 235 | 236 | ptr = mxGetPr(rhs[id]); 237 | model->l = (int)ptr[0]; 238 | id++; 239 | 240 | // rho 241 | n = model->nr_class * (model->nr_class-1)/2; 242 | model->rho = (double*) malloc(n*sizeof(double)); 243 | ptr = mxGetPr(rhs[id]); 244 | for(i=0;irho[i] = ptr[i]; 246 | id++; 247 | 248 | // label 249 | if(mxIsEmpty(rhs[id]) == 0) 250 | { 251 | model->label = (int*) malloc(model->nr_class*sizeof(int)); 252 | ptr = mxGetPr(rhs[id]); 253 | for(i=0;inr_class;i++) 254 | model->label[i] = (int)ptr[i]; 255 | } 256 | id++; 257 | 258 | // probA 259 | if(mxIsEmpty(rhs[id]) == 0) 260 | { 261 | model->probA = (double*) malloc(n*sizeof(double)); 262 | ptr = mxGetPr(rhs[id]); 263 | for(i=0;iprobA[i] = ptr[i]; 265 | } 266 | id++; 267 | 268 | // probB 269 | if(mxIsEmpty(rhs[id]) == 0) 270 | { 271 | model->probB = (double*) malloc(n*sizeof(double)); 272 | ptr = mxGetPr(rhs[id]); 273 | for(i=0;iprobB[i] = ptr[i]; 275 | } 276 | id++; 277 | 278 | // nSV 279 | if(mxIsEmpty(rhs[id]) == 0) 280 | { 281 | model->nSV = (int*) malloc(model->nr_class*sizeof(int)); 282 | ptr = mxGetPr(rhs[id]); 283 | for(i=0;inr_class;i++) 284 | model->nSV[i] = (int)ptr[i]; 285 | } 286 | id++; 287 | 288 | // sv_coef 289 | ptr = mxGetPr(rhs[id]); 290 | model->sv_coef = (double**) malloc((model->nr_class-1)*sizeof(double)); 291 | for( i=0 ; i< model->nr_class -1 ; i++ ) 292 | model->sv_coef[i] = (double*) malloc((model->l)*sizeof(double)); 293 | for(i = 0; i < model->nr_class - 1; i++) 294 | for(j = 0; j < model->l; j++) 295 | model->sv_coef[i][j] = ptr[i*(model->l)+j]; 296 | id++; 297 | 298 | // SV 299 | { 300 | int sr, sc, elements; 301 | int num_samples; 302 | mwIndex *ir, *jc; 303 | mxArray *pprhs[1], *pplhs[1]; 304 | 305 | // transpose SV 306 | pprhs[0] = rhs[id]; 307 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 308 | { 309 | svm_free_and_destroy_model(&model); 310 | *msg = "cannot transpose SV matrix"; 311 | return NULL; 312 | } 313 | rhs[id] = pplhs[0]; 314 | 315 | sr = (int)mxGetN(rhs[id]); 316 | sc = (int)mxGetM(rhs[id]); 317 | 318 | ptr = mxGetPr(rhs[id]); 319 | ir = mxGetIr(rhs[id]); 320 | jc = mxGetJc(rhs[id]); 321 | 322 | num_samples = (int)mxGetNzmax(rhs[id]); 323 | 324 | elements = num_samples + sr; 325 | 326 | model->SV = (struct svm_node **) malloc(sr * sizeof(struct svm_node *)); 327 | x_space = (struct svm_node *)malloc(elements * sizeof(struct svm_node)); 328 | 329 | // SV is in column 330 | for(i=0;iSV[i] = &x_space[low+i]; 335 | for(j=low;jSV[i][x_index].index = (int)ir[j] + 1; 338 | model->SV[i][x_index].value = ptr[j]; 339 | x_index++; 340 | } 341 | model->SV[i][x_index].index = -1; 342 | } 343 | 344 | id++; 345 | } 346 | mxFree(rhs); 347 | 348 | return model; 349 | } 350 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svm_model_matlab.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "svm.h" 4 | 5 | #include "mex.h" 6 | 7 | #if MX_API_VER < 0x07030000 8 | typedef int mwIndex; 9 | #endif 10 | 11 | #define NUM_OF_RETURN_FIELD 10 12 | 13 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 14 | 15 | static const char *field_names[] = { 16 | "Parameters", 17 | "nr_class", 18 | "totalSV", 19 | "rho", 20 | "Label", 21 | "ProbA", 22 | "ProbB", 23 | "nSV", 24 | "sv_coef", 25 | "SVs" 26 | }; 27 | 28 | const char *model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model) 29 | { 30 | int i, j, n; 31 | double *ptr; 32 | mxArray *return_model, **rhs; 33 | int out_id = 0; 34 | 35 | rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); 36 | 37 | /* Parameters */ 38 | rhs[out_id] = mxCreateDoubleMatrix(5, 1, mxREAL); 39 | ptr = mxGetPr(rhs[out_id]); 40 | ptr[0] = model->param.svm_type; 41 | ptr[1] = model->param.kernel_type; 42 | ptr[2] = model->param.degree; 43 | ptr[3] = model->param.gamma; 44 | ptr[4] = model->param.coef0; 45 | out_id++; 46 | 47 | /* nr_class */ 48 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 49 | ptr = mxGetPr(rhs[out_id]); 50 | ptr[0] = model->nr_class; 51 | out_id++; 52 | 53 | /* total SV */ 54 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 55 | ptr = mxGetPr(rhs[out_id]); 56 | ptr[0] = model->l; 57 | out_id++; 58 | 59 | /* rho */ 60 | n = model->nr_class*(model->nr_class-1)/2; 61 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 62 | ptr = mxGetPr(rhs[out_id]); 63 | for(i = 0; i < n; i++) 64 | ptr[i] = model->rho[i]; 65 | out_id++; 66 | 67 | /* Label */ 68 | if(model->label) 69 | { 70 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 71 | ptr = mxGetPr(rhs[out_id]); 72 | for(i = 0; i < model->nr_class; i++) 73 | ptr[i] = model->label[i]; 74 | } 75 | else 76 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 77 | out_id++; 78 | 79 | /* probA */ 80 | if(model->probA != NULL) 81 | { 82 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 83 | ptr = mxGetPr(rhs[out_id]); 84 | for(i = 0; i < n; i++) 85 | ptr[i] = model->probA[i]; 86 | } 87 | else 88 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 89 | out_id ++; 90 | 91 | /* probB */ 92 | if(model->probB != NULL) 93 | { 94 | rhs[out_id] = mxCreateDoubleMatrix(n, 1, mxREAL); 95 | ptr = mxGetPr(rhs[out_id]); 96 | for(i = 0; i < n; i++) 97 | ptr[i] = model->probB[i]; 98 | } 99 | else 100 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 101 | out_id++; 102 | 103 | /* nSV */ 104 | if(model->nSV) 105 | { 106 | rhs[out_id] = mxCreateDoubleMatrix(model->nr_class, 1, mxREAL); 107 | ptr = mxGetPr(rhs[out_id]); 108 | for(i = 0; i < model->nr_class; i++) 109 | ptr[i] = model->nSV[i]; 110 | } 111 | else 112 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 113 | out_id++; 114 | 115 | /* sv_coef */ 116 | rhs[out_id] = mxCreateDoubleMatrix(model->l, model->nr_class-1, mxREAL); 117 | ptr = mxGetPr(rhs[out_id]); 118 | for(i = 0; i < model->nr_class-1; i++) 119 | for(j = 0; j < model->l; j++) 120 | ptr[(i*(model->l))+j] = model->sv_coef[i][j]; 121 | out_id++; 122 | 123 | /* SVs */ 124 | { 125 | int ir_index, nonzero_element; 126 | mwIndex *ir, *jc; 127 | mxArray *pprhs[1], *pplhs[1]; 128 | 129 | if(model->param.kernel_type == PRECOMPUTED) 130 | { 131 | nonzero_element = model->l; 132 | num_of_feature = 1; 133 | } 134 | else 135 | { 136 | nonzero_element = 0; 137 | for(i = 0; i < model->l; i++) { 138 | j = 0; 139 | while(model->SV[i][j].index != -1) 140 | { 141 | nonzero_element++; 142 | j++; 143 | } 144 | } 145 | } 146 | 147 | /* SV in column, easier accessing */ 148 | rhs[out_id] = mxCreateSparse(num_of_feature, model->l, nonzero_element, mxREAL); 149 | ir = mxGetIr(rhs[out_id]); 150 | jc = mxGetJc(rhs[out_id]); 151 | ptr = mxGetPr(rhs[out_id]); 152 | jc[0] = ir_index = 0; 153 | for(i = 0;i < model->l; i++) 154 | { 155 | if(model->param.kernel_type == PRECOMPUTED) 156 | { 157 | /* make a (1 x model->l) matrix */ 158 | ir[ir_index] = 0; 159 | ptr[ir_index] = model->SV[i][0].value; 160 | ir_index++; 161 | jc[i+1] = jc[i] + 1; 162 | } 163 | else 164 | { 165 | int x_index = 0; 166 | while (model->SV[i][x_index].index != -1) 167 | { 168 | ir[ir_index] = model->SV[i][x_index].index - 1; 169 | ptr[ir_index] = model->SV[i][x_index].value; 170 | ir_index++, x_index++; 171 | } 172 | jc[i+1] = jc[i] + x_index; 173 | } 174 | } 175 | /* transpose back to SV in row */ 176 | pprhs[0] = rhs[out_id]; 177 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 178 | return "cannot transpose SV matrix"; 179 | rhs[out_id] = pplhs[0]; 180 | out_id++; 181 | } 182 | 183 | /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ 184 | return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); 185 | 186 | /* Fill struct matrix with input arguments */ 187 | for(i = 0; i < NUM_OF_RETURN_FIELD; i++) 188 | mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); 189 | /* return */ 190 | plhs[0] = return_model; 191 | mxFree(rhs); 192 | 193 | return NULL; 194 | } 195 | 196 | struct svm_model *matlab_matrix_to_model(const mxArray *matlab_struct, const char **msg) 197 | { 198 | int i, j, n, num_of_fields; 199 | double *ptr; 200 | int id = 0; 201 | struct svm_node *x_space; 202 | struct svm_model *model; 203 | mxArray **rhs; 204 | 205 | num_of_fields = mxGetNumberOfFields(matlab_struct); 206 | if(num_of_fields != NUM_OF_RETURN_FIELD) 207 | { 208 | *msg = "number of return field is not correct"; 209 | return NULL; 210 | } 211 | rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); 212 | 213 | for(i=0;irho = NULL; 218 | model->probA = NULL; 219 | model->probB = NULL; 220 | model->label = NULL; 221 | model->nSV = NULL; 222 | model->free_sv = 1; /* XXX */ 223 | 224 | ptr = mxGetPr(rhs[id]); 225 | model->param.svm_type = (int)ptr[0]; 226 | model->param.kernel_type = (int)ptr[1]; 227 | model->param.degree = (int)ptr[2]; 228 | model->param.gamma = ptr[3]; 229 | model->param.coef0 = ptr[4]; 230 | id++; 231 | 232 | ptr = mxGetPr(rhs[id]); 233 | model->nr_class = (int)ptr[0]; 234 | id++; 235 | 236 | ptr = mxGetPr(rhs[id]); 237 | model->l = (int)ptr[0]; 238 | id++; 239 | 240 | /* rho */ 241 | n = model->nr_class * (model->nr_class-1)/2; 242 | model->rho = (double*) malloc(n*sizeof(double)); 243 | ptr = mxGetPr(rhs[id]); 244 | for(i=0;irho[i] = ptr[i]; 246 | id++; 247 | 248 | /* label */ 249 | if(mxIsEmpty(rhs[id]) == 0) 250 | { 251 | model->label = (int*) malloc(model->nr_class*sizeof(int)); 252 | ptr = mxGetPr(rhs[id]); 253 | for(i=0;inr_class;i++) 254 | model->label[i] = (int)ptr[i]; 255 | } 256 | id++; 257 | 258 | /* probA */ 259 | if(mxIsEmpty(rhs[id]) == 0) 260 | { 261 | model->probA = (double*) malloc(n*sizeof(double)); 262 | ptr = mxGetPr(rhs[id]); 263 | for(i=0;iprobA[i] = ptr[i]; 265 | } 266 | id++; 267 | 268 | /* probB */ 269 | if(mxIsEmpty(rhs[id]) == 0) 270 | { 271 | model->probB = (double*) malloc(n*sizeof(double)); 272 | ptr = mxGetPr(rhs[id]); 273 | for(i=0;iprobB[i] = ptr[i]; 275 | } 276 | id++; 277 | 278 | /* nSV */ 279 | if(mxIsEmpty(rhs[id]) == 0) 280 | { 281 | model->nSV = (int*) malloc(model->nr_class*sizeof(int)); 282 | ptr = mxGetPr(rhs[id]); 283 | for(i=0;inr_class;i++) 284 | model->nSV[i] = (int)ptr[i]; 285 | } 286 | id++; 287 | 288 | /* sv_coef */ 289 | ptr = mxGetPr(rhs[id]); 290 | model->sv_coef = (double**) malloc((model->nr_class-1)*sizeof(double)); 291 | for( i=0 ; i< model->nr_class -1 ; i++ ) 292 | model->sv_coef[i] = (double*) malloc((model->l)*sizeof(double)); 293 | for(i = 0; i < model->nr_class - 1; i++) 294 | for(j = 0; j < model->l; j++) 295 | model->sv_coef[i][j] = ptr[i*(model->l)+j]; 296 | id++; 297 | 298 | /* SV */ 299 | { 300 | int sr, sc, elements; 301 | int num_samples; 302 | mwIndex *ir, *jc; 303 | mxArray *pprhs[1], *pplhs[1]; 304 | 305 | /* transpose SV */ 306 | pprhs[0] = rhs[id]; 307 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 308 | { 309 | svm_free_and_destroy_model(&model); 310 | *msg = "cannot transpose SV matrix"; 311 | return NULL; 312 | } 313 | rhs[id] = pplhs[0]; 314 | 315 | sr = (int)mxGetN(rhs[id]); 316 | sc = (int)mxGetM(rhs[id]); 317 | 318 | ptr = mxGetPr(rhs[id]); 319 | ir = mxGetIr(rhs[id]); 320 | jc = mxGetJc(rhs[id]); 321 | 322 | num_samples = (int)mxGetNzmax(rhs[id]); 323 | 324 | elements = num_samples + sr; 325 | 326 | model->SV = (struct svm_node **) malloc(sr * sizeof(struct svm_node *)); 327 | x_space = (struct svm_node *)malloc(elements * sizeof(struct svm_node)); 328 | 329 | /* SV is in column */ 330 | for(i=0;iSV[i] = &x_space[low+i]; 335 | for(j=low;jSV[i][x_index].index = (int)ir[j] + 1; 338 | model->SV[i][x_index].value = ptr[j]; 339 | x_index++; 340 | } 341 | model->SV[i][x_index].index = -1; 342 | } 343 | 344 | id++; 345 | } 346 | mxFree(rhs); 347 | 348 | return model; 349 | } 350 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/README: -------------------------------------------------------------------------------- 1 | ----------------------------------------- 2 | --- MATLAB/OCTAVE interface of LIBSVM --- 3 | ----------------------------------------- 4 | 5 | Table of Contents 6 | ================= 7 | 8 | - Introduction 9 | - Installation 10 | - Usage 11 | - Returned Model Structure 12 | - Examples 13 | - Other Utilities 14 | - Additional Information 15 | 16 | 17 | Introduction 18 | ============ 19 | 20 | This tool provides a simple interface to LIBSVM, a library for support vector 21 | machines (http://www.csie.ntu.edu.tw/~cjlin/libsvm). It is very easy to use as 22 | the usage and the way of specifying parameters are the same as that of LIBSVM. 23 | 24 | Installation 25 | ============ 26 | 27 | On Unix systems, we recommend using GNU g++ as your 28 | compiler and type 'make' to build 'svmtrain.mexglx' and 'svmpredict.mexglx'. 29 | Note that we assume your MATLAB is installed in '/usr/local/matlab', 30 | if not, please change MATLABDIR in Makefile. 31 | 32 | Example: 33 | linux> make 34 | 35 | To use Octave, type 'make octave': 36 | 37 | Example: 38 | linux> make octave 39 | 40 | On Windows systems, pre-built 'svmtrain.mexw32' and 'svmpredict.mexw32' are 41 | included in this package, so no need to conduct installation. If you 42 | have modified the sources and would like to re-build the package, type 43 | 'mex -setup' in MATLAB to choose a compiler for mex first. Then type 44 | 'make' to start the installation. 45 | 46 | Starting from MATLAB 7.1 (R14SP3), the default MEX file extension is changed 47 | from .dll to .mexw32 or .mexw64 (depends on 32-bit or 64-bit Windows). If your 48 | MATLAB is older than 7.1, you have to build these files yourself. 49 | 50 | Example: 51 | matlab> mex -setup 52 | (ps: MATLAB will show the following messages to setup default compiler.) 53 | Please choose your compiler for building external interface (MEX) files: 54 | Would you like mex to locate installed compilers [y]/n? y 55 | Select a compiler: 56 | [1] Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio 57 | [0] None 58 | Compiler: 1 59 | Please verify your choices: 60 | Compiler: Microsoft Visual C/C++ 7.1 61 | Location: C:\Program Files\Microsoft Visual Studio 62 | Are these correct?([y]/n): y 63 | 64 | matlab> make 65 | 66 | 67 | Under 64-bit Windows, Visual Studio 2005 user will need "X64 Compiler and Tools". 68 | The package won't be installed by default, but you can find it in customized 69 | installation options. 70 | 71 | For list of supported/compatible compilers for MATLAB, please check the 72 | following page: 73 | 74 | http://www.mathworks.com/support/compilers/current_release/ 75 | 76 | Usage 77 | ===== 78 | 79 | matlab> model = svmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']); 80 | 81 | -training_label_vector: 82 | An m by 1 vector of training labels (type must be double). 83 | -training_instance_matrix: 84 | An m by n matrix of m training instances with n features. 85 | It can be dense or sparse (type must be double). 86 | -libsvm_options: 87 | A string of training options in the same format as that of LIBSVM. 88 | 89 | matlab> [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']); 90 | 91 | -testing_label_vector: 92 | An m by 1 vector of prediction labels. If labels of test 93 | data are unknown, simply use any random values. (type must be double) 94 | -testing_instance_matrix: 95 | An m by n matrix of m testing instances with n features. 96 | It can be dense or sparse. (type must be double) 97 | -model: 98 | The output of svmtrain. 99 | -libsvm_options: 100 | A string of testing options in the same format as that of LIBSVM. 101 | 102 | Returned Model Structure 103 | ======================== 104 | 105 | The 'svmtrain' function returns a model which can be used for future 106 | prediction. It is a structure and is organized as [Parameters, nr_class, 107 | totalSV, rho, Label, ProbA, ProbB, nSV, sv_coef, SVs]: 108 | 109 | -Parameters: parameters 110 | -nr_class: number of classes; = 2 for regression/one-class svm 111 | -totalSV: total #SV 112 | -rho: -b of the decision function(s) wx+b 113 | -Label: label of each class; empty for regression/one-class SVM 114 | -ProbA: pairwise probability information; empty if -b 0 or in one-class SVM 115 | -ProbB: pairwise probability information; empty if -b 0 or in one-class SVM 116 | -nSV: number of SVs for each class; empty for regression/one-class SVM 117 | -sv_coef: coefficients for SVs in decision functions 118 | -SVs: support vectors 119 | 120 | If you do not use the option '-b 1', ProbA and ProbB are empty 121 | matrices. If the '-v' option is specified, cross validation is 122 | conducted and the returned model is just a scalar: cross-validation 123 | accuracy for classification and mean-squared error for regression. 124 | 125 | More details about this model can be found in LIBSVM FAQ 126 | (http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html) and LIBSVM 127 | implementation document 128 | (http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf). 129 | 130 | Result of Prediction 131 | ==================== 132 | 133 | The function 'svmpredict' has three outputs. The first one, 134 | predictd_label, is a vector of predicted labels. The second output, 135 | accuracy, is a vector including accuracy (for classification), mean 136 | squared error, and squared correlation coefficient (for regression). 137 | The third is a matrix containing decision values or probability 138 | estimates (if '-b 1' is specified). If k is the number of classes, 139 | for decision values, each row includes results of predicting 140 | k(k-1)/2 binary-class SVMs. For probabilities, each row contains k values 141 | indicating the probability that the testing instance is in each class. 142 | Note that the order of classes here is the same as 'Label' field 143 | in the model structure. 144 | 145 | Examples 146 | ======== 147 | 148 | Train and test on the provided data heart_scale: 149 | 150 | matlab> load heart_scale.mat 151 | matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07'); 152 | matlab> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model); % test the training data 153 | 154 | For probability estimates, you need '-b 1' for training and testing: 155 | 156 | matlab> load heart_scale.mat 157 | matlab> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07 -b 1'); 158 | matlab> load heart_scale.mat 159 | matlab> [predict_label, accuracy, prob_estimates] = svmpredict(heart_scale_label, heart_scale_inst, model, '-b 1'); 160 | 161 | To use precomputed kernel, you must include sample serial number as 162 | the first column of the training and testing data (assume your kernel 163 | matrix is K, # of instances is n): 164 | 165 | matlab> K1 = [(1:n)', K]; % include sample serial number as first column 166 | matlab> model = svmtrain(label_vector, K1, '-t 4'); 167 | matlab> [predict_label, accuracy, dec_values] = svmpredict(label_vector, K1, model); % test the training data 168 | 169 | We give the following detailed example by splitting heart_scale into 170 | 150 training and 120 testing data. Constructing a linear kernel 171 | matrix and then using the precomputed kernel gives exactly the same 172 | testing error as using the LIBSVM built-in linear kernel. 173 | 174 | matlab> load heart_scale.mat 175 | matlab> 176 | matlab> % Split Data 177 | matlab> train_data = heart_scale_inst(1:150,:); 178 | matlab> train_label = heart_scale_label(1:150,:); 179 | matlab> test_data = heart_scale_inst(151:270,:); 180 | matlab> test_label = heart_scale_label(151:270,:); 181 | matlab> 182 | matlab> % Linear Kernel 183 | matlab> model_linear = svmtrain(train_label, train_data, '-t 0'); 184 | matlab> [predict_label_L, accuracy_L, dec_values_L] = svmpredict(test_label, test_data, model_linear); 185 | matlab> 186 | matlab> % Precomputed Kernel 187 | matlab> model_precomputed = svmtrain(train_label, [(1:150)', train_data*train_data'], '-t 4'); 188 | matlab> [predict_label_P, accuracy_P, dec_values_P] = svmpredict(test_label, [(1:120)', test_data*train_data'], model_precomputed); 189 | matlab> 190 | matlab> accuracy_L % Display the accuracy using linear kernel 191 | matlab> accuracy_P % Display the accuracy using precomputed kernel 192 | 193 | Note that for testing, you can put anything in the 194 | testing_label_vector. For more details of precomputed kernels, please 195 | read the section ``Precomputed Kernels'' in the README of the LIBSVM 196 | package. 197 | 198 | Other Utilities 199 | =============== 200 | 201 | A matlab function libsvmread reads files in LIBSVM format: 202 | 203 | [label_vector, instance_matrix] = libsvmread('data.txt'); 204 | 205 | Two outputs are labels and instances, which can then be used as inputs 206 | of svmtrain or svmpredict. 207 | 208 | A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format: 209 | 210 | libsvmwrite('data.txt', label_vector, instance_matrix] 211 | 212 | The instance_matrix must be a sparse matrix. (type must be double) 213 | These codes are prepared by Rong-En Fan and Kai-Wei Chang from National 214 | Taiwan University. 215 | 216 | Additional Information 217 | ====================== 218 | 219 | This interface was initially written by Jun-Cheng Chen, Kuan-Jen Peng, 220 | Chih-Yuan Yang and Chih-Huai Cheng from Department of Computer 221 | Science, National Taiwan University. The current version was prepared 222 | by Rong-En Fan and Ting-Fan Wu. If you find this tool useful, please 223 | cite LIBSVM as follows 224 | 225 | Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for 226 | support vector machines, 2001. Software available at 227 | http://www.csie.ntu.edu.tw/~cjlin/libsvm 228 | 229 | For any question, please contact Chih-Jen Lin , 230 | or check the FAQ page: 231 | 232 | http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#/Q9:_MATLAB_interface 233 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmpredict_org.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "svm.h" 5 | 6 | #include "mex.h" 7 | #include "svm_model_matlab.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | 13 | #define CMD_LEN 2048 14 | 15 | void read_sparse_instance(const mxArray *prhs, int index, struct svm_node *x) 16 | { 17 | int i, j, low, high; 18 | mwIndex *ir, *jc; 19 | double *samples; 20 | 21 | ir = mxGetIr(prhs); 22 | jc = mxGetJc(prhs); 23 | samples = mxGetPr(prhs); 24 | 25 | // each column is one instance 26 | j = 0; 27 | low = (int)jc[index], high = (int)jc[index+1]; 28 | for(i=low;iparam.kernel_type == PRECOMPUTED) 89 | { 90 | // precomputed kernel requires dense matrix, so we make one 91 | mxArray *rhs[1], *lhs[1]; 92 | rhs[0] = mxDuplicateArray(prhs[1]); 93 | if(mexCallMATLAB(1, lhs, 1, rhs, "full")) 94 | { 95 | mexPrintf("Error: cannot full testing instance matrix\n"); 96 | fake_answer(plhs); 97 | return; 98 | } 99 | ptr_instance = mxGetPr(lhs[0]); 100 | mxDestroyArray(rhs[0]); 101 | } 102 | else 103 | { 104 | mxArray *pprhs[1]; 105 | pprhs[0] = mxDuplicateArray(prhs[1]); 106 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 107 | { 108 | mexPrintf("Error: cannot transpose testing instance matrix\n"); 109 | fake_answer(plhs); 110 | return; 111 | } 112 | } 113 | } 114 | 115 | if(predict_probability) 116 | { 117 | if(svm_type==NU_SVR || svm_type==EPSILON_SVR) 118 | mexPrintf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); 119 | else 120 | prob_estimates = (double *) malloc(nr_class*sizeof(double)); 121 | } 122 | 123 | plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 124 | if(predict_probability) 125 | { 126 | // prob estimates are in plhs[2] 127 | if(svm_type==C_SVC || svm_type==NU_SVC) 128 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); 129 | else 130 | plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); 131 | } 132 | else 133 | { 134 | // decision values are in plhs[2] 135 | if(svm_type == ONE_CLASS || 136 | svm_type == EPSILON_SVR || 137 | svm_type == NU_SVR) 138 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 139 | else 140 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class*(nr_class-1)/2, mxREAL); 141 | } 142 | 143 | ptr_predict_label = mxGetPr(plhs[0]); 144 | ptr_prob_estimates = mxGetPr(plhs[2]); 145 | ptr_dec_values = mxGetPr(plhs[2]); 146 | x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) ); 147 | for(instance_index=0;instance_indexparam.kernel_type != PRECOMPUTED) // prhs[1]^T is still sparse 155 | read_sparse_instance(pplhs[0], instance_index, x); 156 | else 157 | { 158 | for(i=0;i 4 || nrhs < 3) 257 | { 258 | exit_with_help(); 259 | fake_answer(plhs); 260 | return; 261 | } 262 | 263 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 264 | mexPrintf("Error: label vector and instance matrix must be double\n"); 265 | fake_answer(plhs); 266 | return; 267 | } 268 | 269 | if(mxIsStruct(prhs[2])) 270 | { 271 | const char *error_msg; 272 | 273 | // parse options 274 | if(nrhs==4) 275 | { 276 | int i, argc = 1; 277 | char cmd[CMD_LEN], *argv[CMD_LEN/2]; 278 | 279 | // put options in argv[] 280 | mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); 281 | if((argv[argc] = strtok(cmd, " ")) != NULL) 282 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 283 | ; 284 | 285 | for(i=1;i=argc) 289 | { 290 | exit_with_help(); 291 | fake_answer(plhs); 292 | return; 293 | } 294 | switch(argv[i-1][1]) 295 | { 296 | case 'b': 297 | prob_estimate_flag = atoi(argv[i]); 298 | break; 299 | default: 300 | mexPrintf("Unknown option: -%c\n", argv[i-1][1]); 301 | exit_with_help(); 302 | fake_answer(plhs); 303 | return; 304 | } 305 | } 306 | } 307 | 308 | model = matlab_matrix_to_model(prhs[2], &error_msg); 309 | if (model == NULL) 310 | { 311 | mexPrintf("Error: can't read model: %s\n", error_msg); 312 | fake_answer(plhs); 313 | return; 314 | } 315 | 316 | if(prob_estimate_flag) 317 | { 318 | if(svm_check_probability_model(model)==0) 319 | { 320 | mexPrintf("Model does not support probabiliy estimates\n"); 321 | fake_answer(plhs); 322 | svm_free_and_destroy_model(&model); 323 | return; 324 | } 325 | } 326 | else 327 | { 328 | if(svm_check_probability_model(model)!=0) 329 | printf("Model supports probability estimates, but disabled in predicton.\n"); 330 | } 331 | 332 | predict(plhs, prhs, model, prob_estimate_flag); 333 | // destroy model 334 | svm_free_and_destroy_model(&model); 335 | } 336 | else 337 | { 338 | mexPrintf("model file should be a struct array\n"); 339 | fake_answer(plhs); 340 | } 341 | 342 | return; 343 | } 344 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmpredict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "svm.h" 5 | 6 | #include "mex.h" 7 | #include "svm_model_matlab.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | 13 | #define CMD_LEN 2048 14 | 15 | void read_sparse_instance(const mxArray *prhs, int index, struct svm_node *x) 16 | { 17 | int i, j, low, high; 18 | mwIndex *ir, *jc; 19 | double *samples; 20 | 21 | ir = mxGetIr(prhs); 22 | jc = mxGetJc(prhs); 23 | samples = mxGetPr(prhs); 24 | 25 | /* each column is one instance */ 26 | j = 0; 27 | low = (int)jc[index], high = (int)jc[index+1]; 28 | for(i=low;iparam.kernel_type == PRECOMPUTED) 89 | { 90 | /* precomputed kernel requires dense matrix, so we make one */ 91 | mxArray *rhs[1], *lhs[1]; 92 | rhs[0] = mxDuplicateArray(prhs[1]); 93 | if(mexCallMATLAB(1, lhs, 1, rhs, "full")) 94 | { 95 | mexPrintf("Error: cannot full testing instance matrix\n"); 96 | fake_answer(plhs); 97 | return; 98 | } 99 | ptr_instance = mxGetPr(lhs[0]); 100 | mxDestroyArray(rhs[0]); 101 | } 102 | else 103 | { 104 | mxArray *pprhs[1]; 105 | pprhs[0] = mxDuplicateArray(prhs[1]); 106 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 107 | { 108 | mexPrintf("Error: cannot transpose testing instance matrix\n"); 109 | fake_answer(plhs); 110 | return; 111 | } 112 | } 113 | } 114 | 115 | if(predict_probability) 116 | { 117 | if(svm_type==NU_SVR || svm_type==EPSILON_SVR) 118 | mexPrintf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model)); 119 | else 120 | prob_estimates = (double *) malloc(nr_class*sizeof(double)); 121 | } 122 | 123 | plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 124 | if(predict_probability) 125 | { 126 | /* prob estimates are in plhs[2] */ 127 | if(svm_type==C_SVC || svm_type==NU_SVC) 128 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); 129 | else 130 | plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); 131 | } 132 | else 133 | { 134 | /* decision values are in plhs[2] */ 135 | if(svm_type == ONE_CLASS || 136 | svm_type == EPSILON_SVR || 137 | svm_type == NU_SVR) 138 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 139 | else 140 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class*(nr_class-1)/2, mxREAL); 141 | } 142 | 143 | ptr_predict_label = mxGetPr(plhs[0]); 144 | ptr_prob_estimates = mxGetPr(plhs[2]); 145 | ptr_dec_values = mxGetPr(plhs[2]); 146 | x = (struct svm_node*)malloc((feature_number+1)*sizeof(struct svm_node) ); 147 | for(instance_index=0;instance_indexparam.kernel_type != PRECOMPUTED) /* prhs[1]^T is still sparse */ 155 | read_sparse_instance(pplhs[0], instance_index, x); 156 | else 157 | { 158 | for(i=0;i 4 || nrhs < 3) 257 | { 258 | exit_with_help(); 259 | fake_answer(plhs); 260 | return; 261 | } 262 | 263 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 264 | mexPrintf("Error: label vector and instance matrix must be double\n"); 265 | fake_answer(plhs); 266 | return; 267 | } 268 | 269 | if(mxIsStruct(prhs[2])) 270 | { 271 | const char *error_msg; 272 | 273 | /* parse options */ 274 | if(nrhs==4) 275 | { 276 | int i, argc = 1; 277 | char cmd[CMD_LEN], *argv[CMD_LEN/2]; 278 | 279 | /* put options in argv[] */ 280 | mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); 281 | if((argv[argc] = strtok(cmd, " ")) != NULL) 282 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 283 | ; 284 | 285 | for(i=1;i=argc) 289 | { 290 | exit_with_help(); 291 | fake_answer(plhs); 292 | return; 293 | } 294 | switch(argv[i-1][1]) 295 | { 296 | case 'b': 297 | prob_estimate_flag = atoi(argv[i]); 298 | break; 299 | default: 300 | mexPrintf("Unknown option: -%c\n", argv[i-1][1]); 301 | exit_with_help(); 302 | fake_answer(plhs); 303 | return; 304 | } 305 | } 306 | } 307 | 308 | model = matlab_matrix_to_model(prhs[2], &error_msg); 309 | if (model == NULL) 310 | { 311 | mexPrintf("Error: can't read model: %s\n", error_msg); 312 | fake_answer(plhs); 313 | return; 314 | } 315 | 316 | if(prob_estimate_flag) 317 | { 318 | if(svm_check_probability_model(model)==0) 319 | { 320 | mexPrintf("Model does not support probabiliy estimates\n"); 321 | fake_answer(plhs); 322 | svm_free_and_destroy_model(&model); 323 | return; 324 | } 325 | } 326 | else 327 | { 328 | if(svm_check_probability_model(model)!=0) 329 | printf("Model supports probability estimates, but disabled in predicton.\n"); 330 | } 331 | 332 | predict(plhs, prhs, model, prob_estimate_flag); 333 | /* destroy model */ 334 | svm_free_and_destroy_model(&model); 335 | } 336 | else 337 | { 338 | mexPrintf("model file should be a struct array\n"); 339 | fake_answer(plhs); 340 | } 341 | 342 | return; 343 | } 344 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmtrain_org.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "svm.h" 6 | 7 | #include "mex.h" 8 | #include "svm_model_matlab.h" 9 | 10 | #if MX_API_VER < 0x07030000 11 | typedef int mwIndex; 12 | #endif 13 | 14 | #define CMD_LEN 2048 15 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 16 | 17 | void print_null(const char *s) {} 18 | 19 | void exit_with_help() 20 | { 21 | mexPrintf( 22 | "Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');\n" 23 | "libsvm_options:\n" 24 | "-s svm_type : set type of SVM (default 0)\n" 25 | " 0 -- C-SVC\n" 26 | " 1 -- nu-SVC\n" 27 | " 2 -- one-class SVM\n" 28 | " 3 -- epsilon-SVR\n" 29 | " 4 -- nu-SVR\n" 30 | "-t kernel_type : set type of kernel function (default 2)\n" 31 | " 0 -- linear: u'*v\n" 32 | " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 33 | " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 34 | " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 35 | " 4 -- precomputed kernel (kernel values in training_instance_matrix)\n" 36 | "-d degree : set degree in kernel function (default 3)\n" 37 | "-g gamma : set gamma in kernel function (default 1/num_features)\n" 38 | "-r coef0 : set coef0 in kernel function (default 0)\n" 39 | "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 40 | "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 41 | "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 42 | "-m cachesize : set cache memory size in MB (default 100)\n" 43 | "-e epsilon : set tolerance of termination criterion (default 0.001)\n" 44 | "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 45 | "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 46 | "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 47 | "-v n : n-fold cross validation mode\n" 48 | "-q : quiet mode (no outputs)\n" 49 | ); 50 | } 51 | 52 | // svm arguments 53 | struct svm_parameter param; // set by parse_command_line 54 | struct svm_problem prob; // set by read_problem 55 | struct svm_model *model; 56 | struct svm_node *x_space; 57 | int cross_validation; 58 | int nr_fold; 59 | 60 | 61 | double do_cross_validation() 62 | { 63 | int i; 64 | int total_correct = 0; 65 | double total_error = 0; 66 | double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 67 | double *target = Malloc(double,prob.l); 68 | double retval = 0.0; 69 | 70 | svm_cross_validation(&prob,¶m,nr_fold,target); 71 | if(param.svm_type == EPSILON_SVR || 72 | param.svm_type == NU_SVR) 73 | { 74 | for(i=0;i 2) 134 | { 135 | // put options in argv[] 136 | mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1); 137 | if((argv[argc] = strtok(cmd, " ")) != NULL) 138 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 139 | ; 140 | } 141 | 142 | // parse options 143 | for(i=1;i=argc && argv[i-1][1] != 'q') // since option -q has no parameter 148 | return 1; 149 | switch(argv[i-1][1]) 150 | { 151 | case 's': 152 | param.svm_type = atoi(argv[i]); 153 | break; 154 | case 't': 155 | param.kernel_type = atoi(argv[i]); 156 | break; 157 | case 'd': 158 | param.degree = atoi(argv[i]); 159 | break; 160 | case 'g': 161 | param.gamma = atof(argv[i]); 162 | break; 163 | case 'r': 164 | param.coef0 = atof(argv[i]); 165 | break; 166 | case 'n': 167 | param.nu = atof(argv[i]); 168 | break; 169 | case 'm': 170 | param.cache_size = atof(argv[i]); 171 | break; 172 | case 'c': 173 | param.C = atof(argv[i]); 174 | break; 175 | case 'e': 176 | param.eps = atof(argv[i]); 177 | break; 178 | case 'p': 179 | param.p = atof(argv[i]); 180 | break; 181 | case 'h': 182 | param.shrinking = atoi(argv[i]); 183 | break; 184 | case 'b': 185 | param.probability = atoi(argv[i]); 186 | break; 187 | case 'q': 188 | print_func = &print_null; 189 | i--; 190 | break; 191 | case 'v': 192 | cross_validation = 1; 193 | nr_fold = atoi(argv[i]); 194 | if(nr_fold < 2) 195 | { 196 | mexPrintf("n-fold cross validation: n must >= 2\n"); 197 | return 1; 198 | } 199 | break; 200 | case 'w': 201 | ++param.nr_weight; 202 | param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight); 203 | param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight); 204 | param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); 205 | param.weight[param.nr_weight-1] = atof(argv[i]); 206 | break; 207 | default: 208 | mexPrintf("Unknown option -%c\n", argv[i-1][1]); 209 | return 1; 210 | } 211 | } 212 | 213 | svm_set_print_string_function(print_func); 214 | 215 | return 0; 216 | } 217 | 218 | // read in a problem (in svmlight format) 219 | int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat) 220 | { 221 | int i, j, k; 222 | int elements, max_index, sc, label_vector_row_num; 223 | double *samples, *labels; 224 | 225 | prob.x = NULL; 226 | prob.y = NULL; 227 | x_space = NULL; 228 | 229 | labels = mxGetPr(label_vec); 230 | samples = mxGetPr(instance_mat); 231 | sc = (int)mxGetN(instance_mat); 232 | 233 | elements = 0; 234 | // the number of instance 235 | prob.l = (int)mxGetM(instance_mat); 236 | label_vector_row_num = (int)mxGetM(label_vec); 237 | 238 | if(label_vector_row_num!=prob.l) 239 | { 240 | mexPrintf("Length of label vector does not match # of instances.\n"); 241 | return -1; 242 | } 243 | 244 | if(param.kernel_type == PRECOMPUTED) 245 | elements = prob.l * (sc + 1); 246 | else 247 | { 248 | for(i = 0; i < prob.l; i++) 249 | { 250 | for(k = 0; k < sc; k++) 251 | if(samples[k * prob.l + i] != 0) 252 | elements++; 253 | // count the '-1' element 254 | elements++; 255 | } 256 | } 257 | 258 | prob.y = Malloc(double,prob.l); 259 | prob.x = Malloc(struct svm_node *,prob.l); 260 | x_space = Malloc(struct svm_node, elements); 261 | 262 | max_index = sc; 263 | j = 0; 264 | for(i = 0; i < prob.l; i++) 265 | { 266 | prob.x[i] = &x_space[j]; 267 | prob.y[i] = labels[i]; 268 | 269 | for(k = 0; k < sc; k++) 270 | { 271 | if(param.kernel_type == PRECOMPUTED || samples[k * prob.l + i] != 0) 272 | { 273 | x_space[j].index = k + 1; 274 | x_space[j].value = samples[k * prob.l + i]; 275 | j++; 276 | } 277 | } 278 | x_space[j++].index = -1; 279 | } 280 | 281 | if(param.gamma == 0 && max_index > 0) 282 | param.gamma = 1.0/max_index; 283 | 284 | if(param.kernel_type == PRECOMPUTED) 285 | for(i=0;i max_index) 288 | { 289 | mexPrintf("Wrong input format: sample_serial_number out of range\n"); 290 | return -1; 291 | } 292 | } 293 | 294 | return 0; 295 | } 296 | 297 | int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat) 298 | { 299 | int i, j, k, low, high; 300 | mwIndex *ir, *jc; 301 | int elements, max_index, num_samples, label_vector_row_num; 302 | double *samples, *labels; 303 | mxArray *instance_mat_col; // transposed instance sparse matrix 304 | 305 | prob.x = NULL; 306 | prob.y = NULL; 307 | x_space = NULL; 308 | 309 | // transpose instance matrix 310 | { 311 | mxArray *prhs[1], *plhs[1]; 312 | prhs[0] = mxDuplicateArray(instance_mat); 313 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 314 | { 315 | mexPrintf("Error: cannot transpose training instance matrix\n"); 316 | return -1; 317 | } 318 | instance_mat_col = plhs[0]; 319 | mxDestroyArray(prhs[0]); 320 | } 321 | 322 | // each column is one instance 323 | labels = mxGetPr(label_vec); 324 | samples = mxGetPr(instance_mat_col); 325 | ir = mxGetIr(instance_mat_col); 326 | jc = mxGetJc(instance_mat_col); 327 | 328 | num_samples = (int)mxGetNzmax(instance_mat_col); 329 | 330 | // the number of instance 331 | prob.l = (int)mxGetN(instance_mat_col); 332 | label_vector_row_num = (int)mxGetM(label_vec); 333 | 334 | if(label_vector_row_num!=prob.l) 335 | { 336 | mexPrintf("Length of label vector does not match # of instances.\n"); 337 | return -1; 338 | } 339 | 340 | elements = num_samples + prob.l; 341 | max_index = (int)mxGetM(instance_mat_col); 342 | 343 | prob.y = Malloc(double,prob.l); 344 | prob.x = Malloc(struct svm_node *,prob.l); 345 | x_space = Malloc(struct svm_node, elements); 346 | 347 | j = 0; 348 | for(i=0;i 0) 363 | param.gamma = 1.0/max_index; 364 | 365 | return 0; 366 | } 367 | 368 | static void fake_answer(mxArray *plhs[]) 369 | { 370 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 371 | } 372 | 373 | // Interface function of matlab 374 | // now assume prhs[0]: label prhs[1]: features 375 | void mexFunction( int nlhs, mxArray *plhs[], 376 | int nrhs, const mxArray *prhs[] ) 377 | { 378 | const char *error_msg; 379 | 380 | // fix random seed to have same results for each run 381 | // (for cross validation and probability estimation) 382 | srand(1); 383 | 384 | // Transform the input Matrix to libsvm format 385 | if(nrhs > 1 && nrhs < 4) 386 | { 387 | int err; 388 | 389 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 390 | mexPrintf("Error: label vector and instance matrix must be double\n"); 391 | fake_answer(plhs); 392 | return; 393 | } 394 | 395 | if(parse_command_line(nrhs, prhs, NULL)) 396 | { 397 | exit_with_help(); 398 | svm_destroy_param(¶m); 399 | fake_answer(plhs); 400 | return; 401 | } 402 | 403 | if(mxIsSparse(prhs[1])) 404 | { 405 | if(param.kernel_type == PRECOMPUTED) 406 | { 407 | // precomputed kernel requires dense matrix, so we make one 408 | mxArray *rhs[1], *lhs[1]; 409 | 410 | rhs[0] = mxDuplicateArray(prhs[1]); 411 | if(mexCallMATLAB(1, lhs, 1, rhs, "full")) 412 | { 413 | mexPrintf("Error: cannot generate a full training instance matrix\n"); 414 | svm_destroy_param(¶m); 415 | fake_answer(plhs); 416 | return; 417 | } 418 | err = read_problem_dense(prhs[0], lhs[0]); 419 | mxDestroyArray(lhs[0]); 420 | mxDestroyArray(rhs[0]); 421 | } 422 | else 423 | err = read_problem_sparse(prhs[0], prhs[1]); 424 | } 425 | else 426 | err = read_problem_dense(prhs[0], prhs[1]); 427 | 428 | // svmtrain's original code 429 | error_msg = svm_check_parameter(&prob, ¶m); 430 | 431 | if(err || error_msg) 432 | { 433 | if (error_msg != NULL) 434 | mexPrintf("Error: %s\n", error_msg); 435 | svm_destroy_param(¶m); 436 | free(prob.y); 437 | free(prob.x); 438 | free(x_space); 439 | fake_answer(plhs); 440 | return; 441 | } 442 | 443 | if(cross_validation) 444 | { 445 | double *ptr; 446 | plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); 447 | ptr = mxGetPr(plhs[0]); 448 | ptr[0] = do_cross_validation(); 449 | } 450 | else 451 | { 452 | int nr_feat = (int)mxGetN(prhs[1]); 453 | const char *error_msg; 454 | model = svm_train(&prob, ¶m); 455 | error_msg = model_to_matlab_structure(plhs, nr_feat, model); 456 | if(error_msg) 457 | mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); 458 | svm_free_and_destroy_model(&model); 459 | } 460 | svm_destroy_param(¶m); 461 | free(prob.y); 462 | free(prob.x); 463 | free(x_space); 464 | } 465 | else 466 | { 467 | exit_with_help(); 468 | fake_answer(plhs); 469 | return; 470 | } 471 | } 472 | -------------------------------------------------------------------------------- /code/libsvm-mat-3.0.1/svmtrain.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "svm.h" 6 | 7 | #include "mex.h" 8 | #include "svm_model_matlab.h" 9 | 10 | #if MX_API_VER < 0x07030000 11 | typedef int mwIndex; 12 | #endif 13 | 14 | #define CMD_LEN 2048 15 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 16 | 17 | void print_null(const char *s) {} 18 | 19 | void exit_with_help() 20 | { 21 | mexPrintf( 22 | "Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');\n" 23 | "libsvm_options:\n" 24 | "-s svm_type : set type of SVM (default 0)\n" 25 | " 0 -- C-SVC\n" 26 | " 1 -- nu-SVC\n" 27 | " 2 -- one-class SVM\n" 28 | " 3 -- epsilon-SVR\n" 29 | " 4 -- nu-SVR\n" 30 | "-t kernel_type : set type of kernel function (default 2)\n" 31 | " 0 -- linear: u'*v\n" 32 | " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 33 | " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 34 | " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 35 | " 4 -- precomputed kernel (kernel values in training_instance_matrix)\n" 36 | "-d degree : set degree in kernel function (default 3)\n" 37 | "-g gamma : set gamma in kernel function (default 1/num_features)\n" 38 | "-r coef0 : set coef0 in kernel function (default 0)\n" 39 | "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 40 | "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 41 | "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 42 | "-m cachesize : set cache memory size in MB (default 100)\n" 43 | "-e epsilon : set tolerance of termination criterion (default 0.001)\n" 44 | "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 45 | "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 46 | "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 47 | "-v n : n-fold cross validation mode\n" 48 | "-q : quiet mode (no outputs)\n" 49 | ); 50 | } 51 | 52 | /* svm arguments */ 53 | struct svm_parameter param; /* set by parse_command_line */ 54 | struct svm_problem prob; /* set by read_problem */ 55 | struct svm_model *model; 56 | struct svm_node *x_space; 57 | int cross_validation; 58 | int nr_fold; 59 | 60 | 61 | double do_cross_validation() 62 | { 63 | int i; 64 | int total_correct = 0; 65 | double total_error = 0; 66 | double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; 67 | double *target = Malloc(double,prob.l); 68 | double retval = 0.0; 69 | 70 | svm_cross_validation(&prob,¶m,nr_fold,target); 71 | if(param.svm_type == EPSILON_SVR || 72 | param.svm_type == NU_SVR) 73 | { 74 | for(i=0;i 2) 134 | { 135 | /* put options in argv[] */ 136 | mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1); 137 | if((argv[argc] = strtok(cmd, " ")) != NULL) 138 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 139 | ; 140 | } 141 | 142 | /* parse options */ 143 | for(i=1;i=argc && argv[i-1][1] != 'q') /* since option -q has no parameter */ 148 | return 1; 149 | switch(argv[i-1][1]) 150 | { 151 | case 's': 152 | param.svm_type = atoi(argv[i]); 153 | break; 154 | case 't': 155 | param.kernel_type = atoi(argv[i]); 156 | break; 157 | case 'd': 158 | param.degree = atoi(argv[i]); 159 | break; 160 | case 'g': 161 | param.gamma = atof(argv[i]); 162 | break; 163 | case 'r': 164 | param.coef0 = atof(argv[i]); 165 | break; 166 | case 'n': 167 | param.nu = atof(argv[i]); 168 | break; 169 | case 'm': 170 | param.cache_size = atof(argv[i]); 171 | break; 172 | case 'c': 173 | param.C = atof(argv[i]); 174 | break; 175 | case 'e': 176 | param.eps = atof(argv[i]); 177 | break; 178 | case 'p': 179 | param.p = atof(argv[i]); 180 | break; 181 | case 'h': 182 | param.shrinking = atoi(argv[i]); 183 | break; 184 | case 'b': 185 | param.probability = atoi(argv[i]); 186 | break; 187 | case 'q': 188 | print_func = &print_null; 189 | i--; 190 | break; 191 | case 'v': 192 | cross_validation = 1; 193 | nr_fold = atoi(argv[i]); 194 | if(nr_fold < 2) 195 | { 196 | mexPrintf("n-fold cross validation: n must >= 2\n"); 197 | return 1; 198 | } 199 | break; 200 | case 'w': 201 | ++param.nr_weight; 202 | param.weight_label = (int *)realloc(param.weight_label,sizeof(int)*param.nr_weight); 203 | param.weight = (double *)realloc(param.weight,sizeof(double)*param.nr_weight); 204 | param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); 205 | param.weight[param.nr_weight-1] = atof(argv[i]); 206 | break; 207 | default: 208 | mexPrintf("Unknown option -%c\n", argv[i-1][1]); 209 | return 1; 210 | } 211 | } 212 | 213 | svm_set_print_string_function(print_func); 214 | 215 | return 0; 216 | } 217 | 218 | /* read in a problem (in svmlight format) */ 219 | int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat) 220 | { 221 | int i, j, k; 222 | int elements, max_index, sc, label_vector_row_num; 223 | double *samples, *labels; 224 | 225 | prob.x = NULL; 226 | prob.y = NULL; 227 | x_space = NULL; 228 | 229 | labels = mxGetPr(label_vec); 230 | samples = mxGetPr(instance_mat); 231 | sc = (int)mxGetN(instance_mat); 232 | 233 | elements = 0; 234 | /* the number of instance */ 235 | prob.l = (int)mxGetM(instance_mat); 236 | label_vector_row_num = (int)mxGetM(label_vec); 237 | 238 | if(label_vector_row_num!=prob.l) 239 | { 240 | mexPrintf("Length of label vector does not match # of instances.\n"); 241 | return -1; 242 | } 243 | 244 | if(param.kernel_type == PRECOMPUTED) 245 | elements = prob.l * (sc + 1); 246 | else 247 | { 248 | for(i = 0; i < prob.l; i++) 249 | { 250 | for(k = 0; k < sc; k++) 251 | if(samples[k * prob.l + i] != 0) 252 | elements++; 253 | /* count the '-1' element */ 254 | elements++; 255 | } 256 | } 257 | 258 | prob.y = Malloc(double,prob.l); 259 | prob.x = Malloc(struct svm_node *,prob.l); 260 | x_space = Malloc(struct svm_node, elements); 261 | 262 | max_index = sc; 263 | j = 0; 264 | for(i = 0; i < prob.l; i++) 265 | { 266 | prob.x[i] = &x_space[j]; 267 | prob.y[i] = labels[i]; 268 | 269 | for(k = 0; k < sc; k++) 270 | { 271 | if(param.kernel_type == PRECOMPUTED || samples[k * prob.l + i] != 0) 272 | { 273 | x_space[j].index = k + 1; 274 | x_space[j].value = samples[k * prob.l + i]; 275 | j++; 276 | } 277 | } 278 | x_space[j++].index = -1; 279 | } 280 | 281 | if(param.gamma == 0 && max_index > 0) 282 | param.gamma = 1.0/max_index; 283 | 284 | if(param.kernel_type == PRECOMPUTED) 285 | for(i=0;i max_index) 288 | { 289 | mexPrintf("Wrong input format: sample_serial_number out of range\n"); 290 | return -1; 291 | } 292 | } 293 | 294 | return 0; 295 | } 296 | 297 | int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat) 298 | { 299 | int i, j, k, low, high; 300 | mwIndex *ir, *jc; 301 | int elements, max_index, num_samples, label_vector_row_num; 302 | double *samples, *labels; 303 | mxArray *instance_mat_col; /* transposed instance sparse matrix */ 304 | 305 | prob.x = NULL; 306 | prob.y = NULL; 307 | x_space = NULL; 308 | 309 | /* transpose instance matrix */ 310 | { 311 | mxArray *prhs[1], *plhs[1]; 312 | prhs[0] = mxDuplicateArray(instance_mat); 313 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 314 | { 315 | mexPrintf("Error: cannot transpose training instance matrix\n"); 316 | return -1; 317 | } 318 | instance_mat_col = plhs[0]; 319 | mxDestroyArray(prhs[0]); 320 | } 321 | 322 | /* each column is one instance */ 323 | labels = mxGetPr(label_vec); 324 | samples = mxGetPr(instance_mat_col); 325 | ir = mxGetIr(instance_mat_col); 326 | jc = mxGetJc(instance_mat_col); 327 | 328 | num_samples = (int)mxGetNzmax(instance_mat_col); 329 | 330 | /* the number of instance */ 331 | prob.l = (int)mxGetN(instance_mat_col); 332 | label_vector_row_num = (int)mxGetM(label_vec); 333 | 334 | if(label_vector_row_num!=prob.l) 335 | { 336 | mexPrintf("Length of label vector does not match # of instances.\n"); 337 | return -1; 338 | } 339 | 340 | elements = num_samples + prob.l; 341 | max_index = (int)mxGetM(instance_mat_col); 342 | 343 | prob.y = Malloc(double,prob.l); 344 | prob.x = Malloc(struct svm_node *,prob.l); 345 | x_space = Malloc(struct svm_node, elements); 346 | 347 | j = 0; 348 | for(i=0;i 0) 363 | param.gamma = 1.0/max_index; 364 | 365 | return 0; 366 | } 367 | 368 | static void fake_answer(mxArray *plhs[]) 369 | { 370 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 371 | } 372 | 373 | /* Interface function of matlab */ 374 | /* now assume prhs[0]: label prhs[1]: features */ 375 | void mexFunction( int nlhs, mxArray *plhs[], 376 | int nrhs, const mxArray *prhs[] ) 377 | { 378 | const char *error_msg; 379 | 380 | /* fix random seed to have same results for each run */ 381 | /* (for cross validation and probability estimation) */ 382 | srand(1); 383 | 384 | /* Transform the input Matrix to libsvm format */ 385 | if(nrhs > 1 && nrhs < 4) 386 | { 387 | int err; 388 | 389 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 390 | mexPrintf("Error: label vector and instance matrix must be double\n"); 391 | fake_answer(plhs); 392 | return; 393 | } 394 | 395 | if(parse_command_line(nrhs, prhs, NULL)) 396 | { 397 | exit_with_help(); 398 | svm_destroy_param(¶m); 399 | fake_answer(plhs); 400 | return; 401 | } 402 | 403 | if(mxIsSparse(prhs[1])) 404 | { 405 | if(param.kernel_type == PRECOMPUTED) 406 | { 407 | /* precomputed kernel requires dense matrix, so we make one */ 408 | mxArray *rhs[1], *lhs[1]; 409 | 410 | rhs[0] = mxDuplicateArray(prhs[1]); 411 | if(mexCallMATLAB(1, lhs, 1, rhs, "full")) 412 | { 413 | mexPrintf("Error: cannot generate a full training instance matrix\n"); 414 | svm_destroy_param(¶m); 415 | fake_answer(plhs); 416 | return; 417 | } 418 | err = read_problem_dense(prhs[0], lhs[0]); 419 | mxDestroyArray(lhs[0]); 420 | mxDestroyArray(rhs[0]); 421 | } 422 | else 423 | err = read_problem_sparse(prhs[0], prhs[1]); 424 | } 425 | else 426 | err = read_problem_dense(prhs[0], prhs[1]); 427 | 428 | /* svmtrain's original code */ 429 | error_msg = svm_check_parameter(&prob, ¶m); 430 | 431 | if(err || error_msg) 432 | { 433 | if (error_msg != NULL) 434 | mexPrintf("Error: %s\n", error_msg); 435 | svm_destroy_param(¶m); 436 | free(prob.y); 437 | free(prob.x); 438 | free(x_space); 439 | fake_answer(plhs); 440 | return; 441 | } 442 | 443 | if(cross_validation) 444 | { 445 | double *ptr; 446 | plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); 447 | ptr = mxGetPr(plhs[0]); 448 | ptr[0] = do_cross_validation(); 449 | } 450 | else 451 | { 452 | int nr_feat = (int)mxGetN(prhs[1]); 453 | const char *error_msg; 454 | model = svm_train(&prob, ¶m); 455 | error_msg = model_to_matlab_structure(plhs, nr_feat, model); 456 | if(error_msg) 457 | mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); 458 | svm_free_and_destroy_model(&model); 459 | } 460 | svm_destroy_param(¶m); 461 | free(prob.y); 462 | free(prob.x); 463 | free(x_space); 464 | } 465 | else 466 | { 467 | exit_with_help(); 468 | fake_answer(plhs); 469 | return; 470 | } 471 | } 472 | -------------------------------------------------------------------------------- /code/func/preprocfMRIData.m: -------------------------------------------------------------------------------- 1 | function [pDs,pDp,props_new]=preprocfMRIData_test(Ds,Dp,props) 2 | % 3 | % preprocess fMRI data to create features for decoding analysis 4 | % 5 | 6 | %% voxel selection within pre-defined ROIs 7 | fprintf('Voxel selection...\n') 8 | useVox=any(props.roiMask,1); 9 | props.roiMask=props.roiMask(:,useVox); 10 | Ds.data=Ds.data(:,useVox); 11 | Dp.data=Dp.data(:,useVox); 12 | Ds.xyz=Ds.xyz(:,useVox); 13 | Dp.xyz=Dp.xyz(:,useVox); 14 | props.xyz=Ds.xyz; 15 | 16 | %% shift data for hemodynamic delay 17 | fprintf('Shift data...\n') 18 | params.nshift= 1;% [volume] default = 1 = 3 sec 19 | 20 | % sleep data is not shifted 21 | % perception data 22 | [Dp,params]=shiftData(Dp,params); 23 | 24 | %% reduce outliers 25 | fprintf('Reduce outliers...\n') 26 | params.std_thres=3; 27 | params.num_its = 10; 28 | params.remove = 1; 29 | params.method = 2; 30 | params.max_val = inf; 31 | params.min_val = 100; 32 | 33 | % sleep data 34 | Ds=reduceOutliers(Ds,params); 35 | % perception data 36 | Dp=reduceOutliers(Dp,params); 37 | 38 | %% feature matching 39 | % this is necessary because reduceOutliers may change the # of features 40 | [nouse,idx_s,idx_p]=intersect(Ds.xyz',Dp.xyz','rows'); 41 | 42 | % sleep data 43 | Ds.data=Ds.data(:,idx_s); 44 | Ds.xyz=Ds.xyz(:,idx_s); 45 | % perception data 46 | Dp.data=Dp.data(:,idx_p); 47 | Dp.xyz=Dp.xyz(:,idx_p); 48 | 49 | % roi mask data 50 | [nouse,idx_r]=intersect(props.xyz',Ds.xyz','rows'); 51 | props.xyz=props.xyz(:,idx_r); 52 | props.roiMask=props.roiMask(:,idx_r); 53 | 54 | %% linear detrending 55 | fprintf('Detrending...\n') 56 | params.sub_mean = 0; 57 | params.method = 'linear'; 58 | 59 | % sleep data 60 | Ds=detrenddata(Ds,params); 61 | % perception data 62 | Dp=detrenddata(Dp,params); 63 | 64 | %% feature normalization 65 | fprintf('Normalize data...\n') 66 | % sleep data 67 | params.analysisPeriod=40; % [volume] default = 40 = 2 min before awakening for each period 68 | params.normalizeBaselineDuration=10; %[volume] default = 10 = 30 sec duration for normalization 69 | params.normalizationOnsetfromAwake=20; % [vlume] defualt = 20 = 1 min before awakening 70 | params.zero_thres = 1; 71 | 72 | [Ds,params]=normalizeSleepData(Ds,params); 73 | 74 | % perception data 75 | params.base_labels = 'all'; 76 | params.norm_mode = 0; 77 | params.zero_thres = 1; 78 | 79 | Dp=normalizeData(Dp,params); 80 | 81 | %% average to create samples 82 | fprintf('Average data...\n') 83 | params.twShift=1; 84 | params.winDur=3; % volume default = 3 = 9s before awakening 85 | 86 | % sleep data 87 | Ds=averageSleepData(Ds,params); 88 | 89 | % perception data 90 | Dp=averagePerceptionData(Dp); 91 | 92 | %% select calculate pairs 93 | nSampleThreshold=10; 94 | synsetLabel=find(~cellfun(@isempty,strfind(Ds.labels_type,'Synset_'))); 95 | sum(Ds.labels(:,synsetLabel)); 96 | nSynset=length(synsetLabel); 97 | nminSamp=zeros(nSynset); 98 | for i=1:(nSynset-1) 99 | for j=(i+1):nSynset 100 | lab1=Ds.labels(:,synsetLabel(i))==1; 101 | lab2=Ds.labels(:,synsetLabel(j))==1; 102 | overlap=(lab1+lab2)==2; 103 | lab1(overlap)=0; 104 | lab2(overlap)=0; 105 | nminSamp(j,i)=min(sum([lab1,lab2])); 106 | end 107 | end 108 | [tri,i,j]=getTri(nminSamp); 109 | usePairsIdx=(tri>=nSampleThreshold); 110 | 111 | % rename synset names 112 | synsetNames=Ds.labels_type(synsetLabel); 113 | for ix=1:nSynset 114 | synsetNames{ix}=strrep(synsetNames{ix},'[','_'); 115 | synsetNames{ix}=strrep(synsetNames{ix},':','_'); 116 | synsetNames{ix}=synsetNames{ix}(1:end-1); 117 | end 118 | 119 | props.synsetPairs=[synsetNames(i(usePairsIdx));synsetNames(j(usePairsIdx))]'; 120 | props.synsetNames=synsetNames; 121 | 122 | %% exclude awake samples from sleep data 123 | awakeTrials=Ds.labels(:,ismember(Ds.labels_type,'EEG sleep score'))==0; 124 | Ds.data(awakeTrials,:)=[]; 125 | Ds.labels(awakeTrials,:)=[]; 126 | 127 | %% create data matrix 128 | % sleep data 129 | synsetLabelIdx=find(~cellfun(@isempty,strfind(Ds.labels_type,'Synset_'))); 130 | eegScoreIdx=find(~cellfun(@isempty,strfind(Ds.labels_type,'EEG sleep score'))); 131 | sessionNumberIdx=find(~cellfun(@isempty,strfind(Ds.labels_type,'Session number'))); 132 | sleep_fmri=[... 133 | Ds.data,... % fMRI data 134 | ]; 135 | sleep_labels=[... 136 | Ds.labels(:,synsetLabelIdx),... % base synset labels (data) 137 | Ds.labels(:,eegScoreIdx),... % EEG sleep socre (labels) 138 | ]; 139 | sleep_design=[... 140 | Ds.labels(:,sessionNumberIdx),... % session number (design) 141 | (1:size(Ds.data,1))',... % sample number (design) 142 | ]; 143 | sleep_data=[sleep_fmri,sleep_labels,sleep_design]; 144 | 145 | % perception data 146 | synsetLabelPerception=find(~cellfun(@isempty,strfind(Dp.labels_type,'Synset_'))); 147 | synsetNamesPerception=Dp.labels_type(synsetLabelPerception); 148 | [ismem,synserMatchOrd]=ismember(Ds.labels_type(synsetLabel),synsetNamesPerception); 149 | sessionNumberIdx=find(~cellfun(@isempty,strfind(Dp.labels_type,'Session number'))); 150 | perception_fmri=[... 151 | Dp.data,... % fMRI data 152 | ]; 153 | perception_labels=[... 154 | Dp.labels(:,synsetLabelPerception(synserMatchOrd)),... % base synset labels (data) 155 | ]; 156 | perception_design=[... 157 | Dp.labels(:,sessionNumberIdx),... % session number (design) 158 | (1:size(Dp.data,1))',... % sample number (design) 159 | ]; 160 | perception_data=[perception_fmri,perception_labels,perception_design]; 161 | 162 | %% create feature meta data 163 | % sleep data 164 | voxelData=[... 165 | ones(1,size(sleep_fmri,2));... % voxelData 166 | Ds.xyz;... % xyz coordinate 167 | props.roiMask... % roi mask 168 | ]; 169 | voxelData_definition={'voxelData','X','Y','Z',props.roiNames{:}}; 170 | 171 | labelData=[... 172 | ones(1,size(sleep_labels,2));... % label 173 | diag(ones(1,size(sleep_labels,2)));... % index for each label 174 | ]; 175 | label_definition={'label',props.synsetNames{:},'EEG_sleep_score'}; 176 | 177 | designData=[... 178 | ones(1,size(sleep_design,2));... % design 179 | diag(ones(1,size(sleep_design,2)));... % index for each design 180 | ]; 181 | design_definition={'design','session_number','sample_number'}; 182 | 183 | sleepMetaData=blkdiag(voxelData,labelData,designData); 184 | sleepDefinition={voxelData_definition{:},label_definition{:},design_definition{:}}; 185 | 186 | % perception data 187 | voxelData=[... 188 | ones(1,size(perception_fmri,2));... % voxelData 189 | Dp.xyz;... % xyz coordinate 190 | props.roiMask... % roi mask 191 | ]; 192 | voxelData_definition={'voxelData','X','Y','Z',props.roiNames{:}}; 193 | 194 | labelData=[... 195 | ones(1,size(perception_labels,2));... % label 196 | diag(ones(1,size(perception_labels,2)));... % index for each label 197 | ]; 198 | label_definition={'label',props.synsetNames{:}}; 199 | 200 | designData=[... 201 | ones(1,size(perception_design,2));... % design 202 | diag(ones(1,size(perception_design,2)));... % index for each design 203 | ]; 204 | design_definition={'design','session_number','sample_number'}; 205 | 206 | perceptionMetaData=blkdiag(voxelData,labelData,designData); 207 | perceptionDefinition={voxelData_definition{:},label_definition{:},design_definition{:}}; 208 | 209 | %% create feature definition data 210 | sleepMetaDefinition=cell(length(sleepDefinition),1); 211 | for i=1:length(sleepDefinition) 212 | switch sleepDefinition{i} 213 | case {'voxelData','label','design'} 214 | sleepMetaDefinition{i}=['0 = not ',sleepDefinition{i},', 1 = ',sleepDefinition{i}]; 215 | case {'X','Y','Z'} 216 | sleepMetaDefinition{i}=['Value = ',sleepDefinition{i},' coordinate']; 217 | case props.roiNames 218 | sleepMetaDefinition{i}=['0 = not ',sleepDefinition{i},' voxel, 1 = ',sleepDefinition{i},' voxel']; 219 | case 'session_number' 220 | sleepMetaDefinition{i}='Number = Sesssion number'; 221 | case 'sample_number' 222 | sleepMetaDefinition{i}='Number = Sample number'; 223 | otherwise 224 | sleepMetaDefinition{i}=['0 = absent, 1 = present']; 225 | end 226 | end 227 | % sleepMetaDefinition 228 | 229 | perceptionMetaDefinition=cell(length(perceptionDefinition),1); 230 | for i=1:length(perceptionDefinition) 231 | switch perceptionDefinition{i} 232 | case {'voxelData','label','design'} 233 | perceptionMetaDefinition{i}=['0 = not ',perceptionDefinition{i},', 1 = ',perceptionDefinition{i}]; 234 | case {'X','Y','Z'} 235 | perceptionMetaDefinition{i}=['Value = ',perceptionDefinition{i},' coordinate']; 236 | case props.roiNames 237 | perceptionMetaDefinition{i}=['0 = not ',perceptionDefinition{i},' voxel, 1 = ',perceptionDefinition{i},' voxel']; 238 | case 'session_number' 239 | perceptionMetaDefinition{i}='Number = Sesssion number'; 240 | case 'sample_number' 241 | perceptionMetaDefinition{i}='Number = Sample number'; 242 | otherwise 243 | perceptionMetaDefinition{i}=['0 = absent, 1 = present']; 244 | end 245 | end 246 | % perceptionMetaDefinition 247 | 248 | %% renew data structure 249 | clear pD* 250 | 251 | pDs.data=sleep_data; 252 | for i=1:size(sleepMetaData,1) 253 | pDs.metaData.(sleepDefinition{i})=sleepMetaData(i,:); 254 | end 255 | pDs.metaDefinition=sleepMetaDefinition; 256 | 257 | pDp.data=perception_data; 258 | for i=1:size(perceptionMetaData,1) 259 | pDp.metaData.(perceptionDefinition{i})=perceptionMetaData(i,:); 260 | end 261 | pDp.metaDefinition=perceptionMetaDefinition; 262 | 263 | props_new=props; 264 | 265 | %% end 266 | fprintf('Preprocessing end\n') 267 | 268 | end 269 | 270 | 271 | 272 | 273 | %% functions 274 | % shift data ---------------------------------------- 275 | function [D,params]=shiftData(D,params) 276 | nshift = getFieldDef(params,'nshift',1); 277 | 278 | sessionIdx=D.labels(:,ismember(D.labels_type,'Session number')); 279 | unisession=unique(sessionIdx); 280 | num_breaks = length(unisession); 281 | 282 | inds_del_data = zeros(num_breaks*nshift,1); 283 | inds_del_labels = zeros(num_breaks*nshift,1); 284 | 285 | for itr=1:num_breaks 286 | idx=find(sessionIdx==unisession(itr)); 287 | bi=idx(1); 288 | ei=idx(end); 289 | 290 | inds_del_data(nshift*(itr-1)+1:nshift*itr) = bi:bi+nshift-1; 291 | inds_del_labels(nshift*(itr-1)+1:nshift*itr) = ei-nshift+1:ei; 292 | end 293 | 294 | 295 | D.data(inds_del_data,:) = []; 296 | D.labels(inds_del_labels,:) = []; 297 | end 298 | 299 | % reduce outliers ---------------------------------------- 300 | function [D,params]=reduceOutliers(D,params) 301 | app_dim = getFieldDef(params,'app_dim',1); 302 | std_thres = getFieldDef(params,'std_thres',3); 303 | num_its = getFieldDef(params,'num_its',10); 304 | max_val = getFieldDef(params,'max_val',inf); 305 | min_val = getFieldDef(params,'min_val',-inf); 306 | 307 | if min_val>-inf || max_val=2 314 | remove = getFieldDef(params,'remove',1); 315 | else 316 | remove = getFieldDef(params,'remove',0); 317 | end 318 | 319 | sessionIdx=D.labels(:,ismember(D.labels_type,'Session number')); 320 | unisession=unique(sessionIdx); 321 | num_breaks = length(unisession); 322 | 323 | ind_all = sparse(size(D.data,1),size(D.data,2)); % keep num of outliers 324 | for itb=1:num_breaks 325 | idx=sessionIdx==unisession(itb); 326 | data_temp = D.data(idx,:); 327 | data_size = size(data_temp); 328 | ind_m1 = zeros(data_size); % indexes of outliers found in method1 (max std deviation) 329 | ind_m2 = zeros(data_size); % indexes of outliers found in method2 (constant min_val, max_val) 330 | if method==1 || method==3 331 | for its=1:num_its % do num_its iterations 332 | mu = mean(data_temp,app_dim); % mean 333 | sd = std(data_temp,0,app_dim); % standard deviation 334 | % Find and clip values OVER threshold: 335 | if app_dim==1, thres_mat = repmat(mu+sd*std_thres,data_size(1),1); % make threshold matrix 336 | else thres_mat = repmat(mu+sd*std_thres,1,data_size(2)); end 337 | ind_out = data_temp>thres_mat; % find values over thres_mat 338 | data_temp(ind_out) = thres_mat(ind_out); % set those to thres_mat 339 | ind_m1(ind_out) = ind_m1(ind_out)+1; 340 | % Find and clip values UNDER threshold: 341 | if app_dim==1, thres_mat = repmat(mu-sd*std_thres,data_size(1),1); % make threshold matrix 342 | else thres_mat = repmat(mu-sd*std_thres,1,data_size(2)); end 343 | ind_out = data_temp=2 349 | if max_valmax_val; 352 | data_temp(ind_out) = max_val; 353 | ind_m2(ind_out) = ind_m2(ind_out)+1; 354 | end 355 | if min_val>-inf 356 | % Below min_val: 357 | ind_out = data_temp0; 411 | ExcludePeriodIdx = D.labels(AwakePeriods(1,:),ismember(D.labels_type,'Excluded samples'))>0; 412 | cnt=0; 413 | for i=1:size(AwakePeriods,2) 414 | if visualDreamIdx(i)>0 && ExcludePeriodIdx(i)==0 415 | cnt=cnt+1; 416 | AnalyzeWindow(:,cnt) = [AwakePeriods(1,i)-analysisPeriod;AwakePeriods(1,i)-1]; 417 | NormalizeWindow(:,cnt) = [AwakePeriods(1,i)-normalizationOnsetfromAwake-normalizeBaselineDuration;AwakePeriods(1,i)-normalizationOnsetfromAwake-1]; 418 | end 419 | end 420 | AnalyzeWindow(:,cnt+1:end) = []; 421 | NormalizeWindow(:,cnt+1:end) = []; 422 | 423 | % Sleep data extraction and normalization 424 | data_tmp = cell(1,size(AnalyzeWindow,2)); 425 | labels_tmp = cell(1,size(AnalyzeWindow,2)); 426 | sleeplabels_tmp = cell(1,size(AnalyzeWindow,2)); 427 | zero_ind = cell(1,size(AnalyzeWindow,2)); 428 | sleepLabelIdx = ismember(D.labels_type,'EEG sleep score'); 429 | triallabels = cell(1,size(AnalyzeWindow,2)); 430 | for i = 1:size(AnalyzeWindow,2) 431 | baseline = mean(D.data(NormalizeWindow(1,i):NormalizeWindow(2,i),:),1); 432 | baseMat = repmat(baseline,analysisPeriod,1); 433 | zero_ind{i} = (abs(baseline) <= zero_thres)'; 434 | data_tmp{i} = 100*(D.data(AnalyzeWindow(1,i):AnalyzeWindow(2,i),:)-baseMat)'./baseMat'; 435 | labels_tmp{i} = repmat(D.labels(AnalyzeWindow(2,i)+1,:),analysisPeriod,1)'; 436 | sleeplabels_tmp{i} = repmat(D.labels(AnalyzeWindow(2,i),sleepLabelIdx),analysisPeriod,1)'; 437 | triallabels{i} = ones(1,analysisPeriod)*i; 438 | end 439 | data_tmp2 = [data_tmp{:}]'; 440 | labels_tmp2 = [labels_tmp{:}]'; 441 | sleeplabels_tmp2 = [sleeplabels_tmp{:}]'; 442 | params.triallabels = [triallabels{:}]'; 443 | zero_ind2 = [zero_ind{:}]'; 444 | if any(sum(zero_ind2)), data_tmp2(:,sum(zero_ind2)>0) = 0; end 445 | D.data = data_tmp2; 446 | D.labels = labels_tmp2; 447 | D.labels(:,ismember(D.labels_type,'EEG sleep score')) = sleeplabels_tmp2; 448 | 449 | end 450 | 451 | 452 | % normalization for perception data---------------------------------------- 453 | function D=normalizeData(D,params) 454 | base_labels = getFieldDef(params,'base_labels',1); 455 | zero_thres = getFieldDef(params,'zero_thres',1); 456 | norm_mode = getFieldDef(params,'norm_mode',0); 457 | 458 | 459 | sessionIdx=D.labels(:,ismember(D.labels_type,'Session number')); 460 | unisession=unique(sessionIdx); 461 | num_breaks = length(unisession); 462 | 463 | for itb=1:num_breaks 464 | idx=find(sessionIdx==unisession(itb)); 465 | 466 | % Pull section (run) out: 467 | data_temp = D.data(idx,:); 468 | 469 | % Find indexes of base condition: 470 | if isstr(base_labels) && strcmp(base_labels,'all') 471 | ind_use = 1:numel(idx); 472 | elseif isnumeric(base_labels) 473 | ind_use = find(ismember(D.labels(idx),base_labels)); 474 | end 475 | 476 | % Calc baseline: 477 | baseline = mean(data_temp(ind_use,:),1); 478 | sd = std(data_temp(ind_use,:),[],1); 479 | 480 | % Find baseline indexes ~= 0 (to avoid dividing by them): 481 | if norm_mode == 0 || norm_mode == 1 482 | zero_ind = find(abs(baseline) <= zero_thres); 483 | elseif norm_mode == 3 484 | zero_ind = find(abs(sd) <= zero_thres); 485 | else 486 | zero_ind = []; 487 | end 488 | 489 | num_zeros = numel(zero_ind); 490 | if num_zeros>1 % If there are some zero values in the baseline 491 | baseline(zero_ind) = zero_thres; % set them to zero_thres to avoid dividing by them 492 | 493 | end 494 | 495 | % mean mat 496 | baseline_mat = repmat(baseline,size(data_temp,1),1); 497 | % sd mat 498 | sd_mat = repmat(sd,size(data_temp,1),1); 499 | 500 | switch norm_mode 501 | case 0 502 | % percent-signal change 503 | data_temp = 100 * (data_temp - baseline_mat) ./ baseline_mat; 504 | case 1 505 | % division by mean only 506 | data_temp = 100 * data_temp ./ baseline_mat; 507 | case 2 508 | % subtraction of mean only 509 | data_temp = data_temp - baseline_mat; 510 | case 3 511 | % z-score 512 | data_temp = (data_temp - baseline_mat) ./ sd_mat; 513 | end 514 | 515 | % Set zero_ind data to zero: 516 | if num_zeros>1, data_temp(zero_ind) = 0; end 517 | % Put normalized section (run) back: 518 | D.data(idx,:) = data_temp; 519 | end 520 | end 521 | 522 | % averaging sleep data ---------------------------------------- 523 | function D=averageSleepData(D,params) 524 | twShift = getFieldDef(params,'twShift',1); 525 | winDur = getFieldDef(params,'winDur',3); 526 | if ~isfield(params,'triallabels') 527 | error('missing trial labels.') 528 | else 529 | triallabels=params.triallabels; 530 | end 531 | 532 | % extract analysis window and average them 533 | inds_blocks=cpDetect(triallabels); 534 | data_tmp=zeros(size(inds_blocks,2),size(D.data,2)); 535 | labels_tmp=zeros(size(inds_blocks,2),size(D.labels,2)); 536 | for i=1:size(inds_blocks,2) 537 | idx=inds_blocks(2,i)-(twShift+winDur-1:-1:twShift)+1; 538 | data_tmp(i,:)=mean(D.data(idx,:)); 539 | labels_tmp(i,:)=D.labels(idx(end),:); 540 | end 541 | D.data=data_tmp; 542 | D.labels=labels_tmp; 543 | end 544 | 545 | 546 | 547 | % averaging perception data ---------------------------------------- 548 | function D=averagePerceptionData(D) 549 | analyzeLabels=[4]; % 4 is labels for stimuls periods 550 | % averaged data 551 | conditionlabelIdx=ismember(D.labels_type,'Experimental conditions'); 552 | inds=ismember(D.labels(:,conditionlabelIdx),analyzeLabels); 553 | cp=cpDetect(inds); 554 | use_periods=cp(:,2:2:end); 555 | data_tmp=zeros(size(use_periods,2),size(D.data,2)); 556 | labels_tmp=zeros(size(use_periods,2),size(D.labels,2)); 557 | for itr=1:size(use_periods,2) 558 | ind=use_periods(1,itr):use_periods(2,itr); 559 | data_tmp(itr,:)=mean(D.data(ind,:),1); 560 | labels_tmp(itr,:)=D.labels(ind(1),:); 561 | end 562 | D.data=data_tmp; 563 | D.labels=labels_tmp; 564 | end 565 | 566 | 567 | 568 | --------------------------------------------------------------------------------