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(isinstance(serialized, 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'}))
def photon_spectrum_to_energy_spectrum(apps, schema_editor): reduced_datum = apps.get_model('tom_dataproducts', 'ReducedDatum') spectrum_serializer = SpectrumSerializer() for row in reduced_datum.objects.filter(data_type='spectroscopy'): # In order to avoid a KeyError on already-corrected data or data that has no need to be corrected, we only # perform the spectroscopy correction on values that have photon_flux/photon_flux_units if all(k in row.value.keys() for k in ['photon_flux', 'photon_flux_units', 'wavelength', 'wavelength_units']): photon_counts = Quantity(value=row.value['photon_flux'], unit=row.value['photon_flux_units']) wavelength = Quantity(value=row.value['wavelength'], unit=row.value['wavelength_units']) photon_spectrum = Spectrum1D(flux=photon_counts, spectral_axis=wavelength) energy_spectrum = photon_spectrum.flux * (photon_spectrum.energy / photon) energy_spectrum_object = Spectrum1D( spectral_axis=wavelength, flux=energy_spectrum.to('erg / (s cm2 AA)', spectral_density(wavelength))) row.value = spectrum_serializer.serialize(energy_spectrum_object) row.save()
def photon_spectrum_to_energy_spectrum(apps, schema_editor): reduced_datum = apps.get_model('tom_dataproducts', 'ReducedDatum') spectrum_serializer = SpectrumSerializer() for row in reduced_datum.objects.filter(data_type='spectroscopy'): photon_counts = Quantity(value=row.value['photon_flux'], unit=row.value['photon_flux_units']) wavelength = Quantity(value=row.value['wavelength'], unit=row.value['wavelength_units']) photon_spectrum = Spectrum1D(flux=photon_counts, spectral_axis=wavelength) energy_spectrum = photon_spectrum.flux * (photon_spectrum.energy / photon) energy_spectrum_object = Spectrum1D(spectral_axis=wavelength, flux=energy_spectrum.to( 'erg / (s cm2 AA)', spectral_density(wavelength))) row.value = spectrum_serializer.serialize(energy_spectrum_object) row.save()