2
2
import numpy as np
3
3
from functools import lru_cache
4
4
import pandas as pd
5
+ from scipy .sparse import csr_matrix
6
+ from scipy .spatial import cKDTree
5
7
6
8
from .constants import (
7
9
FOCAL_LENGTH_MM , PINCUSHION_DISTORTION_SLOPE ,
8
- PIXEL_SPACING_MM , FOV_PER_PIXEL_DEG
10
+ PIXEL_SPACING_MM , FOV_PER_PIXEL_DEG , N_PIXEL
9
11
)
10
12
11
13
@@ -172,8 +174,10 @@ def combine_bias_patch_current_to_trigger_patch_current(bias_patch_currents):
172
174
fivers = pi [pi .bias_patch_size == 5 ].sort_values ('trigger_patch_id' )
173
175
174
176
b_c = bias_patch_currents # just to shorten the name
175
- t_c = b_c [fourers .bias_patch_id .values ] * 4 / 9 + b_c [fivers .bias_patch_id .values ] * 5 / 9
176
-
177
+ t_c = (
178
+ b_c [fourers .bias_patch_id .values ] * 4 / 9
179
+ + b_c [fivers .bias_patch_id .values ] * 5 / 9
180
+ )
177
181
trigger_patch_currents = t_c # unshorten the name
178
182
return trigger_patch_currents
179
183
@@ -190,3 +194,39 @@ def take_apart_trigger_values_for_bias_patches(trigger_rates):
190
194
pi = patch_indices ().sort_values ('bias_patch_id' )
191
195
192
196
return trigger_rates [pi .trigger_patch_id .values ]
197
+
198
+
199
+ @lru_cache (maxsize = 1 )
200
+ def get_neighbor_matrix ():
201
+ '''
202
+ Returns a sparse boolean neighbor matrix with n[chid, other_chid] = is neighbor.
203
+ '''
204
+ xy = get_pixel_dataframe ().loc [:, ['x' , 'y' ]].values
205
+ tree = cKDTree (xy )
206
+ neighbors = tree .query_ball_tree (tree , r = 10 )
207
+
208
+ n_neighbors = [len (n ) for n in neighbors ]
209
+ col = np .repeat (np .arange (N_PIXEL ), n_neighbors )
210
+ row = [pix for n in neighbors for pix in n ]
211
+ data = np .ones (len (row ))
212
+ m = csr_matrix ((data , (row , col )), shape = (N_PIXEL , N_PIXEL ), dtype = bool )
213
+ m .setdiag (False )
214
+ return m
215
+
216
+
217
+ @lru_cache (maxsize = 1 )
218
+ def get_num_neighbors ():
219
+ '''
220
+ Return a numpy array with the number of neighbors for each pixel in chid order
221
+ '''
222
+ return get_neighbor_matrix ().sum (axis = 0 ).A1
223
+
224
+
225
+ @lru_cache ()
226
+ def get_border_pixel_mask (width = 1 ):
227
+ if width == 1 :
228
+ return get_num_neighbors () < 6
229
+
230
+ n = get_neighbor_matrix ().todense ().A
231
+
232
+ return (n & get_border_pixel_mask (width - 1 )).any (axis = 1 )
0 commit comments