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

SpiNNakerManchester / sPyNNaker / 7459032187

09 Jan 2024 09:18AM UTC coverage: 69.136%. Remained the same
7459032187

push

github

web-flow
Merge pull request #1432 from SpiNNakerManchester/remove_8

remove spynnaker8 refrence

6977 of 9657 branches covered (0.0%)

Branch coverage included in aggregate %.

12713 of 18823 relevant lines covered (67.54%)

0.68 hits per line

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

9.2
/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
from types import ModuleType
1✔
18
from typing import Optional
1✔
19
import numpy as np
1✔
20
plt: Optional[ModuleType]
1✔
21
try:
1✔
22
    import matplotlib.pyplot  # type: ignore[import]
1✔
23
    plt = matplotlib.pyplot
1✔
24
except ImportError:
×
25
    plt = None
×
26

27

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

43

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

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

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

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

77

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

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

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

113

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

123

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

135

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

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

147
    if isinstance(spikes, np.ndarray):
×
148
        spikes = [spikes]
×
149

150
    colours = _get_colour()
×
151

152
    min_time = sys.maxsize
×
153
    max_time = 0
×
154
    min_spike = sys.maxsize
×
155
    max_spike = 0
×
156

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

181

182
# This is code for manual testing.
183
if __name__ == "__main__":
1!
184
    spike_data = np.loadtxt("spikes.csv", delimiter=',')
×
185
    plot_spikes(spike_data)
×
186
    doubled_spike_data = np.loadtxt("spikes.csv", delimiter=',')
×
187
    for _i, doubled_spike_data_i in enumerate(doubled_spike_data):
×
188
        doubled_spike_data_i[0] = doubled_spike_data[_i][0] + 5
×
189
    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