def test_normalize_by_int_median_mult(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1) stack = IrisStack([sed1, sed2, sed3]) # normalize SEDs with avg statistic norm_stack = stack.normalize_by_int(stats='median') numpy.testing.assert_array_almost_equal(norm_stack[0].y, 0.4270427*sed1.y) numpy.testing.assert_array_almost_equal(norm_stack[1].y, 8.54*sed2.y) self.assertAlmostEqual(norm_stack[2].norm_constant, 1.0)
def test_normalize_by_int_avg_add(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1) stack = IrisStack([sed1, sed2, sed3]) # normalize SEDs with avg statistic norm_stack = stack.normalize_by_int(stats='avg', norm_operator=1) numpy.testing.assert_array_almost_equal(norm_stack[0].y, 0 - 253.8 + sed1.y) numpy.testing.assert_array_almost_equal(norm_stack[1].y, 221.15 + sed2.y) self.assertAlmostEqual(norm_stack[2].norm_constant, 32.65)
def test_redshift_no_z(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr, id='sed1') sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1, id='sed2') y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1, id='sed3') stack = IrisStack([sed1, sed2, sed3]) shifted_stack = stack.shift(0.0, correct_flux=False) self.assertEqual(len(shifted_stack.segments), 3) self.assertRaises((InvalidRedshiftError, NoRedshiftError), sed1.shift, -5.0) self.assertEqual(shifted_stack.excluded, ['sed1', 'sed2', 'sed3'])
def test_normalize_at_point_avg_mult(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.5, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1) stack = IrisStack([sed1, sed2, sed3]) # normalize SEDs with avg statistic norm_stack = stack.normalize_at_point(5.0, 1.0, stats='avg', norm_operator=0) numpy.testing.assert_array_almost_equal(norm_stack[0].y, (8/3.)/0.5*sed1.y) numpy.testing.assert_array_almost_equal(norm_stack[1].y, (8/3.)/3.0*sed2.y) self.assertAlmostEqual(norm_stack[2].norm_constant, (8/3.)/4.5)
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 test_normalize_by_int_avg_mult(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1) stack = IrisStack([sed1, sed2, sed3]) # normalize SEDs with avg statistic norm_stack = stack.normalize_by_int(minWavelength='min', maxWavelength='max', stats='avg', y0=1.0, norm_operator=0, correct_flux=False, z0=None) numpy.testing.assert_array_almost_equal(norm_stack[0].y, 0.49234923*sed1.y) numpy.testing.assert_array_almost_equal(norm_stack[1].y, 9.846*sed2.y) self.assertAlmostEqual(norm_stack[2].norm_constant, 1.1529274)
def test_outside_norm_ranges(self): sed1 = IrisSed(x=[1,2,3,4,5], y=[1,2,3,4,5], id='sed1') sed2 = IrisSed(x=[10,20,30,40], y=[1,2,3,4], id='sed2') stack = IrisStack([sed1, sed2]) norm_stack = stack.normalize_by_int(minWavelength=2, maxWavelength=9) self.assertEqual(norm_stack.excluded[0], 'sed2') sed3 = IrisSed(x=[20,30,40], y=[1,2,3], id='sed3') stack.add_segment(sed3) norm_stack = stack.normalize_by_int(minWavelength=2, maxWavelength=9) numpy.testing.assert_array_equal(norm_stack.excluded, ['sed2', 'sed3']) self.assertEqual(len(norm_stack), 3)
def test_redshift_no_correct_flux(self): # what the yerrs should be yerr = numpy.array([ [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, numpy.nan, 0.92,], [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, numpy.nan, 0.09, 0.09, 0.09, 0.09, 0.82,], [0.09, 0.09, 0.09, numpy.nan, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.78,], [0.63, 0.36, 0.44, 0.16, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.81,], [0.11, 0.09, 0.13, 0.13, 0.09, 0.09, 0.09, 0.21, 0.09, 0.09, 0.09, 0.09, 0.09, 0.78,], [0.68, 0.30, 0.35, 0.30, 0.22, 0.25, 0.63, 0.51, 0.14, 0.14, 0.14, 0.14, 0.14, 1.22,] ]) # make dummy data y = numpy.array([ [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 1.0, 0.92,], [0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 1.0, 0.09, 0.09, 0.09, 0.09, 0.82,], [0.09, 0.09, 0.09, 1.0, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.78,], [0.63, 0.36, 0.44, 0.16, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.81,], [0.11, 0.09, 0.13, 0.13, 0.09, 0.09, 0.09, 0.21, 0.09, 0.09, 0.09, 0.09, 0.09, 0.78,], [0.68, 0.30, 0.35, 0.30, 0.22, 0.25, 0.63, 0.51, 0.14, 0.14, 0.14, 0.14, 0.14, 1.22,] ]) x = numpy.arange(len(y[0])) x = numpy.array([x, x[0:13], x, x, x, x]) z = [1,2,3,4,5,6] # populate IrisStack seds = [] for i in range(6): seds.append(IrisSed(x=x[i], y=y[i], yerr=yerr[i], z=z[i])) seds = IrisStack(seds) # shift the stack without correcting for flux shifted_seds = seds.shift(0, correct_flux=False) # The y-errors should be the same before and after the shift for i, sed in enumerate(shifted_seds): numpy.testing.assert_array_almost_equal(yerr[i], sed.yerr)
def test_redshift_correct_flux(self): sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr, id='sed1', z=0.1) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1, id='sed2', z=0.1) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1, id='sed3', z=0.1) iris_stack = IrisStack([sed1, sed2, sed3]) shifted_iris_stack = iris_stack.shift(0.0, correct_flux=True) sed1 = IrisSed(x=self.x,y=self.y,yerr=self.yerr, z=0.1) sed2 = IrisSed(x=numpy.array([2,4,5,8,10]), y=numpy.arange(5)+1.0, yerr=numpy.arange(5)+1.0*0.1, z=0.2) y = numpy.array([5.0, 15.0, 7.0, 4.5, 13.5, 10.5]) x = numpy.array([0.5, 1.5, 3.0, 5.0, 10.5, 21.0]) sed3 = IrisSed(x=x, y=y, yerr=y*0.1, z=0.3) stack = Stack([sed1, sed2, sed3]) shifted_stack = stack.shift(0.0, correct_flux=True) self.assertEqual(len(shifted_iris_stack.segments), 3) numpy.testing.assert_array_almost_equal(shifted_stack[0].x, shifted_iris_stack[0].x) numpy.testing.assert_array_almost_equal(shifted_stack[0].y, shifted_iris_stack[0].y)
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()))