Page **1** of **1**

### Convert python array to neuron vector

Posted: **Thu May 03, 2012 2:45 pm**

by **Prokopiou**

Hello,

I have been trying to convert a python array ( numpy.ndarray ) into a neuron vector.

I used the command:

Code: Select all

`neuron_vector.from_python(python_array)`

and it does work (dimension-wise) but doesn't get the correct values. The only value the neuron vector stores is 0 but I noticed when the sign changes in python so does in neuron vector. So I have a vector full of -0 and 0.

This makes me think that there must be an error in the bits the number is represented, each value in the python array is a 'numpy.float64'.

Is there an issue in transferring 64 bit numbers using the from_python() function?

Thanks

### Re: Convert python array to neuron vector

Posted: **Thu May 03, 2012 3:29 pm**

by **Prokopiou**

Hello,

Please ignore my previous post, it does convert the 64bit array when I test it with a different array which confuses me even more now...

Code: Select all

```
stim_vec = h.Vector()
time_vec = h.Vector()
time_vec.from_python(t)
i = 0
for sec in h.allsec():
for seg in sec:
stim_vec.from_python(external_voltage[:,i])
stim_vec.play(seg._ref_e_extracellular, time_vec)
i += 1
```

this is the code that seems to not work...

t is an array in python which converts just fine to a neuron vector.

the external_voltage[:,i] gives back 1D array (same length as t) but the stim_vec remains at zero with alternating sign (negative-positive) when the external_voltage changes value. I cant really see why it should do this.

Thanks

### Re: Convert python array to neuron vector

Posted: **Fri May 04, 2012 8:08 am**

by **hines**

Since there is only one instance of stim_vec, I would have expected every segment to get the same last external_voltage 1d array.

Perhaps you need an array of stim_vec as in

stim_vec = []

...........

stim_vec.append(Vector(external_voltage[:,i])

stim_vec[-1].play(seg....)

However, I would not have expected that last external voltage to be alternating +-0 so perhaps there is a bug in the

transfer of the numpy 1-d array to a Vector. Your fragment does not exhibit the creation of the 2-d numpy array. A complete

fragment would help me see the problem and work on a diagnosis. The following test

Code: Select all

```
import numpy
a=numpy.matrix([[1,2],[3,4]])
from neuron import h
v = h.Vector(a[:,0])
v.printf()
for i in range(0,2):
v.from_python(a[:,i])
v.printf()
```

produces the correct output

### Re: Convert python array to neuron vector

Posted: **Fri May 04, 2012 12:38 pm**

by **Prokopiou**

I seem to have resolved the problem...in a way that doesn't make sense to me so it is still a problem I guess..

Im using a play , record pair to stimulate the cell with an external voltage and this is the code I use:

Code: Select all

```
time_vec = h.Vector()
time_vec.from_python(t)
test_vec = []
i = 0
for sec in h.allsec():
for seg in sec:
stim_vec = h.Vector()
stim_vec.from_python(external_voltage[:,i])
test_vec.append(stim_vec)
stim_vec.play(seg._ref_e_extracellular, time_vec)
i += 1
results = []
for sec in h.allsec():
for seg in sec:
rec_vec = h.Vector()
rec_vec.record(seg._ref_v)
results.append(rec_vec)
neuron.init()
neuron.run(time)
results_list = []
for v in results:
temp = v.to_python()
temp.remove(temp[0])
results_list.append(temp)
internal_voltage = np.array(results_list)
```

this seems to work just fine, what I don't quite understand is when I comment out the test_vec like this:

Code: Select all

```
time_vec = h.Vector()
time_vec.from_python(t)
#test_vec = []
i = 0
for sec in h.allsec():
for seg in sec:
stim_vec = h.Vector()
stim_vec.from_python(external_voltage[:,i])
#test_vec.append(stim_vec)
stim_vec.play(seg._ref_e_extracellular, time_vec)
i += 1
results = []
for sec in h.allsec():
for seg in sec:
rec_vec = h.Vector()
rec_vec.record(seg._ref_v)
results.append(rec_vec)
neuron.init()
neuron.run(time)
results_list = []
for v in results:
temp = v.to_python()
temp.remove(temp[0])
results_list.append(temp)
internal_voltage = np.array(results_list)
```

The problem I was talking about before seems to pop up again. This confuses me and makes me wonder if im using the play record pair correctly in this python code.

Thanks,

Andreas.

### Re: Convert python array to neuron vector

Posted: **Fri May 04, 2012 3:52 pm**

by **hines**

by not putting the stim_vec instance in the test_vec, then when

stim_vec = h.Vector()

is executed, the reference count of the Vector previously referenced by stim_vec goes to 0

and that deletes that Vector.