PyNN icon indicating copy to clipboard operation
PyNN copied to clipboard

Mixed type parameters in FromListConnector

Open nstepp opened this issue 5 years ago • 3 comments

I was attempting to use FromListConnector with a custom synapse that has boolean parameters plastic and inhibitory, e.g.

sim.FromListConnector(connections, column_names=['weight','delay','plastic','inhibitory'])

where connections is a list with elements like (i,j,0.5,5.0,True,False).

What ends up happening, however, is that the connection list gets turned into a numpy array with a dtype of float (in FromListConnector.__init__). This causes a failure later when the ParameterSpace is updated, due to mismatched types (numpy.can_cast fails). The type from the connection list has been cast to float, but the schema type is bool.

I attempted to pass in a numpy array with dtype=object, but that still fails can_cast.

Is this something that should work, or are mixed type parameters not intended to be supported? If it should work, the logic in FromListConnector might want to preserve the connection list elements as tuples and treat them that way in FromListConnector.connect. I would be happy to make such changes and submit a PR.

Here is the relevant part of the stacktrace:

  File "/usr/local/lib/python3.7/site-packages/pyNN/connectors.py", line 541, in connect
    connection_parameters.update(**{name: self.conn_list[l:r, col]})
  File "/usr/local/lib/python3.7/site-packages/pyNN/parameters.py", line 311, in update
    dtype=expected_dtype)
  File "/usr/local/lib/python3.7/site-packages/pyNN/parameters.py", line 62, in __init__
    super(LazyArray, self).__init__(value, shape, dtype)
  File "/usr/local/lib/python3.7/site-packages/lazyarray.py", line 196, in __init__
    assert numpy.can_cast(value.dtype, dtype, casting='safe')  # or could convert value to the provided dtype

nstepp avatar Sep 09 '19 06:09 nstepp

This should work, but we've never had non-float parameters before, so we've taken shortcuts.

A PR would be very welcome. I'm not sure if preserving the connection elements as tuples will work, another approach would be to use a NumPy record array or structured array to preserve the dtypes of the individual columns

apdavison avatar Nov 14 '19 13:11 apdavison

Thanks for the reply; agreed about the tuples.

nstepp avatar Nov 14 '19 21:11 nstepp

I just came across this ancient thread and wanted to leave a note to explain that I couldn't submit a PR because of legalities with my company. Sorry :/

nstepp avatar Dec 10 '21 02:12 nstepp