Exemple #1
0
    def test_bayesian_mnist(self):
        from astroNN.models import MNIST_BCNN
        import pylab as plt
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        y_train = utils.to_categorical(y_train, 10)
        # To convert to desirable type
        y_train = y_train.astype(np.float32)
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)

        # Create a astroNN neural network instance and set the basic parameter
        net = MNIST_BCNN()
        net.task = 'classification'
        net.callbacks = ErrorOnNaN()
        net.max_epochs = 1  # Just use 5 epochs for quick result

        # Trian the nerual network
        net.train(x_train[:200], y_train[:200])
        plt.close()  # Travis-CI memory error??
        # net.plot_model()  # disable due to memory issue on Travis CI
        net.save('mnist_bcnn_test')
        net.plot_dense_stats()
        net.evaluate(x_train[:10], y_train[:10])

        net_reloaded = load_folder("mnist_bcnn_test")
        net_reloaded.mc_num = 3  # prevent memory issue on Tavis CI
        prediction_loaded = net_reloaded.test(x_test[:200])

        net_reloaded.folder_name = None  # set to None so it can be saved
        net_reloaded.save()

        load_folder(
            net_reloaded.folder_name)  # ignore pycharm warning, its not None
Exemple #2
0
    def test_bayesian_mnist(self):
        import pylab as plt

        # Create a astroNN neural network instance and set the basic parameter
        net = MNIST_BCNN()
        net.task = 'classification'
        net.callbacks = ErrorOnNaN()
        net.max_epochs = 1

        # Train the neural network
        net.train(x_train, y_train)
        net.save('mnist_bcnn_test')
        net.plot_dense_stats()
        plt.close()  # Travis-CI memory error??
        net.evaluate(x_test, utils.to_categorical(y_test, 10))

        pred, pred_err = net.test(x_test)
        test_num = y_test.shape[0]
        assert (np.sum(pred == y_test)) / test_num > 0.9  # assert accuracy

        net_reloaded = load_folder("mnist_bcnn_test")
        net_reloaded.mc_num = 3  # prevent memory issue on Tavis CI
        prediction_loaded = net_reloaded.test(x_test[:200])

        net_reloaded.folder_name = None  # set to None so it can be saved
        net_reloaded.save()

        load_folder(net_reloaded.folder_name)  # ignore pycharm warning, its not None
Exemple #3
0
    def test_arXiv_1808_04428(self):
        """
        original astroNN paper models
        """
        from astroNN.apogee import visit_spectra, apogee_continuum
        from astropy.io import fits

        # first model
        models_url = [
            "https://github.com/henrysky/astroNN_spectra_paper_figures/trunk/astroNN_0606_run001",
            "https://github.com/henrysky/astroNN_spectra_paper_figures/trunk/astroNN_0617_run001"
        ]

        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)
        # load neural net
        neuralnet = load_folder('astroNN_0617_run001')

        # 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)

        # assert temperature and gravity okay
        self.assertEqual(np.all(pred[0, 0:2] > [4700., 2.40]), True)
        self.assertEqual(np.all(pred[0, 0:2] < [4750., 2.47]), True)

        # load neural net
        neuralnet = load_folder('astroNN_0606_run001')

        # 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)

        # assert temperature and gravity okay
        self.assertEqual(np.all(pred[0, 0:2] > [4700., 2.40]), True)
        self.assertEqual(np.all(pred[0, 0:2] < [4750., 2.47]), True)
Exemple #4
0
    def test_mnist(self):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = utils.to_categorical(y_train, 10)

        # To convert to desirable type
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train[:200], y_train[:200])
        output_shape = mnist_test.output_shape
        mnist_test.test(x_test[:200])
        mnist_test.evaluate(x_train[:100], y_train[:100])

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train[200:400], y_train[200:400].astype(bool))
        prediction = mnist_test.test(x_test[200:400])
        print("12345: ", mnist_test.input_std)
        mnist_test.save('mnist_test')
        print("12345: ", mnist_test.input_std)
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test[200:400])
        mnist_reloaded.jacobian_old(x_test[:2])
        eval_result = mnist_reloaded.evaluate(x_test[200:400],
                                              y_train[200:400].astype(bool))

        # Cifar10_CNN without dropout is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # test verbose metrics
        mnist_reloaded.metrics = ['accuracy']
        mnist_reloaded.compile()
        print("12345: ", mnist_test.input_std)
        mnist_test.save('mnist_test_accuracy')
        mnist_reloaded_again = load_folder("mnist_test_accuracy")
        eval_result_again = mnist_reloaded_again.evaluate(
            x_test[200:400], y_train[200:400])
        # assert saving again wont affect the model
        self.assertAlmostEqual(eval_result_again['loss'],
                               eval_result['loss'],
                               places=3)
Exemple #5
0
    def test_color_images(self):
        # test colored 8bit images
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        x_train = np.random.randint(0, 255, size=(1000, 28, 28, 3))
        x_test = np.random.randint(0, 255, size=(100, 28, 28, 3))
        y_train = y_train[:1000]
        y_train = np_utils.to_categorical(y_train, 10)
        # To convert to desirable type

        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train, y_train[:1000])
        mnist_test.test(x_test[:1000])

        # create model instance for binary classification
        mnist_test = Galaxy10CNN()
        mnist_test.max_epochs = 1

        mnist_test.train(x_train[:1000], y_train[:1000])
        prediction = mnist_test.test(x_test[:1000])

        mnist_test.save('cifar10_test')
        mnist_reloaded = load_folder("cifar10_test")
        prediction_loaded = mnist_reloaded.test(x_test[:1000])
        mnist_reloaded.jacobian(x_test[:10], mean_output=True, mc_num=2)

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
    def test_apogee_cnn(self):
        # Apogee_CNN
        print("======Apogee_CNN======")
        neuralnet = ApogeeCNN()
        neuralnet.max_epochs = 1
        neuralnet.callbacks = ErrorOnNaN()
        neuralnet.train(random_xdata, random_ydata)
        prediction = neuralnet.test(random_xdata)
        jacobian = neuralnet.jacobian(random_xdata[:10])

        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        np.testing.assert_array_equal(jacobian.shape, [random_xdata[:10].shape[0], random_ydata.shape[1],
                                                       random_xdata.shape[1]])
        neuralnet.save(name='apogee_cnn')

        neuralnet_loaded = load_folder("apogee_cnn")
        neuralnet_loaded.max_epochs = 1
        neuralnet_loaded.callbacks = ErrorOnNaN()
        prediction_loaded = neuralnet_loaded.test(random_xdata)

        # Apogee_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # Fine tuning test
        neuralnet_loaded.train(random_xdata, random_ydata)
        prediction_loaded = neuralnet_loaded.test(random_xdata)
        # prediction should not be equal after fine-tuning
        self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)
Exemple #7
0
    def test_color_images(self):
        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train_color, y_train)
        pred = mnist_test.test(x_test_color)
        test_num = y_test.shape[0]
        assert (np.sum(np.argmax(pred, axis=1) == y_test)) / test_num > 0.9  # assert accuracy

        # create model instance for binary classification
        mnist_test = Galaxy10CNN()
        mnist_test.max_epochs = 1
        mnist_test.mc_num = 3

        mnist_test.train(x_train[:200], y_train[:200])
        prediction = mnist_test.test(x_test[:200])

        mnist_test.save('cifar10_test')
        mnist_reloaded = load_folder("cifar10_test")
        prediction_loaded = mnist_reloaded.test(x_test[:200])
        mnist_reloaded.jacobian(x_test[:2], mean_output=True, mc_num=2)
        # mnist_reloaded.hessian_diag(x_test[:10], mean_output=True, mc_num=2)

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
Exemple #8
0
    def test_mnist(self):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = utils.to_categorical(y_train, 10)

        # To convert to desirable type
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train[:200], y_train[:200])
        output_shape = mnist_test.output_shape
        mnist_test.test(x_test[:200])
        mnist_test.evaluate(x_train[:100], y_train[:100])

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train[:200], y_train[:200])
        prediction = mnist_test.test(x_test[:200])

        mnist_test.save('mnist_test')
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test[:200])
        mnist_reloaded.jacobian_old(x_test[:2])

        # Cifar10_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
Exemple #9
0
    def test_apogee_cvae(self):
        # Data preparation
        random_xdata = np.random.normal(0, 1, (200, 7514))

        # ApogeeCVAE
        print("======ApogeeCVAE======")
        cvae_net = ApogeeCVAE()
        cvae_net.max_epochs = 1
        cvae_net.latent_dim = 2
        cvae_net.callbacks = ErrorOnNaN()
        cvae_net.train(random_xdata, random_xdata)
        prediction = cvae_net.test(random_xdata)
        encoding = cvae_net.test_encoder(random_xdata)
        print(cvae_net.evaluate(random_xdata, random_xdata))

        np.testing.assert_array_equal(prediction.shape, np.expand_dims(random_xdata, axis=-1).shape)
        np.testing.assert_array_equal(encoding.shape, [random_xdata.shape[0], cvae_net.latent_dim])
        cvae_net.save(name='apogee_cvae')

        # just to make sure it can load it back without error
        cvae_net_loaded = load_folder("apogee_cvae")
        encoding = cvae_net_loaded.test_encoder(random_xdata)
        np.testing.assert_array_equal(encoding.shape, [random_xdata.shape[0], cvae_net.latent_dim])

        # Fine-tuning test
        cvae_net_loaded.max_epochs = 1
        cvae_net.callbacks = ErrorOnNaN()
        cvae_net_loaded.train(random_xdata, random_xdata)
Exemple #10
0
    def test_apogeedr14_gaiadr2(self):
        """
        Test ApogeeDR14GaiaDR2BCNN models
        - training, testing, evaluation
        """
        # Data preparation
        random_xdata_error1 = np.random.normal(0, 1, (200, 7514))
        random_xdata_error2 = np.random.normal(0, 1, (200, 7515))
        random_xdata = np.random.normal(0, 1, (200, 7516))

        # ApogeeBCNNCensored
        print("======ApogeeDR14GaiaDR2BCNN======")
        random_ydata = np.random.normal(0, 1, (200, 1))

        apogeedr14gaiadr2bcnn = ApogeeDR14GaiaDR2BCNN()
        apogeedr14gaiadr2bcnn.max_epochs = 1
        apogeedr14gaiadr2bcnn.callbacks = ErrorOnNaN()
        self.assertRaises(IndexError, apogeedr14gaiadr2bcnn.train, random_xdata_error1, random_ydata)

        apogeedr14gaiadr2bcnn = ApogeeDR14GaiaDR2BCNN()
        apogeedr14gaiadr2bcnn.max_epochs = 1
        apogeedr14gaiadr2bcnn.callbacks = ErrorOnNaN()
        self.assertRaises(ValueError, apogeedr14gaiadr2bcnn.train, random_xdata_error2, random_ydata)

        apogeedr14gaiadr2bcnn = ApogeeDR14GaiaDR2BCNN()
        apogeedr14gaiadr2bcnn.max_epochs = 1
        apogeedr14gaiadr2bcnn.callbacks = ErrorOnNaN()
        apogeedr14gaiadr2bcnn.train(random_xdata, random_ydata)

        # prevent memory issue on Tavis CI
        apogeedr14gaiadr2bcnn.mc_num = 2
        prediction, prediction_err = apogeedr14gaiadr2bcnn.test(random_xdata)
        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        apogeedr14gaiadr2bcnn.save(name='apogeedr14_gaiadr2')
        bneuralnetcensored_loaded = load_folder("apogeedr14_gaiadr2")
Exemple #11
0
    def test_mnist(self):
        # create model instance
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 1
        mnist_test.callbacks = ErrorOnNaN()

        mnist_test.train(x_train, y_train)
        output_shape = mnist_test.output_shape
        pred = mnist_test.test(x_test)
        test_num = y_test.shape[0]
        assert (np.sum(np.argmax(pred, axis=1) == y_test)
                ) / test_num > 0.9  # assert accurancy
        mnist_test.evaluate(x_test, utils.to_categorical(y_test, 10))

        # create model instance for binary classification
        mnist_test = Cifar10CNN()
        mnist_test.max_epochs = 2
        mnist_test.task = 'binary_classification'

        mnist_test.train(x_train, y_train.astype(bool))
        prediction = mnist_test.test(x_test)
        assert (np.sum(np.argmax(prediction, axis=1) == y_test)
                ) / test_num > 0.9  # assert accuracy
        mnist_test.save('mnist_test')
        mnist_reloaded = load_folder("mnist_test")
        prediction_loaded = mnist_reloaded.test(x_test)
        eval_result = mnist_reloaded.evaluate(x_test,
                                              utils.to_categorical(y_test, 10))

        # Cifar10_CNN without dropout is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # test verbose metrics
        mnist_reloaded.metrics = ['accuracy']
        mnist_reloaded.compile()
        mnist_test.save('mnist_test_accuracy')
        mnist_reloaded_again = load_folder("mnist_test_accuracy")
        # test with astype boolean deliberately
        eval_result_again = mnist_reloaded_again.evaluate(
            x_test,
            utils.to_categorical(y_test, 10).astype(bool))
        # assert saving again wont affect the model
        self.assertAlmostEqual(eval_result_again['loss'],
                               eval_result['loss'],
                               places=3)
    def test_apogee_bcnn(self):
        """
        Test ApogeeBCNN models
        - training, testing, evaluation
        - Apogee plotting functions
        """

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        # deliberately chosen targetname to test targetname conversion too
        bneuralnet.targetname = ['logg', 'feh']

        bneuralnet.max_epochs = 5  # for quick result
        bneuralnet.callbacks = ErrorOnNaN(
        )  # Raise error and fail the test if Nan
        bneuralnet.train(xdata, ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape

        bneuralnet.mc_num = 2
        prediction, prediction_err = bneuralnet.test(xdata)
        mape = np.median(
            np.abs(prediction[bneuralnet.val_idx] - ydata[bneuralnet.val_idx])
            / ydata[bneuralnet.val_idx],
            axis=0)
        self.assertEqual(np.all(0.15 > mape),
                         True)  # assert less than 15% error
        self.assertEqual(
            np.all(0.25 > np.median(prediction_err['total'], axis=0)),
            True)  # assert entropy
        # assert all of them not equal becaues of MC Dropout
        self.assertEqual(
            np.all(
                bneuralnet.evaluate(xdata, ydata) != bneuralnet.evaluate(
                    xdata, ydata)), True)
        jacobian = bneuralnet.jacobian(xdata[:2], mean_output=True)
        np.testing.assert_array_equal(prediction.shape, ydata.shape)
        bneuralnet.save(name='apogee_bcnn')
        bneuralnet.train_on_batch(xdata[:64],
                                  ydata[:64])  # single batch fine-tuning test

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(xdata)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 5
        bneuralnet_loaded.callbacks = ErrorOnNaN()
        bneuralnet_loaded.train(xdata, ydata)
Exemple #13
0
    def test_apogee_bcnn(self):
        """
        Test ApogeeBCNN models
        - training, testing, evaluation
        - Apogee plotting functions
        """
        # Data preparation
        random_xdata = np.random.normal(0, 1, (200, 7514))
        random_ydata = np.random.normal(0, 1, (200, 7))

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        # deliberately chosen targetname to test targetname conversion too
        bneuralnet.targetname = [
            'teff', 'logg', 'M', 'alpha', 'C1', 'Ti', 'Ti2'
        ]

        bneuralnet.max_epochs = 1  # for quick result
        bneuralnet.callbacks = ErrorOnNaN(
        )  # Raise error and fail the test if Nan
        bneuralnet.train(random_xdata, random_ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape
        # prevent memory issue on Tavis CI so set mc_num=2
        bneuralnet.mc_num = 2
        prediction, prediction_err = bneuralnet.test(random_xdata)
        # assert all of them not equal becaues of MC Dropout
        self.assertEqual(
            np.all(
                bneuralnet.evaluate(random_xdata, random_ydata) !=
                bneuralnet.evaluate(random_xdata, random_ydata)), True)
        jacobian = bneuralnet.jacobian(random_xdata[:2], mean_output=True)
        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        bneuralnet.save(name='apogee_bcnn')
        bneuralnet.train_on_batch(
            random_xdata, random_ydata)  # single batch fine-tuning test

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(random_xdata)
        bneuralnet_loaded.aspcap_residue_plot(pred, pred, pred_err['total'])
        bneuralnet_loaded.jacobian_aspcap(jacobian)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 1
        bneuralnet_loaded.callbacks = ErrorOnNaN()
        bneuralnet_loaded.train(random_xdata, random_ydata)
        pred, pred_err = bneuralnet_loaded.test_old(random_xdata)
Exemple #14
0
    def test_bayesian_binary_mnist(self):
        # Create a astroNN neural network instance and set the basic parameter
        net = MNIST_BCNN()
        net.task = 'binary_classification'
        net.callbacks = ErrorOnNaN()
        net.max_epochs = 1
        net.train(x_train, y_train)
        pred, pred_err = net.test(x_test)
        test_num = y_test.shape[0]

        net.save('mnist_binary_bcnn_test')
        net_reloaded = load_folder("mnist_binary_bcnn_test")
        net_reloaded.mc_num = 3
        prediction_loaded, prediction_loaded_err = net_reloaded.test(x_test)
Exemple #15
0
    def test_bayesian_binary_mnist(self):
        # Create a astroNN neural network instance and set the basic parameter
        net = MNIST_BCNN()
        net.task = 'binary_classification'
        net.callbacks = ErrorOnNaN()
        net.max_epochs = 1  # Just use 1 epochs for quick result

        # Train the neural network
        net.train(self.x_train[:200], self.y_train[:200])

        net.save('mnist_binary_bcnn_test')
        net_reloaded = load_folder("mnist_binary_bcnn_test")
        net_reloaded.mc_num = 3  # prevent memory issue on Tavis CI
        prediction_loaded = net_reloaded.test(self.x_test[:200])
Exemple #16
0
    def test_custom_model(self):
        import shutil
        import os
        import astroNN
        from astroNN.config import config_path

        test_config_path = os.path.join(os.path.dirname(astroNN.__path__[0]),
                                        'tests', 'config.ini')
        astroNN_config_path = config_path()
        if os.path.exists(astroNN_config_path):
            os.remove(astroNN_config_path)
        shutil.copy(test_config_path, astroNN_config_path)

        test_modelsource_path = os.path.join(
            os.path.dirname(astroNN.__path__[0]), 'tests', 'custom_model',
            'custom_models.py')
        shutil.copy(
            test_modelsource_path,
            os.path.join('/home/travis/build/henrysky', 'custom_models.py'))

        import sys
        from importlib import import_module
        head, tail = os.path.split(test_modelsource_path)

        sys.path.insert(0, head)
        CustomModel_Test = getattr(import_module(tail.strip('.py')),
                                   str('CustomModel_Test'))

        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = np_utils.to_categorical(y_train, 10)

        # To convert to desirable type
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)
        y_train = y_train.astype(np.float32)

        # create model instance
        custom_model = CustomModel_Test()
        custom_model.max_epochs = 1

        custom_model.train(x_train[:1000], y_train[:1000])
        prediction = custom_model.test(x_test[:1000])

        custom_model.save('custom_model_testing_folder')

        custom_model_loaded = load_folder("custom_model_testing_folder")
        prediction_loaded = custom_model_loaded.test(x_test[:1000])
        # CustomModel_Test is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)
    def test_apogee_bcnn(self):
        random_xdata = np.random.normal(0, 1, (200, 7514))
        random_ydata = np.random.normal(0, 1, (200, 7))

        # ApogeeBCNN
        print("======ApogeeBCNN======")
        bneuralnet = ApogeeBCNN()
        bneuralnet.targetname = [
            'teff', 'logg', 'M', 'alpha', 'C1', 'Ti', 'Ti2'
        ]

        bneuralnet.max_epochs = 1
        bneuralnet.callbacks = ErrorOnNaN()
        bneuralnet.train(random_xdata, random_ydata)
        output_shape = bneuralnet.output_shape
        input_shape = bneuralnet.input_shape
        # prevent memory issue on Tavis CI
        bneuralnet.mc_num = 3
        prediction, prediction_err = bneuralnet.test(random_xdata)

        print(bneuralnet.evaluate(random_xdata, random_ydata))

        bneuralnet.plot_dense_stats()
        bneuralnet.plot_model()
        jacobian = bneuralnet.jacobian(random_xdata[:3], mean_output=True)

        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        bneuralnet.save(name='apogee_bcnn')

        # just to make sure it can load it back without error
        bneuralnet_loaded = load_folder("apogee_bcnn")
        bneuralnet_loaded.plot_dense_stats()
        bneuralnet_loaded.callbacks = ErrorOnNaN()

        # prevent memory issue on Tavis CI
        bneuralnet_loaded.mc_num = 2
        pred, pred_err = bneuralnet_loaded.test(random_xdata)
        bneuralnet_loaded.aspcap_residue_plot(pred, pred, pred_err['total'])
        bneuralnet_loaded.jacobian_aspcap(jacobian)
        bneuralnet_loaded.save()

        # Fine-tuning test
        bneuralnet_loaded.max_epochs = 1
        bneuralnet_loaded.train(random_xdata, random_ydata)
        pred, pred_err = bneuralnet_loaded.test_old(random_xdata)
    def test_apogee_bcnnconsered(self):
        random_xdata = np.random.normal(0, 1, (200, 7514))

        # ApogeeBCNNCensored
        print("======ApogeeBCNNCensored======")
        bneuralnetcensored = ApogeeBCNNCensored()
        datalen = len(bneuralnetcensored.targetname)
        random_ydata = np.random.normal(0, 1, (200, datalen))

        bneuralnetcensored.max_epochs = 1
        bneuralnetcensored.callbacks = ErrorOnNaN()
        bneuralnetcensored.train(random_xdata, random_ydata)
        output_shape = bneuralnetcensored.output_shape
        input_shape = bneuralnetcensored.input_shape
        # prevent memory issue on Tavis CI
        bneuralnetcensored.mc_num = 3
        prediction, prediction_err = bneuralnetcensored.test(random_xdata)
        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        bneuralnetcensored.save(name='apogee_bcnncensored')
        bneuralnetcensored_loaded = load_folder("apogee_bcnncensored")
Exemple #19
0
    def test_bayesian_binary_mnist(self):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        y_train = utils.to_categorical(y_train, 10)
        y_train = y_train.astype(np.float32)
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)

        # Create a astroNN neural network instance and set the basic parameter
        net = MNIST_BCNN()
        net.task = 'binary_classification'
        net.callbacks = ErrorOnNaN()
        net.max_epochs = 1  # Just use 5 epochs for quick result

        # Trian the nerual network
        net.train(x_train[:200], y_train[:200])

        net.save('mnist_binary_bcnn_test')
        net_reloaded = load_folder("mnist_binary_bcnn_test")
        net_reloaded.mc_num = 3  # prevent memory issue on Tavis CI
        prediction_loaded = net_reloaded.test(x_test[:200])
    def test_ApogeeKplerEchelle(self):
        """
        Test ApogeeKplerEchelle models
        - training, testing
        """
        # Data preparation, keep the data size large (>800 data points to prevent issues)
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        y_train = utils.to_categorical(y_train, 10)
        y_test = utils.to_categorical(y_test, 10)
        # To convert to desirable type
        y_train = y_train.astype(np.float32)
        y_test = y_test.astype(np.float32)
        x_train = x_train.astype(np.float32)
        x_test = x_test.astype(np.float32)

        print("======ApogeeKplerEchelle======")
        apokasc_nn = ApogeeKplerEchelle()
        apokasc_nn.max_epochs = 2
        apokasc_nn.dropout_rate = 0.
        apokasc_nn.input_norm_mode = {'input': 255, 'aux': 0}
        apokasc_nn.labels_norm_mode = 0
        apokasc_nn.task = 'classification'
        apokasc_nn.callbacks = ErrorOnNaN()
        apokasc_nn.train({
            'input': x_train,
            'aux': y_train
        }, {'output': y_train})
        prediction = apokasc_nn.test({'input': x_train, 'aux': y_train})
        # we ave the answer as aux input so the prediction should be near perfect
        total_num = y_train.shape[0]
        assert np.sum((prediction > 0.5) == (y_train > 0.5)) > total_num * 0.99
        apokasc_nn.save(name='apokasc_nn')

        apokasc_nn_reloaded = load_folder('apokasc_nn')
        prediction_reloaded = apokasc_nn_reloaded.test({
            'input': x_train,
            'aux': y_train
        })
        np.testing.assert_array_equal(prediction, prediction_reloaded)
    def test_starnet2017(self):
        # StarNet2017
        print("======StarNet2017======")
        starnet2017 = StarNet2017()
        starnet2017.max_epochs = 1
        starnet2017.callbacks = ErrorOnNaN()
        starnet2017.train(random_xdata, random_ydata)
        prediction = starnet2017.test(random_xdata)
        jacobian = starnet2017.jacobian(random_xdata[:10])

        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        np.testing.assert_array_equal(jacobian.shape, [random_xdata[:10].shape[0], random_ydata.shape[1],
                                                       random_xdata.shape[1]])
        starnet2017.save(name='starnet2017')

        starnet2017_loaded = load_folder("starnet2017")
        prediction_loaded = starnet2017_loaded.test(random_xdata)
        # StarNet2017 is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # Fine-tuning test
        starnet2017_loaded.max_epochs = 1
        starnet2017.callbacks = ErrorOnNaN()
        starnet2017_loaded.train(random_xdata, random_ydata)
Exemple #22
0
if os.path.exists(astronn_dist_f):
    raise FileExistsError(f"{astronn_dist_f} already existed")
if os.path.exists(astronn_ages_f):
    raise FileExistsError(f"{astronn_ages_f} already existed")

allstar_data = fits.getdata(allstar_path)

allspec_f = fits.open(contspac_file_name)
all_spec = allspec_f[0].data
bad_spec_idx = (~np.array(allspec_f[1].data, bool)
                | bitmask_boolean(allstar_data["STARFLAG"],
                                  target_bit=[0, 3, 4]))[0]

# ====================================== Abundances ====================================== #

net = load_folder(astronn_chem_model)

pred, pred_error = net.predict_dataset(fits.getdata(contspac_file_name))

# some spectra are all zeros, set prediction for those spectra to np.nan
pred[bad_spec_idx] = np.nan
pred_error["total"][bad_spec_idx] = np.nan

# deal with infinite error issue, set them to np.nan
inf_err_idx = np.array([pred_error["total"] == np.inf])[0]
pred[inf_err_idx] = np.nan
pred[bad_spec_idx] = np.nan
pred_error["total"][inf_err_idx] = np.nan
pred_error["total"][bad_spec_idx] = np.nan

# save a fits
Exemple #23
0
    def test_apogee_cnn(self):
        """
        Test ApogeeCNN models
        - training, testing, evaluation
        - basic astroNN model method
        """
        print("======ApogeeCNN======")

        # Data preparation
        random_xdata = np.random.normal(0, 1, (200, 1024))
        random_ydata = np.random.normal(0, 1, (200, 2))

        # setup model instance
        neuralnet = ApogeeCNN()
        print(neuralnet)
        # assert no model before training
        self.assertEqual(neuralnet.has_model, False)
        neuralnet.max_epochs = 1  # for quick result
        neuralnet.callbacks = ErrorOnNaN()  # Raise error and fail the test if Nan
        neuralnet.train(random_xdata, random_ydata)  # training
        self.assertEqual(neuralnet.uses_learning_phase, True)  # Assert ApogeeCNN uses learning phase (bc of Dropout)

        # test basic astroNN model method
        neuralnet.get_weights()
        neuralnet.summary()
        output_shape = neuralnet.output_shape
        input_shape = neuralnet.input_shape
        neuralnet.get_config()
        neuralnet.save_weights('save_weights_test.h5')  # save astroNN weight only
        neuralnet.plot_dense_stats()
        neuralnet.plot_model()

        prediction = neuralnet.test(random_xdata)
        jacobian = neuralnet.jacobian(random_xdata[:2])
        hessian = neuralnet.hessian_diag(random_xdata[:2])
        hessian_full_approx = neuralnet.hessian(random_xdata[:2], method='approx')
        hessian_full_exact = neuralnet.hessian(random_xdata[:2], method='exact')

        #  make sure raised if data dimension not as expected
        self.assertRaises(ValueError, neuralnet.jacobian, np.atleast_3d(random_xdata[:3]))
        # make sure evaluate run in testing phase instead of learning phase
        # ie no Dropout which makes model deterministic
        self.assertEqual(
            np.all(neuralnet.evaluate(random_xdata, random_ydata) == neuralnet.evaluate(random_xdata, random_ydata)),
            True)

        # assert shape correct as expected
        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        np.testing.assert_array_equal(jacobian.shape, [random_xdata[:2].shape[0], random_ydata.shape[1],
                                                       random_xdata.shape[1]])
        np.testing.assert_array_equal(hessian.shape, [random_xdata[:2].shape[0], random_ydata.shape[1],
                                                      random_xdata.shape[1]])
        # hessian approx and exact result should have the same shape
        np.testing.assert_array_equal(hessian_full_approx.shape, hessian_full_exact.shape)

        # save weight and model again
        neuralnet.save(name='apogee_cnn')
        neuralnet.save_weights('save_weights_test.h5')

        # load the model again
        neuralnet_loaded = load_folder("apogee_cnn")
        neuralnet_loaded.plot_dense_stats()
        # assert has model without training because this is a trained model
        self.assertEqual(neuralnet_loaded.has_model, True)
        # fine tune test
        prediction_loaded = neuralnet_loaded.test(random_xdata)

        # ApogeeCNN is deterministic check again
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # Fine tuning test
        neuralnet_loaded.max_epochs = 1
        neuralnet_loaded.callbacks = ErrorOnNaN()
        neuralnet_loaded.train(random_xdata, random_ydata)
        prediction_loaded = neuralnet_loaded.test(random_xdata)

        # prediction should not be equal after fine-tuning
        self.assertRaises(AssertionError, np.testing.assert_array_equal, prediction, prediction_loaded)
    def test_apogee_cnn(self):
        """
        Test ApogeeCNN models
        - training, testing, evaluation
        - basic astroNN model method
        """
        print("======ApogeeCNN======")

        # setup model instance
        neuralnet = ApogeeCNN()
        print(neuralnet)
        # assert no model before training
        self.assertEqual(neuralnet.has_model, False)
        neuralnet.max_epochs = 5  # for quick result
        neuralnet.callbacks = ErrorOnNaN(
        )  # Raise error and fail the test if Nan
        neuralnet.targetname = ['logg', 'feh']
        neuralnet.train(xdata, ydata)  # training
        neuralnet.train_on_batch(xdata[:64],
                                 ydata[:64])  # single batch fine-tuning test
        # self.assertEqual(neuralnet.uses_learning_phase, True)  # Assert ApogeeCNN uses learning phase (bc of Dropout)

        # test basic astroNN model method
        neuralnet.get_weights()
        neuralnet.summary()
        output_shape = neuralnet.output_shape
        input_shape = neuralnet.input_shape
        neuralnet.get_config()
        neuralnet.save_weights(
            'save_weights_test.h5')  # save astroNN weight only
        neuralnet.plot_dense_stats()
        neuralnet.plot_model()

        prediction = neuralnet.test(xdata)
        mape = np.median(
            np.abs(prediction[neuralnet.val_idx] - ydata[neuralnet.val_idx]) /
            ydata[neuralnet.val_idx],
            axis=0)
        self.assertEqual(np.all(0.15 > mape),
                         True)  # assert less than 15% error
        jacobian = neuralnet.jacobian(xdata[:5])
        # assert shape correct as expected
        np.testing.assert_array_equal(prediction.shape, ydata.shape)
        np.testing.assert_array_equal(
            jacobian.shape,
            [xdata[:5].shape[0], ydata.shape[1], xdata.shape[1]])

        hessian = neuralnet.hessian(xdata[:5], mean_output=True)
        np.testing.assert_array_equal(
            hessian.shape, [ydata.shape[1], xdata.shape[1], xdata.shape[1]])

        # make sure raised if data dimension not as expected
        self.assertRaises(ValueError, neuralnet.jacobian,
                          np.atleast_3d(xdata[:3]))
        # make sure evaluate run in testing phase instead of learning phase
        # ie no Dropout which makes model deterministic
        self.assertEqual(
            np.all(
                neuralnet.evaluate(xdata, ydata) == neuralnet.evaluate(
                    xdata, ydata)), True)

        # save weight and model again
        neuralnet.save(name='apogee_cnn')
        neuralnet.save_weights('save_weights_test.h5')

        # load the model again
        neuralnet_loaded = load_folder("apogee_cnn")
        neuralnet_loaded.plot_dense_stats()
        # assert has model without training because this is a trained model
        self.assertEqual(neuralnet_loaded.has_model, True)
        # fine tune test
        prediction_loaded = neuralnet_loaded.test(xdata)

        # ApogeeCNN is deterministic check again
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # Fine tuning test
        neuralnet_loaded.max_epochs = 5
        neuralnet_loaded.callbacks = ErrorOnNaN()
        neuralnet_loaded.train(xdata, ydata)
        prediction_loaded = neuralnet_loaded.test(xdata[neuralnet.val_idx])

        # prediction should not be equal after fine-tuning
        self.assertRaises(AssertionError, np.testing.assert_array_equal,
                          prediction, prediction_loaded)
Exemple #25
0
    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)
    def test_apogee_cnn(self):
        # Data preparation, keep the data size large (>800 data points to prevent issues)
        random_xdata = np.random.normal(0, 1, (200, 1024))
        random_ydata = np.random.normal(0, 1, (200, 2))

        # ApogeeCNN
        print("======ApogeeCNN======")
        neuralnet = ApogeeCNN()
        self.assertEqual(neuralnet.has_model, False)
        neuralnet.max_epochs = 1
        neuralnet.callbacks = ErrorOnNaN()
        neuralnet.train(random_xdata, random_ydata)
        self.assertEqual(neuralnet.uses_learning_phase, True)
        neuralnet.get_weights()
        neuralnet.get_config()
        neuralnet.save_weights('save_weights_test.h5')
        neuralnet.summary()
        output_shape = neuralnet.output_shape
        input_shape = neuralnet.input_shape
        prediction = neuralnet.test(random_xdata)
        jacobian = neuralnet.jacobian(random_xdata[:3])
        hessian = neuralnet.hessian_diag(random_xdata[:2])
        hessian_full_approx = neuralnet.hessian(random_xdata[:2],
                                                method='approx')
        hessian_full_exact = neuralnet.hessian(random_xdata[:2],
                                               method='exact')

        self.assertRaises(ValueError, neuralnet.jacobian,
                          np.atleast_3d(random_xdata[:3]))
        # make sure evaluate run in testing phase instead of learning phase
        # ie no Dropout which makes model deterministic
        self.assertEqual(
            np.all(
                neuralnet.evaluate(random_xdata, random_ydata) ==
                neuralnet.evaluate(random_xdata, random_ydata)), True)

        np.testing.assert_array_equal(prediction.shape, random_ydata.shape)
        np.testing.assert_array_equal(jacobian.shape, [
            random_xdata[:3].shape[0], random_ydata.shape[1],
            random_xdata.shape[1]
        ])
        np.testing.assert_array_equal(hessian.shape, [
            random_xdata[:2].shape[0], random_ydata.shape[1],
            random_xdata.shape[1]
        ])
        neuralnet.save(name='apogee_cnn')
        neuralnet.save_weights('save_weights_test.h5')

        neuralnet_loaded = load_folder("apogee_cnn")
        self.assertEqual(neuralnet_loaded.has_model, True)
        neuralnet_loaded.max_epochs = 1
        neuralnet_loaded.callbacks = ErrorOnNaN()
        prediction_loaded = neuralnet_loaded.test(random_xdata)

        # Apogee_CNN is deterministic
        np.testing.assert_array_equal(prediction, prediction_loaded)

        # Fine tuning test
        neuralnet_loaded.train(random_xdata, random_ydata)
        prediction_loaded = neuralnet_loaded.test(random_xdata)
        # prediction should not be equal after fine-tuning
        self.assertRaises(AssertionError, np.testing.assert_array_equal,
                          prediction, prediction_loaded)