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

SpiNNakerManchester / sPyNNaker / 8783112236

22 Apr 2024 11:16AM UTC coverage: 69.531%. Remained the same
8783112236

Pull #1420

github

web-flow
Merge branch 'master' into dependabot/github_actions/JamesIves/github-pages-deploy-action-4.5.0
Pull Request #1420: Bump JamesIves/github-pages-deploy-action from 4.4.3 to 4.5.0

7334 of 10026 branches covered (73.15%)

Branch coverage included in aggregate %.

12791 of 18918 relevant lines covered (67.61%)

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
# pylint: disable=invalid-name
21
plt: Optional[ModuleType]
1✔
22
try:
1✔
23
    import matplotlib.pyplot  # type: ignore[import]
1✔
24
    plt = matplotlib.pyplot
1✔
25
except ImportError:
×
26
    plt = None
×
27

28

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

44

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

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

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

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

78

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

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

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

114

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

124

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

136

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

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

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

151
    colours = _get_colour()
×
152

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

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

182

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