Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slpartitionpca_apply
Home > sltoolbox > subspace_ex > slpartitionpca_apply.m

slpartitionpca_apply

PURPOSE ^

SLPARTITIONPCA_APPLY applies partition-based PCA to a set of arrays

SYNOPSIS ^

function feas = slpartitionpca_apply(S, modeldir, data, n, k)

DESCRIPTION ^

SLPARTITIONPCA_APPLY applies partition-based PCA to a set of arrays

 $ Syntax $
   - feas = slpartitionpca_apply(S, modeldir, data, n)
   - feas = slpartitionpca_apply(S, modeldir, data, n, k)

 $ Arguments $
   - S:            the partition-based PCA model struct or model core file
   - modeldir:     the directory path of the projection files
   - data:         the arrays for feature extraction
   - n:            the number of samples
   - k:            the finally used number of features
   - feas:         the features

 $ Description $
   - feas = slpartitionpca_apply(S, modeldir, data, n) applies the 
     partition PCA to a set of array units given in data. Here, S can be 
     a struct loaded from core file or the core filename. modeldir is the 
     directory of the corefile and the related projection matrix files.
     data can be either an array or a cell array of array filenames.

   - feas = slpartitionpca_apply(S, modeldir, data, n, k) further 
     reduces the feature space dimension to k by discarding the trailing
     feature components. The feature space can only be truncated when
     the combined model is learned.

 $ History $
   - Created by Dahua Lin on Jul 30th, 2006
   - Modified by Dahua Lin, on Sep 10, 2006
       - replace sladd by sladdvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdvec SLADDVEC adds a vector to columns or rows of a matrix
  • slreadarray SLREADARRAY Reads an array from an array file
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • sladdpath SLADDPATH Adds dirpath to precede the filenames
  • slrange2indcells SLRANGE2INDCELLS Converts a range array to indices cell array
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function feas = slpartitionpca_apply(S, modeldir, data, n, k)
0002 %SLPARTITIONPCA_APPLY applies partition-based PCA to a set of arrays
0003 %
0004 % $ Syntax $
0005 %   - feas = slpartitionpca_apply(S, modeldir, data, n)
0006 %   - feas = slpartitionpca_apply(S, modeldir, data, n, k)
0007 %
0008 % $ Arguments $
0009 %   - S:            the partition-based PCA model struct or model core file
0010 %   - modeldir:     the directory path of the projection files
0011 %   - data:         the arrays for feature extraction
0012 %   - n:            the number of samples
0013 %   - k:            the finally used number of features
0014 %   - feas:         the features
0015 %
0016 % $ Description $
0017 %   - feas = slpartitionpca_apply(S, modeldir, data, n) applies the
0018 %     partition PCA to a set of array units given in data. Here, S can be
0019 %     a struct loaded from core file or the core filename. modeldir is the
0020 %     directory of the corefile and the related projection matrix files.
0021 %     data can be either an array or a cell array of array filenames.
0022 %
0023 %   - feas = slpartitionpca_apply(S, modeldir, data, n, k) further
0024 %     reduces the feature space dimension to k by discarding the trailing
0025 %     feature components. The feature space can only be truncated when
0026 %     the combined model is learned.
0027 %
0028 % $ History $
0029 %   - Created by Dahua Lin on Jul 30th, 2006
0030 %   - Modified by Dahua Lin, on Sep 10, 2006
0031 %       - replace sladd by sladdvec to increase efficiency
0032 %
0033 
0034 %% parse and verify input arguments
0035 
0036 if nargin < 4
0037     raise_lackinput('slpartitionpca_apply', 4);
0038 end
0039 
0040 if ischar(S)
0041     S = load(S);
0042 elseif ~isstruct(S)
0043     error('sltoolbox:invalidarg', ...
0044         'The S should be the filename of the core file or the core struct');
0045 end
0046 
0047 if ~isnumeric(data) && ~iscell(data)
0048     error('sltoolbox:invalidarg', ...
0049         'data should be an numeric array or a cell array of strings');
0050 end
0051 
0052 if nargin < 5
0053     k = S.diminfo.feadim;
0054 else
0055     if k > S.diminfo.feadim
0056         error('k is higher than the dimension of the whole feature subspace');
0057     elseif k < S.diminfo.feadim && isempty(S.combprojfile)
0058         error('The feature space cannot be truncated without the combined model');
0059     end
0060 end
0061 
0062 
0063 %% Generate the intermediate stacked features
0064 
0065 NBlks = numel(S.blocks);
0066 intfeas = zeros(S.diminfo.intdim, n);
0067 
0068 projpaths = sladdpath(S.projfiles, modeldir);
0069 
0070 dc = 0;
0071 for ib = 1 : NBlks
0072     
0073     curblock = S.blocks{ib};
0074     cursubdim = S.diminfo.subdims(ib);    
0075     rgncell = slrange2indcells(curblock);
0076     
0077     projmat = slreadarray(projpaths{ib});
0078     
0079     V = generate_intfeature(S, data, n, rgncell, projmat);
0080     intfeas(dc+1:dc+cursubdim, :) = V;
0081     dc = dc + cursubdim;
0082     
0083     clear projmat;    
0084 end
0085 
0086 
0087 %% Apply combined projection
0088 
0089 if ~isempty(S.combprojfile)
0090     combprojpath = sladdpath(S.combprojfile, modeldir);
0091     
0092     P = slreadarray(combprojpath);    
0093     if k < S.diminfo.feadim
0094         P = P(:, 1:k);
0095     end    
0096     P = P';
0097     
0098     feas = P * intfeas;
0099 else
0100     feas = intfeas;
0101 end
0102             
0103 
0104 
0105 %% Core computing functions
0106 
0107 function V = generate_intfeature(S, data, n, rangecell, projmat)
0108 
0109 [d0, d1] = size(projmat);
0110 nd = length(rangecell);
0111 
0112 if isnumeric(data)
0113     
0114     if size(data, nd+1) ~= n
0115         error('sltoolbox:sizmismatch', ...
0116             'The data size does not match the specified sample number');
0117     end
0118     
0119     localarr = data(rangecell{:}, :);
0120     localarr = reshape(localarr, [d0, n]);
0121     localmean = S.meanarr(rangecell{:}, :);
0122     localmean = reshape(localmean, [d0, 1]);
0123     
0124     localarr = sladdvec(localarr, -localmean, 1);
0125     
0126     V = projmat' * localarr;
0127     
0128 else
0129     
0130     V = zeros(d1, n);
0131     
0132     cf = 0;
0133     nfiles = length(data);
0134     for i = 1 : nfiles        
0135         datapart = slreadarray(data{i});
0136         curn = size(datapart, nd+1);        
0137         V(:, cf+1:cf+curn) = generate_intfeature(S, datapart, curn, rangecell, projmat);      
0138         cf = cf + curn;
0139     end
0140     
0141     if cf ~= n
0142         error('sltoolbox:sizmismatch', ...
0143             'The total number of units in the set of array files is not n');
0144     end
0145 end
0146 
0147 
0148 
0149     
0150         
0151

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com