Skip to content

Commit

Permalink
BRAYNS-577 Handle sonata node sets. (#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 authored Mar 19, 2024
1 parent 38e2061 commit 7cc0f4a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
17 changes: 14 additions & 3 deletions plugins/CircuitExplorer/io/sonataloader/ParameterCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,26 @@ class NodeChecker
void _checkNodeSets(const SonataNodePopulationParameters &params)
{
auto &nodeSets = params.node_sets;

if (nodeSets.empty())
{
return;
}

auto &nsPath = _config.getNodesetsPath();
if (!std::filesystem::is_regular_file(nsPath))
auto allNodeSets = _config.getNodeSets();

if (!allNodeSets)
{
throw std::invalid_argument("Cannot access nodesets file at " + nsPath);
throw std::invalid_argument("No node sets in config");
}

auto names = allNodeSets->names();
for (const auto &nodeSet : nodeSets)
{
if (!names.contains(nodeSet))
{
throw std::invalid_argument("Invalid node set: '" + nodeSet + "'");
}
}
}

Expand Down
30 changes: 18 additions & 12 deletions plugins/CircuitExplorer/io/sonataloader/Selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,27 @@ class NodeSetFilter
const std::vector<std::string> &nodeSets)
{
auto nodePopulation = config.getNodes(population);
if (!nodeSets.empty())

if (nodeSets.empty())
{
return nodePopulation.selectAll();
}

auto allNodeSets = config.getNodeSets();

if (!allNodeSets)
{
bbp::sonata::Selection result({});
auto &nodeSetsPath = config.getNodesetsPath();
auto nodeSetFile = bbp::sonata::NodeSets::fromFile(nodeSetsPath);
for (auto &nodeSetName : nodeSets)
{
auto newSelection = nodeSetFile.materialize(nodeSetName, nodePopulation);
result = result | newSelection;
}

return result;
throw std::invalid_argument("No node sets specified");
}

return nodePopulation.selectAll();
auto selection = bbp::sonata::Selection({});
for (auto &nodeSetName : nodeSets)
{
auto newSelection = allNodeSets->materialize(nodeSetName, nodePopulation);
selection = selection | newSelection;
}

return selection;
}
};

Expand Down
28 changes: 26 additions & 2 deletions plugins/CircuitExplorer/io/sonataloader/data/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,33 @@ const std::string &Config::getConfigAsJson() const noexcept
return _config.getExpandedJSON();
}

const std::string &Config::getNodesetsPath() const
std::optional<bbp::sonata::NodeSets> Config::getNodeSets() const
{
return _config.getNodeSetsPath();
auto circuit = _config.getNodeSetsPath();

if (circuit.empty() && !_simConfig)
{
return std::nullopt;
}

if (!_simConfig)
{
return bbp::sonata::NodeSets::fromFile(circuit);
}

auto &simulation = _simConfig->getNodeSetsFile();

if (circuit.empty())
{
return bbp::sonata::NodeSets::fromFile(simulation);
}

auto base = bbp::sonata::NodeSets::fromFile(circuit);
auto extension = bbp::sonata::NodeSets::fromFile(simulation);

base.update(extension);

return base;
}

bbp::sonata::NodePopulation Config::getNodes(const std::string &name) const
Expand Down
7 changes: 4 additions & 3 deletions plugins/CircuitExplorer/io/sonataloader/data/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include <bbp/sonata/config.h>
#include <bbp/sonata/node_sets.h>

#include <filesystem>
#include <optional>
Expand Down Expand Up @@ -59,10 +60,10 @@ class Config
const std::string &getConfigAsJson() const noexcept;

/**
* @brief Path to the nodesets file.
* @return const std::string&
* @brief Extract node sets if any.
* @return std::optional<bbp::sonata::NodeSets>
*/
const std::string &getNodesetsPath() const;
std::optional<bbp::sonata::NodeSets> getNodeSets() const;

/**
* @brief Returns the population object of the given population name.
Expand Down

0 comments on commit 7cc0f4a

Please sign in to comment.