def testExampleUsage(self): A = SDR(10) B = SDR(10) C = SDR(20) A.sparse = [0, 1, 2] B.sparse = [0, 1, 2] C.concatenate(A, B) assert (set(C.sparse) == set([0, 1, 2, 10, 11, 12]))
def testReturn(self): """ Check that the following short hand will work, and will not incur extra copying: >>> C = SDR( A.size + B.size ).concatenate( A.flatten(), B.flatten() ) """ A = SDR((10, 10)).randomize(.5) B = SDR((10, 10)).randomize(.5) C = SDR(A.size + B.size) D = C.concatenate(A.flatten(), B.flatten()) assert (C is D)
def testVersusNumpy(self): # Each testcase is a pair of lists of SDR dimensions and axis # dimensions. test_cases = [ ([(9, 30, 40), (2, 30, 40)], 0), ([(2, 30, 40), (2, 99, 40)], 1), ([(2, 30, 40), (2, 30, 99)], 2), ([(100, ), (10), (30)], 0), ([(100, 2), (10, 2), (30, 2)], 0), ([(1, 77), (1, 99), (1, 88)], 1), ([(1, 77, 2), (1, 99, 2), (1, 88, 2)], 1), ] for sdr_dims, axis in test_cases: sdrs = [SDR(dims) for dims in sdr_dims] [sdr.randomize(.50) for sdr in sdrs] cat_dims = sdrs[0].dimensions cat_dims[axis] = sum(sdr.dimensions[axis] for sdr in sdrs) cat = SDR(cat_dims) cat.concatenate(sdrs, axis) np_cat = np.concatenate([sdr.dense for sdr in sdrs], axis=axis) assert ((cat.dense == np_cat).all())
def testMirroring(self): A = SDR(100) A.randomize(.05) Ax10 = SDR(100 * 10) Ax10.concatenate([A] * 10) assert (Ax10.getSum() == 100 * 10 * .05)
def testConstructorErrors(self): A = SDR(100) B = SDR((100, 2)) AB = SDR(300) C = SDR([3, 3]) D = SDR([3, 4]) CD = SDR([3, 7]) # Test bad argument dimensions with pytest.raises(TypeError): AB.concatenate(A) # Not enough inputs! with pytest.raises(RuntimeError): AB.concatenate(A, B) # Different numbers of dimensions! with pytest.raises(RuntimeError): AB.concatenate(B, C) # All dims except axis must match! with pytest.raises(RuntimeError): CD.concatenate(C, D) # All dims except axis must match! with pytest.raises(RuntimeError): CD.concatenate(C, D, axis=2) # Invalid axis! with pytest.raises(TypeError): CD.concatenate(C, D, axis=-1) # Invalid axis! # Test KeyWord Arguments. These should all work. CD.concatenate(C, D, 1) CD.concatenate(C, D, axis=1) CD.concatenate([C, D], axis=1) CD.concatenate(inputs=[C, D], axis=1)
def get_encoding(self, feature): encodings = [self.encoder.encode(feat) for feat in feature] encoding = SDR(self.sdr_length * self.n_features) encoding.concatenate(encodings) return encoding
def encode(self, inp, output=None): """ Argument inp: (datetime) representing the time being encoded """ if output is None: output = SDR(self.dimensions) else: assert (isinstance(output, SDR)) assert (all(x == y for x, y in zip(output.dimensions, self.dimensions))) if inp is None or (isinstance(inp, float) and math.isnan(inp)): output.zero() return output elif not isinstance(inp, datetime.datetime): raise ValueError("Input is type %s, expected datetime. Value: %s" % (type(inp), str(inp))) # ------------------------------------------------------------------------- # Encode each sub-field sdrs = [] timetuple = inp.timetuple() timeOfDay = timetuple.tm_hour + float(timetuple.tm_min) / 60.0 if self.seasonEncoder is not None: # Number the days starting at zero, intead of 1 like the datetime does. dayOfYear = timetuple.tm_yday - 1 assert (dayOfYear >= 0) # dayOfYear -= self.seasonEncoder.parameters.radius / 2. # Round towards the middle of the season. sdrs.append(self.seasonEncoder.encode(dayOfYear)) if self.dayOfWeekEncoder is not None: hrs_ = float( timeOfDay ) / 24.0 # add hours as decimal value in extension to day dayOfWeek = timetuple.tm_wday + hrs_ dayOfWeek -= .5 # Round towards noon, not midnight, this means similarity of representations changes at midnights, not noon. # handle underflow: on Mon before noon -> move to Sun if dayOfWeek < 0: dayOfWeek += 7 assert (dayOfWeek >= 0 and dayOfWeek < 7) sdrs.append(self.dayOfWeekEncoder.encode(dayOfWeek)) if self.weekendEncoder is not None: # saturday, sunday or friday evening if (timetuple.tm_wday == 6 or timetuple.tm_wday == 5 or (timetuple.tm_wday == 4 and timeOfDay > 18)): weekend = 1 else: weekend = 0 sdrs.append(self.weekendEncoder.encode(weekend)) if self.customDaysEncoder is not None: if timetuple.tm_wday in self.customDays: customDay = 1 else: customDay = 0 sdrs.append(self.customDaysEncoder.encode(customDay)) if self.holidayEncoder is not None: # A "continuous" binary value. = 1 on the holiday itself and smooth ramp # 0->1 on the day before the holiday and 1->0 on the day after the holiday. # holidays is a list of holidays that occur on a fixed date every year val = 0 for h in self.holidays: # hdate is midnight on the holiday if len(h) == 3: hdate = datetime.datetime(h[0], h[1], h[2], 0, 0, 0) else: hdate = datetime.datetime(timetuple.tm_year, h[0], h[1], 0, 0, 0) if inp > hdate: diff = inp - hdate if diff.days == 0: # return 1 on the holiday itself val = 1 break elif diff.days == 1: # ramp smoothly from 1 -> 0 on the next day val = 1.0 + (float(diff.seconds) / 86400) break else: diff = hdate - inp if diff.days == 0: # ramp smoothly from 0 -> 1 on the previous day val = 1.0 - (float(diff.seconds) / 86400) sdrs.append(self.holidayEncoder.encode(val)) if self.timeOfDayEncoder is not None: sdrs.append(self.timeOfDayEncoder.encode(timeOfDay)) if len(sdrs) > 1: output.concatenate(sdrs) else: output.setSDR(sdrs[0]) return output