Skip to content

Commit

Permalink
Update of cmaboss. many things
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-noel committed Oct 21, 2024
1 parent c9165e7 commit 600c259
Show file tree
Hide file tree
Showing 12 changed files with 823 additions and 166 deletions.
7 changes: 7 additions & 0 deletions engine/python/cmaboss/maboss_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ MODULE_INIT_NAME(void)
return NULL;
}

// Py_INCREF(&cMaBoSSNode);
// if (PyModule_AddObject(m, "MaBoSSNode", (PyObject *) &cMaBoSSNode) < 0) {
// Py_DECREF(&cMaBoSSNode);
// Py_DECREF(m);
// return NULL;
// }

Py_INCREF(&cMaBoSSNetwork);
if (PyModule_AddObject(m, "MaBoSSNet", (PyObject *) &cMaBoSSNetwork) < 0) {
Py_DECREF(&cMaBoSSNetwork);
Expand Down
109 changes: 97 additions & 12 deletions engine/python/cmaboss/maboss_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,99 @@
#include "maboss_node.cpp"



static void cMaBoSSNetwork_dealloc(cMaBoSSNetworkObject *self)
{
delete self->network;
Py_TYPE(self)->tp_free((PyObject *) self);
}

static Network* cMaBoSSNetwork_getNetwork(cMaBoSSNetworkObject* self)
static PyObject *cMaBoSSNetwork_str(PyObject *self) {
PyObject* str = PyUnicode_FromString(((cMaBoSSNetworkObject* )self)->network->toString().c_str());
Py_INCREF(str);
return str;
}

static PyObject* cMaBoSSNetwork_setOutput(cMaBoSSNetworkObject* self, PyObject *args)
{
return self->network;
PyObject* list;
if (!PyArg_ParseTuple(args, "O", &list))
return NULL;

for (auto* node: self->network->getNodes())
{
if (PySequence_Contains(list, PyUnicode_FromString(node->getLabel().c_str()))) {
node->isInternal(false);
} else {
node->isInternal(true);
}
}
return Py_None;
}

static PyObject* cMaBoSSNetwork_getListNodes(cMaBoSSNetworkObject* self)
static PyObject* cMaBoSSNetwork_getOutput(cMaBoSSNetworkObject* self)
{
PyObject *list = PyList_New(self->network->getNodes().size());
PyObject* output = PyList_New(0);
for (auto* node: self->network->getNodes())
{
if (!node->isInternal()) {
PyList_Append(output, PyUnicode_FromString(node->getLabel().c_str()));
}
}
Py_INCREF(output);
return output;
}

size_t index = 0;
for (auto* node: self->network->getNodes()) {
PyList_SetItem(list, index, PyUnicode_FromString(node->getLabel().c_str()));
index++;
static PyObject* cMaBoSSNetwork_setObservedGraphNode(cMaBoSSNetworkObject* self, PyObject *args)
{
PyObject* list;
if (!PyArg_ParseTuple(args, "O", &list))
return NULL;

for (auto* node: self->network->getNodes())
{
if (PySequence_Contains(list, PyUnicode_FromString(node->getLabel().c_str()))) {
node->inGraph(true);
} else {
node->inGraph(false);
}
}
return Py_None;
}

return list;
static PyObject* cMaBoSSNetwork_getObservedGraphNode(cMaBoSSNetworkObject* self, PyObject *args)
{
PyObject* output = PyList_New(0);
for (auto* node: self->network->getNodes())
{
if (node->inGraph()) {
PyList_Append(output, PyUnicode_FromString(node->getLabel().c_str()));
}
}
Py_INCREF(output);
return output;
}

static PyObject* cMaBoSSNetwork_addNode(cMaBoSSNetworkObject* self, PyObject *args)
{
char * name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;

try{
cMaBoSSNodeObject * pynode = (cMaBoSSNodeObject *) PyObject_New(cMaBoSSNodeObject, &cMaBoSSNode);
pynode->_network = self->network;
pynode->_node = self->network->getOrMakeNode(name);
PyDict_SetItemString(self->nodes, name, (PyObject*) pynode);
Py_INCREF(pynode);


} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

return Py_None;
}

static PyObject * cMaBoSSNetwork_new(PyTypeObject* type, PyObject *args, PyObject* kwargs)
Expand All @@ -91,7 +162,17 @@ static PyObject * cMaBoSSNetwork_new(PyTypeObject* type, PyObject *args, PyObjec

try{
pynetwork->network->parse(network_file);

pynetwork->nodes = PyDict_New();

for (auto* node: pynetwork->network->getNodes())
{

cMaBoSSNodeObject * pynode = (cMaBoSSNodeObject *) PyObject_New(cMaBoSSNodeObject, &cMaBoSSNode);
pynode->_node = node;
PyDict_SetItemString(pynetwork->nodes, node->getLabel().c_str(), (PyObject*) pynode);
Py_INCREF(pynode);
}

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
Expand All @@ -102,8 +183,11 @@ static PyObject * cMaBoSSNetwork_new(PyTypeObject* type, PyObject *args, PyObjec


static PyMethodDef cMaBoSSNetwork_methods[] = {
{"getNetwork", (PyCFunction) cMaBoSSNetwork_getNetwork, METH_NOARGS, "returns the network object"},
{"getListNodes", (PyCFunction) cMaBoSSNetwork_getListNodes, METH_NOARGS, "returns the list of nodes"},
{"set_output", (PyCFunction) cMaBoSSNetwork_setOutput, METH_VARARGS, "sets the output nodes"},
{"get_output", (PyCFunction) cMaBoSSNetwork_getOutput, METH_NOARGS, "gets the output nodes"},
{"set_observed_graph_nodes", (PyCFunction) cMaBoSSNetwork_setObservedGraphNode, METH_VARARGS, "sets the observed graph nodes"},
{"get_observed_graph_nodes", (PyCFunction) cMaBoSSNetwork_getObservedGraphNode, METH_VARARGS, "gets the observed graph nodes"},
{"add_node", (PyCFunction) cMaBoSSNetwork_addNode, METH_VARARGS, "adds a node to the network"},
{NULL} /* Sentinel */
};

Expand All @@ -118,6 +202,7 @@ static PyTypeObject cMaBoSSNetwork = []{
net.tp_new = cMaBoSSNetwork_new;
net.tp_dealloc = (destructor) cMaBoSSNetwork_dealloc;
net.tp_methods = cMaBoSSNetwork_methods;
net.tp_str = cMaBoSSNetwork_str;
return net;
}();
#endif
7 changes: 7 additions & 0 deletions engine/python/cmaboss/maboss_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@

#include <Python.h>
#include <set>

#ifndef MABOSS_NETWORK_H
#define MABOSS_NETWORK_H

#include "src/BooleanNetwork.h"
#include "src/MaBEstEngine.h"

typedef struct {
PyObject_HEAD
Network* network;
PyObject* nodes;
} cMaBoSSNetworkObject;

#endif
141 changes: 136 additions & 5 deletions engine/python/cmaboss/maboss_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@
#include <set>
#include "src/BooleanNetwork.h"
#include "src/MaBEstEngine.h"
#include "maboss_net.h"

typedef struct {
PyObject_HEAD
Node* _node;
Network* _network;
} cMaBoSSNodeObject;

static void cMaBoSSNode_dealloc(cMaBoSSNodeObject *self)
Expand All @@ -65,15 +67,137 @@ static void cMaBoSSNode_dealloc(cMaBoSSNodeObject *self)
Py_TYPE(self)->tp_free((PyObject *) self);
}

static Node* cMaBoSSNode_getNode(cMaBoSSNodeObject* self)
static PyObject* cMaBoSSNode_getLabel(cMaBoSSNodeObject* self)
{
return self->_node;
return PyUnicode_FromString(self->_node->getLabel().c_str());
}

static PyObject* cMaBoSSNode_setLogic(cMaBoSSNodeObject* self, PyObject* args)
{
PyObject * logic = NULL;
if (!PyArg_ParseTuple(args, "O", &logic))
return NULL;

try{
std::map<std::string, NodeIndex> nodes_indexes;
for (auto* node: self->_network->getNodes()) {
nodes_indexes[node->getLabel()] = node->getIndex();
}

static PyObject* cMaBoSSNode_getLabel(cMaBoSSNodeObject* self)
if (logic != NULL) {
// std::cout << "Setting logical input to NULL" << std::endl;
// self->_node->setLogicalInputExpression(NULL);
// Py_RETURN_NONE;
// } else {
Expression* logic_expr = self->_network->parseSingleExpression(PyUnicode_AsUTF8(logic), &nodes_indexes);
self->_node->setLogicalInputExpression(logic_expr);
}

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

Py_RETURN_NONE;
}

static PyObject* cMaBoSSNode_getLogic(cMaBoSSNodeObject* self)
{
return PyUnicode_FromString(self->_node->getLabel().c_str());
if (self->_node->getLogicalInputExpression() != NULL) {
return PyUnicode_FromString(self->_node->getLogicalInputExpression()->toString().c_str());
} else {
Py_RETURN_NONE;
}

}

static PyObject * cMaBoSSNode_setRawRateUp(cMaBoSSNodeObject* self, PyObject* args)
{
PyObject* rate_up = NULL;
if (!PyArg_ParseTuple(args, "O", &rate_up))
return NULL;

try{
std::map<std::string, NodeIndex> nodes_indexes;
for (auto* node: self->_network->getNodes()) {
nodes_indexes[node->getLabel()] = node->getIndex();
}

Expression* rate_up_expr = self->_network->parseSingleExpression(PyUnicode_AsUTF8(rate_up), &nodes_indexes);
self->_node->setRateUpExpression(rate_up_expr);

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

Py_RETURN_NONE;
}

static PyObject * cMaBoSSNode_setRawRateDown(cMaBoSSNodeObject* self, PyObject* args)
{

PyObject* rate_down = NULL;
if (!PyArg_ParseTuple(args, "O", &rate_down))
return NULL;

try{
std::map<std::string, NodeIndex> nodes_indexes;
for (auto* node: self->_network->getNodes()) {
nodes_indexes[node->getLabel()] = node->getIndex();
}

Expression* rate_down_expr = self->_network->parseSingleExpression(PyUnicode_AsUTF8(rate_down), &nodes_indexes);
self->_node->setRateUpExpression(rate_down_expr);

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

Py_RETURN_NONE;
}

static PyObject* cMaBoSSNode_setRate(cMaBoSSNodeObject* self, PyObject* args)
{
double rate_up = 0.0;
double rate_down = 0.0;
if (!PyArg_ParseTuple(args, "dd", &rate_up, &rate_down))
return NULL;

try{
std::map<std::string, NodeIndex> nodes_indexes;
for (auto* node: self->_network->getNodes()) {
nodes_indexes[node->getLabel()] = node->getIndex();
}

self->_node->setRateUpExpression(
new CondExpression(new AliasExpression("logic"), new ConstantExpression(rate_up), new ConstantExpression(0.0))
);
self->_node->setRateDownExpression(
new CondExpression(new AliasExpression("logic"), new ConstantExpression(0.0), new ConstantExpression(rate_down))
);

} catch (BNException& e) {
PyErr_SetString(PyBNException, e.getMessage().c_str());
return NULL;
}

Py_RETURN_NONE;
}

static PyObject* cMaBoSSNode_getRateUp(cMaBoSSNodeObject* self)
{
PyObject* rate_up_str = PyUnicode_FromString(self->_node->getRateUpExpression()->toString().c_str());
Py_INCREF(rate_up_str);
return rate_up_str;
}

static PyObject* cMaBoSSNode_getRateDown(cMaBoSSNodeObject* self)
{
PyObject* rate_down_str = PyUnicode_FromString(self->_node->getRateDownExpression()->toString().c_str());
Py_INCREF(rate_down_str);
return rate_down_str;
}

static PyObject * cMaBoSSNode_new(PyTypeObject* type, PyObject *args, PyObject* kwargs)
Expand All @@ -90,6 +214,7 @@ static PyObject * cMaBoSSNode_new(PyTypeObject* type, PyObject *args, PyObject*
cMaBoSSNodeObject * pynode = (cMaBoSSNodeObject *) type->tp_alloc(type, 0);

try{
pynode->_network = network->network;
pynode->_node = network->network->getOrMakeNode(name);

} catch (BNException& e) {
Expand All @@ -102,8 +227,14 @@ static PyObject * cMaBoSSNode_new(PyTypeObject* type, PyObject *args, PyObject*


static PyMethodDef cMaBoSSNode_methods[] = {
// {"getNode", (PyCFunction) cMaBoSSNode_getNode, METH_NOARGS, "returns the node object"},
{"getLabel", (PyCFunction) cMaBoSSNode_getLabel, METH_NOARGS, "returns the node object"},
{"set_logic", (PyCFunction) cMaBoSSNode_setLogic, METH_VARARGS, "sets the logic of the node"},
{"get_logic", (PyCFunction) cMaBoSSNode_getLogic, METH_NOARGS, "returns the logic of the node"},
{"set_rate", (PyCFunction) cMaBoSSNode_setRate, METH_VARARGS, "sets the rate of the node"},
{"set_rate_up", (PyCFunction) cMaBoSSNode_setRawRateUp, METH_VARARGS, "sets the rate of the node"},
{"set_rate_down", (PyCFunction) cMaBoSSNode_setRawRateDown, METH_VARARGS, "sets the rate of the node"},
{"get_rate_up", (PyCFunction) cMaBoSSNode_getRateUp, METH_NOARGS, "returns the rate of the node"},
{"get_rate_down", (PyCFunction) cMaBoSSNode_getRateDown, METH_NOARGS, "returns the rate of the node"},
{NULL} /* Sentinel */
};

Expand Down
Loading

0 comments on commit 600c259

Please sign in to comment.