# coding: utf-8

# In[1]:

import numpy as np
from pymongo import MongoClient

from rnpp.poisson_processes.datatypes import GaussianPoissonProcess
from rnpp.utils.etc.parameters import data_params

# Set to Data Base

client = MongoClient("129.26.78.40:27017")
db = client["PointProcesses"]
# clean database
collection = db['TrainingData']
collection.remove({})

# Parameters

lenght_scale = data_params['length_scale']
T = data_params['T']
N = data_params['N']

read_data = False
simulate_function = True
simulate_arrivals = True
inducing_points = np.linspace(0., T, N)
gpp_parameters = {"intensity_kernel": {"type_of_kernel": "squared_exponential",
                                       "kernel_parameters": [1., 2., lenght_scale], },
                  "inducing_points": list(inducing_points),
                  "inducing_function": None,
                  "T": T,
                  "inference": "variational"}

# GPP

simulate_function = True
GPP = GaussianPoissonProcess((gpp_parameters, simulate_function, 0))
gpp_parameters["inducing_function"] = list(GPP.inducing_function)

# arrivals = GPP.thinning(T)
# GPP.plot_intensity_and_arrivals(arrivals, False)

support_tuple = (0., T, 500)
WHERE_TO_EVALUATE_INTENSITY = np.linspace(*support_tuple)
INTENSITY = GPP.__intensity(WHERE_TO_EVALUATE_INTENSITY)

# sample arrivals and store to database

for i in range(300):
    arrivals = GPP.thinning(T)
    ROW_JSON = {"__intensity": INTENSITY.tolist(),
                "support": support_tuple,
                "arrivals": arrivals.tolist(),
                "model_type": "GaussianPoisson",
                "model_parameters": gpp_parameters,
                "simulate_parameters": None,
                "data_params": data_params}
    db["TrainingData"].insert_one(ROW_JSON)

import pylab as pl

pl.plot(INTENSITY)
pl.show()
