1 /** 2 * 3 * Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) 4 * {prenom.nom}_at_lip6.fr 5 * 6 * This library is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with this library. If not, see <http://www.gnu.org/licenses/>. 18 * 19 */ 20 // defines some macro for PGM classes 21 22 23 %pythonappend gum::DAGmodel::dag %{ 24 val = DAG(val) # copying the DAG 25 %} 26 27 %define IMPROVE_GRAPHICAL_MODEL_API(classname) 28 %extend classname { names()29 PyObject* names() const { 30 PyObject* q=PySet_New(0); 31 32 for ( auto node : self->nodes()) { 33 PySet_Add(q,PyString_FromString(self->variable(node).name().c_str())); 34 } 35 return q; 36 }; 37 } 38 39 // macro from graphs.i 40 ADD_METHODS_FOR_ALL_GUM_GRAPHCLASS(classname); 41 %enddef 42 43 %define IMPROVE_DIRECTED_GRAPHICAL_MODEL_API(classname) 44 45 IMPROVE_GRAPHICAL_MODEL_API(classname); 46 47 %extend classname { arcs()48 PyObject *arcs() const { 49 return PyAgrumHelper::PySetFromArcSet(self->arcs()); 50 }; 51 parents(PyObject * norid)52 PyObject *parents(PyObject* norid) const { 53 return PyAgrumHelper::PySetFromNodeSet(self->parents(PyAgrumHelper::nodeIdFromNameOrIndex(norid,self->variableNodeMap()))); 54 }; children(PyObject * norid)55 PyObject *children(PyObject* norid) const { 56 return PyAgrumHelper::PySetFromNodeSet(self->children(PyAgrumHelper::nodeIdFromNameOrIndex(norid,self->variableNodeMap()))); 57 }; family(PyObject * norid)58 PyObject *family(PyObject* norid) const { 59 return PyAgrumHelper::PySetFromNodeSet(self->family(PyAgrumHelper::nodeIdFromNameOrIndex(norid,self->variableNodeMap()))); 60 }; descendants(PyObject * norid)61 PyObject *descendants(PyObject* norid) const { 62 return PyAgrumHelper::PySetFromNodeSet(self->descendants(PyAgrumHelper::nodeIdFromNameOrIndex(norid,self->variableNodeMap()))); 63 }; ancestors(PyObject * norid)64 PyObject *ancestors(PyObject* norid) const { 65 return PyAgrumHelper::PySetFromNodeSet(self->ancestors(PyAgrumHelper::nodeIdFromNameOrIndex(norid,self->variableNodeMap()))); 66 }; 67 moralizedAncestralGraph(PyObject * nodes)68 gum::UndiGraph moralizedAncestralGraph(PyObject* nodes) { 69 gum::NodeSet sonodes; 70 PyAgrumHelper::populateNodeSetFromPySequenceOfIntOrString(sonodes,nodes,self->variableNodeMap()); 71 return self->moralizedAncestralGraph(sonodes); 72 }; 73 } 74 %enddef 75 76 77 %define IMPROVE_UNDIRECTED_GRAPHICAL_MODEL_API(classname) 78 79 IMPROVE_GRAPHICAL_MODEL_API(classname); 80 81 %extend classname { neighbours(PyObject * norid)82 PyObject* neighbours(PyObject * norid) const { 83 return PyAgrumHelper::PySetFromNodeSet(self->neighbours( 84 PyAgrumHelper::nodeIdFromNameOrIndex(norid, self->variableNodeMap()))); 85 }; 86 edges()87 PyObject* edges() const { 88 return PyAgrumHelper::PySetFromEdgeSet(self->graph().edges()); 89 }; 90 } 91 %enddef