Skip to content

Commit

Permalink
experiment preview with jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaputko committed Jan 3, 2024
1 parent 09285d2 commit cf4adbc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
16 changes: 10 additions & 6 deletions smartsim/_core/control/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import typing as t
import jinja2


class Viewexp:
def __init__(self, exp_entity: t.Any) -> None:
self.exp_entity = exp_entity

def to_human_readable(self) -> str:
preview = "\n=== Experiment Overview ===\n"
preview += f"\tExperiment: {self.exp_entity.name}\n"
preview += f"\tExperiment Path: {self.exp_entity.exp_path}\n"
preview += f"\tLauncher: {self.exp_entity.get_launcher()}\n"
def render(self) -> str:
"""
Render the template from the supplied entity
"""
loader = jinja2.PackageLoader("templates")
env = jinja2.Environment(loader=loader, autoescape=True)
tpl = env.get_template("master.pytpl")
template = tpl.render(exp_entity=self.exp_entity)

return preview
return template
9 changes: 6 additions & 3 deletions smartsim/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def preview(
output_format: t.Optional[str] = None,
output_filename: t.Optional[str] = None,
verbosity_level: t.Optional[str] = None,
) -> None:
) -> str:
"""Preview entity information prior to launch. This method
aggregates multiple pieces of information to give users insight
into what and how entities will be launched. Any instance of
Expand Down Expand Up @@ -832,11 +832,14 @@ def preview(
if verbosity_level:
raise NotImplementedError

logger.info(self._preview())
return self._preview()

def _preview(self) -> str:
"""String formatting"""
preview = Viewexp(self).to_human_readable()

preview = Viewexp(self).render()
logger.info(preview)

return preview

def get_launcher(self) -> str:
Expand Down
4 changes: 4 additions & 0 deletions templates/templates/experiment.pytpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== Experiment Overview ===
Experiment: {{ exp_entity.name }}
Experiment Path: {{ exp_entity.exp_path }}
Launcher: {{ exp_entity.get_launcher() }}
2 changes: 2 additions & 0 deletions templates/templates/master.pytpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{% extends "experiment.pytpl" %}
30 changes: 20 additions & 10 deletions tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,53 @@


def test_experiment_preview(test_dir, wlmutils):
"""Test correct preview output for Experiment preview"""
"""Test correct preview output items for Experiment preview"""
test_launcher = wlmutils.get_test_launcher()
exp_name = "test_prefix"
exp = Experiment(exp_name, exp_path=test_dir, launcher=test_launcher)
# Call method for string formatting for testing
output = exp._preview().strip()

summary_lines = output.split("\n")
summary_lines = [item.replace("\t", "") for item in summary_lines[-3:]]

assert 3 == len(summary_lines)
summary_dict = dict(row.split(": ") for row in summary_lines)
assert set(["Experiment", "Experiment Path", "Launcher"]).issubset(summary_dict)


def test_experiment_preview_properties(test_dir, wlmutils):
"""Test correct preview output properties for Experiment preview"""
test_launcher = wlmutils.get_test_launcher()
exp_name = "test_experiment_preview_properties"
exp = Experiment(exp_name, exp_path=test_dir, launcher=test_launcher)
# Call method for string formatting for testing
output = exp._preview().strip()
summary_lines = output.split("\n")
summary_lines = [item.replace("\t", "") for item in summary_lines[-3:]]
assert 3 == len(summary_lines)
summary_dict = dict(row.split(": ") for row in summary_lines)
assert exp.name == summary_dict["Experiment"]
assert exp.exp_path == summary_dict["Experiment Path"]
assert exp.get_launcher() == summary_dict["Launcher"]


def test_output_format_error():
exp_name = "test_prefix"
exp_name = "test_output_format"
exp = Experiment(exp_name)

with pytest.raises(NotImplementedError):
exp.preview(exp_name, output_format="hello")
exp.preview(output_format="hello")


def test_output_filename_error():
exp_name = "test_prefix"
exp_name = "test_output_filename"
exp = Experiment(exp_name)

with pytest.raises(NotImplementedError):
exp.preview(exp_name, output_filename="hello")
exp.preview(output_filename="hello")


def test_verbosity_level_error():
exp_name = "test_prefix"
exp_name = "test_verbosity_level"
exp = Experiment(exp_name)

with pytest.raises(NotImplementedError):
exp.preview(exp_name, verbosity_level="hello")
exp.preview(verbosity_level="hello")

0 comments on commit cf4adbc

Please sign in to comment.