forked from open-ephys/plugin-GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLfpDisplayNodeTests.cpp
More file actions
382 lines (326 loc) · 14.5 KB
/
LfpDisplayNodeTests.cpp
File metadata and controls
382 lines (326 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2024 Open Ephys
------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "gtest/gtest.h"
#include "../LfpDisplayCanvas.h"
#include "../LfpDisplayNode.h"
#include <ModelApplication.h>
#include <ModelProcessors.h>
#include <ProcessorHeaders.h>
#include <TestFixtures.h>
/*
Class that can hold buffers of sample data written to the canvas and produce an 'expected' result image
Used to verify the LFP Canvas' ability to accurately display voltages
*/
class ExpectedImage
{
public:
ExpectedImage (int channels, int samples) : numChannels (channels), numSamplesInView (samples), lastSampleWritten (0)
{
for (int c = 0; c < numChannels; c++)
{
Array<float> newBuffer;
newBuffer.insertMultiple (0, 0, samples);
buffers.add (newBuffer);
}
}
/*Adds samples from an AudioBuffer to internal buffer. Should be same buffer passed to the LFP Canvas*/
void addToBuffer (AudioBuffer<float> newSamples)
{
for (int channelIndex = 0; channelIndex < numChannels; channelIndex++)
{
const float* channelReadPointer = newSamples.getReadPointer (channelIndex);
int currentSampleBufferIndex = lastSampleWritten;
for (int sampleIndex = 0; sampleIndex < newSamples.getNumSamples(); sampleIndex++)
{
float val = channelReadPointer[sampleIndex];
Array<float>& writeBuffer = buffers.getReference (channelIndex);
writeBuffer.set (currentSampleBufferIndex++, val);
if (currentSampleBufferIndex >= numSamplesInView)
{
currentSampleBufferIndex = 0;
}
}
}
lastSampleWritten += newSamples.getNumSamples() % numSamplesInView;
}
/*Constructs the trace information from internal buffers into an Image object*/
Image getImage (int width, int height, Array<Colour> channelColours, Colour backgroundColour, Colour midlineColour, int scaleFactor) const
{
Image expectedImage (Image::ARGB, width, height, true, SoftwareImageType());
//Fill image with background colour
Graphics g (expectedImage);
g.fillAll (backgroundColour);
int heightPerChannel = height / numChannels;
float samplesPerPixel = (float) numSamplesInView / (float) width;
//Draw Midline
for (int channelIndex = 0; channelIndex < numChannels; channelIndex += 1)
{
for (int x = 0; x < width; x++)
{
expectedImage.setPixelAt (x, channelIndex * heightPerChannel + heightPerChannel / 2, midlineColour);
}
}
//Draw trace data
for (int channelIndex = 0; channelIndex < numChannels; channelIndex++)
{
int lastPixelY = 0;
for (int xWritePixel = 0; xWritePixel < width; xWritePixel++)
{
int avg = 0;
int startSampleIndex = std::ceil (xWritePixel * samplesPerPixel);
int endSampleIndex = (std::ceil ((xWritePixel + 1) * samplesPerPixel) - 1);
for (int i = startSampleIndex; i < endSampleIndex; i++)
{
avg += buffers[channelIndex][i];
}
avg /= (endSampleIndex - startSampleIndex);
int amplitude = float (avg) / float (scaleFactor) * ((height / numChannels) / 2);
int channelMidline = channelIndex * heightPerChannel + heightPerChannel / 2;
int yWritePixel = channelMidline - amplitude;
if (yWritePixel >= 0 && yWritePixel < height)
{
expectedImage.setPixelAt (xWritePixel, yWritePixel, channelColours[channelIndex]);
if (xWritePixel != 0)
{
//Need to fill in gaps if difference between y pixels
int currentY = lastPixelY;
while (currentY != yWritePixel)
{
expectedImage.setPixelAt (xWritePixel, currentY, channelColours[channelIndex]);
currentY = yWritePixel > currentY ? currentY + 1 : currentY - 1;
}
}
lastPixelY = yWritePixel;
}
}
}
//Draw playback line
int playbackXPixel = int (std::ceil (float (lastSampleWritten) / float (samplesPerPixel))) % width + 1;
for (int playbackYPixel = 0; playbackYPixel < height; playbackYPixel += 2)
{
expectedImage.setPixelAt (playbackXPixel, playbackYPixel + 1, Colours::yellow);
}
return expectedImage;
}
int lastSampleWritten;
int numSamplesInView;
int numChannels;
Array<Array<float>> buffers;
int height;
int width;
};
class LfpDisplayNodeTests : public testing::Test
{
protected:
void SetUp() override
{
numChannels = 16;
tester = std::make_unique<ProcessorTester> (TestSourceNodeBuilder (FakeSourceNodeParams {
numChannels,
sampleRate,
bitVolts }));
processor = tester->createProcessor<LfpViewer::LfpDisplayNode> (Plugin::Processor::SINK);
width = 0;
height = 0;
x = 0;
y = 0;
midlineColour = Colour (50, 50, 50);
playheadColour = Colours::yellow;
}
/*Create a new AudioBuffer filled with step data*/
AudioBuffer<float> createBuffer (float starting_value, float step, int numChannels, int numSamples)
{
AudioBuffer<float> inputBuffer (numChannels, numSamples);
// in microvolts
float currValue = starting_value;
for (int chidx = 0; chidx < numChannels; chidx++)
{
for (int sampleIdx = 0; sampleIdx < numSamples; sampleIdx++)
{
inputBuffer.setSample (chidx, sampleIdx, currValue);
currValue += step;
}
}
return inputBuffer;
}
/*Creates a new AudioBuffer filled with sinusoidal waves*/
AudioBuffer<float> createBufferSinusoidal (int cycles, int numChannels, int numSamples, int amplitude)
{
AudioBuffer<float> inputBuffer (numChannels, numSamples);
float pi = 3.1415;
// in microvolts
for (int chidx = 0; chidx < numChannels; chidx++)
{
for (int sampleIdx = 0; sampleIdx < numSamples; sampleIdx++)
{
float value = amplitude * sin (float (sampleIdx) / (float (numSamples) / float (cycles)) * 2 * pi);
inputBuffer.setSample (chidx, sampleIdx, value);
}
}
return inputBuffer;
}
/*Writes data from an AudioBuffer to processor. Verifies that the AudioBuffer is not changed*/
void writeBlock (AudioBuffer<float>& inputBuffer)
{
auto outputBuffer = tester->processBlock (processor, inputBuffer);
// Assert the buffer hasn't changed after process()
ASSERT_EQ (inputBuffer.getNumSamples(), outputBuffer.getNumSamples());
ASSERT_EQ (inputBuffer.getNumChannels(), outputBuffer.getNumChannels());
for (int chidx = 0; chidx < inputBuffer.getNumChannels(); chidx++)
{
for (int sampleIdx = 0; sampleIdx < inputBuffer.getNumSamples(); ++sampleIdx)
{
ASSERT_EQ (inputBuffer.getSample (chidx, sampleIdx), outputBuffer.getSample (chidx, sampleIdx));
}
}
currentSampleIndex += inputBuffer.getNumSamples();
}
/*Gets information from canvas needed to build an ExpectedImage*/
void setExpectedImageParameters (LfpViewer::LfpDisplayCanvas* canvas)
{
canvas->getChannelBitmapBounds (0, x, y, width, height);
canvas->getChannelColours (0, channelColours, backgroundColour);
ASSERT_EQ (channelColours.size(), numChannels);
}
/*Compares 2 Image objects and returns the number of different pixels*/
int getImageDifferencePixelCount (Image expectedImage, Image actualImage) const
{
int missCount = 0;
Image diffImage (Image::ARGB, width, height, true);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Colour expectedPixel = expectedImage.getPixelAt (x, y);
Colour actualPixel = actualImage.getPixelAt (x, y);
if (expectedPixel != actualPixel)
{
//Allow some variance - if expectedPixel is a voltage Pixel then see if actualPixel +- 1 pixel is also a voltage Pixel.
//If true then don't count as a miss
if (expectedPixel != backgroundColour && expectedPixel != midlineColour && expectedPixel != playheadColour)
{
Colour postiveVariance = actualImage.getPixelAt (x, y + 1);
Colour negativeVariance = actualImage.getPixelAt (x, y - 1);
if (postiveVariance == expectedPixel || negativeVariance == expectedPixel)
{
continue;
}
}
//Same for if actualPixel is a voltagePixel and expectedPixel isn't
if (actualPixel != backgroundColour && actualPixel != midlineColour && actualPixel != playheadColour)
{
Colour postiveVariance = expectedImage.getPixelAt (x, y + 1);
Colour negativeVariance = expectedImage.getPixelAt (x, y - 1);
if (postiveVariance == actualPixel || negativeVariance == actualPixel)
{
continue;
}
}
missCount += 1;
}
}
}
return missCount;
}
void dumpPng (String path, Image image)
{
FileOutputStream stream ((File (path)));
PNGImageFormat pngWriter;
pngWriter.writeImageToStream (image, stream);
}
LfpViewer::LfpDisplayNode* processor;
int numChannels;
float bitVolts = 1.0;
std::unique_ptr<ProcessorTester> tester;
int64_t currentSampleIndex = 0;
float sampleRate = 2000;
Image expectedImage;
Colour midlineColour;
Colour backgroundColour;
Colour playheadColour;
Array<Colour> channelColours;
int width;
int height;
int x;
int y;
};
TEST_F (LfpDisplayNodeTests, VisualIntegrityTest)
{
const int canvasX = 600;
const int canvasY = 800;
const float errorThreshold = 0.05f; //5% error threshold
//Initialize LFP Canvas
std::unique_ptr<LfpViewer::LfpDisplayCanvas> canvas = std::make_unique<LfpViewer::LfpDisplayCanvas> (processor, LfpViewer::SplitLayouts::SINGLE, false);
canvas->updateSettings();
canvas->setSize (canvasX, canvasY);
canvas->resized();
canvas->setVisible (true);
//Get LFP size parameters from canvas
setExpectedImageParameters (canvas.get());
//Create snapshot of canvas channel bitmap and expected image
Rectangle<int> canvasSnapshot (x, y, width, height);
ExpectedImage expected (numChannels, sampleRate * 2); //2 seconds to match canvas timebase
processor->startAcquisition ();
canvas->beginAnimation();
//Add 5 10Hz waves with +-125uV amplitude
auto inputBuffer = createBufferSinusoidal (5, numChannels, 1000, 125);
writeBlock (inputBuffer);
expected.addToBuffer (inputBuffer);
canvas->refreshState();
Image canvasImage = canvas->createComponentSnapshot (canvasSnapshot);
Image expectedImage = expected.getImage (width, height, channelColours, backgroundColour, midlineColour, 125);
int missCount = getImageDifferencePixelCount (expectedImage, canvasImage);
EXPECT_LE (float(missCount) / float(width * height), errorThreshold);
//Add 5 10Hz waves with +-250uV amplitude
inputBuffer = createBufferSinusoidal (5, numChannels, 1000, 250);
writeBlock (inputBuffer);
expected.addToBuffer (inputBuffer);
canvas->refreshState();
canvasImage = canvas->createComponentSnapshot (canvasSnapshot);
expectedImage = expected.getImage (width, height, channelColours, backgroundColour, midlineColour, 125);
missCount = getImageDifferencePixelCount (expectedImage, canvasImage);
EXPECT_LE (float (missCount) / float (width * height), errorThreshold);
//Add 10 40Hz waves with +-250uV amplitude
inputBuffer = createBufferSinusoidal (10, numChannels, 1000, 250);
writeBlock (inputBuffer);
expected.addToBuffer (inputBuffer);
canvas->refreshState();
canvasImage = canvas->createComponentSnapshot (canvasSnapshot);
expectedImage = expected.getImage (width, height, channelColours, backgroundColour, midlineColour, 125);
missCount = getImageDifferencePixelCount (expectedImage, canvasImage);
EXPECT_LE (float (missCount) / float (width * height), errorThreshold);
//Resize canvas to have half the vertical height and twice the uV range
canvas->setChannelHeight (0, 20);
canvas->setChannelRange (0, 500, ContinuousChannel::Type::ELECTRODE);
canvas->refreshState();
setExpectedImageParameters (canvas.get());
canvasSnapshot.setBounds (x, y, width, height);
canvasImage = canvas->createComponentSnapshot (canvasSnapshot);
expectedImage = expected.getImage (width, height, channelColours, backgroundColour, midlineColour, 125);
missCount = getImageDifferencePixelCount (expectedImage, canvasImage);
EXPECT_LE (float (missCount) / float (width * height), errorThreshold);
processor->stopAcquisition();
}
TEST_F (LfpDisplayNodeTests, DataIntegrityTest)
{
int numSamples = 100;
processor->startAcquisition ();
auto inputBuffer = createBuffer (1000.0, 20.0, numChannels, numSamples);
writeBlock (inputBuffer);
processor->stopAcquisition();
}