Exemplo n.º 1
0
def test_visualize_save_cannot(flash_model, tmp_path):
    flowsheet = flash_model.fs
    with pytest.raises(errors.VisualizerError):
        fsvis.visualize(flowsheet,
                        "foo",
                        save="foo",
                        save_dir=Path("/a/b/c/d/e/f/g"))
Exemplo n.º 2
0
def test_visualize_save_versions(flash_model, save_files_prefix):
    # test versioned file saves
    flowsheet = flash_model.fs
    path = Path(save_files_prefix + "_save")
    work_dir = path.parent
    fs_name = path.name
    for i in range(4):
        save_arg = (True,
                    None)[i % 2]  # try both kinds of 'use default' values
        if i < 3:
            result = fsvis.visualize(flowsheet,
                                     fs_name,
                                     save_dir=work_dir,
                                     browser=False,
                                     save=save_arg,
                                     load_from_saved=False)
            if i == 0:
                assert re.search(f"{path.name}.json", result.store.filename)
            else:
                assert re.search(f"{path.name}.*{i}.*\.json",
                                 result.store.filename)
        else:
            msv, fsvis.MAX_SAVED_VERSIONS = fsvis.MAX_SAVED_VERSIONS, i - 1
            with pytest.raises(RuntimeError):
                fsvis.visualize(flowsheet,
                                fs_name,
                                save_dir=work_dir,
                                browser=False,
                                load_from_saved=False)
            fsvis.MAX_SAVED_VERSIONS = msv
Exemplo n.º 3
0
def test_visualize_fn(flash_model):
    flowsheet = flash_model.fs
    result = fsvis.visualize(flowsheet, browser=False, save=False)
    assert result.store.filename == ""
    #
    for bad_save_as in (1, "/no/such/file/exists.I.hope", flowsheet):
        with pytest.raises(errors.VisualizerError):
            fsvis.visualize(flowsheet, save=bad_save_as, browser=False)
Exemplo n.º 4
0
    def visualize(self, model_name, **kwargs):
        """
        Starts up a flask server that serializes the model and pops up a 
        webpage with the visualization

        Args:
            model_name : The name of the model that flask will use as an argument
                         for the webpage
        Keyword Args:
            **kwargs: Additional keywords for :func:`idaes.ui.fsvis.visualize()`

        Returns:
            None
        """
        visualize(self, model_name, **kwargs)
Exemplo n.º 5
0
def test_visualize_save_explicit(flash_model, save_files_prefix):
    # test explicit filename
    flowsheet = flash_model.fs
    howdy = Path(save_files_prefix + "_howdy")
    result = fsvis.visualize(flowsheet, "flowsheet", save=howdy, browser=False)
    assert re.search(howdy.name, result.store.filename)
    # overwrite but this time break explicit file into relative name and directory
    result = fsvis.visualize(
        flowsheet,
        "flowsheet",
        save=howdy.name,
        save_dir=howdy.parent,
        browser=False,
        overwrite=True,
    )
    assert re.search(howdy.name, result.store.filename)
Exemplo n.º 6
0
def test_flowsheet_name(flash_model, tmp_path):
    raw_name = "Hello World"
    result = fsvis.visualize(flash_model.fs,
                             name=raw_name,
                             browser=False,
                             save_dir=tmp_path)
    assert re.search(raw_name, result.store.filename)
Exemplo n.º 7
0
    def visualize(self, model_name, browser=True, overwrite=False):
        """
        Starts up a flask server that serializes the model and pops up a 
        webpage with the visualization

        Args:
            model_name : The name of the model that flask will use as an argument
                         for the webpage
            browser : If True a browser window/tab will be opened with the 
                      visualization. Defaults to True
            overwrite : If True the visualization ignores any saved visualization 
                        file

        Returns:
            None
        """
        visualize(self, model_name, browser, overwrite)
Exemplo n.º 8
0
def test_mock_webbrowser(flash_model):
    from idaes.ui.fsvis import fsvis

    wb = fsvis.webbrowser
    for wb_mock in (MockWB(True), MockWB(False)):
        fsvis.webbrowser = wb_mock
        _ = fsvis.visualize(flash_model.fs, save=False)
    fsvis.webbrowser = wb
Exemplo n.º 9
0
def test_visualize_save_loadfromsaved(flash_model, save_files_prefix):
    flowsheet = flash_model.fs
    name = "flash_tvslfs"
    save_dir = Path(save_files_prefix).parent
    # save initial
    result = fsvis.visualize(flowsheet, name, save_dir=save_dir)
    path_base = save_dir / (name + ".json")
    assert path_base.exists()
    # this time, should use loaded one
    # there should still be only one file
    result = fsvis.visualize(flowsheet, name, save_dir=save_dir)
    path_v1 = save_dir / (name + "-1.json")
    assert not path_v1.exists()
    # same behavior with explicit flag
    result = fsvis.visualize(flowsheet,
                             name,
                             save_dir=save_dir,
                             load_from_saved=True)
    assert not path_v1.exists()
Exemplo n.º 10
0
def test_visualize_save_overwrite(flash_model, save_files_prefix):
    flowsheet = flash_model.fs
    howdy = Path(save_files_prefix + "_howdy")
    howdy.open("w").write("howdy")
    howdy_stat = os.stat(howdy)
    result = fsvis.visualize(
        flowsheet, "flowsheet", save=howdy, overwrite=True, browser=False
    )
    howdy_stat2 = os.stat(result.store.filename)
    assert (
        howdy_stat2.st_mtime > howdy_stat.st_mtime
    )  # modification time should be later
Exemplo n.º 11
0
def test_save_visualization(flash_model, tmp_path):
    # view logs from the persistence module
    logging.getLogger("idaes.ui.fsvis").setLevel(logging.DEBUG)
    flowsheet = flash_model.fs
    # Start the visualization server, using temporary save location
    save_location = tmp_path / "flash-vis.json"
    fsvis_result = fsvis.visualize(
        flowsheet, "Flash", browser=False, save=save_location, save_dir=tmp_path
    )
    # Check the contents of the saved file are the same as what is returned by the server
    with open(fsvis_result.store.filename) as fp:
        file_data = json.load(fp)
    resp = requests.get(f"http://127.0.0.1:{fsvis_result.port}/fs?id=Flash")
    net_data = resp.json()
    assert file_data == net_data
Exemplo n.º 12
0
def test_visualize(flash_model, tmp_path):
    from pathlib import Path

    flowsheet = flash_model.fs
    # Start the visualization server
    result = fsvis.visualize(flowsheet,
                             "Flash",
                             browser=False,
                             save_dir=tmp_path)
    # Get the model
    resp = requests.get(f"http://127.0.0.1:{result.port}/fs?id=Flash")
    data = resp.json()
    # Validate the model
    ok, msg = validate_flowsheet(data)
    assert ok, f"Invalid flowsheet returned: {msg}"
    assert data["model"]["id"] == "Flash"
    assert data["model"]["unit_models"]["flash"]["type"] == "flash"
    assert len(data["cells"]) == 7
    units = [x for x in data["cells"] if x["type"] == "standard.Image"]
    assert len(units) == 4
    unit_images = [Path(x["attrs"]["image"]["xlinkHref"]).name for x in units]
    unit_images.sort()
    assert unit_images == [
        "feed.svg", "flash.svg", "product.svg", "product.svg"
    ]
    # Modify the model by deleting its one and only component
    flowsheet.del_component("flash")
    # Get the model (again)
    resp = requests.get(f"http://127.0.0.1:{result.port}/fs?id=Flash")
    data = resp.json()
    # Validate the modified model
    expected = {
        "model": {
            "id": "Flash",
            "stream_table": {
                "columns": ["", "Variable"],
                "data": [],
                "index": []
            },
            "unit_models": {},
            "arcs": {},
        },
        "cells": [],
    }
    assert data == expected
Exemplo n.º 13
0
def test_visualize(flash_model):
    from pathlib import Path
    flowsheet = flash_model.fs
    # Start the visualization server
    port = fsvis.visualize(flowsheet, "Flash", browser=False, save_as=None)
    # Get the model
    resp = requests.get(f"http://127.0.0.1:{port}/fs?id=Flash")
    data = resp.json()
    # Validate the model
    ok, msg = validate_flowsheet(data)
    assert ok, f"Invalid flowsheet returned: {msg}"
    assert data["model"]["id"] == "Flash"
    assert data["model"]["unit_models"]["flash"]["type"] == "flash"
    assert len(data["cells"]) == 7
    units = [x for x in data["cells"] if x["type"] == "standard.Image"]
    assert len(units) == 4
    unit_images = [Path(x["attrs"]["image"]["xlinkHref"]).name for x in units]
    unit_images.sort()
    assert unit_images == [
        "feed.svg", "flash.svg", "product.svg", "product.svg"
    ]
    # Modify the model by deleting its one and only component
    flowsheet.del_component("flash")
    # Get the model (again)
    resp = requests.get(f"http://127.0.0.1:{port}/fs?id=Flash")
    data = resp.json()
    # Validate the modified model
    expected = {
        'model': {
            'id': 'Flash',
            'unit_models': {},
            'arcs': {}
        },
        'cells': []
    }
    assert data == expected