Skip to content

Commit

Permalink
Add and pass testWeekend()
Browse files Browse the repository at this point in the history
  • Loading branch information
utensil committed Jan 4, 2015
1 parent 517da2f commit 9d62b4c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/main/java/org/numenta/nupic/encoders/DateEncoder.java
Expand Up @@ -188,18 +188,30 @@ public void init() {
}

if(isValidEncoderPropertyTuple(customDays)) {
List<String> days = (List<String>) customDays.get(1);

StringBuilder customDayEncoderName = new StringBuilder();

if(days.size() == 1) {
customDayEncoderName.append(days.get(0));
} else {
for(String day : days) {
customDayEncoderName.append(day).append(" ");
}
}

customDaysEncoder = ScalarEncoder.builder()
.w((int) customDays.get(0))
.radius(1)
.minVal(0)
.maxVal(1)
.periodic(true)
.name("customdays")
.periodic(false)
.name(customDayEncoderName.toString())
.forced(this.isForced())
.build();
addChildEncoder(customDaysEncoder);

addCustomDays((ArrayList<String>) customDays.get(1));
//customDaysEncoder is special in naming
addEncoder("customdays", customDaysEncoder);
addCustomDays(days);
}

if(isValidEncoderPropertyTuple(holiday)) {
Expand Down Expand Up @@ -247,12 +259,9 @@ public void addEncoder(String name, Encoder child) {

protected void addChildEncoder(ScalarEncoder encoder) {
this.addEncoder(encoder.getName(), encoder);
// childEncoders.add(new EncoderTuple(encoder.getName(), encoder, offset));
// description.add(new Tuple(2, encoder.getName(), offset));
// width += encoder.getWidth();
}

protected void addCustomDays(ArrayList<String> daysList) {
protected void addCustomDays(List<String> daysList) {
for(String dayStr : daysList)
{
switch (dayStr.toLowerCase())
Expand Down Expand Up @@ -286,7 +295,9 @@ protected void addCustomDays(ArrayList<String> daysList) {
customDaysList.add(6);
break;
default:
throw new IllegalArgumentException("Invalid custom day: " + dayStr);
throw new IllegalArgumentException(
String.format("Unable to understand %s as a day of week", dayStr)
);
}
}
}
Expand Down Expand Up @@ -655,7 +666,7 @@ public DateEncoder.Builder weekend(int weekend) {
/**
* Set how many bits are used to encode customDays
*/
public DateEncoder.Builder customDays(int customDays, ArrayList<String> customDaysList) {
public DateEncoder.Builder customDays(int customDays, List<String> customDaysList) {
this.customDays = new Tuple(2, customDays, customDaysList);
return this;
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/numenta/nupic/encoders/DateEncoderTest.java
Expand Up @@ -109,6 +109,7 @@ public void testDecoding() {
setUp();
initDE();

//TODO Why null is needed?
Tuple decoded = de.decode(bits, null);

Map<String, RangeList> fieldsMap = (Map<String, RangeList>)decoded.get(0);
Expand Down Expand Up @@ -216,5 +217,48 @@ public void testHoliday() {
assertArrayEquals(holiday2, e.encode(d));
}

/**
* Test weekend encoder
*/
@Test
public void testWeekend() {
//use of forced is not recommended, used here for readability, see ScalarEncoder
DateEncoder e = DateEncoder.builder().customDays(21, Arrays.asList(
"sat", "sun", "fri"
)).forced(true).build();
DateEncoder mon = DateEncoder.builder().customDays(21, Arrays.asList(
"Monday"
)).forced(true).build();
DateEncoder e2 = DateEncoder.builder().weekend(21, 1).forced(true).build();
DateTime d = new DateTime(1988,5,29,20,00);

assertArrayEquals(e.encode(d.toDate()), e2.encode(d.toDate()));

for (int i = 0; i < 300; i++) {
DateTime curDate = d.plusDays(i + 1);
assertArrayEquals(e.encode(curDate.toDate()), e2.encode(curDate.toDate()));

//Make sure
Tuple decoded = mon.decode(mon.encode(curDate.toDate()), null);

Map<String, RangeList> fieldsMap = (Map<String, RangeList>)decoded.get(0);
List<String> fieldsOrder = (List<String>)decoded.get(1);

assertNotNull(fieldsMap);
assertNotNull(fieldsOrder);
assertEquals(1, fieldsMap.size());

RangeList range = fieldsMap.get("Monday");
assertEquals(1, range.size());
assertEquals(1, ((List<MinMax>)range.get(0)).size());
MinMax minmax = range.getRange(0);

if(minmax.min() == 1.0) {
assertEquals(1, curDate.getDayOfWeek());
} else {
assertNotEquals(1, curDate.getDayOfWeek());
}
}
}
}

0 comments on commit 9d62b4c

Please sign in to comment.