Skip to content

Instantly share code, notes, and snippets.

@cogmission
Last active August 29, 2015 14:10
Show Gist options
  • Save cogmission/178239c1d60392e3165f to your computer and use it in GitHub Desktop.
Save cogmission/178239c1d60392e3165f to your computer and use it in GitHub Desktop.
Changes to get Builder to work
package org.numenta.nupic.encoders;
public class AdaptiveScalarEncoder extends ScalarEncoder {
/*
* This is an implementation of the scalar encoder that adapts the min and
* max of the scalar encoder dynamically. This is essential to the streaming
* model of the online prediction framework.
*
* Initialization of an adapive encoder using resolution or radius is not
* supported; it must be intitialized with n. This n is kept constant while
* the min and max of the encoder changes.
*
* The adaptive encoder must be have periodic set to false.
*
* The adaptive encoder may be initialized with a minval and maxval or with
* `None` for each of these. In the latter case, the min and max are set as
* the 1st and 99th percentile over a window of the past 100 records.
*
* *Note:** the sliding window may record duplicates of the values in the
* dataset, and therefore does not reflect the statistical distribution of
* the input data and may not be used to calculate the median, mean etc.
*/
/*
* (non-Javadoc)
*
* @see org.numenta.nupic.encoders.ScalarEncoder#init()
*/
@Override
public void init() {
this.setPeriodic(false);
super.init();
}
/*
* (non-Javadoc)
*
* @see org.numenta.nupic.encoders.ScalarEncoder#initEncoder(int, double,
* double, int, double, double)
*/
@Override
public void initEncoder(int w, double minVal, double maxVal, int n,
double radius, double resolution) {
this.setPeriodic(false);
this.encLearningEnabled = true;
if (this.periodic) {
throw new IllegalStateException(
"Adaptive scalar encoder does not encode periodic inputs");
}
assert n != 0;
super.initEncoder(w, minVal, maxVal, n, radius, resolution);
}
/**
*
*/
public AdaptiveScalarEncoder() {
}
/**
* Returns a builder for building AdaptiveScalarEncoder.
* This builder may be reused to produce multiple builders
*
* @return a {@code AdaptiveScalarEncoder.Builder}
*/
public static AdaptiveScalarEncoder.Builder adaptiveBuilder() {
return new AdaptiveScalarEncoder.Builder();
}
public static class Builder extends Encoder.Builder<AdaptiveScalarEncoder.Builder, AdaptiveScalarEncoder> {
private Builder() {}
@Override
public AdaptiveScalarEncoder build() {
encoder = new AdaptiveScalarEncoder();
super.build();
((AdaptiveScalarEncoder)encoder).init();
return (AdaptiveScalarEncoder) encoder;
}
}
}
/* ---------------------------------------------------------------------
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2014, Numenta, Inc. Unless you have an agreement
* with Numenta, Inc., for a separate license for this software code, the
* following terms and conditions apply:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
* ---------------------------------------------------------------------
*/
package org.numenta.nupic.encoders;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* @author sambit
*
*/
public class AdaptiveScalarEncoderTest {
private AdaptiveScalarEncoder ase;
private AdaptiveScalarEncoder.Builder builder;
@Rule
public ExpectedException exception = ExpectedException.none();
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
*
*/
@Before
public void setUp() {
builder = AdaptiveScalarEncoder.adaptiveBuilder()
.n(14)
.w(3)
.minVal(1)
.maxVal(8)
.radius(1.5)
.resolution(0.5)
.periodic(false)
.forced(true);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
private void initASE() {
ase = builder.build();
}
/**
* Test method for {@link org.numenta.nupic.encoders.AdaptiveScalarEncoder#AdaptiveScalarEncoder()}.
*/
@Test
public void testAdaptiveScalarEncoder() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
}
/**
* Test method for
* {@link org.numenta.nupic.encoders.AdaptiveScalarEncoder#init()}.
*/
@Test
public void testInitThrowsIllegalStateExceptionForW() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null",
ase);
exception.expect(IllegalStateException.class);
ase.init();
}
@Test
public void testInitThrowsIllegalStateExceptionForMaxVal() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
ase.setW(3);
exception.expect(IllegalStateException.class);
ase.init();
}
@Test
public void testInitThrowsIllegalStateExceptionForN() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
ase.setW(3);
ase.setMinVal(1);
ase.setMaxVal(8);
ase.setN(14);
exception.expect(IllegalStateException.class);
ase.init();
}
@Test
public void testInitThrowsIllegalStateExceptionForRadius() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
ase.setW(3);
ase.setMinVal(1);
ase.setMaxVal(8);
ase.setN(14);
ase.setRadius(1.5);
exception.expect(IllegalStateException.class);
ase.init();
}
@Test
public void testInitThrowsIllegalStateExceptionForInterval() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
ase.setW(3);
ase.setMinVal(1);
ase.setMaxVal(8);
ase.setN(14);
ase.setResolution(0.5);
exception.expect(IllegalStateException.class);
ase.init();
}
@Test
public void testInit() {
setUp();
initASE();
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
ase.setW(3);
ase.setMinVal(1);
ase.setMaxVal(8);
ase.setN(14);
ase.setRadius(1.5);
ase.setResolution(0.5);
ase.setForced(true);
ase.init();
}
/**
* Test method for
* {@link org.numenta.nupic.encoders.AdaptiveScalarEncoder#initEncoder(int, double, double, int, double, double)}
* .
*/
@Test
public void testInitEncoder() {
setUp();
initASE();
ase.initEncoder(3, 1, 8, 14, 1.5, 0.5);
Assert.assertNotNull("AdaptiveScalarEncoder class is null", ase);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment