config.h
1 /*************************************************************************
2  * Copyright (C) 2018-2021 Blue Brain Project
3  * Jonas Karlsson <jonas.karlsson@epfl.ch>
4  * Juan Hernando <juan.hernando@epfl.ch>
5  *
6  * This file is part of 'libsonata', distributed under the terms
7  * of the GNU Lesser General Public License version 3.
8  *
9  * See top-level COPYING.LESSER and COPYING files for details.
10  *************************************************************************/
11 
12 #pragma once
13 
14 #include <set>
15 #include <string>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include <bbp/sonata/edges.h>
20 #include <bbp/sonata/nodes.h>
21 #include <bbp/sonata/optional.hpp>
22 
23 #include "common.h"
24 #include "optional.hpp"
25 #include "variant.hpp"
26 
27 
28 namespace bbp {
29 namespace sonata {
30 
32 
33 struct CommonPopulationProperties {
37  std::string type;
38 
44  std::string elementsPath;
45 
51  std::string typesPath;
52 
56  std::string biophysicalNeuronModelsDir;
57 
61  std::string morphologiesDir;
62 
66  std::unordered_map<std::string, std::string> alternateMorphologyFormats;
67 };
68 
72 struct SONATA_API NodePopulationProperties: public CommonPopulationProperties {
76  std::string spatialSegmentIndexDir;
77 
82  nonstd::optional<std::string> vasculatureFile{nonstd::nullopt};
83 
88  nonstd::optional<std::string> vasculatureMesh{nonstd::nullopt};
89 
94  nonstd::optional<std::string> microdomainsFile{nonstd::nullopt};
95 
99  nonstd::optional<std::string> spineMorphologiesDir{nonstd::nullopt};
100 };
101 
105 struct SONATA_API EdgePopulationProperties: public CommonPopulationProperties {
109  std::string spatialSynapseIndexDir;
110 
114  nonstd::optional<std::string> endfeetMeshesFile{nonstd::nullopt};
115 };
116 
120 class SONATA_API CircuitConfig
121 {
122  public:
123  enum class ConfigStatus {
125  invalid,
128  complete,
135  partial
136  };
137 
147  CircuitConfig(const std::string& contents, const std::string& basePath);
148 
160  static CircuitConfig fromFile(const std::string& path);
161 
166  ConfigStatus getCircuitConfigStatus() const;
167 
171  const std::string& getNodeSetsPath() const;
172 
176  std::set<std::string> listNodePopulations() const;
177 
184  NodePopulation getNodePopulation(const std::string& name, const Hdf5Reader& hdf5_reader) const;
185  NodePopulation getNodePopulation(const std::string& name) const;
189  std::set<std::string> listEdgePopulations() const;
190 
197  EdgePopulation getEdgePopulation(const std::string& name, const Hdf5Reader& hdf5_reader) const;
198  EdgePopulation getEdgePopulation(const std::string& name) const;
199 
207  NodePopulationProperties getNodePopulationProperties(const std::string& name) const;
208 
216  EdgePopulationProperties getEdgePopulationProperties(const std::string& name) const;
217 
222  const std::string& getExpandedJSON() const;
223 
224  private:
225  struct Components {
226  std::string morphologiesDir;
227  std::unordered_map<std::string, std::string> alternateMorphologiesDir;
228  std::string biophysicalNeuronModelsDir;
229 
230  nonstd::optional<std::string> vasculatureFile{nonstd::nullopt};
231  nonstd::optional<std::string> vasculatureMesh{nonstd::nullopt};
232  nonstd::optional<std::string> endfeetMeshesFile{nonstd::nullopt};
233  nonstd::optional<std::string> microdomainsFile{nonstd::nullopt};
234  nonstd::optional<std::string> spineMorphologiesDir{nonstd::nullopt};
235  };
236 
237  class Parser;
238 
239  // Circuit config json string whose paths and variables have been expanded to include
240  // manifest variables
241  std::string _expandedJSON;
242 
243  // How strict we are checking the circuit config
244  ConfigStatus _status = ConfigStatus::complete;
245 
246  // Path to the nodesets file
247  std::string _nodeSetsFile;
248 
249  // Node populations that override default components variables
250  std::unordered_map<std::string, NodePopulationProperties> _nodePopulationProperties;
251 
252  // Edge populations that override default components variables
253  std::unordered_map<std::string, EdgePopulationProperties> _edgePopulationProperties;
254 };
255 
259 class SONATA_API SimulationConfig
260 {
261  public:
265  struct Run {
266  enum class IntegrationMethod { invalid = -1, euler, nicholson, nicholson_ion };
267 
268  static constexpr double DEFAULT_spikeThreshold = -30.0;
269  static constexpr IntegrationMethod DEFAULT_IntegrationMethod = IntegrationMethod::euler;
270  static constexpr int DEFAULT_stimulusSeed = 0;
271  static constexpr int DEFAULT_ionchannelSeed = 0;
272  static constexpr int DEFAULT_minisSeed = 0;
273  static constexpr int DEFAULT_synapseSeed = 0;
274 
276  double tstop{};
278  double dt{};
280  int randomSeed{};
282  double spikeThreshold = DEFAULT_spikeThreshold;
285  IntegrationMethod integrationMethod = DEFAULT_IntegrationMethod;
288  int stimulusSeed = DEFAULT_stimulusSeed;
290  int ionchannelSeed = DEFAULT_ionchannelSeed;
293  int minisSeed = DEFAULT_minisSeed;
295  int synapseSeed = DEFAULT_synapseSeed;
297  std::string electrodesFile;
298  };
302  struct Output {
303  enum class SpikesSortOrder { invalid = -1, none, by_id, by_time };
304 
305  static constexpr char DEFAULT_outputDir[] = "output";
306  static constexpr char DEFAULT_logFile[] = "";
307  static constexpr char DEFAULT_spikesFile[] = "out.h5";
308  static constexpr SpikesSortOrder DEFAULT_sortOrder = SpikesSortOrder::by_time;
309 
311  std::string outputDir = DEFAULT_outputDir;
313  std::string logFile = DEFAULT_logFile;
315  std::string spikesFile = DEFAULT_spikesFile;
317  SpikesSortOrder sortOrder = DEFAULT_sortOrder;
318  };
319 
321  enum class ModificationType { invalid = -1, TTX, ConfigureAllSections };
322 
324  std::string nodeSet;
326  ModificationType type;
327  };
328 
330 
335  std::string sectionConfigure;
336  };
337 
339 
340  using ModificationMap = std::unordered_map<std::string, Modification>;
341 
345  struct Conditions {
346  enum class SpikeLocation { invalid = -1, soma, AIS };
347  static constexpr double DEFAULT_celsius = 34.0;
348  static constexpr double DEFAULT_vInit = -80.0;
349  static constexpr SpikeLocation DEFAULT_spikeLocation = SpikeLocation::soma;
350  static constexpr bool DEFAULT_randomizeGabaRiseTime = false;
351 
353  double celsius = DEFAULT_celsius;
355  double vInit = DEFAULT_vInit;
357  SpikeLocation spikeLocation = DEFAULT_spikeLocation;
360  nonstd::optional<double> extracellularCalcium{nonstd::nullopt};
363  bool randomizeGabaRiseTime = DEFAULT_randomizeGabaRiseTime;
367  std::unordered_map<std::string, std::unordered_map<std::string, variantValueType>>
368  mechanisms;
371  ModificationMap modifications;
373  std::set<std::string> listModificationNames() const;
376  const Modification& getModification(const std::string& name) const;
377  };
381  struct Report {
382  enum class Sections { invalid = -1, soma, axon, dend, apic, all };
383  enum class Type { invalid = -1, compartment, lfp, summation, synapse };
384  enum class Scaling { invalid = -1, none, area };
385  enum class Compartments { invalid = -1, center, all };
386 
388  std::string cells;
390  Sections sections;
392  Type type;
395  Scaling scaling;
398  Compartments compartments;
402  std::string variableName;
404  std::string unit;
406  double dt{};
408  double startTime{};
410  double endTime{};
412  std::string fileName;
414  bool enabled = true;
415  };
416 
417  using ReportMap = std::unordered_map<std::string, Report>;
418 
419  struct InputBase {
420  enum class Module {
421  invalid = -1,
422  linear,
423  relative_linear,
424  pulse,
425  subthreshold,
426  hyperpolarizing,
427  synapse_replay,
428  seclamp,
429  noise,
430  shot_noise,
431  relative_shot_noise,
432  absolute_shot_noise,
433  ornstein_uhlenbeck,
434  relative_ornstein_uhlenbeck
435  };
436 
437  enum class InputType {
438  invalid = -1,
439  spikes,
440  extracellular_stimulation,
441  current_clamp,
442  voltage_clamp,
443  conductance
444  };
445 
447  Module module;
449  InputType inputType;
451  double delay{};
453  double duration{};
455  std::string nodeSet;
456  };
457 
458  struct InputLinear: public InputBase {
460  double ampStart{};
462  double ampEnd{};
464  bool representsPhysicalElectrode = false;
465  };
466 
469  double percentStart{};
471  double percentEnd{};
473  bool representsPhysicalElectrode = false;
474  };
475 
476  struct InputPulse: public InputBase {
478  double ampStart{};
480  double ampEnd{};
482  double width{};
484  double frequency{};
486  bool representsPhysicalElectrode = false;
487  };
488 
489  struct InputSubthreshold: public InputBase {
491  double percentLess{};
493  bool representsPhysicalElectrode = false;
494  };
495 
498  bool representsPhysicalElectrode = false;
499  };
500 
501  struct InputSynapseReplay: public InputBase {
503  std::string spikeFile;
504  };
505 
506  struct InputSeclamp: public InputBase {
508  double voltage{};
510  double seriesResistance{};
511  };
512 
513  struct InputNoise: public InputBase {
515  nonstd::optional<double> mean{nonstd::nullopt};
517  nonstd::optional<double> meanPercent{nonstd::nullopt};
520  double variance{};
522  bool representsPhysicalElectrode = false;
523  };
524 
525  struct InputShotNoise: public InputBase {
527  double riseTime{};
529  double decayTime{};
531  nonstd::optional<int> randomSeed{nonstd::nullopt};
533  double reversal{};
535  double dt{};
537  int rate{};
539  double ampMean{};
542  double ampVar{};
544  bool representsPhysicalElectrode = false;
545  };
546 
549  double riseTime{};
551  double decayTime{};
553  nonstd::optional<int> randomSeed{nonstd::nullopt};
555  double reversal{};
557  double dt{};
559  double ampCv{};
562  double meanPercent{};
565  double sdPercent{};
567  bool representsPhysicalElectrode = false;
568  };
569 
570  struct InputAbsoluteShotNoise: public InputBase {
572  double riseTime{};
574  double decayTime{};
576  nonstd::optional<int> randomSeed{nonstd::nullopt};
578  double reversal{};
580  double dt{};
582  double ampCv{};
584  double mean{};
586  double sigma{};
588  bool representsPhysicalElectrode = false;
589  };
590 
591  struct InputOrnsteinUhlenbeck: public InputBase {
593  double tau{};
595  double reversal{};
597  double dt{};
599  nonstd::optional<int> randomSeed{nonstd::nullopt};
601  double mean{};
603  double sigma{};
605  bool representsPhysicalElectrode = false;
606  };
607 
610  double tau{};
612  double reversal{};
614  double dt{};
616  nonstd::optional<int> randomSeed{nonstd::nullopt};
619  double meanPercent{};
622  double sdPercent{};
624  bool representsPhysicalElectrode = false;
625  };
626 
629  InputPulse,
633  InputSeclamp,
640 
641  using InputMap = std::unordered_map<std::string, Input>;
642 
647  struct ConnectionOverride {
649  std::string name;
651  std::string source;
653  std::string target;
655  double weight{1.};
657  nonstd::optional<double> spontMinis{nonstd::nullopt};
660  nonstd::optional<std::string> synapseConfigure{nonstd::nullopt};
663  nonstd::optional<std::string> modoverride{nonstd::nullopt};
666  nonstd::optional<double> synapseDelayOverride{nonstd::nullopt};
669  double delay{0.};
672  nonstd::optional<double> neuromodulationDtc{nonstd::nullopt};
675  nonstd::optional<double> neuromodulationStrength{nonstd::nullopt};
676  };
677 
678  enum class SimulatorType { invalid = -1, NEURON, CORENEURON };
679 
687  SimulationConfig(const std::string& content, const std::string& basePath);
688 
698  static SimulationConfig fromFile(const std::string& path);
699 
703  const std::string& getBasePath() const noexcept;
704 
708  const std::string& getJSON() const noexcept;
709 
713  const std::string& getNetwork() const noexcept;
714 
718  const Run& getRun() const noexcept;
719 
723  const Output& getOutput() const noexcept;
724 
728  const Conditions& getConditions() const noexcept;
729 
733  std::set<std::string> listReportNames() const;
734 
741  const Report& getReport(const std::string& name) const;
742 
746  std::set<std::string> listInputNames() const;
747 
753  const Input& getInput(const std::string& name) const;
754 
759  const std::vector<ConnectionOverride>& getConnectionOverrides() const noexcept;
760 
765  const SimulationConfig::SimulatorType& getTargetSimulator() const;
766 
771  const std::string& getNodeSetsFile() const noexcept;
772 
776  const nonstd::optional<std::string>& getNodeSet() const noexcept;
777 
781  const std::unordered_map<std::string, variantValueType>& getMetaData() const noexcept;
782 
783 
787  const std::unordered_map<std::string, variantValueType>& getBetaFeatures() const noexcept;
788 
793  const std::string& getExpandedJSON() const;
794 
795  private:
796  // JSON string
797  std::string _expandedJSON;
798  // Base path of the simulation config file
799  std::string _basePath;
800 
801  // Run section
802  Run _run;
803  // Output section
804  Output _output;
805  // List of reports
806  ReportMap _reports;
807  // Conditions section
808  Conditions _conditions;
809  // Path of circuit config file for the simulation
810  std::string _network;
811  // List of inputs
812  InputMap _inputs;
813  // List of connections
814  std::vector<ConnectionOverride> _connection_overrides;
815  // Name of simulator
816  SimulatorType _targetSimulator;
817  // Path of node sets file
818  std::string _nodeSetsFile;
819  // Name of node set
820  nonstd::optional<std::string> _nodeSet{nonstd::nullopt};
821  // Remarks on the simulation
822  std::unordered_map<std::string, variantValueType> _metaData;
823  // Variables for a new feature in development, to be moved to other sections once in production
824  std::unordered_map<std::string, variantValueType> _betaFeatures;
825 
826  class Parser;
827 };
828 
829 } // namespace sonata
830 } // namespace bbp
bbp::sonata::CommonPopulationProperties::morphologiesDir
std::string morphologiesDir
Definition: config.h:79
bbp::sonata::NodePopulationProperties
Definition: config.h:90
bbp::sonata::SimulationConfig::InputRelativeShotNoise
Definition: config.h:565
bbp::sonata::SimulationConfig::InputPulse
Definition: config.h:494
bbp::sonata::CommonPopulationProperties::biophysicalNeuronModelsDir
std::string biophysicalNeuronModelsDir
Definition: config.h:74
bbp::sonata::SimulationConfig::InputNoise
Definition: config.h:531
bbp::sonata::NodePopulation
Definition: nodes.h:38
bbp::sonata::SimulationConfig::ModificationConfigureAllSections
Definition: config.h:349
bbp::sonata::SimulationConfig::Report
Definition: config.h:399
bbp::sonata::SimulationConfig::InputRelativeOrnsteinUhlenbeck
Definition: config.h:626
bbp::sonata::CommonPopulationProperties::type
std::string type
Definition: config.h:55
bbp::sonata::SimulationConfig::InputSynapseReplay
Definition: config.h:519
bbp::sonata::CommonPopulationProperties
Definition: config.h:51
bbp::sonata::SimulationConfig::InputBase
Definition: config.h:437
bbp::sonata::SimulationConfig::InputHyperpolarizing
Definition: config.h:514
bbp::sonata::SimulationConfig::ModificationTTX
Definition: config.h:347
bbp::sonata::CommonPopulationProperties::elementsPath
std::string elementsPath
Definition: config.h:62
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:521
bbp::sonata::SimulationConfig::InputSubthreshold
Definition: config.h:507
bbp::sonata::SimulationConfig::Output
Definition: config.h:320
bbp::sonata::EdgePopulationProperties
Definition: config.h:123
bbp::sonata::CommonPopulationProperties::typesPath
std::string typesPath
Definition: config.h:69
bbp::sonata::SimulationConfig::Run
Definition: config.h:283
bbp::sonata::CircuitConfig
Definition: config.h:138
bbp::sonata::Hdf5Reader
Definition: hdf5_reader.h:121
bbp::sonata::SimulationConfig::Conditions
Definition: config.h:363
bbp::sonata::SimulationConfig::InputRelativeLinear
Definition: config.h:485
bbp::sonata::SimulationConfig::InputOrnsteinUhlenbeck
Definition: config.h:609
bbp::sonata::SimulationConfig
Definition: config.h:277
bbp::sonata::SimulationConfig::InputAbsoluteShotNoise
Definition: config.h:588
bbp::sonata::SimulationConfig::ModificationBase
Definition: config.h:338
bbp::sonata::SimulationConfig::InputLinear
Definition: config.h:476
bbp::sonata::SimulationConfig::InputSeclamp
Definition: config.h:524
bbp::sonata::SimulationConfig::InputShotNoise
Definition: config.h:543
bbp::sonata::EdgePopulation
Definition: edges.h:40
bbp::sonata::SimulationConfig::ConnectionOverride
Definition: config.h:665
nonstd::variants::variant
Definition: variant.hpp:1523
bbp::sonata::CommonPopulationProperties::alternateMorphologyFormats
std::unordered_map< std::string, std::string > alternateMorphologyFormats
Definition: config.h:84