def test_from_parameters_force_width(self, width, latching, tc): # Create the mock signal and connection signal = SignalParameters(latching=latching) rps = ReceptionParameters(nengo.Lowpass(tc), width, None) # Create the filter lpf = LowpassFilter.from_parameters(signal, rps, width=2) assert lpf == LowpassFilter(2, latching, tc)
def test_equivalent_filters(self, minimise): """Test construction of filter regions from signals and keyspaces.""" # Create two keyspaces, two signal parameters and two reception # parameters with equivalent synapses. ks_a = mock.Mock(name="Keyspace[A]") signal_a = SignalParameters(keyspace=ks_a, latching=False) ks_b = mock.Mock(name="Keyspace[B]") signal_b = SignalParameters(keyspace=ks_b, latching=False) rp_a = ReceptionParameters(nengo.Lowpass(0.01), 3, None) rp_b = ReceptionParameters(nengo.Lowpass(0.01), 3, None) # Create the data structure that is expected as input specs = [ ReceptionSpec(signal_a, rp_a), ReceptionSpec(signal_b, rp_b), ] # Create the regions, with minimisation filter_region, routing_region = make_filter_regions( specs, 0.001, minimise=minimise, filter_routing_tag="spam", index_field="eggs") # Check that the filter region is as expected assert filter_region.dt == 0.001 if minimise: assert len(filter_region.filters) == 1 assert filter_region.filters[0] == LowpassFilter(3, False, 0.01) else: assert len(filter_region.filters) == 2 assert filter_region.filters[0] == LowpassFilter(3, False, 0.01) assert filter_region.filters[1] == LowpassFilter(3, False, 0.01) # Check that the routing region is as expected assert routing_region.filter_routing_tag == "spam" assert routing_region.index_field == "eggs" if minimise: assert (signal_a, 0) in routing_region.signal_routes assert (signal_b, 0) in routing_region.signal_routes else: if (signal_a, 0) in routing_region.signal_routes: assert (signal_b, 1) in routing_region.signal_routes else: assert (signal_b, 0) in routing_region.signal_routes
def test_standard(self, width, latching, dt, tc): """Test creating and writing out lowpass filters.""" lpf = LowpassFilter(width, latching, tc) # Pack the filter into some data data = bytearray(lpf.size) lpf.pack_into(dt, data) # Check the values are sane v1, v2, mask, size = struct.unpack("<4I", data) val = math.exp(-dt / tc) assert v1 == tp.value_to_fix(val) assert v2 == tp.value_to_fix(1 - val) assert mask == (0xffffffff if latching else 0x00000000) assert size == width
def test_pack_data(self, tau, dt): # Create the filter f = LowpassFilter(0, False, tau) # Pack into an array data = bytearray(8) f.pack_data(dt, data, 0) # Compute expected values exp_a = tp.value_to_fix(np.exp(-dt / tau)) exp_b = tp.value_to_fix(1.0 - np.exp(-dt / tau)) # Unpack and check for accuracy (a, b) = struct.unpack_from("<2I", data) assert a == exp_a assert b == exp_b
def test_forced_filter_width(self): """Test construction of filter regions from signals and keyspaces.""" # Create two keyspaces, two signals and two connections with equivalent # synapses. # Create two keyspaces, two signals and two reception parameters with # different synapses. ks_a = mock.Mock(name="Keyspace[A]") signal_a = SignalParameters(keyspace=ks_a, latching=False) ks_b = mock.Mock(name="Keyspace[B]") signal_b = SignalParameters(keyspace=ks_b, latching=False) rp_a = ReceptionParameters(nengo.Lowpass(0.01), 3, None) rp_b = ReceptionParameters(None, 5, None) # Create the type of dictionary that is expected as input specs = [ ReceptionSpec(signal_a, rp_a), ReceptionSpec(signal_b, rp_b), ] # Create the regions, with minimisation filter_region, routing_region = make_filter_regions(specs, 0.001, width=1) # Check that the filter region is as expected for f in filter_region.filters: assert (f == LowpassFilter(1, False, 0.01) or f == NoneFilter(1, False)) # noqa: E711
def test_filter_region_force_width(): """Test creation of a filter data region.""" # Create two filters fs = [LowpassFilter(5, False, 0.1), NoneFilter(3, False)] # Create the filter region fr = FilterRegion(fs, dt=0.001) # The size should be correct (count of words + header 1 + data 1 + header 2 # + data 2) assert fr.sizeof() == 4 + 16 + 8 + 16 + 0 # Check that the data is written out correctly fp = tempfile.TemporaryFile() fr.write_subregion_to_file(fp, filter_width=1) fp.seek(0) length, = struct.unpack("<I", fp.read(4)) assert length == len(fs) expected_data = bytearray(fr.sizeof() - 4) fs[0].pack_into(fr.dt, expected_data, width=1) fs[1].pack_into(fr.dt, expected_data, (fs[0].size_words() + 4) * 4, width=1) assert fp.read() == expected_data
def test_eq(self): lpfs = [ LowpassFilter(5, True, 0.01), LowpassFilter(5, True, 0.02), LowpassFilter(3, True, 0.01), LowpassFilter(5, False, 0.01) ] for lpf in lpfs[1:]: assert lpf != lpfs[0] lpf = LowpassFilter(5, True, 0.01) assert lpf == lpfs[0] nf = NoneFilter(5, True) assert lpf != nf and nf != lpf
def test_pack_data_tau_zero(self): # Create the filter f = LowpassFilter(0, False, 0) # Pack into an array data = bytearray(8) f.pack_data(0.001, data, 0) # Compute expected values exp_a = tp.value_to_fix(0.0) exp_b = tp.value_to_fix(1.0) # Unpack and check for accuracy (a, b) = struct.unpack_from("<2I", data) assert a == exp_a assert b == exp_b
def test_from_parameters_force_width(self, width, latching, tc): # Create the mock signal and connection signal = SignalParameters(latching=latching) rps = ReceptionParameters(nengo.Lowpass(tc), width) # Create the filter lpf = LowpassFilter.from_parameters(signal, rps, width=2) assert lpf == LowpassFilter(2, latching, tc)
def test_eq(self): # Check that linear filters are equal iff. they share a numerator and # denominator. lpf = LowpassFilter(0, False, 0.1) lf1 = LinearFilter(0, False, [1.0], [0.1, 0.2]) lf2 = LinearFilter(0, False, [1.0], [0.1, 0.3]) lf3 = LinearFilter(0, False, [1.0], [0.1, 0.2]) assert lf1 != lpf assert lf2 != lf1 assert lf3 != lf2 assert lf3 == lf1
def test_from_signal_and_connection_with_slice(self, latching, tc): # Create the mock signal and connection signal = mock.Mock(name="signal", spec_set=["latching"]) signal.latching = latching with nengo.Network(): a = nengo.Ensemble(100, 1) b = nengo.Ensemble(100, 2) connection = nengo.Connection(a, b[1], synapse=nengo.Lowpass(tc)) # Create the filter lpf = LowpassFilter.from_signal_and_connection(signal, connection) assert lpf == LowpassFilter(2, latching, tc)
def test_different_filters(self): """Test construction of filter regions from signals and keyspaces.""" # Create two keyspaces, two signals and two reception parameters with # different synapses. ks_a = mock.Mock(name="Keyspace[A]") signal_a = SignalParameters(keyspace=ks_a, latching=False) ks_b = mock.Mock(name="Keyspace[B]") signal_b = SignalParameters(keyspace=ks_b, latching=False) rp_a = ReceptionParameters(nengo.Lowpass(0.01), 3, None) rp_b = ReceptionParameters(None, 3, None) # Create the type of dictionary that is expected as input specs = [ ReceptionSpec(signal_a, rp_a), ReceptionSpec(signal_b, rp_b), ] # Create the regions, with minimisation filter_region, routing_region = make_filter_regions( specs, 0.001, minimise=True, # Shouldn't achieve anything filter_routing_tag="spam", index_field="eggs") # Check that the filter region is as expected assert filter_region.dt == 0.001 assert len(filter_region.filters) == 2 for f in filter_region.filters: assert (f == LowpassFilter(3, False, 0.01) or f == NoneFilter(3, False)) # noqa: E711 # Check that the routing region is as expected assert routing_region.filter_routing_tag == "spam" assert routing_region.index_field == "eggs" if (signal_a, 0) in routing_region.signal_routes: assert (signal_b, 1) in routing_region.signal_routes else: assert (signal_b, 0) in routing_region.signal_routes