def test_merge(self): e_si = si.Event() # event test events = [ db.Event(e_si, 3, "a"), db.Event(e_si, 5, "b"), db.Event.make_empty(e_si), db.Event(e_si, 12, "c") ] result = db.combine(*events) self.assertEqual(result[0], "a") self.assertEqual(result[1], "b") self.assertEqual(result[2], "c") self.assertTrue(np.array_equal(result.TS, np.asarray([3, 5, 12]))) self.assertEqual(result.SI, e_si) # event empty test empty_events = [db.Event.make_empty(e_si), db.Event.make_empty(e_si)] empty_events_result = db.combine(*empty_events) self.assertEqual(empty_events_result, db.Event.make_empty(e_si)) # event empty test 1 empty_event = [db.Event.make_empty(e_si)] empty_event_result = db.combine(*empty_event) self.assertEqual(empty_event_result, db.Event.make_empty(e_si))
def _split_data(self, signal, times, events): result = db.make_empty(self._si) while len(events) > 0 and len(signal) > 0: gs = events.TS[0] ts_index = np.array(times).searchsorted(gs) if ts_index == 0 and times[0] > gs: events = events[1:] continue if ts_index < len(times): pos = ts_index + self._shift if pos < 1: events = events[1:] continue if len(times) >= (pos + self._window_size): window_range = range(pos, pos + self._window_size) wnd = db.Window(self._si, times[window_range], signal[window_range, ], events[0:1]) events = events[1:] result = db.combine(result, wnd) continue break else: break return result, events
def test_merge(self): # window test w_si = si.Window(3, 7, 250) data_a = np.arange(1, 22).reshape((7, 3)) data_b = np.arange(22, 43).reshape((7, 3)) data_c = np.arange(43, 64).reshape((7, 3)) time_a = 3 time_b = 6 time_c = 35 A = db.Window(w_si, time_a, data_a, '1') B = db.Window(w_si, time_b, data_b, '2') C = db.Window(w_si, time_c, data_c, '3') M = db.combine(A, B, C) self.assertEqual(M[0], data_a) self.assertEqual(M[1], data_b) self.assertEqual(M[2], data_c) self.assertTrue(np.array_equal(M.TS, [time_a, time_b, time_c])) # self.assertNotEqual(db.SingleWindow(3, np.arange(1, 21)), np.arange(21, 41)) self.assertTrue(np.array_equal(M.metadata, ['1', '2', '3'])) self.assertEqual(M[0].metadata, '1')
def test_empty(self): w_si = si.Window(5, 20, 250) emptyWindows = [db.Window.make_empty(w_si), db.Window.make_empty(w_si)] result2 = db.combine(*emptyWindows) self.assertEqual(result2, db.Window.make_empty(w_si))
def test_merge_si_override(self): si1 = si.Channels(1, 20, id=1) si2 = si.Channels(1, 20, id=2) si3 = si.Channels(1, 20, id=3) A = db.Channels(si1, timeoption2ts(si1, 205), [1]) B = db.Channels(si2, timeoption2ts(si2, 206), [2]) merged = db.combine(A, B, si=si3) expected = db.Channels(si3, timeoption2ts(si3, 206), [1, 2]) self.assertEqual(merged, expected)
def test_merge(self): c_si = si.Channels(5, 20) A = db.Channels(c_si, timeoption2ts(c_si, 205), np.arange(1, 26)) B = db.Channels(c_si, timeoption2ts(c_si, 215), np.arange(26, 76)) C = db.Channels(c_si, timeoption2ts(c_si, 224), np.arange(76, 121)) M = db.combine(A, B, C) O = db.Channels(c_si, timeoption2ts(c_si, 224), np.arange(1, 121)) self.assertEqual(O, M)
def online(self, input_stream, events): # combine signal self._signal.extend(input_stream) self._times.extend(input_stream.TS) # combine events if len(events) > 0: self._events = db.combine(self._events, events) (result, self._events) = self._split_data(self._signal, self._times, self._events) return result
def check(self, i_si, blocks): def code(): resonance.createOutput(resonance.input(0), 'out') online = run.online([i_si], blocks, code) offline = run.offline([i_si], blocks, code) if len(blocks) > 0: origin = db.combine(*blocks) else: origin = db.make_empty(i_si) self.assertEqual(online['out'], origin) self.assertEqual(online['out'], offline['out'])
def online(stream_info: Sequence[StreamInfo], blocks: Sequence[db.Base], code: code_type, return_blocks: bool = False): outputs = {} id = 100 for i_si in stream_info: i_si.online = True if i_si.id is None: i_si._id = id id += 1 def process_queue(): queue = resonance.internal.pop_queue() if len(queue) > 0: for cmd, data in queue: if cmd == 'createOutputStream': outputs[data.name] = [] pass if cmd == 'sendBlockToStream': si, block = data outputs[si.name].append(block) pass events.on_prepare(code, stream_info) process_queue() events.on_start() process_queue() for block in blocks: events.on_data_block(block) process_queue() events.on_stop() process_queue() if return_blocks: return outputs else: return {name: db.combine(*values) for name, values in outputs.items()}
def online(self, input): return db.combine( db.make_empty(self._si), *[ db.Event(self._si, block.TS[0:1], self._transform(block)) for block in input ])
def combine_blocks(si): data = list(filter(lambda x: x.SI == si, blocks)) if len(data): return db.combine(*data) else: return db.make_empty(si)
def offline(self, a, b): result = db.combine(a, b, si=self._si) order = np.argsort(result.TS) result = result[order] return result