SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
plot_membrane_pot.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 """
22 Plots the membrane voltage, possibility to plot spike times on top of it
23 """
24 from builtins import range
25 import argparse
26 
27 parser = argparse.ArgumentParser(description='Plot membrane potential graphs')
28 
29 # Optional arguments
30 parser.add_argument("--ymin", type=float, help="minimal y-value")
31 parser.add_argument("--ymax", type=float, help="maximal y-value")
32 parser.add_argument("--xmin", type=float, help="minimal x-value")
33 parser.add_argument("--xmax", type=float, help="maximal x-value")
34 parser.add_argument("-s", type=str, help="Name of the simulator", default="")
35 parser.add_argument("-tc", type=int, help="Column of time-values", default=0)
36 parser.add_argument("-sp", type=str, help="file containing spikes")
37 parser.add_argument(
38  "-spc", type=int, help="Number of the Column containing spikes", default=0)
39 
40 
41 # Required Parameters
42 parser.add_argument("-y", type=int, required=True, help="Column of y-values")
43 parser.add_argument("file", metavar="file", type=str,
44  help="file with membrane voltage to plot")
45 
46 args = parser.parse_args()
47 
48 
49 import numpy as np
50 import matplotlib.pyplot as plt
51 import matplotlib.colorbar
52 import sys
53 import os
54 
55 from dim_labels import *
56 
57 
58 def cm2inch(value):
59  return value / 2.54
60 
61 
62 def get_max(data):
63  idcs = np.isfinite(data)
64  return np.max(data[np.isfinite(data)])
65 
66 
67 def get_min(data):
68  idcs = np.isfinite(data)
69  return np.min(data[np.isfinite(data)])
70 
71 
72 def plot_membrane(ax, xs, ys, color, ymin=None, ymax=None, xmin=None, xmax=None, label=""):
73  """
74  Plots membrane potential and spikes
75 
76  ax -- plt.figure object to plot into
77  xs -- x values
78  ys -- y values
79  color -- color for the membrane potential graph
80  ymin -- Minimal y value for limits of the plot
81  ymax -- Maximal y value for limits of the plot
82  xmin -- Mnimial x value for limits of the plot
83  xmin -- Maximal x value for limits of the plot
84  """
85 
86  ax.plot(xs, ys, color=color, lw=0.3, zorder=1, label=label)
87 
88  ax.set_xlabel("Time in ms")
89  ax.set_ylabel("Voltage in mV")
90 
91  # Set the limits of y-axxis
92  if (ymin is None) and (ymax is None):
93  ax.autoscale_view()
94  elif ymax is None:
95  ax.set_ylim(bottom=ymin)
96  elif ymin is None:
97  ax.set_ylim(top=ymax)
98  else:
99  ax.set_ylim(bottom=ymin, top=ymax)
100 
101  # Set the limits of x-axxis
102  if (xmin is None) and (xmax is None):
103  ax.set_xlim(left=get_min(xs), right=get_max(xs))
104  elif xmax is None:
105  ax.set_xlim(left=xmin, right=get_max(xs))
106  elif xmin is None:
107  ax.set_xlim(left=get_min(xs), right=xmax)
108  else:
109  ax.set_xlim(left=xmin, right=xmax)
110 
111 
112 fig = plt.figure(figsize=(cm2inch(12), cm2inch(6.0)))
113 ax = fig.add_subplot(111)
114 
115 # Read membrane potential
116 results = np.genfromtxt(args.file, delimiter=',', names=None)
117 data = np.zeros((results.shape[0], len(results[0])))
118 for i in range(0, len(results)):
119  data[i] = np.array(list(results[i]))
120 
121 xs = np.array(data[:, args.tc])
122 ys = np.array(data[:, args.y])
123 
124 label = "Membrane Voltage"
125 if args.s is not None:
126  label = label + " of " + SIMULATOR_LABELS[args.s]
127 
128 plot_membrane(ax, xs, ys, color="#000000", ymin=args.ymin,
129  ymax=args.ymax, xmin=args.xmin, xmax=args.xmax, label=label)
130 
131 
132 # Plot spike times
133 if args.sp is not None:
134  results2 = np.genfromtxt(args.sp, delimiter=',', names=None)
135  # One dimensional data
136  if not isinstance(results2[0], list):
137  first = True
138  for i in results2:
139  if first:
140  ax.axvline(x=i, linestyle=':', color='r',
141  lw=0.3, label="Spike Times")
142  first = False
143  else:
144  ax.axvline(x=i, linestyle=':', color='r',
145  lw=0.3)
146  else:
147  # Convert data
148  spikes = np.zeros((results2.shape[0], len(results2[0])))
149  for i in range(0, len(results2)):
150  spikes[i] = np.array(list(results2[i]))
151 
152  # Plot
153  first = True
154  for i in spikes[:, args.spc]:
155  if first:
156  ax.axvline(x=i, linestyle=':', color='r',
157  lw=0.3, label="Spike Times")
158  first = False
159  else:
160  ax.axvline(x=i, linestyle=':', color='r',
161  lw=0.3)
162 
163 
164 ax.legend(loc='lower center', bbox_to_anchor=(0.5, 1.05),
165  ncol=4)
166 fig.savefig(args.file.split('.csv')[0] +
167  ".pdf", format='pdf', bbox_inches='tight')
def plot_membrane(ax, xs, ys, color, ymin=None, ymax=None, xmin=None, xmax=None, label="")