Skip to content

Commit

Permalink
modifications as a list, add delay parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
WeinaJi committed Apr 30, 2024
1 parent 7580c24 commit 24998c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
19 changes: 8 additions & 11 deletions include/bbp/sonata/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,14 @@ class SONATA_API SimulationConfig

struct ModificationBase {
enum class ModificationType { invalid = -1, TTX, ConfigureAllSections };

/// Node set which receives the manipulation
std::string nodeSet;
/// Name of the manipulation. Supported values are “TTX” and “ConfigureAllSections”.
ModificationType type;
/// Delay of the modification operation. Defaut is 0;
double delay{0.0};
/// Name of the modification setting.
std::string name;
};

struct ModificationTTX: public ModificationBase {};
Expand All @@ -337,8 +340,6 @@ class SONATA_API SimulationConfig

using Modification = nonstd::variant<ModificationTTX, ModificationConfigureAllSections>;

using ModificationMap = std::unordered_map<std::string, Modification>;

/**
* Parameters defining global experimental conditions.
*/
Expand Down Expand Up @@ -366,14 +367,10 @@ class SONATA_API SimulationConfig
/// dictionaries of variables' names and values.
std::unordered_map<std::string, std::unordered_map<std::string, variantValueType>>
mechanisms;
/// Collection of dictionaries with each member decribing a modification that mimics
/// experimental manipulations to the circuit.
ModificationMap modifications;
/// Returns the names of the modifications
std::set<std::string> listModificationNames() const;
/// Returns the given modification parameters
/// \throws SonataError if the given modification name does not exist
const Modification& getModification(const std::string& name) const;
/// List of modifications that mimics experimental manipulations to the circuit.
std::vector<Modification> modifications;
// Method to return the full list of modifications in the Conditions section
const std::vector<Modification>& getModifications() const noexcept;
};
/**
* List of report parameters collected during the simulation
Expand Down
19 changes: 10 additions & 9 deletions python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,8 @@ PYBIND11_MODULE(_libsonata, m) {
.def_readonly("mechanisms",
&SimulationConfig::Conditions::mechanisms,
DOC_SIMULATIONCONFIG(Conditions, mechanisms))
.def_property_readonly("list_modification_names",
&SimulationConfig::Conditions::listModificationNames,
DOC_SIMULATIONCONFIG(Conditions, listModificationNames))
.def("modification",
&SimulationConfig::Conditions::getModification,
"name"_a,
DOC_SIMULATIONCONFIG(Conditions, getModification));
.def("modifications",
&SimulationConfig::Conditions::getModifications);


py::enum_<SimulationConfig::Conditions::SpikeLocation>(conditions, "SpikeLocation")
Expand All @@ -710,12 +705,16 @@ PYBIND11_MODULE(_libsonata, m) {

py::class_<SimulationConfig::ModificationBase> modificationBase(simConf, "ModificationBase");
modificationBase
.def_readonly("name",
&SimulationConfig::ModificationBase::name)
.def_readonly("node_set",
&SimulationConfig::ModificationBase::nodeSet,
DOC_SIMULATIONCONFIG(ModificationBase, nodeSet))
.def_readonly("type",
&SimulationConfig::ModificationBase::type,
DOC_SIMULATIONCONFIG(ModificationBase, type));
DOC_SIMULATIONCONFIG(ModificationBase, type))
.def_readonly("delay",
&SimulationConfig::ModificationBase::delay);

py::class_<SimulationConfig::ModificationTTX, SimulationConfig::ModificationBase>(
simConf, "ModificationTTX");
Expand All @@ -724,7 +723,9 @@ PYBIND11_MODULE(_libsonata, m) {
SimulationConfig::ModificationBase>(simConf, "ModificationConfigureAllSections")
.def_readonly("section_configure",
&SimulationConfig::ModificationConfigureAllSections::sectionConfigure,
DOC_SIMULATIONCONFIG(ModificationConfigureAllSections, sectionConfigure));
DOC_SIMULATIONCONFIG(ModificationConfigureAllSections, sectionConfigure))
.def_readonly("delay",
&SimulationConfig::ModificationConfigureAllSections::delay);

py::enum_<SimulationConfig::ModificationBase::ModificationType>(modificationBase,
"ModificationType")
Expand Down
37 changes: 18 additions & 19 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,16 @@ void parseConditionsMechanisms(
}

void parseConditionsModifications(const nlohmann::json& it,
SimulationConfig::ModificationMap& buf) {
std::vector<SimulationConfig::Modification>& buf) {
const auto sectionIt = it.find("modifications");
if (sectionIt == it.end()) {
if (sectionIt == it.end() || sectionIt->is_null()) {
return;
}
if (!sectionIt->is_array()) {
throw SonataError("`modifications` must be an array");
}
buf.reserve(sectionIt->size());

for (auto& mIt : sectionIt->items()) {
const auto valueIt = mIt.value();
const auto debugStr = fmt::format("modification {}", mIt.key());
Expand All @@ -629,16 +634,20 @@ void parseConditionsModifications(const nlohmann::json& it,
case SimulationConfig::ModificationBase::ModificationType::TTX: {
SimulationConfig::ModificationTTX result;
result.type = type;
parseMandatory(valueIt, "name", debugStr, result.name);
parseMandatory(valueIt, "node_set", debugStr, result.nodeSet);
buf[mIt.key()] = result;
parseOptional(valueIt, "delay", result.delay);
buf.push_back(std::move(result));
break;
}
case SimulationConfig::ModificationBase::ModificationType::ConfigureAllSections: {
SimulationConfig::ModificationConfigureAllSections result;
result.type = type;
parseMandatory(valueIt, "name", debugStr, result.name);
parseMandatory(valueIt, "node_set", debugStr, result.nodeSet);
parseMandatory(valueIt, "section_configure", debugStr, result.sectionConfigure);
buf[mIt.key()] = result;
parseOptional(valueIt, "delay", result.delay);
buf.push_back(std::move(result));
break;
}
default:
Expand Down Expand Up @@ -1384,6 +1393,11 @@ const SimulationConfig::Conditions& SimulationConfig::getConditions() const noex
return _conditions;
}

const std::vector<SimulationConfig::Modification>& SimulationConfig::Conditions::getModifications()
const noexcept {
return modifications;
}

const std::string& SimulationConfig::getNetwork() const noexcept {
return _network;
}
Expand Down Expand Up @@ -1446,20 +1460,5 @@ const std::string& SimulationConfig::getExpandedJSON() const {
return _expandedJSON;
}

std::set<std::string> SimulationConfig::Conditions::listModificationNames() const {
return getMapKeys(modifications);
}

const SimulationConfig::Modification& SimulationConfig::Conditions::getModification(
const std::string& name) const {
const auto it = modifications.find(name);
if (it == modifications.end()) {
throw SonataError(
fmt::format("The modification '{}' is not present in the simulation config file",
name));
}
return it->second;
}

} // namespace sonata
} // namespace bbp
15 changes: 9 additions & 6 deletions tests/data/config/simulation_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@
"property4": "test"
}
},
"modifications": {
"applyTTX": {
"modifications": [
{
"node_set": "single",
"type": "TTX"
"type": "TTX",
"name": "applyTTX"
},
"no_SK_E2": {
{
"node_set": "single",
"type": "ConfigureAllSections",
"section_configure": "%s.gSK_E2bar_SK_E2 = 0"
"section_configure": "%s.gSK_E2bar_SK_E2 = 0",
"name": "no_SK_E2",
"delay": 10.0
}
}
]
},
"inputs": {
"ex_linear": {
Expand Down
15 changes: 8 additions & 7 deletions tests/test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,19 @@ TEST_CASE("SimulationConfig") {
itr = config.getConditions().mechanisms.find("GluSynapse");
CHECK(nonstd::get<double>(itr->second.find("property3")->second) == 0.025);
CHECK(nonstd::get<std::string>(itr->second.find("property4")->second) == "test");
CHECK(config.getConditions().listModificationNames() ==
std::set<std::string>{"applyTTX", "no_SK_E2"});
const auto TTX = nonstd::get<SimulationConfig::ModificationTTX>(
config.getConditions().getModification("applyTTX"));
const auto configAllSects = nonstd::get<SimulationConfig::ModificationConfigureAllSections>(
config.getConditions().getModification("no_SK_E2"));
CHECK_THROWS_AS(config.getConditions().getModification("DoesNotExist"), SonataError);
const auto modifications = config.getConditions().getModifications();
CHECK(modifications.size() == 2);
const auto TTX = nonstd::get<SimulationConfig::ModificationTTX>(modifications[0]);
CHECK(TTX.name == "applyTTX");
CHECK(TTX.type == SimulationConfig::ModificationBase::ModificationType::TTX);
CHECK(TTX.nodeSet == "single");
CHECK(TTX.delay == 0.0);
const auto configAllSects = nonstd::get<SimulationConfig::ModificationConfigureAllSections>(modifications[1]);
CHECK(configAllSects.name == "no_SK_E2");
CHECK(configAllSects.type ==
SimulationConfig::ModificationBase::ModificationType::ConfigureAllSections);
CHECK(configAllSects.sectionConfigure == "%s.gSK_E2bar_SK_E2 = 0");
CHECK(configAllSects.delay == 10.0);

CHECK_THROWS_AS(config.getReport("DoesNotExist"), SonataError);

Expand Down

0 comments on commit 24998c3

Please sign in to comment.