curvit icon indicating copy to clipboard operation
curvit copied to clipboard

Refactor duplicated code in curvit.py

Open Copilot opened this issue 5 months ago • 0 comments

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 both single_star and multiple_star branches of combine_events_lists()
  • get_framerate_from_header() - Consolidates 18-line duplication of framerate reading logic across two locations in combine_events_lists()
  • setup_auto_background() - Wrapper for auto_bg() eliminating 12-line duplication between curve() and curve_orbitwise()
  • create_quicklook_figure() - Consolidates 23-line matplotlib visualization code duplicated in curve() and curve_orbitwise()
  • handle_background_estimation() - Consolidates 38-line background estimation workflow duplicated in curve() and curve_orbitwise()
  • remove_wcs_header_keys() - Consolidates WCS header cleanup, eliminating 30 lines of duplicate try/except blocks in image_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.

Copilot avatar Nov 03 '25 12:11 Copilot