graph2mat.tools.server

Utilites to serve and request matrix predictions.

Initializing a model takes time. For this reason, sometimes it is useful to have a server running the model, waiting for requests to compute predictions.

This module implements a very simple HTTP server to provide matrix predictions, as well as a very simple HTML front end and a very simple python client API so that you are not forced to use raw requests.

Launching the server is easiest from the e3mat CLI with e3mat serve. You can also use e3mat request, which uses the client.

class graph2mat.tools.server.ServerClient(url: str | None = None, host: str | None = 'localhost', port: int | None = 56000)[source]

Bases: object

Client to interact easily with the e3nn server.

Parameters:
  • url (str or None, optional) –

    Root url where the server is running.

    If it is set to None, the environment variable E3MAT_SERVER_URL will be used if present.

    If it is set to None and the environment variable is not present, the url will be constructed from the values of host and port.

  • host (str or None, optional) –

    Host where the server is running.

    If it is set to None, the environment variable E3MAT_SERVER_HOST will be used if present, otherwise the default value of host will be used.

  • port (int, optional) –

    Port where the server is listening.

    If it is set to None, the environment variable E3MAT_SERVER_PORT will be used if present, otherwise the default value of port will be used.

__init__(url: str | None = None, host: str | None = 'localhost', port: int | None = 56000)[source]
avail_models() List[str][source]

Returns the models that are available in the server.

host: str | None
port: int | None
predict(geometry: str | Path | Geometry, output: str | Path | Type[SparseOrbital], model: str, local: bool = False) Path | SparseOrbital[source]

Predicts the matrix for a given geometry.

Parameters:
  • geometry (Union[str, sisl.Geometry]) – Either the path to the geometry file or the geometry itself.

  • output (Union[str, Type[sisl.SparseOrbital]]) – Either the path to the file where the prediction should be saved or the type of the object to be returned.

  • model (str) – Name of the model to use for the prediction. This model must be available in the server. You can check the available models with the avail_models method.

  • local (bool) – Whether the paths (if given) are in the local filesystem.

root_url: str
graph2mat.tools.server.create_server_app(models: Dict[str, ModelSpecification], local: bool = False) FastAPI[source]

Creates a flask app to listen to requests and predict matrices.

The app is to be ran with uvicorn. For example:

>>> import uvicorn
>>> models = {}
>>> app = server_app(models)
>>> uvicorn.run(app, host="localhost", port=56000)
Parameters:
  • models (Dict[str, ModelSpecification]) – A dictionary with the models to be used. The keys are the names of the models, and the values are dictionaries with everything that we need/know about the model.

  • local (bool, optional) – If True, the server allows the user to ask for changes in the local file system.

graph2mat.tools.server.create_server_app_from_filesystem(model_files: Dict[str, str] = {}, local: bool = False, cpu: bool = True)[source]

Launches a server that serves predictions from trained models stored in checkpoint files.

This function just builds the dictionary of models from the ckpt files and then calls server_app.

Parameters:
  • ckpt_files (Sequence[str]) – List of checkpoint files to load.

  • local (bool, optional) – If True, the server allows the user to ask for changes in the local file system.

  • cpu (bool, optional) – Load parameters in the CPU regardless of whether they were in the GPU, by default True.

Modules

api_client

Simple HTTP client to interact with the server.

extrapolation

Functionality for matrix extrapolation from a time series.

server_app

Implements a fastapi server API that accepts requests to predict matrices.