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

Rename cplots to convergence_plots #1770

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions docs/io/visualization/convergence_plot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"id": "dc1a0c1f",
"metadata": {},
"source": [
"The Convergence Plots consist of two Plotly FigureWidget Subplots, the `plasma_plot` and the `t_inner_luminosities_plot`. The plots are stored in the `cplots` attribute of the simulation object `sim` and can be accessed using `sim.cplots.plasma_plot` and `sim.cplots.t_inner_luminosities_plot`.\n",
"The Convergence Plots consist of two Plotly FigureWidget Subplots, the `plasma_plot` and the `t_inner_luminosities_plot`. The plots are stored in the `convergence_plots` attribute of the simulation object `sim` and can be accessed using `sim.convergence_plots.plasma_plot` and `sim.convergence_plots.t_inner_luminosities_plot`.\n",
"\n",
"The Convergence Plots are shown by default when you running TARDIS because `show_cplots` parameter of the `run_tardis()` function is set to `True`. If you don't want to do this, set it to `False`. "
"The Convergence Plots are shown by default when you running TARDIS because `show_convergence_plots` parameter of the `run_tardis()` function is set to `True`. If you don't want to do this, set it to `False`. "
]
},
{
Expand All @@ -27,7 +27,7 @@
"\n",
"Note\n",
" \n",
"You only need to include `export_cplots=True` in the `run_tardis` function when you want to share the notebook. The function shows the plot using the Plotly `notebook_connected` renderer, which helps display the plot online. You don't need to do it when running the notebook locally.\n",
"You only need to include `export_convergence_plots=True` in the `run_tardis` function when you want to share the notebook. The function shows the plot using the Plotly `notebook_connected` renderer, which helps display the plot online. You don't need to do it when running the notebook locally.\n",
"\n",
"</div>"
]
Expand All @@ -42,7 +42,7 @@
"outputs": [],
"source": [
"from tardis import run_tardis\n",
"sim = run_tardis('tardis_example.yml', export_cplots=True)"
"sim = run_tardis('tardis_example.yml', export_convergence_plots=True)"
]
},
{
Expand All @@ -61,7 +61,7 @@
"metadata": {},
"outputs": [],
"source": [
"sim.cplots.plasma_plot.show(renderer=\"notebook_connected\")"
"sim.convergence_plots.plasma_plot.show(renderer=\"notebook_connected\")"
]
},
{
Expand All @@ -71,7 +71,7 @@
"metadata": {},
"outputs": [],
"source": [
"sim.cplots.t_inner_luminosities_plot.show(renderer=\"notebook_connected\")"
"sim.convergence_plots.t_inner_luminosities_plot.show(renderer=\"notebook_connected\")"
]
},
{
Expand Down Expand Up @@ -127,7 +127,7 @@
" 'rgb(248, 156, 116)',\n",
" 'rgb(220, 176, 242)',\n",
" 'rgb(135, 197, 95)'],\n",
" export_cplots = True\n",
" export_convergence_plots = True\n",
")"
]
},
Expand All @@ -148,10 +148,10 @@
"\n",
"For sake of simplicity, all properties in the data dictionary are applied equally across all traces, meaning traces-specific properties can't be changed from the function. They however be changed after the simulation has finished, for example:\n",
"```py\n",
"sim.cplots.t_inner_luminosities_plot.data[0].line.dash = \"dashdot\"\n",
"sim.convergence_plots.t_inner_luminosities_plot.data[0].line.dash = \"dashdot\"\n",
"```\n",
"\n",
"You can investigate more about the layout/data of any plots by calling `sim.cplots.t_inner_luminosities_plot.layout` or `sim.cplots.t_inner_luminosities_plot.data`. \n",
"You can investigate more about the layout/data of any plots by calling `sim.convergence_plots.t_inner_luminosities_plot.layout` or `sim.convergence_plots.t_inner_luminosities_plot.data`. \n",
"\n",
"Here is an example:"
]
Expand Down Expand Up @@ -195,7 +195,7 @@
" \n",
" },\n",
" },\n",
" export_cplots = True)"
" export_convergence_plots = True)"
]
},
{
Expand All @@ -214,7 +214,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand Down
10 changes: 5 additions & 5 deletions tardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run_tardis(
packet_source=None,
simulation_callbacks=[],
virtual_packet_logging=False,
show_cplots=True,
show_convergence_plots=True,
log_level=None,
specific_log_level=None,
**kwargs,
Expand Down Expand Up @@ -50,7 +50,7 @@ def run_tardis(
If True, only show the log messages from a particular log level, set by `log_level`.
If False, the logger shows log messages belonging to the level set and all levels above it in severity.
The default value None means that the `specific_log_level` specified in the configuration file will be used.
show_cplots : bool, default: True, optional
show_convergence_plots : bool, default: True, optional
Option to enable tardis convergence plots.
**kwargs : dict, optional
Optional keyword arguments including those
Expand Down Expand Up @@ -81,8 +81,8 @@ def run_tardis(
)
tardis_config = Configuration.from_config_dict(config)

if not isinstance(show_cplots, bool):
raise TypeError("Expected bool in show_cplots argument")
if not isinstance(show_convergence_plots, bool):
raise TypeError("Expected bool in show_convergence_plots argument")

logging_state(log_level, tardis_config, specific_log_level)

Expand All @@ -100,7 +100,7 @@ def run_tardis(
packet_source=packet_source,
atom_data=atom_data,
virtual_packet_logging=virtual_packet_logging,
show_cplots=show_cplots,
show_convergence_plots=show_convergence_plots,
**kwargs,
)
for cb in simulation_callbacks:
Expand Down
77 changes: 44 additions & 33 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Simulation(PlasmaStateStorerMixin, HDFWriterMixin):
luminosity_nu_start : astropy.units.Quantity
luminosity_nu_end : astropy.units.Quantity
luminosity_requested : astropy.units.Quantity
cplots_kwargs: dict
convergence_plots_kwargs: dict
nthreads : int
The number of threads to run montecarlo with

Expand Down Expand Up @@ -132,8 +132,8 @@ def __init__(
luminosity_requested,
convergence_strategy,
nthreads,
show_cplots,
cplots_kwargs,
show_convergence_plots,
convergence_plots_kwargs,
):

super(Simulation, self).__init__(iterations, model.no_of_shells)
Expand Down Expand Up @@ -168,17 +168,23 @@ def __init__(
f"- input is {convergence_strategy.type}"
)

if show_cplots:
self.cplots = ConvergencePlots(
iterations=self.iterations, **cplots_kwargs
if show_convergence_plots:
self.convergence_plots = ConvergencePlots(
iterations=self.iterations, **convergence_plots_kwargs
)

if "export_cplots" in cplots_kwargs:
if not isinstance(cplots_kwargs["export_cplots"], bool):
raise TypeError("Expected bool in export_cplots argument")
self.export_cplots = cplots_kwargs["export_cplots"]
if "export_convergence_plots" in convergence_plots_kwargs:
if not isinstance(
convergence_plots_kwargs["export_convergence_plots"], bool
):
raise TypeError(
"Expected bool in export_convergence_plots argument"
)
self.export_convergence_plots = convergence_plots_kwargs[
"export_convergence_plots"
]
else:
self.export_cplots = False
self.export_convergence_plots = False

self._callbacks = OrderedDict()
self._cb_next_id = 0
Expand Down Expand Up @@ -307,19 +313,19 @@ def advance_state(self):
else:
next_t_inner = self.model.t_inner

if hasattr(self, "cplots"):
self.cplots.fetch_data(
if hasattr(self, "convergence_plots"):
self.convergence_plots.fetch_data(
name="t_inner",
value=self.model.t_inner.value,
item_type="value",
)
self.cplots.fetch_data(
self.convergence_plots.fetch_data(
name="t_rad", value=self.model.t_rad, item_type="iterable"
)
self.cplots.fetch_data(
self.convergence_plots.fetch_data(
name="w", value=self.model.w, item_type="iterable"
)
self.cplots.fetch_data(
self.convergence_plots.fetch_data(
name="velocity", value=self.model.velocity, item_type="iterable"
)

Expand Down Expand Up @@ -377,18 +383,18 @@ def iterate(self, no_of_packets, no_of_virtual_packets=0, last_run=False):
reabsorbed_luminosity = self.runner.calculate_reabsorbed_luminosity(
self.luminosity_nu_start, self.luminosity_nu_end
)
if hasattr(self, "cplots"):
self.cplots.fetch_data(
if hasattr(self, "convergence_plots"):
self.convergence_plots.fetch_data(
name="Emitted",
value=emitted_luminosity.value,
item_type="value",
)
self.cplots.fetch_data(
self.convergence_plots.fetch_data(
name="Absorbed",
value=reabsorbed_luminosity.value,
item_type="value",
)
self.cplots.fetch_data(
self.convergence_plots.fetch_data(
name="Requested",
value=self.luminosity_requested.value,
item_type="value",
Expand All @@ -413,8 +419,8 @@ def run(self):
)
self.iterate(self.no_of_packets)
self.converged = self.advance_state()
if hasattr(self, "cplots"):
self.cplots.update()
if hasattr(self, "convergence_plots"):
self.convergence_plots.update()
self._call_back()
if self.converged:
if self.convergence_strategy.stop_if_converged:
Expand All @@ -432,13 +438,16 @@ def run(self):
)

self.reshape_plasma_state_store(self.iterations_executed)
if hasattr(self, "cplots"):
self.cplots.fetch_data(
if hasattr(self, "convergence_plots"):
self.convergence_plots.fetch_data(
name="t_inner",
value=self.model.t_inner.value,
item_type="value",
)
self.cplots.update(export_cplots=self.export_cplots, last=True)
self.convergence_plots.update(
export_convergence_plots=self.export_convergence_plots,
last=True,
)

logger.info(
f"Simulation finished in {self.iterations_executed:d} iterations "
Expand Down Expand Up @@ -582,7 +591,7 @@ def from_config(
config,
packet_source=None,
virtual_packet_logging=False,
show_cplots=True,
show_convergence_plots=True,
**kwargs,
):
"""
Expand Down Expand Up @@ -629,16 +638,18 @@ def from_config(
virtual_packet_logging=virtual_packet_logging,
)

cplots_config_options = [
convergence_plots_config_options = [
"plasma_plot_config",
"t_inner_luminosities_config",
"plasma_cmap",
"t_inner_luminosities_colors",
"export_cplots",
"export_convergence_plots",
]
cplots_kwargs = {}
for item in set(cplots_config_options).intersection(kwargs.keys()):
cplots_kwargs[item] = kwargs[item]
convergence_plots_kwargs = {}
for item in set(convergence_plots_config_options).intersection(
kwargs.keys()
):
convergence_plots_kwargs[item] = kwargs[item]

luminosity_nu_start = config.supernova.luminosity_wavelength_end.to(
u.Hz, u.spectral()
Expand All @@ -663,7 +674,7 @@ def from_config(
model=model,
plasma=plasma,
runner=runner,
show_cplots=show_cplots,
show_convergence_plots=show_convergence_plots,
no_of_packets=int(config.montecarlo.no_of_packets),
no_of_virtual_packets=int(config.montecarlo.no_of_virtual_packets),
luminosity_nu_start=luminosity_nu_start,
Expand All @@ -672,5 +683,5 @@ def from_config(
luminosity_requested=config.supernova.luminosity_requested.cgs,
convergence_strategy=config.montecarlo.convergence_strategy,
nthreads=config.montecarlo.nthreads,
cplots_kwargs=cplots_kwargs,
convergence_plots_kwargs=convergence_plots_kwargs,
)
8 changes: 4 additions & 4 deletions tardis/visualization/tools/convergence_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ConvergencePlots(object):
t_inner_luminosities_colors : str or list, optional
String defining cmap for luminosity and inner boundary temperature plot.
The list can be a list of colors in rgb, hex or css-names format as well.
export_cplots : bool, default: False, optional
export_convergence_plots : bool, default: False, optional
If True, plots are displayed again using the `notebook_connected` renderer. This helps
to display the plots in the documentation or in platforms like nbviewer.

Expand Down Expand Up @@ -390,13 +390,13 @@ def update_t_inner_luminosities_plot(self):
4
].hovertemplate = "<b>%{y:.2f}%</b> at X = %{x:,.0f}"

def update(self, export_cplots=False, last=False):
def update(self, export_convergence_plots=False, last=False):
"""
Update the convergence plots every iteration.

Parameters
----------
export_cplots : bool, default: False, optional
export_convergence_plots : bool, default: False, optional
Displays the convergence plots again using plotly's `notebook_connected` renderer.
This helps to display the plots in notebooks when shared on platforms like nbviewer.
Please see https://plotly.com/python/renderers/ for more information.
Expand All @@ -423,7 +423,7 @@ def update(self, export_cplots=False, last=False):

# the display function expects a Widget, while
# fig.show() returns None, which causes the TraitError.
if export_cplots:
if export_convergence_plots:
with suppress(TraitError):
display(
widgets.VBox(
Expand Down
6 changes: 3 additions & 3 deletions tardis/visualization/tools/tests/test_convergence_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
@pytest.fixture(scope="module", params=[0, 1, 2])
def convergence_plots(request):
"""Initialize ConvergencePlots class and build empty plots."""
cplots = ConvergencePlots(iterations=request.param)
cplots.build(display_plot=False)
return cplots
convergence_plots = ConvergencePlots(iterations=request.param)
convergence_plots.build(display_plot=False)
return convergence_plots


@pytest.fixture()
Expand Down