Skip to content

Commit

Permalink
initial test of NodeSet extend
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed Jul 14, 2023
1 parent 4adb756 commit 365aace
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
9 changes: 9 additions & 0 deletions include/bbp/sonata/node_sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class SONATA_API NodeSets
*/
std::set<std::string> names() const;

/**
* Extend this NodeSets to include the `other` nodeset
*
* Duplicate names are overriden with the values from `other`
*
* The duplicate names are returned.
*/
std::set<std::string> extend(const NodeSets& other) const;

/**
* Return string version of node sets
*/
Expand Down
3 changes: 3 additions & 0 deletions python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ PYBIND11_MODULE(_libsonata, m) {
.def_static("from_file", [](py::object path) { return NodeSets::fromFile(py::str(path)); })
.def_property_readonly("names", &NodeSets::names, DOC_NODESETS(names))
.def("materialize", &NodeSets::materialize, DOC_NODESETS(materialize))
.def("extend", &NodeSets::extend,
"other"_a,
DOC_NODESETS(extend))
.def("toJSON", &NodeSets::toJSON, DOC_NODESETS(toJSON));

py::class_<CommonPopulationProperties>(m,
Expand Down
7 changes: 7 additions & 0 deletions python/generated/docstrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ static const char *__doc_bbp_sonata_NodeSets_NodeSets_3 = R"doc()doc";

static const char *__doc_bbp_sonata_NodeSets_NodeSets_4 = R"doc()doc";

static const char *__doc_bbp_sonata_NodeSets_extend =
R"doc(Extend this NodeSets to include the `other` nodeset
Duplicate names are overriden with the values from `other`
The duplicate names are returned.)doc";

static const char *__doc_bbp_sonata_NodeSets_fromFile = R"doc(Open a SONATA `node sets` file from a path */)doc";

static const char *__doc_bbp_sonata_NodeSets_impl = R"doc()doc";
Expand Down
17 changes: 17 additions & 0 deletions python/tests/test_nodesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,20 @@ def test_library_datatype(self):

j = json.dumps({"NodeSet0": { "E-mapping-good": [0, 1, ] }})
self.assertRaises(SonataError, NodeSets(j).materialize, "NodeSet0", self.population)

def test_extend(self):
ns0 = NodeSets(json.dumps({"NodeSet0": { "E-mapping-good": 0 }}))
dup = ns0.extend(ns0)
self.assertEqual(dup, {"NodeSet0"})
self.assertEqual(ns0.names, {"NodeSet0"})

ns1 = NodeSets(json.dumps({"NodeSet1": { "E-mapping-good": 0 }}))
dup = ns0.extend(ns1)
self.assertEqual(dup, set())
self.assertEqual(ns0.names, {"NodeSet0", "NodeSet1"})

ns0 = NodeSets(json.dumps({"NodeSet0": { "E-mapping-good": 0 }}))
ns1 = NodeSets(json.dumps({"NodeSet0": { "E-mapping-good": 0 }}))
dup = ns0.extend(ns1)
self.assertEqual(dup, {"NodeSet0"})
self.assertEqual(ns0.names, {"NodeSet0"})
80 changes: 72 additions & 8 deletions src/node_sets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class NodeSetRule
virtual bool is_compound() const {
return false;
}
virtual std::unique_ptr<NodeSetRule> clone() const = 0;
};

using NodeSetRulePtr = std::unique_ptr<NodeSetRule>;
Expand Down Expand Up @@ -101,6 +102,20 @@ class NodeSets
return getMapKeys(node_sets_);
}

std::set<std::string> extend(const NodeSets& other){
if(&other == this){
return names();
}
std::set<std::string> duplicates;
for(const auto& ns : other.node_sets_){
if(node_sets_.count(ns.first) > 0){
duplicates.insert(ns.first);
}
node_sets_[ns.first] = ns.second->clone();
}
return duplicates;
}

std::string toJSON() const {
std::string ret{"{\n"};
for (const auto& pair : node_sets_) {
Expand All @@ -125,15 +140,20 @@ class NodeSetNullRule: public NodeSetRule
std::string toJSON() const final {
return {};
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetNullRule>();
}
};

// { 'region': ['region1', 'region2', ...] }
template <typename T>
class NodeSetBasicRule: public NodeSetRule
{
public:
NodeSetBasicRule(std::string attribute, std::vector<T>& values)
: attribute_(std::move(attribute))
NodeSetBasicRule(const std::string attribute, const std::vector<T>& values)
: attribute_(attribute)
, values_(values) {}

Selection materialize(const detail::NodeSets& /* unused */,
Expand All @@ -152,6 +172,11 @@ class NodeSetBasicRule: public NodeSetRule
return toString(attribute_, values_);
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetBasicRule<T>>(attribute_, values_);
}

private:
std::string attribute_;
std::vector<T> values_;
Expand All @@ -161,7 +186,7 @@ class NodeSetBasicRule: public NodeSetRule
class NodeSetBasicPopulation: public NodeSetRule
{
public:
explicit NodeSetBasicPopulation(std::vector<std::string>& values)
explicit NodeSetBasicPopulation(const std::vector<std::string>& values)
: values_(values) {}

Selection materialize(const detail::NodeSets& /* unused */,
Expand All @@ -177,6 +202,11 @@ class NodeSetBasicPopulation: public NodeSetRule
return toString("population", values_);
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetBasicPopulation>(values_);
}

private:
std::vector<std::string> values_;
};
Expand All @@ -185,7 +215,7 @@ class NodeSetBasicPopulation: public NodeSetRule
class NodeSetBasicNodeIds: public NodeSetRule
{
public:
explicit NodeSetBasicNodeIds(Selection::Values&& values)
explicit NodeSetBasicNodeIds(const Selection::Values& values)
: values_(values) {}

Selection materialize(const detail::NodeSets& /* unused */,
Expand All @@ -197,6 +227,11 @@ class NodeSetBasicNodeIds: public NodeSetRule
return toString("node_ids", values_);
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetBasicNodeIds>(values_);
}

private:
Selection::Values values_;
};
Expand Down Expand Up @@ -230,6 +265,16 @@ class NodeSetBasicMultiClause: public NodeSetRule
return ret;
}

std::unique_ptr<NodeSetRule> clone() const final
{
std::vector<NodeSetRulePtr> clauses;
clauses.reserve(clauses_.size());
for(const auto& clause : clauses_){
clauses.push_back(clause->clone());
}
return std::make_unique<detail::NodeSetBasicMultiClause>(std::move(clauses));
}

private:
std::vector<NodeSetRulePtr> clauses_;
};
Expand All @@ -238,12 +283,12 @@ class NodeSetBasicMultiClause: public NodeSetRule
class NodeSetBasicOperatorString: public NodeSetRule
{
public:
explicit NodeSetBasicOperatorString(std::string attribute,
explicit NodeSetBasicOperatorString(const std::string& attribute,
const std::string& op,
std::string value)
const std::string& value)
: op_(string2op(op))
, attribute_(std::move(attribute))
, value_(std::move(value)) {}
, attribute_(attribute)
, value_(value) {}

Selection materialize(const detail::NodeSets& /* unused */,
const NodePopulation& np) const final {
Expand Down Expand Up @@ -279,6 +324,11 @@ class NodeSetBasicOperatorString: public NodeSetRule
}
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetBasicOperatorString>(attribute_, op2string(op_), value_);
}

private:
Op op_;
std::string attribute_;
Expand Down Expand Up @@ -349,6 +399,11 @@ class NodeSetBasicOperatorNumeric: public NodeSetRule
}
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetBasicOperatorNumeric>(name_, op2string(op_), value_);
}

private:
std::string name_;
double value_;
Expand Down Expand Up @@ -382,6 +437,11 @@ class NodeSetCompoundRule: public NodeSetRule
return targets_;
}

std::unique_ptr<NodeSetRule> clone() const final
{
return std::make_unique<detail::NodeSetCompoundRule>(name_, targets_);
}

private:
std::string name_;
CompoundTargets targets_;
Expand Down Expand Up @@ -673,6 +733,10 @@ std::set<std::string> NodeSets::names() const {
return impl_->names();
}

std::set<std::string> NodeSets::extend(const NodeSets& other) const {
return impl_->extend(*other.impl_);
}

std::string NodeSets::toJSON() const {
return impl_->toJSON();
}
Expand Down

0 comments on commit 365aace

Please sign in to comment.