For large arrays, copying between numpy and the hoc Vector is not only time-inefficient, it is also memory inefficient. (There are, at least transiently, two copies of the same data in memory, one as numpy.array, the other as hoc Vector.) Eventually, I hope, Vector.from_python() will allow hoc Vectors to share memory with numpy arrays which would eliminate both types of inefficiency.
Until then, here is a proposed feature which would sometimes avoid the memory-inefficiency: How about allowing a Python generator to be the argument of from_python().
1) There's no question that this would be a feature of a hypothetical utopian future totally-Pythonic version of NEURON.
2) In some circumstances this would avoid keeping two copies of data in memory. For example, suppose the generator reads data from a file on disk, or computes a function f(x) over a range of values of x.
3) I don't know much about the internals of Python or hoc, but it seems like this might be easier to implement than memory sharing. I think from_python() just needs to check if its argument is a list or numpy.array, and then, if not, check if its argument has a next() method, and then, if so call the next() method repeatedly, and finally, catch a StopIteration exception.
4) This would allow any Python enumerable type to be copied into a hoc Vector, without first copying it into a list or numpy.array
Just write a generator expression:
Code: Select all
myVec.from_python((x for x in myEnumerableObject))
As it is, you have to write:
Code: Select all
myVec.from_python([x for x in myEnumerableObject])
Note () generator expression, rather than [] list expansion. The list expansion generates a temporary, anonymous list, which could be memory-expensive if myEnumerableObject is large.