This page was generated from examples/setup.ipynb.

Setup

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.

Getting the Grid

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.

As a convenience, we provide a helper to download PHOENIX models from the Goettingen servers. Note this will skip any files already on disk.

[1]:
import numpy as np

from Starfish.grid_tools import download_PHOENIX_models

ranges = [[5700, 8600], [4.0, 6.0], [-0.5, 0.5]]  # T, logg, Z

download_PHOENIX_models(path="PHOENIX", ranges=ranges)
lte08600-6.00+0.5.PHOENIX-ACES-AGSS-COND-2011-HiRes.fits: 100%|██████████| 330/330 [00:00<00:00, 1285.27it/s]

Now that we have the files downloaded, let’s set up a grid interface

[2]:
from Starfish.grid_tools import PHOENIXGridInterfaceNoAlpha

grid = PHOENIXGridInterfaceNoAlpha(path="PHOENIX")

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.

Setting up the HDF5 Interface

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.

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

\[\Gamma = \frac{c}{R}\]

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.

[3]:
from Starfish.grid_tools.instruments import SPEX
from Starfish.grid_tools import HDF5Creator

creator = HDF5Creator(
    grid, "F_SPEX_grid.hdf5", instrument=SPEX(), wl_range=(0.9e4, np.inf), ranges=ranges
)
creator.process_grid()
Processing [8.6e+03 6.0e+00 5.0e-01]: 100%|██████████| 330/330 [06:33<00:00,  1.19s/it]

Setting up the Spectral Emulator

Once we have our pre-processed grid, we can make our spectral emulator and train its Gaussian process hyperparameters.

[4]:
from Starfish.emulator import Emulator

# can load from string or HDF5Interface
emu = Emulator.from_grid("F_SPEX_grid.hdf5")
emu
[4]:
Emulator
--------
Trained: False
lambda_xi: 1.000
Variances:
        10000.00
        10000.00
        10000.00
        10000.00
Lengthscales:
        [ 600.00  1.50  1.50 ]
        [ 600.00  1.50  1.50 ]
        [ 600.00  1.50  1.50 ]
        [ 600.00  1.50  1.50 ]
Log Likelihood: -1272.34
[5]:
%time emu.train(options=dict(maxiter=1e5))
emu
CPU times: user 17min 39s, sys: 1min 58s, total: 19min 38s
Wall time: 4min 55s
[5]:
Emulator
--------
Trained: True
lambda_xi: 1.010
Variances:
        176330.09
        1681.55
        1364.83
        433.88
Lengthscales:
        [ 2039.21  16.11  3.21 ]
        [ 1313.27  1.49  1.86 ]
        [ 2122.93  2.58  2.21 ]
        [ 1009.99  1.20  3.26 ]
Log Likelihood: -778.44

Note: If the emulator does not optimize the first time you use train, just run it again. You can also tweak the arguments passed to scipy.optimize.minimize by passing them as keyword arguments to the call.

Warning: Training the emulator will take on the order of minutes to complete. The more eigenspectra that are used as well as the resolution of the spectrograph will mainly dominate this runtime.

We can do a sanity check on the optimization by looking at slice of the emulator’s parameter space and the corresponding Gaussian process fit. We should see a smooth line connecting all the parameter values with some uncertainty that grows with large gaps or turbulent weights.

[6]:
%matplotlib inline
from Starfish.emulator.plotting import plot_emulator

plot_emulator(emu)
../_images/examples_setup_11_0.png

If we are satisfied, let’s save this emulator and move on to fitting some data.

[7]:
emu.save("F_SPEX_emu.hdf5")