# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: py_37_env
#     language: python
#     name: py_37_env
# ---

# # Lecture  des données et import du model

# +
import innvestigate
import numpy as np
from keras.models import load_model
model = load_model('model.h5')
import pickle

train_x = pickle.load(open('train_x.pickle', "rb"))
test_x = pickle.load(open('test_x.pickle', "rb"))
test_y = pickle.load(open('test_y.pickle', "rb"))

print(test_x.shape)
print(test_y.shape)
tests = np.append(test_x, test_y, axis=1)
print(tests)
print(tests.shape)
tests_sorted_by_class = tests[np.lexsort((tests[:,-6], tests[:,-5], tests[:,-4], tests[:,-3], tests[:,-2], tests[:,-1]))]

# -

# # Séparation du dataset par ses classes

# +
classes_index = {}
for i in range(1, 7):
    #print(i)
    #print(np.argwhere(tests_sorted_by_class[-i]>0))
    classes_index[str(6-i)]=np.argwhere(tests_sorted_by_class[-i]==1)

classes = {}
classes['0'] = []
classes['1'] = []
classes['2'] = []
classes['3'] = []
classes['4'] = []
classes['5'] = []
for i in tests_sorted_by_class:
    classes[str(list(i[-6:]).index(1))].append(i[:-6])
print(classes.keys())
for i in classes:
    print("Len "+str(i)+" = "+str(len(classes[i])))


# +

methods = [
    ("lrp.z"),
    ]

analyzers = {}
method = methods[0]
print(methods)
print("Analyzer for Method : "+str(method))    
analyzer = innvestigate.create_analyzer(method, model)
analyzer.fit(train_x, batch_size=256, verbose=1)

# -

# # Analyse de chaque classe avec LRP et sauvegarde des résultats

# +
import os

classes_name = ["0" ,"1", "2", "3", "4", "5"]
analysis_results = {}
for i in classes_name:
    print("Analyzing for class "+str(i))
    tmp = []
    x = np.array(classes[i])
    analysis_results[i] = analyzer.analyze(x)
    print("analysis results shape : "+str(analysis_results[i].shape))
means = {}
for i in classes_name:
    print("class "+str(classes_name[int(i)]))
    mat = analysis_results[i]
    mean = mat.mean(0)
    means[i] = mean
# -

# # Import des analyses et représentation en HeatMap

# +
plots = []
sorted_plots = []
inverse_sort = []
classes_equivalents = ["ALL", "AML", "CLL", "CML", "MDS", "Non-leukemia"]
for i in means:
    plots.append(means[i])
toplot = np.array(plots)
print("toplot shape : "+str(toplot.shape))
print(str(toplot))
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
print("SHAPE : "+str(toplot.shape))

# We use ax parameter to tell seaborn which subplot to use for this plot
f,ax = plt.subplots(1,sharey=True)
f.set_figheight(20)
f.set_figwidth(25)

g1 = sns.heatmap(toplot,cmap="PuBuGn",ax=ax)
g1.set(yticklabels = classes_equivalents)
plt.yticks(fontsize=20, rotation=0)
g1.set_ylabel('')
g1.set_xlabel('Importance des variables')
# -

# # ACP et Clustering Hiéarchique

# +
from sklearn.preprocessing import StandardScaler
import numpy as np
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.cluster.hierarchy import dendrogram, linkage


import scipy.spatial as spatial
from sklearn.decomposition import PCA
classes_equivalents = ["ALL", "AML", "CLL", "CML", "MDS", "Non-leukemia"]

for i in range(6):
    print("Class "+str(classes_equivalents[i]))
    sc = StandardScaler()
    mat = analysis_results[str(i)]
    Z = sc.fit_transform(mat)
    acp = PCA(svd_solver='full', n_components=10)

    x_pca = acp.fit_transform(Z)
    plt.figure(figsize=(8,6))
    sns.clustermap(mat[0])
    plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered