Skip to content

Commit

Permalink
Added config interface to HASCO
Browse files Browse the repository at this point in the history
* added renameAttribute to TaskChunk/Task
  • Loading branch information
mwever committed Aug 20, 2018
1 parent de9e943 commit 7d1ee66
Show file tree
Hide file tree
Showing 4 changed files with 661 additions and 530 deletions.
217 changes: 112 additions & 105 deletions JAICore/jaicore-basic/src/jaicore/basic/chunks/Task.java
Original file line number Diff line number Diff line change
@@ -1,117 +1,124 @@
package jaicore.basic.chunks;

import jaicore.basic.kvstore.SimpleKVStore;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import jaicore.basic.kvstore.SimpleKVStore;

public class Task extends SimpleKVStore {
/**
*
*/
private static final long serialVersionUID = -8259795114344707332L;

private static final String FIELD_TASKID = "taskID";
private TaskChunk<? extends Task> chunk;

public Task() {
super();
}

public Task(final String stringRepresentation) {
super(stringRepresentation);
}

public Task(final Task taskToCopy) {
super(new HashMap<>());
this.chunk = taskToCopy.chunk;
for (Entry<String, String> entry : taskToCopy.getAllKVEntries()) {
this.store(entry.getKey(), entry.getValue());
}
}

public Task(final Map<String, String> keyValueMap) {
super(keyValueMap);
}

@Override
public Task clone() {
Task t = new Task();
t.getKeyValueMap().putAll(this.getKeyValueMap());
return t;
}

public void setChunk(final TaskChunk<? extends Task> chunk) {
this.chunk = chunk;
}

public TaskChunk<? extends Task> getChunk() {
return this.chunk;
}

public String getTaskID() {
return this.getValueAsString(FIELD_TASKID);
}

public void setTaskID(final String taskID) {
this.store(FIELD_TASKID, taskID);
}

public Collection<Task> storeEach(final String key, final Set<String> values) {
return Task.storeEach(this, key, values);
}

public static Collection<Task> storeEach(final Task task, final String key, final Set<String> values) {
Collection<Task> allCombinations = new HashSet<>();
for (String value : values) {
Task copy = new Task(task);
copy.store(key, value);
allCombinations.add(copy);
}
return allCombinations;
}

@Override
public String toString() {
return super.toString();
}

public void implode(final String[] fieldKeys, final String separator, final String newKey) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String fieldKey : fieldKeys) {
if (first) {
first = false;
} else {
sb.append(separator);
}
sb.append(this.getValueAsString(fieldKey));
this.getKeyValueMap().remove(fieldKey);
}
this.store(newKey, sb.toString());
}

public void prefixKeys(final String prefix) {
Set<String> keySet = new HashSet<>(this.getKeyValueMap().keySet());
for (String key : keySet) {
String value = this.getValueAsString(key);
this.getKeyValueMap().remove(key);
this.store(prefix + key, value);
}
}

@Override
public boolean equals(final Object other) {
if (!(other instanceof Task)) {
return false;
}

Task t = (Task) other;
return t.getKeyValueMap().equals(this.getKeyValueMap());
}
/**
*
*/
private static final long serialVersionUID = -8259795114344707332L;

private static final String FIELD_TASKID = "taskID";
private TaskChunk<? extends Task> chunk;

public Task() {
super();
}

public Task(final String stringRepresentation) {
super(stringRepresentation);
}

public Task(final Task taskToCopy) {
super(new HashMap<>());
this.chunk = taskToCopy.chunk;
for (Entry<String, String> entry : taskToCopy.getAllKVEntries()) {
this.store(entry.getKey(), entry.getValue());
}
}

public Task(final Map<String, String> keyValueMap) {
super(keyValueMap);
}

@Override
public Task clone() {
Task t = new Task();
t.getKeyValueMap().putAll(this.getKeyValueMap());
return t;
}

public void setChunk(final TaskChunk<? extends Task> chunk) {
this.chunk = chunk;
}

public TaskChunk<? extends Task> getChunk() {
return this.chunk;
}

public String getTaskID() {
return this.getValueAsString(FIELD_TASKID);
}

public void setTaskID(final String taskID) {
this.store(FIELD_TASKID, taskID);
}

public Collection<Task> storeEach(final String key, final Set<String> values) {
return Task.storeEach(this, key, values);
}

public static Collection<Task> storeEach(final Task task, final String key, final Set<String> values) {
Collection<Task> allCombinations = new HashSet<>();
for (String value : values) {
Task copy = new Task(task);
copy.store(key, value);
allCombinations.add(copy);
}
return allCombinations;
}

@Override
public String toString() {
return super.toString();
}

public void implode(final String[] fieldKeys, final String separator, final String newKey) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String fieldKey : fieldKeys) {
if (first) {
first = false;
} else {
sb.append(separator);
}
sb.append(this.getValueAsString(fieldKey));
this.getKeyValueMap().remove(fieldKey);
}
this.store(newKey, sb.toString());
}

public void prefixKeys(final String prefix) {
Set<String> keySet = new HashSet<>(this.getKeyValueMap().keySet());
for (String key : keySet) {
String value = this.getValueAsString(key);
this.getKeyValueMap().remove(key);
this.store(prefix + key, value);
}
}

public void renameAttribute(final String attributeName, final String replacement) {
if (this.containsKey(attributeName)) {
this.store(replacement, this.getValueAsString(attributeName));
this.projectRemove(new String[] { attributeName });
}
}

@Override
public boolean equals(final Object other) {
if (!(other instanceof Task)) {
return false;
}

Task t = (Task) other;
return t.getKeyValueMap().equals(this.getKeyValueMap());
}

}
69 changes: 52 additions & 17 deletions JAICore/jaicore-basic/src/jaicore/basic/chunks/TaskChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,30 +259,64 @@ public void removeAny(final String[] value, final boolean or) {

public void removeAny(final Map<String, String> condition, final boolean or) {
if (or) {
taskList.removeIf(t -> {
this.taskList.removeIf(t -> {
for (String key : condition.keySet()) {
String val = t.getValueAsString(key);
if (val == null && condition.get(key) == null || val != null && val.equals(condition.get(key)))
if (val == null && condition.get(key) == null || val != null && val.equals(condition.get(key))) {
return true;
}
}
return false;
});
} else {
taskList.removeIf(t -> {
this.taskList.removeIf(t -> {
for (String key : condition.keySet()) {
if (!t.getValueAsString(key).equals(condition.get(key)))
if (!t.getValueAsString(key).equals(condition.get(key))) {
return false;
}
}
return true;
});
}
}

public void removeGroupsIfNotAtLeastWithSize(int size) {
public void removeGroupsIfNotAtLeastWithSize(final int size) {
Map<String, String> groupSizeCondition = new HashMap<>();
for (int i = 1; i < size; i++) {
groupSizeCondition.put("GROUP_SIZE", "" + i);
removeAny(groupSizeCondition, true);
this.removeAny(groupSizeCondition, true);
}
}

public void removeGroupsIfNotAtLeastWithSizeButOne(final int size, final String[] groupingKeys) {

Map<String, String> groupSizeCondition = new HashMap<>();
for (int i = 1; i < size; i++) {
System.out.println("Remove any groups that dont have at least " + (i + 1) + " entries.");

int currentMinLength = i;
TaskChunk<V> group = new TaskChunk<>(this.getStringRepresentation());
group.renameAttribute("GROUP_SIZE", "size");
group = group.group(groupingKeys, new HashMap<>());

for (V t : group) {
List<Integer> sizeList = t.getValueAsIntList("size", ",").stream().filter(x -> x > currentMinLength).collect(Collectors.toList());
System.out.println(currentMinLength + " " + sizeList + " " + t.getValueAsIntList("size", ","));
if (sizeList.size() > 0) {
for (String groupingKey : groupingKeys) {
groupSizeCondition.put(groupingKey, t.getValueAsString(groupingKey));
}
groupSizeCondition.put("GROUP_SIZE", "" + i);
System.out.println(groupSizeCondition);
this.removeAny(groupSizeCondition, false);
}
}
}
}

public void renameAttribute(final String attributeName, final String replacement) {
for (V t : this) {
t.renameAttribute(attributeName, replacement);
}
}

Expand Down Expand Up @@ -492,8 +526,7 @@ public void mergeTasks(final Task other, final Map<String, String> combineMap) {

boolean equals = true;
for (Entry<String, String> combineEntry : combineMap.entrySet()) {
if (!t.containsKey(combineEntry.getKey()) || !other.containsKey(combineEntry.getValue())
|| !t.getValueAsString(combineEntry.getKey()).equals(other.getValueAsString(combineEntry.getValue()))) {
if (!t.containsKey(combineEntry.getKey()) || !other.containsKey(combineEntry.getValue()) || !t.getValueAsString(combineEntry.getKey()).equals(other.getValueAsString(combineEntry.getValue()))) {
equals = false;
break;
}
Expand Down Expand Up @@ -567,9 +600,9 @@ public void mannWhitneyU(final String keyFieldName, final String comparatorField
}

}

public void best(final String keyFieldName, final String comparatorFieldName, final String valueFieldName) {
best (keyFieldName, comparatorFieldName, valueFieldName, "best");
this.best(keyFieldName, comparatorFieldName, valueFieldName, "best");
}

public void best(final String keyFieldName, final String comparatorFieldName, final String valueFieldName, final String outputFieldName) {
Expand Down Expand Up @@ -610,23 +643,23 @@ public void best(final String keyFieldName, final String comparatorFieldName, fi
}

}

public void singleBest(final String keyFieldName, final String comparatorFieldName, final String valueFieldName) {
singleBest(keyFieldName, comparatorFieldName, valueFieldName, "best");
this.singleBest(keyFieldName, comparatorFieldName, valueFieldName, "best");
}

public void singleBest(final String keyFieldName, final String comparatorFieldName, final String valueFieldName, final String outputFieldName) {
best(keyFieldName, comparatorFieldName, valueFieldName, outputFieldName);
this.best(keyFieldName, comparatorFieldName, valueFieldName, outputFieldName);
List<V> distinctTasks = new ArrayList<>();
Set<String> consideredKeys = new HashSet<>();
taskList.forEach(t -> {
this.taskList.forEach(t -> {
String keyValue = t.getValueAsString(keyFieldName);
if (!consideredKeys.contains(keyValue) && t.getValueAsBoolean(outputFieldName)) {
consideredKeys.add(keyValue);
distinctTasks.add(t);
}
});
taskList = distinctTasks;
this.taskList = distinctTasks;
}

public void best(final String keyFieldName, final String comparatorFieldName, final String valueFieldName, final Set<String> compareObjects) {
Expand Down Expand Up @@ -784,9 +817,11 @@ public void tTest(final String keyFN, final String idFN, final String valueListF
StatisticalSummaryValues summaryComp = new StatisticalSummaryValues(mean2, variance2, n2, max2, min2, sum2);

try {
sig = test.tTest(summaryGT, summaryComp, 0.05);
} catch(NumberIsTooSmallException e) {
sig = test.tTest(summaryGT, summaryComp, 0.05);
} catch (NumberIsTooSmallException e) {
System.out.println("Cannot apply ttest for dataset " + groundTruthEntry.getKey() + " and comparison of " + name1 + " and " + name2);
System.out.println(summaryGT);
System.out.println(summaryComp);
throw e;
}

Expand Down
Loading

0 comments on commit 7d1ee66

Please sign in to comment.