Cypress  1.0
C++ Spiking Neural Network Simulation Framework
to_json.hpp
Go to the documentation of this file.
1 /*
2  * Cypress -- C++ Spiking Neural Network Simulation Framework
3  * Copyright (C) 2019 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 #include <cypress/core/backend.hpp>
22 #include <cypress/core/network.hpp>
24 #include <cypress/util/json.hpp>
25 
26 #include <thread>
27 
28 namespace cypress {
29 
34 class ToJson : public Backend {
35 protected:
36  std::string m_simulator;
38  bool m_save_json = false;
39  bool m_no_output = false;
40  void do_run(NetworkBase &network, Real duration) const override;
41  std::string m_path, m_json_path;
42 
43  Json output_json(NetworkBase &network, Real duration) const;
44  void read_json(Json &result, NetworkBase &network) const;
45 
46 public:
56  ToJson(const std::string &simulator, const Json &setup = Json());
57 
61  ~ToJson() override;
62 
63  std::unordered_set<const NeuronType *> supported_neuron_types()
64  const override;
65 
69  std::string name() const override;
70 
79  static std::vector<bool> inhom_rec_single(const PopulationBase &pop,
80  size_t index);
81 
90  static Json inhom_rec_to_json(const PopulationBase &pop);
91 
100  static Json connector_to_json(const ConnectionDescriptor &conn);
101 
108  static Json pop_to_json(const PopulationBase &pop);
109 
117  static void hom_rec_to_json(const PopulationBase &pop, Json &json);
118 
126  static Json pop_vec_to_json(const std::vector<PopulationBase> &pops);
127 
134  static Json recs_to_json(const PopulationBase &pop);
135 
144  static void read_recordings_from_json(const Json &pop_data,
145  NetworkBase &netw);
146 
153  static void create_pop_from_json(const Json &pop_json, Network &netw);
154 
162  static std::shared_ptr<SynapseBase> get_synapse(
163  const std::string &name, const std::vector<Real> &parameters);
164 
171  static void create_conn_from_json(const Json &con_json, Network &netw);
172 
179  static NetworkBase network_from_json(std::string path);
180 
181  static void learned_weights_from_json(const Json &json, NetworkBase netw);
182 };
183 
191 void to_json(Json &j, const Network &netw);
192 
201 void from_json(const Json &j, Network &netw);
202 
209 void from_json(const Json &json, NetworkRuntime &runtime);
210 
217 void to_json(Json &json, const NetworkRuntime &runtime);
218 
219 template <typename T>
228 void to_json(Json &j, const Matrix<T> &mat)
229 {
230  /*if (mat.cols() == 1) {
231  std::vector<Real> rows(mat.rows());
232  for (size_t k = 0; k < mat.rows(); k++) {
233  rows[k] = mat(k, 0);
234  }
235  j.push_back(rows);
236  }
237  else {*/
238  for (size_t i = 0; i < mat.rows(); i++) {
239  std::vector<Real> rows(mat.cols());
240  for (size_t k = 0; k < mat.cols(); k++) {
241  rows[k] = mat(i, k);
242  }
243  j.push_back(rows);
244  }
245  //}
246 
247  // Maybe transpose // TODO
248  // std::cout << j<<std::endl;
249 }
250 
251 template <typename T>
260 void from_json(const Json &j, Matrix<T> &mat)
261 {
262  size_t rows = j.size();
263  if (rows == 0) {
264  return;
265  }
266  size_t cols = j[0].size();
267  mat = Matrix<T>(rows, cols);
268  for (size_t i = 0; i < rows; i++) {
269  for (size_t k = 0; k < cols; k++) {
270  mat(i, k) = T(j[i][k]);
271  }
272  }
273 }
274 
275 } // namespace cypress
static void hom_rec_to_json(const PopulationBase &pop, Json &json)
Takes a Json object created by pop_to_json and adds information about record flags, assumes record flags are homogeneous.
void to_json(Json &j, const Network &netw)
Automatic conversion of a Network object to JSON. Now you can uses Json(network) as you would do with...
Definition: backend.hpp:50
static Json connector_to_json(const ConnectionDescriptor &conn)
Converts the information of a ConnectionDescriptor to a JSON object. List connectors and dynamic conn...
static Json pop_to_json(const PopulationBase &pop)
Creates a Json containing information about a population.
void from_json(const Json &j, Network &netw)
With the method you can just use Network netw = json.get<Network>() as you can do with STL containers...
double Real
Definition: types.hpp:56
std::string m_path
Definition: to_json.hpp:41
static void create_conn_from_json(const Json &con_json, Network &netw)
Uses the information in JSON to create a single Connection.
static void read_recordings_from_json(const Json &pop_data, NetworkBase &netw)
Reads recorded signal from a json object, puts data into network object.
nlohmann::json Json
Definition: json.hpp:27
static std::vector< bool > inhom_rec_single(const PopulationBase &pop, size_t index)
returns a vector encoding recording flags for a single recording signal and inhomogeneous record flag...
Definition: network_base.hpp:75
ToJson(const std::string &simulator, const Json &setup=Json())
Definition: matrix.hpp:51
size_t rows() const
Definition: matrix.hpp:339
size_t cols() const
Definition: matrix.hpp:344
Json output_json(NetworkBase &network, Real duration) const
Definition: network_base.hpp:116
std::string m_simulator
Definition: to_json.hpp:36
void read_json(Json &result, NetworkBase &network) const
Definition: network_base_objects.hpp:53
static Json pop_vec_to_json(const std::vector< PopulationBase > &pops)
Creates an array of populations in return json, which contains all information of all populations...
std::string m_json_path
Definition: to_json.hpp:41
static std::shared_ptr< SynapseBase > get_synapse(const std::string &name, const std::vector< Real > &parameters)
Create the synapse for a connection dependent on the name.
Definition: to_json.hpp:34
Definition: network.hpp:349
Definition: brainscales_lib.hpp:39
Json m_setup
Definition: to_json.hpp:37
static NetworkBase network_from_json(std::string path)
Uses the data in a JSON object to create a full network.
~ToJson() override
void do_run(NetworkBase &network, Real duration) const override
static void create_pop_from_json(const Json &pop_json, Network &netw)
Uses data in JSon to create populations and set record flags.
std::unordered_set< const NeuronType * > supported_neuron_types() const override
static void learned_weights_from_json(const Json &json, NetworkBase netw)
static Json recs_to_json(const PopulationBase &pop)
Converts all recorded data of a population to json.
Definition: connector.hpp:705
bool m_no_output
Definition: to_json.hpp:39
std::string name() const override
static Json inhom_rec_to_json(const PopulationBase &pop)
returns a json encoding all records for a single population. Json[<signal>] will return a vector of b...
bool m_save_json
Definition: to_json.hpp:38