MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
testSynapse.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2013 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #ifdef DO_UNIT_TESTS
11 
12 #include <queue>
13 #include "header.h"
14 #include "Synapse.h"
15 #include "SynEvent.h"
16 #include "SynHandlerBase.h"
17 #include "SimpleSynHandler.h"
18 #include "RollingMatrix.h"
19 #include "SeqSynHandler.h"
20 #include "../shell/Shell.h"
21 
22 double doCorrel( RollingMatrix& rm, vector< vector< double >> & kernel )
23 {
24  int nr = kernel.size();
25  vector< double > correlVec( nr, 0.0 );
26  for ( int i = 0; i < nr; ++i )
27  rm.correl( correlVec, kernel[i], i );
28  double seqActivation = 0.0;
29  for ( int i = 0; i < nr; ++i )
30  seqActivation += correlVec[i];
31  return seqActivation;
32 }
33 
34 void testRollingMatrix2()
35 {
36  int nr = 5;
37  RollingMatrix rm;
38  rm.resize( nr, nr );
39  vector< vector< double > > kernel( nr );
40  for ( int i = 0; i < nr; ++i ) {
41  kernel[i].resize( nr, 0.0 );
42  rm.zeroOutRow( i );
43  for ( int j = 0; j < nr; ++j ) {
44  kernel[i][j] = 16 - (i-j)*(i-j); // symmetric, forward
45  rm.sumIntoEntry( (i==j), i, j );
46  }
47  }
48  double ret1 = doCorrel( rm, kernel );
49 
50  for ( int i = 0; i < nr; ++i ) {
51  kernel[i].clear();
52  kernel[i].resize( nr, 0.0 );
53  rm.zeroOutRow( i );
54  for ( int j = 0; j < nr; ++j ) {
55  int k = nr-i-1;
56  kernel[i][j] = 16 - (k-j)*(k-j); // symmetric, backwards
57  rm.sumIntoEntry( (k==j), i, j );
58  }
59  }
60  double ret2 = doCorrel( rm, kernel );
61  assert( doubleEq( ret1, ret2 ) );
62 }
63 
64 void testRollingMatrix()
65 {
66  int nr = 5;
67  int ncol = 10;
68  RollingMatrix rm;
69  rm.resize( 5, 10 );
70 
71  for ( int i = 0; i < nr; ++i ) {
72  rm.sumIntoEntry( i + 1, i, i );
73  }
74  for ( int i = 0; i < nr; ++i ) {
75  for ( int j = 0; j < ncol; ++j ) {
76  assert( rm.get( i, j ) == ( i == j ) * (i+1) );
77  }
78  }
79  cout << "." << flush;
80 
81  // Old row0 becomes row1 and so on. Old row4 (now 0) should be cleared.
82  rm.rollToNextRow();
83  for ( int i = 0; i < nr; ++i ) {
84  for ( int j = 0; j < ncol; ++j ) {
85  // cout << rm.get( i, j );
86  assert( rm.get( i, j ) == ( i == j+1 ) * i );
87  }
88  // Here are the entries in the rm.rows_ matrix
89  // 000000000
90  // 100000000
91  // 020000000
92  // 003000000
93  // 000400000
94  }
95  cout << "." << flush;
96 
97  vector< double > input( 10, 0.0 );
98  for ( int i = 0; i < nr; ++i )
99  input[i] = i + 1;
100 
101  assert( doubleEq( rm.dotProduct( input, 0, 5 ), 0.0 ) );
102  assert( doubleEq( rm.dotProduct( input, 1, 5 ), 1.0 ) );
103  assert( doubleEq( rm.dotProduct( input, 2, 5 ), 4.0 ) );
104  assert( doubleEq( rm.dotProduct( input, 3, 5 ), 9.0 ) );
105  assert( doubleEq( rm.dotProduct( input, 4, 5 ), 16.0 ) );
106  assert( doubleEq( rm.dotProduct( input, 4, 6 ), 12.0 ) );
107  assert( doubleEq( rm.dotProduct( input, 4, 7 ), 8.0 ) );
108  assert( doubleEq( rm.dotProduct( input, 4, 8 ), 4.0 ) );
109  assert( doubleEq( rm.dotProduct( input, 4, 9 ), 0.0 ) );
110 
111  // Note that the input and the row are aligned at col=5, the
112  // middle of the input vector.
113  rm.sumIntoRow( input, 0 ); // input == [1234500000]
114  vector< double > corr;
115  rm.correl( corr, input, 4 ); // rm[4] == [00040000]
116  assert( doubleEq( corr[0], 0.0 ) );
117  assert( doubleEq( corr[1], 0.0 ) );
118  assert( doubleEq( corr[2], 0.0 ) );
119  assert( doubleEq( corr[3], 0.0 ) );
120  assert( doubleEq( corr[4], 20.0 ) );
121  assert( doubleEq( corr[5], 16.0 ) );
122  assert( doubleEq( corr[6], 12.0 ) );
123  assert( doubleEq( corr[7], 8.0 ) );
124  assert( doubleEq( corr[8], 4.0 ) );
125  assert( doubleEq( corr[9], 0.0 ) );
126 
127  corr.assign( corr.size(), 0 );
128  rm.correl( corr, input, 0 ); // rm[0] == [1234500000]
129  assert( doubleEq( corr[0], 0.0 ) );
130  assert( doubleEq( corr[1], 5.0 ) );
131  assert( doubleEq( corr[2], 14.0 ) );
132  assert( doubleEq( corr[3], 26.0 ) );
133  assert( doubleEq( corr[4], 40.0 ) );
134  assert( doubleEq( corr[5], 55.0 ) );
135  assert( doubleEq( corr[6], 40.0 ) );
136  assert( doubleEq( corr[7], 26.0 ) );
137  assert( doubleEq( corr[8], 14.0 ) );
138  assert( doubleEq( corr[9], 5.0 ) );
139 
140  cout << "." << flush;
141 }
142 
143 // FIXME: This test is failing on travis.
144 void testSeqSynapse()
145 {
146  int numSyn = 10;
147  int kernelWidth = 5;
148  SeqSynHandler ssh;
149  ssh.vSetNumSynapses( numSyn );
150 
151  // for ( int i = 0; i < numSyn; ++i )
152  // ssh.addSynapse();
153 
154  assert( static_cast< int >( ssh.vGetNumSynapses() ) == numSyn );
155  ssh.setSeqDt( 1.0 );
156  ssh.setHistoryTime( 5.0 );
157  ssh.setKernelWidth( kernelWidth );
158  ssh.setKernelEquation( "(x == t)*5 + ((x+1)==t || (x-1)==t) * 2 - 1" );
159 
160  vector< double > ret = ssh.getKernel();
161  assert( ret.size() == static_cast< unsigned int > (5 * kernelWidth ) );
162  vector< double >::iterator k = ret.begin();
163  for ( int t = 0; t < 5; ++t ) {
164  for ( int x = 0; x < kernelWidth; ++x ) {
165  double val = (x == t)*5 + ((x+1)==t || (x-1)==t) * 2 - 1;
166  assert( doubleEq( *k++, val ) );
167  }
168  }
169 
170  cout << "." << flush;
171 
172  ssh.setBaseScale( 1.0 );
173  ssh.setSequenceScale( 1.0 );
174  for ( int i = 0; i < numSyn; ++i ) {
175  ssh.addSpike( i, 0.0, 1.0 );
176  }
177  ssh.setPlasticityScale( 1.0 );
178  ProcInfo p;
179 
180  Eref sheller( Id().eref() );
181  Shell* shell = reinterpret_cast< Shell* >( sheller.data() );
182  Id sid = shell->doCreate( "SeqSynHandler", Id(), "sid", 1 );
183  assert( sid.element()->getName() == "sid" );
184  ssh.vProcess( sid.eref(), &p );
185 
186  // Here we correlate the vector [1,1,1,1,1,1,1,1,1,1,1] with
187  // the kernel [4,1,-1,-1,-1]
188  // Other lines are zeros.
189  assert( doubleEq( ssh.getSeqActivation(), 14.0 ) );
190  vector< double > wts = ssh.getWeightScaleVec();
191  for ( int i = 2; i < numSyn-2; ++i )
192  assert( doubleEq( wts[i], 2.0 ) );
193  assert( doubleEq( wts[0], -3 ) ); // Edge effects.
194  assert( doubleEq( wts[1], -2 ) ); // Edge effects.
195  assert( doubleEq( wts[8], 3 ) ); // Edge effects.
196  assert( doubleEq( wts[9], 4 ) ); // Edge effects.
197 
198  cout << "." << flush;
199  shell->doDelete( sid );
200 }
201 
202 #endif // DO_UNIT_TESTS
203 
204 // This tests stuff without using the messaging.
206 {
207 #ifdef DO_UNIT_TESTS
208  testRollingMatrix();
209  testRollingMatrix2();
210  testSeqSynapse();
211 #endif // DO_UNIT_TESTS
212 }
213 
214 // This is applicable to tests that use the messaging and scheduling.
216 {
217 }
218 
vector< double > getKernel() const
void resize(unsigned int numRows, unsigned int numColumns)
double dotProduct(const vector< double > &input, unsigned int row, unsigned int startColumn) const
void sumIntoRow(const vector< double > &input, unsigned int row)
Element * element() const
Synonym for Id::operator()()
Definition: Id.cpp:113
vector< double > getWeightScaleVec() const
void setPlasticityScale(double v)
void vProcess(const Eref &e, ProcPtr p)
void addSpike(unsigned int index, double time, double weight)
void setKernelEquation(string eq)
Eref eref() const
Definition: Id.cpp:125
Id doCreate(string type, ObjId parent, string name, unsigned int numData, NodePolicy nodePolicy=MooseBlockBalance, unsigned int preferredNode=1)
Definition: Shell.cpp:181
void zeroOutRow(unsigned int row)
void setKernelWidth(unsigned int v)
bool doubleEq(double x, double y)
Definition: doubleEq.cpp:16
void setSeqDt(double v)
void testSynapseProcess()
void vSetNumSynapses(unsigned int num)
double getSeqActivation() const
void sumIntoEntry(double input, unsigned int row, unsigned int column)
Definition: Eref.h:26
void correl(vector< double > &ret, const vector< double > &input, unsigned int row) const
unsigned int vGetNumSynapses() const
bool doDelete(ObjId oid)
Definition: Shell.cpp:259
Definition: Id.h:17
void setBaseScale(double v)
void setHistoryTime(double v)
double get(unsigned int row, unsigned int column) const
void testSynapse()
const string & getName() const
Definition: Element.cpp:56
void setSequenceScale(double v)
Definition: Shell.h:43