def pack_data(self, dt, buffer, offset=0): """Pack the struct describing the filter into the buffer.""" # Compute the coefficients a = np.exp(-dt / self.time_constant) b = 1.0 - a struct.pack_into("<2I", buffer, offset, tp.value_to_fix(a), tp.value_to_fix(b))
def test_write_subregion_to_file( self, machine_timestep, dt, size_in, tau_ref, tau_rc, size_out, probe_spikes, vertex_slice, vertex_neurons ): # Check that the region is correctly written to file region = lif.SystemRegion(size_in, size_out, machine_timestep, tau_ref, tau_rc, dt, probe_spikes) # Create the file fp = tempfile.TemporaryFile() # Write to it region.write_subregion_to_file(fp, vertex_slice) # Read back and check that the values are sane fp.seek(0) values = fp.read() assert len(values) == region.sizeof() (n_in, n_out, n_n, m_t, t_ref, dt_over_t_rc, rec_spikes, i_dims) = struct.unpack_from("<8I", values) assert n_in == size_in assert n_out == size_out assert n_n == vertex_neurons assert m_t == machine_timestep assert t_ref == int(tau_ref // dt) assert ( tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 0.9 < dt_over_t_rc < tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 1.1 ) assert (probe_spikes and rec_spikes != 0) or (not probe_spikes and rec_spikes == 0) assert i_dims == 1
def pack_into(self, dt, buffer, offset=0): """Pack the struct describing the filter into the buffer.""" val = math.exp(-dt / self.time_constant) struct.pack_into(self._pack_chars, buffer, offset, tp.value_to_fix(val), tp.value_to_fix(1 - val)) super(LowpassFilter, self).pack_into( dt, buffer, offset + struct.calcsize(self._pack_chars))
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_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_standard(self, width, latching): nf = NoneFilter(width, latching) # Pack the filter into some data data = bytearray(nf.size) nf.pack_into(0.001, data) # Check the values are sane v1, v2, mask, size = struct.unpack("<4I", data) assert v1 == 0 assert v2 == tp.value_to_fix(1.0) assert mask == (0xffffffff if latching else 0x00000000) assert size == width
def test_LIFRegion(dt, tau_rc, tau_ref): """Test region specific to LIF neurons.""" # Create the region region = lif.LIFRegion(dt, tau_rc, tau_ref) # Check that the size is reported correctly assert region.sizeof() == 2*4 # 2 words # Check that the data is written out correctly # Create the file and write to it fp = tempfile.TemporaryFile() region.write_subregion_to_file(fp) # Read everything back fp.seek(0) values = fp.read() assert len(values) == region.sizeof() # Unpack and check that the values written out are reasonable dt_over_t_rc, t_ref = struct.unpack("<2I", values) assert t_ref == int(tau_ref // dt) assert (tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 0.9 < dt_over_t_rc < tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 1.1)
def test_LIFRegion(dt, tau_rc, tau_ref): """Test region specific to LIF neurons.""" # Create the region region = lif.LIFRegion(dt, tau_rc, tau_ref) # Check that the size is reported correctly assert region.sizeof() == 2 * 4 # 2 words # Check that the data is written out correctly # Create the file and write to it fp = tempfile.TemporaryFile() region.write_subregion_to_file(fp) # Read everything back fp.seek(0) values = fp.read() assert len(values) == region.sizeof() # Unpack and check that the values written out are reasonable dt_over_t_rc, t_ref = struct.unpack("<2I", values) assert t_ref == int(tau_ref // dt) assert (tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 0.9 < dt_over_t_rc < tp.value_to_fix(-np.expm1(-dt / tau_rc)) * 1.1)
def write_subregion_to_file(self, fp): """Write the region to the file-like object.""" # The value -e^(-dt / tau_rc) is precomputed and is scaled down ever so # slightly to account for the effects of fixed point. The result is # that the tuning curves of SpiNNaker neurons are usually within 5Hz of # the ideal curve and the tuning curve of reference Nengo neurons. The # fudge factor applied (i.e. 1.0*2^-11) was determined by running the # tuning curve test in "regression-tests/test_tuning_curve.py", # plotting the results and stopping when the ideal tuning curve was # very closely matched by the SpiNNaker tuning curve - further # improvement of this factor may be possible. fp.write( struct.pack( "<2I", tp.value_to_fix(-np.expm1(-self.dt / self.tau_rc) * (1.0 - 2**-11)), int(self.tau_ref // self.dt)))
def write_subregion_to_file(self, fp): """Write the region to the file-like object.""" # The value -e^(-dt / tau_rc) is precomputed and is scaled down ever so # slightly to account for the effects of fixed point. The result is # that the tuning curves of SpiNNaker neurons are usually within 5Hz of # the ideal curve and the tuning curve of reference Nengo neurons. The # fudge factor applied (i.e. 1.0*2^-11) was determined by running the # tuning curve test in "regression-tests/test_tuning_curve.py", # plotting the results and stopping when the ideal tuning curve was # very closely matched by the SpiNNaker tuning curve - further # improvement of this factor may be possible. fp.write(struct.pack( "<2I", tp.value_to_fix( -np.expm1(-self.dt / self.tau_rc) * (1.0 - 2**-11) ), int(self.tau_ref // self.dt) ))
def write_subregion_to_file(self, fp, vertex_slice): """Write the system region for a specific vertex slice to a file-like object. """ # Prepare the flags, these indicate any additional tasks to be # performed by the executable. flags = 0x0 if self.probe_spikes: flags |= 1 << 0 if self.probe_voltages: flags |= 1 << 1 # The value -e^(-dt / tau_rc) is precomputed and is scaled down ever so # slightly to account for the effects of fixed point. The result is # that the tuning curves of SpiNNaker neurons are usually within 5Hz of # the ideal curve and the tuning curve of reference Nengo neurons. # The fudge factor applied (i.e. 1.0*2^-11) was determined by running # the tuning curve test in "regression-tests/test_tuning_curve.py", # plotting the results and stopping when the ideal tuning curve was # very closely matched by the SpiNNaker tuning curve - further # improvement of this factor may be possible. n_neurons = vertex_slice.stop - vertex_slice.start data = struct.pack( "<9I", self.n_input_dimensions, self.n_output_dimensions, n_neurons, self.machine_timestep, int(self.t_ref // self.dt), # tau_ref expressed as in ticks tp.value_to_fix(-np.expm1(-self.dt / self.t_rc) * (1.0 - 2**-11)), flags, 1, self.num_profiler_samples ) fp.write(data)
def pack_into(self, dt, buffer, offset=0): """Pack the struct describing the filter into the buffer.""" struct.pack_into(self._pack_chars, buffer, offset, tp.value_to_fix(0.0), tp.value_to_fix(1.0)) super(NoneFilter, self).pack_into( dt, buffer, offset + struct.calcsize(self._pack_chars))