From 094e539c7639dd0e23d984dd424571521513003a Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 18 Apr 2019 21:34:24 +0200 Subject: [PATCH 1/6] + erste Versuche mit Weka Signed-off-by: Johannes Theiner --- .../spm/baudas/analysis/ShoppingTimes.java | 58 +++++++++++++++++++ .../baudas/analysis/ShoppingTimesTest.java | 40 +++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java create mode 100644 src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java new file mode 100644 index 0000000..bca82b1 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -0,0 +1,58 @@ +package de.hsel.spm.baudas.analysis; + +import javafx.util.Pair; +import weka.core.Instances; +import weka.filters.Filter; +import weka.filters.unsupervised.attribute.Remove; + +import java.io.File; +import java.util.Map; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class ShoppingTimes implements Analysis, Integer>>{ + + private Instances instances; + private Map, Integer> result; + + public ShoppingTimes(File file) { + instances = load(file); + } + + + @Override + public Map, Integer> getResult() { + if(result == null) { + int dayIndex = 0; + int hourIndex = 1; + int[] keepIndexes = new int[]{5, 6}; + + Remove remove = new Remove(); + try { + remove.setAttributeIndicesArray(keepIndexes); + remove.setInvertSelection(true); + remove.setInputFormat(instances); + instances = Filter.useFilter(instances, remove); + } catch (Exception ex) { + ex.printStackTrace(); + } + + + + + /*Map, Integer> hours = new HashMap<>(); + AttributeStats stats = instances.attributeStats(hourIndex); + for(int i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { + Pair pair = new Pair<>(instances.attribute(hourIndex).value(i), instances.attribute(0).value(i)); + hours.put(pair, stats.nominalCounts[i]); + } + result = hours;*/ + + + } + return result; + } +} \ No newline at end of file diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java new file mode 100644 index 0000000..c0d419b --- /dev/null +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -0,0 +1,40 @@ +package de.hsel.spm.baudas.analysis; + +import javafx.util.Pair; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URL; +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertTimeout; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class ShoppingTimesTest { + + //TODO: mehr Tests + + @Test + public void test100() { + URL url = getClass().getClassLoader().getResource("kd100.csv"); + assert url != null; + ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); + + AtomicReference, Integer>> results = new AtomicReference<>(); + + assertTimeout(Duration.ofMillis(450), () -> results.set(overview.getResult())); + /*for(Pair pair : results.get().keySet()) { + System.out.println(pair); + }*/ + + + //assertEquals(21, results.get().get(new Pair<>("Montag", ">17 Uhr"))); + //TODO: mehr Tests + } +} \ No newline at end of file From 6ebf46b65c9a0d080de9ffb9885c0423fcf3e087 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 19 Apr 2019 13:58:02 +0200 Subject: [PATCH 2/6] ~ tried understanding weka further, no luck so far Signed-off-by: Johannes Theiner #SPM-17 development 1h --- .../hsel/spm/baudas/analysis/Attribute.java | 39 +++++++++++++++ .../de/hsel/spm/baudas/analysis/DayHour.java | 16 ++++++ .../spm/baudas/analysis/ShoppingTimes.java | 49 +++++++++++++------ .../baudas/analysis/ShoppingTimesTest.java | 18 +++++-- 4 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/Attribute.java create mode 100644 src/main/java/de/hsel/spm/baudas/analysis/DayHour.java diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java new file mode 100644 index 0000000..375714b --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/Attribute.java @@ -0,0 +1,39 @@ +package de.hsel.spm.baudas.analysis; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ +public class Attribute { + + static final int SEX = 0; + static final int AGE = 1; + static final int KIDS = 2; + static final int MARITAL_STATUS = 3; + static final int EMPLOYED = 4; + static final int SHOPPING_DAY = 5; + static final int SHOPPING_HOUR = 6; + static final int RESIDENCE = 7; + static final int HOUSEHOLD_INCOME = 8; + static final int REGULAR = 9; + static final int PURCHASE_AMOUNT = 10; + static final int POWER_TOOLS = 11; + static final int HARDWARE = 12; + static final int HAND_TOOLS = 13; + static final int SANITARY = 14; + static final int LOCKING = 15; + static final int TILES = 16; + static final int COLORS = 17; + static final int FITTINGS = 18; + static final int RENOVATION = 19; + static final int ELECTRICAL = 20; + static final int BUILDING_MATERIAL = 21; + static final int WOOD = 22; + static final int FLOORING = 23; + static final int LIGHTS = 24; + static final int GARDENING_TOOLS = 25; + + + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java new file mode 100644 index 0000000..054032e --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java @@ -0,0 +1,16 @@ +package de.hsel.spm.baudas.analysis; + +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + **/ + +@Data +public class DayHour { + + private String day; + private String hour; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index bca82b1..bc68fea 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,6 +1,5 @@ package de.hsel.spm.baudas.analysis; -import javafx.util.Pair; import weka.core.Instances; import weka.filters.Filter; import weka.filters.unsupervised.attribute.Remove; @@ -13,10 +12,10 @@ import java.util.Map; * @version 0.1 * @since 0.1 **/ -public class ShoppingTimes implements Analysis, Integer>>{ +public class ShoppingTimes implements Analysis> { private Instances instances; - private Map, Integer> result; + private Map result; public ShoppingTimes(File file) { instances = load(file); @@ -24,14 +23,14 @@ public class ShoppingTimes implements Analysis, Integer @Override - public Map, Integer> getResult() { - if(result == null) { + public Map getResult() { + if (result == null) { + int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; int dayIndex = 0; int hourIndex = 1; - int[] keepIndexes = new int[]{5, 6}; - Remove remove = new Remove(); try { + Remove remove = new Remove(); remove.setAttributeIndicesArray(keepIndexes); remove.setInvertSelection(true); remove.setInputFormat(instances); @@ -42,14 +41,36 @@ public class ShoppingTimes implements Analysis, Integer - - /*Map, Integer> hours = new HashMap<>(); - AttributeStats stats = instances.attributeStats(hourIndex); - for(int i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { - Pair pair = new Pair<>(instances.attribute(hourIndex).value(i), instances.attribute(0).value(i)); - hours.put(pair, stats.nominalCounts[i]); + /*try { + SubsetByExpression byExpression = new SubsetByExpression(); + byExpression.setExpression("(1 == Montag)"); + byExpression.setInputFormat(instances); + instances = Filter.useFilter(instances, byExpression); + } catch (Exception e) { + e.printStackTrace(); } - result = hours;*/ + + + Enumeration dayValues = instances.attribute(dayIndex).enumerateValues(); + int i = 0; + while (dayValues.hasMoreElements()) { + System.out.println(dayValues.nextElement() + " " + instances.attributeStats(dayIndex).nominalCounts[i]); + i++; + } + + Enumeration hourValues = instances.attribute(hourIndex).enumerateValues(); + i = 0; + while (hourValues.hasMoreElements()) { + System.out.println(hourValues.nextElement() + " " + instances.attributeStats(hourIndex).nominalCounts[i]); + i++; + }*/ + + + + /* AttributeStats stats = instances.attributeStats(hourIndex); + for (i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { + System.out.println(instances.attribute(hourIndex).value(i) + " " + stats.nominalCounts[i]); + }*/ } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index c0d419b..fdc4d29 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -1,6 +1,5 @@ package de.hsel.spm.baudas.analysis; -import javafx.util.Pair; import org.junit.jupiter.api.Test; import java.io.File; @@ -26,9 +25,9 @@ public class ShoppingTimesTest { assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); - AtomicReference, Integer>> results = new AtomicReference<>(); + AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(450), () -> results.set(overview.getResult())); + assertTimeout(Duration.ofMillis(500), () -> results.set(overview.getResult())); /*for(Pair pair : results.get().keySet()) { System.out.println(pair); }*/ @@ -37,4 +36,17 @@ public class ShoppingTimesTest { //assertEquals(21, results.get().get(new Pair<>("Montag", ">17 Uhr"))); //TODO: mehr Tests } + + @Test + public void test10000() { + URL url = getClass().getClassLoader().getResource("kd10000.csv"); + assert url != null; + ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); + + AtomicReference> results = new AtomicReference<>(); + + assertTimeout(Duration.ofMillis(550), () -> results.set(overview.getResult())); + + //TODO: mehr Tests + } } \ No newline at end of file From e6f0a9ce6f6f73a94b5f632221d59371a53d203b Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 23 Apr 2019 14:12:05 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Funktionierende=20Version=20=C3=BCber=20kle?= =?UTF-8?q?inen=20Umweg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Theiner #SPM-17: add work development 1h 30m funktionierende Version --- .../de/hsel/spm/baudas/analysis/DayHour.java | 4 ++ .../spm/baudas/analysis/ShoppingTimes.java | 49 +++++++------------ .../baudas/analysis/ShoppingTimesTest.java | 9 ++-- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java index 054032e..52f7793 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java @@ -1,6 +1,8 @@ package de.hsel.spm.baudas.analysis; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; /** * @author Johannes Theiner @@ -9,6 +11,8 @@ import lombok.Data; **/ @Data +@EqualsAndHashCode +@AllArgsConstructor public class DayHour { private String day; diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index bc68fea..3a97155 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,10 +1,13 @@ package de.hsel.spm.baudas.analysis; +import weka.core.AttributeStats; +import weka.core.Instance; import weka.core.Instances; import weka.filters.Filter; import weka.filters.unsupervised.attribute.Remove; import java.io.File; +import java.util.HashMap; import java.util.Map; /** @@ -25,6 +28,7 @@ public class ShoppingTimes implements Analysis> { @Override public Map getResult() { if (result == null) { + result = new HashMap<>(); int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; int dayIndex = 0; int hourIndex = 1; @@ -39,40 +43,21 @@ public class ShoppingTimes implements Analysis> { ex.printStackTrace(); } - - - /*try { - SubsetByExpression byExpression = new SubsetByExpression(); - byExpression.setExpression("(1 == Montag)"); - byExpression.setInputFormat(instances); - instances = Filter.useFilter(instances, byExpression); - } catch (Exception e) { - e.printStackTrace(); + for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) { + String day = instances.attribute(dayIndex).value(i); + Instances instancesCopy = new Instances(instances, 0, instances.numInstances()); + for (int j = instancesCopy.numInstances() - 1; j >= 0; j--) { + Instance instance = instancesCopy.get(j); + if(!instance.stringValue(dayIndex).equals(day)) + instancesCopy.delete(j); + } + AttributeStats stats = instancesCopy.attributeStats(hourIndex); + for (int j = 0; j < instancesCopy.attribute(hourIndex).numValues(); j++) { + DayHour dayHour = new DayHour(day, instancesCopy.attribute(hourIndex).value(j)); + result.put(dayHour, stats.nominalCounts[j]); + } } - - Enumeration dayValues = instances.attribute(dayIndex).enumerateValues(); - int i = 0; - while (dayValues.hasMoreElements()) { - System.out.println(dayValues.nextElement() + " " + instances.attributeStats(dayIndex).nominalCounts[i]); - i++; - } - - Enumeration hourValues = instances.attribute(hourIndex).enumerateValues(); - i = 0; - while (hourValues.hasMoreElements()) { - System.out.println(hourValues.nextElement() + " " + instances.attributeStats(hourIndex).nominalCounts[i]); - i++; - }*/ - - - - /* AttributeStats stats = instances.attributeStats(hourIndex); - for (i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { - System.out.println(instances.attribute(hourIndex).value(i) + " " + stats.nominalCounts[i]); - }*/ - - } return result; } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index fdc4d29..14fad53 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -8,6 +8,7 @@ import java.time.Duration; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTimeout; /** @@ -27,13 +28,9 @@ public class ShoppingTimesTest { AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(500), () -> results.set(overview.getResult())); - /*for(Pair pair : results.get().keySet()) { - System.out.println(pair); - }*/ + assertTimeout(Duration.ofMillis(1300), () -> results.set(overview.getResult())); - - //assertEquals(21, results.get().get(new Pair<>("Montag", ">17 Uhr"))); + assertEquals(4, results.get().get(new DayHour("Montag", "10-12 Uhr"))); //TODO: mehr Tests } From feec46ecadd41f95a49f5daf580adedbb741d583 Mon Sep 17 00:00:00 2001 From: joethei Date: Tue, 23 Apr 2019 15:36:22 +0200 Subject: [PATCH 4/6] + Documentation #SPM-17: add work documentation 30m Dokumentation --- .../de/hsel/spm/baudas/analysis/Analysis.java | 6 +++--- .../hsel/spm/baudas/analysis/ShoppingTimes.java | 16 ++++++++++++++++ .../spm/baudas/analysis/ShoppingTimesTest.java | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java b/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java index b8bc054..3f8f86a 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/Analysis.java @@ -8,7 +8,7 @@ import java.io.File; import java.io.IOException; /** - * base interface for analysis + * base interface for analysis. * * @author Johannes Theiner * @version 0.1 @@ -19,7 +19,7 @@ public interface Analysis { /** - * loads data from csv file + * loads data from csv file. * * @param file File to analyze * @return loaded data in weka format @@ -37,7 +37,7 @@ public interface Analysis { } /** - * result of the analysis + * result of the analysis. * * @return result as T */ diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index 3a97155..b904cb4 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -11,6 +11,8 @@ import java.util.HashMap; import java.util.Map; /** + * Shopping Times Analysis. + * * @author Johannes Theiner * @version 0.1 * @since 0.1 @@ -25,6 +27,11 @@ public class ShoppingTimes implements Analysis> { } + /** + * get customer count at specific times. + * + * @return Map of Day-Hour Combinations and the corresponding customer count + */ @Override public Map getResult() { if (result == null) { @@ -34,6 +41,7 @@ public class ShoppingTimes implements Analysis> { int hourIndex = 1; try { + //Remove all other attributes Remove remove = new Remove(); remove.setAttributeIndicesArray(keepIndexes); remove.setInvertSelection(true); @@ -43,14 +51,22 @@ public class ShoppingTimes implements Analysis> { ex.printStackTrace(); } + /* + remove all days except one and get the stats + needed because weka has no other way to combine attributes + */ for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) { String day = instances.attribute(dayIndex).value(i); Instances instancesCopy = new Instances(instances, 0, instances.numInstances()); + + for (int j = instancesCopy.numInstances() - 1; j >= 0; j--) { Instance instance = instancesCopy.get(j); if(!instance.stringValue(dayIndex).equals(day)) instancesCopy.delete(j); } + + //get customer count and put into map AttributeStats stats = instancesCopy.attributeStats(hourIndex); for (int j = 0; j < instancesCopy.attribute(hourIndex).numValues(); j++) { DayHour dayHour = new DayHour(day, instancesCopy.attribute(hourIndex).value(j)); diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index 14fad53..b5651cc 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTimeout; /** - * @author Johannes Theiner + * @author hier Namen eintragen * @version 0.1 * @since 0.1 **/ @@ -28,7 +28,7 @@ public class ShoppingTimesTest { AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(1300), () -> results.set(overview.getResult())); + assertTimeout(Duration.ofMillis(1700), () -> results.set(overview.getResult())); assertEquals(4, results.get().get(new DayHour("Montag", "10-12 Uhr"))); //TODO: mehr Tests From 5541e1b9c8bb8fb75edbfed89592792d8f40c194 Mon Sep 17 00:00:00 2001 From: Karsten Date: Thu, 2 May 2019 14:31:42 +0200 Subject: [PATCH 5/6] Test Cases --- .../baudas/analysis/ShoppingTimesTest.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index b5651cc..1087bad 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTimeout; /** - * @author hier Namen eintragen + * @author Karsten Eden * @version 0.1 * @since 0.1 **/ @@ -31,7 +31,27 @@ public class ShoppingTimesTest { assertTimeout(Duration.ofMillis(1700), () -> results.set(overview.getResult())); assertEquals(4, results.get().get(new DayHour("Montag", "10-12 Uhr"))); - //TODO: mehr Tests + assertEquals(9, results.get().get(new DayHour("Samstag", "12-14 Uhr"))); + assertEquals(1, results.get().get(new DayHour("Donnerstag", "14-17 Uhr"))); + assertEquals(3, results.get().get(new DayHour("Mittwoch", ">17 Uhr"))); + assertEquals(6, results.get().get(new DayHour("Dienstag", "<10 Uhr"))); + } + + @Test + public void test1000(){ + URL url = getClass().getClassLoader().getResource("kd1000.csv"); + assert url != null; + ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); + + AtomicReference> results = new AtomicReference<>(); + + assertTimeout(Duration.ofMillis(3000), () -> results.set(overview.getResult())); + + assertEquals(37, results.get().get(new DayHour("Montag", "10-12 Uhr"))); + assertEquals(87, results.get().get(new DayHour("Samstag", "12-14 Uhr"))); + assertEquals(23, results.get().get(new DayHour("Donnerstag", "14-17 Uhr"))); + assertEquals(18, results.get().get(new DayHour("Mittwoch", ">17 Uhr"))); + assertEquals(18, results.get().get(new DayHour("Dienstag", "<10 Uhr"))); } @Test @@ -42,8 +62,12 @@ public class ShoppingTimesTest { AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(550), () -> results.set(overview.getResult())); + assertTimeout(Duration.ofMillis(3000), () -> results.set(overview.getResult())); - //TODO: mehr Tests + assertEquals(303, results.get().get(new DayHour("Montag", "10-12 Uhr"))); + assertEquals(737, results.get().get(new DayHour("Samstag", "12-14 Uhr"))); + assertEquals(192, results.get().get(new DayHour("Donnerstag", "14-17 Uhr"))); + assertEquals(199, results.get().get(new DayHour("Mittwoch", ">17 Uhr"))); + assertEquals(149, results.get().get(new DayHour("Dienstag", "<10 Uhr"))); } } \ No newline at end of file From f3770b09f9df88156bb6952d06aa926a01dd33c9 Mon Sep 17 00:00:00 2001 From: joethei Date: Thu, 2 May 2019 15:14:28 +0200 Subject: [PATCH 6/6] ~ schnellere Version mit weniger Weka #SPM-17 add work development 20m --- .../spm/baudas/analysis/ShoppingTimes.java | 46 +++---------------- .../baudas/analysis/ShoppingTimesTest.java | 10 ++-- 2 files changed, 11 insertions(+), 45 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index b904cb4..865517f 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,10 +1,7 @@ package de.hsel.spm.baudas.analysis; -import weka.core.AttributeStats; import weka.core.Instance; import weka.core.Instances; -import weka.filters.Filter; -import weka.filters.unsupervised.attribute.Remove; import java.io.File; import java.util.HashMap; @@ -36,44 +33,15 @@ public class ShoppingTimes implements Analysis> { public Map getResult() { if (result == null) { result = new HashMap<>(); - int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; - int dayIndex = 0; - int hourIndex = 1; - try { - //Remove all other attributes - Remove remove = new Remove(); - remove.setAttributeIndicesArray(keepIndexes); - remove.setInvertSelection(true); - remove.setInputFormat(instances); - instances = Filter.useFilter(instances, remove); - } catch (Exception ex) { - ex.printStackTrace(); + for(Instance instance : instances) { + DayHour dayHour = new DayHour(instance.stringValue(Attribute.SHOPPING_DAY), instance.stringValue(Attribute.SHOPPING_HOUR)); + int tmp = 1; + if(result.containsKey(dayHour)) + tmp = result.get(dayHour) + 1; + + result.put(dayHour, tmp); } - - /* - remove all days except one and get the stats - needed because weka has no other way to combine attributes - */ - for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) { - String day = instances.attribute(dayIndex).value(i); - Instances instancesCopy = new Instances(instances, 0, instances.numInstances()); - - - for (int j = instancesCopy.numInstances() - 1; j >= 0; j--) { - Instance instance = instancesCopy.get(j); - if(!instance.stringValue(dayIndex).equals(day)) - instancesCopy.delete(j); - } - - //get customer count and put into map - AttributeStats stats = instancesCopy.attributeStats(hourIndex); - for (int j = 0; j < instancesCopy.attribute(hourIndex).numValues(); j++) { - DayHour dayHour = new DayHour(day, instancesCopy.attribute(hourIndex).value(j)); - result.put(dayHour, stats.nominalCounts[j]); - } - } - } return result; } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index 1087bad..f6f572f 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -16,12 +16,10 @@ import static org.junit.jupiter.api.Assertions.assertTimeout; * @version 0.1 * @since 0.1 **/ -public class ShoppingTimesTest { - - //TODO: mehr Tests +class ShoppingTimesTest { @Test - public void test100() { + void test100() { URL url = getClass().getClassLoader().getResource("kd100.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); @@ -38,7 +36,7 @@ public class ShoppingTimesTest { } @Test - public void test1000(){ + void test1000(){ URL url = getClass().getClassLoader().getResource("kd1000.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile())); @@ -55,7 +53,7 @@ public class ShoppingTimesTest { } @Test - public void test10000() { + void test10000() { URL url = getClass().getClassLoader().getResource("kd10000.csv"); assert url != null; ShoppingTimes overview = new ShoppingTimes(new File(url.getFile()));