wolffd@0: % --- wolffd@0: % class cvpartition_trunctrain wolffd@0: % NOTE: this is a fake cvpartition double for wolffd@0: % using cvpartitions in truncated-training size experiments wolffd@0: % wolffd@0: % differently from cvpartition_trunctrain, we take all the training sizes wolffd@0: % at once and generate training partitions where the smaller ones are subsets wolffd@0: % of the bigger ones wolffd@0: % --- wolffd@0: classdef cvpartition_trunctrain_incsubsets wolffd@0: wolffd@0: properties (Hidden) wolffd@0: wolffd@0: mtest; wolffd@0: mtraining; wolffd@0: end wolffd@0: properties wolffd@0: N; wolffd@0: NumTestSets; wolffd@0: TrainSize; wolffd@0: TestSize; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: methods wolffd@0: wolffd@0: % --- wolffd@0: % constuctor: directly calculates the truncated testset wolffd@0: % --- wolffd@0: function P = cvpartition_trunctrain_incsubsets(Pin, perctrain) wolffd@0: wolffd@0: % --- wolffd@0: % NOTE: we use a different permutation for each cv-Buun (testset), wolffd@0: % as otherwise the very small training sets will have about the same wolffd@0: % data wolffd@0: % --- wolffd@0: if ~cvpartition_trunctrain_incsubsets.exists_permutation(Pin) wolffd@0: cvpartition_trunctrain_incsubsets.renew_permutation(Pin); wolffd@0: end wolffd@0: wolffd@0: P.N = Pin.N; wolffd@0: P.NumTestSets = Pin.NumTestSets; wolffd@0: wolffd@0: for i = 1:Pin.NumTestSets wolffd@0: wolffd@0: % copy testing data wolffd@0: P.TestSize(i) = Pin.TestSize(i); wolffd@0: P.mtest{i} = Pin.test(i); wolffd@0: wolffd@0: % calculate new training size wolffd@0: P.TrainSize(i) = ceil(perctrain * Pin.TrainSize(i)); wolffd@0: wolffd@0: % get actual training indices wolffd@0: idx = find(Pin.training(i)); wolffd@0: wolffd@0: % --- wolffd@0: % NOTE: the Test-Set-Specific permutation is applied wolffd@0: % we only extract as many indices as fit in Pin wolffd@0: % --- wolffd@0: permu = cvpartition_trunctrain_incsubsets.get_permutation(i,Pin.TrainSize(i)); wolffd@0: wolffd@0: % truncate the indices wolffd@0: idx = idx(permu(1:P.TrainSize(i))); wolffd@0: wolffd@0: % build truncated training set wolffd@0: P.mtraining{i} = false(P.N, 1); wolffd@0: P.mtraining{i}(idx) = true; wolffd@0: end wolffd@0: end wolffd@0: function out = test(P, i) wolffd@0: wolffd@0: out = P.mtest{i}; wolffd@0: end wolffd@0: wolffd@0: function out = training(P, i) wolffd@0: wolffd@0: out = P.mtraining{i}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: methods (Static) wolffd@0: wolffd@0: % --- wolffd@0: % TODO: save the permutation in a global variable, wolffd@0: % tomake the same smaller set available wolffd@0: % for all further experiments. wolffd@0: % moreover, it would be great if the smaller training sets wolffd@0: % are subsets of the bigger ones wolffd@0: % --- wolffd@0: function renew_permutation(P) wolffd@0: global globalvars; wolffd@0: wolffd@0: if isfield(globalvars.camir, ... wolffd@0: 'cvpartition_trunctrain_incsubsets'); wolffd@0: warning 'renwewing permutations for train sets'; wolffd@0: end wolffd@0: wolffd@0: for i = 1:P.NumTestSets wolffd@0: globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(i).data = ... wolffd@0: randperm(P.N); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: function idx = get_permutation(testId, trainSize) wolffd@0: % returns the permutation for specific test set wolffd@0: global globalvars; wolffd@0: wolffd@0: idx = globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(testId).data; wolffd@0: wolffd@0: % cut the permutation to contain no exxcess numbers wolffd@0: idx = idx(idx <= trainSize); wolffd@0: end wolffd@0: wolffd@0: function out = exists_permutation(P) wolffd@0: global globalvars; wolffd@0: if isfield(globalvars.camir, ... wolffd@0: 'cvpartition_trunctrain_incsubsets'); wolffd@0: wolffd@0: out = (numel(globalvars.camir.cvpartition_trunctrain_incsubsets.permutation) == P.NumTestSets) ... wolffd@0: && (numel(globalvars.camir.cvpartition_trunctrain_incsubsets.permutation(1).data) == P.N); wolffd@0: wolffd@0: wolffd@0: else out = false; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: end wolffd@0: end