-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
166 lines (129 loc) · 5.4 KB
/
algorithms.py
File metadata and controls
166 lines (129 loc) · 5.4 KB
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
import time
import numpy as np
from skimage import io
from scipy.io import loadmat
try:
from ALP4 import *
from seabreeze.spectrometers import Spectrometer
except:
print('It is not possible to run in real-time')
class Algorithm:
'''
Base class for all algorithms
'''
def reconstruct_sample(self, data):
'''
Reconstructs the data
'''
codes = data['codes']
measurements = data['measurements']
wavelengths = data['wavelengths']
num_shots = data['num_shots']
M = data['M']
N = M
x_hat = 0
x_plot = 0
for i in range(num_shots):
time_start = time.time()
CodeTomar = codes[i]
CodeTomar = CodeTomar.astype(np.float32)
measurement = measurements[i]
intensity_sum = measurement.mean()
H = CodeTomar * 2 - 1
x_hat += H * intensity_sum * (1 / M * N)
x_plot = np.copy(x_hat)
x_plot -= (x_plot.min() + 1e-8)
x_plot /= (x_plot.max() + 1e-8)
x_plot = (x_plot * 255).astype(np.uint8)
time_elapsed = (time.time() - time_start)
yield i, dict(x_plot=x_plot, codes=codes, H=H, measurement=measurement,
wavelengths=wavelengths, time_elapsed=time_elapsed)
yield dict(codes=codes, x_plot=x_plot, measurements=measurements, wavelengths=wavelengths)
def reconstruct_sample_real_time(self, data):
num_shots = data['num_shots']
M = data['M']
integration_time = data['integration_time']
N = M
# devices
DMD = ALP4(version='4.3')
DMD.Initialize() # Initialize the device
# variables
pp = io.imread("data/dmd_template.bmp")
pp = pp > 128
pp_flat = pp.ravel()
codes = loadmat('data/coded_apertures.mat')['coded_apertures']
CodeTomar = codes[10, :, :]
CodeTomar = CodeTomar.astype(np.float32)
Size = CodeTomar.shape
Factor = np.array(512 / Size[1])
Factor = Factor.astype(np.int8)
Px = 400 - Factor * M / 2
Px2 = 400 + Factor * M / 2 - 1
Py = 640 - Factor * N / 2
Py2 = 640 + Factor * N / 2 - 1
assert codes.shape[
0] >= num_shots, "Not enough coded apertures for the selected number of shots, reduce compression ratio"
# start lecture
measurements = np.zeros((M * M, 512))
x_hat = 0
for i in range(num_shots):
time_start = time.time()
CodeTomar = codes[i]
CodeTomar = CodeTomar.astype(np.float32)
CodeKron = np.kron(CodeTomar,np.ones((Factor, Factor)))
Base = np.zeros((800,1280))
# Falta llenar la base con el código de apertura
Base[Px.astype(np.int16) - 1:Px2.astype(np.int16),
Py.astype(np.int16) - 1:Py2.astype(np.int16)] = CodeKron
Base = 255 * Base.astype(np.uint8)
imgSeq = Base.ravel()
# Binary amplitude image (0 or 1)
bitDepth = 1
measurement = 0
vals = [1, -1]
for val in vals:
# Allocate the onboard memory for the image sequence
# nbImg me dice cuantos codigos quiero poner en secuencia!
DMD.SeqAlloc(nbImg=1, bitDepth=bitDepth)
# Send the image sequence as a 1D list/array/numpy array
if val == -1:
code = 255 - imgSeq
else:
code = imgSeq
DMD.SeqPut(imgData=code * pp_flat)
# Set image rate to 50 Hz
DMD.SetTiming(pictureTime=4000) # 3000 picture rate [fps] = 1 000 000 / ALP_PICTURE_TIME [µs]
DMD.Run()
time.sleep(0.009) # 0.09
# CODE PARA ADQUIRIR CON EL ESPECTROMETRO
number = Spectrometer.from_serial_number()
number.integration_time_micros(integration_time) # 30000
wavelengths = number.wavelengths()
intensityX = number.intensities() #-NEGRO
intensityX = intensityX.astype(np.float64) / 64000
# print(np.max(intensityX))
time.sleep(0.015) # 0.09
## CLOSE ESPECTROMETROS
Spectrometer.close(number)
# Save
intensidad_nir = np.array(intensityX)
intensidad_nir = intensidad_nir
measurement += intensidad_nir * val * (1)
intensity_sum = measurement[125:235].mean()
measurements[i] = measurement
H = CodeTomar * 2 - 1
x_hat += H * intensity_sum * (1 / M * N)
x_plot = np.copy(x_hat)
x_plot -= (x_plot.min() + 1e-8)
x_plot /= (x_plot.max() + 1e-8)
x_plot = (x_plot * 255).astype(np.uint8)
time_elapsed = (time.time() - time_start)
yield i, dict(x_plot=x_plot, codes=codes, H=H, measurement=measurement,
wavelengths=wavelengths, time_elapsed=time_elapsed, DMD=DMD)
# Stop the sequence display
DMD.Halt()
# Free the sequence from the onboard memory
DMD.FreeSeq()
# De-allocate the device
DMD.Free()
yield dict(codes=codes, x_plot=x_plot, measurements=measurements, wavelengths=wavelengths, num_shots=num_shots, M=M)