Commit 18d63002 by Christophe Gonzales

Merge branch 'master' into feature/learningNewDatabase

parents 72486185 f558bf90
......@@ -343,6 +343,13 @@ namespace gum {
/** @return the id chosen by the internal idFactory */
virtual NodeId addNode();
/** insert n nodes
*
* @param n the number of nodes to add
* @return the vector of chosen ids
*/
std::vector< NodeId > addNodes(Size n);
/// try to insert a node with the given id
/** @warning This method should be carefully used. Please prefer
* @ref populateNodes or @ref populateNodesFromProperty when possible
......
......@@ -267,6 +267,15 @@ namespace gum {
return newNode;
}
INLINE std::vector< NodeId > NodeGraphPart::addNodes(Size N) {
std::vector< NodeId > v;
v.reserve(N);
for (Idx i = 0; i < N; i++)
v.push_back(this->addNode());
return v;
}
INLINE Size NodeGraphPart::sizeNodes() const {
return (__holes) ? (__boundVal - __holes->size()) : __boundVal;
}
......
......@@ -263,6 +263,44 @@ namespace gum_tests {
TS_ASSERT_EQUALS(cpt, max_cpt);
}
void testIteratorAddNodes() {
gum::NodeGraphPart nodeset;
auto v = nodeset.addNodes(100);
for (gum::NodeId i = 0; i < 100; i++)
TS_ASSERT_EQUALS(v[i], i)
for (int i = 0; i < 5; i++)
nodeset.eraseNode(gum::randomValue(100));
nodeset.addNodes(5);
TS_ASSERT_EQUALS(nodeset.size(), gum::Size(100));
gum::NodeId i = 0;
for (auto n : nodeset.nodes())
TS_ASSERT_EQUALS(n, i++);
gum::NodeGraphPart nodeset2;
nodeset2.addNodes(10);
gum::NodeGraphPart futureIds;
nodeset2.eraseNode(1);
futureIds.addNodeWithId(1);
nodeset2.eraseNode(3);
futureIds.addNodeWithId(3);
nodeset2.eraseNode(5);
futureIds.addNodeWithId(5);
auto v2 = nodeset2.addNodes(4);
futureIds.addNodeWithId(10); // the 4th added node as 10 for id
for (auto n : v2) {
TS_GUM_ASSERT_THROWS_NOTHING(futureIds.eraseNode(n));
}
TS_ASSERT(futureIds.empty());
}
private:
#define NBR_PROFILING_NODES 50000
void __testBigNodeGrapPart() {
......
......@@ -4999,6 +4999,11 @@ class DiGraph(_object):
return _pyAgrum.DiGraph_nodes(self)
def addNodes(self, n):
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.DiGraph_addNodes(self, n)
def arcs(self):
"""
arcs(self) -> PyObject *
......@@ -5300,6 +5305,11 @@ class DAG(DiGraph):
def __del__(self):
return None
def addNodes(self, n):
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.DAG_addNodes(self, n)
def addArc(self, *args):
"""
addArc(self, tail, head)
......@@ -5476,6 +5486,11 @@ class UndiGraph(_object):
return _pyAgrum.UndiGraph_nodes(self)
def addNodes(self, n):
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.UndiGraph_addNodes(self, n)
def edges(self):
"""
edges(self) -> PyObject *
......@@ -5858,6 +5873,11 @@ class MixedGraph(UndiGraph, DiGraph):
return _pyAgrum.MixedGraph___str__(self)
def addNodes(self, n):
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.MixedGraph_addNodes(self, n)
def addNode(self):
"""addNode(self) -> gum::NodeId"""
return _pyAgrum.MixedGraph_addNode(self)
......
......@@ -5034,6 +5034,11 @@ class DiGraph(_object):
return _pyAgrum.DiGraph_nodes(self)
def addNodes(self, n: 'gum::Size') -> "PyObject *":
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.DiGraph_addNodes(self, n)
def arcs(self) -> "PyObject *":
"""
arcs(self) -> PyObject *
......@@ -5335,6 +5340,11 @@ class DAG(DiGraph):
def __del__(self):
return None
def addNodes(self, n: 'gum::Size') -> "PyObject *":
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.DAG_addNodes(self, n)
def addArc(self, *args) -> "void":
"""
addArc(self, tail, head)
......@@ -5511,6 +5521,11 @@ class UndiGraph(_object):
return _pyAgrum.UndiGraph_nodes(self)
def addNodes(self, n: 'gum::Size') -> "PyObject *":
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.UndiGraph_addNodes(self, n)
def edges(self) -> "PyObject *":
"""
edges(self) -> PyObject *
......@@ -5893,6 +5908,11 @@ class MixedGraph(UndiGraph, DiGraph):
return _pyAgrum.MixedGraph___str__(self)
def addNodes(self, n: 'gum::Size') -> "PyObject *":
"""addNodes(self, n) -> PyObject *"""
return _pyAgrum.MixedGraph_addNodes(self, n)
def addNode(self) -> "gum::NodeId":
"""addNode(self) -> gum::NodeId"""
return _pyAgrum.MixedGraph_addNode(self)
......
%define ADD_NODES_METHOD_TO_GRAPHCLASS(classname)
%define ADD_CST_NODES_METHOD_TO_GRAPHCLASS(classname)
%extend classname {
PyObject *nodes() const {
return PyAgrumHelper::PySetFromNodeSet(self->nodes());
......@@ -12,13 +12,33 @@
};
};
%enddef
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::DiGraph); // add for the sub-classes (including MixedGraph)
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::UndiGraph);
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::EssentialGraph);
ADD_CST_NODES_METHOD_TO_GRAPHCLASS(gum::DiGraph); // add for the sub-classes (including MixedGraph)
ADD_CST_NODES_METHOD_TO_GRAPHCLASS(gum::UndiGraph);
ADD_CST_NODES_METHOD_TO_GRAPHCLASS(gum::EssentialGraph);
%ignore gum::EssentialGraph::nodes const;
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::MarkovBlanket);
ADD_CST_NODES_METHOD_TO_GRAPHCLASS(gum::MarkovBlanket);
%ignore gum::MarkovBlanket::nodes const;
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::IBayesNet);
ADD_CST_NODES_METHOD_TO_GRAPHCLASS(gum::IBayesNet);
%define ADD_NODES_METHOD_TO_GRAPHCLASS(classname)
%ignore classname::addNodes(gum::Size n);
%extend classname {
PyObject *addNodes(gum::Size n) const {
PyObject* q=PySet_New(0);
for(auto node : const_cast<classname *>(self)->addNodes(n)) {
PySet_Add(q,PyInt_FromLong(node));
}
return q;
};
};
%enddef
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::DiGraph);
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::UndiGraph);
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::MixedGraph);
ADD_NODES_METHOD_TO_GRAPHCLASS(gum::DAG);
%define ADD_DI_METHOD_TO_GRAPHCLASS(classname)
%extend classname {
......
# -*- encoding: UTF-8 -*-
import unittest
import pyAgrum as gum
from pyAgrumTestSuite import pyAgrumTestCase, addTests
class TestGraph(pyAgrumTestCase):
def _testAddNodes(self,g):
self.assertEqual(g.addNodes(6), set(range(6)))
g.eraseNode(1)
g.eraseNode(2)
g.eraseNode(4)
self.assertEqual(g.nodes(), set([0, 3, 5]))
self.assertEqual(g.addNodes(4), set([1, 2, 4, 6]))
self.assertEqual(g.nodes(), set(range(7)))
def testAddNodes(self):
self._testAddNodes(gum.DiGraph())
self._testAddNodes(gum.UndiGraph())
self._testAddNodes(gum.MixedGraph())
self._testAddNodes(gum.DAG())
ts = unittest.TestSuite()
addTests(ts, TestGraph)
......@@ -23,6 +23,7 @@ from tests import BNDatabaseGeneratorTestSuite
from tests import BNLearnerTestSuite
from tests import BNListenerTestSuite
from tests import EssentialGraphTestSuite
from tests import GraphTestSuite
from tests import ICIModelsForBNTestSuite
from tests import InstantiationTestSuite
from tests import JTInferenceTestSuite
......@@ -45,6 +46,7 @@ tests.append(BNDatabaseGeneratorTestSuite.ts)
tests.append(BNLearnerTestSuite.ts)
tests.append(BNListenerTestSuite.ts)
tests.append(EssentialGraphTestSuite.ts)
tests.append(GraphTestSuite.ts)
tests.append(ICIModelsForBNTestSuite.ts)
tests.append(InstantiationTestSuite.ts)
tests.append(JTInferenceTestSuite.ts)
......
......@@ -81,9 +81,13 @@ ADD_APPROXIMATIONSCHEME_API(gum::learning::genericBNLearner,gum::learning::BNLea
%define ADD_NODEGRAPHPART_API(classname)
%extend classname {
// erase node is not in this list since it is redefined by the very classes {Mixed|Di|Undi}Graph)
gum::NodeId addNode() {
return self->gum::NodeGraphPart::addNode();
}
std::vector<gum::NodeId> addNodes(gum::Size n) {
return self->gum::NodeGraphPart::addNodes(n);
}
void addNodeWithId(const gum::NodeId id) {
self->gum::NodeGraphPart::addNodeWithId(id);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment