-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing_1meas.py
198 lines (87 loc) · 4.6 KB
/
preprocessing_1meas.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 8 10:39:39 2025
@author: chiliaeva
Preprocessing program for just 1 measurement (no loop)
"""
import os
import numpy as np
import cv2 as cv
from scipy import interpolate
from spas.metadata2 import read_metadata
#%%
threshold_ = 4e5 # nb counts/pixel on background for t_i = 1s, for a 16x16 image # threshold for binary masks
type_reco = 'had_reco' # 'had_reco' or 'nn_reco'
type_reco_npz = type_reco + '.npz'
if type_reco == 'nn_reco':
threshold_ = threshold_/4
root = 'D:/'
root_data = root + 'd/' # all the patient folders were saved in the root_data directory
root_ref = root + 'ref/' # the reference spectra are in the root_ref directory
# 'C:/d/P64/obj_biopsy-10-intern-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/obj_biopsy-10-intern-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1_had_reco.npz'
# 'C:/d/P64/obj_biopsy-11-singular-portion_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/obj_biopsy-11-singular-portion_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1_had_reco.npz'
# 'C:/d/P64/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1_had_reco.npz'
# 'C:/d/P65/obj_biopsy-10-posterior-limit_source_white_LED_f80mm-P2_Walsh_im_32x32_ti_10ms_zoom_x1/obj_biopsy-10-posterior-limit_source_white_LED_f80mm-P2_Walsh_im_32x32_ti_10ms_zoom_x1_had_reco.npz'
subpath = 'C:/d/P64/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/'
file_cube_white = 'C:/d/P64/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1_' + type_reco_npz
file_metadata = 'C:/d/P64/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1/obj_biopsy-12-anter-limit_source_white_LED_f80mm-P2_Walsh_im_16x16_ti_100ms_zoom_x1_metadata.json'
metadata, acquisition_params, spectrometer_params, dmd_params = read_metadata(file_metadata)
wavelengths = acquisition_params.wavelengths
t_i = spectrometer_params.integration_time_ms
# Read hypercube laser
cubeobj = np.load(file_cube_white)
cubehyper = cubeobj['arr_0']
threshold = threshold_ *t_i*1e-3/(np.shape(cubehyper)[0]*np.shape(cubehyper)[1]/(16**2)) # absolute threshold
greyscale_img = np.sum(cubehyper, axis=2) # sum the hypercube over all the spectral channels
mask = cv.threshold(greyscale_img, threshold, 1, cv.THRESH_BINARY) # thresholding function
if os.path.isfile(subpath + type_reco + '_mask.npy') == False :
np.save(subpath + type_reco + '_mask.npy', mask[1])
print("mask saved")
else :
print("mask already exists")
#%%
'''
#%% REFERENCE SPECTRA
# TODO: put this part in a separate file
file_name_ppix620 = 'ref620_3lamda.npy'
file_name_ppix634 = 'ref634_3lamda.npy'
file_name_lambda = 'Lambda.npy'
ppix620 = np.load(root_ref + file_name_ppix620)
ppix634 = np.load(root_ref + file_name_ppix634)
lambd = np.load(root_ref + file_name_lambda)
spectr634 = ppix634[0, :] # use 0th line only
spectr634[0] = 0 # otherwise kernel dies
spectr620 = ppix620[0, :]
spectr620[0] = 0
del ppix620
del ppix634
# Normalize the reference spectra
spectr620_norm = spectr620/np.amax(spectr620)
spectr620 = spectr620_norm
del spectr620_norm
spectr634_norm = spectr634/np.amax(spectr634)
spectr634 = spectr634_norm
del spectr634_norm
crop_start = np.digitize(wavelengths[0], lambd, right=True) # crop the ref spectra, keep the part from wavelengths[0] to wavelengths[-1]
crop_stop = np.digitize(wavelengths[-1], lambd, right=True)
lambd_crop = lambd[crop_start:crop_stop]
spectr620_crop = spectr620[crop_start:crop_stop]
spectr634_crop = spectr634[crop_start:crop_stop]
lambd = lambd_crop
spectr620 = spectr620_crop
spectr634 = spectr634_crop
del lambd_crop
del spectr620_crop
del spectr634_crop
# Interpolate the reference spectra
func620 = interpolate.make_interp_spline(lambd, spectr620) # interp1d is legacy
func634 = interpolate.make_interp_spline(lambd, spectr634)
spectr620_interp = func620(wavelengths) # import wavelengths from metadata
spectr634_interp = func634(wavelengths)
# save in ref folder :
np.save(root_ref + '_spectr620_interp.npy', spectr620_interp)
np.save(root_ref + '_spectr634_interp.npy', spectr634_interp)
#%% DEFINE FIT FUNCTION
def func_fit(x, a1, a2, a3, shift620, shift634, lambd_c, sigma):
return a1*func620(x-shift620) + a2*func634(x-shift634) + a3*np.exp(-(lambd_c-x)**2/sigma**2)
'''