123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- #ifndef VECCONTEST_H
- #define VECCONTEST_H
- #include <cxxtest/TestSuite.h>
- #include "debug.hpp"
- #include "simloop.hpp"
- #include "vconnection.hpp"
- #include "layer.hpp"
- #include "chunkfile.h"
- #include <sys/stat.h>
- using namespace std;
- class VecConTest : public CxxTest::TestSuite
- {
- public:
- VecConTest(): mTempDirWasCreated(false), mTempDir("gagadir"), mL1(NULL), mL2(NULL){}
- void testMakeTempDir()
- {
- makeTempDir();
- }
-
- void setUp()
- {
- makeTempDir();
- setupSTDim();
- }
-
- void tearDown()
- {
- if (mL1) {
- delete mL1;
- mL1 = NULL;
- }
- if (mL2) {
- delete mL2;
- mL2 = NULL;
- }
- }
-
- void xtestTurnOnDebug()
- {
- // Debug( dc::con.on() );
- // Debug( dc::chunk.on() );
- // Debug( libcw_do.on() );
- }
- void testSaveWeightsAndReadHeader()
- {
- saveWeightFileOldAndNew("veccon_v2_0.weights");
- ChunkFileReader MyReader(mTempDir+"/" + mFileName, mVecFileFormat);
- SourceTargetDim VecConHeader;
- MyReader.read(VecConHeader);
-
- TS_ASSERT_EQUALS(mSTDim_Saved.stringify(),VecConHeader.stringify());
- }
- void testLoadWeightFile()
- {
- saveWeightFileOldAndNew("veccon_v2_0.weights");
- VecConnection MyVecCon;
- MyVecCon.Load_VecConnection_2_0(mTempDir+"/" + mFileName);
- SourceTargetDim STDim_Loaded = MyVecCon.GetSourceTargetDim();
- TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
- }
-
- void testCompareVecConnections()
- {
- VecConnection Con1;
- VecConnection Con2;
- TS_ASSERT(Con1.hasEqualSynapses(Con2));
- }
-
- void testCompareVecDiffers()
- {
- VecConnection Con1;
- setupSTDim();
- VecConnection* Con3 = createVecConnection(mSTDim_Saved);
- Con3->ConnectSelf(0.3);
- VecConnection* Con4 = createVecConnection(mSTDim_Saved);
- Con4->ConnectSelf(0.5);
-
- TS_ASSERT(!Con1.hasEqualSynapses(*Con3));
- TS_ASSERT(!(Con4->hasEqualSynapses(*Con3)));
-
- delete Con3;
- delete Con4;
- }
-
- void testCompareVecDelay()
- {
- setupSTDim();
- VecConnection* Con1 = createVecConnection(mSTDim_Saved);
- Con1->ConnectSelf(0.3, 5,2);
- VecConnection* Con2 = createVecConnection(mSTDim_Saved);
- Con2->ConnectSelf(0.3, 10, 9.75);
- VecConnection* Con3 = createVecConnection(mSTDim_Saved);
- Con3->ConnectSelf(0.3, 10, 9.75);
- TS_ASSERT(!Con1->hasEqualSynapses(*Con2));
- TS_ASSERT(Con3->hasEqualSynapses(*Con2));
- delete Con1;
- delete Con2;
- delete Con3;
- }
- void testWriteAndLoadVecCon()
- {
- string FileName=mTempDir+"/VecCon_v2_0.weights";
- setupSTDim();
- VecConnection* Con1 = createVecConnection(mSTDim_Saved);
- Con1->ConnectSelf(0.3, 5,2);
- Con1->Save_VecConnection_2_0(FileName);
- VecConnection LoadVecCon;
- LoadVecCon.Load_VecConnection_2_0(FileName);
- SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
- TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
- TS_ASSERT(Con1->hasEqualSynapses(LoadVecCon));
- delete Con1;
- }
-
- void testLoadVecConViaLoad()
- {
- string FileName="VecCon_v2_0.weights";
- setupSTDim();
- VecConnection* Con1 = createVecConnection(mSTDim_Saved);
- Con1->ConnectSelf(0.3, 5,2);
- Con1->Save_VecConnection_2_0(mTempDir+"/"+FileName);
-
- VecConnection LoadVecCon;
- LoadVecCon.Load(FileName);
- SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
- TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
- TS_ASSERT(Con1->hasEqualSynapses(LoadVecCon));
- delete Con1;
- }
- void testReadFileFormat()
- {
- saveWeightFileOldAndNew("veccon_v2_0.weights");
- FileFormat MyFileFormat = readFileFormat(mTempDir+"/"+mFileName);
- TS_ASSERT_EQUALS(MyFileFormat.print(), mVecFileFormat.print());
- }
- void testSaveAndLoadOldFileFormat()
- {
- VecConnection LoadVecCon;
- if (!LoadVecCon.compiledWithMemsave()) {
- setupSTDim();
- VecConnection* MyVecCon=createAnd_SaveOldFileFormat(mSTDim_Saved);
- LoadVecCon.Load(mFileName);
- SourceTargetDim STDim_Loaded = LoadVecCon.GetSourceTargetDim();
- TS_ASSERT_EQUALS(STDim_Loaded.stringify(), mSTDim_Saved.stringify());
- TS_ASSERT(MyVecCon->hasEqualSynapses(LoadVecCon));
- delete MyVecCon;
- }
- }
- void testIfCompiledWithMemsave()
- {
- VecConnection MyVecCon;
- if (compiledWithLowMemConfig()) {
- TS_ASSERT (MyVecCon.compiledWithMemsave());
- } else {
- TS_ASSERT (!MyVecCon.compiledWithMemsave());
- }
- }
- void testExceptionThrownWhenLoadOldFormatWithMemsave()
- {
- GetGlobalSimLoop()->SetDataDirectory_(".");
- string FileAndDirName = "testdata/VecConnection_1.0_weights";
- VecConnection LoadVecCon;
- if (LoadVecCon.compiledWithMemsave()) {
- TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), NotSupportedWithMemsave);
- } else {
- TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
- }
- }
-
-
- void testExceptionThrownWhenSaveOldFormatWithMemsave()
- {
- VecConnection* MyVecCon = createVecConnectionGaussian(mSTDim_Saved);
- if (MyVecCon->compiledWithMemsave()) {
- TS_ASSERT_THROWS( MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName), NotSupportedWithMemsave );
- } else {
- TS_ASSERT_THROWS_NOTHING( MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName) );
- }
- delete MyVecCon;
- }
- void xtestSaveBigWeightFile()
- {
- setupSTDim(400,20,20, 400,20,20);
- saveWeightFileOldAndNew("BigWeightFile.weights", 0.06);
- }
-
- void testLoadMemsaveFileInNonMemsaveMode()
- {
- GetGlobalSimLoop()->SetDataDirectory_(".");
- string FileAndDirName = "testdata/VecCon_v2_0_memsave.weights";
- VecConnection LoadVecCon;
- TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
- }
-
- void xtestWriteWeightFileLongDelays()
- {
- setupSTDim(16,4,4,16,4,4);
- float MaxDelay= ( DMAX -1 ) * 0.25;
- #ifndef DELAYS_CHARs
- saveWeightFileOldAndNew("LongDelay.weights", 0.4, MaxDelay);
- #endif
- }
- void testCheckExceptionIfLoadMemsaveWithTooLongDelays()
- {
- GetGlobalSimLoop()->SetDataDirectory_(".");
- string FileAndDirName = "testdata/LongDelay.weights";
- VecConnection LoadVecCon;
- if (LoadVecCon.compiledWithMemsave()) {
- TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), DataLossAtVectorCast);
- } else {
- TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), RequestedDelayTooLarge);
- }
- }
- void testLayerNNeuronsTooLarge()
- {
- // DecoLifLayer L1(NNEURONS_MAX+1);
- }
-
- void xtestWriteWeightFileManyNeurons()
- {
- setupSTDim(40000,200,200,16,4,4);
- float MaxDelay=130;
- #ifndef NNEURONS_SHORT
- saveWeightFileOldAndNew("ManyNeurons.weights", 0.4);
- #endif
- }
- void testLoadWeightFileManyNeurons()
- {
- GetGlobalSimLoop()->SetDataDirectory_(".");
- string FileAndDirName = "testdata/ManyNeurons.weights";
- VecConnection LoadVecCon;
- if (LoadVecCon.compiledWithMemsave()) {
- TS_ASSERT_THROWS(LoadVecCon.Load(FileAndDirName), DataLossAtVectorCast);
- } else {
- TS_ASSERT_THROWS_NOTHING(LoadVecCon.Load(FileAndDirName));
- }
- }
-
-
- void xtestCleanUp()
- {
- if (mTempDirWasCreated) {
- system((string("rm -rf ") + mTempDir).c_str());
- }
- }
-
- private:
- SourceTargetDim mSTDim_Saved;
- string mFileName;
- FileFormat mVecFileFormat;
- string mTempDir_Memsave;
- string mTempDir_NoMemsave;
- string mTempDir;
- bool mTempDirWasCreated;
- DecoLifLayer* mL1;
- DecoLifLayer* mL2;
-
- void saveWeightFileOldAndNew(const string& FileName, float sigma=0.15, float MaxDelay=10)
- {
- mFileName = FileName;
- mVecFileFormat = FileFormat("VecConnection", 2, 0);
- allocateLayers(mSTDim_Saved);
- VecConnection MyVecCon(mL1,mL2);
- MyVecCon.ConnectGaussian(sigma, 10, MaxDelay);
- if (!MyVecCon.compiledWithMemsave()) {
- MyVecCon.Save_VecConnection_1_0(mTempDir+"/SavedVecCon_1.0_weights");
- }
- MyVecCon.Save(mFileName.c_str());
- }
- void setupSTDim(int NS, int NSx, int NSy, int NT, int NTx, int NTy)
- {
- mSTDim_Saved = SourceTargetDim(NS, NSx, NSy, NT, NTx, NTy);
- }
-
-
- void setupSTDim()
- {
- int NSx=6;
- int NSy=7;
- int NTx=7;
- int NTy=5;
- int NS = NSx*NSy;
- int NT = NTx*NTy;
- mSTDim_Saved = SourceTargetDim(NS, NSx, NSy, NT, NTx, NTy);
- }
- VecConnection* createAnd_SaveOldFileFormat(const SourceTargetDim& STDim)
- {
- return createAnd_SaveOldFileFormat("VecCon_OldFormat_weights", STDim);
- }
-
- VecConnection* createAnd_SaveOldFileFormat(const string& FileName, const SourceTargetDim& STDim)
- {
- mFileName = FileName;
- VecConnection* MyVecCon = createVecConnection(STDim);
- MyVecCon->ConnectGaussian(0.3, 10);
- MyVecCon->Save_VecConnection_1_0(mTempDir+"/"+mFileName);
- return MyVecCon;
- }
- void allocateLayers(const SourceTargetDim& STDim)
- {
- mL1 = new DecoLifLayer(STDim.NSource);
- mL2 = new DecoLifLayer(STDim.NTarget);
- mL1->SetupPositions(STDim.NSx, STDim.NSy,true);
- mL2->SetupPositions(STDim.NTx, STDim.NTy,true);
- }
- VecConnection* createVecConnectionGaussian(const SourceTargetDim& STDim)
- {
- allocateLayers(STDim);
- VecConnection* MyVecCon = new VecConnection(mL1,mL2);
- MyVecCon->ConnectGaussian(0.3, 10);
- return MyVecCon;
- }
-
-
- VecConnection* createVecConnection(const SourceTargetDim& STDim)
- {
- allocateLayers(STDim);
- VecConnection* MyVecCon = new VecConnection(mL1,mL2);
- return MyVecCon;
- }
- void makeTempDir()
- {
- mTempDir_Memsave = "veccontmp_memsave";
- mTempDir_NoMemsave = "veccontmp";
- #ifdef DELAYS_CHAR
- mTempDir = mTempDir_Memsave;
- #else
- mTempDir = mTempDir_NoMemsave;
- #endif
- mkdir(mTempDir.c_str(),16877);
- GetGlobalSimLoop()->SetDataDirectory_(mTempDir.c_str());
- mTempDirWasCreated = true;
- }
- };
- #endif // VECCONTEST_H
|