Skip to main content

ML Pipeline

Machine learning pipeline.

How to create and run an Agent?

To create an Agent you need to clone the ml-pipe-service repository and install ml-pipe-core.

git clone 

mkdir .venv
pipenv install .

Afterwards you create a class which inherits form SimpleService.

class SillyAgent(SimpleService):
    def __init__(self, name, adapter):
        super().__init__(name, adapter)
        self.factor = 1.0

    def reconfig_event(self, msg):
        factor = msg.get('factor')
        if factor is None:
            _logger.error("reconfig message doesn't have 'factor' as a key.")
        else:
            _logger.debug(f"set new factor to: {factor}")
            self.factor = factor

    def proposal(self, params):
        _logger.debug(f"use factor: {self.factor}")
        hcors = self.machine_adapter.get_hcors(names=self.machine_adapter.get_hcor_device_names(), is_group_call=True)
        self.set_machine(SetMachineMessage({'hcor': {"names": self.machine_adapter.get_hcor_device_names(),  "values": [val * self.factor for val in hcors]}}))

if __name__ == '__main__':
    agent = SillyAgent('silly_agent',  PetraSimulationAdapter())
    agent.init_local()

Inside this class you need to overload

def proposal(self, params)

which will be called when the Agent is triggered. The Machine/Simulation can be set with

def set_machine(self, data: SetMachineMessage)

This function is taking an SetMachineMessage as input parameter. SetMachineMessage holds a python dictionary with which you can set the Machine/Simulation. Currently only setting horizontal/vertical correctors, machine parameters and twiss parameters are supported. The structure of the dictionary have to look like this:

{'hcor': {'name': str, 'values': List[float]},
 'vcor': {'name': str, 'values': List[float]},
 'machineparams': {'name': str, 'values': List[float]}, 
 'twiss': {'name': str, 'mat': List[List[float]]} # only for simulation
 }

Optionally you can overload

def reconfig_event(self, msg) 

which give you the possibility to reset internal parameters without restarting your Agent.

After creating an Agent you need configure the agent in a yaml file.

Here is an example of config.yaml file.

Notes: You have to replace localhost with the real server name.

observer:
  # used to check if jbb is done
  url: localhost:5003
  event_db_pw: xxxxx
  # event_db_url:
  event_db_usr: xxxxx
register:
  # registers all services
  url: localhost:5004
simulation:
  # sql database which maps the machine parameter
  sim_db_pw: xxxxx
  sim_db_usr: xxxxx
  sim_db_url: localhost:3306
msg_bus:
  # message bus
  broker_urls: localhost:9094,localhost:9096

Afterwards, set the path to the config yaml file in the environment value PIPE_CONFIG_FILEPATH.

PIPE_CONFIG_FILEPATH = /path/to/ml-pipe-config.yaml

To run the Agent you you can activate the python env.

pipenv shell

Afterwards you can run the Agent

python agents/testing/silly_agent.py

To interact with the pipeline (e.g trigger, start, list, stop Agents) you can used KafkaPipeClient.

from ml_pipe_core.client.kafka_client import KafkaPipeClient

pipe = KafkaPipeClient()
print(pipe.get_running_service_info())
p_id = pipe.run_service('SillyAgent')
pipe.get_observation(p_id) # wait for response

An GUI will be come soon.

Developer: Core Services and ml_pipe_core Package

The ML-Pipeline is using docker to run the infrastructure and test it locally. For this you need docker and docker-compose >= 1.28.0.

Build Docker images

To build all docker images run:

make build-all

This can take a while...

Notes: After changing code you just need to rebuild the service images, which is much faster.

make build-service-images

Start Core Services

To Start the core services of the ml-pipeline you need to set the following environment values

PIPE_EXTERNAL_HOST_ADDR=localhost
PIPE_DB_PW=xxxx
PIPE_DB_USER=xxxx
PIPE_CONFIG_FILEPATH = /path/to/ml-pipe-config.yaml
PATH_TO_ML_PIPE_SERVICES_ROOT=/path/to/ml-pipe-services

PIPE_CONFIG_FILEPATH is the path to the yaml config file, which look like this:

observer:
  # used to check if jbb is done
  url: observer:5003
  event_db_pw: xxxx
  # event_db_url:
  event_db_usr: xxxx
register:
  # registers all services
  url: register:5004
simulation:
  # sql database which maps the machine parameter
  sim_db_pw: xxxx
  sim_db_usr: xxxx
  sim_db_url: simulation_database:3306
msg_bus:
  # message bus
  # external_host_addr: localhost
  broker_urls: kafka_1:9092,kafka_2:9096

In production replace PIPE_EXTERNAL_HOST_ADDR=localhost with the server url and set PATH_TO_ML_PIPE_SERVICES_ROOT to the location of the cloned ml-pipe-services repository. The environment values PIPE_DB_PW and PIPE_DB_USER define the credentials for the databases used by the pipeline.

Stop Core Services

You have to stop the docker container at the end.

make down

Tests locally

Note: Docker and docker-compose => 2.80 is required to run tests locally

Run all tests:

make tests ENV_FILE=.env

Run end to end tests:

make e2e-tests ENV_FILE=.env

Run integration tests:

make integration-tests ENV_FILE=.env

Run unit tests

make unit-tests ENV_FILE=.env

Additional Stuff

Maxwell

Log in via ssh to max-wgs.desy.de

Install python 3.8.8

wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
sh Anaconda3-2021.05-Linux-x86_64.sh
export PATH=$HOME/anaconda3/bin:$PATH

PyTine and K2I2K_os on Machine PETRA III

The machine observer and controller for PETRA III are using the PetraAdapter which is using the libs PyTine and K2I2K_os. The Path to this libs have to be added to the PYTHONPATH.

For PyTine have a look at https://confluence.desy.de/display/HLC/Developing+with+Python.

K2I2K_os can be cloned via git from:

git clone https://username@stash.desy.de/scm/pihp/petra3.optics.tools.git

Jupyter notebook

To use KafkaPipeClient in a Jupyter notebook you need to add the virtual environment to Jupyter.

First activate the python virtual environment.

For Pipenv

pipenv shell

Start the jupyter notebook:

pip install --user ipykernel

Note: You have to add the virtual enviroment to jupyter. First, activate the virutal environment. Then run:

python -m ipykernel install --user --name=<myenv> 

Metadata

Release files for ml-pipe-core 0.0.23

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ml-pipe-core 0.0.23
File Size Uploaded Provenance Actions
ml_pipe_core-0.0.23.tar.gz 75.6 kB Without provenance Details for ml_pipe_core-0.0.23.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ml-pipe-core 0.0.23
File Interpreter ABI Platform Actions
ml_pipe_core-0.0.23-py3-none-any.whl Python 3 none any Details for ml_pipe_core-0.0.23-py3-none-any.whl

Total release size: 158.5 kB

Release files / ml_pipe_core-0.0.23.tar.gz

Download URL ml_pipe_core-0.0.23.tar.gz
Size 75.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0fe245443fa6d5617c3bfe943085f197b1524441ca864e786848efb90935d43b
BLAKE2b-256 checksum
How to use checksums
843921f8dc141cab2c248d822779584d30d04a7fc58e00c5dd93cb89b0875f46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

Release files / ml_pipe_core-0.0.23-py3-none-any.whl

Download URL ml_pipe_core-0.0.23-py3-none-any.whl
Size 82.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b53f88969f594a045844e488665606e189787614f367b1432619e88a0e556f3
BLAKE2b-256 checksum
How to use checksums
5b5e31046b52790eac1f7f35ece1f4afff822537cee7a20a601c0171882a5761
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

Security for ml-pipe-core 0.0.23

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page