-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnr_map.py
188 lines (92 loc) · 3.3 KB
/
snr_map.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
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 8 18:56:46 2025
@author: chiliaeva
# Signal-to-noise ratio maps
"""
import math
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
from snr_functions import compute_snr
from matplotlib_scalebar.scalebar import ScaleBar
len_fov = 5 # size of FOV in mm
#%% Get the files
savefig_map = False
save_histogram = False
root = 'C:/Users/chiliaeva/Documents/Resultats_traitement/'
type_reco = 'had_reco'
num_patient = 'P61_'
num_biopsy = 'B8_'
root_saveresults = root + 'fitresults_241119_full_spectra/'
file_spectrum_tab = root_saveresults + num_patient + type_reco + '/' + num_biopsy + type_reco + '_' + 'spectrum_tab.npy'
spectrum_tab = np.load(file_spectrum_tab)
root_savefig = root_saveresults + 'fig/snr/'
if os.path.exists(root_savefig) == False :
os.mkdir(root_savefig)
root_savefig_hist = root_savefig + 'hist/'
if os.path.exists(root_savefig_hist) == False :
os.mkdir(root_savefig_hist)
file_wvlgth = root_saveresults + 'wavelengths_mask_606-616.npy'
wavelengths = np.load(file_wvlgth)
#%% Spatial loop
std_bounds = [650, 748]
max_interval = [620, 640]
nb_map = np.empty_like(spectrum_tab[:,:,0])
std_map = np.empty_like(spectrum_tab[:,:,0])
snr_map = np.empty_like(spectrum_tab[:,:,0])
integral = np.empty_like(spectrum_tab[:,:,0])
for i in range(spectrum_tab.shape[0]):
for j in range(spectrum_tab.shape[1]):
nb_map[i, j], std_map[i, j], snr_map[i, j], integral[i,j] = compute_snr(spectrum_tab, wavelengths, i, j, std_bounds, max_interval)
#%% Plots
scalebar = ScaleBar(len_fov/(np.shape(spectrum_tab)[0]), "mm")
plt.figure('STD')
plt.clf()
plt.title('STD map')
plt.imshow(std_map)
plt.colorbar()
ax1 = plt.gca()
ax1.add_artist(scalebar)
if savefig_map == True :
plt.savefig(root_savefig + num_patient + num_biopsy + type_reco + '_std_map.png', bbox_inches='tight')
scalebar = ScaleBar(len_fov/(np.shape(spectrum_tab)[0]), "mm")
plt.figure('SNR')
plt.clf()
plt.title('SNR map')
plt.imshow(snr_map)
plt.colorbar()
ax2 = plt.gca()
ax2.add_artist(scalebar)
if savefig_map == True :
plt.savefig(root_savefig + num_patient + num_biopsy + type_reco + '_snr_map.png', bbox_inches='tight')
##############################################################################################################
#%% Statistics
# Histogram
bins = 50
plt.figure('Histogram ')
plt.clf()
plt.title('Number of pixels in the image with a given SNR value')
plt.hist(snr_map.flatten(), bins)
# plt.hist(snr_map, bins, range=(range_min, range_max))
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel('SNR', fontsize=16)
plt.ylabel('Number of pixels', fontsize=16)
if save_histogram == True :
plt.savefig(root_savefig_hist + num_patient + num_biopsy + type_reco + '_snr_histogram.png', bbox_inches='tight')
#%%
# Number of pixels with SNR >= 10 :
def nb_values_over_n(array, n):
nb = 0
for i in range(len(array)):
if array[i] >= n :
nb += 1
return nb
flat = snr_map.flatten()
nb_non_nan = 0
for i in range(len(flat)):
if math.isnan(flat[i]) == False :
nb_non_nan += 1
nb_pts_snr_over_ten = nb_values_over_n(flat, 10)