Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
function [subkernelWeights, predLabels] = performMKLwithShogunPython(Cval, Ytrain, Ytest, mklNorm, imbalance, train_allSeqsConformedSetKernel, test_allSeqsConformedSetKernel, outputFolder, whetherPerformTest, debugLevel, debugMsgLocation)
% PERFORMMKLWITHSHOGUNPYTHON
%
% INPUT PARAMS
% Param 'Cval'
% Cost value for SVM
%
% Param 'Ytrain'
% Labels of the training examples
%
% Param 'Ytest'
% Labels of the test examples
%
% Param 'mklNorm'
% p-norm for the MKL
%
% Param 'imbalance'
% Class imbalance, to be used for upweighting the mis-classification costs of the
% minority class
%
% Param 'train_allSeqsConformedSetKernel'
% Kernel matrix corresponding to the training sequences (train x train dimension)
%
% Param 'test_allSeqsConformedSetKernel'
% Kernel matrix corresponding to the test sequences (possibly test x train dimension)
%
% Param 'outputFolder'
% Location/path on disk of the output folder
%
% Param 'whetherPerformTest'
% Flag to specify to the python script that is run from the function if test
% predictions are to be made using the test kernel
%
% Param 'debugLevel'
%
% Param 'debugMsgLocation
%
% OUTPUT PARAMS
% Param 'subkernelWeights'
% Weights assigned to the sub kernel matrices by MKL
%
% Param 'predLabels'
% Prediction labels assigned by the trained model
%
% ADDITIONAL NOTES
%
% Author: snikumbh@mpi-inf.mpg.de
totalArguments = 10;
if nargin < totalArguments
debugLevel = 2;
end
if nargin < totalArguments - 1
whetherPerformTest = 1;
debugLevel = 2;
end
nKernels = size(train_allSeqsConformedSetKernel, 2); %number of kernels is same for train and test, only dimensions differ
% 1. write the kernel files to disk
trainkernelfilesListFilename = strcat(outputFolder, '/train_kernel_filenames.list');
trainlabelsFilename = strcat(outputFolder, '/trainlabels.txt');
kernelfilesListFile = fopen(trainkernelfilesListFilename, 'w');
% all kernels
filenamePrefix = strcat(outputFolder, '/train_allSeqsConformedSetKernel_');
for k=1:nKernels
filename = strcat(filenamePrefix, num2str(k), '.csv');
dlmwrite(filename, train_allSeqsConformedSetKernel{k});
fprintf(kernelfilesListFile, '%s\n', filename);
end
fclose(kernelfilesListFile);
% write labels
dlmwrite(trainlabelsFilename, Ytrain, '\n');
logMessages(debugMsgLocation, sprintf('Using %d different training kernels, all written to disk\n', k), debugLevel);
% Test
testkernelfilesListFilename = strcat(outputFolder, '/test_kernel_filenames.list');
testlabelsFilename = strcat(outputFolder, '/testlabels.txt');
kernelfilesListFile = fopen(testkernelfilesListFilename, 'w');
filenamePrefix = strcat(outputFolder, '/test_allSeqsConformedSetKernel_');
for k=1:nKernels
filename = strcat(filenamePrefix, num2str(k), '.csv');
dlmwrite(filename, test_allSeqsConformedSetKernel{k});
fprintf(kernelfilesListFile, '%s\n', filename);
end
fclose(kernelfilesListFile);
% write labels
dlmwrite(testlabelsFilename, Ytest, '\n');
logMessages(debugMsgLocation, sprintf('%d kernels for test, all written to disk\n', k), debugLevel);
% 2. run the python script that will read them and perform MKL
% system cmd to handle python script running
%
predLabelsFilename = strcat(outputFolder, '/predictedlabels.txt');
subkernelWeightsFilename = strcat(outputFolder, '/subkernel_weights.txt');
%
predLabels = zeros(size(Ytest));
subkernelWeights = zeros(nKernels,1);
%
cmdToRunPythonScript = ['./mkl.py -K ', trainkernelfilesListFilename, ' -Y ', trainlabelsFilename, ' -C ', num2str(Cval), ' -P ', num2str(mklNorm), ' -O ', predLabelsFilename, ' -T ', testkernelfilesListFilename, ' -L ', testlabelsFilename, ' -I ', num2str(imbalance), ' -W ', subkernelWeightsFilename, ' -t ', num2str(whetherPerformTest)];
logMessages(debugMsgLocation, sprintf('%s', cmdToRunPythonScript), debugLevel);
if debugLevel == 0
[status, cmdout] = system(cmdToRunPythonScript);
elseif debugLevel == 2
[status, cmdout] = system(cmdToRunPythonScript);% '-echo'
end
logMessages(debugMsgLocation, sprintf('%s', cmdout), debugLevel);
% 3. read in the output from the python script and
% return back the values.
if status == 0 %status 0 indicates command completed successfully
predLabels = dlmread(predLabelsFilename);
subkernelWeights = dlmread(subkernelWeightsFilename);
%else
% Files exist?
% Possibly, MKL failed. Parse cmdout to find out.
% What to do?
% vectors result and subkernelWeights are already set to zeros.
% Thus, interpret these zero vectors accordingly in the parent function
end
end