Skip to content

Commit

Permalink
removing duplicated calculations & better handling of header keywords…
Browse files Browse the repository at this point in the history
… with failed extractions
  • Loading branch information
ajmejia committed Sep 24, 2024
1 parent 65060be commit c6b6441
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
13 changes: 5 additions & 8 deletions python/lvmdrp/core/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,25 +604,22 @@ def plot_wavesol_lsf(xpix, lsf, lines_pixels, wave_poly, wave_coeffs, lsf_poly,
return ax


def plot_fiber_thermal_shift(columns, column_shifts, ax=None, labels=False):
def plot_fiber_thermal_shift(columns, column_shifts, median_shift, std_shift, ax=None, labels=False):
""""Plots the thermal shifts measured in the fiber centroids"""
if ax is None:
_, ax = create_subplots(figsize=(15,5), layout="constrained")

mean_shifts = np.nanmean(column_shifts)
std_shifts = np.nanstd(column_shifts)

ax.plot(columns, column_shifts, "o-", color="tab:blue")
ax.axhline(0, color="0.1", ls=":")
ax.axhspan(mean_shifts-std_shifts, mean_shifts+std_shifts, color="tab:red", alpha=0.1)
ax.axhline(mean_shifts, color="tab:red", lw=1, zorder=0)
ax.axhspan(median_shift-std_shift, median_shift+std_shift, color="tab:red", alpha=0.1)
ax.axhline(median_shift, color="tab:red", lw=1, zorder=0)
ax.set_title("Y shifts for each column")
ax.set_xlabel("X (pixel)")
ax.set_ylabel("Y shift (pixel)")

if labels:
ax.annotate(f"mean: {mean_shifts:.2f}", (0.9, 0.9), xycoords="axes fraction", ha="right", va="top", color="tab:red")
ax.annotate(f"std: {std_shifts:.2f}", (0.9, 0.85), xycoords="axes fraction", ha="right", va="top", color="tab:red")
ax.annotate(f"mean: {median_shift:.2f}", (0.9, 0.9), xycoords="axes fraction", ha="right", va="top", color="tab:red")
ax.annotate(f"std: {std_shift:.2f}", (0.9, 0.85), xycoords="axes fraction", ha="right", va="top", color="tab:red")

return ax

Expand Down
36 changes: 18 additions & 18 deletions python/lvmdrp/functions/imageMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def _fix_fiber_thermal_shifts(image, trace_cent, trace_width=None, trace_amp=Non
trace_cent_fixed._coeffs[:, 0] += median_shift
trace_cent_fixed.eval_coeffs()

return trace_cent_fixed, column_shifts, fiber_model
return trace_cent_fixed, column_shifts, median_shift, std_shift, fiber_model


def _apply_electronic_shifts(images, out_images, drp_shifts=None, qc_shifts=None, custom_shifts=None, raw_shifts=None,
Expand Down Expand Up @@ -2614,14 +2614,14 @@ def extract_spectra(

# fix centroids for thermal shifts
log.info(f"measuring fiber thermal shifts @ columns: {','.join(map(str, columns))}")
trace_mask, shifts, _ = _fix_fiber_thermal_shifts(img, trace_mask, 2.5,
fiber_model=fiber_model,
trace_amp=10000,
columns=columns,
column_width=column_width,
shift_range=shift_range, axs=[axs_cc, axs_fb])
trace_mask, shifts, median_shift, std_shift, _ = _fix_fiber_thermal_shifts(img, trace_mask, 2.5,
fiber_model=fiber_model,
trace_amp=10000,
columns=columns,
column_width=column_width,
shift_range=shift_range, axs=[axs_cc, axs_fb])
# save columns measured for thermal shifts
plot_fiber_thermal_shift(columns, shifts, ax=ax_shift)
plot_fiber_thermal_shift(columns, shifts, median_shift, std_shift, ax=ax_shift)
save_fig(fig, product_path=out_rss, to_display=display_plots, figure_path="qa", label="fiber_thermal_shifts")

if method == "optimal":
Expand Down Expand Up @@ -2725,48 +2725,48 @@ def extract_spectra(
rss.setHdrValue("DISPAXIS", 1)
rss.setHdrValue(
"HIERARCH FIBER CENT MIN",
bn.nanmin(trace_mask._data[rss._good_fibers]),
bn.nanmin(trace_mask._data),
)
rss.setHdrValue(
"HIERARCH FIBER CENT MAX",
bn.nanmax(trace_mask._data[rss._good_fibers]),
bn.nanmax(trace_mask._data),
)
rss.setHdrValue(
"HIERARCH FIBER CENT AVG",
bn.nanmean(trace_mask._data[rss._good_fibers]) if data.size != 0 else 0,
bn.nanmean(trace_mask._data) if data.size != 0 else 0,
)
rss.setHdrValue(
"HIERARCH FIBER CENT MED",
bn.nanmedian(trace_mask._data[rss._good_fibers])
bn.nanmedian(trace_mask._data)
if data.size != 0
else 0,
)
rss.setHdrValue(
"HIERARCH FIBER CENT SIG",
numpy.std(trace_mask._data[rss._good_fibers]) if data.size != 0 else 0,
numpy.std(trace_mask._data) if data.size != 0 else 0,
)
if method == "optimal":
rss.setHdrValue(
"HIERARCH FIBER WIDTH MIN",
bn.nanmin(trace_fwhm._data[rss._good_fibers]),
bn.nanmin(trace_fwhm._data),
)
rss.setHdrValue(
"HIERARCH FIBER WIDTH MAX",
bn.nanmax(trace_fwhm._data[rss._good_fibers]),
bn.nanmax(trace_fwhm._data),
)
rss.setHdrValue(
"HIERARCH FIBER WIDTH AVG",
bn.nanmean(trace_fwhm._data[rss._good_fibers]) if data.size != 0 else 0,
bn.nanmean(trace_fwhm._data) if data.size != 0 else 0,
)
rss.setHdrValue(
"HIERARCH FIBER WIDTH MED",
bn.nanmedian(trace_fwhm._data[rss._good_fibers])
bn.nanmedian(trace_fwhm._data)
if data.size != 0
else 0,
)
rss.setHdrValue(
"HIERARCH FIBER WIDTH SIG",
numpy.std(trace_fwhm._data[rss._good_fibers]) if data.size != 0 else 0,
numpy.std(trace_fwhm._data) if data.size != 0 else 0,
)
# save extracted RSS
log.info(f"writing extracted spectra to {os.path.basename(out_rss)}")
Expand Down

0 comments on commit c6b6441

Please sign in to comment.