A variable in the while loop doesnt change

152 views Asked by At

I am doing a Self Organized Map in python like in this tutorial. It partially works however I encountered a wierd problem in one of my while loops. Here is the code for the problem part:

radius = 15    
while radius > 2:
            #print(radius)                    
            while checkW < targetImage.w:
                while checkH < targetImage.h:
                    #print(radius)
                    nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
                    if(nodeDistance <= radius):
                        theta =  math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
                        targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
                        targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
                        targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
                        targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
                        targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
                        targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)                    
                    checkH =  checkH + 1
                checkH = 0
                checkW = checkW + 1
            radius = radius - 1
            #print(radius)

radius is initially set to 15 on each pixel iteration and the idea is to set the r,g,b values according to the radius, reduce it and set new r,g,b values and so on. Note that calculating radius is different than radius = radius - 1 in the algorithm but I wanted to test it with something simple.

My problem is that in the first and the third print(radius) I get the expected values 15,14,13,12... etc. But in the middle one I always get 15 which is the initial value. I do not understand why radius does not change at that point while it changes on the other points. Any help would be appreciated.

1

There are 1 answers

0
Murat Aykanat On BEST ANSWER

Ok it seems that I missed a cruicial part in the loop. checkW and checkW are not reset to zero after each radius iteration so the loop would run only once for radius = 15. After adding checkH = 0 and checkW = 0 at the end of the radius iteration it fixed the problem and my SOM works perfectly.

Here is the code:

 while radius > 2:
        #print(radius)                    
        while checkW < targetImage.w:                
            while checkH < targetImage.h:
                #print(radius)
                nodeDistance = math.sqrt(math.fabs(bmuW - checkW) * math.fabs(bmuW - checkW) + math.fabs(bmuH - checkH) * math.fabs(bmuH - checkH))
                if(nodeDistance <= radius):
                    theta =  math.exp((-1) * ((nodeDistance * nodeDistance) / (2 * radius * radius)))
                    targetImage.canvas[checkW, checkH].w0 = targetImage.canvas[checkW, checkH].w0 + theta * 0.1 * (inputR - targetImage.canvas[checkW, checkH].w0)
                    targetImage.canvas[checkW, checkH].w1 = targetImage.canvas[checkW, checkH].w1 + theta * 0.1 * (inputG - targetImage.canvas[checkW, checkH].w1)
                    targetImage.canvas[checkW, checkH].w2 = targetImage.canvas[checkW, checkH].w2 + theta * 0.1 * (inputB - targetImage.canvas[checkW, checkH].w2)
                    targetImage.canvas[checkW,checkH].r = int(targetImage.canvas[checkW, checkH].w0 * 255)
                    targetImage.canvas[checkW,checkH].g = int(targetImage.canvas[checkW, checkH].w1 * 255)
                    targetImage.canvas[checkW,checkH].b = int(targetImage.canvas[checkW, checkH].w2 * 255)                    
                checkH =  checkH + 1
            checkH = 0
            checkW = checkW + 1
        radius = radius - 1
        checkH = 0
        checkW = 0
        #print(radius)