"""
add streamorder/src in the system path with relative path
"""

import sys
import scipy
import cvxpy
import os
from reldg import *
import seaborn as sns
from sklearn.linear_model import LinearRegression
import random
from readdata import to_undirected_2
from estimator_stat import generate_Z, generate_Y, estimators, compute_p

import networkx as nx
import numpy as np
import time

## define these parameters
## parameters will be given in the gen_combinations.sh script 
myN = myN_
my_p_rw = my_p_rw_
mylambda1 = mylambda1_
mylambda2 = mylambda2_
myepsilon = myepsilon_

myd = 10 # degree parameter
myS = 10 # number of partitions
myM = 1000 # number of permutation replicates
myB = 500 # bootstrap sample size
N = myN # number of nodes
d = myd  # degree parameter
num_shards = S = myS # number of clusters
p_rw = my_p_rw
lambda1, lambda2 = mylambda1, mylambda2

M = myM
num_iterations = 20
epsilon = myepsilon

out_file = 'expr_' + str(mylambda1) + '_' + str(mylambda2) + '_' + str(myN) + '_' + str(p_rw) + '_' + str(epsilon)
neuralcrowd_file = open(out_file, 'a')

# within the loop is one bootstrap
for b in range(myB):
    # generate graph G with node size N, degree parameter d, rewiring probability p_rw
    G = nx.watts_strogatz_graph(N, d, p_rw)
    # get graph details for graph clustering 
    neighbors_map, num_nodes, node_indices, edges = to_undirected_2(G, shuffle=False)
    # partition graph by reldg (using opensource streamorder)
    reldgEdgeFracs, _, reldgPeriodicity, shardMap = reldg(num_nodes,
                                            edges,
                                            node_indices,
                                            neighbors_map,
                                            num_shards,
                                            num_iterations,
                                            epsilon,
                                            return_periodicity=True,
                                            version = 'random')
    # assign treatment/control group at cluster level    
    Zc = generate_Z(S)
    # generate response values
    Y = generate_Y(-1.5, lambda1, lambda2, G, shardMap, Zc, T=3)

    # compute p-value at individual level and at cluste level
    p_naive_i, p_neighbor_i, p_lm_i, p_naive_c, p_neighbor_c, p_lm_c = compute_p(G, shardMap, Y, Zc, S, M=M)
    neuralcrowd_file.writelines('\t'.join([str(p_naive_i), str(p_neighbor_i), str(p_lm_i), str(p_naive_c), str(p_neighbor_c), str(p_lm_c)]))
    neuralcrowd_file.writelines('\n')

neuralcrowd_file.close()
