[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270482866
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/storage/DenseVectorStorage.java
 ##
 @@ -56,19 +61,90 @@ public DenseVectorStorage(double[] data) {
 this.data = data;
 }
 
+/**
+ * @param data Backing data array.
+ */
+public DenseVectorStorage(Serializable[] data) {
+assert data != null;
+
+this.rawData = data;
+}
+
 /** {@inheritDoc} */
 @Override public int size() {
-return data == null ? 0 : data.length;
+if (data == null && rawData == null)
+return 0;
+else {
+if (data != null)
+return data.length;
+if (rawData != null)
+return rawData.length;
+else
+throw new IllegalStateException();
+}
+}
+
+/**
+ * Copies values between internal arrays If toNumericRepresentation = true 
then method tries cast all serializable
+ * objects to double, initializes double array with converted values and 
cleans raw data array. If
+ * toNumericRepresentation = false then method copies double array to raw 
data array and cleans double array.
+ *
+ * @param toNumericRepresentation To numeric representation.
+ */
+private void swap(boolean toNumericRepresentation) {
+A.ensure(data == null || rawData == null, "data == null || rawData == 
null");
+if (data == null && rawData == null)
+return;
+
+if (toNumericRepresentation) {
+if (data != null)
+return;
+
+data = new double[rawData.length];
+for (int i = 0; i < rawData.length; i++)
+data[i] = ((Number)rawData[i]).doubleValue();
+rawData = null;
+} else {
+if (rawData != null)
+return;
+
+rawData = new Serializable[data.length];
+for (int i = 0; i < rawData.length; i++)
+rawData[i] = data[i];
+data = null;
+}
 }
 
 /** {@inheritDoc} */
 @Override public double get(int i) {
-return data[i];
+if (data != null)
+return data[i];
+
+Serializable v = rawData[i];
+if (v == null)
+return 0.0;
+else
+return ((Number)rawData[i]).doubleValue();
+}
+
+/** {@inheritDoc} */
+@Override public  T getRaw(int i) {
+swap(false);
+return (T)rawData[i];
 }
 
 /** {@inheritDoc} */
 @Override public void set(int i, double v) {
-data[i] = v;
+if (data != null)
+data[i] = v;
+else
+rawData[i] = v;
+}
+
+/** {@inheritDoc} */
+@Override public  void setRaw(int i, T v) {
+swap(false);
 
 Review comment:
   Would it be better to keep two keep both arrays "active" and bitset that 
tells for each index which array contains actual value?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270486954
 
 

 ##
 File path: 
modules/ml/src/test/java/org/apache/ignite/ml/math/primitives/vector/storage/DenseVectorStorageTest.java
 ##
 @@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.vector.storage;
+
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.primitives.vector.VectorStorage;
+import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector;
+
+/**
+ * Tests for DenseVectorStorage.
+ */
+public class DenseVectorStorageTest extends AbstractStorageTest {
+
+/** {@inheritDoc} */
+@Override protected boolean isNumericVector(VectorStorage storage) {
+try {
+Field f = storage.getClass().getDeclaredField("data");
 
 Review comment:
   Does it make sense to make protected getter `getData`? It would help it case 
field is renamed.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270477186
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/storage/DenseVectorStorage.java
 ##
 @@ -56,19 +61,90 @@ public DenseVectorStorage(double[] data) {
 this.data = data;
 }
 
+/**
+ * @param data Backing data array.
+ */
+public DenseVectorStorage(Serializable[] data) {
+assert data != null;
+
+this.rawData = data;
+}
+
 /** {@inheritDoc} */
 @Override public int size() {
-return data == null ? 0 : data.length;
+if (data == null && rawData == null)
+return 0;
+else {
+if (data != null)
+return data.length;
+if (rawData != null)
+return rawData.length;
+else
+throw new IllegalStateException();
 
 Review comment:
   Unreachable statement?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270475481
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/AbstractVector.java
 ##
 @@ -297,6 +342,22 @@ protected Element makeElement(int idx) {
 return this;
 }
 
+/** {@inheritDoc} */
+@Override public  Vector setRaw(int idx, T val) {
 
 Review comment:
   Why not just protected void storageSetRaw(int i, Serializable v)?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270474376
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/AbstractVector.java
 ##
 @@ -125,6 +126,20 @@ protected void storageSet(int i, double v) {
 maxElm = minElm = null;
 }
 
+/**
+ * @param i Index.
+ * @param v Value.
+ */
+protected  void storageSetRaw(int i, T v) {
 
 Review comment:
   Why not just `protected void storageSetRaw(int i, Serializable v)`?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270485159
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/storage/SparseVectorStorage.java
 ##
 @@ -105,7 +70,16 @@ public int getAccessMode() {
 
 /** {@inheritDoc} */
 @Override public double get(int i) {
-return sto.getOrDefault(i, 0.0);
+Serializable obj = sto.get(i);
+if (obj == null)
+return 0.0;
 
 Review comment:
   Why not `Double.NaN`?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270474877
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/AbstractVector.java
 ##
 @@ -160,6 +183,18 @@ protected void checkIndex(int idx) {
 return storageGet(idx);
 }
 
+/** {@inheritDoc} */
+@Override public  T getRaw(int idx) {
+checkIndex(idx);
+
+return sto.getRaw(idx);
+}
+
+/** {@inheritDoc} */
+@Override public  T getRawX(int idx) {
 
 Review comment:
   Why somewhere you use `checkIndex(idx)`, but somewhere not?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270474582
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/AbstractVector.java
 ##
 @@ -133,6 +148,14 @@ protected double storageGet(int i) {
 return sto.get(i);
 }
 
+/**
 
 Review comment:
   Javadoc?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270479520
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/storage/DenseVectorStorage.java
 ##
 @@ -56,19 +61,90 @@ public DenseVectorStorage(double[] data) {
 this.data = data;
 }
 
+/**
+ * @param data Backing data array.
+ */
+public DenseVectorStorage(Serializable[] data) {
+assert data != null;
+
+this.rawData = data;
+}
+
 /** {@inheritDoc} */
 @Override public int size() {
-return data == null ? 0 : data.length;
+if (data == null && rawData == null)
+return 0;
+else {
+if (data != null)
+return data.length;
+if (rawData != null)
+return rawData.length;
+else
+throw new IllegalStateException();
+}
+}
+
+/**
+ * Copies values between internal arrays If toNumericRepresentation = true 
then method tries cast all serializable
+ * objects to double, initializes double array with converted values and 
cleans raw data array. If
+ * toNumericRepresentation = false then method copies double array to raw 
data array and cleans double array.
+ *
+ * @param toNumericRepresentation To numeric representation.
+ */
+private void swap(boolean toNumericRepresentation) {
 
 Review comment:
   Lets make two methods `toNumericArray` and `toGenericArray`. Looks like it 
will be more clear.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270482092
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/storage/DenseVectorStorage.java
 ##
 @@ -56,19 +61,90 @@ public DenseVectorStorage(double[] data) {
 this.data = data;
 }
 
+/**
+ * @param data Backing data array.
+ */
+public DenseVectorStorage(Serializable[] data) {
+assert data != null;
+
+this.rawData = data;
+}
+
 /** {@inheritDoc} */
 @Override public int size() {
-return data == null ? 0 : data.length;
+if (data == null && rawData == null)
+return 0;
+else {
+if (data != null)
+return data.length;
+if (rawData != null)
+return rawData.length;
+else
+throw new IllegalStateException();
+}
+}
+
+/**
+ * Copies values between internal arrays If toNumericRepresentation = true 
then method tries cast all serializable
+ * objects to double, initializes double array with converted values and 
cleans raw data array. If
+ * toNumericRepresentation = false then method copies double array to raw 
data array and cleans double array.
+ *
+ * @param toNumericRepresentation To numeric representation.
+ */
+private void swap(boolean toNumericRepresentation) {
+A.ensure(data == null || rawData == null, "data == null || rawData == 
null");
+if (data == null && rawData == null)
+return;
+
+if (toNumericRepresentation) {
+if (data != null)
+return;
+
+data = new double[rawData.length];
+for (int i = 0; i < rawData.length; i++)
+data[i] = ((Number)rawData[i]).doubleValue();
 
 Review comment:
   NpE?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [ignite] dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML Vectors should work with all Serializable objects besides double

2019-03-29 Thread GitBox
dmitrievanthony commented on a change in pull request #6378: IGNITE-11647: ML 
Vectors should work with all Serializable objects besides double
URL: https://github.com/apache/ignite/pull/6378#discussion_r270474010
 
 

 ##
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/vector/AbstractVector.java
 ##
 @@ -125,6 +126,20 @@ protected void storageSet(int i, double v) {
 maxElm = minElm = null;
 }
 
+/**
 
 Review comment:
   Javadoc?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services