def test_multi_delayed_summer(self): ''' Test adding multiple (50) input signals where one signal is delayed. ''' DELAY = 20 num_input_channels, num_data_points = 50, 100 input_channels = [Channel('DT') for i in xrange(num_input_channels)] output_channel = Channel('DT') # Fill each channel with num_data_points of its own index # So channel 5 will be full of the value 4, then a terminal event for i, input_channel in enumerate(input_channels): [input_channel.put(Event(value=i, tag=j)) for j in xrange(num_data_points) if i is not 0] [input_channels[0].put(Event(value=0, tag=j + DELAY)) for j in xrange(num_data_points)] [input_channel.put(LastEvent()) for input_channel in input_channels] summer = Summer(input_channels, output_channel) summer.start() summer.join() s = sum(xrange(num_input_channels)) for i in xrange(num_data_points - DELAY): self.assertEquals(output_channel.get()['value'], s) self.assertTrue(output_channel.get().last)
def __init__(self): output = Channel() reduced_output = Channel() step = Step(output, switch_time=15, timestep=100, simulation_time=30) reducer = Decimator(output, reduced_output, 100) plotter = Stemmer(reduced_output) self.components = [step, reducer, plotter]
def test_alternating_merge(self): ''' Test merging two channels with out-of-sync tags. ''' q_in_1 = Channel() q_in_2 = Channel() q_out = Channel() input1 = [Event(value=1, tag=2.0*i) for i in xrange(100)] + [LastEvent()] input2 = [Event(value=2, tag=2.0*i + 1.0) for i in xrange(100)] + [LastEvent()] merge = Merge([q_in_1, q_in_2], q_out) merge.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) merge.join() for i in xrange(99): self.assertEquals(q_out.get().value, 1) self.assertEquals(q_out.get().value, 2) # The termination event from channel 1 will cause the # last value event from channel 2 to be lost self.assertEquals(q_out.get().value, 1) self.assertTrue(q_out.get().last) self.assertTrue(q_out.get().last)
class AbsTests( unittest.TestCase ): '''Test the absolute actor''' def setUp( self ): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() def test_positive_integers( self ): '''Test a simple positive integer signal. ''' inp = [Event(value=i, tag=i) for i in xrange( 0, 100, 1 )] expected_outputs = inp[:] abs = Abs( self.q_in, self.q_out ) abs.start() [self.q_in.put( val ) for val in inp] self.q_in.put( LastEvent() ) abs.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals( out.value, expected_output.value ) self.assertEquals( out.tag, expected_output.tag ) self.assertTrue( self.q_out.get().last )
class BundleDerivativeTests(unittest.TestCase): def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel() def test_first_order_diff(self): '''Test a first order diff''' input_values = [0, 1, 2, 3, 3, 3, 3, 1] outs = [1, 1, 1, 0, 0, 0, -2] self.input = [Event(value=input_values[i], tag=i) for i in xrange(len(input_values))] bundler = Bundle(self.q_in, self.q_out) diffblock = BundleDerivative(self.q_out, self.q_out2, threshold=50) [self.q_in.put(i) for i in self.input + [LastEvent()]] [block.start() for block in [bundler, diffblock]] [block.join() for block in [bundler, diffblock]] outputs = self.q_out2.get() self.assertNotEqual(outputs, None) [self.assertEquals(outs[i], outputs['Value'][i]) for i in xrange(len(outs))] self.assertTrue(self.q_out2.get().last)
def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel()
class Ct2DtTests(unittest.TestCase): '''Test the Ct2Dt actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("DT") def test_simple_ramp(self): '''Convert a simple ramp signal''' inp = [Event(value=i, tag=i / 10.0) for i in xrange(0, 100, 1)] expected_outputs = [ Event(value=i * 2, tag=i) for i in xrange(0, 50, 1) ] sampler = Ct2Dt(self.q_in, self.q_out, 5) sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) sampler.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertAlmostEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag)
class QuantizerTests(unittest.TestCase): '''Test the quantizer actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("CT") def test_ramp_quantization(self): ''' Test quantizing a simple ramp signal. ''' inp = [Event(i, i) for i in xrange(-20, 21, 1)] quantizer = Quantizer(self.q_in, self.q_out, 2) expected_outputs = [ Event(i, i if i % 2 == 0 else i - 1) for i in xrange(-20, 21, 1) ] quantizer.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) quantizer.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag) self.assertTrue(self.q_out.get().last)
class DecimatorTests(unittest.TestCase): '''Test the decimation actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() def test_basic_integer_tags(self): '''Test halving the frequency we sample a simple integer signal. Create a discrete time signal with a 1hz frequency down-sample to 0.5hz by a factor 2 reduction ''' inp = [Event(value=1, tag=i) for i in xrange(0, 100, 1)] expected_outputs = [Event(value=1, tag=i) for i in xrange(0, 100, 2)] down_sampler = Decimator(self.q_in, self.q_out, 2) down_sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) down_sampler.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag) self.assertTrue(self.q_out.get().last)
class Ct2DtTests(unittest.TestCase): '''Test the Ct2Dt actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("DT") def test_simple_ramp(self): '''Convert a simple ramp signal''' inp = [Event(value=i, tag=i/10.0) for i in xrange(0, 100, 1)] expected_outputs = [Event(value=i*2, tag=i) for i in xrange(0, 50, 1)] sampler = Ct2Dt(self.q_in, self.q_out, 5) sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) sampler.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertAlmostEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag)
class AbsTests(unittest.TestCase): '''Test the absolute actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() def test_positive_integers(self): '''Test a simple positive integer signal. ''' inp = [Event(value=i, tag=i) for i in xrange(0, 100, 1)] expected_outputs = inp[:] abs = Abs(self.q_in, self.q_out) abs.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) abs.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag) self.assertTrue(self.q_out.get().last)
class QuantizerTests(unittest.TestCase): '''Test the quantizer actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("CT") def test_ramp_quantization(self): ''' Test quantizing a simple ramp signal. ''' inp = [Event(i, i) for i in xrange(-20, 21, 1)] quantizer = Quantizer(self.q_in, self.q_out, 2) expected_outputs = [Event(i, i if i%2==0 else i-1) for i in xrange(-20, 21, 1)] quantizer.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) quantizer.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag) self.assertTrue(self.q_out.get().last)
class EventFilterTests(unittest.TestCase): '''Test the event-filter actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("DE") def test_ramp_event_detection(self): ''' Test event detection on a simple ramp signal. ''' inp = [Event(i/3, i/3) for i in xrange(-60, 63, 1)] ef = EventFilter(self.q_in, self.q_out, 2) expected_outputs = [Event(i, i) for i in xrange(-20, 21, 2)] ef.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) ef.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.tag, expected_output.tag) self.assertEquals(out.value, expected_output.value) self.assertTrue(self.q_out.get().last)
def test_multi_delayed_summer(self): ''' Test adding multiple (50) input signals where one signal is delayed. ''' DELAY = 20 num_input_channels, num_data_points = 50, 100 input_channels = [Channel('DT') for i in xrange(num_input_channels)] output_channel = Channel('DT') # Fill each channel with num_data_points of its own index # So channel 5 will be full of the value 4, then a terminal event for i, input_channel in enumerate(input_channels): [ input_channel.put(Event(value=i, tag=j)) for j in xrange(num_data_points) if i is not 0 ] [ input_channels[0].put(Event(value=0, tag=j + DELAY)) for j in xrange(num_data_points) ] [input_channel.put(LastEvent()) for input_channel in input_channels] summer = Summer(input_channels, output_channel) summer.start() summer.join() s = sum(xrange(num_input_channels)) for i in xrange(num_data_points - DELAY): self.assertEquals(output_channel.get()['value'], s) self.assertTrue(output_channel.get().last)
def setUp( self ): self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel() self.input = [Event(value=1, tag=i) for i in xrange( 100 )] self.title = "test plot" self.url = os.path.join(os.getcwd() , self.title) + ".png"
def setUp(self): self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel() self.input = [Event(value=1, tag=i) for i in xrange(100)] self.title = "test plot" self.url = os.path.join(os.getcwd(), self.title) + ".png"
def test_text_reader(self): filename = PATH_TO_THIS_FILE output = Channel() reader = TextReader(output, filename, send_as_words=True) reader.start() reader.join() self.assertEquals("'''", output.get().value) self.assertEquals(1, output.get().tag)
def test_linear_interpolation_dt(self): '''Test linear interpolation of a simple DT signal. ''' inp = [Event(value = i, tag = i) for i in xrange(-10, 11, 1)] expected_outputs = [Event(tag = t, value = (t / 2.0 - 5.0) ) for t in xrange(-10, 31, 1)] self.q_in = Channel('DT') self.q_out = Channel('DT') block = InterpolatorLinear(self.q_in, self.q_out) SisoTestHelper(self, block, inp, expected_outputs)
def test_text_writer(self): tfile = tempfile.NamedTemporaryFile() filename = tfile.name output = Channel() writer = TextWriter(output, filename) [output.put(e) for e in [Event(value=i ** 3, tag=i) for i in xrange(100)] + [LastEvent()]] writer.start() writer.join() for i, line in enumerate(tfile): self.assertEquals('%s, %s' % (str(i), str(i**3)), line.strip())
def __init__(self): '''Setup the sim''' super(RampGainPlot, self).__init__() ramp2gain = Channel() gain2plot = Channel() src = Ramp(ramp2gain) filt = Proportional(ramp2gain, gain2plot) dst = Plotter(gain2plot) self.components = [src, filt, dst]
def __init__(self): '''Setup the simulation''' super(QSSinPlot, self).__init__() connection1 = Channel(domain="CT") connection2 = Channel(domain="CT") # 0.2 Hz, 90 degree phase src = CTSinGenerator(connection1, 2.1, 0.2, numpy.pi / 2) de = EventFilter(connection1, connection2, 0.2) dst = StemPlotter(connection2, refresh_rate=1.0 / 2) self.components = [src, de, dst]
def setUp(self): '''General set up that will be used in all tests''' self.data_in = Channel() self.alt_data_in = Channel() self.bool_in = Channel() self.output = Channel() # Some fake boolean data self.every_second_point = [True, False] * 50 # some fake pass through data, and add it to the channel self.data = range(100) self.data_alt = 10 * self.data [ self.alt_data_in.put(Event(tag=i, value=self.data_alt[i])) for i in range(100) ] [self.data_in.put(Event(tag=i, value=i)) for i in range(100)] self.data_in.put(LastEvent()) self.alt_data_in.put(LastEvent()) # create the block self.block = PassThrough(self.bool_in, self.data_in, self.output, else_data_input=self.alt_data_in)
def test_different_rate(self): '''Test that integration of a simple positive constant signal doesn't change with samplerate. ''' from scipy import arange inp_1 = [Event(value = 10, tag = i) for i in arange(0, 10, 1)] inp_2 = [Event(value = 10, tag = i) for i in arange(0, 10, 0.1)] expected_output_values_1 = [10 * i for i in arange(0, 10, 1)] expected_output_values_2 = [10 * i for i in arange(0, 10, 0.1)] for Block in [CTIntegratorForwardEuler]: q_in1, q_out1, q_in2, q_out2 = Channel(), Channel(), Channel(), Channel() block1 = Block(q_in1, q_out1) block2 = Block(q_in2, q_out2) block1.start(); block2.start() [q_in1.put(val) for val in inp_1] [q_in2.put(val) for val in inp_2] q_in1.put(LastEvent()); q_in2.put(LastEvent()) block1.join(); block2.join() out = [] for expected_output in expected_output_values_1: out.append(q_out1.get()) out = [item.value for item in out] self.assertEquals(len(out), len(expected_output_values_1)) #[self.assertEquals(out[i], expected_output[i]) for i, _ in enumerate(expected_output_values_1)] self.assertTrue(q_out1.get().last) for expected_output in expected_output_values_2: out = q_out2.get() self.assertAlmostEquals(out.value, expected_output) self.assertTrue(q_out2.get().last)
def __init__(self): super(DT_Sin_Plot, self).__init__() chan1 = Channel("DT") src = DTSinGenerator(chan1) pltr = StemPlotter(chan1) self.components = [src, pltr]
def __init__(self): '''Run the simulation''' super(RandomPlot, self).__init__() connection = Channel() src = RandomSource(connection) dst = Plotter(connection) self.components = [src, dst]
class FileIOTests(unittest.TestCase): '''Test the FileIO Actors''' def setUp(self): self.chan = Channel() self.signal = [Event(value=i ** 3, tag=i) for i in xrange(100)] + [LastEvent()] [self.chan.put(val) for val in self.signal] self.f = tempfile.NamedTemporaryFile()#delete=False) def tearDown(self): self.f.close() def test_file_write(self): '''Test that we can save data''' fileWriter = Writer(self.chan, self.f.name) # Run the file writer and save the output to a temp file fileWriter.start() fileWriter.join() # Check that the channel is empty... self.assertRaises(Channel.Empty, lambda: self.chan.get(block=False)) # TODO check the data (load with numpy) def test_file_read(self): '''Test that we can retrieve data''' # Using self.f here, even with delete set to false, doesn't seem to # work. So we need to manually create a temp file. fileName = tempfile.gettempdir() + '/numpy_test_data.npy' fileWriter = Writer(self.chan, fileName) fileWriter.start() fileWriter.join() fileReader = Reader(output_channel=self.chan, file_name=fileName) fileReader.start() fileReader.join() for expected in self.signal: if not expected.last: received = self.chan.get() self.assertEqual(received.tag, expected.tag) self.assertEqual(received.value, expected.value) self.assertTrue(expected.last) os.remove(fileName) # clean up
def __init__(self): '''Setup the simulation''' connection1 = Channel() connection2 = Channel() connection3 = Channel() connection4 = Channel() src1 = Ramp(connection1) src2 = RandomSource(connection2) summer = Summer([connection1, connection2], connection3) bundler = Bundle(connection3, connection4) dst = BundlePlotter(connection4, title="Scipy-Simulation: Noise + Ramp Sum", show=True) #dst = Plotter(connection3) self.components = [src1, src2, summer, bundler, dst]
def __init__(self): '''Create the components''' super(RampPlot, self).__init__() connection = Channel() src = Ramp(connection) dst = Plotter(connection, xlabel='Time (s)', ylabel='Amplitude', title='Ramp Plot') self.components = [src, dst]
class BundlePlotTests( unittest.TestCase ): def setUp( self ): self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel() self.input = [Event(value=1, tag=i) for i in xrange( 100 )] self.title = "test plot" self.url = os.path.join(os.getcwd() , self.title) + ".png" def tearDown( self ): del self.q_in del self.q_out try: os.remove(self.url) except OSError: pass def test_getting_bundle_data( self ): '''Test bundling a signal and getting the data back''' block = Bundle( self.q_in, self.q_out ) block.start() [self.q_in.put( i ) for i in self.input + [LastEvent()]] block.join() bundled_data = self.q_out.get() self.assertEqual( len( bundled_data ), 100 ) self.assertEqual( type( bundled_data ), numpy.ndarray ) values = bundled_data["Value"] self.assertTrue( all( values == 1 ) ) tags = bundled_data["Tag"] [self.assertEquals( tags[i] , i ) for i in xrange( 100 ) ] def test_plotting( self ): bundler = Bundle( self.q_in, self.q_out ) bundlingPlotter = BundlePlotter( self.q_out, self.title ) [block.start() for block in [bundler, bundlingPlotter]] [self.q_in.put( i ) for i in self.input + [LastEvent()]] [block.join() for block in [bundler, bundlingPlotter]] self.assertTrue(os.path.exists(self.url))
class SinkTests(unittest.TestCase): '''Test the sink actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() def test_sink(self): '''Test that sink runs. ''' inp = [Event(i, i) for i in xrange(0, 100, 1)] try: sink = Sink(self.q_in) sink.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) sink.join() except: self.fail("Sink failed to run without exception.")
class BundlePlotTests(unittest.TestCase): def setUp(self): self.q_in = Channel() self.q_out = Channel() self.q_out2 = Channel() self.input = [Event(value=1, tag=i) for i in xrange(100)] self.title = "test plot" self.url = os.path.join(os.getcwd(), self.title) + ".png" def tearDown(self): del self.q_in del self.q_out try: os.remove(self.url) except OSError: pass def test_getting_bundle_data(self): '''Test bundling a signal and getting the data back''' block = Bundle(self.q_in, self.q_out) block.start() [self.q_in.put(i) for i in self.input + [LastEvent()]] block.join() bundled_data = self.q_out.get() self.assertEqual(len(bundled_data), 100) self.assertEqual(type(bundled_data), numpy.ndarray) values = bundled_data["Value"] self.assertTrue(all(values == 1)) tags = bundled_data["Tag"] [self.assertEquals(tags[i], i) for i in xrange(100)] def test_plotting(self): bundler = Bundle(self.q_in, self.q_out) bundlingPlotter = BundlePlotter(self.q_out, self.title) [block.start() for block in [bundler, bundlingPlotter]] [self.q_in.put(i) for i in self.input + [LastEvent()]] [block.join() for block in [bundler, bundlingPlotter]] self.assertTrue(os.path.exists(self.url))
def test_mass_merge(self): ''' Test merging a large number (50) of input signals. ''' num_input_channels, num_data_points = 50, 100 input_channels = MakeChans(num_input_channels) output_channel = Channel() # Fill each channel with num_data_points of (channel, data) pairs, # then a LastEvent() for i, input_channel in enumerate(input_channels): _ = [input_channel.put(Event(value=(i,j), tag=j)) for j in xrange(num_data_points)] _ = [input_channel.put(LastEvent()) for input_channel in input_channels] merge = Merge(input_channels, output_channel) merge.start() merge.join() for i in xrange(num_data_points): for chan in xrange(num_input_channels): self.assertEquals(output_channel.get().value, (chan, i)) self.assertTrue(output_channel.get().last)
class SinkTests(unittest.TestCase): '''Test the sink actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() def test_sink(self): '''Test that sink runs. ''' inp = [Event(i,i) for i in xrange(0, 100, 1)] try: sink = Sink(self.q_in) sink.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) sink.join() except: self.fail("Sink failed to run without exception.")
def test_simple_merge(self): '''Test merging two channels of complete pairs together''' q_in_1 = Channel() q_in_2 = Channel() q_out = Channel() input1 = [Event(value=1, tag=i) for i in xrange(100)] + [LastEvent()] input2 = [Event(value=2, tag=i) for i in xrange(100)] + [LastEvent()] merge = Merge([q_in_1, q_in_2], q_out) merge.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) merge.join() for i in xrange(100): self.assertEquals(q_out.get().value, 1) self.assertEquals(q_out.get().value, 2) self.assertTrue(q_out.get().last)
def __init__(self): '''Setup the simulation''' super(SumSinPlot, self).__init__() connection1 = Channel(domain="CT") connection2 = Channel(domain="CT") connection3 = Channel() # 2 Hz, 90 degree phase src1 = CTSinGenerator(connection1, 2, 2.0, numpy.pi / 2, timestep=0.001) # 4 Hz, 45 degree phase, different timestep src2 = CTSinGenerator(connection2, 1, 3.5, numpy.pi / 4, timestep=0.005) summer = Summer([connection1, connection2], connection3) dst = Plotter(connection3, refresh_rate=50) self.components = [src1, src2, summer, dst]
class DelayTests(unittest.TestCase): def setUp(self): self.q_in = Channel() self.q_out = Channel() def tearDown(self): del self.q_in del self.q_out def test_basic_delay(self): '''Test delaying a basic integer tagged signal by 1''' delay = 2 input1 = [Event(value=1, tag=i) for i in xrange(100)] expected_output = [Event(value=1, tag=i + delay) for i in xrange(100)] block = Delay(self.q_in, self.q_out, delay) block.start() [self.q_in.put(i) for i in input1 + [LastEvent()]] block.join() actual_output = [self.q_out.get() for i in xrange(100)] [ self.assertEquals(actual_output[i], expected_output[i]) for i in xrange(100) ] self.assertTrue(self.q_out.get().last) def test_complex_delay(self): '''Test delaying a CT signal''' delay = 11.5 # Delay by this amount simulation_time = 120 # seconds to simulate resolution = 10.0 # samples per second (10hz) tags = linspace(0, simulation_time, simulation_time / resolution) values = arange(len(tags)) data_in = [ Event(value=values[i], tag=tags[i]) for i in xrange(len(tags)) ] expected_output = [ Event(value=values[i], tag=tags[i] + delay) for i in xrange(len(tags)) ] block = Delay(self.q_in, self.q_out, delay) block.start() [self.q_in.put(i) for i in data_in + [LastEvent()]] block.join() actual_output = [self.q_out.get() for i in xrange(len(tags))] [ self.assertEquals(actual_output[i], expected_output[i]) for i in xrange(len(tags)) ] self.assertTrue(self.q_out.get().last)
def __init__(self, out, amplitude=1.0, freq=1.0, phi=0.0, timestep=0.001, simulation_time=10): super(CTSinGenerator, self).__init__(output_channel=out) self.chan = Channel() self.ramp = Ramp(self.chan, resolution=1.0 / timestep, simulation_time=simulation_time) self.sin = Sin(self.chan, self.output_channel, amplitude=amplitude, freq=freq, phi=phi)
def test_basic_proportional(self): '''Test doubling a channel.''' q_in = Channel() q_out = Channel() inp = [Event(value=1, tag=i) for i in xrange(100)] expected_output = [Event(value=2, tag=i) for i in xrange(100)] doubler = Proportional(q_in, q_out) doubler.start() [q_in.put(val) for val in inp] q_in.put(LastEvent()) doubler.join() for i in xrange(100): out = q_out.get() self.assertEquals(out.value, expected_output[i].value) self.assertEquals(out.tag, expected_output[i].tag) self.assertTrue(q_out.get().last)
class DelayTests(unittest.TestCase): def setUp(self): self.q_in = Channel() self.q_out = Channel() def tearDown(self): del self.q_in del self.q_out def test_basic_delay(self): '''Test delaying a basic integer tagged signal by 1''' delay = 2 input1 = [Event(value=1, tag=i) for i in xrange(100)] expected_output = [Event(value=1, tag=i + delay) for i in xrange(100)] block = Delay(self.q_in, self.q_out, delay) block.start() [self.q_in.put(i) for i in input1 + [LastEvent()]] block.join() actual_output = [self.q_out.get() for i in xrange(100)] [self.assertEquals(actual_output[i], expected_output[i]) for i in xrange(100)] self.assertTrue(self.q_out.get().last) def test_complex_delay(self): '''Test delaying a CT signal''' delay = 11.5 # Delay by this amount simulation_time = 120 # seconds to simulate resolution = 10.0 # samples per second (10hz) tags = linspace(0, simulation_time, simulation_time / resolution) values = arange(len(tags)) data_in = [Event(value = values[i], tag = tags[i]) for i in xrange(len(tags))] expected_output = [Event(value = values[i], tag = tags[i] + delay) for i in xrange(len(tags))] block = Delay(self.q_in, self.q_out, delay) block.start() [self.q_in.put(i) for i in data_in + [LastEvent()]] block.join() actual_output = [self.q_out.get() for i in xrange(len(tags))] [self.assertEquals(actual_output[i], expected_output[i]) for i in xrange(len(tags))] self.assertTrue(self.q_out.get().last)
def __init__(self, out, amplitude=1.0, freq=0.01, phi=0.0, simulation_length=100): ''' Construct a discrete sin generator model. @param out: output channel ''' super(DTSinGenerator, self).__init__(output_channel=out) logging.debug("Setting model paramaters.") self.amplitude = amplitude self.frequency = freq self.phase = phi self.simulation_length = simulation_length assert out.domain is "DT" logging.info('Constructing the inner actors that make up this "model"') self.chan1 = Channel() # The values of the ramp will be ignored, it just provides the "clock" self.ramp = Ramp(self.chan1, resolution=1, simulation_time=self.simulation_length) # Note in this case we have connected the output of the last inner component directly to # the output of the model - this isn't always going to be the case. self.sin = Sin(self.chan1, self.output_channel, amplitude=self.amplitude, freq=self.frequency, phi=self.phase) logging.info("Inner actors have been constructed")
class DTIntegratorTests(unittest.TestCase): '''Test the integrator actors''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel('DT') self.q_out = Channel('DT') def test_backward_euler(self): '''Test backward Euler integration of a simple positive integer signal. ''' inp = [Event(value=i, tag=i) for i in xrange(0, 10, 1)] expected_output_values = [sum(range(i)) for i in xrange(1, 11)] block = DTIntegratorBackwardEuler(self.q_in, self.q_out) block.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) block.join() for expected_output in expected_output_values: out = self.q_out.get() self.assertEquals(out.value, expected_output) self.assertTrue(self.q_out.get().last) def test_forward_euler(self): '''Test forward Euler integration of a simple positive integer signal. ''' inp = [Event(value=i, tag=i) for i in xrange(0, 10, 1)] expected_output_values = [sum(range(i)) for i in xrange(0, 10)] block = DTIntegratorForwardEuler(self.q_in, self.q_out) block.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) block.join() for expected_output in expected_output_values: out = self.q_out.get() self.assertEquals(out.value, expected_output) self.assertTrue(self.q_out.get().last) def test_trapezoidal(self): '''Test trapezoidal integration of a simple positive integer signal. ''' inp = [Event(value=i, tag=i) for i in xrange(0, 10, 1)] x_avgs = [0.5 * (x + (x - 1)) for x in xrange(1, 11, 1)] expected_output_values = [sum(x_avgs[:i]) for i in range(10)] block = DTIntegratorTrapezoidal(self.q_in, self.q_out) block.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) block.join() for expected_output in expected_output_values: out = self.q_out.get() self.assertEquals(out.value, expected_output) self.assertTrue(self.q_out.get().last)
class CTIntegratorTests(unittest.TestCase): '''Test the integrator actors''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel() self.q_out = Channel() def test_forward_euler(self): '''Test forward Euler integration of a simple positive integer signal. ''' inp = [Event(value = i, tag = i) for i in xrange(0, 10, 1)] expected_output_values = [sum(range(i)) for i in xrange(1, 11)] block = CTIntegratorForwardEuler(self.q_in, self.q_out) block.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) block.join() for expected_output in expected_output_values: out = self.q_out.get() self.assertEquals(out.value, expected_output) self.assertTrue(self.q_out.get().last) def test_different_rate(self): '''Test that integration of a simple positive constant signal doesn't change with samplerate. ''' from scipy import arange inp_1 = [Event(value = 10, tag = i) for i in arange(0, 10, 1)] inp_2 = [Event(value = 10, tag = i) for i in arange(0, 10, 0.1)] expected_output_values_1 = [10 * i for i in arange(0, 10, 1)] expected_output_values_2 = [10 * i for i in arange(0, 10, 0.1)] for Block in [CTIntegratorForwardEuler]: q_in1, q_out1, q_in2, q_out2 = Channel(), Channel(), Channel(), Channel() block1 = Block(q_in1, q_out1) block2 = Block(q_in2, q_out2) block1.start(); block2.start() [q_in1.put(val) for val in inp_1] [q_in2.put(val) for val in inp_2] q_in1.put(LastEvent()); q_in2.put(LastEvent()) block1.join(); block2.join() out = [] for expected_output in expected_output_values_1: out.append(q_out1.get()) out = [item.value for item in out] self.assertEquals(len(out), len(expected_output_values_1)) #[self.assertEquals(out[i], expected_output[i]) for i, _ in enumerate(expected_output_values_1)] self.assertTrue(q_out1.get().last) for expected_output in expected_output_values_2: out = q_out2.get() self.assertAlmostEquals(out.value, expected_output) self.assertTrue(q_out2.get().last)
def test_basic_summer(self): '''Test adding two channels of complete pairs together''' q_in_1 = Channel('DT') q_in_2 = Channel('DT') q_out = Channel('DT') input1 = [Event(value=1, tag=i) for i in xrange(100)] input2 = [Event(value=2, tag=i) for i in xrange(100)] summer = Summer([q_in_1, q_in_2], q_out) summer.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) q_in_1.put(LastEvent()) q_in_2.put(LastEvent()) summer.join() for i in xrange(100): self.assertEquals(q_out.get()['value'], 3) self.assertTrue(q_out.get().last)
def test_signed_summer(self): '''Test subtracting one channel from another''' q_in_1 = Channel('DT') q_in_2 = Channel('DT') q_out = Channel('DT') input1 = [Event(value=1, tag=i) for i in xrange(100)] input2 = [Event(value=2, tag=i) for i in xrange(100)] summer = Summer([q_in_1, (q_in_2, '-')], q_out) summer.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) q_in_1.put(LastEvent()) q_in_2.put(LastEvent()) summer.join() for i in xrange(100): self.assertEquals(q_out.get()['value'], -1) self.assertTrue(q_out.get().last)
class SamplerTests(unittest.TestCase): '''Test the sampling actor''' def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("DT") self.q_out = Channel("DT") def test_basic_integer_tags(self): '''Test halving the frequency we sample a simple integer signal. Create a discrete time signal with a 1hz frequency and down-sample to 0.5hz ''' inp = [Event(value=1, tag=i) for i in xrange(0, 100, 1)] expected_outputs = [Event(value=1, tag=i) for i in xrange(0, 100, 2)] down_sampler = Sampler(self.q_in, self.q_out, 0.5) down_sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) down_sampler.join() for expected_output in expected_outputs: out = self.q_out.get() self.assertEquals(out.value, expected_output.value) self.assertEquals(out.tag, expected_output.tag) self.assertTrue(self.q_out.get().last) def test_compatible_signals(self): '''Test reducing the frequency of a more complicated signal. Create a signal of 120 seconds, with 10 samples per second. (10hz) Down-sample this to a 2hz signal. ''' simulation_time = 120 # seconds to simulate resolution = 10.0 # samples per second (10hz) desired_resolution = 2 # what we want out - (2hz) # Create tags for a signal from 0 to 120 seconds. # length = number of seconds * ( samples per second + 1) freq = simulation_time * resolution tags = linspace(0, simulation_time , (freq) + 1) # Create 120 seconds of a discrete time signal with a 10hz frequency inp = [Event(value=1, tag=i) for i in tags] step = resolution / desired_resolution expected_output = [ Event(value=1, tag=i) for i in tags[::step]] down_sampler = Sampler(self.q_in, self.q_out, desired_resolution) down_sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) down_sampler.join() for expected_output_element in expected_output: out = self.q_out.get() self.assertEquals(out.value, expected_output_element.value) self.assertEquals(out.tag, expected_output_element.tag) self.assertTrue(self.q_out.get().last) def test_incompatible_signals(self): '''Test reducing the frequency by non integer factor. First create a signal of 120 seconds, with 10 samples per second. Down-sample this to a 8hz signal. ''' simulation_time = 120 # seconds to simulate resolution = 10.0 # samples per second (10hz) desired_resolution = 8 # what we want out - (8hz) # Create tags for a signal from 0 to 120 seconds. # length = number of seconds * ( samples per second + 1) freq = simulation_time * resolution tags = linspace(0, simulation_time , (freq) + 1) # Create 120 seconds of a discrete time signal with a 10hz frequency inp = [Event(value=1, tag=i) for i in tags] down_sampler = Sampler(self.q_in, self.q_out, desired_resolution) down_sampler.start() [self.q_in.put(val) for val in inp] self.q_in.put(LastEvent()) down_sampler.join() out = self.q_out.get() # @todo: NEED TO WORK OUT HOW WE WANT TO DO THIS... #self.assertEquals(type(out), InvalidSimulationInput) self.assertTrue(self.q_out.get().last)
def test_ct_summer_with_different_rates(self): ''' Test adding two channels where one is operates at a different rate than the other. The sum should appear at the fastest rate, with values that assume constant-interpolation between events in the slower channel. ''' DELAY = 2 q_in_1 = Channel('CT') q_in_2 = Channel('CT') q_out = Channel('CT') input1 = [Event(value=i, tag=i) for i in xrange(1,100)] input2 = [Event(value=2*i, tag=2*i) for i in xrange(1,50)] summer = Summer([q_in_1, q_in_2], q_out) summer.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) q_in_1.put(LastEvent()) q_in_2.put(LastEvent()) summer.join() self.assertEquals(q_out.get()['value'], 1.0) for i in xrange(2, 98, 2): self.assertEquals(q_out.get()['value'], i + i) self.assertEquals(q_out.get()['value'], i + i + 1) self.assertEquals(q_out.get()['value'], 196) self.assertTrue(q_out.get().last)
def setUp(self): self.q_in = Channel() self.q_out = Channel()
def setUp(self): ''' Unit test setup code ''' self.q_in = Channel("CT") self.q_out = Channel("DE")
def test_interleaving_merge(self): ''' Test merging two channels that have different numbers of events, and don't simply alternate their tags. ''' q_in_1 = Channel() q_in_2 = Channel() q_out = Channel() input1 = [Event(value=1, tag=2.0*i) for i in xrange(3)] + [LastEvent()] input2 = [Event(value=2, tag=0.5*i) for i in xrange(11)] + [LastEvent()] merge = Merge([q_in_1, q_in_2], q_out) merge.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) merge.join() self.assertEquals(q_out.head().tag, 0.0) self.assertEquals(q_out.get().value, 1) # 0 self.assertEquals(q_out.head().tag, 0.0) self.assertEquals(q_out.get().value, 2) # 0 self.assertEquals(q_out.head().tag, 0.5) self.assertEquals(q_out.get().value, 2) # 0.5 self.assertEquals(q_out.head().tag, 1.0) self.assertEquals(q_out.get().value, 2) # 1.0 self.assertEquals(q_out.head().tag, 1.5) self.assertEquals(q_out.get().value, 2) # 1.5 self.assertEquals(q_out.head().tag, 2.0) self.assertEquals(q_out.get().value, 1) self.assertEquals(q_out.head().tag, 2.0) self.assertEquals(q_out.get().value, 2) self.assertEquals(q_out.head().tag, 2.5) self.assertEquals(q_out.get().value, 2) self.assertEquals(q_out.head().tag, 3.0) self.assertEquals(q_out.get().value, 2) self.assertEquals(q_out.head().tag, 3.5) self.assertEquals(q_out.get().value, 2) self.assertEquals(q_out.head().tag, 4.0) self.assertEquals(q_out.get().value, 1) self.assertEquals(q_out.head().tag, 4.0) self.assertEquals(q_out.get().value, 2) self.assertTrue(q_out.get().last) self.assertTrue(q_out.get().last)
def test_delayed_summer3(self): ''' Test adding two channels where one is delayed by ONE time step difference Summer is set up to SUM incomplete sets ''' q_in_1 = Channel('DT') q_in_2 = Channel('DT') q_out = Channel('DT') input1 = [Event(value=1, tag=i) for i in xrange(100)] # First tag is 0, last tag is 99. input2 = [Event(value=2, tag=i + 1) for i in xrange(100)] # First tag is 1, last tag is 100. summer = DTSummer([q_in_1, q_in_2], q_out, False) summer.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) q_in_1.put(LastEvent()) q_in_2.put(LastEvent()) summer.join() # First value should be 1, next 99 should be 3, last should be 2. data = q_out.get() self.assertEquals(data['value'], 1) self.assertEquals(data['tag'], 0) for i in xrange(1, 100): data = q_out.get() self.assertEquals(data['value'], 3) self.assertEquals(data['tag'], i) data = q_out.get() # lastly the channel should contain a terminal event self.assertTrue(q_out.get().last)
def test_delayed_summer2(self): ''' Test adding two channels where one is delayed by an arbitrary time step difference. Summer is set up to discard incomplete sets ''' DELAY = 2 q_in_1 = Channel('DT') q_in_2 = Channel('DT') q_out = Channel('DT') input1 = [Event(value=1, tag=i) for i in xrange(100)] input2 = [Event(value=2, tag=i + DELAY) for i in xrange(100)] summer = DTSummer([q_in_1, q_in_2], q_out, True) summer.start() for val in input1: q_in_1.put(val) for val in input2: q_in_2.put(val) q_in_1.put(LastEvent()) q_in_2.put(LastEvent()) summer.join() for i in xrange(DELAY, 100): self.assertEquals(q_out.get()['value'], 3) self.assertTrue(q_out.get().last)