org.numenta.nupic.util.Tuple Java Examples

The following examples show how to use org.numenta.nupic.util.Tuple. 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: ParametersTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
                    null,
                    0, // n
                    0, // w
                    0, 0, 0, 0, null, null, null,
                    "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
                    fieldEncodings, 
                    25, 
                    3, 
                    0, 0, 0, 0.1, null, null, null, 
                    "consumption", "float", "RandomDistributedScalarEncoder");

    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");

    return fieldEncodings;
}
 
Example #2
Source File: NetworkDemoHarness.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getNetworkDemoFieldEncodingMap() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
            null,
            0, // n
            0, // w
            0, 0, 0, 0, null, null, null,
            "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
            fieldEncodings, 
            50, 
            21, 
            0, 100, 0, 0.1, null, Boolean.TRUE, null, 
            "consumption", "float", "ScalarEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(21,9.5)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    return fieldEncodings;
}
 
Example #3
Source File: NetworkDemoHarness.java    From htm.java-examples with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
            null,
            0, // n
            0, // w
            0, 0, 0, 0, null, null, null,
            "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
            fieldEncodings, 
            25, 
            3, 
            0, 0, 0, 0.1, null, null, null, 
            "consumption", "float", "RandomDistributedScalarEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    return fieldEncodings;
}
 
Example #4
Source File: BatchedCsvStream.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Constructs a new {@code BatchedCsvHeader}
 * 
 * @param lines                     List of csv strings
 * @param configuredHeaderLength    number of header rows
 */
public <T> BatchedCsvHeader(List<T> lines, int configuredHeaderLength) {
    
    if((configuredHeaderLength < 1 || lines == null || lines.size() < 1) || 
        (configuredHeaderLength > 1 && lines.size() != configuredHeaderLength)) {
        
        throw new IllegalStateException("Actual Header was not the expected size: " + 
            (configuredHeaderLength < 1 ? "> 1" : configuredHeaderLength) + 
                ", but was: " + (lines == null ? "null" : lines.size()));
    }
    
    headerValues = new Tuple[configuredHeaderLength];
    for(int i = 0;i < headerValues.length;i++) {
        headerValues[i] = new Tuple((Object[])lines.get(i));
    }
}
 
Example #5
Source File: ScalarEncoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns a list of {@link Tuple}s which in this case is a list of
 * key value parameter values for this {@code ScalarEncoder}
 *
 * @return	a list of {@link Tuple}s
 */
public List<Tuple> dict() {
    List<Tuple> l = new ArrayList<Tuple>();
    l.add(new Tuple("maxval", getMaxVal()));
    l.add(new Tuple("bucketValues", getBucketValues(Double.class)));
    l.add(new Tuple("nInternal", getNInternal()));
    l.add(new Tuple("name", getName()));
    l.add(new Tuple("minval", getMinVal()));
    l.add(new Tuple("topDownValues", Arrays.toString(getTopDownValues())));
    l.add(new Tuple("clipInput", clipInput()));
    l.add(new Tuple("n", getN()));
    l.add(new Tuple("padding", getPadding()));
    l.add(new Tuple("range", getRange()));
    l.add(new Tuple("periodic", isPeriodic()));
    l.add(new Tuple("radius", getRadius()));
    l.add(new Tuple("w", getW()));
    l.add(new Tuple("topDownMappingM", getTopDownMapping()));
    l.add(new Tuple("halfwidth", getHalfWidth()));
    l.add(new Tuple("resolution", getResolution()));
    l.add(new Tuple("rangeInternal", getRangeInternal()));

    return l;
}
 
Example #6
Source File: ParametersTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap_varyN() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
                    null,
                    20, // n
                    0, // w
                    0, 0, 0, 0, null, null, null,
                    "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
                    fieldEncodings, 
                    25, 
                    3, 
                    0, 0, 0, 0.1, null, null, null, 
                    "consumption", "float", "RandomDistributedScalarEncoder");

    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");

    return fieldEncodings;
}
 
Example #7
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return the field names for each of the scalar values returned by
    * .
 * @param parentFieldName	parentFieldName The name of the encoder which is our parent. This name
    *     						is prefixed to each of the field names within this encoder to form the
    *      					keys of the dict() in the retval.
    *
 * @return
 */
@SuppressWarnings("unchecked")
public List<String> getScalarNames(String parentFieldName) {
	List<String> names = new ArrayList<String>();
	if(getEncoders() != null) {
		List<EncoderTuple> encoders = getEncoders(this);
		for(Tuple tuple : encoders) {
			List<String> subNames = ((Encoder<T>)tuple.get(1)).getScalarNames(getName());
			List<String> hierarchicalNames = new ArrayList<String>();
			if(parentFieldName != null) {
				for(String name : subNames) {
					hierarchicalNames.add(String.format("%s.%s", parentFieldName, name));
				}
			}
			names.addAll(hierarchicalNames);
		}
	}else{
		if(parentFieldName != null) {
			names.add(parentFieldName);
		}else{
			names.add((String)getEncoderTuple(this).get(0));
		}
	}

	return names;
}
 
Example #8
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return a pretty print string representing the return values from
    * getScalars and getScalarNames().
 * @param scalarValues 	input values to encode to string
 * @param scalarNames 	optional input of scalar names to convert. If None, gets
    *                  	scalar names from getScalarNames()
 *
 * @return string representation of scalar values
 */
public String scalarsToStr(List<?> scalarValues, List<String> scalarNames) {
	if(scalarNames == null || scalarNames.isEmpty()) {
		scalarNames = getScalarNames("");
	}

	StringBuilder desc = new StringBuilder();
	for(Tuple t : ArrayUtils.zip(scalarNames, scalarValues)) {
		if(desc.length() > 0) {
			desc.append(String.format(", %s:%.2f", t.get(0), t.get(1)));
		}else{
			desc.append(String.format("%s:%.2f", t.get(0), t.get(1)));
		}
	}
	return desc.toString();
}
 
Example #9
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Pretty-print a header that labels the sub-fields of the encoded
    * output. This can be used in conjunction with {@link #pprint(int[], String)}.
 * @param prefix
 */
public void pprintHeader(String prefix) {
	LOGGER.info(prefix == null ? "" : prefix);

	List<Tuple> description = getDescription();
	description.add(new Tuple("end", getWidth()));

	int len = description.size() - 1;
	for(int i = 0;i < len;i++) {
		String name = (String)description.get(i).get(0);
		int width = (int)description.get(i+1).get(1);

		String formatStr = String.format("%%-%ds |", width);
		StringBuilder pname = new StringBuilder(name);
		if(name.length() > width) pname.setLength(width);

           LOGGER.info(String.format(formatStr, pname));
	}

	len = getWidth() + (description.size() - 1)*3 - 1;
	StringBuilder hyphens = new StringBuilder();
	for(int i = 0;i < len;i++) hyphens.append("-");
       LOGGER.info(new StringBuilder(prefix).append(hyphens).toString());
   }
 
Example #10
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Pretty-print the encoded output using ascii art.
 * @param output
 * @param prefix
 */
public void pprint(int[] output, String prefix) {
	LOGGER.info(prefix == null ? "" : prefix);

	List<Tuple> description = getDescription();
	description.add(new Tuple("end", getWidth()));

	int len = description.size() - 1;
	for(int i = 0;i < len;i++) {
		int offset = (int)description.get(i).get(1);
		int nextOffset = (int)description.get(i + 1).get(1);

           LOGGER.info(
                   String.format("%s |",
                           ArrayUtils.bitsToString(
                                   ArrayUtils.sub(output, ArrayUtils.range(offset, nextOffset))
                           )
                   )
           );
       }
   }
 
Example #11
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Return a pretty print string representing the return value from decode().
 *
 * @param decodeResults
 * @return
 */
@SuppressWarnings("unchecked")
public String decodedToStr(Tuple decodeResults) {
	StringBuilder desc = new StringBuilder();
	Map<String, Tuple> fieldsDict = (Map<String, Tuple>)decodeResults.get(0);
	List<String> fieldsOrder = (List<String>)decodeResults.get(1);
	for(String fieldName : fieldsOrder) {
		Tuple ranges = fieldsDict.get(fieldName);
		if(desc.length() > 0) {
			desc.append(", ").append(fieldName).append(":");
		}else{
			desc.append(fieldName).append(":");
		}
		desc.append("[").append(ranges.get(1)).append("]");
	}
	return desc.toString();
}
 
Example #12
Source File: SDRClassifier.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Calculate error signal.
 *
 * @param classification {@link Map} of the classification information:
 * <p>&emsp;"bucketIdx" - index of the encoder bucket
 * <p>&emsp;"actValue" -  actual value doing into the encoder
 * @return
 * {@link Map} containing error. The key is the number of steps. The value
 * is a double[] of the error at the output layer.
    */
private Map<Integer, double[]> calculateError(Map<String, Object> classification) {
	Map<Integer, double[]> error = new HashMap<Integer, double[]>();
	int[] targetDist = new int[maxBucketIdx + 1];
	targetDist[(int)classification.get("bucketIdx")] = 1;

	int iteration = 0;
	int[] learnPatternNZ = null;
	int nSteps = 0;
	for(Tuple t : patternNZHistory) {
		iteration = (int) t.get(0);
		learnPatternNZ = (int[]) t.get(1);
		nSteps = learnIteration - iteration;

		if(steps.contains(nSteps)) {
			double[] predictDist = inferSingleStep(learnPatternNZ, weightMatrix.get(nSteps));
			double[] targetDistMinusPredictDist = new double[maxBucketIdx + 1];
			for(int i = 0; i <= maxBucketIdx; i++) {
				targetDistMinusPredictDist[i] = targetDist[i] - predictDist[i];
			}
			error.put(nSteps, targetDistMinusPredictDist);
		}
	}

	return error;
}
 
Example #13
Source File: HTMSensorTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private Parameters getTestEncoderParams() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
        null,
        0, // n
        0, // w
        0, 0, 0, 0, null, null, null,
        "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
        fieldEncodings, 
        25, 
        3, 
        0, 0, 0, 0.1, null, null, null, 
        "consumption", "float", "RandomDistributedScalarEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    // This will work also
    //fieldEncodings.get("timestamp").put(KEY.DATEFIELD_FORMATTER.getFieldName(), DateEncoder.FULL_DATE);
            
    Parameters p = Parameters.getEncoderDefaultParameters();
    p.set(KEY.FIELD_ENCODING_MAP, fieldEncodings);
    
    return p;
}
 
Example #14
Source File: HeaderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private ValueList getTestHeaderOff() {
    return new ValueList() {
        Tuple[] ta = new Tuple[] {
            new Tuple("timestamp", "consumption"),
            new Tuple("datetime", "float"),
            new Tuple("T"),
        };

        @Override
        public Tuple getRow(int row) {
            return ta[row];
        }

        @Override
        public int size() {
            return ta.length;
        }
    };
}
 
Example #15
Source File: HeaderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private ValueList getTestHeaderSeq() {
    return new ValueList() {
        Tuple[] ta = new Tuple[] {
            new Tuple("timestamp", "consumption"),
            new Tuple("datetime", "float"),
            new Tuple("T", "B", "S"),
        };

        @Override
        public Tuple getRow(int row) {
            return ta[row];
        }

        @Override
        public int size() {
            return ta.length;
        }
    };
}
 
Example #16
Source File: HeaderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private ValueList getTestHeaderReset() {
    return new ValueList() {
        Tuple[] ta = new Tuple[] {
            new Tuple("timestamp", "consumption"),
            new Tuple("datetime", "float"),
            new Tuple("T", "B", "R"),
        };

        @Override
        public Tuple getRow(int row) {
            return ta[row];
        }

        @Override
        public int size() {
            return ta.length;
        }
    };
}
 
Example #17
Source File: HeaderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private ValueList getTestHeaderLearn() {
    return new ValueList() {
        Tuple[] ta = new Tuple[] {
            new Tuple("timestamp", "consumption"),
            new Tuple("datetime", "float"),
            new Tuple("T", "B", "L"),
        };

        @Override
        public Tuple getRow(int row) {
            return ta[row];
        }

        @Override
        public int size() {
            return ta.length;
        }
    };
}
 
Example #18
Source File: NetworkTestHarness.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
            null,
            0, // n
            0, // w
            0, 0, 0, 0, null, null, null,
            "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
            fieldEncodings, 
            25, 
            3, 
            0, 0, 0, 0.1, null, null, null, 
            "consumption", "float", "RandomDistributedScalarEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    return fieldEncodings;
}
 
Example #19
Source File: NetworkTestHarness.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getNetworkDemoFieldEncodingMap() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
            null,
            0, // n
            0, // w
            0, 0, 0, 0, null, null, null,
            "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
            fieldEncodings, 
            50, 
            21, 
            0, 100, 0, 0.1, null, Boolean.TRUE, null, 
            "consumption", "float", "ScalarEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(21,9.5)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    return fieldEncodings;
}
 
Example #20
Source File: ParametersTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the Hot Gym encoder setup.
 * @return
 */
public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap_varyDateFieldTupleValue() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
                    null,
                    0, // n
                    0, // w
                    0, 0, 0, 0, null, null, null,
                    "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
                    fieldEncodings, 
                    25, 
                    3, 
                    0, 0, 0, 0.1, null, null, null, 
                    "consumption", "float", "RandomDistributedScalarEncoder");

    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 2.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");

    return fieldEncodings;
}
 
Example #21
Source File: PersistenceAPITest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
private Parameters getTestEncoderParams() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
        null,
        0, // n
        0, // w
        0, 0, 0, 0, null, null, null,
        "timestamp", "datetime", "DateEncoder");

    fieldEncodings = setupMap(
        fieldEncodings, 
        25, 
        3, 
        0, 0, 0, 0.1, null, null, null, 
        "consumption", "float", "RandomDistributedScalarEncoder");

    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");

    Parameters p = Parameters.getEncoderDefaultParameters();
    p.set(KEY.FIELD_ENCODING_MAP, fieldEncodings);

    return p;
}
 
Example #22
Source File: ParametersTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
     * Returns the Hot Gym encoder setup.
     * @return
     */
    public static Map<String, Map<String, Object>> getHotGymFieldEncodingMap_varyDateFieldKey() {
        Map<String, Map<String, Object>> fieldEncodings = setupMap(
                        null,
                        0, // n
                        0, // w
                        0, 0, 0, 0, null, null, null,
                        "timestamp", "datetime", "DateEncoder");
        fieldEncodings = setupMap(
                        fieldEncodings, 
                        25, 
                        3, 
                        0, 0, 0, 0.1, null, null, null, 
                        "consumption", "float", "RandomDistributedScalarEncoder");

//        fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
        fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
        fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");

        return fieldEncodings;
    }
 
Example #23
Source File: RandomDistributedScalarEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Test that the getWidth, getDescription, and getDecoderOutputFieldTypes
    * methods work.
 */
@Test
public void testGetMethods() {
	builder = RandomDistributedScalarEncoder.builder()
			.name("theName")
			.resolution(1)
			.n(500);
	rdse = builder.build();

	assertEquals("getWidth doesn't return the correct result", 500,
			rdse.getWidth());
	assertEquals(
			"getDescription doesn't return the correct result",
			new ArrayList<Tuple>(Arrays.asList(new Tuple[] { new Tuple("theName",
					0) })), rdse.getDescription());

	assertThat(
			"getDecoderOutputFieldTypes doesn't return the correct result",
			rdse.getDecoderOutputFieldTypes(),
			is(equalTo(new LinkedHashSet<>(Arrays.asList(FieldMetaType.FLOAT, FieldMetaType.INTEGER)))));
}
 
Example #24
Source File: MultiEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void runScalarTests(MultiEncoder me) {
	// should be 7 bits wide
	// use of forced=true is not recommended, but here for readability, see scalar.py
	int[] expected = new int[]{0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
	Map<String, Object> d = new HashMap<String, Object>();
	d.put("dow",  3.);
	d.put("myval",  10.);
	int[] output = me.encode(d);

	assertTrue(Arrays.equals(expected, output));
	
	// Check decoding
	Tuple decoded = me.decode(output, "");
	Map<String, RangeList> fields = (HashMap<String, RangeList>) decoded.get(0);
	assertEquals(fields.keySet().size(), 2);
	
	MinMax minMax = fields.get("aux").getRange(0);
	assertTrue(minMax.toString().equals(new MinMax(10.0, 10.0).toString()));
	
	minMax = fields.get("day of week").getRange(0);
	assertTrue(minMax.toString().equals(new MinMax(3.0, 3.0).toString()));
}
 
Example #25
Source File: ScalarEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Test closenessScores for a periodic encoder
 */
@Test
public void testCloseness() {
	setUp();
	builder.name("day of week")
        .w(7)
        .radius(1.0)
        .minVal(0.0)
        .maxVal(7.0)
        .periodic(true)
        .forced(true);
	initSE();
	
	TDoubleList expValues = new TDoubleArrayList(new double[] { 2, 4, 7 });
	TDoubleList actValues = new TDoubleArrayList(new double[] { 4, 2, 1 });
	
	TDoubleList scores = se.closenessScores(expValues, actValues, false);
	for(Tuple t : ArrayUtils.zip(Arrays.asList(2, 2, 1), Arrays.asList(scores.get(0)))) {
		double a = (int)t.get(0);
		double b = (double)t.get(1);
		assertTrue(a == b);
	}
}
 
Example #26
Source File: RunLayer.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
public Tuple encodingStep(DateTime timestamp, double value, boolean isVerbose) {
            if(valueEncoder == null) {
                List<EncoderTuple> encoderTuples = encoder.getEncoders(encoder);
                valueEncoder = encoderTuples.get(0).getEncoder(); 
            }
            
            Map<String, Object> encodingInput = new HashMap<String, Object>();
            encodingInput.put("value", value);
            encodingInput.put("timestamp",  timestamp);
            int[] encoding = encoder.encode(encodingInput);
            if(isVerbose) {
                System.out.println("ScalarEncoder Output = " + Arrays.toString(encoding));
            }
            
            int bucketIdx = valueEncoder.getBucketIndices((double)value)[0];
//            writeEncOutput(encoding);
            return new Tuple(encoding, bucketIdx);
        }
 
Example #27
Source File: RunLayer.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Input through the Temporal Memory
 * @param sparseSPOutput
 * @param learn
 * @param isVerbose
 * @return Tuple { active cell indices, previous predicted column indices, predicted column indices }
 */
public Tuple tmStep(int[] sparseSPOutput, boolean learn, boolean isVerbose) {
    // Input into the Temporal Memory
    ComputeCycle cc = tm.compute(connections, sparseSPOutput, learn);
    int[] activeCellIndices = cc.activeCells().stream().mapToInt(c -> c.getIndex()).sorted().toArray();
    int[] predColumnIndices = SDR.cellsAsColumnIndices(cc.predictiveCells(), connections.getCellsPerColumn());
    int[] activeColumns = Arrays.stream(activeCellIndices)
        .map(cell -> cell / connections.getCellsPerColumn())
        .distinct()
        .sorted()
        .toArray();
    if(isVerbose) {
        System.out.println("          TemporalMemory Input: " + Arrays.toString(sparseSPOutput));
        System.out.println("TemporalMemory prev. predicted: " + Arrays.toString(prevPredictedCols));
        System.out.println("         TemporalMemory active: " + Arrays.toString(activeColumns));
    }
    
    return new Tuple(activeCellIndices, prevPredictedCols, predColumnIndices);
}
 
Example #28
Source File: KryoSerializerTest.java    From flink-htm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testReadWrite() throws Exception {

    // write numerous objects to the stream, to verify that the read buffers
    // aren't too greedy

    Tuple expected1 = new Tuple(42);
    Tuple expected2 = new Tuple(101);

    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    Output output = new Output(baout);
    kryo.writeObject(output, expected1);
    kryo.writeObject(output, expected2);
    output.close();

    ByteArrayInputStream bain = new ByteArrayInputStream(baout.toByteArray());
    Input input = new Input(bain);

    Tuple actual1 = kryo.readObject(input, Tuple.class);
    assertNotSame(expected1, actual1);
    assertEquals(expected1, actual1);

    Tuple actual2 = kryo.readObject(input, Tuple.class);
    assertNotSame(expected2, actual2);
    assertEquals(expected2, actual2);
}
 
Example #29
Source File: KryoSerializerTest.java    From flink-htm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCopy() throws Exception {
    Tuple expected = new Tuple(42);

    Tuple actual = kryo.copy(expected);
    assertNotSame(expected, actual);
    assertEquals(expected, actual);
}
 
Example #30
Source File: HTMSensorTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
private Parameters getCategoryEncoderParams() {
    Map<String, Map<String, Object>> fieldEncodings = setupMap(
        null,
        0, // n
        0, // w
        0, 0, 0, 0, null, null, null,
        "timestamp", "datetime", "DateEncoder");
    fieldEncodings = setupMap(
        fieldEncodings, 
        25, 
        3, 
        0, 0, 0, 0.1, null, null, null, 
        "consumption", "float", "RandomDistributedScalarEncoder");
    fieldEncodings = setupMap(
        fieldEncodings, 
        25, 
        3, 
        0, 0, 0, 0.0, null, null, Boolean.TRUE, 
        "type", "list", "SDRCategoryEncoder");
    
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_DOFW.getFieldName(), new Tuple(1, 1.0)); // Day of week
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_TOFD.getFieldName(), new Tuple(5, 4.0)); // Time of day
    fieldEncodings.get("timestamp").put(KEY.DATEFIELD_PATTERN.getFieldName(), "MM/dd/YY HH:mm");
    
    String categories = "ES;S1;S2;S3;S4;S5;S6;S7;S8;S9;S10;S11;S12;S13;S14;S15;S16;S17;S18;S19;GB;US";
    fieldEncodings.get("type").put(KEY.CATEGORY_LIST.getFieldName(), categories);
    
    // This will work also
    //fieldEncodings.get("timestamp").put(KEY.DATEFIELD_FORMATTER.getFieldName(), DateEncoder.FULL_DATE);
            
    Parameters p = Parameters.getEncoderDefaultParameters();
    p.set(KEY.FIELD_ENCODING_MAP, fieldEncodings);
    
    return p;
}