Esempio n. 1
0
 def __init__(self, X, Y, kern, name="PseudoGPR"):
     super(PseudGPR, self).__init__(name=name)
     # GPy.Parameterized.__init__(self, name=name)
     self.X = GPy.Param("input", X)
     self.link_parameter(self.X)
     self.Y = GPy.Param("target", Y)
     self.link_parameter(self.Y)
     self.kern = kern
     self.link_parameter(self.kern)
     self.likelihood = GPy.likelihoods.Gaussian()
     self.link_parameter(self.likelihood)
Esempio n. 2
0
def predictTest(filename):
    startTime = datetime.now()
    cheatPickle = GPy.load(
        'cheatPickle.pkl')  # - Stirr pickle to the right class,
    #       otherwise it tries to load a paramz object.
    # - Pickle is not working properly with GPy objects
    #       saved on Pegasus 2, it only works with
    #       the ones saved in my WS.
    model_v = GPy.load(filename + '_v.pkl')
    model_u = GPy.load(filename + '_u.pkl')
    f = sio.loadmat(filename + '.mat')
    Xt = f['Xt']
    obst = f['test_points']
    vt = obst[:, 0]
    ut = obst[:, 1]
    Nt = ut.size
    step = Nt / 10

    for i in range(0, Nt, step):
        if (i + step < Nt):
            Xt2 = Xt[i:i + step, :]
        elif (i < Nt):
            Xt2 = Xt[i:, :]
        V2, VVar2 = model_v.predict(Xt2)
        U2, UVar2 = model_u.predict(Xt2)
        if i == 0:
            V = V2
            VVar = VVar2
            U = U2
            UVar = UVar2
        else:
            V = np.concatenate([V, V2], axis=0)
            U = np.concatenate([U, U2], axis=0)
            VVar = np.concatenate([VVar, VVar2], axis=0)
            UVar = np.concatenate([UVar, UVar2], axis=0)
        print 'step ' + str(i + 1) + '/' + str(10)

    print 'End of script, time : ' + str(datetime.now() - startTime)
    output = filename + '_test.mat'
    sio.savemat(
        output, {
            'Xt': Xt,
            'Vp': V,
            'VpVar': VVar,
            'Up': U,
            'UpVar': UVar,
            'test_points': obst
        })
 def test_load_pickle(self):
     import os
     m = GPy.load(
         os.path.join(os.path.abspath(os.path.split(__file__)[0]),
                      'pickle_test.pickle'))
     self.assertTrue(m.checkgrad())
     self.assertEqual(m.log_likelihood(), -4.7351019830022087)
Esempio n. 4
0
def create_girth_predictor(girths):
    version = 1

    if not girths:
        return None

    g = girths[0]
    m = models.GirthEstimator
    est = m.query.filter((m.version == version) & (m.user_id == g.user_id) & (m.location == g.location)).first()

    days = [w.date for w in girths]
    start_day = min(days)

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        observations = np.array([[w.value] for w in girths])
        integer_days = _days_to_integers(days, start_day).reshape(len(days), 1)

        if len(days) > 1:
            k_rbf = GPy.kern.RBF(1)
            k_rbf.lengthscale.set_prior(Prior.gamma(200, 200))

            kernel = k_rbf

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel)
        else:
            w = girths[0]

            k_var = (w.value / 3000) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)

            mf = GPy.mappings.Constant(1, 1, w.value)

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, mean_function=mf)

        model.optimize()
        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = m(version=version, date=date.today(), location=g.location, model=m_pickle, user_id=g.user_id)
        models.db.session.add(est)
        models.db.session.commit()

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - start_day).days
            days_ = tuple(start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([[i] for i in range(duration)])
        (means, variances) = model.predict(integer_days_)
        stds = np.sqrt(variances)
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    predict.start_day = start_day
    return predict
Esempio n. 5
0
def runRestarts(fname, nres=10, nKernels=2):
    filename = fname + '.pkl'
    startTime = datetime.now()
    cheatPickle = GPy.load(
        'cheatPickle.pkl')  # - Stirr pickle to the right class,
    #       otherwise it tries to load a paramz object.
    # - Pickle is not working properly with GPy objects
    #       saved on Pegasus 2, it only works with
    #       the ones saved in my WS.
    model = GPy.load(filename)
    if (len(model.optimization_runs) > 0):
        # This part is to overcome a bug when reopening a model that was  previously
        # optimized. For some reason, the dictionaries of the optimization_runs are lost
        # when the model is pickled.
        # So I'm saving these dictionaries in a list as 'dict_'+filename, and loading
        # them whenever I want to carry out more optimization runs.
        optDict = read_object(fname + '_dict.pkl')
        for i in range(len(model.optimization_runs)):
            model.optimization_runs[i].__dict__ = optDict[i]
    ###
    hyp_old = model.param_array[:]
    model.optimize_restarts(messages=False, num_restarts=nres)
    hyp = model.param_array
    model.pickle(filename)
    optDict = []
    ### saving dictionaries of the optimization runs
    for i in range(len(model.optimization_runs)):
        optDict.append(model.optimization_runs[i].__dict__)
    saveDict(optDict, fname + '_dict.pkl')
    print 'Optimized Hyperparameters ================================================='
    for i in range(nKernels):
        print 'Var.' + str(i + 1) + ' = ' + str(
            hyp_old[4 * i]) + '   :   ' + str(hyp[4 * i])
        print 'Lt ' + str(i + 1) + '  = ' + str(
            hyp_old[4 * i + 1]) + '   :   ' + str(hyp[4 * i + 1])
        print 'Ly ' + str(i + 1) + '  = ' + str(
            hyp_old[4 * i + 2]) + '   :   ' + str(hyp[4 * i + 2])
        print 'Lx ' + str(i + 1) + '  = ' + str(
            hyp_old[4 * i + 3]) + '   :   ' + str(hyp[4 * i + 3])

    print 'Noise = ' + str(hyp_old[-1]) + '   :   ' + str(hyp[-1])
    print '==========================================================================='

    print 'End of script, time : ' + str(datetime.now() - startTime)
Esempio n. 6
0
 def create_models(self, X, Y):
     for i in range(self.control_dim):
         kernel = GPy.kern.RBF(input_dim=self.state_dim, ARD=1)
         model = PseudGPR(X, Y[:, i:i + 1], kernel)
         # model = GPy.models.GPRegression(X, Y[:, i:i+1], kernel)
         # model.likelihood = GPy.likelihoods.Gaussian()
         # model.X = GPy.Param(X)
         # model.Y = GPy.Param(Y)
         # model._add_parameter_name()
         self.models.append(model)
         display(model)
     self.models = GPy.Param("Models", self.models)
Esempio n. 7
0
def runRestarts(fname,nres=10,nKernels=2):
   filename = fname+'.pkl'
   startTime = datetime.now()
   cheatPickle = GPy.load('cheatPickle.pkl')# - Stirr pickle to the right class,
                                            #       otherwise it tries to load a paramz object.
                                            # - Pickle is not working properly with GPy objects 
                                            #       saved on Pegasus 2, it only works with 
                                            #       the ones saved in my WS. 
   model = GPy.load(filename) 
   if (len(model.optimization_runs)>0):
   # This part is to overcome a bug when reopening a model that was  previously 
   # optimized. For some reason, the dictionaries of the optimization_runs are lost 
   # when the model is pickled. 
   # So I'm saving these dictionaries in a list as 'dict_'+filename, and loading 
   # them whenever I want to carry out more optimization runs. 
      optDict = read_object(fname + '_dict.pkl')
      for i in range(len(model.optimization_runs)):
          model.optimization_runs[i].__dict__ = optDict[i]
   ###
   hyp_old = model.param_array[:]
   model.optimize_restarts(messages=False,num_restarts=nres)
   hyp = model.param_array
   model.pickle(filename)
   optDict = []
   ### saving dictionaries of the optimization runs
   for i in range(len(model.optimization_runs)):
       optDict.append(model.optimization_runs[i].__dict__)
   saveDict(optDict,fname+'_dict.pkl')
   print 'Optimized Hyperparameters ================================================='
   for i in range(nKernels):
       print 'Var.' + str(i+1) + ' = ' + str(hyp_old[4*i]) + '   :   ' + str(hyp[4*i]) 
       print 'Lt ' + str(i+1) + '  = ' + str(hyp_old[4*i+1]) + '   :   ' + str(hyp[4*i+1])
       print 'Ly ' + str(i+1) + '  = ' + str(hyp_old[4*i+2]) + '   :   ' + str(hyp[4*i+2]) 
       print 'Lx ' + str(i+1) + '  = ' + str(hyp_old[4*i+3]) + '   :   ' + str(hyp[4*i+3])

   print 'Noise = ' + str(hyp_old[-1]) + '   :   ' + str(hyp[-1]) 
   print '==========================================================================='

   print 'End of script, time : ' + str(datetime.now()-startTime)
Esempio n. 8
0
def predictTest(filename):
   startTime = datetime.now()
   cheatPickle = GPy.load('cheatPickle.pkl')# - Stirr pickle to the right class,
                                            #       otherwise it tries to load a paramz object.
                                            # - Pickle is not working properly with GPy objects 
                                            #       saved on Pegasus 2, it only works with 
                                            #       the ones saved in my WS. 
   model_v = GPy.load(filename + '_v.pkl')
   model_u = GPy.load(filename + '_u.pkl')
   f = sio.loadmat(filename+'.mat')
   Xt = f['Xt']
   obst=f['test_points']
   vt = obst[:,0]
   ut = obst[:,1]
   Nt = ut.size
   step = Nt/10
   
   for i in range(0,Nt,step):
      if (i+step<Nt):
         Xt2 = Xt[i:i+step,:]
      elif (i<Nt):
         Xt2 = Xt[i:,:]
      V2,VVar2 = model_v.predict(Xt2)
      U2,UVar2 = model_u.predict(Xt2)
      if i==0:
         V = V2
         VVar = VVar2
         U = U2
         UVar = UVar2
      else:
         V = np.concatenate([V,V2],axis=0)
         U = np.concatenate([U,U2],axis=0)
         VVar = np.concatenate([VVar,VVar2],axis=0)
         UVar = np.concatenate([UVar,UVar2],axis=0)
      print 'step '+str(i+1)+'/'+str(10)

   print 'End of script, time : ' + str(datetime.now()-startTime)
   output = filename +'_test.mat'
   sio.savemat(output,{'Xt':Xt,'Vp':V,'VpVar':VVar,'Up':U,'UpVar':UVar,'test_points':obst})
Esempio n. 9
0
import mcmcGP
import GPy
from multiclassLikelihood_GPy import Multiclass
from ahmc import HMC, AHMC
import sys
from time import time

np.random.seed(1)
ndata = None # none for all data
vb_filename = 'mnist_467M_scg.pickle'
ahmc_num_steps = 50
ahmc_s_per_step = 20
hmc_num_samples = 300
thin = 2

data = GPy.load('mnist_pickle')
X_train, Y_train, X_test, Y_test = data['Xtrain'], data['Ytrain'], data['Xtest'], data['Ytest']
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

#scale data
X_train = X_train/255.0
X_train = X_train*2. - 1.
X_test = X_test/255.0
X_test = X_test*2. - 1.

#randomize order
i = np.random.permutation(X_train.shape[0])
i = i[:ndata]
X_train, Y_train = X_train[i,:], Y_train[i,:]
Esempio n. 10
0
def predict(filename,
            tlim=[0, 0],
            ylim=[0, 0],
            xlim=[0, 0],
            dt=0.5,
            dx=0.5,
            xL=40,
            yL=40,
            Simul=0):
    startTime = datetime.now()
    cheatPickle = GPy.load(
        'cheatPickle.pkl')  # - Stirr pickle to the right class,
    #       otherwise it tries to load a paramz object.
    # - Pickle is not working properly with GPy objects
    #       saved on Pegasus 2, it only works with
    #       the ones saved in my WS.
    model_v = GPy.load(filename + '_v.pkl')
    hypv = model_v.param_array
    hypv_names = model_v.parameter_names()

    model_u = GPy.load(filename + '_u.pkl')
    hypu = model_u.param_array
    #   hypu_names = model_u.parameter_names()

    #   with open(filename+'_v.pkl','rb') as input:
    #        model_v = pickle.load(input)
    #   with open(filename+'_u.pkl','rb') as input:
    #        model_u = pickle.load(input)

    if (ylim[0] == ylim[1]) & (xlim[0] == xlim[1]):
        f = sio.loadmat(filename + '.mat')
        Xo = f['Xo']
        print Xo[:, 0].min(), Xo[:, 0].max()
        print Xo[:, 1].min(), Xo[:, 1].max()
        print Xo[:, 2].min(), Xo[:, 2].max()

        if Simul == 1:  # get NCOM grid
            ncom = Dataset('osprein_2013_8.nc', 'r')
            ylim = np.array([Xo[:, 1].min(), Xo[:, 1].max()])
            xlim = np.array([Xo[:, 2].min(), Xo[:, 2].max()])
            lon_nc = ncom.variables['lon'][:]
            lat_nc = ncom.variables['lat'][:]
            print lon_nc.size, lat_nc.size
            ti_nc = ncom.variables['time'][1:] - ncom.variables['time'][1]
            dt = ti_nc[1]
            tp = np.arange(Xo[:, 0].min(), Xo[:, 0].max() + dt, dt)
            Lon_nc, Lat_nc = np.meshgrid(lon_nc, lat_nc)
            xnc, ync = NAD83(Lon_nc, Lat_nc)
            xnc2 = (np.reshape(xnc, [1, -1]) - x_ori) / 1000.
            ync2 = (np.reshape(ync, [1, -1]) - y_ori) / 1000.
            limit = np.where((xnc2 >= xlim[0]) & (xnc2 <= xlim[1])
                             & (ync2 >= ylim[0]) & (ync2 <= ylim[1]))
            # The next part is necessary for the netcdf grid, so that the space dimensions
            # can be defined by the model grid's lon and lat
            Lo_mx = np.reshape(Lon_nc, [
                1, -1
            ])[limit].max()  # get the lon inside the space limit that we want
            Lo_mn = np.reshape(Lon_nc, [
                1, -1
            ])[limit].min()  # get the lon inside the space limit that we want
            La_mx = np.reshape(Lat_nc, [1, -1])[limit].max()
            La_mn = np.reshape(Lat_nc, [1, -1])[limit].min()
            # make the min,max of lon and lat to define the limits of the grid, and not x and y
            limit2 = np.where((Lon_nc >= Lo_mn) & (Lon_nc <= Lo_mx)
                              & (Lat_nc >= La_mn) & (Lat_nc <= La_mx))
            xnc = (xnc[limit2][None, :] - x_ori) / 1000.
            xnc = np.repeat(xnc, tp.size, axis=0)
            xnc = np.reshape(xnc, [-1, 1])
            ync = (ync[limit2][None, :] - y_ori) / 1000.
            ync = np.repeat(ync, tp.size, axis=0)
            ync = np.reshape(ync, [-1, 1])
            tnc = np.repeat(tp[:, None], xnc.size / tp.size, axis=1)
            tnc = np.reshape(tnc, [-1, 1])
            Xp = np.concatenate([tnc, ync, xnc], axis=1)
            # xp,yp are lon,lat here!
            xp = lon_nc[np.where((lon_nc >= Lo_mn) & (lon_nc <= Lo_mx))]
            yp = lat_nc[np.where((lat_nc >= La_mn) & (lat_nc <= La_mx))]
            sio.savemat(filename + '_grid.mat', {'Xp': Xp})
        else:
            Xp, tp, yp, xp = getGrid(Xo[:, 0], Xo[:, 1], Xo[:, 2])
    else:
        Xp, tp, yp, xp = getGrid(tlim, ylim, xlim, dt, dx, xL, yL)
    inc = yp.size * xp.size
    i2 = 0
    for i in range(tp.size):
        Xp2 = Xp[i2:i2 + inc, :]
        V2, VVar2 = model_v.predict(Xp2)
        U2, UVar2 = model_u.predict(Xp2)
        if i == 0:
            V = V2
            VVar = VVar2
            U = U2
            UVar = UVar2
        else:
            V = np.concatenate([V, V2], axis=0)
            U = np.concatenate([U, U2], axis=0)
            VVar = np.concatenate([VVar, VVar2], axis=0)
            UVar = np.concatenate([UVar, UVar2], axis=0)
        i2 += inc
        print gc.collect(2)
        print 'step ' + str(i + 1) + '/' + str(tp.size)
#   V,VVar = model_v.predict(Xp)
#   U,UVar = model_u.predict(Xp)
    print 'Creating Netcdf file; running time = ' + str(datetime.now() -
                                                        startTime)
    createNC(filename + '.nc', tp, yp, xp, hypv)
    V = np.reshape(V, [tp.size, yp.size, xp.size])
    VVar = np.reshape(VVar, [tp.size, yp.size, xp.size])
    U = np.reshape(U, [tp.size, yp.size, xp.size])
    UVar = np.reshape(UVar, [tp.size, yp.size, xp.size])
    fi = Dataset(filename + '.nc', 'a')
    fi = writeNC(fi, 'v', V)
    fi = writeNC(fi, 'u', U)
    fi = writeNC(fi, 'vvar', VVar)
    fi = writeNC(fi, 'uvar', UVar)
    fi = writeNC(fi, 'hyperparam_v', hypv)
    fi = writeNC(fi, 'hyperparam_u', hypu)
    fi.close()
    print 'End of script, time : ' + str(datetime.now() - startTime)
Esempio n. 11
0
def create_bodyfat_predictor(bodyfats):
    version = 1.57

    if not bodyfats:
        return None

    days = [a.date for a in bodyfats]
    start_day = min(days)
    integer_days = _days_to_integers(days, start_day).reshape(len(days), 1)

    a = bodyfats[0]
    m = models.BodyfatEstimator
    est = m.query.filter((m.version == version) & (m.user_id == m.user_id) & (m.user_id == a.user_id)).first()

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        observation_weights = np.diag([b.measurement_type.std_err ** 2 for b in bodyfats])
        observations = np.array([[b.value] for b in bodyfats])

        if len(days) > 1:
            k_rbf = GPy.kern.RBF(1)
            k_rbf.lengthscale.set_prior(Prior.gamma(75, 500))
            k_rbf.variance.set_prior(Prior.gamma(12.5, 20))

            k_bias = GPy.kern.Bias(1)
            k_bias.variance.set_prior(Prior.gamma(10, 10))

            k_fixed = GPy.kern.Fixed(1, observation_weights)
            k_fixed.variance.set_prior(Prior.gamma(0.5, 2.5))

            kernel = k_rbf + k_fixed + k_bias

            mean = np.average([b.value for b in bodyfats], weights=[1/b.measurement_type.std_err for b in bodyfats])
            mf = GPy.mappings.Constant(1, 1, mean)

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, mean_function=mf,
                                            noise_var=1E-10, normalizer=None)
        else:
            total_precision = sum(1 / (b.measurement_type.std_err ** 2) for b in bodyfats)
            mean = sum(b.value / (b.measurement_type.std_err ** 2) for b in bodyfats) / total_precision

            k_var = (mean / 1000) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel)

        model.optimize()
        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = m(version=version, date=date.today(), model=m_pickle, user_id=a.user_id)
        models.db.session.add(est)
        models.db.session.commit()

    print(model)

    def model_predictor(d):
        try:
            return model.predict(d)
        except ValueError:
            return model.predict(d, kern=model.kern.rbf + model.kern.bias)

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - start_day).days
            days_ = tuple(start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([[i] for i in range(duration)])
        (means, variances) = model_predictor(integer_days_)
        stds = np.sqrt(variances)
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    predict.start_day = start_day

    return predict
Esempio n. 12
0
def scikit_prior(filename0,
                 varname='v',
                 dt=0,
                 tlim=6,
                 radar='',
                 xlim=[0, 0],
                 ylim=[0, 0],
                 dx=0,
                 ind=0,
                 xrange=3):
    startTime = datetime.now()
    dir0, a = filename0.split("res")
    b, fname0 = a.split("/")
    fname0 = dir0 + fname0
    fm = sio.loadmat(fname0 + '.mat')
    print 'Longitude limits:', xlim
    print 'Latitude limits :', ylim

    # get radar data grid, if that is the case:
    if radar != '':
        inFile = Dataset(radar, 'r')
        lon0, lat0 = inFile.variables['imageOriginPosition'][:]
        x0, y0 = NAD83(lon0, lat0)
        x0 = (x0 - x_ori) / 1000.  # in km
        y0 = (y0 - y_ori) / 1000.  # in km
        xg = x0 + inFile.variables['xCoords'][:] / 1000.
        yg = y0 + inFile.variables['yCoords'][:] / 1000.
        tr = inFile.variables['time'][:]
        ur = inFile.variables['ux'][:]
        vr = inFile.variables['uy'][:]
        t0 = datetime(2016, 01, 01)  # radar data counts from here, in hours
        t0D = datetime(2016, 2, 7, 2,
                       15)  # first time from Filtered_2016_2_7.pkl'
        tg = np.array([(t0 + timedelta(tr[0]) - t0D).total_seconds() / 3600])
        it = 0
        filename = filename0 + '_radar'
        Yg, Tg, Xg = np.meshgrid(yg, tg, xg)
        Tg = np.reshape(Tg, [Tg.size, 1])
        Yg = np.reshape(Yg, [Yg.size, 1])
        Xg = np.reshape(Xg, [Xg.size, 1])
        X = np.concatenate([Tg, Yg, Xg], axis=1)
    else:  # DEFINE GRID
        if (xlim[1] > xlim[0]) & (ylim[1] > ylim[0]):  # should focus here
            X, tcenter, yg, xg = getGrid([dt, dt + 1], ylim, xlim, 1, dx)
            filename = filename0 + '_cyc'
        else:  # this is for preexisting grids
            f = Dataset(filename0 + '.nc', 'r')
            #         HPU = f.variables['hyperparam_u'][:]
            #         HPV = f.variables['hyperparam_v'][:]
            xg = f.variables['x'][:]
            yg = f.variables['y'][:]
            tg = f.variables['time'][:]
            it = dt  #tg.size/2 + dt
            tcenter = np.array([tg[it]])
            Yg, Tg, Xg = np.meshgrid(yg, tg, xg)
            Tg = np.reshape(Tg, [Tg.size, 1])
            Yg = np.reshape(Yg, [Yg.size, 1])
            Xg = np.reshape(Xg, [Xg.size, 1])
            X = np.concatenate([Tg, Yg, Xg], axis=1)
            filename = filename0
            inc = yg.size * xg.size
            i2 = inc * it
            X = X[i2:i2 + inc, :]

    filename = filename + '_' + str(np.round(tcenter[0],
                                             decimals=2)) + 'h_scikit_'
    outFile = filename + str(ind) + '.nc'

    # LOAD Observations
    to = fm['Xo'][:, 0]
    tt = fm['Xt'][:, 0]
    xo = fm['Xo'][:, 2]
    xt = fm['Xt'][:, 2]
    ito = np.where((to >= tcenter - tlim) & (to <= tcenter + tlim)
                   & (xo >= xlim[0] - xrange) & (xo <= xlim[1] + xrange))
    itt = np.where((tt >= tcenter - tlim) & (tt <= tcenter + tlim)
                   & (xt >= xlim[0] - xrange) & (xt <= xlim[1] + xrange))
    Xo = fm['Xo'][ito, :].squeeze()
    Xt = fm['Xt'][itt, :].squeeze()
    XT = np.concatenate([Xo, Xt], axis=0)
    print 'Number of observation points: ', np.size(XT, 0)
    obs = fm['obs'][ito, :].squeeze()
    obst = fm['test_points'][itt, :].squeeze()
    # LOAD Hyper-Parameters
    cheatPickle = GPy.load('cheatPickle.pkl')
    model = GPy.load(fname0 + '_' + varname + '.pkl')
    HP = model.param_array
    covarname = varname + 'var'
    modelName = filename + varname + '.pkl'
    if varname == 'u':
        u = np.concatenate([obs[:, 1], obst[:, 1]])[:, None]
    else:
        u = np.concatenate([obs[:, 0], obst[:, 0]])[:, None]
    N = HP.size - 1
    noise = HP[-1]
    print 'noise = ' + str(HP[-1])
    # Build Model
    print modelName
    #   if not os.path.isfile(modelName):
    k = HP[0] * kernels.RBF(length_scale=[HP[1], HP[2], HP[3]])
    print 'var1 = ' + str(HP[0])
    if N > 5:
        i = 4
        k = k + HP[i] * kernels.RBF(
            length_scale=[HP[i + 1], HP[i + 2], HP[i + 3]])
        print 'var2 = ' + str(HP[i])
    k = k + kernels.WhiteKernel(noise_level=noise)
    print k
    model_u = GaussianProcessRegressor(kernel=k, optimizer=None)
    print np.size(XT, 0), np.size(XT, 1)
    print np.size(u, 0), np.size(u, 1)
    model_u.fit(XT, u)
    # file might be to large to save
    #      with open(modelName,'wb') as output:
    #      pickle.dump(model_u,open(modelName,'wb'))
    #   else:
    #      with open(modelName,'rb') as input:
    #           model_u = pickle.load(input)

    # REGRESSION
    U, Ustd = model_u.predict(X, return_std=True)
    U = np.reshape(U, [tcenter.size, yg.size, xg.size])
    Ustd = np.reshape(Ustd, [tcenter.size, yg.size, xg.size])
    # SAVE NETCDF
    if not os.path.isfile(outFile):
        createNC(outFile, tcenter, yg, xg, HP)
    print np.ndim(U), np.size(U, 0), np.size(U, 1)
    print np.ndim(Ustd), np.size(Ustd, 0), np.size(Ustd, 1)
    fi = Dataset(outFile, 'a')
    fi = writeNC(fi, varname, U)
    fi = writeNC(fi, covarname, Ustd**2)
    fi = writeNC(fi, 'hyperparam_' + varname, HP)
    fi.close()
    print 'End of script, time : ' + str(datetime.now() - startTime)
Esempio n. 13
0
m_mc.kern.white.variance.fix(1e-3)
pr = GPy.priors.Gamma(3, 2)
m_mc.kern.rbf.lengthscale.set_prior(pr)

m_vb.likelihood.delta.fix(1e-3)
m_mc.likelihood.delta.fix(1e-3)

m_vb.Z.fix()
m_vb.kern.fix()

#m_vb.optimize('bfgs', max_iters=vb_frozen_iters, messages=True)
#m_vb.Z.unfix()
#m_vb.kern.constrain_positive()
#m_vb.kern.white.fix(1e-3) # to keep the same as mcmc
#m_vb.optimize('bfgs', max_iters=vb_max_iters, messages=True)
m_vb = GPy.load('m_vb')

drawPredictionSpace(m_vb)
from matplotlib2tikz import save as save_tikz

save_tikz('simple_vb.tikz')

#m_mc.kern.rbf[:] = m_vb.kern.rbf[:]*1
m_mc.Z[:] = m_vb.Z[:] * 1
m_mc.Z.fix()
L = GPy.util.choleskies.flat_to_triang(m_vb.q_u_chol)
U = np.vstack(
    [np.dot(L[i, :, :], np.random.randn(m_mc.V.shape[0])) for i in range(3)]).T
U = U + m_vb.q_u_mean
K = m_mc.kern.K(m_mc.Z)
L = GPy.util.linalg.jitchol(K)
Esempio n. 14
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 20:35:17 2020

@author: andrew
"""

import GPy
GPy.tests()

import numpy as np

X = np.random.uniform(-5, 5, (20, 1))
Y = np.array([0.25 * (a * a) + (.25 * np.random.randn()) for a in X])

kernel = GPy.kern.RBF(input_dim=1)
m = GPy.models.GPRegression(X, Y, kernel)
print(m)
m.plot()
Esempio n. 15
0
def scikit_prior(filename0,varname='v',dt=0,tlim=6,radar='',xlim=[0,0],ylim=[0,0],dx=0,ind=0,xrange=3):
   startTime = datetime.now()
   dir0,a = filename0.split("res")
   b,fname0 = a.split("/") 
   fname0 = dir0 + fname0
   fm = sio.loadmat(fname0 + '.mat') 
   print 'Longitude limits:',  xlim
   print 'Latitude limits :',  ylim

   # get radar data grid, if that is the case:
   if radar!='':
      inFile = Dataset(radar, 'r')
      lon0,lat0 = inFile.variables['imageOriginPosition'][:]
      x0,y0=NAD83(lon0,lat0)
      x0 = (x0 - x_ori)/1000. # in km
      y0 = (y0 - y_ori)/1000. # in km
      xg = x0 + inFile.variables['xCoords'][:]/1000.
      yg = y0 + inFile.variables['yCoords'][:]/1000.
      tr = inFile.variables['time'][:]
      ur = inFile.variables['ux'][:]
      vr = inFile.variables['uy'][:]
      t0 = datetime(2016,01,01) # radar data counts from here, in hours
      t0D = datetime(2016, 2, 7, 2, 15) # first time from Filtered_2016_2_7.pkl'
      tg = np.array([(t0 + timedelta(tr[0]) - t0D).total_seconds()/3600])
      it=0
      filename = filename0 + '_radar'
      Yg,Tg,Xg = np.meshgrid(yg,tg,xg)
      Tg = np.reshape(Tg,[Tg.size,1])
      Yg = np.reshape(Yg,[Yg.size,1]) 
      Xg = np.reshape(Xg,[Xg.size,1])
      X = np.concatenate([Tg,Yg,Xg],axis=1)
   else:   # DEFINE GRID
      if (xlim[1]>xlim[0])&(ylim[1]>ylim[0]): # should focus here
         X,tcenter,yg,xg = getGrid([dt,dt+1],ylim,xlim,1,dx)
         filename = filename0 + '_cyc'
      else: # this is for preexisting grids
         f = Dataset(filename0 + '.nc','r')
#         HPU = f.variables['hyperparam_u'][:]
#         HPV = f.variables['hyperparam_v'][:]
         xg = f.variables['x'][:]
         yg = f.variables['y'][:]
         tg = f.variables['time'][:]
         it = dt #tg.size/2 + dt
         tcenter = np.array([tg[it]]) 
         Yg,Tg,Xg = np.meshgrid(yg,tg,xg)
         Tg = np.reshape(Tg,[Tg.size,1])
         Yg = np.reshape(Yg,[Yg.size,1]) 
         Xg = np.reshape(Xg,[Xg.size,1])
         X = np.concatenate([Tg,Yg,Xg],axis=1)
         filename = filename0
         inc = yg.size * xg.size 
         i2= inc*it
         X = X[i2:i2+inc,:]

   filename = filename +'_'+ str(np.round(tcenter[0],decimals=2))  + 'h_scikit_'
   outFile = filename +str(ind)+'.nc'

# LOAD Observations  
   to = fm['Xo'][:,0]   
   tt = fm['Xt'][:,0]   
   xo = fm['Xo'][:,2]   
   xt = fm['Xt'][:,2]   
   ito = np.where((to>=tcenter-tlim)&(to<=tcenter+tlim)&(xo>=xlim[0]-xrange)&(xo<=xlim[1]+xrange))
   itt = np.where((tt>=tcenter-tlim)&(tt<=tcenter+tlim)&(xt>=xlim[0]-xrange)&(xt<=xlim[1]+xrange))
   Xo = fm['Xo'][ito,:].squeeze()
   Xt = fm['Xt'][itt,:].squeeze()
   XT = np.concatenate([Xo,Xt],axis=0)
   print 'Number of observation points: ',np.size(XT,0)
   obs = fm['obs'][ito,:].squeeze()   
   obst = fm['test_points'][itt,:].squeeze()   
# LOAD Hyper-Parameters
   cheatPickle = GPy.load('cheatPickle.pkl')
   model = GPy.load(fname0 +'_'+varname+'.pkl')
   HP = model.param_array
   covarname = varname + 'var'
   modelName = filename + varname + '.pkl'
   if varname=='u':
      u = np.concatenate([obs[:,1],obst[:,1]])[:,None]
   else:
      u = np.concatenate([obs[:,0],obst[:,0]])[:,None]
   N = HP.size - 1
   noise = HP[-1]
   print 'noise = ' + str(HP[-1])
# Build Model
   print modelName   
#   if not os.path.isfile(modelName):
   k = HP[0]* kernels.RBF(length_scale=[HP[1],HP[2],HP[3]])
   print 'var1 = '+str(HP[0])
   if N > 5:
      i=4
      k = k + HP[i]* kernels.RBF(length_scale=[HP[i+1],HP[i+2],HP[i+3]])
      print 'var2 = ' + str(HP[i])    
   k  = k + kernels.WhiteKernel(noise_level=noise)
   print k
   model_u = GaussianProcessRegressor(kernel=k,optimizer=None)
   print np.size(XT,0),np.size(XT,1)
   print np.size(u,0), np.size(u,1)
   model_u.fit(XT,u)
# file might be to large to save
#      with open(modelName,'wb') as output:
#      pickle.dump(model_u,open(modelName,'wb'))
#   else:
#      with open(modelName,'rb') as input:
#           model_u = pickle.load(input)
 
# REGRESSION   
   U,Ustd = model_u.predict(X,return_std=True)
   U = np.reshape(U,[tcenter.size,yg.size,xg.size])
   Ustd = np.reshape(Ustd,[tcenter.size,yg.size,xg.size])
# SAVE NETCDF
   if not os.path.isfile(outFile):
      createNC(outFile,tcenter,yg,xg,HP)
   print np.ndim(U),np.size(U,0),np.size(U,1)
   print np.ndim(Ustd),np.size(Ustd,0),np.size(Ustd,1)
   fi = Dataset(outFile,'a')
   fi = writeNC(fi,varname,U) 
   fi = writeNC(fi,covarname,Ustd**2)
   fi = writeNC(fi,'hyperparam_'+varname,HP)
   fi.close() 
   print 'End of script, time : ' + str(datetime.now()-startTime)
Esempio n. 16
0
def predict(filename,tlim=[0,0],ylim=[0,0],xlim=[0,0],dt=0.5,dx=0.5,xL=40,yL=40,Simul=0):
   startTime = datetime.now()
   cheatPickle = GPy.load('cheatPickle.pkl')# - Stirr pickle to the right class,
                                            #       otherwise it tries to load a paramz object.
                                            # - Pickle is not working properly with GPy objects 
                                            #       saved on Pegasus 2, it only works with 
                                            #       the ones saved in my WS. 
   model_v = GPy.load(filename + '_v.pkl')
   hypv = model_v.param_array
   hypv_names = model_v.parameter_names()

   model_u = GPy.load(filename + '_u.pkl')
   hypu = model_u.param_array
#   hypu_names = model_u.parameter_names()

#   with open(filename+'_v.pkl','rb') as input:
#        model_v = pickle.load(input)
#   with open(filename+'_u.pkl','rb') as input:
#        model_u = pickle.load(input)
      

   if (ylim[0] == ylim[1])&(xlim[0] == xlim[1]):
      f = sio.loadmat(filename+'.mat')
      Xo = f['Xo']
      print Xo[:,0].min() ,Xo[:,0].max() 
      print Xo[:,1].min() ,Xo[:,1].max() 
      print Xo[:,2].min() ,Xo[:,2].max() 

      if Simul==1: # get NCOM grid
         ncom = Dataset( 'osprein_2013_8.nc', 'r')
         ylim = np.array([Xo[:,1].min() ,Xo[:,1].max()])
         xlim = np.array([Xo[:,2].min() ,Xo[:,2].max()])
         lon_nc = ncom.variables['lon'][:]
         lat_nc = ncom.variables['lat'][:]
         print lon_nc.size,lat_nc.size
         ti_nc = ncom.variables['time'][1:]-ncom.variables['time'][1]
         dt = ti_nc[1]
         tp = np.arange(Xo[:,0].min(),Xo[:,0].max()+dt,dt)
         Lon_nc,Lat_nc = np.meshgrid(lon_nc,lat_nc)
         xnc,ync=NAD83(Lon_nc,Lat_nc)
         xnc2 = (np.reshape(xnc,[1,-1])-x_ori)/1000.
         ync2 = (np.reshape(ync,[1,-1])-y_ori)/1000.
         limit = np.where((xnc2>=xlim[0])&(xnc2<=xlim[1])&(ync2>=ylim[0])&(ync2<=ylim[1]))
         # The next part is necessary for the netcdf grid, so that the space dimensions 
         # can be defined by the model grid's lon and lat 
         Lo_mx = np.reshape(Lon_nc,[1,-1])[limit].max() # get the lon inside the space limit that we want
         Lo_mn = np.reshape(Lon_nc,[1,-1])[limit].min() # get the lon inside the space limit that we want
         La_mx = np.reshape(Lat_nc,[1,-1])[limit].max()
         La_mn = np.reshape(Lat_nc,[1,-1])[limit].min()
         # make the min,max of lon and lat to define the limits of the grid, and not x and y
         limit2 = np.where((Lon_nc>=Lo_mn)&(Lon_nc<=Lo_mx)&(Lat_nc>=La_mn)&(Lat_nc<=La_mx))
         xnc = (xnc[limit2][None,:]-x_ori)/1000. 
         xnc = np.repeat(xnc,tp.size,axis=0)
         xnc = np.reshape(xnc,[-1,1])
         ync = (ync[limit2][None,:]-y_ori)/1000. 
         ync = np.repeat(ync,tp.size,axis=0)
         ync = np.reshape(ync,[-1,1])
         tnc = np.repeat(tp[:,None],xnc.size/tp.size,axis=1)         
         tnc = np.reshape(tnc,[-1,1])
         Xp = np.concatenate([tnc,ync,xnc],axis=1)
         # xp,yp are lon,lat here!
         xp = lon_nc[np.where((lon_nc>=Lo_mn)&(lon_nc<=Lo_mx))]
         yp = lat_nc[np.where((lat_nc>=La_mn)&(lat_nc<=La_mx))]
         sio.savemat(filename+'_grid.mat',{'Xp':Xp})
      else:
         Xp,tp,yp,xp = getGrid(Xo[:,0],Xo[:,1],Xo[:,2])
   else:
      Xp,tp,yp,xp = getGrid(tlim,ylim,xlim,dt,dx,xL,yL)
   inc = yp.size*xp.size
   i2=0
   for i in range(tp.size): 
      Xp2 = Xp[i2:i2+inc,:]
      V2,VVar2 = model_v.predict(Xp2)
      U2,UVar2 = model_u.predict(Xp2)
      if i==0:
         V = V2
         VVar = VVar2
         U = U2
         UVar = UVar2
      else:
         V = np.concatenate([V,V2],axis=0)
         U = np.concatenate([U,U2],axis=0)
         VVar = np.concatenate([VVar,VVar2],axis=0)
         UVar = np.concatenate([UVar,UVar2],axis=0)
      i2+=inc
      print gc.collect(2)
      print 'step '+str(i+1)+'/'+str(tp.size)
#   V,VVar = model_v.predict(Xp)
#   U,UVar = model_u.predict(Xp)
   print 'Creating Netcdf file; running time = ' + str(datetime.now()-startTime)
   createNC(filename+'.nc',tp,yp,xp,hypv)
   V = np.reshape(V,[tp.size,yp.size,xp.size])
   VVar = np.reshape(VVar,[tp.size,yp.size,xp.size])
   U = np.reshape(U,[tp.size,yp.size,xp.size])
   UVar = np.reshape(UVar,[tp.size,yp.size,xp.size])
   fi = Dataset(filename+'.nc','a')
   fi = writeNC(fi,'v',V)
   fi = writeNC(fi,'u',U)
   fi = writeNC(fi,'vvar',VVar)
   fi = writeNC(fi,'uvar',UVar)
   fi = writeNC(fi,'hyperparam_v',hypv)
   fi = writeNC(fi,'hyperparam_u',hypu)
   fi.close()
   print 'End of script, time : ' + str(datetime.now()-startTime)
Esempio n. 17
0
from time import time

np.random.seed(0)
reduced_dim = 45
ndata = None  # none for all data
num_inducing = 500
ahmc_num_steps = 50
ahmc_s_per_step = 20
hmc_num_samples = 300
vb_frozen_iters = 8000
vb_max_iters = 200000
vb_batchsize = 1000
step_rates = 1e-1, 5e-2
thin = 2

data = GPy.load('mnist_pickle')
X_train, Y_train, X_test, Y_test = data['Xtrain'], data['Ytrain'], data[
    'Xtest'], data['Ytest']
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

#normalize dta
#Xmean, Xstd = X_train.mean(0), X_train.std(0)
#Xstd = np.where(Xstd==0, 1,Xstd)
#X_train = (X_train-Xmean)/Xstd
#X_test = (X_test-Xmean)/Xstd

#scale data
X_train = X_train / 255.0
X_train = X_train * 2. - 1.
X_test = X_test / 255.0
Esempio n. 18
0
def create_weight_predictor(user_id):
    version = 1.24

    m = models.WeightEstimator
    est = m.query.filter((m.version == version) & (m.user_id == user_id)).first()

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        weights = models.WeightMeasurement.query.filter(models.WeightMeasurement.user_id == user_id).all()

        if not weights:
            return None

        days = [w.date for w in weights]

        w = weights[0]

        observations = np.array([[w.value] for w in weights])
        start_day = min(days)
        integer_days = _days_to_integers(days, start_day).reshape(len(days), 1)

        if len(days) > 1:
            k_rbf = GPy.kern.RBF(1)
            k_rbf.lengthscale.set_prior(Prior.gamma(100, 1000))
            k_bias = GPy.kern.Bias(1)
            kernel = k_rbf + k_bias
            noise_var = (observations.mean() * 0.0325) ** 2
            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, noise_var=noise_var, normalizer=None)
        else:
            k_var = (w.value / 1000) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)
            mf = GPy.mappings.Constant(1, 1, w.value)
            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, mean_function=mf)

        model.optimize()
        model.start_day = start_day

        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = m(version=version, date=date.today(), model=m_pickle, user_id=w.user_id)
        models.db.session.add(est)
        models.db.session.commit()

    print(model)

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, model.start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - model.start_day).days
            days_ = tuple(model.start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([[i] for i in range(duration)])
        (means, variances) = model.predict(integer_days_)
        stds = np.sqrt(variances)
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    return predict
Esempio n. 19
0
import mcmcGP
import GPy
from multiclassLikelihood_GPy import Multiclass
from ahmc import HMC, AHMC
import sys
from time import time

np.random.seed(1)
ndata = None  # none for all data
vb_filename = 'mnist_467M_scg.pickle'
ahmc_num_steps = 50
ahmc_s_per_step = 20
hmc_num_samples = 300
thin = 2

data = GPy.load('mnist_pickle')
X_train, Y_train, X_test, Y_test = data['Xtrain'], data['Ytrain'], data[
    'Xtest'], data['Ytest']
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

#scale data
X_train = X_train / 255.0
X_train = X_train * 2. - 1.
X_test = X_test / 255.0
X_test = X_test * 2. - 1.

#randomize order
i = np.random.permutation(X_train.shape[0])
i = i[:ndata]
X_train, Y_train = X_train[i, :], Y_train[i, :]
Esempio n. 20
0
from time import time

np.random.seed(0)
reduced_dim = 45
ndata = None  # none for all data
num_inducing = 500
ahmc_num_steps = 50
ahmc_s_per_step = 20
hmc_num_samples = 300
vb_frozen_iters = 8000
vb_max_iters = 200000
vb_batchsize = 1000
step_rates = 1e-1, 5e-2
thin = 2

data = GPy.load("mnist_pickle")
X_train, Y_train, X_test, Y_test = data["Xtrain"], data["Ytrain"], data["Xtest"], data["Ytest"]
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

# normalize dta
# Xmean, Xstd = X_train.mean(0), X_train.std(0)
# Xstd = np.where(Xstd==0, 1,Xstd)
# X_train = (X_train-Xmean)/Xstd
# X_test = (X_test-Xmean)/Xstd

# scale data
X_train = X_train / 255.0
X_train = X_train * 2.0 - 1.0
X_test = X_test / 255.0
X_test = X_test * 2.0 - 1.0
Esempio n. 21
0
def create_1rm_predictor(exercise=None, user_id=None):
    version = 1.23

    em = models.StrengthEstimator
    am = models.StrengthAchievement

    est = em.query.filter((em.version == version) & (em.exercise == exercise) & (em.user_id == user_id)).first()

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        achievements = am.query.filter((am.user_id == user_id) & (am.exercise == exercise)).all()
        if not achievements:
            return None
        a = achievements[0]
        days = [a.date for a in achievements]
        start_day = min(days)
        observation_weights = np.diag([0 if a.repetitions == 1 else strength_fixed_variance(a.weight, a.repetitions)
                                       for a in achievements])
        observations = np.array([[a.weight] if a.repetitions == 1 else [predict_1rm(a.weight, a.repetitions)]
                                 for a in achievements])
        integer_days = _days_to_integers(days, start_day).reshape(len(days), 1)

        if len(days) > 1:
            k_rbf = GPy.kern.RBF(1)
            k_rbf.lengthscale.set_prior(Prior.gamma(200, 500))
            k_rbf.variance.set_prior(Prior.gamma(250, 10000))

            k_fixed = GPy.kern.Fixed(1, observation_weights, variance=0.1)
            k_fixed.variance.set_prior(Prior.gamma(2.5, 2.5))

            k_bias = GPy.kern.Bias(1)

            kernel = k_rbf * k_bias + k_fixed
            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel)
            model.Gaussian_noise.variance.set_prior(Prior.gamma(1, 5))

        else:
            noise_var = strength_fixed_variance(a.weight, a.repetitions)

            k_var = (predict_1rm(a.weight, a.repetitions) / 1500) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)

            mf = GPy.mappings.Constant(1, 1, a.weight)

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, noise_var=noise_var,
                                            mean_function=mf)

        model.optimize()
        model.start_day = start_day
        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = em(version=version, date=date.today(), exercise=a.exercise, model=m_pickle, user_id=a.user_id)
        models.db.session.add(est)
        models.db.session.commit()

    def model_predictor(d):
        try:
            return model.predict(d)
        except ValueError:
            return model.predict(d, kern=model.kern.mul.rbf * model.kern.mul.bias)

    print(model)

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, model.start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - model.start_day).days
            days_ = tuple(model.start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([[i] for i in range(duration)])
        (means, variances) = model_predictor(integer_days_)
        stds = np.sqrt(variances)
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    return predict
Esempio n. 22
0
def create_intake_predictor(user, min_date=None):
    version = 1.04
    m = models.IntakeEstimator
    est = m.query.filter((m.version == version) & (m.user_id == user.id)).first()

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        intakes = models.get_intakes(user.id)

        if not intakes:
            return None

        date_intakes = {i.date: i.total_calories for i in intakes}

        if not min_date:
            min_date = user.join_date
        max_date = date.today()

        dp = models.DietPeriod
        diet_periods = dp.query.filter(dp.user_id == user.id).all()

        for i in range((max_date - min_date).days):
            day = min_date + timedelta(days=i)
            for period in diet_periods:
                if (period.start_date or min_date) <= day <= (period.end_date or max_date):
                    if period.example_date in date_intakes and day not in date_intakes:
                        date_intakes[day] = date_intakes[period.example_date]
                    break

        days = list(date_intakes.keys())
        calories = np.array([[date_intakes[d]] for d in days])

        start_day = min(days)
        integer_days = _days_to_integers(days, start_day).reshape(len(days), 1)

        if len(days) > 1:
            k_exp = GPy.kern.Exponential(1)
            k_exp.lengthscale.set_prior(Prior.gamma(1, 10))
            k_bias = GPy.kern.Bias(1)
            kernel = k_exp + k_bias
            model = GPy.models.GPRegression(integer_days, calories, kernel=kernel, normalizer=None)
        else:
            c = calories[0]
            k_var = (c / 1000) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)
            mf = GPy.mappings.Constant(1, 1, c)
            model = GPy.models.GPRegression(integer_days, calories, kernel=kernel, mean_function=mf)

        model.optimize()
        print(model)
        model.start_day = start_day

        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = m(version=version, date=date.today(), model=m_pickle, user_id=user.id)
        models.db.session.add(est)
        models.db.session.commit()

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, model.start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - model.start_day).days
            days_ = tuple(model.start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([[i] for i in range(duration)])
        (means, variances) = model.predict(integer_days_)
        stds = np.sqrt(np.abs(variances))
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    return predict
Esempio n. 23
0
def create_endurance_predictor(achievements, fixed="distance", value=None):
    version = 1

    if not achievements:
        return None

    a = achievements[0]
    m = models.EnduranceEstimator
    where = (m.version == version) & \
            (m.exercise == a.exercise) & \
            (m.fixed == fixed) & \
            (m.user_id == a.user_id)
    if fixed == "time":
        where &= m.time == value
    else:
        where &= (m.distance == a.distance) & (m.distance_type_id == a.distance_type_id)

    est = m.query.filter(where).first()

    days = [w.date for w in achievements]
    start_day = min(days)

    if est:
        with BytesIO(est.model) as f:
            model = GPy.load(f)
    else:
        observations = np.array([[e.foot_second_pace] for e in achievements])
        integer_days = _days_to_integers(days, start_day)

        if fixed == "distance":
            distances = np.array([e.foot_distance for e in achievements])
            # The distances are rescaled here so that a single lengthscale works across the entire input space
            x = np.power(distances, 1 / 5)
            if not value:
                average_distance = distances.mean()
                possible_distances = exercise_distances.get(achievements[0].exercise, [5280])
                value = min(possible_distances, key=lambda d: abs(d - average_distance))
        else:
            times = np.array([e.time for e in achievements])
            # The times are rescaled here so that a single lengthscale works across the entire input space
            x = np.power(times, 1 / 5)
            if not value:
                value = np.median(times)

        inputs = np.column_stack((integer_days, x))

        if len(days) > 1:
            k_rbf_date = GPy.kern.RBF(1, active_dims=0)
            k_rbf_date.lengthscale.set_prior(Prior.gamma(300, 200))

            k_rbf_distance = GPy.kern.RBF(1, active_dims=1)
            k_rbf_distance.lengthscale.set_prior(Prior.gamma(0.5, 0.1))

            kernel = k_rbf_date * k_rbf_distance

            model = GPy.models.GPRegression(inputs, observations, kernel=kernel)
        else:
            e = achievements[0]

            k_var = (e.value / 3000) ** 2
            kernel = GPy.kern.Linear(1, variances=k_var)

            mf = GPy.mappings.Constant(1, 1, e.value)

            model = GPy.models.GPRegression(integer_days, observations, kernel=kernel, mean_function=mf)

        model.optimize()
        with BytesIO() as f:
            model.pickle(f, protocol=3)
            f.seek(0)
            m_pickle = f.read()
        est = m(version=version, date=date.today(), exercise=a.exercise, fixed=fixed, model=m_pickle, user_id=a.user_id)
        if fixed == "time":
            est.time = a.time
        else:
            est.distance = a.distance
            est.distance_type_id = a.distance_type_id
        models.db.session.add(est)
        models.db.session.commit()

    def predict(days_=tuple()):
        if days_:
            integer_days_ = _days_to_integers(days_, start_day).reshape(len(days_), 1)
        else:
            duration = (date.today() + timedelta(days=30) - start_day).days
            days_ = tuple(start_day + timedelta(days=1 * i) for i in range(duration))
            integer_days_ = np.array([i for i in range(duration)])
        x_ = np.power(np.array([value] * len(integer_days_)), 1 / 5)
        inputs_ = np.column_stack((integer_days_, x_))
        (means, variances) = model.predict(inputs_)
        stds = np.sqrt(variances)
        return tuple(means.flatten()), tuple(stds.flatten()), tuple(days_)

    predict.start_day = start_day

    return predict
Esempio n. 24
0
    def __init__(self, model_file, max_points, timer_freq, dim1, dim2,
                 resolution, manual):
        # create joint angle commands and corresponding publishers
        self.left_command = JointCommand(names=LEFT_NAMES,
                                         mode=JointCommand.POSITION_MODE)
        self.right_command = JointCommand(names=RIGHT_NAMES,
                                          mode=JointCommand.POSITION_MODE)

        self.left_pub = rospy.Publisher('/robot/limb/left/joint_command',
                                        JointCommand,
                                        tcp_nodelay=True,
                                        queue_size=1)
        self.right_pub = rospy.Publisher('/robot/limb/right/joint_command',
                                         JointCommand,
                                         tcp_nodelay=True,
                                         queue_size=1)

        # load mrd model from pickle file
        self.mrd_model = GPy.load(model_file)

        mrd_X = self.mrd_model.X.mean
        self.mrd_point_count = mrd_X.shape[0]
        if self.mrd_point_count > max_points:
            rospy.logwarn('Mean contains more samples. Shape: (%d, %d)' %
                          mrd_X.shape)
            downsample_indices = np.random.choice(self.mrd_point_count,
                                                  size=max_points,
                                                  replace=False)
            mrd_X = mrd_X[downsample_indices]

        # parameters for doing latent function inference
        self.q_dim = mrd_X.shape[1]
        self.latent_X = np.zeros((1, self.q_dim))

        self.dim1 = dim1
        self.dim2 = dim2
        self.resolution = resolution

        self.mrd_X = mrd_X[:, [self.dim1, self.dim2]]

        title = 'Baxter Whill Movement using MRD'
        fig, (self.ax1, self.ax2) = plt.subplots(nrows=1,
                                                 ncols=2,
                                                 figsize=(10, 4))
        self.plot_latent_space()
        self.plot_whill_movement()
        fig.canvas.set_window_title(title)
        self.text_handle = self.ax1.text(0.8,
                                         0.1,
                                         'Play Mode: OFF',
                                         horizontalalignment='center',
                                         verticalalignment='center',
                                         transform=self.ax1.transAxes,
                                         bbox={
                                             'facecolor': 'green',
                                             'alpha': 0.5,
                                             'pad': 6
                                         })

        self.counter = 0
        self.whill_move_handle = None
        self.latent_cursor_handle = None

        if manual:
            fig.suptitle(
                'Predicted Whill movements are not sent to the Whill controller',
                fontstyle='italic',
                color='red')
            # variables for mouse cursor based motion
            self.mouse_xy = np.zeros((1, 2))
            self.start_motion = False
            self.cursor_color = 'red'

            # connect the cursor class
            fig.canvas.mpl_connect('button_press_event', self.mouse_click)
            fig.canvas.mpl_connect('motion_notify_event', self.mouse_move)
            fig.subplots_adjust(top=0.80)
        else:
            self.whill_move = None
            self.init_ros_whillpy()
            self.cursor_color = 'green'
            self.text_handle.set_text('Automatic Mode: ON')

            # create a timer to follow the mean trajectory
            self.ros_timer = rospy.Timer(rospy.Duration(1 / timer_freq),
                                         self.timer_callback)

        # adjust the space at the bottom
        fig.subplots_adjust(bottom=0.15)
Esempio n. 25
0
m_mc.kern.white.variance.fix(1e-3)
pr = GPy.priors.Gamma(3,2)
m_mc.kern.rbf.lengthscale.set_prior(pr)

m_vb.likelihood.delta.fix(1e-3)
m_mc.likelihood.delta.fix(1e-3)

m_vb.Z.fix()
m_vb.kern.fix()

#m_vb.optimize('bfgs', max_iters=vb_frozen_iters, messages=True)
#m_vb.Z.unfix()
#m_vb.kern.constrain_positive()
#m_vb.kern.white.fix(1e-3) # to keep the same as mcmc
#m_vb.optimize('bfgs', max_iters=vb_max_iters, messages=True)
m_vb = GPy.load('m_vb')

drawPredictionSpace( m_vb )
from matplotlib2tikz import save as save_tikz
save_tikz('simple_vb.tikz')

#m_mc.kern.rbf[:] = m_vb.kern.rbf[:]*1
m_mc.Z[:] = m_vb.Z[:]*1
m_mc.Z.fix()
L = GPy.util.choleskies.flat_to_triang(m_vb.q_u_chol)
U = np.vstack([np.dot(L[i,:,:], np.random.randn(m_mc.V.shape[0])) for i in range(3)]).T
U = U + m_vb.q_u_mean
K = m_mc.kern.K(m_mc.Z)
L = GPy.util.linalg.jitchol(K)
m_mc.V[:] = GPy.util.linalg.dtrtrs(L, U)[0]