Skip to content

Commit

Permalink
Refactor lengthy list comprehension.
Browse files Browse the repository at this point in the history
Pulled the calculation/tuple creation out of the list comprehension,
since it was difficult to parse.

This should probably be a static method in the long run, but this
is consistent with similar code elsewhere.
  • Loading branch information
teald committed Oct 9, 2023
1 parent fb4eb5c commit e87a91d
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions geminidr/interactive/fit/aperture.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ def __init__(self, ext, **aper_params):

# initial parameters are set as attributes
self.reset()
del self._aper_params[
"direction"
] # no longer passed to find_apertures()
# no longer passed to find_apertures()
del self._aper_params["direction"]

@property
def aper_params(self):
Expand Down Expand Up @@ -313,6 +312,7 @@ def call_listeners(self, method, *args, update_view=False, **kwargs):
func = getattr(listener, method, None)
if func:
func(*args, **kwargs)

if update_view and hasattr(listener, "update_view"):
listener.update_view()

Expand Down Expand Up @@ -386,12 +386,16 @@ def aperture_comparator(a, b):
return 1 if b[1] < a[1] else -1

# find the closest aperture in the visible range
def get_aperture_info(aperture):
# This should probably be a static method.
pos = aperture.source.data["location"][0]
dist = abs(pos - x)
ap_id = aperture.source.data["id"][0]

return dist, ap_id, pos

nearby_apertures = [
(
abs(ap.source.data["location"][0] - x),
ap.source.data["id"][0],
ap.source.data["location"][0],
)
get_aperture_info(ap)
for ap in self.aperture_models.values()
if x_start <= ap.source.data["location"][0] < x_end
]
Expand All @@ -414,19 +418,22 @@ def find_peak(self, x):
if peaks.size:
initx = peaks[np.argmin(abs(peaks - x))]
if abs(initx - x) <= 20:
peaks = pinpoint_peaks(
peaks, _ = pinpoint_peaks(
data=self.profile, mask=self.prof_mask, peaks=[initx]
)[0]
)

limits = get_limits(
np.nan_to_num(self.profile),
self.prof_mask,
peaks=peaks,
threshold=self.threshold,
min_snr=self.min_snr,
)

log.stdinfo(
f"Found source at {self.direction}: {peaks[0]:.1f}"
)

self.add_aperture(peaks[0], *limits[0])

def update(self, extras):
Expand Down

0 comments on commit e87a91d

Please sign in to comment.