1+ classdef test_tools < matlab .unittest .TestCase
2+
3+ methods (Test )
4+ function reduceDim(testCase )
5+ testCase .verifyError(@()reduce_dim(), ' reduce_dim:notEnoughInput' );
6+
7+ % Valid Input
8+ Nd_1d = [1 , 2 , 3 , 4 ];
9+ expected_output_1d = Nd_1d ;
10+ assert(isequal(reduce_dim(Nd_1d , 1 ), expected_output_1d ),' Test failed: unexpected output' );
11+
12+ Nd_1d = [1 , 2 , 1 ; 2 , 3 , 1 ];
13+ expected_output_1d = [3 , 6 , 1 ];
14+ assert(isequal(reduce_dim(Nd_1d , 1 ), expected_output_1d ),' Test failed: unexpected output' );
15+ end
16+ function testNonPoissonSpikeGen(testCase )
17+ testCase .verifyError(@()non_poisson_spike_gen(), ' nonpoissonspikegen:notEnoughInput' );
18+
19+ % Valid Input
20+ time = 0 : 0.1 : 12 ;
21+ rate = 6 ;
22+ noise_prob = 0 ;
23+ spikes = non_poisson_spike_gen(time , rate , noise_prob );
24+ assert(length(spikes ) == length(time ), ' Test failed: Incorrect length of spikes' );
25+ assert(all(ismember(spikes , [0 , 1 ])), ' Test failed: spikes should contain only 0s and 1s' );
26+
27+ time = 0 : 0.1 : 10 ;
28+ rate = 8 ;
29+ noise_prob = 0.2 ;
30+ spikes = non_poisson_spike_gen(time , rate , noise_prob );
31+ assert(length(spikes ) == length(time ), ' Test failed: Incorrect length of spikes' );
32+ assert(all(ismember(spikes , [0 , 1 ])), ' Test failed: spikes should contain only 0s and 1s' );
33+
34+ % Invalid Input
35+ time = 0 ;
36+ rate = 6 ;
37+ noise_prob = 0 ;
38+ testCase .verifyError(@()non_poisson_spike_gen(time , rate , noise_prob ), ' MATLAB:badsubscript' );
39+
40+ time = NaN ;
41+ testCase .verifyError(@()non_poisson_spike_gen(time , rate , noise_prob ), ' nonpoissonspikegen:NaNInput' );
42+ end
43+
44+ function testpoissonSpikeGen(testCase )
45+ testCase .verifyError(@()poisson_spike_gen(), ' poissonspikegen:notEnoughInput' );
46+
47+ % Valid Input
48+ time = 0 : 0.1 : 12 ;
49+ rate = 6 ;
50+ noise_prob = 0 ;
51+ spikes = poisson_spike_gen(time , rate , noise_prob );
52+ assert(length(spikes ) == length(time ), ' Test failed: Incorrect length of spikes' );
53+ assert(all(ismember(spikes , [0 , 1 ])), ' Test failed: spikes should contain only 0s and 1s' );
54+
55+ time = 0 : 0.1 : 10 ;
56+ rate = sin(time );
57+ noise_prob = 0 ;
58+ spikes = poisson_spike_gen(time , rate , noise_prob );
59+ assert(length(spikes ) == length(time ), ' Test failed: Incorrect length of spikes' );
60+ assert(all(ismember(spikes , [0 , 1 ])), ' Test failed: spikes should contain only 0s and 1s' );
61+
62+ time = 0 : 0.1 : 10 ;
63+ rate = 8 ;
64+ noise_prob = 0.2 ;
65+ spikes = poisson_spike_gen(time , rate , noise_prob );
66+ assert(length(spikes ) == length(time ), ' Test failed: Incorrect length of spikes' );
67+ assert(all(ismember(spikes , [0 , 1 ])), ' Test failed: spikes should contain only 0s and 1s' );
68+
69+ % Invalid input
70+ time = 0 ;
71+ rate = 6 ;
72+ noise_prob = 0 ;
73+ testCase .verifyError(@()poisson_spike_gen(time , rate , noise_prob ), ' MATLAB:badsubscript' );
74+
75+ time = NaN ;
76+ testCase .verifyError(@()poisson_spike_gen(time , rate , noise_prob ), ' poissonspikegen:NaNInput' );
77+ end
78+
79+
80+ function testShuffle(testCase )
81+ testCase .verifyError(@()shuffle_core(), ' shuffle_core:notEnoughInput' );
82+
83+ % Valid Input
84+ rng(' default' );
85+ behav_data = randi([1 , 10 ], 100 , 1 );
86+ neural_data = rand(100 , 10 );
87+ consistency = 0 ;
88+ index = [1 , 0 ];
89+ shuffled_data = shuffle_core(behav_data , neural_data , consistency , index );
90+ assert(~isequal(neural_data , shuffled_data ), ' Data should be shuffled' );
91+ nbins = 10 ;
92+ [counts_neural , ~ ] = histcounts(neural_data(: ,1 ), nbins );
93+ [counts_shuffled , ~ ] = histcounts(shuffled_data(: ,1 ), nbins );
94+ assert(isequal(counts_neural , counts_shuffled ));
95+ uniqueBehav = unique(behav_data );
96+ for i = 1 : length(uniqueBehav )
97+ currentBehav = uniqueBehav(i );
98+ idx = behav_data == currentBehav ;
99+ currentNeuralData = neural_data(idx , : );
100+ currentShuffledData = shuffled_data(idx , : );
101+ for object_idx = 1 : size(neural_data , 2 )
102+ [counts_neural , ~ ] = histcounts(currentNeuralData(: ,object_idx ), nbins );
103+ [counts_shuffled , ~ ] = histcounts(currentShuffledData(: ,object_idx ), nbins );
104+ assert(~isequal(counts_neural , counts_shuffled ), ' Histograms of the first dimension should differ.' );
105+ end
106+ end
107+
108+ consistency = 1 ;
109+ shuffled_data = shuffle_core(behav_data , neural_data , consistency , index );
110+ assert(~isequal(neural_data , shuffled_data ), ' Data should be shuffled' );
111+ nbins = 10 ;
112+ [counts_neural , ~ ] = histcounts(neural_data(: ,1 ), nbins );
113+ [counts_shuffled , ~ ] = histcounts(shuffled_data(: ,1 ), nbins );
114+ assert(isequal(counts_neural , counts_shuffled ));
115+ uniqueBehav = unique(behav_data );
116+ for i = 1 : length(uniqueBehav )
117+ currentBehav = uniqueBehav(i );
118+ idx = behav_data == currentBehav ;
119+ currentNeuralData = neural_data(idx , : );
120+ currentShuffledData = shuffled_data(idx , : );
121+ for object_idx = 1 : size(neural_data , 2 )
122+ [counts_neural , ~ ] = histcounts(currentNeuralData(: ,object_idx ), nbins );
123+ [counts_shuffled , ~ ] = histcounts(currentShuffledData(: ,object_idx ), nbins );
124+ assert(isequal(counts_neural , counts_shuffled ), ' Shuffling should be consistent across behavData.' );
125+ end
126+ end
127+
128+ % Test Warning
129+ lastwarn(' ' );
130+ disp(' Dont worry, this warning is part of the test ;)' );
131+ behav_data = randi([1 , 10 ], 100 );
132+ neural_data = rand(100 , 10 );
133+ consistency = 0 ;
134+ index = [1 , 1 ];
135+ shuffle_core(behav_data , neural_data , consistency , index );
136+ [warnMsg , warnId ] = lastwarn ;
137+ expectedWarningMsg = ' Shuffling the second dimension (neuralObjects) may alter probability distributions of individual objects.' ;
138+ assert(contains(warnMsg , expectedWarningMsg ), ' Expected warning message was not issued.' );
139+ end
140+ end
141+ end
0 commit comments