-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLIBStick_comp_spectres.py
213 lines (181 loc) · 8.87 KB
/
LIBStick_comp_spectres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 2 11:31:54 2020
Module outils pour les mesures, la comparaison et la classification des spectres
@author: yannick
"""
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#import mpl_toolkits.mplot3d as plt3d
import LIBStick_outils
limites_zone1 = [534.5, 535.8]
limites_zone2 = [528.0, 543.0]
limites_spectre = [528.0, 543.0]
###################################################################################################
# fonctions création tableaux et dataframe
###################################################################################################
def creer_tableau_avec_x(liste, type_fichier, type_traitement): # données en colonnes
"""
Crée tableau de tous les spectres du répertoire en colonne, normalisés ou non,
avec les lambda en colonne 1
"""
if type_traitement == 0: # On ne normalise pas les spectres
tableau = LIBStick_outils.creer_tableau_avec_x_colonne1(liste, type_fichier)
if type_traitement == 1: # On normalise tous les spectres
tableau = LIBStick_outils.creer_tableau_avec_x_colonne1(liste, type_fichier)
tableau = LIBStick_outils.normalise_tableau_x_aire(tableau)
# print(tableau.shape)
return tableau
def creer_dataframe_resultats(dataframe_comparatif, lim_zone1, lim_zone2, flag_denominateur):
"""
Crée un dataframe avec les résultats des mesures d'aire sous chaque spectre
entre chaque bornes des zones 1 et 2 (option) et leur rapport (option)
"""
if flag_denominateur == 1:
dataframe_tableau_calculs = pd.DataFrame()
sous_dataframe = dataframe_comparatif.loc[:, lim_zone1[0]:lim_zone1[1]]
dataframe_tableau_calculs["Somme zone 1"] = sous_dataframe.sum(axis=1)
sous_dataframe = dataframe_comparatif.loc[:, lim_zone2[0]:lim_zone2[1]]
dataframe_tableau_calculs["Somme zone 2"] = sous_dataframe.sum(axis=1)
dataframe_tableau_calculs["Rapport"] = dataframe_tableau_calculs["Somme zone 1"] / \
dataframe_tableau_calculs["Somme zone 2"]
if flag_denominateur == 0:
dataframe_tableau_calculs = pd.DataFrame()
sous_dataframe = dataframe_comparatif.loc[:, lim_zone1[0]:lim_zone1[1]]
dataframe_tableau_calculs["Somme zone 1"] = sous_dataframe.sum(axis=1)
return dataframe_tableau_calculs
def convertir_dataframe_resultats_tableau(dataframe_comparatif):
"""
Convertit le dataframe_comparatif en tableau et supprime la dernière colonne
contenant la mesure ayant servi au classement
"""
tableau = dataframe_comparatif.values
tableau = np.delete(tableau, -1, axis=1)
# print(tableau.shape)
return tableau
def enregistre_dataframe_resultats(dataframe_resultats):
"""
Enregistre les résultats classés des mesures
aux formats xlsx et tsv (extension .txt)
"""
# dataframe_resultats.to_csv("Resultat_fichiers_classes.csv")
dataframe_resultats.to_excel("Resultat_fichiers_classes.xlsx")
dataframe_resultats.to_csv("Resultat_fichiers_classes.txt", sep='\t', decimal=",")
###################################################################################################
# fonctions d'affichage graphique du tableau de résultats
###################################################################################################
def tableau_256gris(tableau_norm):
"""
Convertit les valeurs du tableau des spectres classés entre 0 et 255
"""
tableau8bits = tableau_norm*255
tableau8bits = tableau8bits.astype(int)
return tableau8bits
def graphique_creation(tableau8bits, nom_echantillon, lim_spectre):
"""
Affiche le tableau de n spectres classés en 256 niveau de gris
avec la LUT Inferno dans une fenêtre matplotlib.pyplot 2D
et la sauvegarde sous figure_plot.png
"""
# fig=plt.figure()
fig, ax = plt.subplots()
# plt.imshow(tableau8bits, cmap="gray", extent=[lim_spectre[0],lim_spectre[1],t
# ableau8bits.shape[0],0], aspect="auto")
plt.imshow(tableau8bits, cmap="inferno", extent=[
lim_spectre[0], lim_spectre[1], tableau8bits.shape[0], 0], aspect="auto")
#imageplot=plt.imshow(tableau8bits, cmap="hot")
#imageplot=plt.imshow(tableau8bits, cmap="nipy_spectral")
# plt.colorbar()
plt.title(nom_echantillon)
plt.xlabel("Longueur d'onde (nm)")
plt.ylabel("Spectres echantillons classés")
plt.yticks(range(0, tableau8bits.shape[0], 5))
ax.yaxis.get_ticklocs(minor=True)
ax.minorticks_on()
ax.xaxis.set_tick_params(which='minor', bottom=False)
plt.savefig("figure_plot.png")
#plt.xlim(lim_spectre[0], lim_spectre[1])
# plt.ioff()
plt.show(block=False)
# def graphique_3D_creation(tableau8bits,nom_echantillon,lim_spectre):
def graphique_3D_creation(tableau8bits, nom_echantillon):
"""
Affiche le tableau de n spectres classés en 256 niveau de gris
avec la LUT Inferno dans une fenêtre matplotlib.pyplot 3D
"""
xx, yy = np.mgrid[0:tableau8bits.shape[0], 0:tableau8bits.shape[1]]
#fig = plt.figure(figsize=(15,15))
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(xx, yy, tableau8bits, rstride=1, cstride=1,
cmap="inferno", linewidth=0, antialiased=False)
ax.view_init(80, 30)
plt.title(nom_echantillon)
plt.ylabel("Longueur d'onde (nm)")
plt.xlabel("Spectres suivant z")
# ax.set_ylim(lim_spectre[0],lim_spectre[1])
plt.xticks(range(0, tableau8bits.shape[0], 5))
ax.xaxis.get_ticklocs(minor=True)
ax.minorticks_on()
ax.yaxis.set_tick_params(which='minor', bottom=False)
plt.show(block=False)
def graphique_sauvegarde(tableau8bits):
"""
Saugegarde le tableau de n spectres classés en 256 niveau de gris
avec la LUT Inferno sous figure.png
"""
plt.imsave("figure.png", tableau8bits, cmap="inferno")
# def graphique_lit_tableau():
# tableau_comparatif = plt.imread("figure.png")
# return tableau_comparatif
###################################################################################################
# programme principal
###################################################################################################
def main(rep_travail, liste_fichiers, type_fichier, tableau_bornes,
type_traitement, flag_denominateur, flag_2D, flag_3D):
"""
Fonction principale du module
Crée un tableau_comparatif avec tous les spectres en colonne et les lambda en colonne 0
Crée un dataframe_comparatif (spectres en ligne) à partir du tableau précédent
Crée un dataframe_resultats de résultats des mesures
Ajoute une colonne au dataframe_comparatif avec une des mesures du dataframe_resultats
Trie par ordre croissant ces deux dataframes à l'aide de cette mesure
Enregistre dataframe_resultats
Remplace tableau_comparatif par les valeurs du dataframe_comparatif des spectres
classés par mesures croissantes et l'affiche sous forme d'image qu'on sauvegarde
Retourne le dataframe_resultats classé par ordre de mesure croissante
"""
os.chdir(rep_travail)
tableau_comparatif = creer_tableau_avec_x(liste_fichiers, type_fichier, type_traitement)
dataframe_comparatif = LIBStick_outils.creer_dataframe_x_tableau_en_colonnes(tableau_comparatif,
liste_fichiers)
limites_zone1[0] = tableau_bornes[0, 0]
limites_zone1[1] = tableau_bornes[0, 1]
limites_zone2[0] = tableau_bornes[1, 0]
limites_zone2[1] = tableau_bornes[1, 1]
dataframe_resultats = creer_dataframe_resultats(
dataframe_comparatif, limites_zone1, limites_zone2, flag_denominateur)
if flag_denominateur == 1:
dataframe_comparatif = pd.concat(
[dataframe_comparatif, dataframe_resultats["Rapport"]], axis=1)
dataframe_comparatif = dataframe_comparatif.sort_values(by=["Rapport"])
dataframe_resultats = dataframe_resultats.sort_values(by=["Rapport"])
enregistre_dataframe_resultats(dataframe_resultats)
if flag_denominateur == 0:
dataframe_comparatif = pd.concat(
[dataframe_comparatif, dataframe_resultats["Somme zone 1"]], axis=1)
dataframe_comparatif = dataframe_comparatif.sort_values(by=["Somme zone 1"])
dataframe_resultats = dataframe_resultats.sort_values(by=["Somme zone 1"])
enregistre_dataframe_resultats(dataframe_resultats)
tableau_comparatif = convertir_dataframe_resultats_tableau(dataframe_comparatif)
tableau8bits = tableau_256gris(tableau_comparatif)
if flag_2D:
graphique_creation(tableau8bits, "Echantillons classés", limites_spectre)
if flag_3D:
# graphique_3D_creation(tableau8bits, "Echantillons classés", limites_spectre)
graphique_3D_creation(tableau8bits, "Echantillons classés")
graphique_sauvegarde(tableau8bits)
return dataframe_resultats