Skip to main content

A generic class which all new Connectors (integrations which provide Simvue tracking for Non-Python software) should inherit from.

Project description

Simvue Integrations - Connectors Template


Simvue

A template which all new Connectors (integrations which provide Simvue tracking for Non-Python software) should inherit from.

WebsiteDocumentation

Implementation

A customised WrappedRun class has been created which has the following methods:

Pre Simulation

The _pre_simulation method is called when the launch() method is called, but before the file monitor is started. This means that it should include:

  • Upload of any input or code files
  • Adding any tags or metadata which are present before the simulation begins
  • Creating alerts
  • Logging events messages required before the simulation begins
  • Adding the process for the simulation, using parameters which will be input into the launch method

Because this class inherits from the base Simvue Run class, all methods associated with that are available through self.

You should also call the functionality from the WrappedRun class at the top of your method using super()._pre_simulation() - this creates a trigger which can be used to abort the file monitor, and checks that a simvue Run has been initialized before the launch method was called.

During Simulation

The _during_simulation method is called when the launch() method is called, and within the self.file_monitor context manager. This is an instance of the FileMonitor class from multiparser, which monitors any files which you wish to track. You should include all of your multiparser code inside this method, eg:

  • Calls to the .track() method, detailing a file to read and the methods or functions used to process and upload data from it
  • Calls to the .tail() method, detailing a file to read line by line as it is written and the methods or functions used to process and upload data from it

Post Simulation

The _post_simulation method is called when the launch() method is called, but after the file monitor is finished. This means that it should include:

  • Upload of any output files
  • Adding any tags or metadata which are generated after the simulation finishes
  • Logging events messages required after the simulation finishes

Because this class inherits from the base Simvue Run class, all methods associated with that are available through self. You should also call the functionality from the WrappedRun class at the bottom of your method using super()._post_simulation() - this adds support for soft aborts of simulation, and termination of simulations with alerts.

Launch

The launch method is the overall method which starts the file monitor and calls the three methods above. This should be overriden to require the user to provide any inputs relevant to running the simulation, for example the path to an executable, the path of an input file, and/or the path of an output directory.

Once you have created your set of required parameters, make sure to call the parent launch method: super().launch().

Load

The load method should allow you to load a set of results from the simulation which have already been run into the Simvue server. This should reuse as many of the other methods already defined as possible, such as the callback and/or parser functions, but note that it will not launch the file monitor.

Installation

To install and use this connector, first create a virtual environment:

python -m venv venv

Then activate it:

source venv/bin/activate

And then use pip to install this module:

pip install simvue-connector

Configuration

The service URL and token can be defined as environment variables:

export SIMVUE_URL=...
export SIMVUE_TOKEN=...

or a file simvue.toml can be created containing:

[server]
url = "..."
token = "..."

The exact contents of both of the above options can be obtained directly by clicking the Create new run button on the web UI. Note that the environment variables have preference over the config file.

Usage example

from simvue_connector.connector import WrappedRun
import multiparser.parsing.tail as mp_tail_parser

# Create a new Connector class which inherits from WrappedRun
class TemperatureRun(WrappedRun):
    script_path: pathlib.Path = None

    # Override the `_pre_simulation` method to launch the process
    def _pre_simulation(self):
        # Call the base method first
        super()._pre_simulation()

        # Add a process to the run using `add_process`
        self.add_process(
            identifier="heating_experiment",
            executable="bash",
            script=self.script_path,
            completion_trigger=self._trigger # Sets a threading Event once the simulation is completed
        )

    # Override the `_during_simulation` method to track the temperature data
    def _during_simulation(self):
        # Use the `tail` method of the Multiparser `FileMonitor` object to track file, line by line
        self.file_monitor.tail(
            path_glob_exprs=str(self.script_path.with_suffix(".csv")),
            parser_func=mp_tail_parser.record_csv, # Use the built-in CSV parser, which returns a dictionary of data and metadata as each line is written
            callback=lambda csv_data, metadata: self.log_metrics( # Use data from those two dictionaries to log a metric:
                {'sample_temperature': csv_data["Temperature"]},
                 time=csv_data["Time"],
                 step=csv_data["Step"],
                 )
        )

    # Override the `_post_simulation` method to upload the final CSV file of temperature data
    def _post_simulation(self):
        self.save_file(self.script_path.with_suffix(".csv"), category="output")

        # And finally call the base method
        super()._post_simulation()

    # Override the `launch` method to accept the path to the bash script
    def launch(self, script_path: str):
        self.script_path = script_path
        # Call the base `launch` method to call the above methods in the correct order
        super().launch()

License

Released under the terms of the Apache 2 license.

Citation

To reference Simvue, please use the information outlined in this citation file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simvue_connector-1.1.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simvue_connector-1.1.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file simvue_connector-1.1.1.tar.gz.

File metadata

  • Download URL: simvue_connector-1.1.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for simvue_connector-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9d471820c0184489e3ccf74a6e9760249047124373792ce64b9ff5e1370096d5
MD5 5213ce5cb2d2cf05caba0fd09726ea9b
BLAKE2b-256 3e45563c096f4176e714c3e52ba4c53ea4ec0537df35f4c74f7dea665b087008

See more details on using hashes here.

Provenance

The following attestation bundles were made for simvue_connector-1.1.1.tar.gz:

Publisher: deploy.yaml on simvue-io/connectors-generic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simvue_connector-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simvue_connector-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f94582da2aae345d80deefddf5bf9bf7c5a0d63bd7059d5eea36bf744ca915b8
MD5 d6fc9e45775a628bef1ab297147a4e6c
BLAKE2b-256 0a167b15e1bcd23f05f665f98aab5fc782377a8989f67800b8f8569e4a5a863e

See more details on using hashes here.

Provenance

The following attestation bundles were made for simvue_connector-1.1.1-py3-none-any.whl:

Publisher: deploy.yaml on simvue-io/connectors-generic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page