Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/main/java/de/dmi3y/behaiv/kernel/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class Kernel {

Expand All @@ -32,12 +33,12 @@ public void setId(String id) {


//list<features>, label
protected ArrayList<Pair<ArrayList<Double>, String>> data = new ArrayList<>();
protected List<Pair<List<Double>, String>> data = new ArrayList<>();


public abstract boolean isEmpty();

public abstract void fit(ArrayList<Pair<ArrayList<Double>, String>> data);
public abstract void fit(List<Pair<List<Double>, String>> data);

public void fit() {
fit(this.data);
Expand All @@ -51,18 +52,18 @@ public boolean readyToPredict() {
return data.size() > treshold;
}

public void update(ArrayList<Pair<ArrayList<Double>, String>> data) {
public void update(List<Pair<List<Double>, String>> data) {
}

public boolean isPartialFitAllowed() {
return partialFitAllowed;
}

public void updateSingle(ArrayList<Double> features, String label) {
public void updateSingle(List<Double> features, String label) {
data.add(new Pair<>(features, label));
}

public abstract String predictOne(ArrayList<Double> features);
public abstract String predictOne(List<Double> features);

public boolean isAlwaysKeepData() {
return alwaysKeepData;
Expand All @@ -80,7 +81,7 @@ public void save(BehaivStorage storage) throws IOException {
}

public void restore(BehaivStorage storage) throws IOException {
final TypeReference<ArrayList<Pair<ArrayList<Double>, String>>> typeReference = new TypeReference<ArrayList<Pair<ArrayList<Double>, String>>>() {
final TypeReference<List<Pair<List<Double>, String>>> typeReference = new TypeReference<List<Pair<List<Double>, String>>>() {
};
try (final BufferedReader reader = new BufferedReader(new FileReader(storage.getDataFile(id)))) {
final String content = reader.readLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean isEmpty() {


@Override
public void fit(ArrayList<Pair<ArrayList<Double>, String>> data) {
public void fit(List<Pair<List<Double>, String>> data) {
this.data = data;
labels = toDistinctListOfPairValues(data);
if (readyToPredict()) {
Expand Down Expand Up @@ -86,12 +86,12 @@ public boolean readyToPredict() {
}

@Override
public void updateSingle(ArrayList<Double> features, String label) {
public void updateSingle(List<Double> features, String label) {
super.updateSingle(features, label);
}

@Override
public String predictOne(ArrayList<Double> features) {
public String predictOne(List<Double> features) {


final double[] doubles = ArrayUtils.toPrimitive(features.toArray(new Double[0]));
Expand Down Expand Up @@ -153,7 +153,7 @@ public void restore(BehaivStorage storage) throws IOException {
}

try (final BufferedReader reader = new BufferedReader(new FileReader(storage.getNetworkMetadataFile(id)))) {
final TypeReference<ArrayList<String>> typeReference = new TypeReference<ArrayList<String>>() {
final TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {
};
final String labelsData = reader.readLine();
if (labelsData == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/dmi3y/behaiv/provider/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*/
public interface Provider {

public List<String> availableFeatures();
List<String> availableFeatures();

public Observable<List<Double>> subscribeFeatures();
Observable<List<Double>> subscribeFeatures();

public Single<List<Double>> getFeature();
Single<List<Double>> getFeature();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public interface ProviderCallback {

public void onFeaturesCaptured(List<Pair<Double, String>> features);
void onFeaturesCaptured(List<Pair<Double, String>> features);
}
8 changes: 4 additions & 4 deletions src/main/java/de/dmi3y/behaiv/storage/BehaivStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

public interface BehaivStorage {

public boolean containsNetworkFile(String id);
boolean containsNetworkFile(String id);

public File getDataFile(String id) throws IOException;
File getDataFile(String id) throws IOException;

public File getNetworkFile(String id) throws IOException;
File getNetworkFile(String id) throws IOException;

public File getNetworkMetadataFile(String id) throws IOException;
File getNetworkMetadataFile(String id) throws IOException;

}
8 changes: 4 additions & 4 deletions src/main/java/de/dmi3y/behaiv/tools/DataMappingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ private DataMappingUtils() {
}


public static List<String> toDistinctListOfPairValues(List<Pair<ArrayList<Double>, String>> data) {
public static List<String> toDistinctListOfPairValues(List<Pair<List<Double>, String>> data) {
Set<String> setOfValues = new HashSet<>();
for(Pair<ArrayList<Double>, String> arrayListStringPair : data ) {
for(Pair<List<Double>, String> arrayListStringPair : data ) {
setOfValues.add(arrayListStringPair.getValue());
}
return new ArrayList<>(setOfValues);
Expand All @@ -31,10 +31,10 @@ public static ArrayList<Double> toListOfPairKeys(List<Pair<Double, String>> feat
return doubleArrayList;
}

public static double[][] toInput2dArray(List<Pair<ArrayList<Double>, String>> data) {
public static double[][] toInput2dArray(List<Pair<List<Double>, String>> data) {
double[][] input2dArray = new double[data.size()][];
int i = 0;
for(Pair<ArrayList<Double>, String> dataPair : data) {
for(Pair<List<Double>, String> dataPair : data) {
double[] doubleArray = ArrayUtils.toPrimitive(dataPair.getKey().toArray(new Double[0]));
input2dArray[i] = doubleArray;
i++;
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/de/dmi3y/behaiv/LibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static de.dmi3y.behaiv.kernel.KernelTest.WORK;
import static org.junit.Assert.assertEquals;
Expand All @@ -19,7 +20,7 @@ public class LibraryTest {
Behaiv behaiv;
private TestSleepProvider positionProvider;
private TestSleepProvider timeProvider;
private ArrayList<Pair<ArrayList<Double>, String>> data;
private List<Pair<List<Double>, String>> data;

public static final String WORK_SCREEN = "WORK_SCREEN";

Expand All @@ -35,8 +36,8 @@ public void setUp() throws Exception {

@Test
public void behaivTest_basicTestFlow_predictsJob() throws Exception {
for (Pair<ArrayList<Double>, String> fToL : data) {
ArrayList<Double> features = fToL.getKey();
for (Pair<List<Double>, String> fToL : data) {
List<Double> features = fToL.getKey();
timeProvider.next(new Double[]{features.get(0)});
positionProvider.next(new Double[]{features.get(1), features.get(2)});
capture(fToL.getValue());
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/de/dmi3y/behaiv/kernel/KernelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -39,8 +40,8 @@ public void setUp() throws Exception {
@Test
public void setTreshold() {
dummyKernel.setTreshold(1L);
dummyKernel.data.add(new Pair<ArrayList<Double>, String>(null, null));
dummyKernel.data.add(new Pair<ArrayList<Double>, String>(null, null));
dummyKernel.data.add(new Pair<List<Double>, String>(null, null));
dummyKernel.data.add(new Pair<List<Double>, String>(null, null));
boolean readyToPredict = dummyKernel.readyToPredict();
assertTrue(readyToPredict);
dummyKernel.setTreshold(10L);
Expand All @@ -66,9 +67,9 @@ public void kernel_saveAndRestore_expectNormalFlow() throws IOException {

}

public static ArrayList<Pair<ArrayList<Double>, String>> getTrainingData() {
ArrayList<Double> list = new ArrayList<>();
ArrayList<Pair<ArrayList<Double>, String>> data = new ArrayList<>();
public static List<Pair<List<Double>, String>> getTrainingData() {
List<Double> list = new ArrayList<>();
List<Pair<List<Double>, String>> data = new ArrayList<>();


list.add((5 * 60 + 00.0) / (24 * 60));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static de.dmi3y.behaiv.kernel.KernelTest.HOME;
Expand All @@ -34,7 +35,7 @@ public void setUp() throws Exception {

@Test
public void predictOne() {
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
Kernel testKernel = new LogisticRegressionKernel("testId");
testKernel.fit(data);
ArrayList<Double> predictList = new ArrayList<>();
Expand All @@ -58,7 +59,7 @@ public void predictOne() {

@Test
public void storeResults() throws IOException, ClassNotFoundException {
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
Kernel kernel = new LogisticRegressionKernel("testId");
kernel.setId("storeTest");
kernel.fit(data);
Expand All @@ -84,7 +85,7 @@ public void storeResults() throws IOException, ClassNotFoundException {

@Test
public void storeResults_addAdditionalLabel_shouldFail() throws IOException, ClassNotFoundException {
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
Kernel kernel = new LogisticRegressionKernel("testId");
kernel.setAlwaysKeepData(false);
kernel.setId("storeTest");
Expand Down Expand Up @@ -142,7 +143,7 @@ public void storeResults_saveWhenDataIsNull_expectException() throws IOException

@Test
public void storeResults_saveDataAndThenTheta_expectNormalFlow() throws IOException, ClassNotFoundException {
ArrayList<Pair<ArrayList<Double>, String>> data = KernelTest.getTrainingData();
List<Pair<List<Double>, String>> data = KernelTest.getTrainingData();
LogisticRegressionKernel kernel = new LogisticRegressionKernel("storeTest", new Random());
kernel.data = data;
kernel.labels = Arrays.asList("time", "lat", "lon", "headphones");
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/de/dmi3y/behaiv/tools/DataMappingUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DataMappingUtilsTest {

@Test
public void toDistinctListOfPairValues_withInputData_returnsExpectedDistinctListOfPairValues() {
List<Pair<ArrayList<Double>, String>> mockData = Arrays.asList(
List<Pair<List<Double>, String>> mockData = Arrays.asList(
getMockPairListDoubleStringData(), getMockPairListDoubleStringData()
);
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(mockData);
Expand All @@ -25,7 +25,7 @@ public void toDistinctListOfPairValues_withInputData_returnsExpectedDistinctList

@Test
public void toDistinctListOfPairValues_withEmptyListOfInputData_returnsEmptyList() {
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(Collections.<Pair<ArrayList<Double>, String>>emptyList());
List<String> actual = DataMappingUtils.toDistinctListOfPairValues(Collections.<Pair<List<Double>, String>>emptyList());
assertThat(actual, notNullValue());
assertTrue(actual.isEmpty());
}
Expand Down Expand Up @@ -67,7 +67,7 @@ public void toInput2dArray_withInputData_returnsExpected2dArray() {

@Test
public void toInput2dArray_withEmptyListOfInputData_returnsEmpty2dArray() {
double[][] actual = DataMappingUtils.toInput2dArray(Collections.<Pair<ArrayList<Double>, String>>emptyList());
double[][] actual = DataMappingUtils.toInput2dArray(Collections.<Pair<List<Double>, String>>emptyList());
assertThat(actual.length, is(0));
}

Expand All @@ -81,8 +81,8 @@ private Pair<Double, String> getMockPairDoubleStringData() {
return new Pair<>(20d, "Fish");
}

private Pair<ArrayList<Double>, String> getMockPairListDoubleStringData() {
ArrayList<Double> myDoubleList = new ArrayList<>();
private Pair<List<Double>, String> getMockPairListDoubleStringData() {
List<Double> myDoubleList = new ArrayList<>();
myDoubleList.add(20d);
return new Pair<>(myDoubleList, "Potato");
}
Expand Down