Exemple #1
0
def spectroscopy_for_target(target, dataproduct=None):
    spectral_dataproducts = DataProduct.objects.filter(target=target,
                                                       tag=SPECTROSCOPY[0])
    if dataproduct:
        spectral_dataproducts = DataProduct.objects.get(
            data_product=dataproduct)

    plot_data = []
    for datum in ReducedDatum.objects.filter(
            data_product__in=spectral_dataproducts):
        deserialized = SpectrumSerializer().deserialize(datum.value)
        plot_data.append(
            go.Scatter(x=deserialized.wavelength.value,
                       y=deserialized.flux.value,
                       name=datetime.strftime(datum.timestamp,
                                              '%Y%m%d-%H:%M:%s')))

    layout = go.Layout(height=600,
                       width=700,
                       xaxis=dict(tickformat="d"),
                       yaxis=dict(tickformat=".1eg"))
    return {
        'target':
        target,
        'plot':
        offline.plot(go.Figure(data=plot_data, layout=layout),
                     output_type='div',
                     show_link=False)
    }
Exemple #2
0
class TestDataSerializer(TestCase):
    def setUp(self):
        self.serializer = SpectrumSerializer()

    def test_serialize_spectrum(self):
        flux = np.arange(1, 200) * units.Jy
        wavelength = np.arange(1, 200) * units.Angstrom
        spectrum = Spectrum1D(spectral_axis=wavelength, flux=flux)
        serialized = self.serializer.serialize(spectrum)

        self.assertTrue(type(serialized) is str)
        serialized = json.loads(serialized)
        self.assertTrue(serialized['photon_flux'])
        self.assertTrue(serialized['photon_flux_units'])
        self.assertTrue(serialized['wavelength'])
        self.assertTrue(serialized['wavelength_units'])

    def test_serialize_spectrum_invalid(self):
        with self.assertRaises(Exception):
            self.serializer.serialize({'flux': [1, 2], 'wavelength': [1, 2]})

    def test_deserialize_spectrum(self):
        serialized_spectrum = json.dumps({
            'photon_flux': [1, 2],
            'photon_flux_units': 'ph / (Angstrom cm2 s)',
            'wavelength': [1, 2],
            'wavelength_units': 'Angstrom'
        })
        deserialized = self.serializer.deserialize(serialized_spectrum)

        self.assertTrue(type(deserialized) is Spectrum1D)
        self.assertEqual(deserialized.flux.mean().value, 1.5)
        self.assertEqual(deserialized.wavelength.mean().value, 1.5)

    def test_deserialize_spectrum_invalid(self):
        with self.assertRaises(Exception):
            self.serializer.deserialize(json.dumps({'invalid_key': 'value'}))
Exemple #3
0
 def setUp(self):
     self.serializer = SpectrumSerializer()