Multi-sample CARNIVAL#

import numpy as np
import corneto as cn
import pandas as pd

cn.info()
Installed version:v1.0.0.dev5 (latest stable: v1.0.0-alpha)
Available backends:CVXPY v1.6.5, PICOS v2.6.1
Default backend (corneto.opt):CVXPY
Installed solvers:CVXOPT, GLPK, GLPK_MI, HIGHS, SCIP, SCIPY
Graphviz version:v0.20.3
Installed path:/home/runner/work/corneto/corneto/corneto
Repository:https://github.com/saezlab/corneto

Toy example#

from corneto.graph import Graph

# A toy PKN signalling network
G = Graph.from_tuples(
    [
        ("rec1", 1, "a"),
        ("rec1", -1, "b"),
        ("rec1", 1, "f"),
        ("rec1", -1, "c"),
        ("rec2", 1, "b"),
        ("rec2", 1, "tf2"),
        ("b", 1, "g"),
        ("g", -1, "d"),
        ("rec2", -1, "d"),
        ("a", 1, "c"),
        ("a", -1, "d"),
        ("c", 1, "d"),
        ("c", -1, "e"),
        ("c", 1, "tf3"),
        ("e", 1, "a"),
        ("d", -1, "c"),
        ("e", 1, "tf1"),
        ("a", -1, "tf1"),
        ("d", 1, "tf2"),
        ("c", -1, "tf2"),
        ("tf1", 1, "tf2"),
        ("tf1", -1, "rec2"),
        ("tf2", 1, "rec1"),
        ("tf1", 1, "f")
    ]
)
G.plot()
../../_images/4fd3b062c8fc55e25036e3fb65b0e7f7ab9ea9f69f11ac63c04901cda82874f4.svg
from corneto.methods.future.carnival import CarnivalFlow

samples = {
    "c1": {
        "rec2": {
            "value": 1,
            "mapping": "vertex",
            "role": "input"
        },
        "tf1": {
            "value": -2,
            "mapping": "vertex",
            "role": "output"
        },
        "tf2": {
            "value": 1,
            "mapping": "vertex",
            "role": "output"
        }
    },
    "c2": {
        "rec1": {
            "value": 1,
            "mapping": "vertex",
            "role": "input"
        },
        "rec2": {
            "value": -1,
            "mapping": "vertex",
            "role": "input"
        },
        "tf1": {
            "value": 1,
            "mapping": "vertex",
            "role": "output"
        },
        "tf2": {
            "value": -1,
            "mapping": "vertex",
            "role": "output"
        },
        "tf3": {
            "value": 3,
            "mapping": "vertex",
            "role": "output"
        }
    }
}

data = cn.Data.from_cdict(samples)
data
Data(n_samples=2, n_feats=[3 5])

Multi-sample CARNIVAL#

c = CarnivalFlow(lambda_reg=1e-3)
P = c.build(G, data)

# Solve the problem using the HIGHs solver included in Scipy
P.solve(verbosity=0, solver="scipy");
Unreachable vertices for sample: 1
Unreachable vertices for sample: 0
# The preprocessed G graph (flow graph)
c.processed_graph.plot()
../../_images/a438b8bfbc6ee6e2918b2c3e230704dff1e48fd6b7dd4d7aac21f9b3b5e843aa.svg
# Show the sub-graph where the signal propagates for all samples (union graph)
c.processed_graph.edge_subgraph(np.flatnonzero(P.expr.flow.value)).plot()
../../_images/656353c500d042826b0fed6bb6ca15215b2203a6e7d3b8765b13739052a2f471.svg
# Extract the values from the problem
pd.DataFrame(P.expr.edge_value.value, index=c.processed_graph.E, columns=["condition_1", "condition_2"]).astype(int)
condition_1 condition_2
(rec1) (a) 1 1
(b) 0 0
(c) 0 0
(rec2) (b) 0 0
(tf2) 1 -1
(b) (g) 0 0
(g) (d) 0 0
(rec2) (d) 0 0
(a) (c) 0 1
(d) 0 0
(c) (d) 0 0
(e) 0 0
(tf3) 0 1
(e) (a) 0 0
(d) (c) 0 0
(e) (tf1) 0 0
(a) (tf1) -1 0
(d) (tf2) 0 0
(c) (tf2) 0 0
(tf1) (tf2) 0 0
(rec2) 0 0
(tf2) (rec1) 1 0
() 0 0
(tf3) () 0 0
(tf1) () 0 0
() (rec1) 0 1
(rec2) 1 -1
# Solution for sample 1
c.processed_graph.edge_subgraph(np.flatnonzero(P.expr.edge_has_signal.value[:,0]>0.5)).plot()
../../_images/f241de182f9f6b73e736b744d931f928b2f4274204a15fe91d745cf9008fa499.svg
# Solution for sample 2
c.processed_graph.edge_subgraph(np.flatnonzero(P.expr.edge_has_signal.value[:,1]>0.5)).plot()
../../_images/22e121a97725d92c691431e413e2c5fea6f510813492ac2a7b6c9fb31fbdbbb0.svg
for o in P.objectives:
    print(o.value)
0.0
1.0
8.0

Plotting solutions on top of the PKN#

c.processed_graph.plot_values(vertex_values=P.expr.vertex_value.value[:,0], edge_values=P.expr.edge_value.value[:, 0])
../../_images/61eba4093c1005294dadb09933743696c7b1ee07180983771659522280941cab.svg
c.processed_graph.plot_values(vertex_values=P.expr.vertex_value.value[:,1], edge_values=P.expr.edge_value.value[:, 1])
../../_images/bdfb654e7dce11528b29a190c0e40922a6bcb998b7342b322dde59d172558313.svg

Single sample using the same multi-sample method#

# Only c1

subset = cn.Data.from_cdict({"c1": samples["c1"]})
c = CarnivalFlow(lambda_reg=1e-3)
P = c.build(G, subset)
P.solve(verbosity=0, solver="scipy");
for o in P.objectives:
    print(o.value)

c.processed_graph.edge_subgraph(np.flatnonzero(P.expr.edge_has_signal.value)).plot()
Unreachable vertices for sample: 0
0.0
5.0
../../_images/f241de182f9f6b73e736b744d931f928b2f4274204a15fe91d745cf9008fa499.svg
# Only c2

subset = cn.Data.from_cdict({"c2": samples["c2"]})
c = CarnivalFlow(lambda_reg=1e-3)
P = c.build(G, subset)
P.solve(verbosity=0, solver="scipy");
for o in P.objectives:
    print(o.value)

c.processed_graph.edge_subgraph(np.flatnonzero(P.expr.edge_has_signal.value)).plot()
Unreachable vertices for sample: 0
1.0
6.0
../../_images/22e121a97725d92c691431e413e2c5fea6f510813492ac2a7b6c9fb31fbdbbb0.svg