% Explicit examples on how to use the different classifiers
size_cache=10;
degree=3;
inhomogene=true;
use_normalization=true;

addpath('.');

% So-called "train data" from the train-train dataset.
traindata_original=load_matrix('training_ba1234GTposAPdupYRamil2MMX.dat');
trainlabels=load_matrix('trainlabel_1165_36936.dat');

% So-called "test data" from the alternative dataset.
testdata_original=load_matrix('test_ba1234GTposAPdupYRamil2MMX.dat');
testlabels=load_matrix('testlabel_673_18239.dat');
pivot=673;

models=["ba1mm2";"ba2mm10";"ba3mm9";"ba4mm10";"GTmm2";"POSmm5";"AP3mm4";"DUPw1j1m7";"YRw1j1m5";"AMI_lg2"];
N=length(models);
M=2330;

printf("Individual Maps\n");
for k=1:1:N
	testdata=testdata_original(k,:);
	traindata=traindata_original(k,1:M);
	trainlabels=trainlabels(1:M);

	sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
	sg('set_features', 'TRAIN', traindata);
	sg('set_labels', 'TRAIN', trainlabels);
	sg('new_classifier', 'LIBSVM');
	sg('train_classifier');
	sg('set_features', 'TEST', testdata);
	result=sg('classify');
	S=sign(result);

	sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
	spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
	Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
	printf("%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(k,:),degree, 100*sens, 100*spec, 100*Mval );
endfor

printf("\n2D\n");
for j=1:1:(N-1)
	for k=(j+1):1:N
		testdata=testdata_original([j,k],:);
		traindata=traindata_original([j,k],1:M);
		trainlabels=trainlabels(1:M);
	
		sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
		sg('set_features', 'TRAIN', traindata);
		sg('set_labels', 'TRAIN', trainlabels);
		sg('new_classifier', 'LIBSVM');
		sg('train_classifier');
		sg('set_features', 'TEST', testdata);
		result=sg('classify');
		S=sign(result);

		sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
		spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
		Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
		printf("%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
	endfor
endfor

printf("\n3D Matrix\n");
for i=1:1:(N-2)
	for j=(i+1):1:(N-1)
		for k=(j+1):1:N
			testdata=testdata_original([i,j,k],:);
			traindata=traindata_original([i,j,k],1:M);
			trainlabels=trainlabels(1:M);

			sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
			sg('set_features', 'TRAIN', traindata);
			sg('set_labels', 'TRAIN', trainlabels);
			sg('new_classifier', 'LIBSVM');
			sg('train_classifier');
			sg('set_features', 'TEST', testdata);
			result=sg('classify');
			S=sign(result);

			sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
			spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
			Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
			printf("%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
		endfor
	endfor
endfor

printf("\n4D Matrix\n");
for h=1:1:(N-3)
	for i=(h+1):1:(N-2)
		for j=(i+1):1:(N-1)
			for k=(j+1):1:N
				testdata=testdata_original([h,i,j,k],:);
				traindata=traindata_original([h,i,j,k],1:M);
				trainlabels=trainlabels(1:M);

				sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
				sg('set_features', 'TRAIN', traindata);
				sg('set_labels', 'TRAIN', trainlabels);
				sg('new_classifier', 'LIBSVM');
				sg('train_classifier');
				sg('set_features', 'TEST', testdata);
				result=sg('classify');
				S=sign(result);

				sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
				spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
				Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
				printf("%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
			endfor
		endfor
	endfor
endfor

printf("\n5D Matrix\n");
for g=1:1:(N-4)
	for h=(g+1):1:(N-3)
		for i=(h+1):1:(N-2)
			for j=(i+1):1:(N-1)
				for k=(j+1):1:N
					testdata=testdata_original([g,h,i,j,k],:);
					traindata=traindata_original([g,h,i,j,k],1:M);
					trainlabels=trainlabels(1:M);
					
					sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
					sg('set_features', 'TRAIN', traindata);
					sg('set_labels', 'TRAIN', trainlabels);
					sg('new_classifier', 'LIBSVM');
					sg('train_classifier');
					sg('set_features', 'TEST', testdata);
					result=sg('classify');
					S=sign(result);

					sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
					spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
					Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
					printf("%s\t%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(g,:),models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
				endfor
			endfor
		endfor
	endfor
endfor

printf("\n6D Matrix\n");
for f=1:1:(N-5)
	for g=(f+1):1:(N-4)
		for h=(g+1):1:(N-3)
			for i=(h+1):1:(N-2)
				for j=(i+1):1:(N-1)
					for k=(j+1):1:N
						A=[f,g,h,i,j,k];
						testdata=testdata_original(A,:);
						traindata=traindata_original(A,1:M);
						trainlabels=trainlabels(1:M);
						
						sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
						sg('set_features', 'TRAIN', traindata);
						sg('set_labels', 'TRAIN', trainlabels);
						sg('new_classifier', 'LIBSVM');
						sg('train_classifier');
						sg('set_features', 'TEST', testdata);
						result=sg('classify');
						S=sign(result);

						sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
						spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
						Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
						printf("%s\t%s\t%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(f,:),models(g,:),models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
					endfor
				endfor
			endfor
		endfor
	endfor
endfor

printf("\n7D Matrix\n");
for d=1:1:(N-6)
	for f=(d+1):1:(N-5)
		for g=(f+1):1:(N-4)
			for h=(g+1):1:(N-3)
				for i=(h+1):1:(N-2)
					for j=(i+1):1:(N-1)
						for k=(j+1):1:N
							A=[d,f,g,h,i,j,k];
							testdata=testdata_original(A,:);
							traindata=traindata_original(A,1:M);
							
							sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
							sg('set_features', 'TRAIN', traindata);
							sg('set_labels', 'TRAIN', trainlabels);
							sg('new_classifier', 'LIBSVM');
							sg('train_classifier');
							sg('set_features', 'TEST', testdata);
							result=sg('classify');
							S=sign(result);

							sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
							spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
							Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
							printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(d,:),models(f,:),models(g,:),models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
						endfor
					endfor
				endfor
			endfor
		endfor
	endfor
endfor

printf("\n8D Matrix\n");
for c=1:1:(N-5)
	for d=(c+1):1:(N-6)
		for f=(d+1):1:(N-5)
			for g=(f+1):1:(N-4)
				for h=(g+1):1:(N-3)
					for i=(h+1):1:(N-2)
						for j=(i+1):1:(N-1)
							for k=(j+1):1:N
								A=[c,d,f,g,h,i,j,k];
								testdata=testdata_original(A,:);
								traindata=traindata_original(A,1:M);
								
								sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
								sg('set_features', 'TRAIN', traindata);
								sg('set_labels', 'TRAIN', trainlabels);
								sg('new_classifier', 'LIBSVM');
								sg('train_classifier');
								sg('set_features', 'TEST', testdata);
								result=sg('classify');
								S=sign(result);

								sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
								spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
								Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
								printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(c,:),models(d,:),models(f,:),models(g,:),models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
							endfor
						endfor
					endfor
				endfor
			endfor
		endfor
	endfor
endfor

printf("\n9D Matrix\n");
for b=1:1:(N-6)
	for c=(b+1):1:(N-5)
		for d=(c+1):1:(N-6)
			for f=(d+1):1:(N-5)
				for g=(f+1):1:(N-4)
					for h=(g+1):1:(N-3)
						for i=(h+1):1:(N-2)
							for j=(i+1):1:(N-1)
								for k=(j+1):1:N
									A=[b,c,d,f,g,h,i,j,k];
									testdata=testdata_original(A,:);
									traindata=traindata_original(A,1:M);
									
									sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
									sg('set_features', 'TRAIN', traindata);
									sg('set_labels', 'TRAIN', trainlabels);
									sg('new_classifier', 'LIBSVM');
									sg('train_classifier');
									sg('set_features', 'TEST', testdata);
									result=sg('classify');
									S=sign(result);

									sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
									spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
									Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
									printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%5.2f\t%5.2f\t%5.2f\n",models(b,:),models(c,:),models(d,:),models(f,:),models(g,:),models(h,:),models(i,:),models(j,:),models(k,:), degree, 100*sens, 100*spec, 100*Mval );
								endfor
							endfor
						endfor
					endfor
				endfor
			endfor
		endfor
	endfor
endfor

printf("\n10D Matrix\n");
testdata=testdata_original;
traindata=traindata_original(:,1:M);
trainlabels=trainlabels(1:M);
sg('set_kernel', 'POLY', 'REAL', size_cache, degree, inhomogene, use_normalization);
sg('set_features', 'TRAIN', traindata);
sg('set_labels', 'TRAIN', trainlabels);
sg('new_classifier', 'LIBSVM');
sg('train_classifier');
sg('set_features', 'TEST', testdata);
result=sg('classify');
S=sign(result);

sens=(sum(abs(S((pivot+1):end)+testlabels((pivot+1):end)))/2)/length(testlabels((pivot+1):end));
spec=(sum(abs(S(1:pivot)+testlabels(1:pivot)))/2)/length(testlabels(1:pivot));
Mval= 1 - sqrt( ( (sens-1)^2 + (spec-1)^2 ) / 2 );
printf("All Models\t%d\t%5.2f\t%5.2f\t%5.2f\n", degree, 100*sens, 100*spec, 100*Mval );
