curvit
curvit copied to clipboard
Refactor duplicated code in curvit.py
Eliminated ~239 lines of duplicated code across curve(), curve_orbitwise(), combine_events_lists(), and image_astrometry() by extracting 6 helper functions.
Changes
Helper Functions Extracted
-
adjust_detection_threshold()- Consolidates 34-line duplication of threshold adjustment logic in bothsingle_starandmultiple_starbranches ofcombine_events_lists() -
get_framerate_from_header()- Consolidates 18-line duplication of framerate reading logic across two locations incombine_events_lists() -
setup_auto_background()- Wrapper forauto_bg()eliminating 12-line duplication betweencurve()andcurve_orbitwise() -
create_quicklook_figure()- Consolidates 23-line matplotlib visualization code duplicated incurve()andcurve_orbitwise() -
handle_background_estimation()- Consolidates 38-line background estimation workflow duplicated incurve()andcurve_orbitwise() -
remove_wcs_header_keys()- Consolidates WCS header cleanup, eliminating 30 lines of duplicate try/except blocks inimage_astrometry()
Example
Before:
# Duplicated in both curve() and curve_orbitwise()
if background == "auto":
lowres_counts, bg_CPS, bg_CPS_e = auto_bg(fx, fy, time, photons, ...)
bins = np.arange(0, 4801, 4096 / whole_figure_resolution)
plt.hist2d(fx, fy, bins=(bins, bins), weights=weights, norm=LogNorm())
# ... 20 more lines of matplotlib setup
After:
if background == "auto":
lowres_counts, bg_CPS, bg_CPS_e = setup_auto_background(fx, fy, time, photons, ...)
create_quicklook_figure(fx, fy, weights, xp, yp, x_bg, y_bg, background, whole_figure_resolution)
Impact
- Single point of maintenance for shared logic
- Bug fixes propagate automatically to all call sites
- No API changes; full backward compatibility maintained
Original prompt
Find and refactor duplicated code
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.