Cypress  1.0
C++ Spiking Neural Network Simulation Framework
neuron_parameters.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2018 Christoph Ostrau
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef CYPRESS_UTILS_NEURON_PARAMETERS_HPP
22 #define CYPRESS_UTILS_NEURON_PARAMETERS_HPP
23 
24 
25 #include <array>
26 #include <iostream>
27 #include <vector>
28 #include <string>
29 
30 
31 #include <cypress/core/network.hpp>
32 #include "logger.hpp"
33 
38 #define NAMED_PARAMETER(NAME, IDX) \
39  static constexpr size_t idx_##NAME = IDX; \
40  void NAME(Real x) { arr[IDX] = x; } \
41  Real &NAME() { return arr[IDX]; } \
42  Real NAME() const { return arr[IDX]; }
43 
44 namespace cypress {
45 
47 private:
48  std::vector<Real> m_params;
49  std::vector<std::string> m_parameter_names;
50 
51 public:
55  NeuronParameter(const NeuronType &type,
56  const Json &json);
57 
59 
60  const std::vector<Real> &parameter() const { return m_params; };
61 
65  NeuronParameter &set(std::string name, cypress::Real value)
66  {
67  for (size_t i = 0; i < m_parameter_names.size(); i++) {
68  if (m_parameter_names[i] == name) {
69  m_params[i] = value;
70  return *this;
71  }
72  }
73  throw std::invalid_argument("Unknown neuron parameter" + name);
74  }
75 
76  cypress::Real get(std::string name)
77  {
78  for (size_t i = 0; i < m_parameter_names.size(); i++) {
79  if (m_parameter_names[i] == name) {
80  return m_params[i];
81  }
82  }
83  throw std::invalid_argument("Unknown neuron parameter" + name);
84  }
85  void print(std::ostream &out = std::cout)
86  {
87  out << "# Neuron Parameters: " << std::endl;
88  for (size_t i = 0; i < m_params.size(); i++) {
89  out << m_parameter_names[i] << ": " << m_params[i] << std::endl;
90  }
91  }
92 };
93 
97 template <typename T>
98 std::map<std::string, T> json_to_map(const cypress::Json &obj)
99 {
100  std::map<std::string, T> input;
101  for (auto i = obj.begin(); i != obj.end(); i++) {
102  const Json value = i.value();
103  // Suppress entries like Neuron_Type and sub-lists
104  if (value.is_number()) {
105  input.emplace(i.key(), value);
106  }
107  }
108  return input;
109 }
110 
111 template <typename T>
112 std::vector<T> read_check(std::map<std::string, T> &input,
113  const std::vector<std::string> &names,
114  const std::vector<T> &defaults)
115 {
116  std::vector<T> res(names.size());
117  for (size_t i = 0; i < res.size(); i++) {
118  auto it = input.find(names[i]);
119  if (it != input.end()) {
120  res[i] = it->second;
121  input.erase(it);
122  }
123  else {
124  res[i] = defaults.at(i);
125  global_logger().info("Cypress",
126  "For " + names[i] + " the default value " +
127  std::to_string(defaults[i]) + " is used");
128  }
129  }
130  for (auto elem : input) {
131  throw std::invalid_argument("Unknown parameter \"" + elem.first + "\"");
132  }
133 
134  return res;
135 }
136 
137 
138 }
139 #undef NAMED_PARAMETER
140 #endif /* CYPRESS_UTILS_NEURON_PARAMETERS_HPP */
Definition: neurons_base.hpp:54
std::map< std::string, T > json_to_map(const cypress::Json &obj)
Definition: neuron_parameters.hpp:98
Logger & global_logger()
const std::vector< Real > & parameter() const
Definition: neuron_parameters.hpp:60
void print(std::ostream &out=std::cout)
Definition: neuron_parameters.hpp:85
NeuronParameter()
Definition: neuron_parameters.hpp:58
double Real
Definition: types.hpp:56
nlohmann::json Json
Definition: json.hpp:27
std::vector< T > read_check(std::map< std::string, T > &input, const std::vector< std::string > &names, const std::vector< T > &defaults)
Definition: neuron_parameters.hpp:112
Definition: neuron_parameters.hpp:46
Definition: brainscales_lib.hpp:39
void info(const std::string &module, const std::string &message)