SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
generate_tex.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # SNABSuite -- Spiking Neural Architecture Benchmark Suite
5 # Copyright (C) 2017 Christoph Jenzen
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>
19 
20 
21 from __future__ import print_function
22 from builtins import str
23 from builtins import range
24 import json
25 import os
26 
27 from dim_labels import *
28 
29 # dictionary storing all results
30 results_dict = dict()
31 # Storing all variables which are arrays
32 config_array_dict = dict()
33 
34 
36  """
37  Adds results from file to global storage
38 
39  filename -- name of the file which will be used
40  """
41 
42  with open(filename) as temp:
43  json_temp = json.load(temp)
44 
45  simulator = filename.split(
46  "/")[-1].split(".json")[0].split("_")[0].split("=")[0].split("pynn.")[-1]
47 
48  # Trigger key errors for unknown simulators
49  if simulator not in SIMULATOR_LABELS:
50  return
51 
52  # Get the array number
53  array_nr = filename.split("/")[-1].split(".json")[0].split("_")[-1]
54  if not array_nr.isdigit():
55  array_nr = "0"
56 
57  if simulator not in results_dict:
58  results_dict[simulator] = dict()
59  results_dict[simulator][array_nr] = json_temp
60 
61 
63  """
64  Gathers all results from build directory
65  """
66 
67  for filename in os.listdir("../build/"):
68  # All jsons besides invisible files and backup data
69  if filename.endswith(".json") and not filename.startswith("."):
70  if not filename.endswith("_bak.json"):
71  result_json_to_global_dict("../build/" + filename)
72 
73 
74 def get_key_arrays(dictionary):
75  """
76  Iterates through the dictionary, gathers all entries with lists
77 
78  dictionary -- dict to iteratively
79  """
80 
81  temp_dict = dict()
82  for i in dictionary:
83  if type(dictionary[i]) == list:
84  temp_dict[i] = dictionary[i]
85  else:
86  if type(dictionary[i]) == dict:
87  a = get_key_arrays(dictionary[i])
88  if len(a) != 0:
89  temp_dict[i] = a
90  return temp_dict
91 
92 
94  """
95  Gathers all arrays in configs from config directory
96  """
97 
98  for filename in os.listdir("../config/"):
99  if filename.endswith(".json") and not filename.startswith("."):
100  with open("../config/" + filename) as temp:
101  json_temp = json.load(temp)
102  config_array_dict[filename.split(
103  ".")[0]] = get_key_arrays(json_temp)
104 
105 
106 def gather_array_names(snabname):
107  """
108  Get the array names for the title of the table
109  """
110 
111  array_names = []
112  for i in config_array_dict[snabname]: # Simulators
113  for j in config_array_dict[snabname][i]:
114  # Check if this is already included
115  if not j in array_names:
116  array_names.append(j)
117  return array_names
118 
119 
121  """
122  Get the measure names for the title of the table
123  """
124 
125  array_names = []
126  for i in results_dict:
127  try:
128  temp = results_dict[i]["0"]
129  except:
130  continue
131  for j in temp:
132  if not j["name"] == snabname:
133  continue
134  for k in j["results"]:
135  array_names.append(k["name"] + " in " + k["measures"])
136  if len(array_names) > 0:
137  return array_names
138  return array_names
139 
140 
141 def write_array_params(texfile, snab, backend, param_names, index):
142  """
143  Write parameters of the benchmark run
144 
145  texfile -- open file descriptor to file
146  snab -- name string of the snab
147  backend -- backend string
148  param_names -- list of array names created by gather_array_names
149  index -- index of the current array entry
150  """
151 
152  for i in param_names:
153  if backend in config_array_dict[snab] and i in config_array_dict[snab][backend]:
154  texfile.write(
155  " & " + str(config_array_dict[snab][backend][i][int(index)]))
156  else:
157  texfile.write(" & ")
158 
159 
160 def write_results(texfile, backend, index, snab, measures, got_snab):
161  """
162  Write results of the benchmark run
163 
164  texfile -- open file descriptor to file
165  snab -- name string of the snab
166  backend -- backend string
167  measures -- list of measure names created by gather_benchmark_measures
168  index -- index of the current array entry
169  got_snab -- boolean wether there was at least one entry
170  """
171 
172  for i in results_dict[backend][index]:
173  if i["name"] == snab:
174  got_snab = True
175  for j in measures:
176  found = False
177  for k in i["results"]:
178  if k["name"] == j.split(" in ")[0]:
179  texfile.write(" & " + '{:.2f}'.format(k["value"]))
180  found = True
181  # SNAB exists but measure was not found?
182  if not found:
183  texfile.write(" & ")
184  texfile.write("\n")
185 
186  # Fill in gaps if there were no results
187  if not got_snab:
188  for j in measures:
189  texfile.write(" & ")
190  texfile.write("\n")
191  return got_snab
192 
193 
196 
197 if not os.path.exists("tables"):
198  os.mkdir("tables")
199 
200 for snabname in config_array_dict: # iterate over all SNABs
201  print("Constructing ", snabname +".tex")
202  # Assemble header: Platform, all array names, benchmark indicators
203  param_names = gather_array_names(snabname)
204  measures = gather_benchmark_measures(snabname)
205  texfile = open("tables/" + snabname + ".tex", 'w')
206  texfile.write("\\begin{tabular}{r")
207  for i in range(0, len(param_names)):
208  texfile.write(" r")
209  for i in range(0, len(measures)):
210  texfile.write(" l")
211  texfile.write("}\n\\toprule\n")
212  texfile.write("Platform")
213  for i in range(0, len(param_names)):
214  texfile.write(" & " + param_names[i])
215  for i in range(0, len(measures)):
216  texfile.write(" & " + measures[i])
217 
218  texfile.write("\n\\midrule\n")
219 
220  # Write data
221  for backend in results_dict:
222  texfile.write(backend)
223  got_snab = False
224  for k in range(0, len(results_dict[backend])):
225  write_array_params(texfile, snabname, backend, param_names, str(k))
226  got_snab = write_results(texfile, backend, str(
227  k), snabname, measures, got_snab)
228 
229  texfile.write("\\bottomrule\n\end{tabular}")
230 
def get_key_arrays(dictionary)
Definition: generate_tex.py:74
def assemble_global_dict()
Definition: generate_tex.py:62
def write_results(texfile, backend, index, snab, measures, got_snab)
def gather_benchmark_measures(snabname)
def get_key_arrays_from_config()
Definition: generate_tex.py:93
def gather_array_names(snabname)
def write_array_params(texfile, snab, backend, param_names, index)
def result_json_to_global_dict(filename)
Definition: generate_tex.py:35