Skip to content

Commit

Permalink
Improved interface for control
Browse files Browse the repository at this point in the history
  • Loading branch information
smijeva committed Oct 11, 2023
1 parent 2c061cb commit 2902232
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "biodivine-aeon-py"
version = "0.4.0-alpha1"
version = "0.4.0-alpha2"
edition = "2021"

[lib]
Expand All @@ -19,7 +19,7 @@ static-z3 = ["z3/static-link-z3"]
pyo3 = { version = "0.19.0", features = ["abi3-py37", "extension-module"] }
biodivine-lib-param-bn = { version="0.4.5", features=["solver-z3"] }
biodivine-lib-bdd = "0.5.1"
biodivine-pbn-control = { git = "https://github.com/sybila/biodivine-pbn-control", rev = "ffe2680" }
biodivine-pbn-control = { git = "https://github.com/sybila/biodivine-pbn-control", rev = "0f92934" }
rand = "0.8.5"
macros = { path = "macros" }

Expand Down
2 changes: 1 addition & 1 deletion conda/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "biodivine_aeon" %}
{% set version = "0.2.0" %}
{% set version = "0.4.0a2" %}

package:
name: "{{ name|lower }}"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "biodivine_aeon"
repository = "https://github.com/sybila/biodivine-aeon-py/"
documentation = "https://biodivine.fi.muni.cz/docs/aeon-py/v0.4.0a1/"
description = "Python bindings for the tool AEON."
version = "0.4.0a1"
version = "0.4.0a2"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
Expand Down
76 changes: 44 additions & 32 deletions src/bindings/pbn_control/_impl_perturbation_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::bindings::pbn_control::{PyControlMap, PyPerturbationGraph, PyPhenotyp
use crate::{throw_runtime_error, AsNative};
use biodivine_lib_param_bn::biodivine_std::bitvector::{ArrayBitVector, BitVector};
use biodivine_lib_param_bn::VariableId;
use biodivine_pbn_control::perturbation::PerturbationGraph;
use biodivine_pbn_control::perturbation::{PerturbationGraph};
use biodivine_pbn_control::phenotype_control::PhenotypeOscillationType;
use pyo3::prelude::*;
use pyo3::types::PyList;

Expand All @@ -30,6 +31,15 @@ impl AsNative<PerturbationGraph> for PyPerturbationGraph {
}
}

pub fn convert_str_to_oscillation_type(osc: &str) -> PhenotypeOscillationType {
match osc {
"Forbidden" => PhenotypeOscillationType::Forbidden,
"Allowed" => PhenotypeOscillationType::Allowed,
"Required" => PhenotypeOscillationType::Required,
_ => panic!("Invalid variant"),
}
}

#[pymethods]
impl PyPerturbationGraph {
/// Create a new `PerturbationGraph` based on a `BooleanNetwork`.
Expand All @@ -52,7 +62,7 @@ impl PyPerturbationGraph {
perturb_vars.push(network.as_ref().find_variable(var)?.unwrap().into());
}

Ok(PerturbationGraph::with_restricted_variables(network.as_native(), &perturb_vars).into())
Ok(PerturbationGraph::with_restricted_variables(network.as_native(), perturb_vars.clone()).into())
}

/// Get a `SymbolicAsyncGraph` that represents the original (unperturbed) behaviour
Expand Down Expand Up @@ -81,6 +91,11 @@ impl PyPerturbationGraph {
self.as_native().variables().map(|i| i.into()).collect()
}

/// Get the list of `VariableId` objects which can be perturbed
pub fn perturbable_variables(&self) -> Vec<PyVariableId> {
self.as_native().perturbable_variables().iter().map(|i| i.clone().into()).collect()
}

/// Get the `ParameterId` of a parameter that is associated with the perturbation
/// of a specific network variable (given as `VariableId` or string name).
pub fn get_perturbation_parameter(&self, variable: &PyAny) -> PyResult<PyParameterId> {
Expand Down Expand Up @@ -263,56 +278,53 @@ impl PyPerturbationGraph {
.into()
}


// pub fn phenotype_permanent_control(
// &self,
// phenotype: GraphVertices,
// admissible_colors_perturbations: GraphColors,
// oscillation: PhenotypeOscillationType,
// verbose: bool
// )

pub fn phenotype_permanent_control(
&self,
phenotype: PyGraphVertices,
admissible_perturbations: PyGraphColors,
allow_oscillation: bool
oscillation: &str,
verbose: bool
) -> PyPhenotypeControlMap {
let converted_phenotype = phenotype.as_native().clone();
let converted_perturbation = admissible_perturbations.as_native().clone();
self.as_native()
let converted_oscillation = convert_str_to_oscillation_type(oscillation);
let result = self.as_native()
.phenotype_permanent_control(converted_phenotype,
converted_perturbation,
allow_oscillation)
.into()
converted_oscillation,
verbose);
return result.into()
}

pub fn ceiled_phenotype_permanent_control(
&self,
phenotype: PyGraphVertices,
size_bound: usize,
perturbation_variables: Vec<PyVariableId>,
oscillation: &str,
required_robustness: f64,
stop_early: bool,
allow_oscillation: bool
) -> PyPhenotypeControlMap {
verbose: bool) -> PyPhenotypeControlMap {
let converted_phenotype = phenotype.as_native().clone();
let converted_perturbation_variables = perturbation_variables.iter().map(|i| i.clone().as_native().clone()).collect();
self.as_native()
let converted_oscillation = convert_str_to_oscillation_type(oscillation);
let result = self.as_native()
.ceiled_phenotype_permanent_control(converted_phenotype,
size_bound,
converted_perturbation_variables,
stop_early,
allow_oscillation)
.into()
size_bound,
converted_oscillation,
required_robustness,
stop_early,
verbose);
return result.into()
}

pub fn ceiled_phenotype_permanent_control_with_true_oscillation(
&self,
phenotype: PyGraphVertices,
size_bound: usize,
perturbation_variables: Vec<PyVariableId>,
stop_early: bool
) -> PyPhenotypeControlMap {
let converted_phenotype = phenotype.as_native().clone();
let converted_perturbation_variables = perturbation_variables.iter().map(|i| i.clone().as_native().clone()).collect();
self.as_native()
.ceiled_phenotype_permanent_control_with_true_oscillation(converted_phenotype,
size_bound,
converted_perturbation_variables,
stop_early)
.into()
}

/// Resolves a string or `VariableId` to `VariableId`.
pub fn find_variable(&self, variable: &PyAny) -> PyResult<PyVariableId> {
Expand Down
15 changes: 15 additions & 0 deletions src/bindings/pbn_control/_impl_phenotype_control_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ fn py_dict_to_rust_hashmap(py_dict: &PyDict) -> HashMap<String, bool> {
rust_hashmap
}

fn rust_hashmap_to_py_dict(py:Python, rust_hashmap: &HashMap<String, bool>) -> Py<PyDict> {
let mut pyDict = PyDict::new(py);

for (k, v) in rust_hashmap {
pyDict.set_item(k, v).unwrap();
}

pyDict.into()
}

impl From<PhenotypeControlMap> for PyPhenotypeControlMap {
fn from(value: PhenotypeControlMap) -> Self {
PyPhenotypeControlMap(value)
Expand Down Expand Up @@ -54,6 +64,11 @@ impl PyPhenotypeControlMap {
self.as_native().as_colored_vertices().clone().into()
}

// pub fn working_perturbations(&self, min_robustness: f64, verbose: bool) -> Vec<(HashMap<String, bool>, GraphColors)> {
/// Obtain list of working perturbations
pub fn working_perturbations(&self, py: Python, min_robustness: f64, verbose: bool) -> Vec<(Py<PyDict>, PyGraphColors)> {
self.as_native().working_perturbations(min_robustness, verbose).iter().map(|i| (rust_hashmap_to_py_dict(py, &i.0), i.1.clone().into())).collect()
}

/// Obtain a set of colours for which the given perturbation works
pub fn perturbation_working_colors(&self, perturbation: &PyDict) -> PyGraphColors {
Expand Down
3 changes: 2 additions & 1 deletion src/bindings/pbn_control/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use biodivine_pbn_control::control::ControlMap;
use biodivine_pbn_control::perturbation::PerturbationGraph;
use biodivine_pbn_control::phenotype_control::PhenotypeControlMap;
use biodivine_pbn_control::phenotype_control::{PhenotypeControlMap,PhenotypeOscillationType};
use pyo3::prelude::*;
use crate::AsNative;

mod _impl_control_map;
mod _impl_perturbation_graph;
Expand Down

0 comments on commit 2902232

Please sign in to comment.