SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
spike_plot.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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 Plots spike time data
22 """
23 
24 import argparse
25 
26 parser = argparse.ArgumentParser(description='Plot spike times')
27 
28 # Required Parameters
29 parser.add_argument("files", metavar="files", nargs='+', help="files to plot")
30 
31 # Optional arguments
32 parser.add_argument("-s", type=str, help="Name of the simulator", default="")
33 parser.add_argument("-p", help="Save as png instead of pdf",
34  default=False, action="store_true")
35 
36 args = parser.parse_args()
37 
38 import numpy as np
39 import matplotlib.pyplot as plt
40 from matplotlib.ticker import MaxNLocator
41 import os
42 
43 from dim_labels import *
44 
45 # Adapted from https://gist.github.com/kylerbrown/5530238
46 def raster_plot(spikes, simulator):
47  fig = plt.figure()
48  ax = plt.gca()
49  for ith, trial in enumerate(spikes):
50  plt.vlines(trial, ith + .5, ith + 1.5, color="#000000", lw=0.5)
51  ax.set_ylim(.5, len(spikes) + .5)
52  if simulator == "":
53  ax.set_title("Spike Times")
54  else:
55  ax.set_title("Spike Times for " + SIMULATOR_LABELS[simulator])
56  ax.set_xlabel(DIM_LABELS["time"])
57  ax.set_ylabel(DIM_LABELS["neuron id"])
58  if len(spikes)>1:
59  ax.yaxis.set_major_locator(MaxNLocator(integer=True))
60  else:
61  ax.set_yticks([1])
62  return fig
63 
64 for target_file in args.files:
65  #import data
66  data = []
67  with open(target_file) as file:
68  for line in file:
69  data2 = []
70  for entry in line.split(","):
71  try:
72  data2.append(float(entry))
73  except:
74  continue
75  data.append(data2)
76 
77  fig = raster_plot(data, args.s)
78 
79  if args.p:
80  fig.savefig(target_file.split(".csv")[0] + ".png", format='png',
81  dpi=600, bbox_inches='tight')
82  else:
83  fig.savefig(target_file.split(".csv")[0] + ".pdf", format='pdf',
84  bbox_inches='tight')
def raster_plot(spikes, simulator)
Definition: spike_plot.py:46