Skip to content
Closed
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
8 changes: 3 additions & 5 deletions src/oedisi/componentframework/basic_component.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Generate basic component from description JSON
"""
"""Generate basic component from description JSON."""

import json
import os
Expand All @@ -12,7 +10,7 @@


class ComponentDescription(BaseModel):
"""Component description for simple ComponentType
"""Component description for simple ComponentType.

Parameters
----------
Expand Down Expand Up @@ -45,7 +43,7 @@ def component_from_json(f, type_checker):


def basic_component(comp_desc: ComponentDescription, type_checker):
"""Uses data in component_definition to create a new component type
"""Uses data in component_definition to create a new component type.

Parameters
----------
Expand Down
10 changes: 5 additions & 5 deletions src/oedisi/componentframework/mock_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import helics as h
import logging
from typing import Dict
from typing import Dict, Optional
import json
import os
from . import system_configuration
Expand All @@ -29,9 +29,9 @@ def __init__(
name,
parameters: Dict[str, Dict[str, str]],
directory: str,
host: str = None,
port: int = None,
comp_type: str = None,
host: Optional[str] = None,
port: Optional[int] = None,
comp_type: Optional[str] = None,
):
self._name = name
self._directory = directory
Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(self):
self.fed = h.helicsCreateValueFederateFromConfig("helics_config.json")
logger.info(f"Created federate {self.fed.name}")

with open("input_mapping.json", "r") as f:
with open("input_mapping.json") as f:
port_mapping = json.load(f)
self.subscriptions = {}
for name, key in port_mapping.items():
Expand Down
20 changes: 12 additions & 8 deletions src/oedisi/componentframework/system_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@


class AnnotatedType(BaseModel):
"Class for representing the types of components and their interfaces"
"Class for representing the types of components and their interfaces."

type: str
description: Optional[str]
unit: Optional[str]
Expand Down Expand Up @@ -105,7 +106,8 @@ def connect(self, port: "Port"):

class Component(BaseModel):
"""Component type used in WiringDiagram, includes name,
component type, and initial parameters"""
component type, and initial parameters.
"""

name: str
type: str
Expand All @@ -125,7 +127,8 @@ def validate_image(cls, v, values, **kwargs):


class WiringDiagram(BaseModel):
"Cosimulation configuration. This may end up wrapped in another interface"
"Cosimulation configuration. This may end up wrapped in another interface."

name: str
components: List[Component]
links: List[Link]
Expand Down Expand Up @@ -154,7 +157,7 @@ def clean_model(self, target_directory="."):

@validator("components")
def check_component_names(cls, components):
"Check that the components all have unique names"
"Check that the components all have unique names."
names = set(map(lambda c: c.name, components))
assert len(names) == len(components)
return components
Expand All @@ -180,7 +183,8 @@ def empty(cls, name="unnamed"):


class Federate(BaseModel):
"Federate configuration for HELICS CLI"
"Federate configuration for HELICS CLI."

directory: str
hostname: str = "localhost"
name: str
Expand All @@ -200,7 +204,7 @@ def initialize_federates(
compatability_checker,
target_directory=".",
) -> List[Federate]:
"Initialize all the federates"
"Initialize all the federates."
components = {}
link_map = get_link_map(wiring_diagram)
for component in wiring_diagram.components:
Expand Down Expand Up @@ -255,7 +259,7 @@ def get_link_map(wiring_diagram: WiringDiagram):


class RunnerConfig(BaseModel):
"""HELICS running config for the full simulation
"""HELICS running config for the full simulation.

Examples
--------
Expand Down Expand Up @@ -312,5 +316,5 @@ def generate_runner_config(
exec=f"helics_broker -f {len(federates)} --loglevel=warning",
)
return RunnerConfig(
name=wiring_diagram.name, federates=(federates + [broker_federate])
name=wiring_diagram.name, federates=([*federates, broker_federate])
)
7 changes: 3 additions & 4 deletions src/oedisi/componentframework/wiring_diagram_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Wiring Diagram utilities
"""Wiring Diagram utilities.

Wiring diagrams can be hard to manage in their final list based form.
Some utilities plot, and future additions include nested wiring
diagram composition and a programmatic interface.
"""

from . import system_configuration
from .system_configuration import WiringDiagram


def get_graph(wiring_diagram: WiringDiagram):
"Get networkx graph representation of wiring_diagram"
"Get networkx graph representation of wiring_diagram."
import networkx as nx

g = nx.MultiDiGraph()
Expand Down Expand Up @@ -90,7 +89,7 @@ def plot_graph_bokeh(wiring_diagram: WiringDiagram):
CustomJS,
)
from bokeh.layouts import layout
from bokeh.io import output_file, show
from bokeh.io import show

G = get_graph(wiring_diagram)
graph_renderer = get_graph_renderer(G)
Expand Down
6 changes: 3 additions & 3 deletions src/oedisi/tools/broker_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import helics as h
from pydantic import BaseModel


class TimeData(BaseModel):
"Time data for a federate"
"Time data for a federate."

name: str
granted_time: float
send_time: float


def pprint_time_data(time_data):
"A table would be better somehow, but which should be the columns"
"A table would be better somehow, but which should be the columns."
print(
f"""
Name : {time_data.name}
Expand Down
35 changes: 17 additions & 18 deletions src/oedisi/tools/cli_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def cli():


def bad_type_checker(type, x):
"Does not check types"
"Does not check types."
return True


Expand Down Expand Up @@ -88,7 +88,7 @@ def build(
broker_port,
node_port=30000,
):
"""Build to the simulation folder
r"""Build to the simulation folder.

Examples::

Expand All @@ -97,9 +97,9 @@ def build(
oedisi build --component-dict components.json --system scenario.json

\f

Parameters
----------

target_directory : str (default="build")
build path
system : str (default="system.json")
Expand All @@ -114,7 +114,7 @@ def build(
The port exposed externally from k8s for external access on localhost
"""
click.echo(f"Loading the components defined in {component_dict}")
with open(component_dict, "r") as f:
with open(component_dict) as f:
component_dict_of_files = json.load(f)
component_types = {
name: get_basic_component(component_file)
Expand Down Expand Up @@ -227,12 +227,12 @@ def edit_docker_file(file_path, component: Component):

with open(file_path, "w") as f:
f.write(f"FROM {BASE_DOCKER_IMAGE}\n")
f.write(f"RUN apt-get update\n")
f.write(f"RUN apt-get install -y git ssh\n")
f.write("RUN apt-get update\n")
f.write("RUN apt-get install -y git ssh\n")
f.write(f"RUN mkdir {component.name}\n")
f.write(f"COPY . ./{component.name}\n")
f.write(f"WORKDIR ./{component.name}\n")
f.write(f"RUN pip install -r requirements.txt\n")
f.write("RUN pip install -r requirements.txt\n")
f.write(f"EXPOSE {component.container_port}/tcp\n")
cmd = f'CMD {["python", "server.py", component.host, str(component.container_port)]}\n'
cmd = cmd.replace("'", '"')
Expand All @@ -252,7 +252,7 @@ def create_docker_compose_file(
config = {"services": {}, "networks": {}}

config["services"][f"{APP_NAME}_{BROKER_SERVICE}"] = {
"build": {"context": f"./broker/."},
"build": {"context": "./broker/."},
"image": f"{DOCKER_HUB_USER}/{APP_NAME}_{BROKER_SERVICE}",
"ports": [f"{broker_port}:{broker_port}"],
"networks": {"custom-network": {"ipv4_address": "10.5.0.2"}},
Expand Down Expand Up @@ -293,7 +293,7 @@ def create_docker_compose_file(
help="Location of helics run json. Usually build/system_runner.json",
)
def run(runner):
"""Calls out to helics run command
"""Calls out to helics run command.

Examples::

Expand Down Expand Up @@ -399,7 +399,7 @@ def run_mc(runner, kubernetes, docker_compose):
help="Path to parameters JSON (default is parameters={})",
)
def test_description(target_directory, component_desc, parameters):
"""Test component intialization from component description
r"""Test component intialization from component description.

Examples::

Expand All @@ -414,9 +414,9 @@ def test_description(target_directory, component_desc, parameters):


\f

Parameters
----------

target_directory : str
build location

Expand All @@ -435,7 +435,6 @@ def test_description(target_directory, component_desc, parameters):
inputs and outputs (basically recorder federate?)
Create and run system with wiring diagram
"""

comp_desc = ComponentDescription.parse_file(component_desc)
comp_desc.directory = os.path.dirname(component_desc)

Expand Down Expand Up @@ -495,16 +494,16 @@ def test_description(target_directory, component_desc, parameters):


def remove_from_runner_config(runner_config, element):
"Remove federate from configuration"
"Remove federate from configuration."
within_feds = [fed for fed in runner_config.federates if fed.name != element]
without_feds = [fed for fed in runner_config.federates if fed.name == element]
new_config = RunnerConfig(name=runner_config.name, federates=within_feds)
return new_config, without_feds


def remove_from_json(system_json, element):
"Remove federate from configuration and resave with revised.json"
with open(system_json, "r") as f:
"Remove federate from configuration and resave with revised.json."
with open(system_json) as f:
runner_config = RunnerConfig.parse_obj(json.load(f))
new_config, without_feds = remove_from_runner_config(runner_config, element)

Expand All @@ -524,17 +523,17 @@ def remove_from_json(system_json, element):
)
@click.option("--foreground", type=str, help="Name of component to run in background")
def debug_component(runner, foreground):
"""
Run system runner json with one component in the JSON
r"""
Run system runner json with one component in the JSON.

We remove one component from system_runner.json
and then call helics run in the background with our new json.
and then run our debugging component in standard in / standard out.

\f

Parameters
----------

runner : str
filepath to system runner json

Expand Down
5 changes: 3 additions & 2 deletions src/oedisi/tools/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
- Mean squared relative error.

For angles:
- Mean absolute angle error."""
- Mean absolute angle error.
"""

import click
from pathlib import Path
Expand Down Expand Up @@ -60,9 +61,9 @@ def evaluate_estimate(path, metric, angle_unit):
- MAAE: Mean absolute angle error.

\f

Parameters
----------

path : Path
Path to the folder containing the measurement files.

Expand Down
2 changes: 1 addition & 1 deletion src/oedisi/tools/testing_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ def wait_until_connected(self):
current_state = self.broker.query("broker", "current_state")
cores = current_state["cores"]
if len(cores) == 2 and all(
(core["state"] == "init_requested" for core in cores)
core["state"] == "init_requested" for core in cores
):
return
Loading