Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ode semantics #211

Merged
merged 7 commits into from
Jul 14, 2023
Merged
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
24 changes: 14 additions & 10 deletions mira/dkg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

This submodule serves as an API for modeling
"""

import json
import uuid
from pathlib import Path
from textwrap import dedent
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Type, Union
from typing import Any, Dict, List, Literal, Optional, Set, Type, Union

import pystow
from fastapi import (
Expand All @@ -33,7 +33,8 @@
from mira.modeling.bilayer import BilayerModel
from mira.modeling.petri import PetriNetModel, PetriNetResponse
from mira.modeling.viz import GraphicalModel
from mira.sources.askenet.flux_span import reproduce_ode_semantics
from mira.sources.askenet.flux_span import reproduce_ode_semantics, \
test_file_path
from mira.sources.askenet.petrinet import template_model_from_askenet_json
from mira.sources.bilayer import template_model_from_bilayer
from mira.sources.biomodels import get_sbml_model
Expand Down Expand Up @@ -700,20 +701,23 @@ def askepetrinet_model_comparison(


class FluxSpanQuery(BaseModel):
flux_span: Dict[str, Any] = Field(
model: Dict[str, Any] = Field(
...,
example={}, # fixme: create example
description="The flux span to recover the de-stratified model from",
example=json.load(test_file_path.open()),
description="The model to recover the ODE-semantics from.",
)


@model_blueprint.post("/flux_span", response_model=TemplateModel, tags=["modeling"])
def flux_span(
@model_blueprint.post("/reconstruct_ode_semnatics",
response_model=TemplateModel,
tags=["modeling"])
def reproduce_ode_semantics_endpoint(
query: FluxSpanQuery = Body(
...,
description="The flux span to recover the de-stratified model from"
description="Reproduce ODE semantics from a stratified model"
"(flux span)."
)
):
"""Get the flux span of a model"""
tm = reproduce_ode_semantics(query.flux_span)
tm = reproduce_ode_semantics(query.model)
return tm
4 changes: 2 additions & 2 deletions mira/sources/askenet/flux_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def reproduce_ode_semantics(flux_span):
"""Reproduce ODE semantics from a flux span."""
"""Reproduce ODE semantics from a stratified model (flux span)."""

# First we make the original template model
tm = template_model_from_askenet_json(flux_span)
Expand Down Expand Up @@ -98,7 +98,7 @@ def reproduce_ode_semantics(flux_span):
# Find the rate law components in the original model
rate_law = deepcopy(original_template.rate_law.args[0])
# Now we need to map states to new states
concept_names = template.get_concept_names()
concept_names = {c.name for c in template.get_interactors()}
for concept_name in concept_names:
# Find the original concept
original_concept = original_map[concept_name]
Expand Down
5 changes: 3 additions & 2 deletions mira/sources/askenet/petrinet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
__all__ = ["model_from_url", "model_from_json_file", "template_model_from_askenet_json"]

import json
from copy import deepcopy

import sympy
import requests
Expand Down Expand Up @@ -170,8 +171,8 @@ def template_model_from_askenet_json(model_json) -> TemplateModel:
# Loop
for transition in model.get('transitions', []):
transition_id = transition['id'] # required, str
inputs = transition.get('input', []) # required, Array[str]
outputs = transition.get('output', []) # required, Array[str]
inputs = deepcopy(transition.get('input', [])) # required, Array[str]
outputs = deepcopy(transition.get('output', [])) # required, Array[str]
transition_grounding = transition.get('grounding', {}) # optional, Object
transition_properties = transition.get('properties', {}) # optional, Object
rate_obj = rates.get(transition_id, {}) # optional, Object
Expand Down
8 changes: 4 additions & 4 deletions tests/test_model_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,13 @@ def test_counts_to_dimensionless_amr(self):
for initial in tm_dimless.initials.values():
assert initial.concept.units.expression.args[0].equals(1)

def test_flux_span_endpoint(self):
def test_reconstruct_ode_semantics_endpoint(self):
# Load test file
from mira.sources.askenet.flux_span import test_file_path
flux_span = json.load(test_file_path.open())
strat_model = json.load(test_file_path.open())
response = self.client.post(
"/api/flux_span",
json={"flux_span": flux_span}
"/api/reconstruct_ode_semnatics",
json={"model": strat_model}
)
self.assertEqual(200, response.status_code)

Expand Down