• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / sPyNNaker / 5474087341

pending completion
5474087341

push

github

Christian-B
merged in master

1932 of 4479 branches covered (43.13%)

Branch coverage included in aggregate %.

11780 of 17882 relevant lines covered (65.88%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.56
/spynnaker/plot_utils.py
1
# Copyright (c) 2017 The University of Manchester
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     https://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
# Imports
16
import sys
1✔
17
import numpy as np
1✔
18
try:
1✔
19
    import matplotlib.pyplot as plt
1✔
20
    matplotlib_missing = False
1✔
21
except ImportError:
×
22
    plt = None
×
23
    matplotlib_missing = True
×
24

25

26
def _precheck(data, title):
1✔
27
    if not len(data):
×
28
        if title is None:
×
29
            print("NO Data")
×
30
        else:
31
            print("NO data for " + title)
×
32
        return False
×
33
    if matplotlib_missing:
×
34
        if title is None:
×
35
            print("matplotlib not installed skipping plotting")
×
36
        else:
37
            print("matplotlib not installed skipping plotting for " + title)
×
38
        return False
×
39
    return True
×
40

41

42
def line_plot(data_sets, title=None):
1✔
43
    """
44
    Build a line plot or plots.
45

46
    :param data_sets: Numpy array of data, or list of numpy arrays of data
47
    :type data_sets: ~numpy.ndarray or list(~numpy.ndarray)
48
    :param title: The title for the plot
49
    :type title: str or None
50
    """
51
    if not _precheck(data_sets, title):
×
52
        return
×
53
    print("Setting up line graph")
×
54
    if isinstance(data_sets, np.ndarray):
×
55
        data_sets = [data_sets]
×
56

57
    print(f"Setting up {len(data_sets)} sets of line plots")
×
58
    (numrows, numcols) = _grid(len(data_sets))
×
59
    for data, index in enumerate(data_sets):
×
60
        plt.subplot(numrows, numcols, index+1)
×
61
        for neuron in np.unique(data[:, 0]):
×
62
            time = [i[1] for i in data if i[0] == neuron]
×
63
            membrane_voltage = [i[2] for i in data if i[0] == neuron]
×
64
            plt.plot(time, membrane_voltage)
×
65

66
        min_data = min(data[:, 2])
×
67
        max_data = max(data[:, 2])
×
68
        adjust = (max_data - min_data) * 0.1
×
69
        plt.axis([min(data[:, 1]), max(data[:, 1]), min_data - adjust,
×
70
                  max_data + adjust])
71
    if title is not None:
×
72
        plt.title(title)
×
73
    plt.show()
×
74

75

76
def heat_plot(data_sets, ylabel=None, title=None):
1✔
77
    """
78
    Build a heatmap plot or plots.
79

80
    :param data_sets: Numpy array of data, or list of numpy arrays of data
81
    :type data_sets: ~numpy.ndarray or list(~numpy.ndarray)
82
    :param ylabel: The label for the Y axis
83
    :type ylabel: str or None
84
    :param title: The title for the plot
85
    :type title: str or None
86
    """
87
    if not _precheck(data_sets, title):
×
88
        return
×
89
    if isinstance(data_sets, np.ndarray):
×
90
        data_sets = [data_sets]
×
91

92
    print(f"Setting up {len(data_sets)} sets of heat graph")
×
93
    (numrows, numcols) = _grid(len(data_sets))
×
94
    for data, index in enumerate(data_sets):
×
95
        plt.subplot(numrows, numcols, index+1)
×
96
        neurons = data[:, 0].astype(int)
×
97
        times = data[:, 1].astype(int)
×
98
        info = data[:, 2]
×
99
        info_array = np.empty((max(neurons)+1, max(times)+1))
×
100
        info_array[:] = np.nan
×
101
        info_array[neurons, times] = info
×
102
        plt.xlabel("Time (ms)")
×
103
        plt.ylabel(ylabel)
×
104
        plt.imshow(info_array, cmap='hot', interpolation='none',
×
105
                   aspect='auto')
106
        plt.colorbar()
×
107
    if title is not None:
×
108
        plt.title(title)
×
109
    plt.show()
×
110

111

112
def _get_colour():
1✔
113
    yield "b."
×
114
    yield "g."
×
115
    yield "r."
×
116
    yield "c."
×
117
    yield "m."
×
118
    yield "y."
×
119
    yield "k."
×
120

121

122
def _grid(length):
1✔
123
    if length == 1:
×
124
        return 1, 1
×
125
    if length == 2:
×
126
        return 1, 2
×
127
    if length == 3:
×
128
        return 1, 3
×
129
    if length == 4:
×
130
        return 2, 2
×
131
    return length // 3 + 1, length % 3 + 1
×
132

133

134
def plot_spikes(spikes, title="spikes"):
1✔
135
    """
136
    Build a spike plot or plots.
137

138
    :param spikes: Numpy array of spikes, or list of numpy arrays of spikes
139
    :type spikes: ~numpy.ndarray or list(~numpy.ndarray)
140
    :param str title: The title for the plot
141
    """
142
    if not _precheck(spikes, title):
×
143
        return
×
144

145
    if isinstance(spikes, np.ndarray):
×
146
        spikes = [spikes]
×
147

148
    colours = _get_colour()
×
149

150
    min_time = sys.maxsize
×
151
    max_time = 0
×
152
    min_spike = sys.maxsize
×
153
    max_spike = 0
×
154

155
    print(f"Plotting {len(spikes)} set of spikes")
×
156
    (numrows, numcols) = _grid(len(spikes))
×
157
    for single_spikes, index in enumerate(spikes):
×
158
        # pylint: disable=nested-min-max
159
        plt.subplot(numrows, numcols, index+1)
×
160
        spike_time = [i[1] for i in single_spikes]
×
161
        spike_id = [i[0] for i in single_spikes]
×
162
        min_time = min(min_time, min(spike_time))
×
163
        max_time = max(max_time, max(spike_time))
×
164
        min_spike = min(min_spike, min(spike_id))
×
165
        max_spike = max(max_spike, max(spike_id))
×
166
        plt.plot(spike_time, spike_id, next(colours), )
×
167
    plt.xlabel("Time (ms)")
×
168
    plt.ylabel("Neuron ID")
×
169
    plt.title(title)
×
170
    time_diff = (max_time - min_time) * 0.05
×
171
    min_time = min_time - time_diff
×
172
    max_time = max_time + time_diff
×
173
    spike_diff = (max_spike - min_spike) * 0.05
×
174
    min_spike = min_spike - spike_diff
×
175
    max_spike = max_spike + spike_diff
×
176
    plt.axis([min_time, max_time, min_spike, max_spike])
×
177
    plt.show()
×
178

179

180
# This is code for manual testing.
181
if __name__ == "__main__":
1!
182
    spike_data = np.loadtxt("spikes.csv", delimiter=',')
×
183
    plot_spikes(spike_data)
×
184
    doubled_spike_data = np.loadtxt("spikes.csv", delimiter=',')
×
185
    for doubled_spike_data_i, _i in enumerate(doubled_spike_data):
×
186
        doubled_spike_data_i[0] = doubled_spike_data[_i][0] + 5
×
187
    plot_spikes([spike_data, doubled_spike_data])
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc