I started playing with numpy two weeks ago. I am a begginer in programming so I don't know if my computer or software can be the cause, but I find it very buggy. I am using Python 2.7. and windows powershell. Sometimes I am getting something like this (especially in bigger arrays) and sometimes not:

import numpy as np

>>> a = np.arange(9)

>>> a.resize((3,3))

>>> a

array([[0,1,2],

[3,4,5],

[6,7,8]])

>>> b = a[2,2]

>>> b

8

>>> b = 234

>>> a

>>>[[0,1,2]

[3,4,5]

[6,7,234]]

When I try to make something just slightly more complex, numpy skips some lines of code. For exlample, I made a program with about 200 lines of codes and seceral ndarrays with dimensions(20,11,19). Near the end of a program it just fails to do the folowing, it simply skips it:

array[array>0] +=1

This will work sometimes and sometimes it will not. If someone could tell me what could be the problem, or if numpy is just too buggy.

## 16 Replies - 1319 Views - Last Post: 21 December 2016 - 04:29 AM

##
**Replies To:** Numpy problem, bugs

### #2

## Re: Numpy problem, bugs

Posted 17 November 2016 - 10:56 AM

I just tried what you wrote:

I don't understand the last part of your problem, maybe you should show the code along with expected result and actual result.

>>> import numpy as np >>> a = np.arange(9) >>> a.resize((3, 3)) >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> b = a[2,2] >>> b 8 >>> b = 234 >>> a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>>I don't get the behovair you described.

I don't understand the last part of your problem, maybe you should show the code along with expected result and actual result.

### #3

## Re: Numpy problem, bugs

Posted 17 November 2016 - 01:25 PM

**Ok, if someone would try to run this code and report the output.**It is for Python 2.7 so i think printing should be modified for Python 3. The first one is a simpler version and it works fine. The second should produce the same output but it isn't. In the second version of the code, line 63 is supposed to add 1 where array 'leglo' > 0, but it's not doing it. It prints out the slice of 'leglo' at the end of a while loop and shows that there are 'ones in there. Then it comes back to the begining of a loop and in line 63 it should add one to where 'leglo > 0'. The thing works in a simpler versions. I simply can't see the mistake in the second version. Since it prints out that there are ones in leglo it should add one there. Also, when I tried in the begining to declare that leglo = sace, it would work "left to right and right to left".

Simpler version:

import numpy as np import time broj_pcela = 15000.0 temperatura = 25.0 gustina_pcela = 3.7 rezolucija = 4 vreme_izleganja = 200 n = 20 v = 11 h = 19 leglo = np.zeros((n, v, h), dtype = 'int16') sace = np.zeros((n, v , h), dtype = 'int16') pcele = np.zeros((n, v, h), dtype = 'int16') cntr_v = v / 2 cntr_h = h / 2 br_str = int(broj_pcela / (((v*h)*((1/gustina_pcela)*rezolucija**2)))) cntr_str = br_str/2 kor_gust = int(100*(1.0/((broj_pcela / (br_str * (v*h)))/rezolucija**2))) pcele[0:br_str,:,:] += kor_gust grd_ram = np.arange(br_str, dtype = 'int16') grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 kor = np.arange(0,v*h*0.00001, step = 0.00001, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor mapa_kor = - mapa_kor + 10000 jed_vremena = (0.5) * rezolucija**2 minut = 0.0 brojac = 0 vreme_leglo = int(vreme_izleganja/jed_vremena) cntr_ram = leglo[cntr_str,:,:] slob_cel = cntr_ram == 0 while np.any(slob_cel): minut = brojac * jed_vremena dan = int(minut/1440) sat = int(minut/60) sace[leglo == vreme_leglo] = 1 leglo[leglo == vreme_leglo] = 0 leglo[leglo>0] += 1 slob_cel = cntr_ram == 0 sledeca_celija = np.max(slob_cel * mapa_kor) cntr_ram[mapa_kor == sledeca_celija] = 1 leglo[cntr_str,:,:] = cntr_ram time.sleep(1) brojac += 1 print 'Leglo:' print leglo[cntr_str,:,:] print '\n' print 'Sace:' print sace[cntr_str,:,:] print "\n"

Second wersion:

import numpy as np import time broj_pcela = 15000.0 temperatura = 25.0 gustina_pcela = 3.7 rezolucija = 4 vreme_izleganja = 200 n = 20 v = 11 h = 19 leglo = np.zeros((n, v, h), dtype = 'int16') sace = np.zeros((n, v , h), dtype = 'int16') pcele = np.zeros((n, v, h), dtype = 'int16') cntr_v = v / 2 cntr_h = h / 2 br_str = int(broj_pcela / (((v*h)*((1/gustina_pcela)*rezolucija**2)))) cntr_str = br_str/2 kor_gust = int(100*(1.0/((broj_pcela / (br_str * (v*h)))/rezolucija**2))) pcele[0:br_str,:,:] += kor_gust grd_ram = np.arange(br_str, dtype = 'int16') grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 kor = np.arange(0,v*h*0.00001, step = 0.00001, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor mapa_kor = - mapa_kor + 10000 jed_vremena = (0.5) * rezolucija**2 minut = 0.0 brojac = 0 vreme_leglo = int(vreme_izleganja/jed_vremena) cntr_ram = leglo[cntr_str,:,:] slob_cel = cntr_ram == 0 while np.any(slob_cel): minut = brojac * jed_vremena dan = int(minut/1440) sat = int(minut/60) sace[leglo == vreme_leglo] = 1 leglo[leglo == vreme_leglo] = 0 leglo[leglo>0] += 1 slob_cel = cntr_ram == 0 sledeca_celija = np.max(slob_cel * mapa_kor) cntr_ram[mapa_kor == sledeca_celija] = 1 leglo[cntr_str,:,:] = cntr_ram time.sleep(1) brojac += 1 print 'Leglo:' print leglo[cntr_str,:,:] print '\n' print 'Sace:' print sace[cntr_str,:,:] print "\n"

Mistake, the second version with a 'bug' is actually this:

import numpy as np import time broj_pcela = 15000.0 temperatura = 25.0 gustina_pcela = 3.7 rezolucija = 4 vreme_izleganja = 200 n = 20 v = 11 h = 19 leglo = np.zeros((n, v, h), dtype = 'int16') sace = np.zeros((n, v , h), dtype = 'int16') pcele = np.zeros((n, v, h), dtype = 'int16') slob_cel_nas = leglo == 0 pokrivene_cel = pcele < 430 dobre_cel = slob_cel_nas | pokrivene_cel cntr_v = v / 2 cntr_h = h / 2 br_str = int(broj_pcela / (((v*h)*((1/gustina_pcela)*rezolucija**2)))) kor_gust = int(100*(1.0/((broj_pcela / (br_str * (v*h)))/rezolucija**2))) pcele[0:br_str,:,:] += kor_gust cntr_str = br_str /2 grd_ram = np.arange(n, dtype = 'int16') mapa_ramova = -((grd_ram - cntr_str)**2*10)+10000 ramovi_korekcija = np.arange(n, dtype = 'int16') mapa_ramova = mapa_ramova + ramovi_korekcija grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 kor = np.arange(0,v*h*0.00001, step = 0.00001, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor mapa_kor = - mapa_kor + 10000 mapa_kor.resize((1,v,h)) jed_vremena = (0.5) * rezolucija**2 minut = 0.0 brojac = 0 vreme_leglo = int(100/jed_vremena) while np.any(dobre_cel): sledeci_ram = np.any(dobre_cel, axis = (1,2)) sledeci_ram = np.max(sledeci_ram * mapa_ramova) trenutni_ram = mapa_ramova == sledeci_ram cntr_ram = leglo[trenutni_ram,:,:] slob_cel = cntr_ram == 0 while np.any(slob_cel): minut = brojac * jed_vremena dan = int(minut/1440) sat = int(minut/60) sace[leglo == vreme_leglo] = 1 leglo[leglo == vreme_leglo] = 0 leglo[leglo>0]+=1 slob_cel = cntr_ram == 0 sledeca_celija = np.max(slob_cel * mapa_kor) cntr_ram[mapa_kor == sledeca_celija] = 1 leglo[trenutni_ram,:,:] = cntr_ram brojac += 1 time.sleep(1) print 'Leglo:' print leglo[trenutni_ram,:,:] print '\n' print 'Sace:' print sace[trenutni_ram,:,:] print '\n'

### #4

## Re: Numpy problem, bugs

Posted 17 November 2016 - 02:13 PM

ok, I see the problem and cannot explain it.

It is, however, very difficult to follow the code because of the non-english variable names.

It is, however, very difficult to follow the code because of the non-english variable names.

### #5

## Re: Numpy problem, bugs

Posted 17 November 2016 - 05:42 PM

The whole code and variables are not important since it print out that there are ones in a slice of 'leglo' in the end, and then go back to the beginning of the loop and in line 63 not add 1 there. If the code is not correct, than it wouldn't print out the slice of leglo.

But here is the explanation if someone is willing to look at it (it's fairly long):

Leglo, sace and pcele represent separate values in the same 3D space. In this case, axes of 3D space are: x = n, y = v and z = h. So there are n (20) slices of h(19) * v(11) surfaces. Some code which determine those values is not included to keep it simpler. So, every 'cell' in 3D space has properties: leglo, pcele and sace.

The following code determine which values of an array will be taken in 'consideration' for adding numbers latter. It means that it will add only in 'cells' where leglo == 0 and pcele < 430.

This determine the coordinates for the center of a v * h slice:

This code determines the number of n slices (br_str) to which broj_pcela = 15000.0 can be evenly distributed so that every cell has a value close to a value gustina_pcela = 3.7. The Kor_gust is a exact number which is close to 3.7 and multiplied by 100 so it can be stored as int16. In this case we will get 356, which acctually means 3.56. Than that value is added to the array "pcele" so that every cell of the first 16 slices has a value of 356. It could be said that the first 16 slices is "inhabited with a density of 356 (3.56) per cell".

This is a center slice of the inhabited section of an array.

This is a map of slices. This will produce an 1D array which will be used in first while loop to choose the slice which will be used in the second while loop. Grd_ram uses 'arange' and add it to map so I would get an unique value for every member of a map. In this manner the center slice of an inhabited array will be choosen first, than the others, based on the distance from the cntr_str.

This is similar to the above, but this is a map of cell distances from the center of one slice. It is manipylated and aranged array is added so that each value will be unique, and more, so that the pattern of latter number adding would be in concentric circles clock - vise. The center value will have the higest value. It is resized in the and so that it could be compared to an other array later.

In the first while loop a slice of a leglo array is chosen. First it will check if there is a slice with at least one cell which can be described as "dobre_cel"(good_cells, described in lines 17-19). It will accept only slices with such values. If there is multiple such slices, it will use a map of slices. Sledeci_ram (next_slice) is an array with boolean values. When multipied with a map of slices (mapa_ramova) it will set unwanted slice addresses to zero. Maximum value will be the address of a acceptable slice that is closest to the center of the inhabited part of space (trenutni_ram or current_slice). Than this 'address' is compared to the array leglo and a slice is choosen. The slice is called cntr_ram

The second loop will work on a center slice as long as there are cells with a value of 0. In a loop, a time is counted, as I wrote 8 min per loop (dan = day, sat = hours). It will add 1 to one cell in a loop which is the closest to a slice center. The center and a array with distances is determined earlier with the use of funcition mgrid and Pythagoras theorem. It works similar to a code which picks a slice in a first while loop. It will multiply slob_cel (free cells) with mapa_kor to discard unwanted values. Than it will add one to the cell which coresponds to the nearest cell to the center with a value 0. Than it will copy the center slice (cntr_ram) back to the coresponding position in array leglo (the coresponding position is "trenutni_ram" or current slice). This is necesary since in the begining of a next loop there are operations which operate on a whole array of leglo so I need to copy this infomation back to leglo.

Then, brojac (counter) goes one up, and the program is put to sleep for 1 second. It prints out a current working slice of leglo and sace. When the loop starts again, cells with a corresponding value will be taken back to zero.

But here is the explanation if someone is willing to look at it (it's fairly long):

Leglo, sace and pcele represent separate values in the same 3D space. In this case, axes of 3D space are: x = n, y = v and z = h. So there are n (20) slices of h(19) * v(11) surfaces. Some code which determine those values is not included to keep it simpler. So, every 'cell' in 3D space has properties: leglo, pcele and sace.

**So 20 slices and each slice has 11 * 19 = 209 cells.**leglo = np.zeros((n, v, h), dtype = 'int16') sace = np.zeros((n, v , h), dtype = 'int16') pcele = np.zeros((n, v, h), dtype = 'int16')

The following code determine which values of an array will be taken in 'consideration' for adding numbers latter. It means that it will add only in 'cells' where leglo == 0 and pcele < 430.

slob_cel_nas = leglo == 0 pokrivene_cel = pcele < 430 dobre_cel = slob_cel_nas | pokrivene_cel

This determine the coordinates for the center of a v * h slice:

cntr_v = v / 2 cntr_h = h / 2

This code determines the number of n slices (br_str) to which broj_pcela = 15000.0 can be evenly distributed so that every cell has a value close to a value gustina_pcela = 3.7. The Kor_gust is a exact number which is close to 3.7 and multiplied by 100 so it can be stored as int16. In this case we will get 356, which acctually means 3.56. Than that value is added to the array "pcele" so that every cell of the first 16 slices has a value of 356. It could be said that the first 16 slices is "inhabited with a density of 356 (3.56) per cell".

br_str = int(broj_pcela / (((v*h)*((1/gustina_pcela)*rezolucija**2)))) kor_gust = int(100*(1.0/((broj_pcela / (br_str * (v*h)))/rezolucija**2))) pcele[0:br_str,:,:] += kor_gust

This is a center slice of the inhabited section of an array.

cntr_str = br_str /2

This is a map of slices. This will produce an 1D array which will be used in first while loop to choose the slice which will be used in the second while loop. Grd_ram uses 'arange' and add it to map so I would get an unique value for every member of a map. In this manner the center slice of an inhabited array will be choosen first, than the others, based on the distance from the cntr_str.

grd_ram = np.arange(n, dtype = 'int16') mapa_ramova = -((grd_ram - cntr_str)**2*10)+10000 ramovi_korekcija = np.arange(n, dtype = 'int16') mapa_ramova = mapa_ramova + ramovi_korekcija

This is similar to the above, but this is a map of cell distances from the center of one slice. It is manipylated and aranged array is added so that each value will be unique, and more, so that the pattern of latter number adding would be in concentric circles clock - vise. The center value will have the higest value. It is resized in the and so that it could be compared to an other array later.

grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 kor = np.arange(0,v*h*0.00001, step = 0.00001, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor mapa_kor = - mapa_kor + 10000 mapa_kor.resize((1,v,h))

**Jed_vremena**is a time unit. 0.5 since if rezolucija is set to 1, than one program tick will represent 0.5 minutes. It is set this way because in reality fastest time to process a cell is 0.5 minutes. So if rezolucija (resolution) is 4, than one cell of 3D space is a square with a surface of 4*4 = 16. And the program tick (set to roughly 1 second) is rezolucija**2 * 0.5 = 8 minutes. The code which links rezolution to dimensions of an array is not copied here. It is above in the program in classes and subclases and I didn't copied it to keep it simpler, but rezolucija affect the size (details) of an array.**Brojac**is a counter of program ticks. So in this case, for every tick it ads 8 to**minut**.**Vreme leglo**sets the time when the value in cell of leglo will be taken back to zero. Acctualy it should be 30240 (21 days), but it 100 here so I could test it more easily (can't wait that long).jed_vremena = (0.5) * rezolucija**2 minut = 0.0 brojac = 0 vreme_leglo = int(100/jed_vremena)

In the first while loop a slice of a leglo array is chosen. First it will check if there is a slice with at least one cell which can be described as "dobre_cel"(good_cells, described in lines 17-19). It will accept only slices with such values. If there is multiple such slices, it will use a map of slices. Sledeci_ram (next_slice) is an array with boolean values. When multipied with a map of slices (mapa_ramova) it will set unwanted slice addresses to zero. Maximum value will be the address of a acceptable slice that is closest to the center of the inhabited part of space (trenutni_ram or current_slice). Than this 'address' is compared to the array leglo and a slice is choosen. The slice is called cntr_ram

**(center_slice). Cntr_ram is a new array.**Than it says that free cells are cells on a cente_slice with a value 0.while np.any(dobre_cel): sledeci_ram = np.any(dobre_cel, axis = (1,2)) sledeci_ram = np.max(sledeci_ram * mapa_ramova) trenutni_ram = mapa_ramova == sledeci_ram cntr_ram = leglo[trenutni_ram,:,:] slob_cel = cntr_ram == 0

The second loop will work on a center slice as long as there are cells with a value of 0. In a loop, a time is counted, as I wrote 8 min per loop (dan = day, sat = hours). It will add 1 to one cell in a loop which is the closest to a slice center. The center and a array with distances is determined earlier with the use of funcition mgrid and Pythagoras theorem. It works similar to a code which picks a slice in a first while loop. It will multiply slob_cel (free cells) with mapa_kor to discard unwanted values. Than it will add one to the cell which coresponds to the nearest cell to the center with a value 0. Than it will copy the center slice (cntr_ram) back to the coresponding position in array leglo (the coresponding position is "trenutni_ram" or current slice). This is necesary since in the begining of a next loop there are operations which operate on a whole array of leglo so I need to copy this infomation back to leglo.

Then, brojac (counter) goes one up, and the program is put to sleep for 1 second. It prints out a current working slice of leglo and sace. When the loop starts again, cells with a corresponding value will be taken back to zero.

**It will add 1 to values which are greater than zero (for a purpose of time tracking). This doesen't happend in the version with the nested while loop, and I dont have the slightest idea why.**Values in sace corresponding to cells which are taken back to zero will be increased by 1. The sace array keeps track of how much times the cell was ocupied.while np.any(slob_cel): minut = brojac * jed_vremena dan = int(minut/1440) sat = int(minut/60) sace[leglo == vreme_leglo] = 1 leglo[leglo == vreme_leglo] = 0 leglo[leglo>0]+=1 slob_cel = cntr_ram == 0 sledeca_celija = np.max(slob_cel * mapa_kor) cntr_ram[mapa_kor == sledeca_celija] = 1 leglo[trenutni_ram,:,:] = cntr_ram brojac += 1 time.sleep(1) print 'Leglo:' print leglo[trenutni_ram,:,:] print '\n' print 'Sace:' print sace[trenutni_ram,:,:] print '\n'

**I know that I can make this simpler by making a 3D map of leglo with unique values, and chosse cells that way in one loop, but i think that such a thing will create some problems. In any case I have definitely encountered some bugs in numpy and if this also is really a bug than it makes Numpy unusable for anything more 'creative' and I will delete python and numpy from my computer and start learning C++ or C right away. I would like someone to prove me wrong.**

### #6

## Re: Numpy problem, bugs

Posted 17 November 2016 - 08:03 PM

One correction which doesen't solve the problem, but it's a mistake by me.

In simpler vesion, line 50 shold be:

sace[leglo == vreme_leglo] += 1

In the longer version, line 61 should be:

sace[leglo == vreme_leglo] += 1

In longer version insert "vreme_izleganja" instead of 100 in line 45.

To get bigger circles increase the value of "vreme_izleganja" in line 9.

In simpler vesion, line 50 shold be:

sace[leglo == vreme_leglo] += 1

In the longer version, line 61 should be:

sace[leglo == vreme_leglo] += 1

In longer version insert "vreme_izleganja" instead of 100 in line 45.

To get bigger circles increase the value of "vreme_izleganja" in line 9.

### #7

## Re: Numpy problem, bugs

Posted 20 November 2016 - 11:22 AM

Conclusion: Numpy is bad and buggy.

### #8

## Re: Numpy problem, bugs

Posted 20 November 2016 - 11:41 AM

Maybe you are right.

I had a similar experience with enumarate, reported here in DIC, http://www.dreaminco...1&#entry2289132

I never got any reaction nor any explanation, but my conclusion is not that enumerate is buggy, rather that I don't know enough about it.

I had a similar experience with enumarate, reported here in DIC, http://www.dreaminco...1&#entry2289132

I never got any reaction nor any explanation, but my conclusion is not that enumerate is buggy, rather that I don't know enough about it.

### #9

## Re: Numpy problem, bugs

Posted 20 November 2016 - 12:43 PM

Ok, I thought the purpose of Numpy and Python is to keep things simple. I don't see the point of using it if a have to know everything that is going on "under the hood". I think that I would have to know and understand the code Numpy functions are written in, but than, the use of Numpy becomes pointless. The thing is ridiculous. That's all I'm saying.

### #10

## Re: Numpy problem, bugs

Posted 20 November 2016 - 12:59 PM

I would like someone to give me an advice on which language and programming concepts should I learn for working on big arrays.

### #11

## Re: Numpy problem, bugs

Posted 21 November 2016 - 02:17 AM

Hm, - APL is old-fasioned and quite complicated; but APL is fantastic at crunching large matrices in many dimensions.

### #13

## Re: Numpy problem, bugs

Posted 20 December 2016 - 12:50 PM

Here it is greatly simplified and explained, with variables in english if someone is willing to look into it. It has very little code, most of it are comments. There are two versions, one with one while loop and the other with two. It's for python 2.7 so printing should be modified I think. I wrote something similar in C and it works but I can't make it work with Numpy.

Version with good output:

The version with two nested while loop which I don't understand:

Version with good output:

# ---------------------------EXPLANATION ------------------------------------------------------------------------------------------------------ # ------ Program adds numbers in 3D array which represent 3D space. # ------ It starts in the center and adds ones every second to where values are zero # ------ Ones are added in the central 2D slice first, until the slice is "full" # ------ Than it goes to the next nearest slice of the center and adds ones in there (where values are zero nearest to the center of the slice) # ------ Every second one is also added to every value in the 3D array which is higher than 0 # ------ In that way it keeps track of the time of when ones are written # ------ The first while loop should pick the nearest slice which has zeros in it # ------ Loop should go on as long as there are 2D slices with at least one zero in it # ------ Second while loop writes ones in the 2D slice which is picked in the first loop as long as there is at least one zero in the 2D slice # ------ Also it adds one to every value in 3D array which is higher than zero # ------ I can't understand why it fails to work the way I want it to when I include the first loop # ------ It skips the line near the end which is " leglo[leglo>0] += 1 ". It says to add one where 3D array leglo is greater than zero # ------ It prints out that there are ones in the end # ------ It works when there are no nested loops import numpy as np import time # ------ array dimensions n = 6 v = 5 h = 5 space_cells = np.zeros((n, v, h), dtype = 'int16') # ------ this says that only cells with the value of zero are good for adding numbers slob_cel_nas = space_cells == 0 # ------ center coordinates cntr_v = v / 2 # vertical cells cntr_h = h / 2 # horizontal cells cntr_str = n /2 # number of v * h slices # ------ Creates a 2D array slice which is a map of physical distances from the slice center # ------ Not real distances, just sufficient precision grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 print 'Map of distaces from the center of 2D slice\n\n' print mapa print '\n' # ----- Adds aranged array and modifies it so that values will raise clockwise in concentric circles when startin from the center #------ Should of arranged slightly different but doesn't matter kor = np.arange(0,v*h*0.01, step = 0.01, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor # ----- Modifies the map so that the highest walue will be the center mapa_kor = - mapa_kor + 10 print """Corrected map of distances from the center of 2D slice\nso that values would rise from the center clockwise\n\n""" print mapa_kor print '\n' cntr_slice = space_cells[cntr_str,:,:] # --------- in this loop ones will be added to the where 3D array "leglo" has values of "0" # --------- this says that "free cells" are only the values on the center slice which have value == 0 free_cells = cntr_slice == 0 # --------- so while there is at least one zero in picked 2D slice, the loop will go on while np.any(free_cells): free_cells = cntr_slice == 0 space_cells[space_cells>0] += 1 # it says to add one in 3D array where value is one or greater # --------- it says that next cell is the maximum value of the product of 2D array "free cells" and the 2D array with corected values of distances # --------- when multipied, the two 2D arrays will give the 2D array which will have zeros where one is added. The highest value will be the nearest cell to the center clockwise. next_cell = np.max(free_cells * mapa_kor) # --------- adds one in the picked cell cntr_slice[mapa_kor == next_cell] = 1 # --------- this copies 2D slice back to 3D array space_cells[cntr_str,:,:] = cntr_slice time.sleep(1) print '3D array Space_cells 2D slice,\ncurrent working slice:\n' print space_cells[cntr_str,:,:] print '\n' print "No more free cells on the slice"

The version with two nested while loop which I don't understand:

# ---------------------------EXPLANATION ------------------------------------------------------------------------------------------------------ # ------ Program adds numbers in 3D array which represent 3D space. # ------ It starts in the center and adds ones every second to where values are zero # ------ Ones are added in the central 2D slice first, until the slice is "full" # ------ Than it goes to the next nearest slice relative to the center slice and adds ones in there (where values are zero nearest to the center of the slice) # ------ Every second one is also added to every value in the 3D array which is higher than 0 # ------ In that way it keeps track of the time of when ones are written # ------ The first while loop should pick the nearest slice which has zeros in it # ------ Loop should go on as long as there are 2D slices with at least one zero in it # ------ Second while loop writes ones in the 2D slice which is picked in the first loop as long as there is at least one zero in the 2D slice # ------ Also it adds one to every value in 3D array which is higher than zero # ------ I can't understand why it fails to work the way I want it to when I include the first loop # ------ It skips the line near the end which is " leglo[leglo>0] += 1 ". It says to add one where 3D array leglo is greater than zero # ------ It prints out that there are ones in the end # ------ It works when there are no nested loops import numpy as np import time # ------ array dimensions n = 6 v = 5 h = 5 space_cells = np.zeros((n, v, h), dtype = 'int16') # ------ this says that only cells with the value of zero are good for adding numbers free_cells_space = space_cells == 0 # ------ center coordinates cntr_v = v / 2 # vertical cells cntr_h = h / 2 # horizontal cells cntr_str = n /2 # number of v * h slices # ------ creates an array with distances of 2D slices from the center slice # ------ not real distances but some numbers good for int16 grd_slc = np.arange(n, dtype = 'int16') slices_map = -((grd_slc - cntr_str)**2*10)+25 cor_slc_map = np.arange(n, dtype = 'int16') slices_map = slices_map + cor_slc_map print "Array with corrected distances from the central slice,\nhighest values will be chossen first." print slices_map print "\n" # ------ Creates a 2D array slice which is a map of physical distances from the slice center # ------ Not real distances, just sufficient precision grd = np.mgrid[0:v, 0:h] mapa = (grd[0] - cntr_v)**2 + (grd[1] - cntr_h)**2 print 'Map of distaces from the center of 2D slice\n\n' print mapa print '\n' # ----- Adds aranged array and modifies it so that values will raise clockwise in concentric circles when startin from the center #------ Should of arranged slightly different but doesn't matter.... kor = np.arange(0,v*h*0.01, step = 0.01, dtype = 'float32') kor.resize(v, h) kor[v/2:,:] = kor[v/2:,::-1] kor[v/2,:h/2] = kor[-1,h/2+1] mapa_kor = mapa + kor # ----- Modifies the map so that the highest walue will be the center mapa_kor = - mapa_kor + 10 print """Corrected map of distances from the center of 2D slice\nso that values would rise from the center clockwise\n\n""" print mapa_kor print '\n' mapa_kor.resize((1,v,h)) # must be resized so that it could be compared to 3D array space_cells while np.any(free_cells_space): # looping while there is at least one empty cell in 3D array next_slice = np.any(free_cells_space, axis = (1,2)) # 1D array with positive values where 2D slices have at least one zero value next_slice = np.max(next_slice * slices_map) # when multiplied, all distances where there is no zeros in 2D slices will become zero, so maximum value will be the value of the slice nearest to the center which has at least one zero in it current_slice = slices_map == next_slice # this is used for indexing in the 3D array to choose the slice from 3D array print "Current slice:\n\n" print current_slice print "\n" # --------- when first while loop not included, numbers are added only in the center 2D slice # --------- cntr_str is the index of the central 2D slice cntr_slice = space_cells[current_slice,:,:] # --------- in this loop ones will be added to the where 3D array "leglo" has values of "0" # --------- this says that "free cells" are only the values on the center slice which have value == 0 free_cells = cntr_slice == 0 # --------- so while there is at least one zero in picke 2D slice, the loop will go on while np.any(free_cells): free_cells = cntr_slice == 0 space_cells[space_cells>0] += 1 # it says to add one in 3D array where value is one or greater # --------- it says that next cell is the maximum value of the product of 2D array "free cells" and the 2D array with corected values of distances # --------- when multipied, the two 2D arrays will give the 2D array which will have zeros where one is added. The highest value will be the nearest cell to the center clockwise. next_cell = np.max(free_cells * mapa_kor) # --------- adds one in the picked cell cntr_slice[mapa_kor == next_cell] = 1 # --------- this copies 2D slice back to 3D array space_cells[current_slice,:,:] = cntr_slice time.sleep(1) print '3D array Space_cells 2D slice,\ncurrent working slice:\n' print space_cells[cntr_str,:,:] print '\n' print "No more free cells on the slice"

### #14

## Re: Numpy problem, bugs

Posted 20 December 2016 - 02:18 PM

I've pinpointed the problem and will ask about it if I don't solve it. Also will post the solution if I solve it. The problem is in the line 81 in version 2.

### #15

## Re: Numpy problem, bugs

Posted 20 December 2016 - 06:27 PM

Well it's caused by advanced indexing somehow. As long as this holds:

numpy will refuse to do arithmetics on space_cells array. It will accept this when I make changes in cntr_slice:

it will also print out this:

But it will not do this:

or even this:

If i use something like this for indexing, or with a variable it's working fine.

I am confused also about how numpy actually works. It seems to me that when I declare

that it makes a copy of space_cells[3,:,:] in memory. But I've been told by someone more experienced than me that it's only a pointer which is really hard for me to accept since the changes in one array are independant comparing to the other array.

cntr_slice = space_cells[current_slice,:,:]

numpy will refuse to do arithmetics on space_cells array. It will accept this when I make changes in cntr_slice:

space_cells[current_slice,:,:] = cntr_slice

it will also print out this:

print space_cells[current_slice,:,:]

But it will not do this:

space_cells[space_cells>0] += 1

or even this:

space_cells += 1

If i use something like this for indexing, or with a variable it's working fine.

cntr_slice = space_cells[3,:,:]

I am confused also about how numpy actually works. It seems to me that when I declare

cntr_slice = space_cells[3,:,:]

that it makes a copy of space_cells[3,:,:] in memory. But I've been told by someone more experienced than me that it's only a pointer which is really hard for me to accept since the changes in one array are independant comparing to the other array.