SOM ‐ Tutorial - danecross/SOM-photoz-BFD GitHub Wiki
This class creates a wrapper for the NoiseSOM class, which has optimized SOM training for the purposes of determining photometric redshift of galaxies. With this class, one can train and validate SOMs for both deep- and wide-field galaxies. This tutorial will cover how to make a single SOM instance, but for Photo-Z analyses the user must make two separate instances.
Note on Training Data: The data the SOM is trained on should be a randomly selected sub-catalog of the full catalog. Usually 1e5-1e6 galaxies is enough to effectively train the SOM. For the training to work, this subcatalog should be a astropy fits table with columns names as specified here.
Import Statement
from SOM import *
This imports the whole SOM class as well as the load_SOM function.
Creating a SOM Instance
For this example, we will make a SOM with resolution of 10x10 cells.
som = SOM(10, 'path/to/training/data.fits')
With this line, a SOM has been initialized. This initialization loads the training data, as well as making a NoiseSOM instance.
Note on table headers: the name of the flux and flux error/covariance columns will change between surveys. Also the use of covariances versus errors will change. In this vein, the user can alter the format of the column names and whether to use error or covariances. See the
col_fmt,cov_fmt, anderr_fmtarguments in the constructor documentation for further information.
The user can check that it has initialized correctly as follows:
print(som.train_sample)
Should print a table object of the training sample.
Initializing the SOM so as to save the trained model
If the user wants to avoid having to train the model every time they need to use it, the user can add an optional argument analysis_output_path to the initialization argument as follows:
som = SOM(10, 'path/to/training/data.fits', analysis_output_path='analysis/output/path/')
When a SOM is initialized like this, when the user runs the training step it will automatically save the trained model to the specified path.
Training a SOM instance
Since the training sample has already been loaded, training the model is as simple as:
som.train()
Loading the Trained Sample
If the user has already trained and saved the SOM, the NoiseSOM object will have been saved in the analysis_output_path. To load the NoiseSOM model, the user can write:
som.load()
And the SOM object can be used to classify as normal.
A full example of training and loading is as follows:
som = SOM(10, 'path/to/training/data.fits', analysis_output_path='analysis/output/path/')
som.train()
Once train has run, in a new session, the user can run:
som = SOM(10, 'path/to/training/data.fits', analysis_output_path='analysis/output/path/')
som.load()
Note that this loading process is different from the save/load process below. This process refers solely to the save/load procedures for the interface between the SOM and NoiseSOM classes. Below details how to save the full SOM object.
Saving and Reloading a SOM
In order to save a SOM in its current state, run:
som.save("path/to/save/SOM.pkl")
And to load:
som = load_SOM("path/to/save/SOM.pkl")