Skip to content

Commit

Permalink
Merge pull request #140 from LSSTDESC/139-assertionerror-in-dsps_sed_…
Browse files Browse the repository at this point in the history
…demo
  • Loading branch information
ztq1996 authored May 11, 2024
2 parents 66cace4 + ca8ea79 commit eb71f47
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-head.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
echo "SPS_HOME=/opt/hostedtoolcache/Python/fsps" >> $GITHUB_ENV
- name: Run notebooks
run: |
rail render-nb --skip examples/creation_examples/plotting_interface_skysim_cosmoDC2_COSMOS2020_demo.ipynb examples/${{ matrix.notebook-dir }}_examples/*.ipynb
rail render-nb --skip examples/creation_examples/dsps_sed_demo.ipynb --skip examples/creation_examples/plotting_interface_skysim_cosmoDC2_COSMOS2020_demo.ipynb examples/${{ matrix.notebook-dir }}_examples/*.ipynb
- name: Send status to Slack app (RAIL CI Reporter)
if: ${{ failure() && github.event_name != 'workflow_dispatch' }}
id: slack
Expand Down
81 changes: 39 additions & 42 deletions examples/creation_examples/dsps_sed_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"source": [
"# Demo of SED and photometry calculations with DSPS\n",
"\n",
"author: Luca Tortorelli, Andrew Hearin\n",
"author: Luca Tortorelli\n",
"\n",
"last run successfully: Aug 1, 2023\n",
"last run successfully: May 10, 2024\n",
"\n",
"This notebook demonstrates some basic usage of the DSPS library. In particular, for a galaxy with some arbitrary star formation history, we'll see how to calculate its restframe SED, and its absolute and apparent magnitude in some band.\n",
"This notebook demonstrates some basic usage of the rail_dsps library. In particular, for a galaxy with some arbitrary star formation history, we'll see how to calculate its restframe SED, and its absolute and apparent magnitude in some band.\n",
"\n",
"SPS calculations are based on a set of template SEDs of simple stellar populations (SSPs). Supplying such templates is outside the planned scope of the DSPS package, and so they will need to be retrieved from some other library. For example, the FSPS library supplies such templates in a convenient form."
"SPS calculations are based on a set of template SEDs of simple stellar populations (SSPs). Supplying such templates is outside the planned scope of the DSPS package, and so they will need to be retrieved from some other library. For example, the FSPS library supplies such templates in a convenient form.\n",
"\n",
"In case the user is not aware of how to generate these templates, the code automatically downloads a pre-defined library from NERSC."
]
},
{
Expand All @@ -41,7 +43,7 @@
}
},
"source": [
"The SingleSedModeler class allows the user to generate a single rest-frame SED at the time with DSPS.\n",
"The SingleSedModeler class allows the user to generate a single rest-frame SED at the time with rail_dsps.\n",
"\n",
"Some example galaxy properties that are required to create a single SED model are generated via this notebook and stored into an hdf5 table. The required galaxy properties are:\n",
"\n",
Expand All @@ -54,22 +56,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"ExecuteTime": {
"end_time": "2024-05-10T15:41:08.510620Z",
"start_time": "2024-05-10T15:41:05.620759Z"
}
},
"outputs": [],
"source": [
"import os\n",
"import rail.dsps\n",
"from rail.core.stage import RailStage\n",
"from rail.core.data import TableHandle\n",
"from rail.core.data import Hdf5Handle\n",
"import numpy as np\n",
"import h5py"
]
],
"outputs": [],
"execution_count": 1
},
{
"cell_type": "code",
Expand All @@ -85,10 +91,9 @@
"DS = RailStage.data_store\n",
"DS.__class__.allow_overwrite = True\n",
"\n",
"RAIL_DSPS_DIR = os.path.abspath(os.path.join(os.path.dirname(rail.dsps.__file__), '..', '..'))\n",
"default_rail_fsps_files_folder = os.path.join(RAIL_DSPS_DIR, 'rail', 'examples_data', 'creation_data',\n",
" 'data', 'dsps_default_data')\n",
"input_file = os.path.join(default_rail_fsps_files_folder, 'input_galaxy_properties_dsps.hdf5')"
"RAIL_DSPS_DIR = os.path.abspath(os.path.join(os.path.dirname(rail.dsps.__file__), '..'))\n",
"default_rail_fsps_files_folder = os.path.join(RAIL_DSPS_DIR, 'examples_data/creation_data/data/dsps_default_data')\n",
"trainFile = os.path.join(default_rail_fsps_files_folder, 'input_galaxy_properties_dsps.hdf5')"
]
},
{
Expand Down Expand Up @@ -117,7 +122,7 @@
"stellar_metallicity = np.full(n_galaxies, gal_lgmet)\n",
"stellar_metallicity_scatter = np.full(n_galaxies, gal_lgmet_scatter)\n",
"\n",
"with h5py.File(input_file, 'w') as h5table:\n",
"with h5py.File(trainFile, 'w') as h5table:\n",
" h5table.create_dataset(name='redshifts', data=redshift)\n",
" h5table.create_dataset(name='cosmic_time_grid', data=cosmic_time_grid)\n",
" h5table.create_dataset(name='star_formation_history', data=star_formation_history)\n",
Expand All @@ -135,10 +140,7 @@
}
},
"outputs": [],
"source": [
"trainFile = os.path.join(input_file)\n",
"training_data = DS.read_file(\"training_data\", TableHandle, trainFile)"
]
"source": "training_data = DS.read_file(\"training_data\", Hdf5Handle, trainFile)"
},
{
"cell_type": "markdown",
Expand All @@ -164,13 +166,14 @@
"outputs": [],
"source": [
"dspssinglesedmodeler = rail.dsps.DSPSSingleSedModeler.make_stage(name='DSPSSingleSedModeler',\n",
" ssp_templates_file=os.path.join(RAIL_DSPS_DIR,'rail/examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" ssp_templates_file=os.path.join(RAIL_DSPS_DIR,'examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" redshift_key='redshifts',\n",
" cosmic_time_grid_key='cosmic_time_grid',\n",
" star_formation_history_key='star_formation_history',\n",
" stellar_metallicity_key='stellar_metallicity',\n",
" stellar_metallicity_scatter_key='stellar_metallicity_scatter',\n",
" restframe_sed_key='restframe_seds', default_cosmology=True)"
" restframe_sed_key='restframe_seds', default_cosmology=True,\n",
" min_wavelength=250, max_wavelength=12000)"
]
},
{
Expand All @@ -183,9 +186,7 @@
}
},
"outputs": [],
"source": [
"dspssinglesedmodel = dspssinglesedmodeler.fit_model(input_data=training_data)"
]
"source": "dspssinglesedmodel = dspssinglesedmodeler.fit_model(input_data=training_data)"
},
{
"cell_type": "code",
Expand Down Expand Up @@ -239,7 +240,7 @@
"import os\n",
"import rail.dsps\n",
"from rail.core.stage import RailStage\n",
"from rail.core.data import TableHandle\n",
"from rail.core.data import Hdf5Handle\n",
"import numpy as np\n",
"import h5py"
]
Expand All @@ -258,9 +259,8 @@
"DS = RailStage.data_store\n",
"DS.__class__.allow_overwrite = True\n",
"\n",
"default_rail_fsps_files_folder = os.path.join(RAIL_DSPS_DIR, 'rail', 'examples_data', 'creation_data',\n",
" 'data', 'dsps_default_data')\n",
"input_file = os.path.join(default_rail_fsps_files_folder, 'input_galaxy_properties_dsps.hdf5')"
"default_rail_fsps_files_folder = os.path.join(RAIL_DSPS_DIR, 'examples_data/creation_data/data/dsps_default_data')\n",
"trainFile = os.path.join(default_rail_fsps_files_folder, 'input_galaxy_properties_dsps.hdf5')"
]
},
{
Expand Down Expand Up @@ -289,7 +289,7 @@
"stellar_metallicity = np.full(n_galaxies, gal_lgmet)\n",
"stellar_metallicity_scatter = np.full(n_galaxies, gal_lgmet_scatter)\n",
"\n",
"with h5py.File(input_file, 'w') as h5table:\n",
"with h5py.File(trainFile, 'w') as h5table:\n",
" h5table.create_dataset(name='redshifts', data=redshift)\n",
" h5table.create_dataset(name='cosmic_time_grid', data=cosmic_time_grid)\n",
" h5table.create_dataset(name='star_formation_history', data=star_formation_history)\n",
Expand All @@ -307,10 +307,7 @@
}
},
"outputs": [],
"source": [
"trainFile = os.path.join(input_file)\n",
"training_data = DS.read_file(\"training_data\", TableHandle, trainFile)"
]
"source": "training_data = DS.read_file(\"training_data\", Hdf5Handle, trainFile)"
},
{
"cell_type": "code",
Expand All @@ -325,13 +322,14 @@
"source": [
"dspspopulationsedmodeler = rail.dsps.DSPSPopulationSedModeler.make_stage(name='DSPSPopulationSedModeler',\n",
" ssp_templates_file=os.path.join(RAIL_DSPS_DIR,\n",
" 'rail/examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" 'examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" redshift_key='redshifts',\n",
" cosmic_time_grid_key='cosmic_time_grid',\n",
" star_formation_history_key='star_formation_history',\n",
" stellar_metallicity_key='stellar_metallicity',\n",
" stellar_metallicity_scatter_key='stellar_metallicity_scatter',\n",
" restframe_sed_key='restframe_seds', default_cosmology=True)"
" restframe_sed_key='restframe_seds', default_cosmology=True,\n",
" min_wavelength=250, max_wavelength=12000)"
]
},
{
Expand Down Expand Up @@ -391,6 +389,7 @@
"- the absolute and apparent magnitudes dataset keyword of the output hdf5 table\n",
"- the folder path containing the filter bands\n",
"- the name of the filter bands in order of increasing wavelength\n",
"- the minimum and maximum wavelength used in the DSPSPopulationSedModeler class to generate the rest-frame SEDs\n",
"- the path to the SSP template files\n",
"- a boolean keyword to use (True) the default cosmology in DSPS.\n",
"\n",
Expand All @@ -411,7 +410,7 @@
"import os\n",
"import rail.dsps\n",
"from rail.core.stage import RailStage\n",
"from rail.core.data import TableHandle"
"from rail.core.data import Hdf5Handle"
]
},
{
Expand All @@ -428,7 +427,7 @@
"DS = RailStage.data_store\n",
"DS.__class__.allow_overwrite = True\n",
"\n",
"input_file = 'model_DSPSPopulationSedModeler.hdf5'"
"trainFile = 'model_DSPSPopulationSedModeler.hdf5'"
]
},
{
Expand All @@ -441,10 +440,7 @@
}
},
"outputs": [],
"source": [
"trainFile = os.path.join(input_file)\n",
"training_data = DS.read_file(\"training_data\", TableHandle, trainFile)"
]
"source": "training_data = DS.read_file(\"training_data\", Hdf5Handle, trainFile)"
},
{
"cell_type": "code",
Expand All @@ -463,11 +459,12 @@
" absolute_mags_key='rest_frame_absolute_mags',\n",
" apparent_mags_key='apparent_mags',\n",
" filter_folder=os.path.join(RAIL_DSPS_DIR,\n",
" 'rail/examples_data/creation_data/data/dsps_default_data/filters'),\n",
" 'examples_data/creation_data/data/dsps_default_data/filters'),\n",
" instrument_name='lsst',\n",
" wavebands='u,g,r,i,z,y',\n",
" min_wavelength=250, max_wavelength=12000,\n",
" ssp_templates_file=os.path.join(RAIL_DSPS_DIR,\n",
" 'rail/examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" 'examples_data/creation_data/data/dsps_default_data/ssp_data_fsps_v3.2_lgmet_age.h5'),\n",
" default_cosmology=True)"
]
},
Expand Down
Loading

0 comments on commit eb71f47

Please sign in to comment.