{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading a Configuration\n", "\n", "This notebook demonstrates how TARDIS reads a configuration file or a configuration dictionary and creates an instance of the `Configuration` class." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T16:56:52.848130Z", "start_time": "2018-07-18T16:56:52.844292Z" } }, "outputs": [], "source": [ "from tardis.io.configuration.config_reader import Configuration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The configuration can be read this way. It will be validated by the json-schema schemas (including filling out default configuration options that had not been specified)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T16:59:08.589550Z", "start_time": "2018-07-18T16:59:08.305384Z" } }, "outputs": [], "source": [ "conf = Configuration.from_yaml('tardis_example.yml')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Despite the very simple nature of tardis_example.yml, the output does contain all the default values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-07-18T17:02:54.736144Z", "start_time": "2018-07-18T17:02:54.729478Z" } }, "outputs": [], "source": [ "conf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Values can either be accessed using `.`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conf.model.structure.velocity.start" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or by treating the configuration as a dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conf['montecarlo']['convergence_strategy']['damping_constant']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similarly to how they are accessed, entries in the configuration can be edited. For example," ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conf['montecarlo']['convergence_strategy']['damping_constant'] = 0.3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "sets that entry of the configuration to 0.3:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conf['montecarlo']['convergence_strategy']['damping_constant']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This can be done using `.` as well.\n", "\n", "You can also read a configuration from a dictionary. For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from astropy import units as u\n", "\n", "conf_dict = {'tardis_config_version': 'v1.0',\n", " 'supernova': {'luminosity_requested': 1.05e+43 * u.erg / u.s,\n", " 'time_explosion': 13 * u.day},\n", " 'atom_data': 'kurucz_cd23_chianti_H_He.h5',\n", " 'model': {'structure': {'type': 'specific',\n", " 'velocity': {'start': 11000. * u.km / u.s,\n", " 'stop': 20000. * u.km / u.s,\n", " 'num': 20},\n", " 'density': {'type': 'branch85_w7'}},\n", " 'abundances': {'type': 'uniform',\n", " 'O': 0.19,\n", " 'Mg': 0.03,\n", " 'Si': 0.52,\n", " 'S': 0.19,\n", " 'Ar': 0.04,\n", " 'Ca': 0.03}},\n", " 'plasma': {'ionization': 'lte',\n", " 'excitation': 'lte',\n", " 'radiative_rates_type': 'dilute-blackbody',\n", " 'line_interaction_type': 'macroatom',},\n", " 'montecarlo': {'seed': 23111963,\n", " 'no_of_packets': 40000.0,\n", " 'iterations': 20,\n", " 'last_no_of_packets': 100000.0,\n", " 'no_of_virtual_packets': 10,},\n", " 'spectrum': {'start': 500. * u.Angstrom,\n", " 'stop': 20000. * u.Angstrom,\n", " 'num': 10000,}}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "conf2 = Configuration.from_config_dict(conf_dict)\n", "conf2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that the `Configuration` object once again has all the default values filled in.\n", "\n", "
\n", "\n", "Note\n", "\n", "In most cases, we strongly recommend using a configuration file as opposed to a configuration dictionary.\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }