daplis 0.9.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
LinoSPAD2/__init__.py ADDED
File without changes
File without changes
@@ -0,0 +1,256 @@
1
+ """Module for computing timestamp differences.
2
+
3
+ Compares all timestamps from the same cycle for the given pair of pixels
4
+ against a given value (delta_window). Differences in that window are saved
5
+ and returned as a list.
6
+
7
+ This file can also be imported as a module and contains the following
8
+ functions:
9
+
10
+ TODO remove
11
+ * calculate_differences_2212 - calculate timestamp differences for
12
+ the given pair of pixels. Works only with firmware version '2212'.
13
+
14
+ * calculate_differences_2212_fast - calculate timestamp differences for
15
+ the given pair of pixels. Works only with firmware version '2212'.
16
+ Uses a faster algorithm than the function above.
17
+
18
+ """
19
+
20
+ from typing import List
21
+ from warnings import warn
22
+
23
+ import numpy as np
24
+ import pandas as pd
25
+ from numpy import ndarray
26
+
27
+ from LinoSPAD2.functions import utils
28
+
29
+
30
+ def calculate_differences_2212(
31
+ data: List[float],
32
+ pixels: List[int] | List[List[int]],
33
+ pix_coor,
34
+ delta_window: float = 50e3,
35
+ ):
36
+ """Calculate timestamp differences for firmware version 2212.
37
+
38
+ Calculate timestamp differences for the given pixels and LinoSPAD2
39
+ firmware version 2212.
40
+
41
+ Parameters
42
+ ----------
43
+ data : list of array-like
44
+ List of data arrays, each corresponding to a different TDC.
45
+ pixels : List[int] | List[List[int]]
46
+ List of pixel numbers for which the timestamp differences should
47
+ be calculated or list of two lists with pixel numbers for peak
48
+ vs. peak calculations.
49
+ pix_coor : array-like
50
+ Array for transforming the pixel address in terms of TDC (0 to 3)
51
+ to pixel number in terms of half of the sensor (0 to 255).
52
+ delta_window : float, optional
53
+ Width of the time window for counting timestamp differences.
54
+ The default is 50e3 (50 ns).
55
+
56
+ Returns
57
+ -------
58
+ deltas_all : dict
59
+ Dictionary containing timestamp differences for each pair of pixels.
60
+
61
+ """
62
+
63
+ # TODO: remove
64
+ warn(
65
+ "This function is deprecated. Use" "'calculate_differences_2212_fast'",
66
+ DeprecationWarning,
67
+ stacklevel=2,
68
+ )
69
+ # Dictionary for the timestamp differences, where keys are the
70
+ # pixel numbers of the requested pairs
71
+ deltas_all = {}
72
+
73
+ pixels_left, pixels_right = utils.pixel_list_transform(pixels)
74
+
75
+ # Find ends of cycles
76
+ cycle_ends = np.argwhere(data[0].T[0] == -2)
77
+ cycle_ends = np.insert(cycle_ends, 0, 0)
78
+
79
+ for q in pixels_left:
80
+ # First pixel in the pair
81
+ tdc1, pix_c1 = np.argwhere(pix_coor == q)[0]
82
+ pix1 = np.where(data[tdc1].T[0] == pix_c1)[0]
83
+ for w in pixels_right:
84
+ if w <= q:
85
+ continue
86
+ deltas_all[f"{q},{w}"] = []
87
+
88
+ # Second pixel in the pair
89
+ tdc2, pix_c2 = np.argwhere(pix_coor == w)[0]
90
+ pix2 = np.where(data[tdc2].T[0] == pix_c2)[0]
91
+
92
+ # Go over cycles, getting data for the appropriate cycle
93
+ # only
94
+ for cyc in range(len(cycle_ends) - 1):
95
+ slice_from = cycle_ends[cyc]
96
+ slice_to = cycle_ends[cyc + 1]
97
+ pix1_slice = pix1[(pix1 >= slice_from) & (pix1 < slice_to)]
98
+ if not np.any(pix1_slice):
99
+ continue
100
+ pix2_slice = pix2[(pix2 >= slice_from) & (pix2 < slice_to)]
101
+ if not np.any(pix2_slice):
102
+ continue
103
+
104
+ # Calculate delta t
105
+ tmsp1 = data[tdc1].T[1][pix1_slice]
106
+ tmsp1 = tmsp1[tmsp1 > 0]
107
+ tmsp2 = data[tdc2].T[1][pix2_slice]
108
+ tmsp2 = tmsp2[tmsp2 > 0]
109
+ for t1 in tmsp1:
110
+ deltas = tmsp2 - t1
111
+ ind = np.where(np.abs(deltas) < delta_window)[0]
112
+ deltas_all[f"{q},{w}"].extend(deltas[ind])
113
+
114
+ return deltas_all
115
+
116
+
117
+ def calculate_differences_2212_fast(
118
+ data: ndarray,
119
+ pixels: List[int] | List[List[int]],
120
+ pix_coor: ndarray,
121
+ delta_window: float = 50e3,
122
+ cycle_length: float = 4e9,
123
+ ):
124
+ """Calculate timestamp differences for firmware version 2212.
125
+
126
+ Calculate timestamp differences for the given pixels and LinoSPAD2
127
+ firmware version 2212.
128
+
129
+ Parameters
130
+ ----------
131
+ data : ndarray
132
+ Matrix of timestamps, where rows correspond to the TDCs.
133
+ pixels : List[int] | List[List[int]]
134
+ List of pixel numbers for which the timestamp differences should
135
+ be calculated or list of two lists with pixel numbers for peak
136
+ vs. peak calculations.
137
+ pix_coor : ndarray
138
+ Array for transforming the pixel address in terms of TDC (0 to 3)
139
+ to pixel number in terms of half of the sensor (0 to 255).
140
+ delta_window : float, optional
141
+ Width of the time window for counting timestamp differences.
142
+ The default is 50e3 (50 ns).
143
+ cycle_length : float, optional
144
+ Length of each acquisition cycle. The default is 4e9 (4 ms).
145
+
146
+ Returns
147
+ -------
148
+ deltas_all : dict
149
+ Dictionary containing timestamp differences for each pair of pixels.
150
+
151
+ """
152
+
153
+ # Dictionary for the timestamp differences, where keys are the
154
+ # pixel numbers of the requested pairs
155
+ deltas_all = {}
156
+
157
+ pixels_left, pixels_right = utils.pixel_list_transform(pixels)
158
+
159
+ # Find ends of cycles
160
+ cycle_ends = np.argwhere(data[0].T[0] == -2)
161
+ cycle_ends = np.insert(cycle_ends, 0, 0)
162
+
163
+ for q in pixels_left:
164
+ # First pixel in the pair
165
+ tdc1, pix_c1 = np.argwhere(pix_coor == q)[0]
166
+ pix1 = np.where(data[tdc1].T[0] == pix_c1)[0]
167
+ for w in pixels_right:
168
+ if w <= q:
169
+ continue
170
+ deltas_all[f"{q},{w}"] = []
171
+
172
+ timestamps_1 = []
173
+ timestamps_2 = []
174
+
175
+ # Second pixel in the pair
176
+ tdc2, pix_c2 = np.argwhere(pix_coor == w)[0]
177
+ pix2 = np.where(data[tdc2].T[0] == pix_c2)[0]
178
+
179
+ # Go over cycles, shifting the timestamps from each next
180
+ # cycle by lengths of cycles before (e.g., for the 4th cycle
181
+ # add 12 ms)
182
+ for i, _ in enumerate(cycle_ends[:-1]):
183
+ slice_from = cycle_ends[i]
184
+ slice_to = cycle_ends[i + 1]
185
+ pix1_slice = pix1[(pix1 >= slice_from) & (pix1 < slice_to)]
186
+ if not np.any(pix1_slice):
187
+ continue
188
+ pix2_slice = pix2[(pix2 >= slice_from) & (pix2 < slice_to)]
189
+ if not np.any(pix2_slice):
190
+ continue
191
+
192
+ # Shift timestamps by cycle length
193
+ tmsp1 = data[tdc1].T[1][pix1_slice]
194
+ tmsp1 = tmsp1[tmsp1 > 0]
195
+ tmsp1 = tmsp1 + cycle_length * i
196
+
197
+ tmsp2 = data[tdc2].T[1][pix2_slice]
198
+ tmsp2 = tmsp2[tmsp2 > 0]
199
+ tmsp2 = tmsp2 + cycle_length * i
200
+
201
+ timestamps_1.extend(tmsp1)
202
+ timestamps_2.extend(tmsp2)
203
+
204
+ timestamps_1 = np.array(timestamps_1)
205
+ timestamps_2 = np.array(timestamps_2)
206
+
207
+ # Indicators for each pixel: 0 for timestamps from one pixel
208
+ # 1 - from the other
209
+ pix1_ind = np.zeros(len(timestamps_1), dtype=np.int32)
210
+ pix2_ind = np.ones(len(timestamps_2), dtype=np.int32)
211
+
212
+ pix1_data = np.vstack((pix1_ind, timestamps_1))
213
+ pix2_data = np.vstack((pix2_ind, timestamps_2))
214
+
215
+ # Dataframe for each pixel with pixel indicator and
216
+ # timestamps
217
+ df1 = pd.DataFrame(
218
+ pix1_data.T, columns=["Pixel_index", "Timestamp"]
219
+ )
220
+ df2 = pd.DataFrame(
221
+ pix2_data.T, columns=["Pixel_index", "Timestamp"]
222
+ )
223
+
224
+ # Combine the two dataframes
225
+ df_combined = pd.concat((df1, df2), ignore_index=True)
226
+
227
+ # Sort the timestamps
228
+ df_combined.sort_values("Timestamp", inplace=True)
229
+
230
+ # Subtract pixel indicators of neighbors; values of 0
231
+ # correspond to timestamp differences for the same pixel
232
+ # '-1' and '1' - to differences from different pixels
233
+ df_combined["Pixel_index_diff"] = df_combined["Pixel_index"].diff()
234
+
235
+ # Calculate timestamp difference between neighbors
236
+ df_combined["Timestamp_diff"] = df_combined["Timestamp"].diff()
237
+
238
+ # Get the correct timestamp difference sign
239
+ df_combined["Timestamp_diff"] = (
240
+ df_combined["Timestamp_diff"] * df_combined["Pixel_index_diff"]
241
+ )
242
+
243
+ # Collect timestamp differences where timestamps are from
244
+ # different pixels
245
+ filtered_df = df_combined[
246
+ abs(df_combined["Pixel_index_diff"]) == 1
247
+ ]
248
+
249
+ # Save only timestamps differences in the requested window
250
+ delta_ts = filtered_df[
251
+ abs(filtered_df["Timestamp_diff"]) < delta_window
252
+ ]["Timestamp_diff"].values
253
+
254
+ deltas_all[f"{q},{w}"].extend(delta_ts)
255
+
256
+ return deltas_all