def test_abs(self): ''' Test absoluate overloaded function. ''' t1 = TimeStamp(self.time_dict_1) t4 = TimeStamp(self.time_dict_4) self.assertEqual( str(abs(t1)), 'TimeStamp: {\'year\': 2019, \'month\': 11, \'day\': 7, \'hour\': 11, \'min\': 4, \'sec\': 3, \'ms\': 12000}', "Expected TimeStamps to be same.") self.assertEqual( str(abs(t4)), 'TimeStamp: {\'year\': 2012, \'month\': 10, \'day\': 3, \'hour\': 12, \'min\': 2, \'sec\': 3, \'ms\': 11800}', "Expected TimeStamps to be same.")
def test_le(self): ''' Test less or equal overloaded operation. ''' t1 = TimeStamp(self.time_dict_1) t2 = TimeStamp(self.time_dict_2) t3 = TimeStamp(self.time_dict_3) t4 = TimeStamp(self.time_dict_4) self.assertTrue(t1 <= t1) self.assertFalse(t1 <= t2) self.assertTrue(t2 <= t1) self.assertFalse(t2 <= t3) self.assertTrue(t3 <= t2) self.assertFalse(t3 <= t4) self.assertTrue(t4 <= t3)
def test_sub(self): t1 = TimeStamp(self.time_dict_1) t2 = TimeStamp(self.time_dict_2) t3 = TimeStamp(self.time_dict_3) self.assertEqual( str(t1 - t2), 'TimeStamp: {\'year\': 0, \'month\': 0, \'day\': 0, \'hour\': 0, \'min\': 0, \'sec\': 0, \'ms\': 200}', "Expected TimeStamps to be same.") self.assertEqual( str(t1 - t3), 'TimeStamp: {\'year\': 7, \'month\': 1, \'day\': 4, \'hour\': -1, \'min\': 2, \'sec\': 0, \'ms\': 200}', "Expected TimeStamps to be same.") self.assertEqual( str(t2 - t1), 'TimeStamp: {\'year\': 0, \'month\': 0, \'day\': 0, \'hour\': 0, \'min\': 0, \'sec\': 0, \'ms\': -200}', "Expected TimeStamps to be same.") self.assertEqual( str(t1 - t1), 'TimeStamp: {\'year\': 0, \'month\': 0, \'day\': 0, \'hour\': 0, \'min\': 0, \'sec\': 0, \'ms\': 0}', "Expected TimeStamps to be same.")
def test_constructor(self): """ Test the default constructor """ t = TimeStamp(self.time_dict_1) self.assertEqual(t.year, self.time_dict_1['year'], "Expected years to be equal.") self.assertEqual(t.month, self.time_dict_1['month'], "Expected months to be equal.") self.assertEqual(t.day, self.time_dict_1['day'], "Expected days to be equal.") self.assertEqual(t.hour, self.time_dict_1['hour'], "Expected hours to be equal.") self.assertEqual(t.mins, self.time_dict_1['min'], "Expected mins to be equal.") self.assertEqual(t.sec, self.time_dict_1['sec'], "Expected secs to be equal.") self.assertEqual(t.ms, self.time_dict_1['ms'], "Expected ms to be equal.")
def get_frame_seqeunces(self, seq_len=5, window_gap=200, overlap=4): self.sorted_annotations = self._sort_by_time(self.annotations) samples = [] i = 0 # Create TimeStamp for given window gap. window_t = TimeStamp({'year': 0, 'month': 0, 'day': 0, 'hour': 0, 'min': 0, 'sec': 0, 'ms': window_gap}) # Iterate over sorted annotations (sorted by time). while i<self.num_samples: sample = [] # Current length of sample seq. curr_length = 0 # First frame of the sample seq. first_frame = self.sorted_annotations[i] sample.append(first_frame) curr_t = TimeStamp(first_frame['time']) if i+seq_len>=self.num_samples: break skip_flag = False for j in range(1, seq_len): next_frame = self.sorted_annotations[i+j] next_t = TimeStamp(next_frame['time']) diff_t = next_t-curr_t if diff_t <= window_t: sample.append(next_frame) curr_t = next_t else: skip_flag = True #print("[INFO] Frames skipped due to large time difference:") #for s in sample: # print(s) #print("Too far:", next_frame) #print() break if(len(sample)!=seq_len): assert RuntimeError("Sample length (%d) does not equal to seq_len (%d)" % (len(sample), seq_len)) else: #print() #for s in sample: # print(s) #print() samples.append(sample) if skip_flag==False: i += seq_len - overlap elif skip_flag==True: i += j return samples
def _sort_by_time(self, annotations): newlist = sorted(annotations, key=lambda k: TimeStamp(k['time'])) return newlist
def test_print(self): t = TimeStamp(self.time_dict_1) self.assertEqual( str(t), 'TimeStamp: {\'year\': 2019, \'month\': 11, \'day\': 7, \'hour\': 11, \'min\': 4, \'sec\': 3, \'ms\': 12000}', "Expected strings to be same.")