Modifying a model

Of course, a premade model may not behave in the exact way you need. You could make your own model from scratch, but it may often be simpler to modify the properties of an existing model to match your needs. Below we demonstrate this using the setter methods on various properties. These methods, called on a specific element of an EmissionModel, can change or add to various properties.

First we load some common modules and a grid.

[1]:
from unyt import dimensionless, kelvin

from synthesizer.emission_models import TotalEmission
from synthesizer.emission_models.attenuation import PowerLaw
from synthesizer.emission_models.dust.emission import Blackbody
from synthesizer.grid import Grid

# Get the grid which we'll need for extraction
grid_dir = "../../../tests/test_grid"
grid_name = "test_grid"
grid = Grid(grid_name, grid_dir=grid_dir)

Adding a mask

We can add any number of masks to any model in the emission tree. Below we add a filter for young stars (<107 Myr) to the nebular components (nebular_continuum and linecont). We’ve used the dictionary look up syntax to extract the models we want to update, and then added the mask.

[2]:
# Make a model
model = TotalEmission(
    grid=grid,
    dust_curve=PowerLaw(slope=-1),
    tau_v=0.33,
    fesc=0.2,
    fesc_ly_alpha=0.7,
    dust_emission_model=Blackbody(temperature=100 * kelvin),
)

model["nebular_continuum"].add_mask("log10ages", "<", 7 * dimensionless)
model["nebular_line"].add_mask("log10ages", "<", 7 * dimensionless)

You will have seen in previous sections that we can instantiate a model with a mask, however this acts globally on all child models. If we want to have multiple masks, we must apply them to individual models as shown above.

Notice that we have supplied units with our age threshold; this is required to maintain consistency across objects while enabling arbitrary units.

We can see the effect of applying this mask in the emission model tree.

[3]:
model.plot_emission_tree()
../_images/emission_models_modify_models_5_0.png
[3]:
(<Figure size 600x600 with 1 Axes>, <Axes: >)

Changing Properties

Any property of a model can be changed by calling its setter (any method of the form set_<attr>).

For most attributes, simply index the model with the label of the child model we want to modify and call the appropriate setter. For example, lets change the grid for the nebular continuum model.

[4]:
model["nebular_continuum"].set_grid(grid)
print(model)
|================================================ EmissionModel: total ===============================================|
|---------------------------------------------------------------------------------------------------------------------|
|  FULL_TRANSMITTED (stellar)                                                                                         |
|---------------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                                    |
|  Grid: test_grid                                                                                                    |
|  Extract key: transmitted                                                                                           |
|  Use velocity shift: False                                                                                          |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  NEBULAR_LINE_NO_FESC (stellar)                                                                                     |
|---------------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                                    |
|  Grid: test_grid                                                                                                    |
|  Extract key: linecont                                                                                              |
|  Use velocity shift: False                                                                                          |
|  Save emission: False                                                                                               |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  NEBULAR_CONTINUUM (stellar)                                                                                        |
|---------------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                                    |
|  Grid: test_grid                                                                                                    |
|  Extract key: nebular_continuum                                                                                     |
|  Use velocity shift: False                                                                                          |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|  Masks:                                                                                                             |
|    - log10ages < 7 dimensionless                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  TRANSMITTED (stellar)                                                                                              |
|---------------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                                   |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.ProcessedFraction'>                  |
|  Apply to: full_transmitted                                                                                         |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - fesc: 0.2                                                                                                      |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  NEBULAR_LINE (stellar)                                                                                             |
|---------------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                                   |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.EscapedFraction'>                    |
|  Apply to: nebular_line_no_fesc                                                                                     |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - fesc_ly_alpha: 0.7                                                                                             |
|    - tau_v: 0.33                                                                                                    |
|  Masks:                                                                                                             |
|    - log10ages < 7 dimensionless                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  NEBULAR_COMBINED (stellar)                                                                                         |
|---------------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                                   |
|  Combine models: nebular_line, nebular_continuum                                                                    |
|  Save emission: False                                                                                               |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  NEBULAR (stellar)                                                                                                  |
|---------------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                                   |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.ProcessedFraction'>                  |
|  Apply to: nebular_combined                                                                                         |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - fesc: fesc                                                                                                     |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  REPROCESSED (stellar)                                                                                              |
|---------------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                                   |
|  Combine models: nebular, transmitted                                                                               |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  ATTENUATED (stellar)                                                                                               |
|---------------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                                   |
|  Transformer: <class 'synthesizer.emission_models.transformers.dust_attenuation.PowerLaw'>                          |
|  Apply to: reprocessed                                                                                              |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  DUST_EMISSION (stellar)                                                                                            |
|---------------------------------------------------------------------------------------------------------------------|
|Generation model:                                                                                                    |
|  Emission generation model: <synthesizer.emission_models.dust.emission.Blackbody object at 0x7f4f24981de0>          |
|  Dust luminosity: reprocessed - attenuated                                                                          |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  ESCAPED (stellar)                                                                                                  |
|---------------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                                   |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.EscapedFraction'>                    |
|  Apply to: full_transmitted                                                                                         |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - fesc: 0.2                                                                                                      |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  EMERGENT (stellar)                                                                                                 |
|---------------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                                   |
|  Combine models: attenuated, escaped                                                                                |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - fesc: fesc                                                                                                     |
|    - tau_v: 0.33                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|  TOTAL (stellar)                                                                                                    |
|---------------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                                   |
|  Combine models: emergent, dust_emission                                                                            |
|  Save emission: True                                                                                                |
|  Fixed parameters:                                                                                                  |
|    - tau_v: 0.33                                                                                                    |
|=====================================================================================================================|

Changing properties on all models

For some properties you might want to change it on every child model; these setters allow you to pass set_all=True.

Here we’ll swap the grid using again, but this time we’ll use the set_all=True argument to change the grid on all models.

[5]:
model.set_grid(grid, set_all=True)

Changing dust attenuation

To change dust properties synthesizer provides a single setter, set_dust_props (though each attribute can be set individually). This defines the model the attenuation is applied to (apply_to), and can optionally change the dust_curve.

[6]:
model["attenuated"].set_dust_props(
    dust_curve=PowerLaw(slope=-0.7),
    apply_to=model["reprocessed"],
)

Replacing models

Instead of changing the properties we can also completely replace a model entirely with one or more models.

Below we swap out the attenuation model with two separate attenuation models, which apply different dust curves to the old and young stellar populations with fixed optical depths (for more information on defining your own models see the custom model docs).

[7]:
from synthesizer.emission_models import EmissionModel

# Define the models well replace attenuate with
young_attenuated = EmissionModel(
    "young_attenuated",
    dust_curve=PowerLaw(slope=-1),
    apply_to=model["reprocessed"],
    tau_v=0.7,
    mask_attr="log10ages",
    mask_thresh=7 * dimensionless,
    mask_op="<=",
    emitter="stellar",
)
old_attenuated = EmissionModel(
    "old_attenuated",
    dust_curve=PowerLaw(slope=-1),
    apply_to=model["reprocessed"],
    tau_v=0.7 + 0.67,
    mask_attr="log10ages",
    mask_thresh=7 * dimensionless,
    mask_op=">",
    emitter="stellar",
)

model.replace_model("attenuated", young_attenuated, old_attenuated)
model.plot_emission_tree()
../_images/emission_models_modify_models_13_0.png
[7]:
(<Figure size 600x600 with 1 Axes>, <Axes: >)

Notice how passing multiple models after the label has also introduced a combination model using the original label. Had we only passed a single model this would have instead replaced the model entirely.

Relabel model

To change the label associated to a model we can use the relabel method.

[8]:
model.relabel("attenuated", "dust_attenuated")
model.plot_emission_tree()
../_images/emission_models_modify_models_15_0.png
[8]:
(<Figure size 600x600 with 1 Axes>, <Axes: >)

Fixing parameters

In the model basics we saw that we could fix parameters when instantiating a model but like with the masks this acts globally on all child models. If we want to target specific models and fix a parameter only on that specific model we can use the fix_parameters setter.

Recall that when an emission model is passed to a get_spectra method any parameters the model defines will override any set on the emitter.

[9]:
model["young_attenuated"].fix_parameters(tau_v=0.67)
print(model["young_attenuated"])
|====================================== EmissionModel: young_attenuated ======================================|
|-------------------------------------------------------------------------------------------------------------|
|  FULL_TRANSMITTED (stellar)                                                                                 |
|-------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                            |
|  Grid: test_grid                                                                                            |
|  Extract key: transmitted                                                                                   |
|  Use velocity shift: False                                                                                  |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  NEBULAR_LINE_NO_FESC (stellar)                                                                             |
|-------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                            |
|  Grid: test_grid                                                                                            |
|  Extract key: linecont                                                                                      |
|  Use velocity shift: False                                                                                  |
|  Save emission: False                                                                                       |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  NEBULAR_CONTINUUM (stellar)                                                                                |
|-------------------------------------------------------------------------------------------------------------|
|Extraction model:                                                                                            |
|  Grid: test_grid                                                                                            |
|  Extract key: nebular_continuum                                                                             |
|  Use velocity shift: False                                                                                  |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.33                                                                                            |
|  Masks:                                                                                                     |
|    - log10ages < 7 dimensionless                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  TRANSMITTED (stellar)                                                                                      |
|-------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                           |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.ProcessedFraction'>          |
|  Apply to: full_transmitted                                                                                 |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - fesc: 0.2                                                                                              |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  NEBULAR_LINE (stellar)                                                                                     |
|-------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                           |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.EscapedFraction'>            |
|  Apply to: nebular_line_no_fesc                                                                             |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - fesc_ly_alpha: 0.7                                                                                     |
|    - tau_v: 0.33                                                                                            |
|  Masks:                                                                                                     |
|    - log10ages < 7 dimensionless                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  NEBULAR_COMBINED (stellar)                                                                                 |
|-------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                           |
|  Combine models: nebular_line, nebular_continuum                                                            |
|  Save emission: False                                                                                       |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  NEBULAR (stellar)                                                                                          |
|-------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                           |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.ProcessedFraction'>          |
|  Apply to: nebular_combined                                                                                 |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - fesc: fesc                                                                                             |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  REPROCESSED (stellar)                                                                                      |
|-------------------------------------------------------------------------------------------------------------|
|Combination model:                                                                                           |
|  Combine models: nebular, transmitted                                                                       |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.33                                                                                            |
|-------------------------------------------------------------------------------------------------------------|
|  YOUNG_ATTENUATED (stellar)                                                                                 |
|-------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                           |
|  Transformer: <class 'synthesizer.emission_models.transformers.dust_attenuation.PowerLaw'>                  |
|  Apply to: reprocessed                                                                                      |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - tau_v: 0.67                                                                                            |
|  Masks:                                                                                                     |
|    - log10ages <= 7 dimensionless                                                                           |
|-------------------------------------------------------------------------------------------------------------|
|  ESCAPED (stellar)                                                                                          |
|-------------------------------------------------------------------------------------------------------------|
|Transformer model:                                                                                           |
|  Transformer: <class 'synthesizer.emission_models.transformers.escape_fraction.EscapedFraction'>            |
|  Apply to: full_transmitted                                                                                 |
|  Save emission: True                                                                                        |
|  Fixed parameters:                                                                                          |
|    - fesc: 0.2                                                                                              |
|    - tau_v: 0.33                                                                                            |
|=============================================================================================================|