def test_known_regression(self): # prevent regression of known bug self.assertEqual(mag_to_absmag(1., MAGIC_NUMBER), MAGIC_NUMBER) self.assertEqual(mag_to_absmag(MAGIC_NUMBER, MAGIC_NUMBER), MAGIC_NUMBER) self.assertEqual( np.all(mag_to_absmag(MAGIC_NUMBER, MAGIC_NUMBER, 1.) == (MAGIC_NUMBER, MAGIC_NUMBER)), True) self.assertEqual( np.all(mag_to_fakemag(MAGIC_NUMBER, MAGIC_NUMBER, 1.) == (MAGIC_NUMBER, MAGIC_NUMBER)), True) self.assertEqual(mag_to_fakemag(1., MAGIC_NUMBER), MAGIC_NUMBER) self.assertEqual(mag_to_fakemag(MAGIC_NUMBER, MAGIC_NUMBER), MAGIC_NUMBER) self.assertEqual(fakemag_to_pc(1., MAGIC_NUMBER).value, MAGIC_NUMBER) self.assertEqual(fakemag_to_pc(-1., 2.).value, MAGIC_NUMBER) self.assertEqual(absmag_to_pc(1., MAGIC_NUMBER).value, MAGIC_NUMBER)
except KeyError: parallax = apogeegaia_file["parallax"] parallax_error = apogeegaia_file["parallax_error"] # set negative parallax after constant offset correction to np.nan parallax_error[(parallax < 0) & (parallax > 1e10)] = np.nan parallax[(parallax < 0) & (parallax > 1e10)] = np.nan # inference net = load_folder(astronn_dist_model) # pred, pred_err = net.predict(all_spec) pred, pred_err = net.predict_dataset(fits.getdata(contspac_file_name)) # unit conversion nn_dist, nn_dist_err = fakemag_to_pc(pred[:, 0], corrected_K, pred_err["total"][:, 0]) _, nn_dist_model_err = fakemag_to_pc(pred[:, 0], corrected_K, pred_err["model"][:, 0]) nn_parallax, nn_parallax_err = fakemag_to_parallax(pred[:, 0], corrected_K, pred_err["total"][:, 0]) _, nn_parallax_model_err = fakemag_to_parallax(pred[:, 0], corrected_K, pred_err["model"][:, 0]) # remove astropy units nn_dist = nn_dist.value nn_dist_err = nn_dist_err.value nn_dist_model_err = nn_dist_model_err.value nn_parallax = nn_parallax.value nn_parallax_err = nn_parallax_err.value nn_parallax_model_err = nn_parallax_model_err.value
def test_astrometry_conversion(self): # Example data of [Vega, Sirius] absmag = np.array([0.582, 1.42]) mag = np.array([0.026, -1.46]) parallax = np.array([130.23, 379.21]) parallax_err = np.array([0.36, 1.58]) # Absmag related test pc = absmag_to_pc(absmag, mag) npt.assert_almost_equal(pc.value, 1000 / parallax, decimal=1) npt.assert_almost_equal(mag_to_absmag(mag, parallax * u.mas), absmag, decimal=1) self.assertEqual( np.any(absmag_to_fakemag(MAGIC_NUMBER) == MAGIC_NUMBER), True) absmag_test, absmag_err_test = mag_to_absmag(mag, parallax * u.mas, parallax_err) absmag_test_uniterr, absmag_err_test_uniterr = mag_to_absmag( mag, parallax * u.mas, parallax_err / 1000 * u.arcsec) absmag_test_arc, absmag_err_test_arc = mag_to_absmag( mag, parallax / 1000 * u.arcsec, parallax_err / 1000) absmag_test_unitless, absmag_err_test_unitless = mag_to_absmag( mag, parallax, parallax_err) # make sure unitless same as using astropy unit npt.assert_almost_equal(absmag_test, absmag_test_unitless) npt.assert_almost_equal(absmag_test, absmag_test_unitless) npt.assert_almost_equal(absmag_err_test, absmag_err_test_uniterr) # make sure astropy unit conversion works fine npt.assert_almost_equal(absmag_test, absmag_test_arc) npt.assert_almost_equal(absmag_err_test, absmag_err_test_arc) # =================== Fakemag related test ===================# # make sure these function did identity transform npt.assert_almost_equal(fakemag_to_absmag(absmag_to_fakemag(absmag)), absmag, decimal=1) # we can tests this after identity transformation confirmed npt.assert_almost_equal(fakemag_to_pc(absmag_to_fakemag(absmag), mag).value, 1000 / parallax, decimal=1) npt.assert_almost_equal(fakemag_to_pc( mag_to_fakemag(mag, parallax * u.mas), mag).value, 1000 / parallax, decimal=1) fakemag_test, fakemag_err_test = mag_to_fakemag( mag, parallax * u.mas, parallax_err) fakemag_test_uniterr, fakemag_err_test_uniterr = mag_to_fakemag( mag, parallax * u.mas, parallax_err / 1000 * u.arcsec) fakemag_test_unitless, fakemag_err_test_unitless = mag_to_fakemag( mag, parallax, parallax_err) fakemag_test_arc, fakemag_err_test_arc = mag_to_fakemag( mag, parallax / 1000 * u.arcsec, parallax_err / 1000) pc_result, pc_result_err = fakemag_to_pc(fakemag_test, mag, fakemag_err_test) pc_result_arc, pc_result_err_arc = fakemag_to_pc( fakemag_test_arc, mag, fakemag_err_test_arc) # Analytically solution checkung npt.assert_almost_equal(pc_result_err.value, (parallax_err / parallax) * pc.value, decimal=1) # make sure unitless same as using astropy unit npt.assert_almost_equal(fakemag_test, fakemag_test_unitless) npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_uniterr) npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_unitless) # make sure astropy unit conversion works fine npt.assert_almost_equal(fakemag_test, fakemag_test_arc) npt.assert_almost_equal(fakemag_err_test, fakemag_err_test_arc) npt.assert_almost_equal(pc_result.value, pc_result_arc.value) npt.assert_almost_equal(pc_result_err.value, pc_result_err_arc.value) # check gaia default dr dr = gaia_default_dr() self.assertEqual(dr, 1) dr = gaia_default_dr(dr=3) self.assertEqual(dr, 3)
apogeegaia_file = fits.getdata(gaia_rowmatch_f) # add the offset we found for inv var weighting parallax parallax = apogeegaia_file["parallax"] + 0.052 parallax_error = apogeegaia_file["parallax_error"] # set negative parallax after constant offset correction to np.nan parallax[(parallax < 0)] = np.nan parallax_error[(parallax < 0)] = np.nan # inference net = load_folder(astronn_dist_model) pred, pred_err = net.test(all_spec) pred[:, 0][pred[:, 0] == 0.] = np.nan # unit conversion nn_dist, nn_dist_err = fakemag_to_pc(pred[:, 0], corrected_K, pred_err['total'][:, 0]) _, nn_dist_model_err = fakemag_to_pc(pred[:, 0], corrected_K, pred_err['model'][:, 0]) nn_parallax, nn_parallax_err = fakemag_to_parallax(pred[:, 0], corrected_K, pred_err['total'][:, 0]) _, nn_parallax_model_err = fakemag_to_parallax(pred[:, 0], corrected_K, pred_err['model'][:, 0]) # remove astropy units nn_dist = nn_dist.value nn_dist_err = nn_dist_err.value nn_dist_model_err = nn_dist_model_err.value nn_parallax = nn_parallax.value nn_parallax_err = nn_parallax_err.value nn_parallax_model_err = nn_parallax_model_err.value
def test_arXiv_1902_08634 (self): """ astroNN spectrophotometric distance """ from astroNN.apogee import visit_spectra, apogee_continuum from astroNN.gaia import extinction_correction, fakemag_to_pc from astropy.io import fits # first model models_url = ["https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_no_offset_model", "https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_constant_model", "https://github.com/henrysky/astroNN_gaia_dr2_paper/trunk/astroNN_multivariate_model"] for model_url in models_url: download_args = ["svn", "export", model_url] res = subprocess.Popen(download_args, stdout=subprocess.PIPE) output, _error = res.communicate() if not _error: pass else: raise ConnectionError(f"Error downloading the models {model_url}") opened_fits = fits.open(visit_spectra(dr=14, location=4405, apogee='2M19060637+4717296')) spectrum = opened_fits[1].data spectrum_err = opened_fits[2].data spectrum_bitmask = opened_fits[3].data # using default continuum and bitmask values to continuum normalize norm_spec, norm_spec_err = apogee_continuum(spectrum, spectrum_err, bitmask=spectrum_bitmask, dr=14) # correct for extinction K = extinction_correction(opened_fits[0].header['K'], opened_fits[0].header['AKTARG']) # ===========================================================================================# # load neural net neuralnet = load_folder('astroNN_no_offset_model') # inference, if there are multiple visits, then you should use the globally # weighted combined spectra (i.e. the second row) pred, pred_err = neuralnet.test(norm_spec) # convert prediction in fakemag to distance pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0]) # assert distance is close enough # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas # no offset correction so further away self.assertEqual(pc.value < 1250, True) self.assertEqual(pc.value > 1100, True) # ===========================================================================================# # load neural net neuralnet = load_folder('astroNN_constant_model') # inference, if there are multiple visits, then you should use the globally # weighted combined spectra (i.e. the second row) pred, pred_err = neuralnet.test(np.hstack([norm_spec, np.zeros((norm_spec.shape[0], 4))])) # convert prediction in fakemag to distance pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0]) # assert distance is close enough # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas self.assertEqual(pc.value < 1150, True) self.assertEqual(pc.value > 1000, True) # ===========================================================================================# # load neural net neuralnet = load_folder('astroNN_multivariate_model') # inference, if there are multiple visits, then you should use the globally # weighted combined spectra (i.e. the second row) pred, pred_err = neuralnet.test(np.hstack([norm_spec, np.zeros((norm_spec.shape[0], 4))])) # convert prediction in fakemag to distance pc, pc_error = fakemag_to_pc(pred[:, 0], K, pred_err['total'][:, 0]) # assert distance is close enough # http://simbad.u-strasbg.fr/simbad/sim-id?mescat.distance=on&Ident=%406876647&Name=KIC+10196240&submit=display+selected+measurements#lab_meas self.assertEqual(pc.value < 1150, True) self.assertEqual(pc.value > 1000, True)