SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
read_json.hpp
Go to the documentation of this file.
1 /*
2  * SNABSuite -- Spiking Neural Architecture Benchmark Suite
3  * Copyright (C) 2016 Christoph Jenzen, Andreas Stöckel
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 SNABSUITE_UTIL_READ_JSON_HPP
22 #define SNABSUITE_UTIL_READ_JSON_HPP
23 
24 #include <cypress/cypress.hpp>
25 
26 #include <fstream>
27 #include <limits>
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 namespace SNAB {
33 using cypress::global_logger;
37 template <typename T>
38 std::map<std::string, T> json_to_map(const cypress::Json &obj)
39 {
40  std::map<std::string, T> input;
41  for (auto i = obj.begin(); i != obj.end(); i++) {
42  const cypress::Json value = i.value();
43  // Suppress entries like Neuron_Type and sub-lists
44  if (value.is_number()) {
45  input.emplace(i.key(), value);
46  }
47  }
48  return input;
49 }
50 
55 template <typename T>
56 std::vector<T> read_check(std::map<std::string, T> &input,
57  const std::vector<std::string> &names,
58  const std::vector<T> &defaults)
59 {
60  std::vector<T> res(names.size());
61  for (size_t i = 0; i < res.size(); i++) {
62  auto it = input.find(names[i]);
63  if (it != input.end()) {
64  res[i] = it->second;
65  input.erase(it);
66  }
67  else {
68  res[i] = defaults.at(i);
69  global_logger().debug("SNABSuite",
70  "For " + names[i] + " the default value " +
71  std::to_string(defaults[i]) + " is used");
72  }
73  }
74  for (auto elem : input) {
75  throw std::invalid_argument("Unknown parameter \"" + elem.first + "\"");
76  }
77 
78  return res;
79 }
80 
81 cypress::Json read_config(const std::string &name, const std::string &backend);
82 
83 bool check_json_for_parameters(const std::vector<std::string> &parameters,
84  const cypress::Json &json,
85  const std::string name);
86 
91 cypress::Json extract_backend(const cypress::Json &config,
92  const std::string &backend);
93 
100 template <typename T>
101 std::vector<T> json_array_to_vector(const cypress::Json &json)
102 {
103  if (!json.is_array() || json.size() == 0 || json[0].is_array()) {
104  throw std::invalid_argument("Error in conversion from Json to array!");
105  }
106  std::vector<T> res;
107  for (auto i : json) {
108  if (i.is_null()) {
109  res.push_back(std::numeric_limits<T>::quiet_NaN());
110  }
111  else {
112  res.push_back(T(i));
113  }
114  }
115  return res;
116 }
117 
124 template <typename T, int n>
125 std::array<T, n> json_array_to_array(const cypress::Json &json)
126 {
127  if (!json.is_array() || json.size() == 0 || json[0].is_array()) {
128  throw std::invalid_argument("Error in conversion from Json to array!");
129  }
130  std::array<T, n> res;
131  size_t index = 0;
132  for (auto i : json) {
133  if (i.is_null()) {
134  res[index] = std::numeric_limits<T>::quiet_NaN();
135  }
136  else {
137  res[index] = T(i);
138  }
139  index ++;
140  }
141  return res;
142 }
143 
149 template <typename T>
150 std::vector<std::vector<T>> json_2Darray_to_vector(const cypress::Json &json)
151 {
152  if (!json.is_array() || !json[0].is_array()) {
153  throw std::invalid_argument("Error in conversion from Json to array!");
154  }
155  std::vector<std::vector<T>> res;
156  for (auto i : json) {
157  res.push_back(json_array_to_vector<T>(i));
158  }
159  return res;
160 }
161 
167 template <typename T, int n>
168 std::vector<std::vector<std::array<T, n>>> json_3Darray_to_vector(const cypress::Json &json)
169 {
170  if (!json.is_array() || !json[0].is_array()) {
171  throw std::invalid_argument("Error in conversion from Json to array!");
172  }
173  std::vector<std::vector<std::array<T, n>>> res;
174  for (size_t i = 0; i< json.size(); i++) {
175  std::vector<std::array<T, n>> res2;
176  for (size_t j = 0; j< json[i].size(); j++) {
177  res2.push_back(json_array_to_array<T, n>(json[i][j]));
178  }
179  res.push_back(res2);
180  }
181  return res;
182 }
183 
191 bool replace_arrays_by_value(cypress::Json &json, const size_t &index = 0,
192  std::string name = "", bool warn = true);
193 } // namespace SNAB
194 
195 #endif /* SNABSUITE_UTIL_READ_JSON_HPP */
std::vector< T > read_check(std::map< std::string, T > &input, const std::vector< std::string > &names, const std::vector< T > &defaults)
Definition: read_json.hpp:56
std::vector< std::vector< std::array< T, n > > > json_3Darray_to_vector(const cypress::Json &json)
Definition: read_json.hpp:168
std::vector< std::vector< T > > json_2Darray_to_vector(const cypress::Json &json)
Definition: read_json.hpp:150
std::vector< T > json_array_to_vector(const cypress::Json &json)
Definition: read_json.hpp:101
cypress::Json read_config(const std::string &name, const std::string &backend)
cypress::Json extract_backend(const cypress::Json &config, const std::string &backend)
std::map< std::string, T > json_to_map(const cypress::Json &obj)
Definition: read_json.hpp:38
bool replace_arrays_by_value(cypress::Json &json, const size_t &index=0, std::string name="", bool warn=true)
bool check_json_for_parameters(const std::vector< std::string > &parameters, const cypress::Json &json, const std::string name)
std::array< T, n > json_array_to_array(const cypress::Json &json)
Definition: read_json.hpp:125