VecConTest.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #ifndef VECCONTEST_H
  2. #define VECCONTEST_H
  3. #include <cxxtest/TestSuite.h>
  4. #include "debug.hpp"
  5. #include "simloop.hpp"
  6. #include "vconnection.hpp"
  7. #include "layer.hpp"
  8. #include "chunkfile.h"
  9. #include <sys/stat.h>
  10. using namespace std;
  11. class VecConTest : public CxxTest::TestSuite
  12. {
  13. public:
  14. VecConTest(): mTempDirWasCreated(false), mTempDir("gagadir"), mL1(NULL), mL2(NULL){}
  15. void testMakeTempDir()
  16. {
  17. makeTempDir();
  18. }
  19. void setUp()
  20. {
  21. makeTempDir();
  22. setupSTDim();
  23. }
  24. void tearDown()
  25. {
  26. if (mL1) {
  27. delete mL1;
  28. mL1 = NULL;
  29. }
  30. if (mL2) {
  31. delete mL2;
  32. mL2 = NULL;
  33. }
  34. }
  35. void xtestTurnOnDebug()
  36. {
  37. // Debug( dc::con.on() );
  38. // Debug( dc::chunk.on() );
  39. // Debug( libcw_do.on() );
  40. }
  41. void testSaveWeightsAndReadHeader()
  42. {
  43. saveWeightFileOldAndNew("veccon_v2_0.weights");
  44. ChunkFileReader MyReader(mTempDir+"/" + mFileName, mVecFileFormat);
  45. SourceTargetDim VecConHeader;
  46. MyReader.read(VecConHeader);
  47. TS_ASSERT_EQUALS(mSTDim_Saved.stringify(),VecConHeader.stringify());
  48. }
  49. void testLoadWeightFile()
  50. {
  51. saveWeightFileOldAndNew("veccon_v2_0.weights");
  52. VecConnection MyVecCon;
  53. MyVecCon.Load_VecConnection_2_0(mTempDir+"/" + mFileName);
  54. SourceTargetDim STDim_Loaded = MyVecCon.GetSourceTargetDim();
  55. TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
  56. }
  57. void testCompareVecConnections()
  58. {
  59. VecConnection Con1;
  60. VecConnection Con2;
  61. TS_ASSERT(Con1.hasEqualSynapses(Con2));
  62. }
  63. void testCompareVecDiffers()
  64. {
  65. VecConnection Con1;
  66. setupSTDim();
  67. VecConnection* Con3 = createVecConnection(mSTDim_Saved);
  68. Con3->ConnectSelf(0.3);
  69. VecConnection* Con4 = createVecConnection(mSTDim_Saved);
  70. Con4->ConnectSelf(0.5);
  71. TS_ASSERT(!Con1.hasEqualSynapses(*Con3));
  72. TS_ASSERT(!(Con4->hasEqualSynapses(*Con3)));
  73. delete Con3;
  74. delete Con4;
  75. }
  76. void testCompareVecDelay()
  77. {
  78. setupSTDim();
  79. VecConnection* Con1 = createVecConnection(mSTDim_Saved);
  80. Con1->ConnectSelf(0.3, 5,2);
  81. VecConnection* Con2 = createVecConnection(mSTDim_Saved);
  82. Con2->ConnectSelf(0.3, 10, 9.75);
  83. VecConnection* Con3 = createVecConnection(mSTDim_Saved);
  84. Con3->ConnectSelf(0.3, 10, 9.75);
  85. TS_ASSERT(!Con1->hasEqualSynapses(*Con2));
  86. TS_ASSERT(Con3->hasEqualSynapses(*Con2));
  87. delete Con1;
  88. delete Con2;
  89. delete Con3;
  90. }
  91. void testWriteAndLoadVecCon()
  92. {
  93. string FileName=mTempDir+"/VecCon_v2_0.weights";
  94. setupSTDim();
  95. VecConnection* Con1 = createVecConnection(mSTDim_Saved);
  96. Con1->ConnectSelf(0.3, 5,2);
  97. Con1->Save_VecConnection_2_0(FileName);
  98. VecConnection LoadVecCon;
  99. LoadVecCon.Load_VecConnection_2_0(FileName);
  100. SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
  101. TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
  102. TS_ASSERT(Con1->hasEqualSynapses(LoadVecCon));
  103. delete Con1;
  104. }
  105. void testLoadVecConViaLoad()
  106. {
  107. string FileName="VecCon_v2_0.weights";
  108. setupSTDim();
  109. VecConnection* Con1 = createVecConnection(mSTDim_Saved);
  110. Con1->ConnectSelf(0.3, 5,2);
  111. Con1->Save_VecConnection_2_0(mTempDir+"/"+FileName);
  112. VecConnection LoadVecCon;
  113. LoadVecCon.Load(FileName);
  114. SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
  115. TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
  116. TS_ASSERT(Con1->hasEqualSynapses(LoadVecCon));
  117. delete Con1;
  118. }
  119. void testReadFileFormat()
  120. {
  121. saveWeightFileOldAndNew("veccon_v2_0.weights");
  122. FileFormat MyFileFormat = readFileFormat(mTempDir+"/"+mFileName);
  123. TS_ASSERT_EQUALS(MyFileFormat.print(), mVecFileFormat.print());
  124. }
  125. void testSaveAndLoadOldFileFormat()
  126. {
  127. VecConnection LoadVecCon;
  128. if (!LoadVecCon.compiledWithMemsave()) {
  129. setupSTDim();
  130. VecConnection* MyVecCon=createAnd_SaveOldFileFormat(mSTDim_Saved);
  131. LoadVecCon.Load(mFileName);
  132. SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
  133. TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
  134. TS_ASSERT(MyVecCon->hasEqualSynapses(LoadVecCon));
  135. delete MyVecCon;
  136. }
  137. }
  138. void testIfCompiledWithMemsave()
  139. {
  140. VecConnection MyVecCon;
  141. if (compiledWithLowMemConfig()) {
  142. TS_ASSERT (MyVecCon.compiledWithMemsave());
  143. } else {
  144. TS_ASSERT (!MyVecCon.compiledWithMemsave());
  145. }
  146. }
  147. void testExceptionThrownWhenLoadOldFormatWithMemsave()
  148. {
  149. GetGlobalSimLoop()->SetDataDirectory_(".");
  150. string FileAndDirName = "testdata/VecConnection_1.0_weights";
  151. VecConnection LoadVecCon;
  152. if (LoadVecCon.compiledWithMemsave()) {
  153. TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), NotSupportedWithMemsave);
  154. } else {
  155. TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
  156. }
  157. }
  158. void testExceptionThrownWhenSaveOldFormatWithMemsave()
  159. {
  160. VecConnection* MyVecCon = createVecConnectionGaussian(mSTDim_Saved);
  161. if (MyVecCon->compiledWithMemsave()) {
  162. TS_ASSERT_THROWS( MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName), NotSupportedWithMemsave );
  163. } else {
  164. TS_ASSERT_THROWS_NOTHING( MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName) );
  165. }
  166. delete MyVecCon;
  167. }
  168. void xtestSaveBigWeightFile()
  169. {
  170. setupSTDim(400,20,20, 400,20,20);
  171. saveWeightFileOldAndNew("BigWeightFile.weights", 0.06);
  172. }
  173. void testLoadMemsaveFileInNonMemsaveMode()
  174. {
  175. GetGlobalSimLoop()->SetDataDirectory_(".");
  176. string FileAndDirName = "testdata/VecCon_v2_0_memsave.weights";
  177. VecConnection LoadVecCon;
  178. TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
  179. }
  180. void xtestWriteWeightFileLongDelays()
  181. {
  182. setupSTDim(16,4,4,16,4,4);
  183. float MaxDelay= ( DMAX -1 ) * 0.25;
  184. #ifndef DELAYS_CHARs
  185. saveWeightFileOldAndNew("LongDelay.weights", 0.4, MaxDelay);
  186. #endif
  187. }
  188. void testCheckExceptionIfLoadMemsaveWithTooLongDelays()
  189. {
  190. GetGlobalSimLoop()->SetDataDirectory_(".");
  191. string FileAndDirName = "testdata/LongDelay.weights";
  192. VecConnection LoadVecCon;
  193. if (LoadVecCon.compiledWithMemsave()) {
  194. TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), DataLossAtVectorCast);
  195. } else {
  196. TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), RequestedDelayTooLarge);
  197. }
  198. }
  199. void testLayerNNeuronsTooLarge()
  200. {
  201. // DecoLifLayer L1(NNEURONS_MAX+1);
  202. }
  203. void xtestWriteWeightFileManyNeurons()
  204. {
  205. setupSTDim(40000,200,200,16,4,4);
  206. float MaxDelay=130;
  207. #ifndef NNEURONS_SHORT
  208. saveWeightFileOldAndNew("ManyNeurons.weights", 0.4);
  209. #endif
  210. }
  211. void testLoadWeightFileManyNeurons()
  212. {
  213. GetGlobalSimLoop()->SetDataDirectory_(".");
  214. string FileAndDirName = "testdata/ManyNeurons.weights";
  215. VecConnection LoadVecCon;
  216. if (LoadVecCon.compiledWithMemsave()) {
  217. TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), DataLossAtVectorCast);
  218. } else {
  219. TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
  220. }
  221. }
  222. void xtestCleanUp()
  223. {
  224. if (mTempDirWasCreated) {
  225. system((string("rm -rf ") + mTempDir).c_str());
  226. }
  227. }
  228. private:
  229. SourceTargetDim mSTDim_Saved;
  230. string mFileName;
  231. FileFormat mVecFileFormat;
  232. string mTempDir_Memsave;
  233. string mTempDir_NoMemsave;
  234. string mTempDir;
  235. bool mTempDirWasCreated;
  236. DecoLifLayer* mL1;
  237. DecoLifLayer* mL2;
  238. void saveWeightFileOldAndNew(const string& FileName, float sigma=0.15, float MaxDelay=10)
  239. {
  240. mFileName = FileName;
  241. mVecFileFormat = FileFormat("VecConnection", 2, 0);
  242. allocateLayers(mSTDim_Saved);
  243. VecConnection MyVecCon(mL1,mL2);
  244. MyVecCon.ConnectGaussian(sigma, 10, MaxDelay);
  245. if (!MyVecCon.compiledWithMemsave()) {
  246. MyVecCon.Save_VecConnection_1_0(mTempDir+"/SavedVecCon_1.0_weights");
  247. }
  248. MyVecCon.Save(mFileName.c_str());
  249. }
  250. void setupSTDim(int NS, int NSx, int NSy, int NT, int NTx, int NTy)
  251. {
  252. mSTDim_Saved = SourceTargetDim(NS, NSx, NSy, NT, NTx, NTy);
  253. }
  254. void setupSTDim()
  255. {
  256. int NSx=6;
  257. int NSy=7;
  258. int NTx=7;
  259. int NTy=5;
  260. int NS = NSx*NSy;
  261. int NT = NTx*NTy;
  262. mSTDim_Saved = SourceTargetDim(NS, NSx, NSy, NT, NTx, NTy);
  263. }
  264. VecConnection* createAnd_SaveOldFileFormat(const SourceTargetDim& STDim)
  265. {
  266. return createAnd_SaveOldFileFormat("VecCon_OldFormat_weights", STDim);
  267. }
  268. VecConnection* createAnd_SaveOldFileFormat(const string& FileName, const SourceTargetDim& STDim)
  269. {
  270. mFileName = FileName;
  271. VecConnection* MyVecCon = createVecConnection(STDim);
  272. MyVecCon->ConnectGaussian(0.3, 10);
  273. MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName);
  274. return MyVecCon;
  275. }
  276. void allocateLayers(const SourceTargetDim& STDim)
  277. {
  278. mL1 = new DecoLifLayer(STDim.NSource);
  279. mL2 = new DecoLifLayer(STDim.NTarget);
  280. mL1->SetupPositions(STDim.NSx, STDim.NSy,true);
  281. mL2->SetupPositions(STDim.NTx, STDim.NTy,true);
  282. }
  283. VecConnection* createVecConnectionGaussian(const SourceTargetDim& STDim)
  284. {
  285. allocateLayers(STDim);
  286. VecConnection* MyVecCon = new VecConnection(mL1,mL2);
  287. MyVecCon->ConnectGaussian(0.3, 10);
  288. return MyVecCon;
  289. }
  290. VecConnection* createVecConnection(const SourceTargetDim& STDim)
  291. {
  292. allocateLayers(STDim);
  293. VecConnection* MyVecCon = new VecConnection(mL1,mL2);
  294. return MyVecCon;
  295. }
  296. void makeTempDir()
  297. {
  298. mTempDir_Memsave = "veccontmp_memsave";
  299. mTempDir_NoMemsave = "veccontmp";
  300. #ifdef DELAYS_CHAR
  301. mTempDir = mTempDir_Memsave;
  302. #else
  303. mTempDir = mTempDir_NoMemsave;
  304. #endif
  305. mkdir(mTempDir.c_str(),16877);
  306. GetGlobalSimLoop()->SetDataDirectory_(mTempDir.c_str());
  307. mTempDirWasCreated = true;
  308. }
  309. };
  310. #endif // VECCONTEST_H