{ "cells": [ { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Setup\n", "\n", "Here I will go over setting up our interfaces and emulators from a raw spectral library to prepare us for fitting some data in further examples." ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false", "colab_type": "text", "id": "spgydwe4FY9j" }, "source": [ "### Getting the Grid\n", "\n", "To begin, we need a spectral model library that we will use for our fitting. One common example are the PHOENIX models, most recently computed by T.O. Husser. We provide many interfaces directly with different libraries, which can be viewed in [Raw Grid Interfaces](../api/grid_tools.rst#raw-grid-interfaces).\n", "\n", "As a convenience, we provide a helper to download PHOENIX models from the Goettingen servers. Note this will skip any files already on disk." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "Collapsed": "false", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "colab_type": "code", "id": "BuJfe4XyERQo", "outputId": "75d34e98-cff9-4c24-8551-3a8be4c66a0c" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "lte08600-6.00+0.5.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits: 100%|██████████| 330/330 [00:00<00:00, 1285.27it/s]\n" ] } ], "source": [ "import numpy as np\n", "\n", "from Starfish.grid_tools import download_PHOENIX_models\n", "\n", "ranges = [[5700, 8600], [4.0, 6.0], [-0.5, 0.5]] # T, logg, Z\n", "\n", "download_PHOENIX_models(path=\"PHOENIX\", ranges=ranges)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false", "colab_type": "text", "id": "O76RgrCVGILs" }, "source": [ "Now that we have the files downloaded, let's set up a grid interface" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "Collapsed": "false", "colab": {}, "colab_type": "code", "id": "TJtk9ecYEMBe" }, "outputs": [], "source": [ "from Starfish.grid_tools import PHOENIXGridInterfaceNoAlpha\n", "\n", "grid = PHOENIXGridInterfaceNoAlpha(path=\"PHOENIX\")" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false", "colab_type": "text", "id": "zOywmJdgGRU2" }, "source": [ "From here, we will want to set up our HDF5 interface that will allow us to go on to using the spectral emulator, but first we need to determine our model subset and instrument.\n", "\n", "### Setting up the HDF5 Interface\n", "\n", "We set up an HDF5 interface in order to allow much quicker reading and writing than compared to loading FITS files over and over again. In addition, when considering the application to our likelihood methods, we know that for a given dataset, any effects characteristic of the instrument can be pre-applied to our models, saving on computation time during the maximum likelihood estimation.\n", "\n", "Looking towards our fitting examples, we know we will try fitting some data from TRES Spectrograph. This instrument is available in our grid tools, but if yours isn't, you can always supply the FWHM in km/s. The FWHM ($\\Gamma$) can be found using the resolving power, $R$\n", "\n", "$$ \\Gamma = \\frac{c}{R} $$\n", "\n", "with $c$ in km/s. Let’s also say that, for a given dataset, we want to only use a reasonable subset of our original model grid. The data provided in future examples is a ~F3V star, so we will limit our model parameter ranges appropriately." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "Collapsed": "false", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "colab_type": "code", "id": "DiUf8DK8E46W", "outputId": "3c61ea76-561a-4b9d-c7a9-762e6da56fe5" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Processing [8.6e+03 6.0e+00 5.0e-01]: 100%|██████████| 330/330 [06:33<00:00, 1.19s/it] \n" ] } ], "source": [ "from Starfish.grid_tools.instruments import SPEX\n", "from Starfish.grid_tools import HDF5Creator\n", "\n", "creator = HDF5Creator(\n", " grid, \"F_SPEX_grid.hdf5\", instrument=SPEX(), wl_range=(0.9e4, np.inf), ranges=ranges\n", ")\n", "creator.process_grid()" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false", "colab_type": "text", "id": "t02wYIl-kGMA" }, "source": [ "### Setting up the Spectral Emulator\n", "\n", "Once we have our pre-processed grid, we can make our spectral emulator and train its Gaussian process hyperparameters." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "Collapsed": "false", "colab": { "base_uri": "https://localhost:8080/", "height": 382 }, "colab_type": "code", "id": "5QUlomkCkNuj", "outputId": "3540ebf9-0c43-4ab4-daf6-71faf5c76491" }, "outputs": [ { "data": { "text/plain": [ "Emulator\n", "--------\n", "Trained: False\n", "lambda_xi: 1.000\n", "Variances:\n", "\t10000.00\n", "\t10000.00\n", "\t10000.00\n", "\t10000.00\n", "Lengthscales:\n", "\t[ 600.00 1.50 1.50 ]\n", "\t[ 600.00 1.50 1.50 ]\n", "\t[ 600.00 1.50 1.50 ]\n", "\t[ 600.00 1.50 1.50 ]\n", "Log Likelihood: -1272.34" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from Starfish.emulator import Emulator\n", "\n", "# can load from string or HDF5Interface\n", "emu = Emulator.from_grid(\"F_SPEX_grid.hdf5\")\n", "emu" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "Collapsed": "false", "colab": {}, "colab_type": "code", "id": "Udo3krt7kV85" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 17min 39s, sys: 1min 58s, total: 19min 38s\n", "Wall time: 4min 55s\n" ] }, { "data": { "text/plain": [ "Emulator\n", "--------\n", "Trained: True\n", "lambda_xi: 1.010\n", "Variances:\n", "\t176330.09\n", "\t1681.55\n", "\t1364.83\n", "\t433.88\n", "Lengthscales:\n", "\t[ 2039.21 16.11 3.21 ]\n", "\t[ 1313.27 1.49 1.86 ]\n", "\t[ 2122.93 2.58 2.21 ]\n", "\t[ 1009.99 1.20 3.26 ]\n", "Log Likelihood: -778.44" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time emu.train(options=dict(maxiter=1e5))\n", "emu" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false", "colab": {}, "colab_type": "code", "id": "2JDpJSR_n2DG" }, "source": [ "