def test_computes_median(self): # data is a repeating series of 0,1,2,..N # the median of this array should be N/2 # timestamps is just an increasing index my_filter = MedianFilter() loop = asyncio.get_event_loop() pipe_in = LocalPipe("float32_%d" % WIDTH, name="input") pipe_out = LocalPipe("float32_%d" % WIDTH, name="output") args = argparse.Namespace(window=WINDOW, pipes="unset") base = np.array([np.arange(x, WINDOW + x) for x in range(WIDTH)]).T async def writer(): prev_ts = 0 for block in range(NUM_BLOCKS): data = np.tile(base, (REPS_PER_BLOCK, 1)) ts = np.arange(prev_ts, prev_ts + len(data)) input_block = np.hstack((ts[:, None], data)) pipe_in.write_nowait(input_block) #await asyncio.sleep(0.1) prev_ts = ts[-1] + 1 # now put in an extra block after an interval break await pipe_in.close_interval() # all 1's (even timestamps) await pipe_in.write(np.ones((100, WIDTH + 1))) #await asyncio.sleep(0.2) await pipe_in.close() loop.run_until_complete(asyncio.ensure_future(writer())) loop.run_until_complete( my_filter.run(args, {"input": pipe_in}, {"output": pipe_out})) result = pipe_out.read_nowait() # expect the output to be a constant (WINDOW-1)/2 # expect the output to be a constant (WINDOW-1)/2 base_output = np.ones( (WINDOW * REPS_PER_BLOCK * NUM_BLOCKS - (WINDOW - 1), WIDTH)) expected = base_output * range(int(WINDOW / 2), int(WINDOW / 2) + WIDTH) np.testing.assert_array_equal(expected, result['data']) self.assertTrue(pipe_out.end_of_interval) pipe_out.consume(len(result)) # now read the extra block and make sure it has all the data result = pipe_out.read_nowait(flatten=True) self.assertEqual(len(result), 100 - WINDOW + 1) loop.close()
def test_generates_random_values(self): my_reader = RandomReader() loop = asyncio.get_event_loop() pipe = LocalPipe("float32_%d" % WIDTH, name="output") args = argparse.Namespace(width=WIDTH, rate=RATE, pipes="unset") loop.call_later(0.1, my_reader.stop) loop.run_until_complete(my_reader.run(args, pipe)) loop.close() # check the results result = pipe.read_nowait() diffs = np.diff(result['timestamp']) self.assertEqual(np.mean(diffs), 1 / RATE * 1e6) self.assertEqual(np.shape(result['data'])[1], WIDTH)
def test_reads_timestamped_values(self): data = helpers.create_data("float32_8", length=3) (data_file, path) = tempfile.mkstemp() with open(data_file, 'w') as f: for row in data: f.write("%d %s\n" % (row['timestamp'], ' '.join(repr(x) for x in row['data']))) my_reader = FileReader() loop = asyncio.get_event_loop() pipe = LocalPipe("float32_8", name="output") args = argparse.Namespace(file=path, delimiter=" ", timestamp=False) loop.run_until_complete(my_reader.run(args, pipe)) # check the results result = pipe.read_nowait() np.testing.assert_array_almost_equal(data['timestamp'], result['timestamp']) np.testing.assert_array_almost_equal(data['data'], result['data']) os.remove(path)
def test_stops_on_request(self): data = helpers.create_data("float32_8") (data_file, path) = tempfile.mkstemp() with open(data_file, 'w') as f: for row in data: f.write("%d %s\n" % (row['timestamp'], ' '.join(repr(x) for x in row['data']))) my_reader = FileReader() loop = asyncio.get_event_loop() pipe = LocalPipe("float32_8", name="output") args = argparse.Namespace(file=path, delimiter=" ", timestamp=False) my_reader.stop() loop.run_until_complete(my_reader.run(args, pipe)) # check the results result = pipe.read_nowait() # output should be shorter than the input self.assertLess(len(result), len(data)) os.remove(path)
def test_timestamps_raw_values(self): data = helpers.create_data("float32_8", length=3) (data_file, path) = tempfile.mkstemp() with open(data_file, 'w') as f: for row in data: f.write("%s\n" % ' '.join(repr(x) for x in row['data'])) my_reader = FileReader() loop = asyncio.get_event_loop() pipe = LocalPipe("float32_8", name="output") args = argparse.Namespace(file=path, delimiter=" ", timestamp=True) loop.run_until_complete(my_reader.run(args, pipe)) # check the results result = pipe.read_nowait() # timestamps should be close to now actual = np.average(result['timestamp']) expected = int(time.time() * 1e6) # should be within 1 second np.testing.assert_almost_equal(actual / 1e6, expected / 1e6, decimal=0) np.testing.assert_array_almost_equal(data['data'], result['data']) os.remove(path)
async def run_case(inputs, expected, primary_width): my_filter = MergeFilter() primary_data = np.vstack( (inputs[0], np.ones((primary_width, len(inputs[0]))))).T secondary1_data = np.vstack((inputs[1], 2 * np.ones( (3, len(inputs[1]))))).T secondary2_data = np.vstack((inputs[2], 3 * np.ones( (1, len(inputs[2]))))).T primary = LocalPipe("float32_%d" % primary_width, name="primary") secondary1 = LocalPipe("float32_3", name="secondary1") secondary2 = LocalPipe("float32_1", name="secondary2") output = LocalPipe("float32_%d" % (primary_width + 4), name="output") args = argparse.Namespace(primary="primary", pipes="unset") # seed the input data await primary.write(primary_data) await secondary1.write(secondary1_data) await secondary2.write(secondary2_data) await secondary1.close() await secondary2.close() await primary.close() # run filter in an event loop await my_filter.run(args, inputs={ 'primary': primary, 'secondary1': secondary1, 'secondary2': secondary2 }, outputs={'output': output}) result = output.read_nowait(flatten=True) expected_data = np.vstack( (expected, np.ones( (primary_width, len(expected))), 2 * np.ones( (3, len(expected))), 3 * np.ones( (1, len(expected))))).T np.testing.assert_array_equal(expected_data, result)