Skip to content

Commit 0fa4a36

Browse files
committed
add tests for dots
1 parent 0543258 commit 0fa4a36

File tree

5 files changed

+113
-58
lines changed

5 files changed

+113
-58
lines changed

src/dot/initDots.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
% thisEvent.speed
1515
%
1616
%
17-
18-
% TODO
19-
% bound direction between 0 and 360 ??
17+
% dots.direction
18+
% dots.isSignal : signal dots (1) and those are noise dots (0)
19+
% dots.directionAllDots
20+
% dots.lifeTime : in frames
21+
% dots.speeds : [ndots, 2] ; horizontal and vertical speed ; in pixels per
22+
% frame
23+
% dots.speedPixPerFrame
2024

2125
dots.direction = thisEvent.direction(1);
2226

src/dot/setDotDirection.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
function dots = setDotDirection(cfg, dots)
2+
% dots = setDotDirection(cfg, dots)
3+
%
4+
% creates some new direction for the dots
5+
%
6+
% coherent dots have a true value in the vector dots.isSignal and get
7+
% assigned a value equals to the one in dots.direction
8+
%
9+
% all the other dots get a random value between 0 and 360.
10+
%
11+
% all directions are in end expressed between 0 and 360
212

313
directionAllDots = nan(cfg.dot.number, 1);
414

515
% Coherent dots
16+
17+
if numel(dots.direction) == 1
18+
dots.direction = ones(sum(dots.isSignal), 1) * dots.direction;
19+
elseif numel(dots.direction) ~= sum(dots.isSignal)
20+
error(['dots.direction must have one element' ...
21+
'or as many element as there are coherent dots'])
22+
end
23+
624
directionAllDots(dots.isSignal) = dots.direction;
725

826
if strcmp(cfg.design.motionType, 'radial')
@@ -11,6 +29,7 @@
1129
end
1230

1331
% Random direction for the non coherent dots
32+
1433
directionAllDots(~dots.isSignal) = rand(sum(~dots.isSignal), 1) * 360;
1534
directionAllDots = rem(directionAllDots, 360);
1635

tests/test_initDots.m

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
end
88

99
function test_initDotsBasic()
10-
10+
1111
%% set up
12-
12+
1313
% % Dot life time in seconds
1414
% cfg.dot.lifeTime
1515
% % Number of dots
@@ -21,7 +21,7 @@ function test_initDotsBasic()
2121
% thisEvent.direction
2222
% % Speed expressed in pixels per frame
2323
% thisEvent.speed
24-
24+
2525
cfg.design.motionType = 'translation';
2626
cfg.dot.number = 10;
2727
cfg.dot.coherence = 1; % proportion
@@ -30,34 +30,37 @@ function test_initDotsBasic()
3030
cfg.screen.winWidth = 2000; % in pixels
3131
cfg.timing.eventDuration = 1; % in seconds
3232
cfg.screen.ifi = 0.01; % in seconds
33-
33+
3434
thisEvent.direction = 0;
3535
thisEvent.speed = 10;
36-
37-
% [dots] = initDots(cfg, thisEvent);
38-
36+
37+
[dots] = initDots(cfg, thisEvent);
38+
3939
%% Undeterministic ouput
40-
% assertTrue(all(dots.positions(:) >= 0));
41-
% assertTrue(all(dots.positions(:) <= 2000));
42-
% assertTrue(all(dots.time(:) >= 0));
43-
% assertTrue(all(dots.time(:) <= 1 / 0.01));
44-
40+
assertTrue(all(dots.positions(:) >= 0));
41+
assertTrue(all(dots.positions(:) <= 2000));
42+
assertTrue(all(dots.time(:) >= 0));
43+
assertTrue(all(dots.time(:) <= 1 / 0.01));
44+
4545
%% Deterministic output : data to test against
4646
expectedStructure.lifeTime = 25;
4747
expectedStructure.isSignal = ones(10, 1);
4848
expectedStructure.speeds = repmat([1 0], 10, 1) * 10;
49-
49+
expectedStructure.speedPixPerFrame = 10;
50+
expectedStructure.direction = zeros(10, 1);
51+
expectedStructure.directionAllDots = zeros(10, 1);
52+
5053
% remove undeterministic output
51-
% dots = rmfield(dots, 'time');
52-
% dots = rmfield(dots, 'positions');
53-
54+
dots = rmfield(dots, 'time');
55+
dots = rmfield(dots, 'positions');
56+
5457
%% test
55-
% assertEqual(expectedStructure, dots);
56-
58+
assertEqual(expectedStructure, dots);
59+
5760
end
5861

5962
function test_initDotsStatic()
60-
63+
6164
cfg.design.motionType = 'translation';
6265
cfg.dot.number = 10;
6366
cfg.dot.coherence = 1; % proportion
@@ -66,28 +69,31 @@ function test_initDotsStatic()
6669
cfg.screen.winWidth = 2000; % in pixels
6770
cfg.timing.eventDuration = 1; % in seconds
6871
cfg.screen.ifi = 0.01; % in seconds
69-
72+
7073
thisEvent.direction = -1;
7174
thisEvent.speed = 10;
72-
73-
% [dots] = initDots(cfg, thisEvent);
74-
75+
76+
[dots] = initDots(cfg, thisEvent);
77+
7578
% remove undeterministic output
76-
% dots = rmfield(dots, 'time');
77-
% dots = rmfield(dots, 'positions');
78-
79+
dots = rmfield(dots, 'time');
80+
dots = rmfield(dots, 'positions');
81+
7982
%% data to test against
8083
expectedStructure.lifeTime = Inf;
8184
expectedStructure.isSignal = ones(10, 1);
8285
expectedStructure.speeds = zeros(10, 2);
83-
86+
expectedStructure.speedPixPerFrame = 0;
87+
expectedStructure.direction = -1 * ones(10, 1);
88+
expectedStructure.directionAllDots = -1 * ones(10, 1);
89+
8490
%% test
85-
% assertEqual(expectedStructure, dots);
86-
91+
assertEqual(expectedStructure, dots);
92+
8793
end
8894

8995
function test_initDotsRadial()
90-
96+
9197
cfg.design.motionType = 'radial';
9298
cfg.dot.number = 10;
9399
cfg.dot.coherence = 1; % proportion
@@ -96,20 +102,20 @@ function test_initDotsRadial()
96102
cfg.screen.winWidth = 2000; % in pixels
97103
cfg.timing.eventDuration = 1; % in seconds
98104
cfg.screen.ifi = 0.01; % in seconds
99-
105+
100106
thisEvent.direction = 666; % outward motion
101107
thisEvent.speed = 10;
102-
103-
% [dots] = initDots(cfg, thisEvent);
104-
108+
109+
[dots] = initDots(cfg, thisEvent);
110+
105111
%% data to test against
106-
% XY = dots.positions - 2000 / 2;
107-
% angle = cart2pol(XY(:, 1), XY(:, 2));
108-
% angle = angle / pi * 180;
109-
% [horVector, vertVector] = decomposeMotion(angle);
110-
% speeds = [horVector, vertVector] * 10;
111-
112+
XY = dots.positions - 2000 / 2;
113+
angle = cart2pol(XY(:, 1), XY(:, 2));
114+
angle = angle / pi * 180;
115+
[horVector, vertVector] = decomposeMotion(angle);
116+
speeds = [horVector, vertVector] * 10;
117+
112118
%% test
113-
% assertEqual(speeds, dots.speeds);
114-
119+
% assertEqual(speeds, dots.speeds);
120+
115121
end

tests/test_reseedDots.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@
77
end
88

99
function test_reseedDotsBasic()
10-
10+
1111
cfg.screen.winWidth = 2000;
12-
12+
1313
cfg.design.motionType = 'radial';
14-
14+
1515
cfg.dot.matrixWidth = 50; % in pixels
1616
cfg.dot.number = 5;
1717
cfg.dot.sizePix = 20;
1818
cfg.dot.proportionKilledPerFrame = 0;
19-
19+
2020
cfg.fixation.widthPix = 20;
21-
21+
2222
dots.lifeTime = 100;
2323
dots.speedPixPerFrame = 3;
2424
dots.direction = 90;
2525
dots.isSignal = true(5, 1);
26-
26+
2727
dots.positions = [ ...
2828
49, 1; % OK
2929
490, 2043; % out of frame
3030
-104, 392; % out of frame
3131
492, 402; % OK
3232
1000, 1000; % on the fixation cross
3333
];
34-
34+
3535
dots.time = [ ...
3636
6; ... OK
3737
4; ... OK
3838
56; ... OK
3939
300; ... % exceeded its life time
4040
50]; % OK
41-
42-
% dots = reseedDots(dots, cfg);
43-
41+
42+
dots = reseedDots(dots, cfg);
43+
4444
reseeded = [ ...
4545
6;
4646
1;
4747
1;
4848
1;
4949
1];
50-
51-
% assertEqual(reseeded, dots.time);
52-
50+
51+
assertEqual(reseeded, dots.time);
52+
5353
end

tests/test_setDotDirection.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function test_suite = test_setDotDirection %#ok<*STOUT>
2+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
3+
test_functions = localfunctions(); %#ok<*NASGU>
4+
catch % no problem; early Matlab versions can use initTestSuite fine
5+
end
6+
initTestSuite;
7+
end
8+
9+
function test_setDotDirectionBasic()
10+
% create 5 coherent dots with direction == 362 (that should give 2 in the
11+
% end)
12+
% also creates 955 additonal dots with random direction between 0 and 360
13+
14+
cfg.dot.number = 1000;
15+
cfg.design.motionType = 'translation';
16+
dots.direction = 362;
17+
18+
dots.isSignal = [true(5,1) ; false(1000 - 5,1)];
19+
20+
dots = setDotDirection(cfg, dots);
21+
22+
assertTrue(all(dots.directionAllDots(1:5) == 2 * ones(5,1)));
23+
assertTrue(all(dots.directionAllDots >= 0 ));
24+
assertTrue(all(dots.directionAllDots <= 360 ));
25+
26+
end

0 commit comments

Comments
 (0)