org.numenta.nupic.encoders.Encoder Java Examples

The following examples show how to use org.numenta.nupic.encoders.Encoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Layer.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Stores a {@link NamedTuple} which contains the input values and bucket
 * information - keyed to the encoded field name so that a classifier can
 * retrieve it later on in the processing sequence.
 * 
 * @param inference
 * @param encoderInputMap
 */
private void doEncoderBucketMapping(Inference inference, Map<String, Object> encoderInputMap) {
    if(encoderTuples == null) {
        encoderTuples = encoder.getEncoders(encoder);
    }

    // Store the encoding
    int[] encoding = inference.getEncoding();

    for(EncoderTuple t : encoderTuples) {
        String name = t.getName();
        Encoder<?> e = t.getEncoder();

        int bucketIdx = -1;
        Object o = encoderInputMap.get(name);
        if(DateTime.class.isAssignableFrom(o.getClass())) {
            bucketIdx = ((DateEncoder)e).getBucketIndices((DateTime)o)[0];
        } else if(Number.class.isAssignableFrom(o.getClass())) {
            bucketIdx = e.getBucketIndices((double)o)[0];
        } else {
            bucketIdx = e.getBucketIndices((String)o)[0];
        }

        int offset = t.getOffset();
        int[] tempArray = new int[e.getWidth()];
        System.arraycopy(encoding, offset, tempArray, 0, tempArray.length);

        inference.getClassifierInput().put(
                name,
                new NamedTuple(
                        new String[] { "name", "inputValue", "bucketIdx", "encoding" },
                        name,
                        o,
                        bucketIdx,
                        tempArray
                ));
    }
}
 
Example #2
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Searches through the specified {@link MultiEncoder}'s previously configured 
 * encoders to find and return one that is of type {@link CoordinateEncoder} or
 * {@link GeospatialCoordinateEncoder}
 * 
 * @param enc   the containing {@code MultiEncoder}
 * @return
 */
private Optional<Encoder<?>> getCoordinateEncoder(MultiEncoder enc) {
    for(EncoderTuple t : enc.getEncoders(enc)) {
        if((t.getEncoder() instanceof CoordinateEncoder) ||
            (t.getEncoder() instanceof GeospatialCoordinateEncoder)) {
            return Optional.of(t.getEncoder());
        }
    }
    
    return Optional.empty();
}
 
Example #3
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Searches through the specified {@link MultiEncoder}'s previously configured 
 * encoders to find and return one that is of type {@link CategoryEncoder} or
 * {@link SDRCategoryEncoder}
 * 
 * @param enc   the containing {@code MultiEncoder}
 * @return
 */
private Optional<Encoder<?>> getCategoryEncoder(MultiEncoder enc) {
    for(EncoderTuple t : enc.getEncoders(enc)) {
        if((t.getEncoder() instanceof CategoryEncoder) ||
            (t.getEncoder() instanceof SDRCategoryEncoder)) {
            return Optional.of(t.getEncoder());
        }
    }
    
    return Optional.empty();
}
 
Example #4
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Searches through the specified {@link MultiEncoder}'s previously configured 
 * encoders to find and return one that is of type {@link ScalarEncoder},
 * {@link RandomDistributedScalarEncoder}, {@link AdaptiveScalarEncoder},
 * {@link LogEncoder} or {@link DeltaEncoder}.
 * 
 * @param enc   the containing {@code MultiEncoder}
 * @return
 */
private Optional<Encoder<?>> getNumberEncoder(MultiEncoder enc) {
    for(EncoderTuple t : enc.getEncoders(enc)) {
        if((t.getEncoder() instanceof RandomDistributedScalarEncoder) ||
            (t.getEncoder() instanceof ScalarEncoder) ||
            (t.getEncoder() instanceof AdaptiveScalarEncoder) ||
            (t.getEncoder() instanceof LogEncoder) ||
            (t.getEncoder() instanceof DeltaEncoder)) {
            
            return Optional.of(t.getEncoder());
        }
    }
    
    return Optional.empty();
 }
 
Example #5
Source File: HTMSensor.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Sets up a mapping which describes the order of occurrence of comma
 * separated fields - mapping their ordinal position to the {@link Encoder}
 * which services the encoding of the field occurring in that position. This
 * sequence of types is contained by an instance of {@link Header} which
 * makes available an array of {@link FieldMetaType}s.
 */
private void makeIndexEncoderMap() {
    indexToEncoderMap = new TIntObjectHashMap<Encoder<?>>();
    
    for (int i = 0, size = header.getFieldNames().size(); i < size; i++) {
        switch (header.getFieldTypes().get(i)) {
            case DATETIME:
                Optional<DateEncoder> de = getDateEncoder(encoder);
                if (de.isPresent()) {
                    indexToEncoderMap.put(i, de.get());
                } else {
                    throw new IllegalArgumentException("DateEncoder never initialized: " + header.getFieldNames().get(i));
                }
                break;
            case BOOLEAN:
            case FLOAT:
            case INTEGER:
                Optional<Encoder<?>> ne = getNumberEncoder(encoder);
                if (ne.isPresent()) {
                    indexToEncoderMap.put(i, ne.get());
                } else {
                    throw new IllegalArgumentException("Number (or Boolean) encoder never initialized: " + header.getFieldNames().get(i));
                }
                break;
            case LIST:
            case STRING:
                Optional<Encoder<?>> ce = getCategoryEncoder(encoder);
                if (ce.isPresent()) {
                    indexToEncoderMap.put(i, ce.get());
                } else {
                    throw new IllegalArgumentException("Category encoder never initialized: " + header.getFieldNames().get(i));
                }
                break;
            case COORD:
            case GEO:
                Optional<Encoder<?>> ge = getCoordinateEncoder(encoder);
                if (ge.isPresent()) {
                    indexToEncoderMap.put(i, ge.get());
                } else {
                    throw new IllegalArgumentException("Coordinate encoder never initialized: " + header.getFieldNames().get(i));
                }
                break;
            case SARR:
            case DARR:
                Optional<SDRPassThroughEncoder> spte = getSDRPassThroughEncoder(encoder);
                if (spte.isPresent()) {
                    indexToEncoderMap.put(i, spte.get());
                } else {
                    throw new IllegalArgumentException("SDRPassThroughEncoder encoder never initialized: " + header.getFieldNames().get(i));
                }
                break;
            default:
                break;
        }
    }
    
}
 
Example #6
Source File: HTMSensorTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Special test case for extra processing for category encoder lists
 */
@Test
public void testCategoryEncoderCreation() {
    Sensor<File> sensor = Sensor.create(
        FileSensor::create, SensorParams.create(
            Keys::path, "", ResourceLocator.path("rec-center-hourly-4period-cat.csv")));
    
    
    // Cast the ValueList to the more complex type (Header)
    HTMSensor<File> htmSensor = (HTMSensor<File>)sensor;
    Header meta = (Header)htmSensor.getMetaInfo();
    assertTrue(meta.getFieldTypes().stream().allMatch(
        l -> l.equals(FieldMetaType.DATETIME) || l.equals(FieldMetaType.FLOAT) || l.equals(FieldMetaType.LIST)));
    assertTrue(meta.getFieldNames().stream().allMatch(
        l -> l.equals("timestamp") || l.equals("consumption") || l.equals("type")));
    assertTrue(meta.getFlags().stream().allMatch(
        l -> l.equals(SensorFlags.T) || l.equals(SensorFlags.B) || l.equals(SensorFlags.C)));
    
    // Set the parameters on the sensor.
    // This enables it to auto-configure itself; a step which will
    // be done at the Region level.
    Encoder<Object> multiEncoder = htmSensor.getEncoder();
    assertNotNull(multiEncoder);
    assertTrue(multiEncoder instanceof MultiEncoder);
    
    // Set the Local parameters on the Sensor
    htmSensor.initEncoder(getCategoryEncoderParams());
    List<EncoderTuple> encoders = multiEncoder.getEncoders(multiEncoder);
    assertEquals(3, encoders.size());
    
    DateEncoder dateEnc = (DateEncoder)encoders.get(1).getEncoder();
    SDRCategoryEncoder catEnc = (SDRCategoryEncoder)encoders.get(2).getEncoder();
    assertEquals("[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]", Arrays.toString(catEnc.encode("ES")));
    
    // Now test the encoding of an input row
    Map<String, Object> d = new HashMap<String, Object>();
    d.put("timestamp", dateEnc.parse("7/12/10 13:10"));
    d.put("consumption", 35.3);
    d.put("type", "ES");
    int[] output = multiEncoder.encode(d);
    System.out.println("output = "+ Arrays.toString(output));
    int[] expected = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                      0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    assertTrue(Arrays.equals(expected, output));
}
 
Example #7
Source File: HTMSensorTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tests the auto-creation of Encoders from Sensor meta data.
 */
@Test
public void testInternalEncoderCreation() {
    
    Sensor<File> sensor = Sensor.create(
        FileSensor::create, 
        SensorParams.create(
            Keys::path, "", ResourceLocator.path("rec-center-hourly.csv")));
    
    
    // Cast the ValueList to the more complex type (Header)
    HTMSensor<File> htmSensor = (HTMSensor<File>)sensor;
    Header meta = (Header)htmSensor.getMetaInfo();
    assertTrue(meta.getFieldTypes().stream().allMatch(
        l -> l.equals(FieldMetaType.DATETIME) || l.equals(FieldMetaType.FLOAT)));
    assertTrue(meta.getFieldNames().stream().allMatch(
        l -> l.equals("timestamp") || l.equals("consumption")));
    assertTrue(meta.getFlags().stream().allMatch(
        l -> l.equals(SensorFlags.T) || l.equals(SensorFlags.B)));
    
    // Set the parameters on the sensor.
    // This enables it to auto-configure itself; a step which will
    // be done at the Region level.
    Encoder<Object> multiEncoder = htmSensor.getEncoder();
    assertNotNull(multiEncoder);
    assertTrue(multiEncoder instanceof MultiEncoder);
    
    // Set the Local parameters on the Sensor
    htmSensor.initEncoder(getTestEncoderParams());
    List<EncoderTuple> encoders = multiEncoder.getEncoders(multiEncoder);
    assertEquals(2, encoders.size());
    
    // Test date specific encoder configuration
    //
    // All encoders in the MultiEncoder are accessed in a particular
    // order (the alphabetical order their corresponding fields are in),
    // so alphabetically "consumption" proceeds "timestamp"
    // so we need to ensure that the proper order is preserved (i.e. exists at index 1)
    DateEncoder dateEnc = (DateEncoder)encoders.get(1).getEncoder();
    try {
        dateEnc.parseEncode("7/12/10 13:10");
        dateEnc.parseEncode("7/12/2010 13:10");
        // Should fail here due to conflict with configured format
        dateEnc.parseEncode("13:10 7/12/10");
        fail();
    }catch(Exception e) {
       assertEquals("Invalid format: \"13:10 7/12/10\" is malformed at \":10 7/12/10\"", e.getMessage());
    }
    
    RandomDistributedScalarEncoder rdse = (RandomDistributedScalarEncoder)encoders.get(0).getEncoder();
    int[] encoding = rdse.encode(35.3);
    System.out.println(Arrays.toString(encoding));
    
    // Now test the encoding of an input row
    Map<String, Object> d = new HashMap<String, Object>();
    d.put("timestamp", dateEnc.parse("7/12/10 13:10"));
    d.put("consumption", 35.3);
    int[] output = multiEncoder.encode(d);
    int[] expected = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    assertTrue(Arrays.equals(expected, output));
}