forked from lund0946/smdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
targetSelector.py
393 lines (340 loc) · 13.4 KB
/
targetSelector.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
TargetSelector.py
This moddule provides tools to select targets and create slits such the meet given criteria
such as not overlapping, minimum separation, etc.
Date: 2018-08-01
Notes:
targets is a list of all targets that are inside the mask.
Main entry point is performSelection
After selecting the targets, the flag 'selected' should be set to 1 for those targets that are selected.
If slit lengths, or angles or widths are changed, these are also updated in the data structure
and returned in getSelected.
"""
import numpy as np
class TargetSelector:
"""
A tool to select slits such that they don't overlap.
"""
def __init__(self, targetList, minX, maxX, minSlitLength, minSep):
"""
targetList is a pandas data frame
minSep is minimal separation between slits in arcsec.
"""
self.targets = targetList.copy()
self.targets["selected"] = 0
self._sortTargets()
self.minX = minX
self.maxX = maxX
self.minSlitLength = minSlitLength
self.minSep = minSep
self.xgaps = []
def _sortTargets(self):
"""
Sorts targets by x-coordinates
"""
tgs = self.targets
tgs["oldIndex"] = range(0, tgs.shape[0])
tgs.sort_values(by=["pcode", "xarcs"], ascending=(False, True), inplace=True)
#self.targets = tgs.reset_index(drop=True)
self.targets = tgs
def restoreIndex(self):
self.targets.sort_values(by="oldIndex", ignore_index=True, inplace=True)
#self.targets = self.targets.reset_index(drop=True)
def _canFit(self, xgaps, xpos, slitLength, minSep):
"""
Tries to fit the new segment in a gap
Returns (canFit, index in xgaps)
"""
# print ('xgaps', xgaps)
for idx, (gapStart, gapEnd) in enumerate(xgaps):
# print ("xpos", xpos, "gap", gapStart, gapEnd)
if xpos < gapStart + minSep:
"""
xpos is left of gap,
It is also left of all remaining gaps, so return false
"""
return False, idx
if xpos >= gapEnd - minSep:
"""
xpos is on the right side, continue
"""
continue
""" xpos is in the gap, checks for margin
returns True if inside
If xpos is in this gap, it cannot be in another gap, so OK to return
"""
gapLength = gapEnd - gapStart - minSep
if gapLength < slitLength:
# Gap too short
return False, idx
return True, idx
return False, -1
def _splitGap(self, xgaps, gIdx, xpos, l1, l2, minSep):
"""
Splits the gap at xgaps[gIdx] into two
"""
gapStart, gapEnd = xgaps[gIdx]
# gap will be split into two gaps
halfSep = minSep / 2
# halfLength = slitLength / 2.0
# left = xpos - halfLength
# right = xpos + halfLength
left = xpos - l1
right = xpos + l2
slitLength= l1 + l2
if left < gapStart:
""" xpos is closer to the left side
so adjust left
"""
left = gapStart
right = left + slitLength
xgaps[gIdx] = (right + minSep, gapEnd)
#print (f"left {left:.2f}, {right:.2f}")
elif right > gapEnd:
""" xpos is closer to the right side,
so adjust right
"""
right = gapEnd
left = right - slitLength
xgaps[gIdx] = (gapStart, left- minSep)
#print (f"righ {left:.2f}, {right:.2f}")
else:
""" slit can fit in the gap, split gap into two
"""
# gap1 = gapStart, left
# gap2 = right, gapEnd
leftEnd = left - minSep
rightStart = right + minSep
if leftEnd < gapStart:
leftEnd = gapStart
xgaps[gIdx] = (gapStart, leftEnd)
if rightStart > gapEnd:
rightStart = gapEnd
xgaps.insert(gIdx + 1, (rightStart, gapEnd))
#print (f"both {gapStart:.2f}, {leftEnd:.2f}, {rightStart:.2f}, {gapEnd:.2f}")
return xgaps, left, right
def insertPairs(self, targets, minx, maxx):
"""
First step in the target selection sequence.
targets: alignment boxes or targets
Returns a list of disjunct segments corresponding to the alignment boxes
"""
sortedBoxes = targets.sort_values(by="xarcs")
lastx = -1e10
xsegms = []
for aIdx, seg in sortedBoxes.iterrows():
xpos = seg.xarcs
x0, x1 = xpos - seg.length1, xpos + seg.length2
if x1 < minx:
continue
if x0 > maxx:
break
if x0 > lastx:
# no overlap
xsegms.append((x0, x1))
else:
# merge
lastSeg = xsegms[-1]
xsegms[-1] = (lastSeg[0], x1)
lastx = x1
self.targets.at[aIdx, "selected"] = 1
print('alignment box segments\n')
print(xsegms)
return xsegms
def segments2Gaps(self, xsegms, minx, maxx, minSep):
"""
Turns segments into gaps.
Add separation to gaps
Returns a list of gaps.
A gap is a pair (left, right) of space that is not occupied.
"""
xgaps = []
currX = minx
for seg in xsegms:
if currX > maxx:
# beyond maxx, done
break
x0, x1 = seg
if currX > x1:
# seg is left of currX
continue
if x0 <= currX <= x1:
# overlapped segments
currX = x1
continue
if currX < x0:
# Segments starts at x0
# There is a gap from currX, to x0.
# The next gap will start at x1
left = currX + minSep
right = x0 - minSep
if left < right:
xgaps.append((left, right))
currX = x1
if currX < maxx:
xgaps.append((currX, maxx))
return xgaps
def _selectTargets(self, xgaps, tgs, minSlitLength, minSep):
"""
Selects targets that can fit in a gap.
tgs: list of targets, pcode > 0
"""
for tIdx, tg in tgs.iterrows():
xpos = tg.xarcs
fits, gIdx = self._canFit(xgaps, xpos, minSlitLength, minSep)
l1=tg.length1
l2=tg.length2
if l1 < 0 or l2 < 0 or l1+l2 < minSlitLength: #if -9999 assign minSlitLength/2
l1=minSlitLength/2
l2=minSlitLength/2
if fits:
xgaps, left, right = self._splitGap(xgaps, gIdx, xpos, l1,l2, minSep)
self.targets.at[tIdx, "length1"] = xpos - left
self.targets.at[tIdx, "length2"] = right - xpos
self.targets.at[tIdx, "selected"] = 1
return xgaps
def _extendSlits(self, xgaps, tgs, minSep):
"""
Extends the slits beyond the maximum length
"""
def updateLengths(start, end, idx, isGap):
"""
Updates the segment at index idx with the new lengths
Ignores segment is no a gap
"""
if isGap == 0:
mid = tgs.at[idx, "xarcs"]
self.targets.at[idx, "length1"] = mid - start
self.targets.at[idx, "length2"] = end - mid
#print (f"update {idx=}, {mid=:.1f}, {start:.1f}, {end:.1f}")
return start, end, idx, isGap
def split3(pairs, idx1, halfSep):
"""
Both left and right sides are segments.
Splits the current gap, left side goes to the left segment
and right side goes to the right segment.
"""
idx0, idx2 = idx1 - 1, idx1 + 1
leftSegm = pairs[idx0]
gap = pairs[idx1]
rightSegm = pairs[idx2]
midX = (gap[0] + gap[1]) / 2
pairs[idx0] = updateLengths(leftSegm[0], midX - halfSep, leftSegm[2], leftSegm[3])
pairs[idx1] = midX, midX, gap[2], gap[3]
pairs[idx2] = updateLengths(midX + halfSep, rightSegm[1], rightSegm[2], rightSegm[3])
def split2Left(pairs, idx1):
"""
Left side is a segment.
Left segment takes all the gap.
"""
idx0, idx2 = idx1 - 1, idx1 + 1
leftSegm = pairs[idx0]
gap = pairs[idx1]
ref = gap[1]
pairs[idx0] = updateLengths(leftSegm[0], ref, leftSegm[2], leftSegm[3])
pairs[idx1] = ref, ref, gap[2], gap[3]
def split2Right(pairs, idx1):
"""
Right side is a segment
Right segment takes all the gap.
"""
idx0, idx2 = idx1 - 1, idx1 + 1
rightSegm = pairs[idx2]
gap = pairs[idx1]
ref = gap[0]
pairs[idx1] = ref, ref, gap[2], gap[3]
pairs[idx2] = updateLengths(ref, *rightSegm[1:])
def genPairs(gaps, tgs):
"""
Generates a list of tuples (x0, x1, idx, isGap),
where x0, x1 are the start and end of a segment or gap, isGap = 1 if this is gap.
idx is the index in the list
"""
allPairs = []
for i, g in enumerate(gaps):
allPairs.append((g[0], g[1], None, 1))
for tIdx, tg in tgs.iterrows():
x = tg.xarcs
x0, x1 = x - tg.length1, x + tg.length2
isSegm = 0
if tg.pcode == -2:
isSegm = -2
allPairs.append((x0, x1, tIdx, isSegm))
#print(f"gen gap {x0:.2f}, {x1:.2f}, {tIdx=}, {tg.oldIndex=} {tg.pcode}, {tg.orgIndex=}, {tg.selected=}")
return sorted(allPairs, key=lambda x: x[0])
def splitGaps(allPairs):
"""
For all the gaps, split the gap to either left, right or both segments
"""
halfSep = minSep / 2
leftPair, currPair = allPairs[:2]
if leftPair[3] == 1 and currPair[3] == 0:
split2Right(allPairs, 0)
leftPair = currPair = rightPair = None
for idx, pair in enumerate(allPairs):
leftPair = currPair
currPair = rightPair
rightPair = pair
if idx < 2: continue
start, end, idx0, isGap = currPair
if isGap <= 0:
continue
if (end - start) < minSep:
continue
# print ("pair ", idx, pair)
# Is left pair a segmenet?
leftIsSegm = leftPair[3] == 0
# Is right pair a segment?
rightIsSegm = rightPair[3] == 0
if leftIsSegm:
if rightIsSegm:
# split gap
#print (idx, "split3", start, end)
split3(allPairs, idx-1, halfSep)
else:
# left takes all
#print (idx, "split left", start, end)
split2Left(allPairs, idx-1)
else:
if rightIsSegm:
# right takes all
#print (idx, "split right", start, end)
split2Right(allPairs, idx-1)
# end of splitGaps
allPairs = genPairs(xgaps, tgs)
#self.allPairs1 = allPairs.copy()
splitGaps(allPairs)
#self.allPairs2 = allPairs.copy()
# print (self.targets[self.targets.pcode >0][['objectId','length1', 'length2']])
return [x for x in allPairs if x[3] == 1]
def performSelection(self, extendSlits=False):
"""
This is the main method.
Performs the selection of the targetS.
Returns a list of indices of the selected targets.
"""
# Inserts the alignment boxes
inAlignBoxes = self.targets[self.targets.inMask == 1]
inAlignBoxes = inAlignBoxes[inAlignBoxes.pcode == -2]
xsegms = self.insertPairs(inAlignBoxes, self.minX, self.maxX)
self.xsegms = xsegms.copy()
# Turns the segments into gaps
xgaps = self.segments2Gaps(xsegms, self.minX, self.maxX, self.minSep)
self.xgaps0 = xgaps.copy()
# Inserts the targets
inTargets = self.targets[self.targets.inMask == 1]
inTargets = inTargets[inTargets.pcode > 0]
print('performSelection')
print(self.targets)
self.xgaps = self._selectTargets(xgaps, inTargets, self.minSlitLength, self.minSep)
self.xgaps1 = self.xgaps.copy()
self.allPairs1 = []
print('after _selectTargets')
print(self.targets)
if extendSlits:
inTargets = self.targets[self.targets.selected == 1]
xsegms = self.insertPairs (inTargets, self.minX, self.maxX)
xgaps = self.segments2Gaps (xsegms, self.minX, self.maxX, self.minSep)
self.xgaps = self._extendSlits(self.xgaps, inTargets, self.minSep)
self.restoreIndex()
return self.targets