Skip to content

Commit

Permalink
Add new tests to LayerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
cogmission committed Oct 17, 2016
1 parent bb72e4c commit 5015a20
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/numenta/nupic/network/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ public Layer<T> postDeSerialize() {
public void setNetwork(Network network) {
this.parentNetwork = network;
}

/**
* Returns the parent {@link Network}
* @return the parent Network;
*/
public Network getNetwork() {
return this.parentNetwork;
}

/**
* Creates a new {@code Layer} initialized with the specified algorithmic
Expand Down Expand Up @@ -415,6 +423,15 @@ public CheckPointOp<byte[]> delegateCheckPointCall() {
public void setRegion(Region r) {
this.parentRegion = r;
}

/**
* Returns the parent {@link Region}
*
* @return the parent Region
*/
public Region getRegion() {
return this.parentRegion;
}

/**
* Finalizes the initialization in one method call so that side effect
Expand Down
124 changes: 124 additions & 0 deletions src/test/java/org/numenta/nupic/network/LayerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.numenta.nupic.algorithms.TemporalMemory;
import org.numenta.nupic.datagen.ResourceLocator;
import org.numenta.nupic.encoders.MultiEncoder;
import org.numenta.nupic.model.Connections;
import org.numenta.nupic.model.SDR;
import org.numenta.nupic.network.Layer.FunctionFactory;
import org.numenta.nupic.network.sensor.FileSensor;
Expand Down Expand Up @@ -111,6 +112,129 @@ public void testMasking() {
algo_content_mask ^= Layer.CLA_CLASSIFIER;
assertEquals(0, algo_content_mask);
}

@Test
public void callsOnClosedLayer() {
Parameters p = NetworkTestHarness.getParameters().copy();
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
p.set(KEY.RANDOM, new UniversalRandom(42));

Network n = new Network("AlreadyClosed", p)
.add(Network.createRegion("AlreadyClosed")
.add(Network.createLayer("AlreadyClosed", p)));

Layer<?> l = n.lookup("AlreadyClosed").lookup("AlreadyClosed");
l.using(new Connections());
l.using(p);

l.close();

try {
l.using(new Connections());

fail(); // Should fail here, disallowing "using" call on closed layer
}catch(Exception e) {
assertEquals(IllegalStateException.class, e.getClass());
assertEquals("Layer already \"closed\"", e.getMessage());
}

try {
l.using(p);

fail(); // Should fail here, disallowing "using" call on closed layer
}catch(Exception e) {
assertEquals(IllegalStateException.class, e.getClass());
assertEquals("Layer already \"closed\"", e.getMessage());
}
}

@Test
public void testNoName() {
Parameters p = Parameters.getAllDefaultParameters();

try {
new Network("", p)
.add(Network.createRegion("")
.add(Network.createLayer("", p)
.add(Sensor.create(
FileSensor::create,
SensorParams.create(
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));

fail(); // Fails due to no name...
}catch(Exception e) {
assertEquals(IllegalStateException.class, e.getClass());
assertEquals("All Networks must have a name. Increases digestion, and overall happiness!",
e.getMessage());
}

try {
new Network("Name", p)
.add(Network.createRegion("")
.add(Network.createLayer("", p)
.add(Sensor.create(
FileSensor::create,
SensorParams.create(
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));

fail(); // Fails due to no name on Region...
}catch(Exception e) {
assertEquals(IllegalArgumentException.class, e.getClass());
assertEquals("Name may not be null or empty. ...not that anyone here advocates name calling!",
e.getMessage());
}

try {
p = NetworkTestHarness.getParameters().copy();
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
p.set(KEY.RANDOM, new UniversalRandom(42));

PublisherSupplier supplier = PublisherSupplier.builder()
.addHeader("dayOfWeek")
.addHeader("int")
.addHeader("B").build();

Network n = new Network("Name", p)
.add(Network.createRegion("Name")
.add(Network.createLayer("Name", p)));

Layer<?> l = n.lookup("Name").lookup("Name");
l.add(Sensor.create(
ObservableSensor::create,
SensorParams.create(
Keys::obs, "", supplier)));

assertEquals(n, l.getNetwork());
assertTrue(l.getRegion() != null);

}catch(Exception e) {
e.printStackTrace();
}
}

@Test
public void testAddSensor() {
Parameters p = NetworkTestHarness.getParameters().copy();
p = p.union(NetworkTestHarness.getDayDemoTestEncoderParams());
p.set(KEY.RANDOM, new UniversalRandom(42));

try {
new Network("Name", p)
.add(Network.createRegion("")
.add(Network.createLayer("", p)
.add(Sensor.create(
FileSensor::create,
SensorParams.create(
Keys::path, "", ResourceLocator.path("rec-center-hourly-small.csv"))))));

fail(); // Fails due to no name...
}catch(Exception e) {
e.printStackTrace();
assertEquals(IllegalStateException.class, e.getClass());
assertEquals("All Networks must have a name. Increases digestion, and overall happiness!",
e.getMessage());
}
}

@Test
public void testGetAllValues() {
Expand Down

0 comments on commit 5015a20

Please sign in to comment.