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

Fix for widgets displaying code in commandline #2253

Merged
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
39 changes: 23 additions & 16 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,29 @@
)

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

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"
]
if not is_notebook():
raise RuntimeError(
"Convergence Plots cannot be displayed in command-line. Set show_convergence_plots "
"to False."
)
else:
self.export_convergence_plots = False
self.convergence_plots = ConvergencePlots(

Check warning on line 181 in tardis/simulation/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/simulation/base.py#L181

Added line #L181 was not covered by tests
iterations=self.iterations, **convergence_plots_kwargs
)

if "export_convergence_plots" in convergence_plots_kwargs:
if not isinstance(

Check warning on line 186 in tardis/simulation/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/simulation/base.py#L186

Added line #L186 was not covered by tests
convergence_plots_kwargs["export_convergence_plots"],
bool,
):
raise TypeError(

Check warning on line 190 in tardis/simulation/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/simulation/base.py#L190

Added line #L190 was not covered by tests
"Expected bool in export_convergence_plots argument"
)
self.export_convergence_plots = convergence_plots_kwargs[

Check warning on line 193 in tardis/simulation/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/simulation/base.py#L193

Added line #L193 was not covered by tests
"export_convergence_plots"
]
else:
self.export_convergence_plots = False

self._callbacks = OrderedDict()
self._cb_next_id = 0
Expand Down Expand Up @@ -633,7 +640,7 @@
config,
packet_source=None,
virtual_packet_logging=False,
show_convergence_plots=True,
show_convergence_plots=False,
show_progress_bars=True,
**kwargs,
):
Expand Down
9 changes: 9 additions & 0 deletions tardis/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def artis_abundances_fname(example_model_file_dir):
return example_model_file_dir / "artis_abundances.dat"


@pytest.fixture(scope="session")
def monkeysession():
"""
Creates a session-scoped fixture to be used to mock functions dependent on the user.
"""
with pytest.MonkeyPatch.context() as mp:
yield mp


def test_malformed_species_error():
malformed_species_error = MalformedSpeciesError("He")
assert malformed_species_error.malformed_element_symbol == "He"
Expand Down
20 changes: 20 additions & 0 deletions tardis/visualization/tools/tests/test_convergence_plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Tests for Convergence Plots."""
from copy import deepcopy

import pytest
from tardis.tests.test_util import monkeysession
from tardis import run_tardis
from tardis.visualization.tools.convergence_plot import (
ConvergencePlots,
transition_colors,
Expand Down Expand Up @@ -205,3 +209,19 @@ def test_override_plot_parameters(convergence_plots):
assert (
convergence_plots.plasma_plot["layout"]["xaxis2"]["showgrid"] == False
)


def test_convergence_plot_command_line(
config_verysimple, atomic_dataset, monkeysession
):
monkeysession.setattr(
"tardis.simulation.base.is_notebook",
lambda: False,
)
atomic_data = deepcopy(atomic_dataset)
with pytest.raises(RuntimeError):
run_tardis(
config_verysimple,
atom_data=atomic_data,
show_convergence_plots=True,
)
197 changes: 103 additions & 94 deletions tardis/visualization/widgets/custom_abundance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

import tardis
from tardis.io.model.readers.generic_readers import read_uniform_abundances
from tardis.util.base import quantity_linspace, is_valid_nuclide_or_elem
from tardis.util.base import (
quantity_linspace,
is_valid_nuclide_or_elem,
is_notebook,
)
from tardis.io.configuration.config_reader import Configuration
from tardis.model import SimulationState
from tardis.io.model.parse_density_configuration import (
Expand Down Expand Up @@ -1284,109 +1288,114 @@
ipywidgets.widgets.widget_box.VBox
A box that contains all the widgets in the GUI.
"""
# --------------Combine widget components--------------
self.box_editor = ipw.HBox(
[
ipw.VBox(self.input_items),
ipw.VBox(self.checks, layout=ipw.Layout(margin="0 0 0 10px")),
]
)

box_add_shell = ipw.HBox(
[
self.input_v_start,
self.input_v_end,
self.btn_add_shell,
self.overwrite_warning,
],
layout=ipw.Layout(margin="0 0 0 50px"),
)

box_head = ipw.HBox(
[self.dpd_shell_no, self.btn_prev, self.btn_next, box_add_shell]
)
if not is_notebook():
print("Please use a notebook to display the widget")

Check warning on line 1292 in tardis/visualization/widgets/custom_abundance.py

View check run for this annotation

Codecov / codecov/patch

tardis/visualization/widgets/custom_abundance.py#L1292

Added line #L1292 was not covered by tests
else:
# --------------Combine widget components--------------
self.box_editor = ipw.HBox(
[
ipw.VBox(self.input_items),
ipw.VBox(
self.checks, layout=ipw.Layout(margin="0 0 0 10px")
),
]
)

box_add_element = ipw.HBox(
[self.input_symb, self.btn_add_element, self.symb_warning],
layout=ipw.Layout(margin="0 0 0 80px"),
)
box_add_shell = ipw.HBox(
[
self.input_v_start,
self.input_v_end,
self.btn_add_shell,
self.overwrite_warning,
],
layout=ipw.Layout(margin="0 0 0 50px"),
)

help_note = ipw.HTML(
value="<p style='text-indent: 40px'>* Select a checkbox "
"to lock the abundance of corresponding element. </p>"
"<p style='text-indent: 40px'> On clicking the 'Normalize' "
"button, the locked abundance(s) will <b>not be normalized</b>."
" </p>",
indent=True,
)
box_head = ipw.HBox(
[self.dpd_shell_no, self.btn_prev, self.btn_next, box_add_shell]
)

self.abundance_note = ipw.HTML(
description="(The following abundances are for the innermost "
"shell in selected range.)",
layout=ipw.Layout(visibility="hidden"),
style={"description_width": "initial"},
)
box_add_element = ipw.HBox(
[self.input_symb, self.btn_add_element, self.symb_warning],
layout=ipw.Layout(margin="0 0 0 80px"),
)

box_norm = ipw.HBox([self.btn_norm, self.norm_warning])
help_note = ipw.HTML(
value="<p style='text-indent: 40px'>* Select a checkbox "
"to lock the abundance of corresponding element. </p>"
"<p style='text-indent: 40px'> On clicking the 'Normalize' "
"button, the locked abundance(s) will <b>not be normalized</b>."
" </p>",
indent=True,
)

box_apply = ipw.VBox(
[
ipw.Label(value="Apply abundance(s) to:"),
self.rbs_single_apply,
ipw.HBox(
[
self.rbs_multi_apply,
self.irs_shell_range,
self.abundance_note,
]
),
],
layout=ipw.Layout(margin="0 0 15px 50px"),
)
self.abundance_note = ipw.HTML(
description="(The following abundances are for the innermost "
"shell in selected range.)",
layout=ipw.Layout(visibility="hidden"),
style={"description_width": "initial"},
)

box_features = ipw.VBox([box_norm, help_note])
box_abundance = ipw.VBox(
[
box_apply,
ipw.HBox([self.box_editor, box_features]),
box_add_element,
]
)
box_density = self.density_editor.display()
box_norm = ipw.HBox([self.btn_norm, self.norm_warning])

box_apply = ipw.VBox(
[
ipw.Label(value="Apply abundance(s) to:"),
self.rbs_single_apply,
ipw.HBox(
[
self.rbs_multi_apply,
self.irs_shell_range,
self.abundance_note,
]
),
],
layout=ipw.Layout(margin="0 0 15px 50px"),
)

main_tab = ipw.Tab([box_abundance, box_density])
main_tab.set_title(0, "Edit Abundance")
main_tab.set_title(1, "Edit Density")
box_features = ipw.VBox([box_norm, help_note])
box_abundance = ipw.VBox(
[
box_apply,
ipw.HBox([self.box_editor, box_features]),
box_add_element,
]
)
box_density = self.density_editor.display()

hint = ipw.HTML(
value="<b><font size='3'>Save model as file: </font></b>"
)
box_output = ipw.VBox(
[
hint,
self.input_i_time_0,
ipw.HBox(
[self.input_path, self.btn_output, self.ckb_overwrite]
),
]
)
main_tab = ipw.Tab([box_abundance, box_density])
main_tab.set_title(0, "Edit Abundance")
main_tab.set_title(1, "Edit Density")

# Initialize the widget and plot colormap
self.plot_cmap = cmap
self.update_line_color()
self.read_abundance()
self.density_editor.read_density()
hint = ipw.HTML(
value="<b><font size='3'>Save model as file: </font></b>"
)
box_output = ipw.VBox(
[
hint,
self.input_i_time_0,
ipw.HBox(
[self.input_path, self.btn_output, self.ckb_overwrite]
),
]
)

return ipw.VBox(
[
self.tbs_scale,
self.fig,
box_head,
main_tab,
box_output,
self.error_view,
]
)
# Initialize the widget and plot colormap
self.plot_cmap = cmap
self.update_line_color()
self.read_abundance()
self.density_editor.read_density()

return ipw.VBox(
[
self.tbs_scale,
self.fig,
box_head,
main_tab,
box_output,
self.error_view,
]
)

@error_view.capture(clear_output=True)
def to_csvy(self, path, overwrite):
Expand Down
Loading
Loading