Skip to content

Commit

Permalink
feat: qualibration graph representation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-v4s committed Aug 5, 2024
1 parent 74268ef commit 269b979
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
28 changes: 28 additions & 0 deletions qualibrate_runner/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Annotated, Mapping, cast

from fastapi import Depends, HTTPException
from qualibrate.qualibration_graph import QualibrationGraph
from qualibrate.qualibration_library import QualibrationLibrary
from qualibrate.qualibration_node import QualibrationNode

Expand Down Expand Up @@ -33,6 +34,13 @@ def get_nodes(
return cast(Mapping[str, QualibrationNode], library.get_nodes())


@cache
def get_graphs(
library: Annotated[QualibrationLibrary, Depends(get_library)],
) -> Mapping[str, QualibrationGraph]:
return cast(Mapping[str, QualibrationGraph], library.get_graphs())


def get_node(
name: str,
nodes: Annotated[Mapping[str, QualibrationNode], Depends(get_nodes)],
Expand All @@ -41,3 +49,23 @@ def get_node(
if node is None:
raise HTTPException(status_code=422, detail=f"Unknown node name {name}")
return node


def get_graph(
name: str,
graphs: Annotated[Mapping[str, QualibrationGraph], Depends(get_graphs)],
) -> QualibrationGraph:
graph = graphs.get(name)
if graph is None:
raise HTTPException(
status_code=422, detail=f"Unknown graph name {name}"
)
return graph


CACHED_DEPENDENCIES = (get_library, get_nodes, get_graphs)


def cache_clear() -> None:
for func in CACHED_DEPENDENCIES:
func.cache_clear()
44 changes: 40 additions & 4 deletions qualibrate_runner/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from typing import Annotated, Any, Mapping, Optional, Type, cast
from typing import Annotated, Any, Mapping, Optional, Sequence, Type, cast

from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
from pydantic import BaseModel
from qualibrate.qualibration_graph import QualibrationGraph
from qualibrate.qualibration_node import QualibrationNode

from qualibrate_runner.api.dependencies import (
cache_clear,
get_library,
get_state,
)
from qualibrate_runner.api.dependencies import (
get_graph as get_qgraph,
)
from qualibrate_runner.api.dependencies import (
get_graphs as get_qgraphs,
)
from qualibrate_runner.api.dependencies import (
get_node as get_qnode,
)
Expand Down Expand Up @@ -54,25 +62,53 @@ def submit_run(

@base_router.get("/get_nodes")
def get_nodes(
nodes: Annotated[Mapping[str, Any], Depends(get_qnodes)],
nodes: Annotated[Mapping[str, QualibrationNode], Depends(get_qnodes)],
settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)],
rescan: bool = False,
) -> Mapping[str, Any]:
if rescan:
get_library.cache_clear()
get_qnodes.cache_clear()
cache_clear()
library = get_library(settings)
nodes = get_qnodes(library)
return {node_name: node.serialize() for node_name, node in nodes.items()}


@base_router.get("/get_graphs")
def get_graphs(
graphs: Annotated[Mapping[str, QualibrationNode], Depends(get_qgraphs)],
settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)],
rescan: bool = False,
) -> Mapping[str, Any]:
if rescan:
cache_clear()
library = get_library(settings)
graphs = get_qnodes(library)
return {
graph_name: graph.serialize() for graph_name, graph in graphs.items()
}


@base_router.get("/get_node")
def get_node(
node: Annotated[QualibrationNode, Depends(get_qnode)],
) -> Mapping[str, Any]:
return cast(Mapping[str, Any], node.serialize())


@base_router.get("/get_graph")
def get_graph(
graph: Annotated[QualibrationGraph, Depends(get_qgraph)],
) -> Mapping[str, Any]:
return cast(Mapping[str, Any], graph.serialize())


@base_router.get("/get_graph/cytoscape")
def get_graph_cytoscape(
graph: Annotated[QualibrationGraph, Depends(get_qgraph)],
) -> Sequence[Mapping[str, Any]]:
return cast(Sequence[Mapping[str, Any]], graph.cytoscape_representation())


@base_router.get("/last_run")
def get_last_run(
state: Annotated[State, Depends(get_state)],
Expand Down
8 changes: 6 additions & 2 deletions qualibrate_runner/core/run_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ def run_job(
idx=-1,
)
try:
node = QualibrationLibrary.active_library.nodes[node.name]
node.run_node(node.parameters_class(**passed_input_parameters))
library = QualibrationLibrary.active_library

node = library.nodes[node.name]
library.run_node(
node.name, node.parameters_class(**passed_input_parameters)
)
except Exception as ex:
state.last_run = LastRun(
name=state.last_run.name,
Expand Down

0 comments on commit 269b979

Please sign in to comment.