import km
import sample_datasets_n
from sklearn import datasets
import numpy as np


def dataloading(filename, AllTorOneF, NoValids):
  malwareset, benignset, malwarenames, benignnames, featNos = sample_datasets_n.load(filename, AllTorOneF)
  print(len(malwareset))
  print(len(benignset))
  #malwareset, benignset = sample_datasets.normTwoSets(malwareset, benignset)
  if NoValids ==False:
    mXYtrain, mXYtest, mYtrain, mYtest, bXYtrain, bXYtest, bYtrain, bYtest= sample_datasets_n.divideTwoSets(malwareset, benignset, 0.16)
  else:
    mXYtrain, mXYtest, mYtrain, mYtest, bXYtrain, bXYtest, bYtrain, bYtest= sample_datasets_n.divideTwoSets(malwareset, benignset, 0.0)#2)
  #concatenate malwareSet/benignSet
  #shuffle instances in the concatenated data set
  #final training/test set seperation
  numColumn = np.shape(mXYtrain)[1]
  mbXY_train = np.concatenate((mXYtrain[:,:],bXYtrain[:,:]))
  mbXY_test = np.concatenate((mXYtest[:,:],bXYtest[:,:]))
  np.random.shuffle(mbXY_train)
  np.random.shuffle(mbXY_test)


  X_train = mbXY_train[:,0:numColumn-1]
  X_test = mbXY_test[:,0:numColumn-1]
  Y_train = mbXY_train[:,numColumn-1]
  Y_test = mbXY_test[:,numColumn-1]
  N_train = mbXY_train[:numColumn]
  
  return X_train, X_test, Y_train, Y_test, featNos, N_train

#filename = "/media/ktg/New Volume/AndroidMalPaper/Arff/functionOP_.arff"
#filename = "/media/ktg/New Volume/AndroidMalPaper/Arff_malgenome(3000)/string_.arff"
#filename = "/media/ktg/New Volume/AndroidMalPaper/Arff_malgenome(3000)/manifest_.arff"
#filename = "/media/ktg/New Volume/AndroidMalPaper/Arff_malgenome(3000)/methodOP_.arff"
#filename = "/media/ktg/New Volume/AndroidMalPaper/Arff_malgenome(3000)/methodAPI_.arff"
filename = "./Arff/manifest_.arff"
#manifest_string_methodOP_methodAPI_functionOP_.arff"
X_train, X_test, Y_train, Y_test, featNos, N_train = dataloading(filename, True, True)
X_total = X_train
Y_total = Y_train

print(Y_total)

mapper = km.KeplerMapper(verbose=1)

# Fit to and transform the data
projected_data = mapper.fit_transform(X_total, projection="dist_mean") # X-Y axis
#projected_data = mapper.fit_transform(X_total, projection="knn_distance_2") # X-Y axis

# Create dictionary called 'graph' with nodes, edges and meta-information

complex = mapper.map(projected_X=projected_data, inverse_X=X_total, 
					 clusterer=km.cluster.DBSCAN(eps=0.5, min_samples=3), 
					 nr_cubes=3000, overlap_perc=0.5)


# Visualize it

mv = mapper.visualize(complex, path_html=filename[:-5]+"_vis.html", 
				 title=filename[:-5],
				 custom_tooltips=Y_total, color_function="average_signal_cluster",
				 graph_gravity=0.5, graph_link_distance=20, graph_charge=-80)
print(mv)

