Skip to content

Commit

Permalink
Save images in trace only if intermediate results are allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiopasra committed Mar 4, 2021
1 parent e9220e2 commit 25a20a8
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions megaradrp/recipes/calibration/trace.py
Expand Up @@ -133,11 +133,11 @@ def check_qc_tracemap(self, tracemap):
continue
if trace.start > start_min:
cost_ids[idx] += 1
msg = 'In fiber {}, trace start > {}'.format(trace.fibid, start_min)
msg = f'In fiber {trace.fibid}, trace start > {start_min}'
warnings.warn(msg)
if trace.stop < end_max:
cost_ids[idx] += 1
msg = 'In fiber {}, trace end < {}'.format(trace.fibid, end_max)
msg = f'In fiber {trace.fibid}, trace end < {end_max}'
warnings.warn(msg)

total = sum(cost_ids)
Expand All @@ -149,7 +149,7 @@ def check_qc_tracemap(self, tracemap):
else:
tracemap.quality_control = qc.QC.GOOD

#@numina.core.validator.validate
# @numina.core.validator.validate
def run(self, rinput):
"""Execute the recipe.
Expand Down Expand Up @@ -295,8 +295,8 @@ def obtain_boxes_from_image(self, reduced, expected, npeaks, cstart=2000):
res = numpy.fft.ifft(yv)
final = res.real
plt.plot(final)
#trend = detrend(final)
#plt.plot(final - trend)
# trend = detrend(final)
# plt.plot(final - trend)
plt.show()

idx = find_peaks_indexes(final, window_width=3, threshold=0.3, fpeak=1)
Expand All @@ -311,7 +311,7 @@ def obtain_boxes_from_image(self, reduced, expected, npeaks, cstart=2000):
nidxs = numpy.sort(nidx)

plt.plot(final)
#plt.scatter(idx, [0.9 for m in idx])
# plt.scatter(idx, [0.9 for m in idx])
plt.scatter(nidx, [0.95 for m in nidx], c='r')
plt.scatter(expected, [1.0 for m in expected])
plt.show()
Expand Down Expand Up @@ -379,14 +379,15 @@ def refine_boxes_from_image(self, reduced, expected, cstart=2000, nsearch=20):
ioffset = xcorr[ycorr.index(max(ycorr))]

# auxiliary plot showing the cross-correlation work
fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot(xcorr, ycorr, 'o-')
ax.set_xlabel('ioffset')
ax.set_ylabel('peak of corrrelation function')
ax.axvline(ioffset, linestyle=':', color='C1')
ax.set_title('optimal initial offset: {}'.format(ioffset))
plt.savefig('offset_borders_cross.png')
plt.close()
if self.intermediate_results:
fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot(xcorr, ycorr, 'o-')
ax.set_xlabel('ioffset')
ax.set_ylabel('peak of corrrelation function')
ax.axvline(ioffset, linestyle=':', color='C1')
ax.set_title('optimal initial offset: {}'.format(ioffset))
plt.savefig('offset_borders_cross.png')
plt.close()

# using the initial offset, refine the peak search around the new
# expected location, looking for a maximum in +/- nsearch pixels
Expand All @@ -398,21 +399,22 @@ def refine_boxes_from_image(self, reduced, expected, cstart=2000, nsearch=20):
refined[ibox] = iargmax + box - nsearch + ioffset

# auxiliary plot showing the initial and final frontier locations
fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot(final, label='cross section at x={}'.format(cstart))
ax.plot(xwave, sp_comb_lines0, label='expected location of frontiers')
for idum, item in enumerate(expected):
if idum == 0:
label = 'refined frontier location'
else:
label = None
ax.plot(refined, final[refined], 'ro', label=label)
ax.legend()
ax.set_title('optimal initial offset: {}'.format(ioffset))
ax.set_xlabel('pixel number - 1')
ax.set_ylabel('inverted normalized signal')
plt.savefig('frontiers_between_pseudoslits.png')
plt.close()
if self.intermediate_results:
fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot(final, label='cross section at x={}'.format(cstart))
ax.plot(xwave, sp_comb_lines0, label='expected location of frontiers')
for idum, item in enumerate(expected):
if idum == 0:
label = 'refined frontier location'
else:
label = None
ax.plot(refined, final[refined], 'ro', label=label)
ax.legend()
ax.set_title('optimal initial offset: {}'.format(ioffset))
ax.set_xlabel('pixel number - 1')
ax.set_ylabel('inverted normalized signal')
plt.savefig('frontiers_between_pseudoslits.png')
plt.close()

return refined, cstart

Expand Down Expand Up @@ -480,10 +482,10 @@ def search_traces(self, reduced, boxes, box_borders, inactive_fibers=None, cstar

if debug_plot:
plt.plot(mm[:, 0], mm[:, 1], '.')
plt.savefig('trace-xy-{:03d}.png'.format(dtrace.fibid))
plt.savefig(f'trace-xy-{dtrace.fibid:03d}.png')
plt.close()
plt.plot(mm[:, 0], mm[:, 2], '.')
plt.savefig('trace-xz-{:03d}.png'.format(dtrace.fibid))
plt.savefig(f'trace-xz-{dtrace.fibid:03d}.png')
plt.close()
if len(mm) < poldeg + 1:
self.logger.warning('in fibid %d, only %d points to fit pol of degree %d',
Expand Down Expand Up @@ -621,8 +623,8 @@ def init_traces(image, center, hs, boxes, box_borders, tol=1.5, threshold=0.37,
plt.plot(ipeaks_int+b1, region[ipeaks_int], 'r*')
plt.xlabel('pixel number - 1')
plt.ylabel('number of counts')
plt.title('pseudoslit box: {}, id: {}'.format(box['name'], boxid))
plt.savefig('central_cut_{:02d}.png'.format(boxid))
plt.title(f'pseudoslit box: {box["name"]}, id: {boxid}')
plt.savefig(f'central_cut_{boxid:02d}.png')
plt.close()

startid = lastid + 1
Expand Down

0 comments on commit 25a20a8

Please sign in to comment.