import sys
sys.path.insert(1, './GR_BASS/BASS_only_original/')
sys.path.insert(1, './GR_BASS/')
import bass as md
import numpy as np
import sys
import bassLibrary as bl

# BASS algorithm parameters
eps  =  0.1
p_d  =  0.2
Jthr = 0.15
seed = 0

# Creating synthetic data for process BASS on and to learn the GMM model
nbClasses          = 5
classNames         = ['a', 'b', 'c', 'd', 'e']
nbInstDataAnalyze  = 4000
probElemDictAppear = 0.05
[dataToAnalyze1, dataForLearn] = bl.createSyntheticDataSet(nbClasses, nbInstDataAnalyze, [[3, 2, 1, 0], [0, 1, 2, 3]], [probElemDictAppear, probElemDictAppear])
l = int(len(dataToAnalyze1)/4)
lengths_data1 = np.array([l, l, l, l])

# Learning the model with the data previously created
model_fit = md.GMM_model(nbClasses)
model_fit.solve(dataForLearn)

# Launch BASS on the synthetic data previously created
posteriorProb1 = bl.getPosteriorProbabilities(dataToAnalyze1, lengths_data1, model_fit)
[P_w1, nbInstances1, w_dict1] = bl.launchBASS(posteriorProb1, lengths_data1, model_fit, eps, p_d, Jthr, seed)
[transmat_, stationary_probs_, a, b, c] = bl.launchMarkovianCompare(posteriorProb1, lengths_data1, model_fit, eps, p_d, Jthr, seed, w_dict1, classNames, 0, {'nameOfFile' : 'syntheticDataTest'})


# Comparing different dataset with different amounts of insertions

for idx, probElemDictAppear2 in enumerate([0.1, 0.05]):

  print("Comparing two different dataset with SAME amounts of insertions. Probability: ", probElemDictAppear2)
  
  [dataToAnalyze2, dataForLearn2] = bl.createSyntheticDataSet(nbClasses, nbInstDataAnalyze, [[3, 2, 1, 0], [0, 1, 2, 3]], [probElemDictAppear2, probElemDictAppear2])
  l = int(len(dataToAnalyze2)/4)
  lengths_data2 = np.array([l, l, l, l])

  posteriorProb2 = bl.getPosteriorProbabilities(dataToAnalyze2, lengths_data2, model_fit)
  [P_w2, nbInstances2, w_dict2] = bl.launchBASS(posteriorProb2, lengths_data2, model_fit, eps, p_d, Jthr, seed)

  w_thr = 1e-4
  p_ins = 0.2
  mu = 1.0
  H_beta_fac = 0
  Sigma = dataToAnalyze1.shape[1]
  std = 0.05
  params = np.array([eps,p_d,p_ins, mu, w_thr,H_beta_fac, Jthr, Sigma, std], dtype =float)

  bl.compareTwoBASSresults(w_dict1, w_dict2, params, model_fit, dataToAnalyze1, lengths_data1, dataToAnalyze2, lengths_data2, {'nameOfFile' : 'syntheticDataTest'}, classNames, str(idx)) # TODO: change compareTwoBASSresults for it to accept the posterior probabilities posteriorProb1 and posteriorProb2 instead of the data dataToAnalyze1 and dataToAnalyze2

