-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
EmissionAbsorptionMismatchedModel
(#10)
* rework matched/mismatched models
- Loading branch information
Showing
15 changed files
with
4,125 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
""" | ||
emission_absorption_ff_model.py | ||
EmissionAbsorptionFFModel definition | ||
emission_absorption_matched_model.py | ||
EmissionAbsorptionMatchedModel definition | ||
Copyright(C) 2024 by | ||
Trey V. Wenger; [email protected] | ||
|
@@ -28,11 +28,11 @@ | |
from caribou_hi import physics | ||
|
||
|
||
class EmissionAbsorptionFFModel(HIModel): | ||
"""Definition of the EmissionAbsorptionFFModel model. SpecData keys must be "emission" and "absorption".""" | ||
class EmissionAbsorptionMatchedModel(HIModel): | ||
"""Definition of the EmissionAbsorptionMatchedModel model. SpecData keys must be "emission" and "absorption".""" | ||
|
||
def __init__(self, *args, bg_temp: float = 3.77, **kwargs): | ||
"""Initialize a new EmissionAbsorptionFFModel instance | ||
"""Initialize a new EmissionAbsorptionMatchedModel instance | ||
Parameters | ||
---------- | ||
|
@@ -48,7 +48,7 @@ def __init__(self, *args, bg_temp: float = 3.77, **kwargs): | |
# Define TeX representation of each parameter | ||
self.var_name_map.update( | ||
{ | ||
"filling_factor": r"f", | ||
"filling_factor": r"$f$", | ||
} | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
emission_absorption_mismatched_model.py | ||
EmissionAbsorptionMismatchedModel definition | ||
Copyright(C) 2024 by | ||
Trey V. Wenger; [email protected] | ||
GNU General Public License v3 (GNU GPLv3) | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, | ||
or (at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import pymc as pm | ||
import pytensor.tensor as pt | ||
|
||
from caribou_hi.hi_model import HIModel | ||
from caribou_hi import physics | ||
|
||
|
||
class EmissionAbsorptionMismatchedModel(HIModel): | ||
"""Definition of the EmissionAbsorptionMismatchedModel model. SpecData keys must be "emission" and "absorption".""" | ||
|
||
def __init__(self, *args, bg_temp: float = 3.77, **kwargs): | ||
"""Initialize a new EmissionAbsorptionMismatchedModel instance | ||
Parameters | ||
---------- | ||
bg_temp : float, optional | ||
Assumed background temperature (K), by default 3.77 | ||
""" | ||
# Initialize HIModel | ||
super().__init__(*args, **kwargs) | ||
|
||
# Save inputs | ||
self.bg_temp = bg_temp | ||
|
||
# Define TeX representation of each parameter | ||
self.var_name_map.update( | ||
{ | ||
"filling_factor": r"$f$", | ||
"absorption_weight": r"$w_\tau$", | ||
} | ||
) | ||
|
||
def add_priors(self, *args, **kwargs): | ||
"""Add priors and deterministics to the model""" | ||
super().add_priors(*args, **kwargs) | ||
|
||
with self.model: | ||
# Filling factor | ||
_ = pm.Uniform("filling_factor", lower=0.0, upper=1.0, dims="cloud") | ||
|
||
# Absorption weight | ||
_ = pm.Uniform("absorption_weight", lower=0.0, upper=1.0, dims="cloud") | ||
|
||
def add_likelihood(self): | ||
"""Add likelihood to the model. SpecData key must be "emission".""" | ||
# Predict optical depth spectrum (shape: spectral, clouds) | ||
absorption_optical_depth = self.model["absorption_weight"] * physics.calc_optical_depth( | ||
self.data["absorption"].spectral, | ||
self.model["velocity"], | ||
10.0 ** self.model["log10_NHI"], | ||
self.model["tspin"], | ||
self.model["fwhm"], | ||
) | ||
emission_optical_depth = physics.calc_optical_depth( | ||
self.data["emission"].spectral, | ||
self.model["velocity"], | ||
10.0 ** self.model["log10_NHI"], | ||
self.model["tspin"], | ||
self.model["fwhm"], | ||
) | ||
|
||
# Sum over clouds | ||
predicted_absorption = 1.0 - pt.exp(-absorption_optical_depth.sum(axis=1)) | ||
|
||
# Evaluate radiative transfer | ||
predicted_emission = physics.radiative_transfer( | ||
emission_optical_depth, self.model["tspin"], self.model["filling_factor"], self.bg_temp | ||
) | ||
|
||
# Add baseline models | ||
baseline_models = self.predict_baseline() | ||
predicted_absorption = predicted_absorption + baseline_models["absorption"] | ||
predicted_emission = predicted_emission + baseline_models["emission"] | ||
|
||
with self.model: | ||
# Evaluate likelihood | ||
_ = pm.Normal( | ||
"absorption", | ||
mu=predicted_absorption, | ||
sigma=self.data["absorption"].noise, | ||
observed=self.data["absorption"].brightness, | ||
) | ||
_ = pm.Normal( | ||
"emission", | ||
mu=predicted_emission, | ||
sigma=self.data["emission"].noise, | ||
observed=self.data["emission"].brightness, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{%- if show_headings %} | ||
{{- [basename] | join(' ') | e | heading }} | ||
|
||
{% endif -%} | ||
.. automodule:: {{ qualname }} | ||
{%- for option in automodule_options %} | ||
:{{ option }}: | ||
{%- endfor %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{%- macro automodule(modname, options) -%} | ||
.. automodule:: {{ modname }} | ||
{%- for option in options %} | ||
:{{ option }}: | ||
{%- endfor %} | ||
{%- endmacro %} | ||
|
||
{%- macro toctree(docnames) -%} | ||
.. toctree:: | ||
:maxdepth: {{ maxdepth }} | ||
{% for docname in docnames %} | ||
{{ docname }} | ||
{%- endfor %} | ||
{%- endmacro %} | ||
|
||
{%- if is_namespace %} | ||
{{- [pkgname] | join(" ") | e | heading }} | ||
{% else %} | ||
{{- [pkgname] | join(" ") | e | heading }} | ||
{% endif %} | ||
|
||
{%- if is_namespace %} | ||
.. py:module:: {{ pkgname }} | ||
{% endif %} | ||
|
||
{%- if modulefirst and not is_namespace %} | ||
{{ automodule(pkgname, automodule_options) }} | ||
{% endif %} | ||
|
||
{%- if subpackages %} | ||
Subpackages | ||
----------- | ||
|
||
{{ toctree(subpackages) }} | ||
{% endif %} | ||
|
||
{%- if submodules %} | ||
Submodules | ||
---------- | ||
{% if separatemodules %} | ||
{{ toctree(submodules) }} | ||
{% else %} | ||
{%- for submodule in submodules %} | ||
{% if show_headings %} | ||
{{- [submodule] | join(" ") | e | heading(2) }} | ||
{% endif %} | ||
{{ automodule(submodule, automodule_options) }} | ||
{% endfor %} | ||
{%- endif %} | ||
{%- endif %} | ||
|
||
{%- if not modulefirst and not is_namespace %} | ||
Module contents | ||
--------------- | ||
|
||
{{ automodule(pkgname, automodule_options) }} | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{{ header | heading }} | ||
|
||
.. toctree:: | ||
:maxdepth: {{ maxdepth }} | ||
{% for docname in docnames %} | ||
{{ docname }} | ||
{%- endfor %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
Oops, something went wrong.