function RUN_CLASSIFY(Aset,Bset,TASK)
% Nomenclature is that Aset TARGETS are classified (1's) from Bset STANDARDS (0's)
% Although the actual data type labeled as such may vary 

for rep = 1:5
    if rem(rep,10)==0
        disp([TASK,'     ',num2str(rep)]);
    end
    
    % Set the random seed
    RandStream.setGlobalStream(RandStream('mt19937ar','seed',sum(100*clock)));
    
    % X-Validation: Equate epochs
    size_targs=size(Aset,3);
    size_stds=size(Bset,3);
    if size_targs<size_stds
        temp=randperm(size_stds);
        TARGETS=Aset;
        STANDARDS=Bset(:,:,temp(1:size_targs));  clear temp size_stds size_targs;
    elseif size_stds<size_targs
        temp=randperm(size_targs);
        TARGETS=Aset(:,:,temp(1:size_stds));  clear temp size_stds size_targs;
        STANDARDS=Bset;
    else
        TARGETS=Aset;
        STANDARDS=Bset;
    end
    AllData = cat(3,TARGETS,STANDARDS);
    EPOCHS = [ones(1,size(TARGETS,3)),zeros(1,size(STANDARDS,3))];
    Size_Per_Set = size(STANDARDS,3);
    
    % validate and test size = 20% trials each   ****************************   5x Cross Validate
    testsize = floor(.2*Size_Per_Set);
    trainsize = Size_Per_Set-2*testsize;
    ForRand = randperm(Size_Per_Set);
    % get shuffled trials for train set
    TrainBool = zeros(1,Size_Per_Set); TrainBool(ForRand(1:trainsize))=1;
    % get shuffled trials for validate set
    Test1Bool = zeros(1,Size_Per_Set); Test1Bool(ForRand(trainsize+1:trainsize+testsize))=1;
    % get shuffled trials for test set
    Test2Bool = zeros(1,Size_Per_Set); Test2Bool(ForRand(trainsize+testsize+1:end))=1;
    % same for standards and targets
    TrainBool = [TrainBool TrainBool];
    Test1Bool = [Test1Bool Test1Bool];
    Test2Bool = [Test2Bool Test2Bool];
    
    
    
    % Classify ****** Targets ****** (EPOCH==1)
    tic
    
    
    % training set
    x_train = AllData(:,:,TrainBool==1);% elec, time, trial
    % normalize!
    x_mean = mean(x_train,3); x_std = std(x_train,[],3);
    x_train_norm = (x_train - repmat(x_mean,[1,1,size(x_train,3)]));  % mean -normalize
    x_train_norm = x_train_norm./repmat(x_std,[1,1,size(x_train,3)]);
    y_train = EPOCHS(TrainBool==1);
    
    % validate set
    xtstset1 = AllData(:,:,Test1Bool == 1);
    % normalize to train set
    xtstset1_norm = (xtstset1-repmat(x_mean,[1,1,size(xtstset1,3)]));
    xtstset1_norm = xtstset1_norm./ repmat(x_std,[1,1,size(xtstset1,3)]); % normalize to trn data
    y_tstset1 = EPOCHS(Test1Bool==1);
    
    % test set
    xtstset2 = AllData(:,:,Test2Bool == 1);
    % normalize to train set
    xtstset2_norm = (xtstset2-repmat(x_mean,[1,1,size(xtstset2,3)]));
    xtstset2_norm = xtstset2_norm./ repmat(x_std,[1,1,size(xtstset2,3)]); % normalize to trn data
    y_tstset2 = EPOCHS(Test2Bool==1);
    
    % store normalizing constants. Important to normalize to that when applying
    % weights to a different data set later.
    LASSO_norm{1}(rep,:,:) = x_mean;
    LASSO_norm{2}(rep,:,:) = x_std;
    
    
    % Make #bins of samples (SpatioTemporal chunks) overlapping by 50%
    Nelec=60;
    samples=12;
    setlength=samples*Nelec; % 12 samples * 60 electrodes
    bins=floor(250/samples + (250/samples)-1); % + 50% overlap bins
    % maxruns=101; % will actually be 100 runs
    STstart=1;  STend=samples;
    for STi=1:bins
        thex_train_norm(STi,:,:) = reshape(x_train_norm(:,STstart:STend,:),...
            [setlength,length(y_train)]);
        thex_tstset1_norm(STi,:,:) = reshape(xtstset1_norm(:,STstart:STend,:),...
            [setlength,length(y_tstset1)]);
        thex_tstset2_norm(STi,:,:) = reshape(xtstset2_norm(:,STstart:STend,:),...
            [setlength,length(y_tstset2)]);
        STstart = STstart+samples/2;  STend=STend+samples/2;
    end
    All_x_train = thex_train_norm; clear x_train the_x_train_norm x_train_norm targets
    All_x_tstset1 = thex_tstset1_norm; clear xtstset1 the_x_tstset1_norm x_tstset1_norm thex_tstset1_norm
    All_x_tstset2 = thex_tstset2_norm; clear xtstset2 the_x_tstset2_norm x_tstset2_norm thex_tstset2_norm
    
    lambda2 = 0;  % Zero here equals LASSO
    for Time = 1:size(All_x_train,1)
        % get actual sets for particular time
        x_train = squeeze(All_x_train(Time,:,:))';
        x_test1 = squeeze(All_x_tstset1(Time,:,:))';
        x_test2 = squeeze(All_x_tstset2(Time,:,:))';
        
        %lasso classify ------- using Matlab 2011/2012 version
        [B,stats] = lasso(x_train, y_train');
        
        % predict train
        predictor_train = logical((x_train * B >0) );
        IsTarget_train = repmat(y_train',1,size(B,2));
        
        % predict val
        predictor_tstset1 = logical((x_test1 * B >0) );
        IsTarget_tstset1 = repmat(y_tstset1',1,size(B,2));
        
        % predict test
        predictor_tstset2 = logical((x_test2 * B >0) );
        IsTarget_tstset2 = repmat(y_tstset2',1,size(B,2));
        
        % select regularization param that gets best generalization on
        % validation set. If many, go for sparsity.
        generalize = mean(predictor_tstset1==IsTarget_tstset1);
        [Test1Acc,tochoose] = max(generalize);
        tochoose = tochoose(1);
        
        % Get Training accuracy for that param
        TrainAcc = mean(IsTarget_train(:,tochoose)==predictor_train(:,tochoose));
        % Get test set accuracy for that param, unbiased
        Test2Acc = mean(IsTarget_tstset2(:,tochoose)==predictor_tstset2(:,tochoose));
        
        % Get separately for the two conditions
        targets = find(y_train == 1);
        TrainAcc1 = mean(IsTarget_train(targets,tochoose)==predictor_train(targets,tochoose));
        targets = find(y_tstset1 == 1);
        Test1Acc1 = mean(IsTarget_tstset1(targets,tochoose)==predictor_tstset1(targets,tochoose));
        targets = find(y_tstset2 == 1);
        Test2Acc1 = mean(IsTarget_tstset2(targets,tochoose)==predictor_tstset2(targets,tochoose));
        
        targets = find(y_train == 0);
        TrainAcc0 = mean(IsTarget_train(targets,tochoose)==predictor_train(targets,tochoose));
        targets = find(y_tstset1 == 0);
        Test1Acc0 = mean(IsTarget_tstset1(targets,tochoose)==predictor_tstset1(targets,tochoose));
        targets = find(y_tstset2 == 0);
        Test2Acc0 = mean(IsTarget_tstset2(targets,tochoose)==predictor_tstset2(targets,tochoose));
        
        % store
        LASSO_Probability_Train(rep,Time,:) = [TrainAcc TrainAcc1 TrainAcc0];
        LASSO_Probability_Tst1(rep,Time,:) = [Test1Acc Test1Acc1 Test1Acc0];
        LASSO_Probability_Tst2(rep,Time,:) = [Test2Acc Test2Acc1 Test2Acc0];
        LASSO_Betas(rep,Time,:)=B(:,tochoose)';
        
        clear beta B info predictor IsTarget MightBeTarget StdAcc TargAcc;
        clear TrainAcc TrainAcc1 TrainAcc0 Test1Acc Test1Acc1 Test1Acc0 Test2Acc Test2Acc1 Test2Acc0;
        clear x_train x_test1 x_test2 stats predictor* IsTarget* generalize tochoose targets;
        
    end % end Time
    clear x* y* All_x* TARGETS STANDARDS AllData EPOCHS *Bool;
    toc
    
end
% Save
save([TASK,'_LASSO.mat'],...
    'LASSO_Probability_Train','LASSO_Probability_Tst1',...
    'LASSO_Probability_Tst2','LASSO_Betas','LASSO_norm');







