Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoordinateEncoder: java.lang.IllegalStateException: java.lang.ClassCastException: java.lang.Double cannot be cast to [I #543

Open
matheus-fatguys opened this issue Sep 19, 2018 · 0 comments · May be fixed by #544

Comments

@matheus-fatguys
Copy link

matheus-fatguys commented Sep 19, 2018

I'm experimenting with GeospatialCoordinateEncoder, CoordinateEncoder and FileSensor
Everything works fine with GeospatialCoordinateEncoder but CoordinateEncoder throws the mentioned exception:
java.lang.IllegalStateException: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
I found that exception is thrown in this line:
List<int[]> neighs = neighbors((int[])inputData.get(0), (double)inputData.get(1));

That's because inputData.get(0) is a double value parsed from the input file line string.

At GeospatialCoordinateEncoder the following lines convert the double value to int and than callsCoordinateEncoder.encodeIntoArray without a problem.

@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);
	}
	
	public int[] coordinateForPosition(double longitude, double lattitude) {
		double[] coordinate = toMercator(longitude, lattitude);
		coordinate[0] /= scale;
		coordinate[1] /= scale;
		return new int[] { (int)coordinate[0], (int)coordinate[1] };
	}

I made a change on FieldMetaType as follows:

	/**
	 * Returns the input type for the {@code FieldMetaType} that this is...
	 * @param input
	 * @param enc
	 * @return
	 */
	@SuppressWarnings("unchecked")
    public <T> T decodeType(String input, Encoder<?> enc) {
	    switch(this) {
            case LIST : 
            case STRING : return (T)input;
            case DATETIME : return (T)((DateEncoder)enc).parse(input);
            case BOOLEAN : return (T)(Boolean.valueOf(input) == true ? new Double(1) : new Double(0));
            case COORD :{
            	String[] parts = input.split("[\\s]*\\;[\\s]*");
                int[] coord =new int[2];
                coord[0]=Integer.parseInt(parts[0]);
                coord[1]=Integer.parseInt(parts[1]);
            	return (T)new Tuple(coord, Double.parseDouble(parts[2]));
            } 
            case GEO :  {
            	String[] parts = input.split("[\\s]*\\;[\\s]*");
            	return (T)new Tuple(Double.parseDouble(parts[0]), Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
            }
            case INTEGER : 
            case FLOAT : return (T)new Double(input);
            case SARR :
            case DARR: { 
                return (T)Arrays.stream(input.replace("[","").replace("]","")
                    .split("[\\s]*\\,[\\s]*")).mapToInt(Integer::parseInt).toArray();
            }
            default : return null;
        }
	}

And other change on CoordinateEncoder as follows:

/**
* {@inheritdoc}
*/
@OverRide
public void encodeIntoArray(Tuple inputData, int[] output) {
List<int[]> neighs = neighbors((int[])inputData.get(0), Math.max((double)inputData.get(1), w));
int[][] neighbors = new int[neighs.size()][];
for(int i = 0;i < neighs.size();i++) neighbors[i] = neighs.get(i);

	int[][] winners = topWCoordinates(this, neighbors, w);

	for(int i = 0;i < winners.length;i++) {
		int bit = bitForCoordinate(winners[i], n);
		output[bit] = 1;
	}
}

And now it's working and doesn't throw any exceptions.

May I request a pull to submit my code?

My .csv file is like this:

timestamp,position
datetime,coord
T,
09/18/2018 00:00,0;1;1
09/18/2018 00:01,0.0999983333416666;0.999950000416665;0.00999995833338542
09/18/2018 00:02,0.199986666933331;0.999800006666578;0.00999995833338542
09/18/2018 00:03,0.299955002024957;0.999550033748988;0.00999995833338541
09/18/2018 00:04,0.399893341866342;0.999200106660978;0.00999995833338542

java.lang.RuntimeException: Unhandled Exception in Sensor Layer [Region 1:Layer 2/3] Thread
at org.numenta.nupic.network.Layer$6.uncaughtException(Layer.java:2054)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)
Caused by: java.lang.IllegalStateException: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:105)
at org.numenta.nupic.encoders.Encoder.encode(Encoder.java:625)
at org.numenta.nupic.network.sensor.HTMSensor.input(HTMSensor.java:429)
at org.numenta.nupic.network.sensor.HTMSensor.lambda$getOutputStream$46(HTMSensor.java:362)
at org.numenta.nupic.network.sensor.HTMSensor$$Lambda$29/1349859160.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
at java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$220(StreamSpliterators.java:294)
at java.util.stream.StreamSpliterators$WrappingSpliterator$$Lambda$9/209813603.getAsBoolean(Unknown Source)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at org.numenta.nupic.network.sensor.HTMSensor$Copy.hasNext(HTMSensor.java:280)
at java.util.Iterator.forEachRemaining(Iterator.java:115)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.numenta.nupic.network.Layer$5.run(Layer.java:2037)
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:167)
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:34)
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:103)
... 22 more
java.lang.RuntimeException: Unhandled Exception in Sensor Layer [Region 1:Layer 2/3] Thread
at org.numenta.nupic.network.Layer$6.uncaughtException(Layer.java:2054)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)
Caused by: java.lang.IllegalStateException: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:105)
at org.numenta.nupic.encoders.Encoder.encode(Encoder.java:625)
at org.numenta.nupic.network.sensor.HTMSensor.input(HTMSensor.java:429)
at org.numenta.nupic.network.sensor.HTMSensor.lambda$getOutputStream$46(HTMSensor.java:362)
at org.numenta.nupic.network.sensor.HTMSensor$$Lambda$29/1349859160.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
at java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$220(StreamSpliterators.java:294)
at java.util.stream.StreamSpliterators$WrappingSpliterator$$Lambda$9/209813603.getAsBoolean(Unknown Source)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at org.numenta.nupic.network.sensor.HTMSensor$Copy.hasNext(HTMSensor.java:280)
at java.util.Iterator.forEachRemaining(Iterator.java:115)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.numenta.nupic.network.Layer$5.run(Layer.java:2037)
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:167)
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:34)
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:103)
... 22 more
java.lang.RuntimeException: Unhandled Exception in Sensor Layer [Region 1:Layer 2/3] Thread
at org.numenta.nupic.network.Layer$6.uncaughtException(Layer.java:2054)
at java.lang.Thread.dispatchUncaughtException(Thread.java:1952)
Caused by: java.lang.IllegalStateException: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:105)
at org.numenta.nupic.encoders.Encoder.encode(Encoder.java:625)
at org.numenta.nupic.network.sensor.HTMSensor.input(HTMSensor.java:429)
at org.numenta.nupic.network.sensor.HTMSensor.lambda$getOutputStream$46(HTMSensor.java:362)
at org.numenta.nupic.network.sensor.HTMSensor$$Lambda$29/1349859160.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
at java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$220(StreamSpliterators.java:294)
at java.util.stream.StreamSpliterators$WrappingSpliterator$$Lambda$9/209813603.getAsBoolean(Unknown Source)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at org.numenta.nupic.network.sensor.HTMSensor$Copy.hasNext(HTMSensor.java:280)
at java.util.Iterator.forEachRemaining(Iterator.java:115)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.numenta.nupic.network.Layer$5.run(Layer.java:2037)
Caused by: java.lang.ClassCastException: java.lang.Double cannot be cast to [I
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:167)
at org.numenta.nupic.encoders.CoordinateEncoder.encodeIntoArray(CoordinateEncoder.java:34)
at org.numenta.nupic.encoders.MultiEncoder.encodeIntoArray(MultiEncoder.java:103)
... 22 more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant