Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: How to construct config files #454

Merged
merged 79 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
f390eda
Add section for creating config files to docs
Lucia-Fonseca Mar 23, 2021
273ccec
Add guide for variables
Lucia-Fonseca Mar 23, 2021
832c6f5
Add guide for functions
Lucia-Fonseca Mar 23, 2021
5da3aa9
Add guide for tables and columns
Lucia-Fonseca Mar 23, 2021
7d0ec30
Fix typo
Lucia-Fonseca Mar 23, 2021
bc2945f
Add guide for cosmology
Lucia-Fonseca Mar 23, 2021
1f12a63
Add walkthrough example
Lucia-Fonseca Mar 23, 2021
f806f84
Change example for variables
Lucia-Fonseca Mar 23, 2021
2e65b25
Change example for functions
Lucia-Fonseca Mar 23, 2021
6cadf83
Change example for tables and columns
Lucia-Fonseca Mar 23, 2021
13ea9ea
Change example for multicomlumn assignment
Lucia-Fonseca Mar 23, 2021
57ea54d
Add example for .ini and depends
Lucia-Fonseca Mar 23, 2021
b28a40d
Add description for init and complete
Lucia-Fonseca Mar 25, 2021
ba8b27a
Fix typos
Lucia-Fonseca Mar 25, 2021
8a4cb0e
Fix name of halo function
Lucia-Fonseca Mar 29, 2021
38bf2e7
Change style for walkthrough example
Lucia-Fonseca Mar 29, 2021
44b3c8a
Fix SkyPy example
Lucia-Fonseca Apr 6, 2021
32fae72
Rename file
Lucia-Fonseca Apr 13, 2021
c1cc929
Fix rename file extension
Lucia-Fonseca Apr 13, 2021
f3b542b
Rename config file section on main doc page
Lucia-Fonseca Apr 13, 2021
dd28a23
Edit for positive wording :)
Lucia-Fonseca Apr 13, 2021
b07dba3
Change sections order to show example first
Lucia-Fonseca Apr 13, 2021
ea06960
Fix typos
Lucia-Fonseca Apr 13, 2021
4afdec0
Edit wording in example section as it now comes first
Lucia-Fonseca Apr 13, 2021
48a12f1
Change to active wording
Lucia-Fonseca Apr 13, 2021
76299a2
Add example plot
Lucia-Fonseca Apr 19, 2021
e71cfde
Commen on running pipeline from command line
Lucia-Fonseca Apr 22, 2021
f2f9c39
Correct variable type
Lucia-Fonseca Apr 22, 2021
7058478
Move variable referencing to the functions section
Lucia-Fonseca Apr 22, 2021
41543b8
Fix functions example
Lucia-Fonseca Apr 22, 2021
4b92db7
Delete duplicated line
Lucia-Fonseca Apr 22, 2021
138de05
Fix cosmic voids example
Lucia-Fonseca Apr 22, 2021
756e4c8
Fix cosmic voids example variance
Lucia-Fonseca Apr 22, 2021
5608612
Fix circular velocity example
Lucia-Fonseca Apr 22, 2021
8c72853
Fix typo
Lucia-Fonseca Apr 22, 2021
f42f226
Restructure text around table referencing
Lucia-Fonseca Apr 27, 2021
12498f1
Set up other cosmologues section
Lucia-Fonseca Apr 27, 2021
c6ccf18
Use numpy functions in abundance matching example
Lucia-Fonseca Apr 27, 2021
5bc5a56
Add clone example in cosmology section
Lucia-Fonseca Apr 27, 2021
d8b0065
Update config style with new import functionality
Lucia-Fonseca Apr 27, 2021
eeec851
Fix typ for Planck13 cosmology
Lucia-Fonseca May 12, 2021
d7d4f0e
Delete how-not-to-do example
Lucia-Fonseca May 12, 2021
94c7160
Fix plot rendering
Lucia-Fonseca May 12, 2021
306779a
Merge branch 'main' into yaml
Lucia-Fonseca May 12, 2021
c3f9c27
Fix typo argument cosmology
Lucia-Fonseca May 13, 2021
0a81051
Merge branch 'yaml' of https://github.com/Lucia-Fonseca/skypy into yaml
Lucia-Fonseca May 13, 2021
75dc98e
Fix typo init and depends
Lucia-Fonseca May 13, 2021
b71aa56
Fix reference table example
Lucia-Fonseca May 13, 2021
4d121ee
Fix argument default cosmology
Lucia-Fonseca May 14, 2021
b261642
Fix header
Lucia-Fonseca May 14, 2021
b6d9dd2
Change to a smaller sky area
Lucia-Fonseca May 14, 2021
a0607ac
Change and explain the command line option
Lucia-Fonseca May 14, 2021
c9857e9
Decrease sky area in the luminosity config file
Lucia-Fonseca May 14, 2021
a75a57d
Fix telescope example
Lucia-Fonseca May 14, 2021
e674976
Fix telescope example 2
Lucia-Fonseca May 14, 2021
88bd0bc
Fix telescope example to have coumns with same length
Lucia-Fonseca May 14, 2021
abd98de
Fix RuntimeWarning for the circular velocities examples
Lucia-Fonseca May 14, 2021
3459113
Fix indentation problem in luminosity pipeline
Lucia-Fonseca May 14, 2021
528ff21
Place import section as a second bullet point under variables
Lucia-Fonseca May 14, 2021
6f6f61b
Add placeholder for parameters section
Lucia-Fonseca May 14, 2021
1d2f201
Add cosmology bullet point in the functions section
Lucia-Fonseca May 14, 2021
4e163a3
Add temporary example for the cosmology feature
Lucia-Fonseca May 14, 2021
7d515d7
Delete blanck lines
Lucia-Fonseca May 14, 2021
19d2ff8
Change example or column reference
Lucia-Fonseca May 20, 2021
91550b1
Edit multicolumn assignement and change examples
Lucia-Fonseca May 20, 2021
e0404c6
Add .depends under functions and include example
Lucia-Fonseca May 20, 2021
7164141
Add table initialisation section and example
Lucia-Fonseca May 20, 2021
699f35d
Edit table reference setion
Lucia-Fonseca May 20, 2021
58df263
Homogenise subsection headings
Lucia-Fonseca May 20, 2021
91aca08
Fix typo
Lucia-Fonseca May 20, 2021
319678e
Fix .depends example
Lucia-Fonseca Jun 8, 2021
11d4a4f
Fix .init example
Lucia-Fonseca Jun 8, 2021
2266d20
Fix .complete example
Lucia-Fonseca Jun 8, 2021
16e21fc
Change growth example for size
Lucia-Fonseca Jun 8, 2021
d1abe32
Fix .depends example by adding cosmology
Lucia-Fonseca Jun 8, 2021
db4fcc8
Rephrase text in multi -column assignment
Lucia-Fonseca Jun 8, 2021
63e6532
Change import objects example
Lucia-Fonseca Jun 8, 2021
a429941
Add line re: comments
Lucia-Fonseca Jun 8, 2021
9f651f9
Fix typo
Lucia-Fonseca Jun 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
349 changes: 349 additions & 0 deletions docs/configuration_files.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,349 @@
###################
Configuration Files
###################

This page outlines how to construct configuration files to run your own routines
with `~skypy.pipeline.Pipeline`.

`SkyPy` is an astrophysical simulation pipeline tool that allows to define any
arbitrary workflow and store data in table format. You may use `SkyPy` `~skypy.pipeline.Pipeline`
to call any function --your own implementation, from any compatible external software or from the `SkyPy library`.
Then `SkyPy` deals with the data dependencies and provides a library of functions to be used with it.

These guidelines start with an example using one of the `SkyPy` functions, and it follows
the concrete YAML syntax necessary for you to write your own configuration files, beyond using `SkyPy`
functions.

SkyPy example
-------------

In this section, we exemplify how you can write a configuration file and use some of the `SkyPy` functions.
In this example, we sample redshifts and magnitudes from the SkyPy luminosity function, `~skypy.galaxies.schechter_lf`.

- `Define variables`:

From the documentation, the parameters for the `~skypy.galaxies.schechter_lf` function are: ``redshift``, the characteristic absolute magnitude ``M_star``, the amplitude ``phi_star``, faint-end slope parameter ``alpha``,
the magnitude limit ``magnitude_limit``, the fraction of sky ``sky_area``, ``cosmology`` and ``noise``.
If you are planning to reuse some of these parameters, you can define them at the top-level of your configuration file.
In our example, we are using ``Astropy`` linear and exponential models for the characteristic absolute magnitude and the amplitude, respectively.
Also, ``noise`` is an optional parameter and you could use its default value by omitting its definition.

.. code:: yaml

cosmology: !astropy.cosmology.default_cosmology.get []
z_range: !numpy.linspace [0, 2, 21]
M_star: !astropy.modeling.models.Linear1D [-0.9, -20.4]
phi_star: !astropy.modeling.models.Exponential1D [3e-3, -9.7]
magnitude_limit: 23
sky_area: 0.1 deg2

- `Tables and columns`:

You can create a table ``blue_galaxies`` and for now add the columns for redshift and magnitude (note here the ``schechter_lf`` returns a 2D object)

.. code:: yaml

tables:
blue_galaxies:
redshift, magnitude: !skypy.galaxies.schechter_lf
redshift: $z_range
M_star: $M_star
phi_star: $phi_star
alpha: -1.3
m_lim: $magnitude_limit
sky_area: $sky_area

`Important:` if cosmology is detected as a parameter but is not set, it automatically uses the cosmology variable defined at the top-level of the file.

This is how the entire configuration file looks like!

.. literalinclude:: luminosity.yml
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
:language: yaml

You may now save it as ``luminosity.yml`` and run it using the `SkyPy` `~skypy.pipeline.Pipeline`:
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved

.. plot::
:include-source: true
:context: close-figs

import matplotlib.pyplot as plt
from skypy.pipeline import Pipeline

# Execute SkyPy luminosity pipeline
pipeline = Pipeline.read("luminosity.yml")
pipeline.execute()

# Blue population
skypy_galaxies = pipeline['blue_galaxies']

# Plot histograms
fig, axs = plt.subplots(1, 2, figsize=(9, 3))

axs[0].hist(skypy_galaxies['redshift'], bins=50, histtype='step', color='purple')
axs[0].set_xlabel(r'$Redshift$')
axs[0].set_ylabel(r'$\mathrm{N}$')
axs[0].set_yscale('log')

axs[1].hist(skypy_galaxies['magnitude'], bins=50, histtype='step', color='green')
axs[1].set_xlabel(r'$Magnitude$')
axs[1].set_yscale('log')

plt.tight_layout()
plt.show()

Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
You can also run the pipeline directly from the command line and write the outputs to a fits file:

.. code-block:: bash

$ skypy luminosity.yml luminosity.fits



Don’t forget to check out for more complete examples_!

.. _examples: https://skypy.readthedocs.io/en/stable/examples/index.html


YAML syntax
-----------
YAML_ is a file format designed to be readable by both computers and humans.
Fundamentally, a file written in YAML consists of a set of key-value pairs.
Each pair is written as ``key: value``, where whitespace after the ``:`` is optional.
The hash character ``#`` denotes the start of a comment and all further text on that
line is ignored by the parser.


Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
This guide introduces the main syntax of YAML relevant when writing
a configuration file to use with ``SkyPy``. Essentially, it begins with
definitions of individual variables at the top level, followed by the tables,
and, within the table entries, the features of objects to simulate are included.
Main keywords: ``parameters``, ``cosmology``, ``tables``.


Variables
^^^^^^^^^
* `Variable definition`: a variable is defined as a key-value pair at the top of the file.
YAML is able to interpret any numeric data with the appropriate type: integer, float, boolean.
Similarly for lists and dictionaries.
In addition, SkyPy has added extra functionality to interpret and store Astropy Quantities_.
Everything else is stored as a string (with or without explicitly using quotes)

.. code:: yaml

# YAML interprets
counter: 100 # An integer
miles: 1000.0 # A floating point
name: "Joy" # A string
planet: Earth # Another string
mylist: [ 'abc', 789, 2.0e3 ] # A list
mydict: { 'fruit': 'orange', 'year': 2020 } # A dictionary

# SkyPy extra functionality
angle: 10 deg
distance: 300 kpc


* `Import objects`:
the SkyPy configuration syntax allows objects to be imported directly from external
(sub)modules using the ``!`` tag and providing neither a list of arguments or a
dictionary of keywords. For example, this enables the import and usage of any Astropy cosmology:

.. code:: yaml

cosmology: !astropy.cosmology.Planck13 # import the Planck13 object and bind it to the variable named "cosmology"


Parameters
^^^^^^^^^^

* `Parameters definition`: parameters are variables that can be modified at execution.

For example,

.. code:: yaml

parameters:
hubble_constant: 70
omega_matter: 0.3


Functions
^^^^^^^^^
* `Function call`: functions are defined as tuples where the first entry is the fully qualified function name tagged with and exclamation mark ``!`` and the second entry is either a list of positional arguments or a dictionary of keyword arguments.

For example, if you need to call the ``log10()`` and ``linspace()`` NumPy_ functions, then you define the following key-value pairs:

.. code:: yaml

log_of_2: !numpy.log10 [2]
myarray: !numpy.linspace [0, 2.5, 10]

You can also define parameters of functions with a dictionary of keyword arguments.
Imagine you want to compute the comoving distance for a range of redshifts and an `Astropy` Planck 2015 cosmology.
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
To run it with the `SkyPy` pipeline, call the function and define the parameters as an indented dictionary.

.. code:: yaml

comoving_distance: !astropy.cosmology.Planck15.comoving_distance
z: !numpy.linspace [ 0, 1.3, 10 ]

Similarly, you can specify the functions arguments as a dictionary:

.. code:: yaml

comoving_distance: !astropy.cosmology.Planck15.comoving_distance
z: !numpy.linspace {start: 0, stop: 1.3, num: 10}

`N.B.` To call a function with no arguments, you should pass an empty list of
``args`` or an empty dictionary of ``kwargs``. For example:

.. code:: yaml

cosmo: !astropy.cosmology.default_cosmology.get []


* `Variable reference`: variables can be referenced by their full name tagged with a dollar sign ``$``.
In the previous example you could also define the variables at the top-level of the file and then reference them:

.. code:: yaml

redshift: !numpy.linspace [ 0, 1.3, 10 ]
comoving_distance: !astropy.cosmology.Planck15.comoving_distance
z: $redshift

* The `cosmology` to be used by functions within the pipeline only needs to be set up at the top. If a function needs ``cosmology`` as an input, you need not define it again, it is automatically detected.

For example, calculate the angular size of a galaxy with a given physical size, at a fixed redshift and for a given cosmology:

.. code:: yaml

cosmology: !astropy.cosmology.FlatLambdaCDM
H0: 70
Om0: 0.3
size: !skypy.galaxies.morphology.angular_size
physical_size: 10 kpc
redshift: 0.2

* `Job completion`: ``.depends`` can be used to force any function call to wait for completion
of any other job.

A simple example where, for some reason, the comoving distance needs to be called after
completion of the angular size function:

.. code:: yaml

cosmology: !astropy.cosmology.Planck15
size: !skypy.galaxies.morphology.angular_size
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
physical_size: 10 kpc
redshift: 0.2
comoving_distance: !astropy.cosmology.Planck15.comoving_distance
z: !numpy.linspace [ 0, 1.3, 10 ]
.depends: size

By doing so, you force the function call ``redshift`` to be completed before is used to compute the comoving distance.


Tables
^^^^^^

* `Table creation`: a dictionary of table names, each resolving to a dictionary of column names for that table.

Let us create a table called ``telescope`` with a column to store the width of spectral lines that follow a normal distribution

.. code:: yaml

tables:
telescope:
spectral_lines: !scipy.stats.norm.rvs
loc: 550
scale: 1.6
size: 100

* `Column addition`: you can add as many columns to a table as you need.
Imagine you want to add a column for the telescope collecting surface

.. code:: yaml

tables:
telescope:
spectral_lines: !scipy.stats.norm.rvs
loc: 550
scale: 1.6
size: 100
collecting_surface: !numpy.random.uniform
low: 6.9
high: 7.1
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
size: 100

* `Column reference`: columns in the pipeline can be referenced by their full name tagged with a dollar sign ``$``.
Example: the galaxy mass that follows a lognormal distribution. You can create a table ``galaxies``
with a column ``mass`` where you sample 10000 object and a second column, ``radius`` which also follows a lognormal distribution
but the mean depends on how massive the galaxies are:

.. code:: yaml

tables:
galaxies:
mass: !numpy.random.lognormal
mean: 5.
size: 10000
radius: !numpy.random.lognormal
mean: $galaxies.mass


* `Multi-column assignment`: multi-column assignment is performed with any 2d-array, where one of the dimensions is interpreted
as the rows of the table and the second dimension, as separate columns. Or you can do it from a function that returns a tuple.

Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
We use multi-column assignment in the following example where we sample a two-dimensional array of values from a lognormal distribution and then store them as three columns in a table:

.. code:: yaml

tables:
halos:
mass, radius, concentration: !numpy.random.lognormal
size: [10000, 3]


* `Table initialisation`: by default tables are initialised using ``astropy.table.Table()`` however this can be overridden using the ``.init`` keyword to initialise the table with any function call.

For example, you can stack galaxy properties such as radii and mass:

.. code:: yaml

radii: !numpy.logspace [ 1, 2, 100 ]
mass: !numpy.logspace [ 9, 12, 100 ]
tables:
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved
galaxies:
.init: !astropy.table.vstack [[ $radii, $mass ]]


* `Table reference`: when a function call depends on tables, you need to ensure the referenced table has the necessary content and is not empty.
You can do that with ``.complete``.

Example: you want to perform a very simple abundance matching, i.e. painting galaxies within your halos.
You can create two tables ``halos`` and ``galaxies`` storing the halo mass and galaxy luminosities.
Then you can stack these two tables and store it in a third table called ``matching``.

.. code:: yaml
Lucia-Fonseca marked this conversation as resolved.
Show resolved Hide resolved

tables:
halos:
halo_mass: !numpy.random.uniform
low: 1.0e8
high: 1.0e14
size: 20
galaxies:
luminosity: !numpy.random.uniform
low: 0.05
high: 10.0
size: 20
matching:
.init: !astropy.table.hstack
tables: [ $halos, $galaxies ]
.depends: [ halos.complete, galaxies.complete ]


.. _YAML: https://yaml.org
.. _NumPy: https://numpy.org
.. _Quantities: https://docs.astropy.org/en/stable/units/
.. _clone(): https://docs.astropy.org/en/stable/api/astropy.cosmology.FLRW.html?highlight=clone#astropy.cosmology.FLRW.clone
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Getting Started

install
feature_list
configuration_files
examples/index

.. _user-docs:
Expand Down
15 changes: 15 additions & 0 deletions docs/luminosity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cosmology: !astropy.cosmology.default_cosmology.get []
z_range: !numpy.linspace [0, 2, 21]
M_star: !astropy.modeling.models.Linear1D [-0.9, -20.4]
phi_star: !astropy.modeling.models.Exponential1D [3e-3, -9.7]
magnitude_limit: 23
sky_area: 0.1 deg2
tables:
blue_galaxies:
redshift, magnitude: !skypy.galaxies.schechter_lf
redshift: $z_range
M_star: $M_star
phi_star: $phi_star
alpha: -1.3
m_lim: $magnitude_limit
sky_area: $sky_area