<commit_before>'''
Some routines to interface with GPML.

@authors: 
          <NAME> (<EMAIL>)
          <NAME> (<EMAIL>)
          <NAME> (<EMAIL>)
'''

import numpy as np
nax = np.newaxis
import scipy.io
import tempfile, os
import subprocess

import config

def run_matlab_code(code, verbose=False):
    # Write to a temp script
    script_file = tempfile.mkstemp(suffix='.m')[1]
    stdout_file = tempfile.mkstemp(suffix='.txt')[1]
    stderr_file = tempfile.mkstemp(suffix='.txt')[1]
    
    f = open(script_file, 'w')
    f.write(code)
    f.close()
    
    call = [config.MATLAB_LOCATION, '-nosplash', '-nojvm', '-nodisplay']
    
    stdin = open(script_file)
    stdout = open(stdout_file, 'w')
    stderr = open(stderr_file, 'w')
    
#    subprocess.call(call, stdin=open(script_file), stdout=open(stdout_file, 'w'), stderr=open(stderr_file, 'w'))
    subprocess.call(call, stdin=stdin, stdout=stdout, stderr=stderr)
    
    stdin.close()
    stdout.close()
    stderr.close()
    
    f = open(stderr_file)
    err_txt = f.read()
    f.close()
    
    if verbose:
        print
        print 'Script file (%s) contents : ==========================================' % script_file
        print open(script_file, 'r').read()
        print
        print 'Std out : =========================================='        
        print open(stdout_file, 'r').read()   
    
    if err_txt != '':
        #### TODO - need to catch error local to new MLG machines
#        print 'Matlab produced the following errors:\n\n%s' % err_txt    
        pass 
#        raise RuntimeError('Matlab produced the following errors:\n\n%s' % err_txt)
    else:     
        # Only remove temporary files if run was successful    
        os.remove(script_file)
        os.remove(stdout_file)
        os.remove(stderr_file)
    


# Matlab code to optimise hyper-parameters on one file, given one kernel.
OPTIMIZE_KERNEL_CODE = r"""
a='Load the data, it should contain X and y.'
load '%(datafile)s'

%% Load GPML
addpath(genpath('%(gpml_path)s'));

%% Set up model.
meanfunc = {@meanConst}
hyp.mean = mean(y)

covfunc = %(kernel_family)s
hyp.cov = %(kernel_params)s

likfunc = @likGauss
hyp.lik = %(noise)s

[hyp_opt, nlls] = minimize(hyp, @gp, -%(iters)s, @infExact, meanfunc, covfunc, likfunc, X, y);
best_nll = nlls(end)

%% Compute Hessian numerically for laplace approx
num_hypers = length(hyp.cov);
hessian = NaN(num_hypers, num_hypers);
delta = 1e-6;
a='Get original gradients';
[nll_orig, dnll_orig] = gp(hyp_opt, @infExact, meanfunc, covfunc, likfunc, X, y);
for d = 1:num_hypers
    dhyp_opt = hyp_opt;
    dhyp_opt.cov(d) = dhyp_opt.cov(d) + delta;
    [nll_delta, dnll_delta] = gp(dhyp_opt, @infExact, meanfunc, covfunc, likfunc, X, y);
    hessian(d, :) = (dnll_delta.cov - dnll_orig.cov) ./ delta;
end
hessian = 0.5 * (hessian + hessian');
logdet = 2 * sum(log(diag(chol(hessian))));<commit_msg>Made laplace approx robust to begative eigenvlaues<commit_after>'''
Some routines to interface with GPML.

@authors: 
          <NAME> (<EMAIL>)
          <NAME> (<EMAIL>)
          <NAME> (<EMAIL>)
'''

import numpy as np
nax = np.newaxis
import scipy.io
import tempfile, os
import subprocess

import config

def run_matlab_code(code, verbose=False):
    # Write to a temp script
    script_file = tempfile.mkstemp(suffix='.m')[1]
    stdout_file = tempfile.mkstemp(suffix='.txt')[1]
    stderr_file = tempfile.mkstemp(suffix='.txt')[1]
    
    f = open(script_file, 'w')
    f.write(code)
    f.close()
    
    call = [config.MATLAB_LOCATION, '-nosplash', '-nojvm', '-nodisplay']
    
    stdin = open(script_file)
    stdout = open(stdout_file, 'w')
    stderr = open(stderr_file, 'w')
    
#    subprocess.call(call, stdin=open(script_file), stdout=open(stdout_file, 'w'), stderr=open(stderr_file, 'w'))
    subprocess.call(call, stdin=stdin, stdout=stdout, stderr=stderr)
    
    stdin.close()
    stdout.close()
    stderr.close()
    
    f = open(stderr_file)
    err_txt = f.read()
    f.close()
    
    if verbose:
        print
        print 'Script file (%s) contents : ==========================================' % script_file
        print open(script_file, 'r').read()
        print
        print 'Std out : =========================================='        
        print open(stdout_file, 'r').read()   
    
    if err_txt != '':
        #### TODO - need to catch error local to new MLG machines
#        print 'Matlab produced the following errors:\n\n%s' % err_txt    
        pass 
#        raise RuntimeError('Matlab produced the following errors:\n\n%s' % err_txt)
    else:     
        # Only remove temporary files if run was successful    
        os.remove(script_file)
        os.remove(stdout_file)
        os.remove(stderr_file)
    


# Matlab code to optimise hyper-parameters on one file, given one kernel.
OPTIMIZE_KERNEL_CODE = r"""
a='Load the data, it should contain X and y.'
load '%(datafile)s'

%% Load GPML
addpath(genpath('%(gpml_path)s'));

%% Set up model.
meanfunc = {@meanConst}
hyp.mean = mean(y)

covfunc = %(kernel_family)s
hyp.cov = %(kernel_params)s

likfunc = @likGauss
hyp.lik = %(noise)s

[hyp_opt, nlls] = minimize(hyp, @gp, -%(iters)s, @infExact, meanfunc, covfunc, likfunc, X, y);
best_nll = nlls(end)

%% Compute Hessian numerically for laplace approx
num_hypers = length(hyp.cov);
hessian = NaN(num_hypers, num_hypers);
delta = 1e-6;
a='Get original gradients';
[nll_orig, dnll_orig] = gp(hyp_opt, @infExact, meanfunc, covfunc, likfunc, X, y);
for d = 1:num_hypers
    dhyp_opt = hyp_opt;
    dhyp_opt.cov(d) = dhyp_opt.cov(d) + delta;
    [nll_delta, dnll_delta] = gp(dhyp_opt, @infExact, meanfunc, covfunc, likfunc, X, y);
    hessian(d, :) = (dnll_delta.cov - dnll_orig.cov) ./ delta;
end
hessian = 0.5 * (hessian + hessian');
%%logdet = 2 * sum(log(diag(chol(hessian))));

%% This way of computing logdet is robust to negative eigenvalues (which do happen, unfortunately) --David
[vectors, values] = eig(hessian);
values(values < eps) = eps;  %% HACK
logdet = sum(log(diag(values)));
{'fraction_non_alphanumeric': 0.16570295721130018, 'fraction_numerical': 0.0029737320337022964, 'mean_word_length': 3.321199143468951, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460544', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2139, 'n_words': 521}