'''
Created on Jan 31, 2018

@author: anon
'''

from hawkes_networks.poisson_processes.datatypes import GaussianPoissonProcess
from pathos.multiprocessing import ProcessingPool as Pool, ProcessingPool

#=================================================
# PARALLEL
#=================================================


def get_attribute(GPP, parameters):
    return getattr(GPP, 'inference_worker')(parameters)

    
def PARALLEL_INFERENCE(arrivals, gpp_parameters, inference_parameters, read_data):
    # PARALLEL STUFF
    number_of_workers = inference_parameters["number_of_workers"]
    
    # GENERATE ALL THE DIFFERENT OBJECTS
    OBJECTS_LIST = ProcessingPool(processes=number_of_workers).map(GaussianPoissonProcess, [(gpp_parameters, not read_data, i) for i in range(number_of_workers)])
    PARAMETERS_LIST = [(arrivals, inference_parameters) for i in range(number_of_workers)]
    
    # PARALLELIZATION
    RESULTS = ProcessingPool(processes=number_of_workers).map(get_attribute, OBJECTS_LIST, PARAMETERS_LIST)
    
    # SELECT AMONG THE MODELS
    BOUNDS = [(result["final_bound"], result["process_id"]) for result in RESULTS]
    BOUNDS.sort()
    
    best_model = BOUNDS[0][1]
    print BOUNDS 
    for result in RESULTS:
        if result["process_id"] == best_model:
            final_SL = result["process_id"] 
            final_w = result["final_w"]
            final_m = result["final_m"]
    best_parameters = (final_m, final_SL, final_w)
    return RESULTS, best_parameters

"""
#z = np.linspace(-3000.,0.,100)
#zfine = np.linspace(-3000.,0.,1000)
#hypergeometric_derivative_interpolation = interp1d(z, y, kind='cubic')
#y = np.asarray([(mpmath.hyp1f1(dt, 0.5, zx) - mpmath.hyp1f1(0., 0.5, zx))/dt  for zx in z])
        
#ax0.tick_params(axis='both', which='major', labelsize=14)
#ax1.tick_params(axis='both', which='minor', labelsize=14)
#ax2.tick_params(axis='both', which='minor', labelsize=14)
        
        
Y = []
for y in np.linspace(0.1,0.9, 30):
    print y
    bound_statistics  = []
    for a in range(10):

        SL = self.KL + np.tril(np.random.uniform(0.,y,(self.number_of_inducing_points,self.number_of_inducing_points)))           
        x0 = self.parameters_to_vector(m,SL)
        bound_statistics.append(self.bound(x0))    
    Y.append(np.asarray(bound_statistics).mean())
plt.plot(Y)
plt.show()
"""
