def spectrum_redshift_calc(private_key, sender_id, msg_id, mtype, params, extra): """ spectrum_redshift_calc """ try: info("spectrum_redshift_calc()") try: payload = DictionaryClass(params) x = decode_string(payload.x) y = decode_string(payload.y) yerr = decode_string(payload.yerr) from_redshift = float(payload.from_redshift) to_redshift = float(payload.to_redshift) sed = Sed(x, y, yerr, from_redshift) sed.redshift(to_redshift) payload.x = encode_string(sed.wavelength) payload.y = encode_string(sed.flux) payload.yerr = encode_string(sed.err) reply_success(msg_id, mtype, payload) except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) return except Exception: error(str(capture_exception()))
def stack_normalize(private_key, sender_id, msg_id, mtype, params, extra): try: info("stack_normalize()") try: payload = DictionaryClass(params) seds = [] for segment in payload.segments: x = decode_string(segment.x) y = decode_string(segment.y) yerr = decode_string(segment.yerr) id_ = str(segment.id) seds.append(IrisSed(x=x, y=y, yerr=yerr, id=id_)) stack = IrisStack(seds) result = normalize(stack, payload) for i, segment in enumerate(payload.segments): segment.x = encode_string(result[i].x) segment.y = encode_string(result[i].y) segment.yerr = encode_string(result[i].yerr) segment.norm_constant = str(result[i].norm_constant) payload.excludeds = result.excluded reply_success(msg_id, mtype, payload.get_dict()) except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) return except Exception: error(str(capture_exception()))
def stack_redshift(private_key, sender_id, msg_id, mtype, params, extra): try: info("stack_redshift()") try: payload = DictionaryClass(params) seds = [] for segment in payload.segments: x = decode_string(segment.x) y = decode_string(segment.y) yerr = decode_string(segment.yerr) z = float(segment.z) id_ = str(segment.id) seds.append(IrisSed(x=x, y=y, yerr=yerr, z=z, id=id_)) z0 = float(payload.z0) correct_flux = payload.correct_flux == "true" result = redshift(IrisStack(seds), z0, correct_flux) for i, segment in enumerate(payload.segments): segment.x = encode_string(result[i].x) segment.y = encode_string(result[i].y) segment.yerr = encode_string(result[i].yerr) payload.excludeds = result.excluded reply_success(msg_id, mtype, payload.get_dict()) except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) return except Exception: error(str(capture_exception()))
def spectrum_interpolate(private_key, sender_id, msg_id, mtype, params, extra): """ spectrum_interpolate """ try: info("spectrum_interpolate()") try: methods = {'Neville' : neville, 'Linear' : linear_interp, 'Nearest Neighbor' : nearest_interp, 'Linear Spline' : interp1d, } payload = DictionaryClass(params) x = decode_string(payload.x) y = decode_string(payload.y) x_min = max(float(payload.x_min), min(x)) x_max = min(float(payload.x_max), max(x)) method = methods[payload.method] info("method " + method.__name__) n_bins = int(payload.n_bins) log = payload.log=='true'; sed = Sed(x, y) newSed = sed.interpolate(method, (x_min, x_max), n_bins, log); filtered = False if payload.smooth == "true": info('smoothing') newSed = filter(newSed) newSed.smooth(int(payload.box_size)) if payload.normalize=="true": info('normalizing') if not filtered: newSed = filter(newSed) newSed.normalise() payload.x = encode_string(newSed.wavelength) payload.y = encode_string(newSed.flux) reply_success(msg_id, mtype, payload) info("success") except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) error("error: " + repr(e)) return except Exception: error(str(capture_exception()))
def spectrum_interpolate(private_key, sender_id, msg_id, mtype, params, extra): """ spectrum_interpolate """ try: info("spectrum_interpolate()") try: methods = { 'Neville': neville, 'Linear': linear_interp, 'Nearest Neighbor': nearest_interp, 'Linear Spline': interp1d, } payload = DictionaryClass(params) x = decode_string(payload.x) y = decode_string(payload.y) x_min = max(float(payload.x_min), min(x)) x_max = min(float(payload.x_max), max(x)) method = methods[payload.method] info("method " + method.__name__) n_bins = int(payload.n_bins) log = payload.log == 'true' sed = Sed(x, y) newSed = sed.interpolate(method, (x_min, x_max), n_bins, log) filtered = False if payload.smooth == "true": info('smoothing') newSed = filter(newSed) newSed.smooth(int(payload.box_size)) if payload.normalize == "true": info('normalizing') if not filtered: newSed = filter(newSed) newSed.normalise() payload.x = encode_string(newSed.wavelength) payload.y = encode_string(newSed.flux) reply_success(msg_id, mtype, payload) info("success") except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) error("error: " + repr(e)) return except Exception: error(str(capture_exception()))
def get_confidence_results(self, confidencemap, confidence_results=None): if confidence_results is None: methodname = confidencemap["name"].strip().lower() method_result = getattr(self.session, "get_%s_results" % methodname) confidence_results = method_result() results = {} results["sigma"] = repr(float(confidence_results.sigma)) results["percent"] = repr(float(confidence_results.percent)) results["parnames"] = list(confidence_results.parnames) results["parvals"] = encode_string(confidence_results.parvals) results["parmins"] = encode_string(confidence_results.parmins) results["parmaxes"] = encode_string(confidence_results.parmaxes) return results
def get_confidence_results(self, confidencemap, confidence_results=None): if confidence_results is None: methodname = confidencemap["name"].strip().lower() method_result = getattr(self.session, 'get_%s_results' % methodname) confidence_results = method_result() results = {} results["sigma"] = repr(float(confidence_results.sigma)) results["percent"] = repr(float(confidence_results.percent)) results["parnames"] = list(confidence_results.parnames) results["parvals"] = encode_string(confidence_results.parvals) results["parmins"] = encode_string(confidence_results.parmins) results["parmaxes"] = encode_string(confidence_results.parmaxes) return results
def stack_stack(private_key, sender_id, msg_id, mtype, params, extra): try: info("stack_stack()") try: payload = DictionaryClass(params) seds = [] for segment in payload.segments: x = decode_string(segment.x) y = decode_string(segment.y) yerr = decode_string(segment.yerr) seds.append(IrisSed(x=x, y=y, yerr=yerr)) i_stack = IrisStack(seds) binsize = float(payload.binsize) statistic = str(payload.statistic) smooth = payload.smooth == "true" smooth_binsize = float(payload.smooth_binsize) logbin = payload.log_bin == "true" result = sedstacker.sed.stack(i_stack, binsize, statistic, fill='remove', smooth=smooth, smooth_binsize=smooth_binsize, logbin=logbin) payload.segments[0].x = encode_string(result.x) payload.segments[0].y = encode_string(result.y) payload.segments[0].yerr = encode_string(result.yerr) payload.segments[0].counts = encode_string(result.counts) payload.segments = [payload.segments[0]] get_dict = payload.get_dict() reply_success(msg_id, mtype, payload.get_dict()) except Exception, e: reply_error(msg_id, sedexceptions.SEDException, e, mtype) return except Exception: error(str(capture_exception()))
def get_fit_results(self, fit_results=None): if fit_results is None: fit_results = self.session.get_fit_results() results = {} results["succeeded"] = str(int(bool(fit_results.succeeded))) results["parvals"] = encode_string(fit_results.parvals) results["parnames"] = list(fit_results.parnames) results["statval"] = repr(float(fit_results.statval)) results["numpoints"] = str(int(fit_results.numpoints)) results["dof"] = repr(float(fit_results.dof)) results["qval"] = "nan" if fit_results.qval is not None: results["qval"] = repr(float(fit_results.qval)) results["rstat"] = "nan" if fit_results.rstat is not None: results["rstat"] = repr(float(fit_results.rstat)) results["nfev"] = str(int(fit_results.nfev)) return results
def get_fit_results(self, fit_results=None): if fit_results is None: fit_results = self.session.get_fit_results() results = {} results["succeeded"] = str(int(bool(fit_results.succeeded))) results["parvals"] = encode_string(fit_results.parvals) results["parnames"] = list(fit_results.parnames) results["statval"] = repr(float(fit_results.statval)) results["numpoints"] = str(int(fit_results.numpoints)) results["dof"] = repr(float(fit_results.dof)) results["qval"] = 'nan' if fit_results.qval is not None: results["qval"] = repr(float(fit_results.qval)) results["rstat"] = 'nan' if fit_results.rstat is not None: results["rstat"] = repr(float(fit_results.rstat)) results["nfev"] = str(int(fit_results.nfev)) return results
except Exception, e: reply_error(msg_id, sedexceptions.StatisticException, e, mtype) return statvals = [] try: for pardict in params["params"]: for parkey in pardict.keys(): # native Sherpa command ui.session.set_par(parkey, pardict[parkey]) # native Sherpa command statvals.append(ui.session.calc_stat()) statvals = encode_string(statvals) except Exception, e: reply_error(msg_id, sedexceptions.StatisticException, e, mtype) return reply_success(msg_id, mtype, {'results' : statvals}) except Exception: error(str(capture_exception())) def spectrum_fit_calc_model_values(private_key, sender_id, msg_id, mtype, params, extra): """
except Exception, e: reply_error(msg_id, sedexceptions.StatisticException, e, mtype) return statvals = [] try: for pardict in params["params"]: for parkey in pardict.keys(): # native Sherpa command ui.session.set_par(parkey, pardict[parkey]) # native Sherpa command statvals.append(ui.session.calc_stat()) statvals = encode_string(statvals) except Exception, e: reply_error(msg_id, sedexceptions.StatisticException, e, mtype) return reply_success(msg_id, mtype, {'results': statvals}) except Exception: error(str(capture_exception())) def spectrum_fit_calc_model_values(private_key, sender_id, msg_id, mtype, params, extra): """ spectrum_fit_calc_model_values