Java Code Examples for org.numenta.nupic.util.Tuple#get()

The following examples show how to use org.numenta.nupic.util.Tuple#get() . 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: 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 2
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 3
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 4
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 5
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Return a description of the given bit in the encoded output.
    * This will include the field name and the offset within the field.
 * @param bitOffset  	Offset of the bit to get the description of
 * @param formatted     If True, the bitOffset is w.r.t. formatted output,
    *                     	which includes separators
 *
    * @return tuple(fieldName, offsetWithinField)
 */
public Tuple encodedBitDescription(int bitOffset, boolean formatted) {
	//Find which field it's in
	List<Tuple> description = getDescription();
	int len = description.size();
	String prevFieldName = null;
	int prevFieldOffset = -1;
	int offset = -1;
	for(int i = 0;i < len;i++) {
		Tuple t = description.get(i);//(name, offset)
		if(formatted) {
			offset = ((int)t.get(1)) + 1;
			if(bitOffset == offset - 1) {
				prevFieldName = "separator";
				prevFieldOffset = bitOffset;
			}
		}
		if(bitOffset < offset) break;
	}
	// Return the field name and offset within the field
    // return (fieldName, bitOffset - fieldOffset)
	int width = formatted ? getDisplayWidth() : getWidth();

	if(prevFieldOffset == -1 || bitOffset > getWidth()) {
		throw new IllegalStateException("Bit is outside of allowable range: " +
			String.format("[0 - %d]", width));
	}
	return new Tuple(prevFieldName, bitOffset - prevFieldOffset);
}
 
Example 6
Source File: MultiEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Convenience method to return the {@code Encoder} contained within this 
 * {@link MultiEncoder}, of a specific type.
 * @param fmt   the {@link FieldMetaType} specifying the type to return.
 * @return  the Encoder of the specified type or null if one isn't found.
 */
@SuppressWarnings("unchecked")
public <T extends Encoder<?>> T getEncoderOfType(FieldMetaType fmt) {
    Encoder<?> retVal = null;
    for(Tuple t : getEncoders(this)) {
        Encoder<?> enc = (Encoder<?>)t.get(1);
        Set<FieldMetaType> subTypes = enc.getDecoderOutputFieldTypes();
        if(subTypes.contains(fmt)) {
            retVal = enc; break;
        }
    }
    
    return (T)retVal;
}
 
Example 7
Source File: GeospatialCoordinateEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void encodeIntoArray(Tuple inputData, int[] output) {
	double longitude = (double)inputData.get(0);
	double lattitude = (double)inputData.get(1);
	double speed = (double)inputData.get(2);
	int[] coordinate = coordinateForPosition(longitude, lattitude);
	double radius = radiusForSpeed(speed);
	
	super.encodeIntoArray(new Tuple(coordinate, radius), output);
}
 
Example 8
Source File: DateEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Decoding date
 */
@Test
public void testDecoding() {
    setUp();
    initDE();

    //TODO Why null is needed?
    Tuple decoded = de.decode(bits, null);

    Map<String, RangeList> fieldsMap = (Map<String, RangeList>)decoded.get(0);
    List<String> fieldsOrder = (List<String>)decoded.get(1);

    assertNotNull(fieldsMap);
    assertNotNull(fieldsOrder);
    assertEquals(4, fieldsMap.size());

    Map<String, Double> expectedMap = new HashMap<>();
    expectedMap.put("season", 305.0);
    expectedMap.put("time of day", 14.4);
    expectedMap.put("day of week", 3.0);
    expectedMap.put("weekend", 0.0);

    for (String key : expectedMap.keySet()) {
        double expected = expectedMap.get(key);
        RangeList actual = fieldsMap.get(key);
        assertEquals(1, actual.size());
        MinMax minmax = actual.getRange(0);
        assertEquals(expected, minmax.min(), de.getResolution());
        assertEquals(expected, minmax.max(), de.getResolution());
    }

    System.out.println(decoded.toString());
    System.out.println(String.format("decodedToStr=>%s", de.decodedToStr(decoded)));
}
 
Example 9
Source File: MultiEncoderAssembler.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Initializes the {@link DateEncoder.Builder} specified
 * @param b         the builder on which to set the mapping.
 * @param m         the map containing the values
 * @param key       the key to be set.
 */
@SuppressWarnings("unchecked")
private static void setDateFieldBits(DateEncoder.Builder b, Map<String, Object> m, String key) {
    Tuple t = (Tuple)m.get(key);
    switch(key) {
        case "season" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.season((int)t.get(0), (double)t.get(1));
            }else{
                b.season((int)t.get(0));
            }
            break;
        }
        case "dayOfWeek" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.dayOfWeek((int)t.get(0), (double)t.get(1));
            }else{
                b.dayOfWeek((int)t.get(0));
            }
            break;
        }
        case "weekend" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.weekend((int)t.get(0), (double)t.get(1));
            }else{
                b.weekend((int)t.get(0));
            }
            break;
        }
        case "holiday" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.holiday((int)t.get(0), (double)t.get(1));
            }else{
                b.holiday((int)t.get(0));
            }
            break;
        }
        case "timeOfDay" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.timeOfDay((int)t.get(0), (double)t.get(1));
            }else{
                b.timeOfDay((int)t.get(0));
            }
            break;
        }
        case "customDays" : {
            if(t.size() > 1 && ((double)t.get(1)) > 0.0) {
                b.customDays((int)t.get(0), (List<String>)t.get(1));
            }else{
                b.customDays((int)t.get(0));
            }
            break;
        }
        
        default: break;
    }
}
 
Example 10
Source File: DateEncoder.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
private boolean isValidEncoderPropertyTuple(Tuple encoderPropertyTuple) {
    return encoderPropertyTuple != null && (int)encoderPropertyTuple.get(0) != 0;
}
 
Example 11
Source File: DateEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Test weekend encoder
 */
@Test
public void testWeekend() {
    //use of forced is not recommended, used here for readability, see ScalarEncoder
    DateEncoder e = DateEncoder.builder().customDays(21, Arrays.asList(
            "sat", "sun", "fri"
    )).forced(true).build();
    DateEncoder mon = DateEncoder.builder().customDays(21, Arrays.asList(
            "Monday"
    )).forced(true).build();
    DateEncoder e2 = DateEncoder.builder().weekend(21, 1).forced(true).build();
    //DateTime d = new DateTime(1988,5,29,20,0);
    DateTime d = new DateTime(1988,5,29,20,0);

    assertArrayEquals(e.encode(d), e2.encode(d));

    for (int i = 0; i < 300; i++) {
        DateTime curDate = d.plusDays(i + 1);
        assertArrayEquals(e.encode(curDate), e2.encode(curDate));

        //Make sure
        Tuple decoded = mon.decode(mon.encode(curDate), null);

        Map<String, RangeList> fieldsMap = (Map<String, RangeList>)decoded.get(0);
        List<String> fieldsOrder = (List<String>)decoded.get(1);

        assertNotNull(fieldsMap);
        assertNotNull(fieldsOrder);
        assertEquals(1, fieldsMap.size());

        RangeList range = fieldsMap.get("Monday");
        assertEquals(1, range.size());
        assertEquals(1, ((List<MinMax>)range.get(0)).size());
        MinMax minmax = range.getRange(0);
        System.out.println("DateEncoderTest.testWeekend(): minmax.min() = " + minmax.min());

        if(minmax.min() == 1.0) {
            assertEquals(1, curDate.getDayOfWeek());
        } else {
            assertNotEquals(1, curDate.getDayOfWeek());
        }
    }
}
 
Example 12
Source File: SpatialPoolerCompatibilityTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
     * Test the Java output against stored Python output allowing "learning"
     * to be randomly turned on and off. Uses the {@link UniversalRandom} RNG
     * in both the Python and Java versions to enable identical output generation.
     */
    @Test
    public void testCompatability1() {
        int numRecords = 100;
        
        Tuple tuple = createSP();
        Connections c = (Connections)tuple.get(0);
        SpatialPooler sp = (SpatialPooler)tuple.get(1);
        
        int[][] potentials = c.getPotentials();
        int[][] connecteds = c. getConnecteds();
        double[][] perms = c.getPermanences();

        /////////////////////////////////////////////////////////////////////////////
        // Used to write the 3 needed outputs to be inserted in the initialization //
        // part of the SP compatibility test                                       //
        /////////////////////////////////////////////////////////////////////////////
//        for(int[] pots : potentials) {
//            System.out.println(Arrays.toString(pots));
//        }
//        System.out.println("\n\n");
//        for(int[] conns : connecteds) {
//            System.out.println(Arrays.toString(conns));
//        }
//        System.out.println("\n\n");
//        for(double[] perm : perms) {
//            System.out.println(Arrays.toString(perm));
//        }
        
        int[][] expectedInitialPotentials = getPythonInitialPotentialMapping1();
        int[][] expectedInitialColumnConnects = getPythonInitialColumnConnections();
        double[][] expectedInitialPerms = getPythonInitialPermanences1();
        
        assertTrue(
            IntStream.range(0, potentials.length)
                .allMatch(i -> Arrays.equals(potentials[i], expectedInitialPotentials[i])));
        assertTrue(
            IntStream.range(0, potentials.length)
                .allMatch(i -> Arrays.equals(connecteds[i], expectedInitialColumnConnects[i])));
        assertTrue(
            IntStream.range(0, perms.length)
                .allMatch(d -> {
                    return IntStream.range(0, perms[d].length).allMatch(i -> {
                        return areEqualDouble(perms[d][i], expectedInitialPerms[d][i], 4); 
                    });
                }));
            
        // Set the percentage of 1's in the test inputs
        double sparsity = 0.4d;
        int[][] inputMatrix = new UniversalRandom(42).binDistrib(numRecords, c.getNumInputs(), sparsity);
        int[][] pythonInputMatrix = getPythonInputs1();
        assertTrue(
            IntStream.range(0, numRecords)
                .allMatch(i -> Arrays.equals(inputMatrix[i], pythonInputMatrix[i])));
        
        runSideBySide(sp, c, pythonInputMatrix, numRecords, new SpatialPoolerCompatibilityActives(), new SpatialPoolerCompatibilityPermanences());
    }