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 };
96 
104  std::string spatialSynapseIndexDir;
105 
109  nonstd::optional<std::string> endfeetMeshesFile{nonstd::nullopt};
110 
114  nonstd::optional<std::string> spineMorphologiesDir{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;
328  std::string name;
329  };
330 
332 
337  std::string sectionConfigure;
338  };
339 
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;
370  std::vector<Modification> modifications;
372  const std::vector<Modification>& getModifications() const noexcept;
373  };
377  struct Report {
378  enum class Sections { invalid = -1, soma, axon, dend, apic, all };
379  enum class Type { invalid = -1, compartment, lfp, summation, synapse };
380  enum class Scaling { invalid = -1, none, area };
381  enum class Compartments { invalid = -1, center, all };
382 
384  std::string cells;
386  Sections sections;
388  Type type;
391  Scaling scaling;
394  Compartments compartments;
398  std::string variableName;
400  std::string unit;
402  double dt{};
404  double startTime{};
406  double endTime{};
408  std::string fileName;
410  bool enabled = true;
411  };
412 
413  using ReportMap = std::unordered_map<std::string, Report>;
414 
415  struct InputBase {
416  enum class Module {
417  invalid = -1,
418  linear,
419  relative_linear,
420  pulse,
421  sinusoidal,
422  subthreshold,
423  hyperpolarizing,
424  synapse_replay,
425  seclamp,
426  noise,
427  shot_noise,
428  relative_shot_noise,
429  absolute_shot_noise,
430  ornstein_uhlenbeck,
431  relative_ornstein_uhlenbeck
432  };
433 
434  enum class InputType {
435  invalid = -1,
436  spikes,
437  extracellular_stimulation,
438  current_clamp,
439  voltage_clamp,
440  conductance
441  };
442 
444  Module module;
446  InputType inputType;
448  double delay{};
450  double duration{};
452  std::string nodeSet;
453  };
454 
455  struct InputLinear: public InputBase {
457  double ampStart{};
459  double ampEnd{};
461  bool representsPhysicalElectrode = false;
462  };
463 
466  double percentStart{};
468  double percentEnd{};
470  bool representsPhysicalElectrode = false;
471  };
472 
473  struct InputPulse: public InputBase {
475  double ampStart{};
477  double width{};
479  double frequency{};
481  bool representsPhysicalElectrode = false;
482  };
483 
484  struct InputSinusoidal: public InputBase {
486  double ampStart{};
488  double frequency{};
490  double dt{};
492  bool representsPhysicalElectrode = false;
493  };
494 
495  struct InputSubthreshold: public InputBase {
497  double percentLess{};
499  bool representsPhysicalElectrode = false;
500  };
501 
504  bool representsPhysicalElectrode = false;
505  };
506 
507  struct InputSynapseReplay: public InputBase {
509  std::string spikeFile;
510  };
511 
512  struct InputSeclamp: public InputBase {
514  double voltage{};
516  double seriesResistance{};
517  };
518 
519  struct InputNoise: public InputBase {
521  nonstd::optional<double> mean{nonstd::nullopt};
523  nonstd::optional<double> meanPercent{nonstd::nullopt};
526  double variance{};
528  bool representsPhysicalElectrode = false;
529  };
530 
531  struct InputShotNoise: public InputBase {
533  double riseTime{};
535  double decayTime{};
537  nonstd::optional<int> randomSeed{nonstd::nullopt};
539  double reversal{};
541  double dt{};
543  int rate{};
545  double ampMean{};
548  double ampVar{};
550  bool representsPhysicalElectrode = false;
551  };
552 
555  double riseTime{};
557  double decayTime{};
559  nonstd::optional<int> randomSeed{nonstd::nullopt};
561  double reversal{};
563  double dt{};
566  double meanPercent{};
569  double sdPercent{};
571  bool representsPhysicalElectrode = false;
575  double relativeSkew{};
576  };
577 
578  struct InputAbsoluteShotNoise: public InputBase {
580  double riseTime{};
582  double decayTime{};
584  nonstd::optional<int> randomSeed{nonstd::nullopt};
586  double reversal{};
588  double dt{};
590  double mean{};
592  double sigma{};
594  bool representsPhysicalElectrode = false;
598  double relativeSkew{};
599  };
600 
601  struct InputOrnsteinUhlenbeck: public InputBase {
603  double tau{};
605  double reversal{};
607  double dt{};
609  nonstd::optional<int> randomSeed{nonstd::nullopt};
611  double mean{};
613  double sigma{};
615  bool representsPhysicalElectrode = false;
616  };
617 
620  double tau{};
622  double reversal{};
624  double dt{};
626  nonstd::optional<int> randomSeed{nonstd::nullopt};
629  double meanPercent{};
632  double sdPercent{};
634  bool representsPhysicalElectrode = false;
635  };
636 
639  InputPulse,
645  InputNoise,
651 
652  using InputMap = std::unordered_map<std::string, Input>;
653 
658  struct ConnectionOverride {
660  std::string name;
662  std::string source;
664  std::string target;
666  double weight{1.};
668  nonstd::optional<double> spontMinis{nonstd::nullopt};
671  nonstd::optional<std::string> synapseConfigure{nonstd::nullopt};
674  nonstd::optional<std::string> modoverride{nonstd::nullopt};
677  nonstd::optional<double> synapseDelayOverride{nonstd::nullopt};
680  double delay{0.};
683  nonstd::optional<double> neuromodulationDtc{nonstd::nullopt};
686  nonstd::optional<double> neuromodulationStrength{nonstd::nullopt};
687  };
688 
689  enum class SimulatorType { invalid = -1, NEURON, CORENEURON };
690 
698  SimulationConfig(const std::string& content, const std::string& basePath);
699 
709  static SimulationConfig fromFile(const std::string& path);
710 
714  const std::string& getBasePath() const noexcept;
715 
719  const std::string& getJSON() const noexcept;
720 
724  const std::string& getNetwork() const noexcept;
725 
729  const Run& getRun() const noexcept;
730 
734  const Output& getOutput() const noexcept;
735 
739  const Conditions& getConditions() const noexcept;
740 
744  std::set<std::string> listReportNames() const;
745 
752  const Report& getReport(const std::string& name) const;
753 
757  std::set<std::string> listInputNames() const;
758 
764  const Input& getInput(const std::string& name) const;
765 
770  const std::vector<ConnectionOverride>& getConnectionOverrides() const noexcept;
771 
776  const SimulationConfig::SimulatorType& getTargetSimulator() const;
777 
782  const std::string& getNodeSetsFile() const noexcept;
783 
787  const nonstd::optional<std::string>& getNodeSet() const noexcept;
788 
792  const std::unordered_map<std::string, variantValueType>& getMetaData() const noexcept;
793 
794 
798  const std::unordered_map<std::string, variantValueType>& getBetaFeatures() const noexcept;
799 
804  const std::string& getExpandedJSON() const;
805 
806  private:
807  // JSON string
808  std::string _expandedJSON;
809  // Base path of the simulation config file
810  std::string _basePath;
811 
812  // Run section
813  Run _run;
814  // Output section
815  Output _output;
816  // List of reports
817  ReportMap _reports;
818  // Conditions section
819  Conditions _conditions;
820  // Path of circuit config file for the simulation
821  std::string _network;
822  // List of inputs
823  InputMap _inputs;
824  // List of connections
825  std::vector<ConnectionOverride> _connection_overrides;
826  // Name of simulator
827  SimulatorType _targetSimulator;
828  // Path of node sets file
829  std::string _nodeSetsFile;
830  // Name of node set
831  nonstd::optional<std::string> _nodeSet{nonstd::nullopt};
832  // Remarks on the simulation
833  std::unordered_map<std::string, variantValueType> _metaData;
834  // Variables for a new feature in development, to be moved to other sections once in production
835  std::unordered_map<std::string, variantValueType> _betaFeatures;
836 
837  class Parser;
838 };
839 
840 } // namespace sonata
841 } // 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:571
bbp::sonata::SimulationConfig::InputPulse
Definition: config.h:491
bbp::sonata::CommonPopulationProperties::biophysicalNeuronModelsDir
std::string biophysicalNeuronModelsDir
Definition: config.h:74
bbp::sonata::SimulationConfig::InputNoise
Definition: config.h:537
bbp::sonata::NodePopulation
Definition: nodes.h:38
bbp::sonata::SimulationConfig::ModificationConfigureAllSections
Definition: config.h:351
bbp::sonata::SimulationConfig::Report
Definition: config.h:395
bbp::sonata::SimulationConfig::InputRelativeOrnsteinUhlenbeck
Definition: config.h:636
bbp::sonata::CommonPopulationProperties::type
std::string type
Definition: config.h:55
bbp::sonata::SimulationConfig::InputSynapseReplay
Definition: config.h:525
bbp::sonata::CommonPopulationProperties
Definition: config.h:51
bbp::sonata::SimulationConfig::InputBase
Definition: config.h:433
bbp::sonata::SimulationConfig::InputHyperpolarizing
Definition: config.h:520
bbp::sonata::SimulationConfig::ModificationTTX
Definition: config.h:349
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:513
bbp::sonata::SimulationConfig::Output
Definition: config.h:320
bbp::sonata::EdgePopulationProperties
Definition: config.h:118
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:482
bbp::sonata::SimulationConfig::InputSinusoidal
Definition: config.h:502
bbp::sonata::SimulationConfig::InputOrnsteinUhlenbeck
Definition: config.h:619
bbp::sonata::SimulationConfig
Definition: config.h:277
bbp::sonata::SimulationConfig::InputAbsoluteShotNoise
Definition: config.h:596
bbp::sonata::SimulationConfig::ModificationBase
Definition: config.h:338
bbp::sonata::SimulationConfig::InputLinear
Definition: config.h:473
bbp::sonata::SimulationConfig::InputSeclamp
Definition: config.h:530
bbp::sonata::SimulationConfig::InputShotNoise
Definition: config.h:549
bbp::sonata::EdgePopulation
Definition: edges.h:40
bbp::sonata::SimulationConfig::ConnectionOverride
Definition: config.h:676
nonstd::variants::variant
Definition: variant.hpp:1523
bbp::sonata::CommonPopulationProperties::alternateMorphologyFormats
std::unordered_map< std::string, std::string > alternateMorphologyFormats
Definition: config.h:84