def find_segments_helper(seg_cnt): # Global is for inter process communication global segdat, objdat, polyorder SegTrace = namedtuple("Trace", "segnum xs ys poly") PAD = 2 the_seg = (segdat == seg_cnt) span = spanrange(the_seg) mnsr = np.max((span[0].y - PAD, 0)) mxsr = np.min((span[1].y + PAD, segdat.shape[1]-1)) y_slc = slice(mnsr, mxsr) y_off = (span[0].y+span[1].y)/2.0 n_el = span[1].x - span[0].x if n_el < 50: tr = {"seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.nan, "ok": False} return tr means = np.zeros(n_el) amps = np.zeros(n_el) sds = np.zeros(n_el) trace_profile = np.zeros(mxsr-mnsr) for i in xrange(n_el): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) XX = i+span[0].x profile = np.median(objdat[y_slc, XX-3:XX+3], 1) profile -= np.min(profile) trace_profile += profile xs = np.arange(len(profile)) + span[0].y means[i] = np.sum(xs*profile)/np.sum(profile)-PAD xs = np.arange(n_el) + span[0].x poly = np.polyfit(xs, means, polyorder) tracefit = gfit1d(trace_profile, par=[0, len(trace_profile)/2., 1.7], quiet=1) tr = {"seg_cnt": seg_cnt, "xs": np.array(xs), "mean_ys": np.array(means), "coeff_ys": np.array(poly), "ok": True, "trace_sigma": np.abs(tracefit.params[2])} outstr = '\r%4.4i: fwhm=%3.2f pix' % (seg_cnt, np.abs(tracefit.params[2])*2.355) print outstr, sys.stdout.flush() return tr if plot: pl.plot(xs, means, 'x') pl.plot(xs, ff(xs))
def gFitTrace(self, specimage, y1, y2): """ Fit a gaussian to each column of an image. """ sizex, sizey = specimage.shape smoytrace = np.zeros(sizey).astype(np.float) boxcar_kernel = signal.boxcar(3) / 3.0 for c in np.arange(sizey): col = specimage[:, c] col = col - np.median(col) smcol = ni.convolve(col, boxcar_kernel).astype(np.float) fit = gfit.gfit1d(smcol, quiet=1, maxiter=15) smoytrace[c] = fit.params[1] return np.array(smoytrace)
def find_segments_helper(seg_cnt): # Global is for inter process communication global segdat, objdat, polyorder SegTrace = namedtuple("Trace", "segnum xs ys poly") PAD = 2 the_seg = (segdat == seg_cnt) span = spanrange(the_seg) mnsr = np.max((span[0].y - PAD, 0)) mxsr = np.min((span[1].y + PAD, segdat.shape[1] - 1)) y_slc = slice(mnsr, mxsr) y_off = (span[0].y + span[1].y) / 2.0 n_el = span[1].x - span[0].x if n_el < 50: tr = { "seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.nan, "ok": False } return tr means = np.zeros(n_el) amps = np.zeros(n_el) sds = np.zeros(n_el) trace_profile = np.zeros(mxsr - mnsr) for i in xrange(n_el): XX = i + span[0].x profile = np.median(objdat[y_slc, XX - 3:XX + 3], 1) profile -= np.min(profile) trace_profile += profile xs = np.arange(len(profile)) + span[0].y means[i] = np.sum(xs * profile) / np.sum(profile) - PAD xs = np.arange(n_el) + span[0].x poly = np.polyfit(xs, means, polyorder) tracefit = gfit1d(trace_profile, par=[0, len(trace_profile) / 2., 1.7], quiet=1) tr = { "seg_cnt": seg_cnt, "xs": np.array(xs), "mean_ys": np.array(means), "coeff_ys": np.array(poly), "ok": True, "trace_sigma": np.abs(tracefit.params[2]) } print '%4.4i: fwhm=%3.2f pix' % (seg_cnt, np.abs(tracefit.params[2]) * 2.355) return tr if plot: pl.plot(xs, means, 'x') pl.plot(xs, ff(xs))
def find_segments_helper(seg_cnt): """Trace a single spaxel segment and return the solution Args: seg_cnt (int): the segment number Returns: dict: Returns a segmentation map Dictionary containing:: { "seg_cnt": Segment ID number, "xs": List of x positions of trace, "mean_ys": Measured Y position (average) of the trace, "coeff_ys": polyfit coefficients to the mean_ys, "trace_sigma": Width of trace in pixels (1 sigma), "ok": Trace has more than 50 pixels } """ # Global is for inter process communication global segdat, objdat, polyorder # Padding in pixels around trace in Y PAD = 2 # Get this segment the_seg = (segdat == seg_cnt) # Test for tiny segment test = the_seg.nonzero() if len(test[0]) < 10 or len(test[1]) < 10: tr = { "seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.array(np.nan), "ok": False } outstr = '\r%4.4i: %4.4i, TINY SEGMENT, %-5s' % (seg_cnt, len(test[0]), tr['ok']) print outstr sys.stdout.flush() # We are OK else: # Get segment's geometry span = spanrange(the_seg) mnsr = np.max((span[0].y - PAD, 0)) mxsr = np.min((span[1].y + PAD, segdat.shape[1] - 1)) y_slc = slice(mnsr, mxsr) # y_off = (span[0].y + span[1].y) / 2.0 # How long is it in X? n_el = span[1].x - span[0].x # Don't fit short traces, but flag them by setting "ok" to False if n_el < 50: # Flag the sigma with zero sig = 0. # Load up the trace with NaNs tr = { "seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.array(np.nan), "ok": False } # Trace is long enough, so let's fit it! else: means = np.zeros(n_el) trace_profile = np.zeros(mxsr - mnsr) for i in xrange(n_el): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) XX = i + span[0].x profile = np.median(objdat[y_slc, XX - 3:XX + 3], 1) profile -= np.min(profile) trace_profile += profile xs = np.arange(len(profile)) + span[0].y means[i] = np.sum(xs * profile) / np.sum(profile) - PAD xs = np.arange(n_el) + span[0].x poly = np.array(np.polyfit(xs, means, polyorder)) tracefit = gfit1d(trace_profile, par=[0, len(trace_profile) / 2., 1.7], quiet=1) sig = np.abs(tracefit.params[2]) tr = { "seg_cnt": seg_cnt, "xs": np.array(xs), "mean_ys": np.array(means), "coeff_ys": poly, "trace_sigma": sig, "ok": True } outstr = '\r%4.4i: %4.4i, fwhm=%3.2f pix, %-5s' % (seg_cnt, n_el, sig * 2.355, tr['ok']) print outstr, sys.stdout.flush() return tr
def find_segments_helper(seg_cnt): """Trace a single spaxel segment and return the solution Args: seg_cnt (int): the segment number Returns: dict: Returns a segmentation map Dictionary containing:: { "seg_cnt": Segment ID number, "xs": List of x positions of trace, "mean_ys": Measured Y position (average) of the trace, "coeff_ys": polyfit coefficients to the mean_ys, "trace_sigma": Width of trace in pixels (1 sigma), "ok": Trace has more than 50 pixels } """ # Global is for inter process communication global segdat, objdat, polyorder # Padding in pixels around trace in Y PAD = 2 # Get this segment the_seg = (segdat == seg_cnt) # Test for tiny segment test = the_seg.nonzero() if len(test[0]) < 10 or len(test[1]) < 10: tr = { "seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.array(np.nan), "ok": False } outstr = '\r%4.4i: %4.4i, TINY SEGMENT, %-5s' % (seg_cnt, len( test[0]), tr['ok']) print outstr sys.stdout.flush() # We are OK else: # Get segment's geometry span = spanrange(the_seg) mnsr = np.max((span[0].y - PAD, 0)) mxsr = np.min((span[1].y + PAD, segdat.shape[1] - 1)) y_slc = slice(mnsr, mxsr) # y_off = (span[0].y + span[1].y) / 2.0 # How long is it in X? n_el = span[1].x - span[0].x # Don't fit short traces, but flag them by setting "ok" to False if n_el < 50: # Flag the sigma with zero sig = 0. # Load up the trace with NaNs tr = { "seg_cnt": seg_cnt, "xs": np.array(np.nan), "mean_ys": np.array(np.nan), "coeff_ys": np.array(np.nan), "trace_sigma": np.array(np.nan), "ok": False } # Trace is long enough, so let's fit it! else: means = np.zeros(n_el) trace_profile = np.zeros(mxsr - mnsr) for i in xrange(n_el): with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) XX = i + span[0].x profile = np.median(objdat[y_slc, XX - 3:XX + 3], 1) profile -= np.min(profile) trace_profile += profile xs = np.arange(len(profile)) + span[0].y means[i] = np.sum(xs * profile) / np.sum(profile) - PAD xs = np.arange(n_el) + span[0].x poly = np.array(np.polyfit(xs, means, polyorder)) tracefit = gfit1d(trace_profile, par=[0, len(trace_profile) / 2., 1.7], quiet=1) sig = np.abs(tracefit.params[2]) tr = { "seg_cnt": seg_cnt, "xs": np.array(xs), "mean_ys": np.array(means), "coeff_ys": poly, "trace_sigma": sig, "ok": True } outstr = '\r%4.4i: %4.4i, fwhm=%3.2f pix, %-5s' % (seg_cnt, n_el, sig * 2.355, tr['ok']) print outstr, sys.stdout.flush() return tr