Creating a coordinate lookup in a window - python

I'm working with PyGame and attempting to create a zoomable/scaleable Mandelbrot Set. I have this set up for square windows and coordinates that only from -1 to 1 on both axes in the complex plane. The way I do this is for every pixel on the screen I call this function:
#Import pygame and initialize
xSize = 50
ySize = 50
scale = 20
size = width, height = (xSize * scale), (ySize * scale)
screen = pygame.display.set_mode(size)
def getCoords(x, y):
complexX = (x/((xSize * scale)/2)) - 1
complexY = (y/((ySize * scale)/2)) - 1
return complexX, complexY
And here is the loop where I actually plot the pixels:
for y in range(0, (ySize * scale)):
for x in range(0, (xSize * scale)):
i = 0
z = getCoords(x, y)
complexNum = complex(z[0], z[1])
zOld = 0
blowsUp = False
#Check to see if (z^2 + c) "blows up"
if blowsUp:
screen.set_at((x, y), color1)
else:
screen.set_at((x, y), color0)
Essentially what I want to be able to do is to have two tuples (one for x and one for y) that contain the maximum and minimum values that get plotted from the complex plane (i.e. here I'm just plotting 1 to -1 on both the real and imaginary axes). I imagine that this would be done by editing the getCoords() function, but after much tinkering with the expression there I can't seem to find a way to do this properly.

I think your question is only marginally related to pygame programming, and is really mostly a math problem.
If I've understood what you're trying to do correctly, essentially it amounts to mapping an integer range of 0..scale to a specified subrange within ±1.0 in both the x and y dimensions. Visualize it as the transformation of the x and y coordinates in one rectangular area or box so they fit within the boundaries of another.
Here's code showing the essence of the math involved.
(Note that the code shown (largely) follows the PEP 8 - Style Guide for Python Code, which I strongly suggest you read and start following.)
scale = 2
size_x, size_y = 15, 15
subrange_x, subrange_y = (-.20, .20), (-.20, .20)
delta_x, delta_y = (subrange_x[1] - subrange_x[0]), (subrange_y[1] - subrange_y[0])
scale_x, scale_y = (size_x * scale), (size_y * scale)
def get_coords(x, y):
real = (x/scale_x * delta_x) + subrange_x[0]
imag = (y/scale_y * delta_y) + subrange_y[0]
return real, imag
for y in range(scale_y):
z_values = []
for x in range(scale_x):
z = get_coords(x, y)
complex_num = complex(z[0], z[1])
z_values.append(f'{complex_num:.2f}')
print(f'y={y:02}:', ' '.join(z_values))
Results printed:
y=00: -0.20-0.20j -0.19-0.20j -0.17-0.20j -0.16-0.20j -0.15-0.20j -0.13-0.20j -0.12-0.20j -0.11-0.20j -0.09-0.20j -0.08-0.20j -0.07-0.20j -0.05-0.20j -0.04-0.20j -0.03-0.20j -0.01-0.20j 0.00-0.20j 0.01-0.20j 0.03-0.20j 0.04-0.20j 0.05-0.20j 0.07-0.20j 0.08-0.20j 0.09-0.20j 0.11-0.20j 0.12-0.20j 0.13-0.20j 0.15-0.20j 0.16-0.20j 0.17-0.20j 0.19-0.20j
y=01: -0.20-0.19j -0.19-0.19j -0.17-0.19j -0.16-0.19j -0.15-0.19j -0.13-0.19j -0.12-0.19j -0.11-0.19j -0.09-0.19j -0.08-0.19j -0.07-0.19j -0.05-0.19j -0.04-0.19j -0.03-0.19j -0.01-0.19j 0.00-0.19j 0.01-0.19j 0.03-0.19j 0.04-0.19j 0.05-0.19j 0.07-0.19j 0.08-0.19j 0.09-0.19j 0.11-0.19j 0.12-0.19j 0.13-0.19j 0.15-0.19j 0.16-0.19j 0.17-0.19j 0.19-0.19j
y=02: -0.20-0.17j -0.19-0.17j -0.17-0.17j -0.16-0.17j -0.15-0.17j -0.13-0.17j -0.12-0.17j -0.11-0.17j -0.09-0.17j -0.08-0.17j -0.07-0.17j -0.05-0.17j -0.04-0.17j -0.03-0.17j -0.01-0.17j 0.00-0.17j 0.01-0.17j 0.03-0.17j 0.04-0.17j 0.05-0.17j 0.07-0.17j 0.08-0.17j 0.09-0.17j 0.11-0.17j 0.12-0.17j 0.13-0.17j 0.15-0.17j 0.16-0.17j 0.17-0.17j 0.19-0.17j
y=03: -0.20-0.16j -0.19-0.16j -0.17-0.16j -0.16-0.16j -0.15-0.16j -0.13-0.16j -0.12-0.16j -0.11-0.16j -0.09-0.16j -0.08-0.16j -0.07-0.16j -0.05-0.16j -0.04-0.16j -0.03-0.16j -0.01-0.16j 0.00-0.16j 0.01-0.16j 0.03-0.16j 0.04-0.16j 0.05-0.16j 0.07-0.16j 0.08-0.16j 0.09-0.16j 0.11-0.16j 0.12-0.16j 0.13-0.16j 0.15-0.16j 0.16-0.16j 0.17-0.16j 0.19-0.16j
y=04: -0.20-0.15j -0.19-0.15j -0.17-0.15j -0.16-0.15j -0.15-0.15j -0.13-0.15j -0.12-0.15j -0.11-0.15j -0.09-0.15j -0.08-0.15j -0.07-0.15j -0.05-0.15j -0.04-0.15j -0.03-0.15j -0.01-0.15j 0.00-0.15j 0.01-0.15j 0.03-0.15j 0.04-0.15j 0.05-0.15j 0.07-0.15j 0.08-0.15j 0.09-0.15j 0.11-0.15j 0.12-0.15j 0.13-0.15j 0.15-0.15j 0.16-0.15j 0.17-0.15j 0.19-0.15j
y=05: -0.20-0.13j -0.19-0.13j -0.17-0.13j -0.16-0.13j -0.15-0.13j -0.13-0.13j -0.12-0.13j -0.11-0.13j -0.09-0.13j -0.08-0.13j -0.07-0.13j -0.05-0.13j -0.04-0.13j -0.03-0.13j -0.01-0.13j 0.00-0.13j 0.01-0.13j 0.03-0.13j 0.04-0.13j 0.05-0.13j 0.07-0.13j 0.08-0.13j 0.09-0.13j 0.11-0.13j 0.12-0.13j 0.13-0.13j 0.15-0.13j 0.16-0.13j 0.17-0.13j 0.19-0.13j
y=06: -0.20-0.12j -0.19-0.12j -0.17-0.12j -0.16-0.12j -0.15-0.12j -0.13-0.12j -0.12-0.12j -0.11-0.12j -0.09-0.12j -0.08-0.12j -0.07-0.12j -0.05-0.12j -0.04-0.12j -0.03-0.12j -0.01-0.12j 0.00-0.12j 0.01-0.12j 0.03-0.12j 0.04-0.12j 0.05-0.12j 0.07-0.12j 0.08-0.12j 0.09-0.12j 0.11-0.12j 0.12-0.12j 0.13-0.12j 0.15-0.12j 0.16-0.12j 0.17-0.12j 0.19-0.12j
y=07: -0.20-0.11j -0.19-0.11j -0.17-0.11j -0.16-0.11j -0.15-0.11j -0.13-0.11j -0.12-0.11j -0.11-0.11j -0.09-0.11j -0.08-0.11j -0.07-0.11j -0.05-0.11j -0.04-0.11j -0.03-0.11j -0.01-0.11j 0.00-0.11j 0.01-0.11j 0.03-0.11j 0.04-0.11j 0.05-0.11j 0.07-0.11j 0.08-0.11j 0.09-0.11j 0.11-0.11j 0.12-0.11j 0.13-0.11j 0.15-0.11j 0.16-0.11j 0.17-0.11j 0.19-0.11j
y=08: -0.20-0.09j -0.19-0.09j -0.17-0.09j -0.16-0.09j -0.15-0.09j -0.13-0.09j -0.12-0.09j -0.11-0.09j -0.09-0.09j -0.08-0.09j -0.07-0.09j -0.05-0.09j -0.04-0.09j -0.03-0.09j -0.01-0.09j 0.00-0.09j 0.01-0.09j 0.03-0.09j 0.04-0.09j 0.05-0.09j 0.07-0.09j 0.08-0.09j 0.09-0.09j 0.11-0.09j 0.12-0.09j 0.13-0.09j 0.15-0.09j 0.16-0.09j 0.17-0.09j 0.19-0.09j
y=09: -0.20-0.08j -0.19-0.08j -0.17-0.08j -0.16-0.08j -0.15-0.08j -0.13-0.08j -0.12-0.08j -0.11-0.08j -0.09-0.08j -0.08-0.08j -0.07-0.08j -0.05-0.08j -0.04-0.08j -0.03-0.08j -0.01-0.08j 0.00-0.08j 0.01-0.08j 0.03-0.08j 0.04-0.08j 0.05-0.08j 0.07-0.08j 0.08-0.08j 0.09-0.08j 0.11-0.08j 0.12-0.08j 0.13-0.08j 0.15-0.08j 0.16-0.08j 0.17-0.08j 0.19-0.08j
y=10: -0.20-0.07j -0.19-0.07j -0.17-0.07j -0.16-0.07j -0.15-0.07j -0.13-0.07j -0.12-0.07j -0.11-0.07j -0.09-0.07j -0.08-0.07j -0.07-0.07j -0.05-0.07j -0.04-0.07j -0.03-0.07j -0.01-0.07j 0.00-0.07j 0.01-0.07j 0.03-0.07j 0.04-0.07j 0.05-0.07j 0.07-0.07j 0.08-0.07j 0.09-0.07j 0.11-0.07j 0.12-0.07j 0.13-0.07j 0.15-0.07j 0.16-0.07j 0.17-0.07j 0.19-0.07j
y=11: -0.20-0.05j -0.19-0.05j -0.17-0.05j -0.16-0.05j -0.15-0.05j -0.13-0.05j -0.12-0.05j -0.11-0.05j -0.09-0.05j -0.08-0.05j -0.07-0.05j -0.05-0.05j -0.04-0.05j -0.03-0.05j -0.01-0.05j 0.00-0.05j 0.01-0.05j 0.03-0.05j 0.04-0.05j 0.05-0.05j 0.07-0.05j 0.08-0.05j 0.09-0.05j 0.11-0.05j 0.12-0.05j 0.13-0.05j 0.15-0.05j 0.16-0.05j 0.17-0.05j 0.19-0.05j
y=12: -0.20-0.04j -0.19-0.04j -0.17-0.04j -0.16-0.04j -0.15-0.04j -0.13-0.04j -0.12-0.04j -0.11-0.04j -0.09-0.04j -0.08-0.04j -0.07-0.04j -0.05-0.04j -0.04-0.04j -0.03-0.04j -0.01-0.04j 0.00-0.04j 0.01-0.04j 0.03-0.04j 0.04-0.04j 0.05-0.04j 0.07-0.04j 0.08-0.04j 0.09-0.04j 0.11-0.04j 0.12-0.04j 0.13-0.04j 0.15-0.04j 0.16-0.04j 0.17-0.04j 0.19-0.04j
y=13: -0.20-0.03j -0.19-0.03j -0.17-0.03j -0.16-0.03j -0.15-0.03j -0.13-0.03j -0.12-0.03j -0.11-0.03j -0.09-0.03j -0.08-0.03j -0.07-0.03j -0.05-0.03j -0.04-0.03j -0.03-0.03j -0.01-0.03j 0.00-0.03j 0.01-0.03j 0.03-0.03j 0.04-0.03j 0.05-0.03j 0.07-0.03j 0.08-0.03j 0.09-0.03j 0.11-0.03j 0.12-0.03j 0.13-0.03j 0.15-0.03j 0.16-0.03j 0.17-0.03j 0.19-0.03j
y=14: -0.20-0.01j -0.19-0.01j -0.17-0.01j -0.16-0.01j -0.15-0.01j -0.13-0.01j -0.12-0.01j -0.11-0.01j -0.09-0.01j -0.08-0.01j -0.07-0.01j -0.05-0.01j -0.04-0.01j -0.03-0.01j -0.01-0.01j 0.00-0.01j 0.01-0.01j 0.03-0.01j 0.04-0.01j 0.05-0.01j 0.07-0.01j 0.08-0.01j 0.09-0.01j 0.11-0.01j 0.12-0.01j 0.13-0.01j 0.15-0.01j 0.16-0.01j 0.17-0.01j 0.19-0.01j
y=15: -0.20+0.00j -0.19+0.00j -0.17+0.00j -0.16+0.00j -0.15+0.00j -0.13+0.00j -0.12+0.00j -0.11+0.00j -0.09+0.00j -0.08+0.00j -0.07+0.00j -0.05+0.00j -0.04+0.00j -0.03+0.00j -0.01+0.00j 0.00+0.00j 0.01+0.00j 0.03+0.00j .04+0.00j 0.05+0.00j 0.07+0.00j 0.08+0.00j 0.09+0.00j 0.11+0.00j 0.12+0.00j 0.13+0.00j 0.15+0.00j 0.16+0.00j 0.17+0.00j 0.19+0.00j
y=16: -0.20+0.01j -0.19+0.01j -0.17+0.01j -0.16+0.01j -0.15+0.01j -0.13+0.01j -0.12+0.01j -0.11+0.01j -0.09+0.01j -0.08+0.01j -0.07+0.01j -0.05+0.01j -0.04+0.01j -0.03+0.01j -0.01+0.01j 0.00+0.01j 0.01+0.01j 0.03+0.01j 0.04+0.01j 0.05+0.01j 0.07+0.01j 0.08+0.01j 0.09+0.01j 0.11+0.01j 0.12+0.01j 0.13+0.01j 0.15+0.01j 0.16+0.01j 0.17+0.01j 0.19+0.01j
y=17: -0.20+0.03j -0.19+0.03j -0.17+0.03j -0.16+0.03j -0.15+0.03j -0.13+0.03j -0.12+0.03j -0.11+0.03j -0.09+0.03j -0.08+0.03j -0.07+0.03j -0.05+0.03j -0.04+0.03j -0.03+0.03j -0.01+0.03j 0.00+0.03j 0.01+0.03j 0.03+0.03j 0.04+0.03j 0.05+0.03j 0.07+0.03j 0.08+0.03j 0.09+0.03j 0.11+0.03j 0.12+0.03j 0.13+0.03j 0.15+0.03j 0.16+0.03j 0.17+0.03j 0.19+0.03j
y=18: -0.20+0.04j -0.19+0.04j -0.17+0.04j -0.16+0.04j -0.15+0.04j -0.13+0.04j -0.12+0.04j -0.11+0.04j -0.09+0.04j -0.08+0.04j -0.07+0.04j -0.05+0.04j -0.04+0.04j -0.03+0.04j -0.01+0.04j 0.00+0.04j 0.01+0.04j 0.03+0.04j 0.04+0.04j 0.05+0.04j 0.07+0.04j 0.08+0.04j 0.09+0.04j 0.11+0.04j 0.12+0.04j 0.13+0.04j 0.15+0.04j 0.16+0.04j 0.17+0.04j 0.19+0.04j
y=19: -0.20+0.05j -0.19+0.05j -0.17+0.05j -0.16+0.05j -0.15+0.05j -0.13+0.05j -0.12+0.05j -0.11+0.05j -0.09+0.05j -0.08+0.05j -0.07+0.05j -0.05+0.05j -0.04+0.05j -0.03+0.05j -0.01+0.05j 0.00+0.05j 0.01+0.05j 0.03+0.05j 0.04+0.05j 0.05+0.05j 0.07+0.05j 0.08+0.05j 0.09+0.05j 0.11+0.05j 0.12+0.05j 0.13+0.05j 0.15+0.05j 0.16+0.05j 0.17+0.05j 0.19+0.05j
y=20: -0.20+0.07j -0.19+0.07j -0.17+0.07j -0.16+0.07j -0.15+0.07j -0.13+0.07j -0.12+0.07j -0.11+0.07j -0.09+0.07j -0.08+0.07j -0.07+0.07j -0.05+0.07j -0.04+0.07j -0.03+0.07j -0.01+0.07j 0.00+0.07j 0.01+0.07j 0.03+0.07j 0.04+0.07j 0.05+0.07j 0.07+0.07j 0.08+0.07j 0.09+0.07j 0.11+0.07j 0.12+0.07j 0.13+0.07j 0.15+0.07j 0.16+0.07j 0.17+0.07j 0.19+0.07j
y=21: -0.20+0.08j -0.19+0.08j -0.17+0.08j -0.16+0.08j -0.15+0.08j -0.13+0.08j -0.12+0.08j -0.11+0.08j -0.09+0.08j -0.08+0.08j -0.07+0.08j -0.05+0.08j -0.04+0.08j -0.03+0.08j -0.01+0.08j 0.00+0.08j 0.01+0.08j 0.03+0.08j 0.04+0.08j 0.05+0.08j 0.07+0.08j 0.08+0.08j 0.09+0.08j 0.11+0.08j 0.12+0.08j 0.13+0.08j 0.15+0.08j 0.16+0.08j 0.17+0.08j 0.19+0.08j
y=22: -0.20+0.09j -0.19+0.09j -0.17+0.09j -0.16+0.09j -0.15+0.09j -0.13+0.09j -0.12+0.09j -0.11+0.09j -0.09+0.09j -0.08+0.09j -0.07+0.09j -0.05+0.09j -0.04+0.09j -0.03+0.09j -0.01+0.09j 0.00+0.09j 0.01+0.09j 0.03+0.09j 0.04+0.09j 0.05+0.09j 0.07+0.09j 0.08+0.09j 0.09+0.09j 0.11+0.09j 0.12+0.09j 0.13+0.09j 0.15+0.09j 0.16+0.09j 0.17+0.09j 0.19+0.09j
y=23: -0.20+0.11j -0.19+0.11j -0.17+0.11j -0.16+0.11j -0.15+0.11j -0.13+0.11j -0.12+0.11j -0.11+0.11j -0.09+0.11j -0.08+0.11j -0.07+0.11j -0.05+0.11j -0.04+0.11j -0.03+0.11j -0.01+0.11j 0.00+0.11j 0.01+0.11j 0.03+0.11j 0.04+0.11j 0.05+0.11j 0.07+0.11j 0.08+0.11j 0.09+0.11j 0.11+0.11j 0.12+0.11j 0.13+0.11j 0.15+0.11j 0.16+0.11j 0.17+0.11j 0.19+0.11j
y=24: -0.20+0.12j -0.19+0.12j -0.17+0.12j -0.16+0.12j -0.15+0.12j -0.13+0.12j -0.12+0.12j -0.11+0.12j -0.09+0.12j -0.08+0.12j -0.07+0.12j -0.05+0.12j -0.04+0.12j -0.03+0.12j -0.01+0.12j 0.00+0.12j 0.01+0.12j 0.03+0.12j 0.04+0.12j 0.05+0.12j 0.07+0.12j 0.08+0.12j 0.09+0.12j 0.11+0.12j 0.12+0.12j 0.13+0.12j 0.15+0.12j 0.16+0.12j 0.17+0.12j 0.19+0.12j
y=25: -0.20+0.13j -0.19+0.13j -0.17+0.13j -0.16+0.13j -0.15+0.13j -0.13+0.13j -0.12+0.13j -0.11+0.13j -0.09+0.13j -0.08+0.13j -0.07+0.13j -0.05+0.13j -0.04+0.13j -0.03+0.13j -0.01+0.13j 0.00+0.13j 0.01+0.13j 0.03+0.13j 0.04+0.13j 0.05+0.13j 0.07+0.13j 0.08+0.13j 0.09+0.13j 0.11+0.13j 0.12+0.13j 0.13+0.13j 0.15+0.13j 0.16+0.13j 0.17+0.13j 0.19+0.13j
y=26: -0.20+0.15j -0.19+0.15j -0.17+0.15j -0.16+0.15j -0.15+0.15j -0.13+0.15j -0.12+0.15j -0.11+0.15j -0.09+0.15j -0.08+0.15j -0.07+0.15j -0.05+0.15j -0.04+0.15j -0.03+0.15j -0.01+0.15j 0.00+0.15j 0.01+0.15j 0.03+0.15j 0.04+0.15j 0.05+0.15j 0.07+0.15j 0.08+0.15j 0.09+0.15j 0.11+0.15j 0.12+0.15j 0.13+0.15j 0.15+0.15j 0.16+0.15j 0.17+0.15j 0.19+0.15j
y=27: -0.20+0.16j -0.19+0.16j -0.17+0.16j -0.16+0.16j -0.15+0.16j -0.13+0.16j -0.12+0.16j -0.11+0.16j -0.09+0.16j -0.08+0.16j -0.07+0.16j -0.05+0.16j -0.04+0.16j -0.03+0.16j -0.01+0.16j 0.00+0.16j 0.01+0.16j 0.03+0.16j 0.04+0.16j 0.05+0.16j 0.07+0.16j 0.08+0.16j 0.09+0.16j 0.11+0.16j 0.12+0.16j 0.13+0.16j 0.15+0.16j 0.16+0.16j 0.17+0.16j 0.19+0.16j
y=28: -0.20+0.17j -0.19+0.17j -0.17+0.17j -0.16+0.17j -0.15+0.17j -0.13+0.17j -0.12+0.17j -0.11+0.17j -0.09+0.17j -0.08+0.17j -0.07+0.17j -0.05+0.17j -0.04+0.17j -0.03+0.17j -0.01+0.17j 0.00+0.17j 0.01+0.17j 0.03+0.17j 0.04+0.17j 0.05+0.17j 0.07+0.17j 0.08+0.17j 0.09+0.17j 0.11+0.17j 0.12+0.17j 0.13+0.17j 0.15+0.17j 0.16+0.17j 0.17+0.17j 0.19+0.17j
y=29: -0.20+0.19j -0.19+0.19j -0.17+0.19j -0.16+0.19j -0.15+0.19j -0.13+0.19j -0.12+0.19j -0.11+0.19j -0.09+0.19j -0.08+0.19j -0.07+0.19j -0.05+0.19j -0.04+0.19j -0.03+0.19j -0.01+0.19j 0.00+0.19j 0.01+0.19j 0.03+0.19j 0.04+0.19j 0.05+0.19j 0.07+0.19j 0.08+0.19j 0.09+0.19j 0.11+0.19j 0.12+0.19j 0.13+0.19j 0.15+0.19j 0.16+0.19j 0.17+0.19j 0.19+0.19j

Related

How to efficiently calculate pairwise ratios on rows using NumPy?

I'm trying to speed up my calculation of calculating pairwise values. However, I'm having trouble visualizing how to do this in NumPy without being overly verbose and explicit.
Is there a way to use vector multiplication or broadcasting to speed up this operation?
Essentially what I'm doing is iterating through each row, then through all combinations of features in each row, and then taking the ratio (followed by log transform).
import pandas as pd
import numpy as np
from collections import *
# Get data
data = {'BC0a': {'1001.2': 4122,'1010.1': 6766,'1010.2': 11734,'1018.2': 5805,'1021.2': 9504,'1026.1': 18111,'1026.2': 1350,'1029.1': 3638,'1029.2': 3967,'1048.2': 9225,'1049.2': 8670,'1057.2': 21796,'1065.1': 5117,'1065.2': 7811,'1067.1': 17563,'1067.2': 8418,'1071.1': 10088,'1071.2': 9878,'1072.1': 13159,'1072.2': 8358,'1073.1': 5368,'1073.2': 2615,'1080.2': 9397,'1086.2': 1303,'2018.1': 2414,'2018.2': 1771,'2028.2': 13710,'2031.2': 3264,'2034.1': 10264,'2034.2': 4670,'2036.1': 7219,'2050.2': 11707,'3038.1': 1388,'3038.2': 13104,'3041.1': 4085,'3041.2': 9160,'3042.1': 8629,'3042.2': 11298,'A-1501-03.A': 4083,'A-1501-03.B': 7952,'A-1505-99.A': 4714,'A-1505-99.B': 4938,'A-1507-168.B': 9238,'M-1506-106.B': 10223,'M-1506-108.A': 2704,'M-1506-108.B': 5056,'M-1506-110.A': 20939,'M-1506-110.B': 18435,'M-1507-113.B': 23343,'M-1507-118.A': 5895,'M-1507-118.B': 5758,'M-1507-119.A': 4404,'M-1507-119.B': 5842,'M-1507-120.A': 9952,'M-1507-120.B': 7825,'M-1507-124.A': 12450,'M-1507-125.B': 28041,'M-1507-126.A': 7669,'M-1507-133.B': 4964,'M-1507-134.B': 1380,'M-1507-136.A': 5557,'M-1507-147.A': 3113,'M-1507-147.B': 1340,'M-1507-154.A': 2848,'M-1507-154.B': 17416,'M-1507-155.A': 5535,'M-1507-155.B': 3792,'S-1409-41.B': 3476,'S-1409-52.B': 3584,'S-1409-57.A': 2065,'S-1410-61.B': 10758,'S-1410-62.A': 5886,'S-1410-62.B': 7680,'S-1410-67.A': 4104,'S-1410-70.A': 2970,'S-1410-70.B': 9990,'S-1410-71.A': 14024,'S-1410-71.B': 11120,'S-1410-72.A': 9835,'S-1410-72.B': 7124,'S-1410-75.A': 1693,'S-1410-75.B': 3334,'S-1410-76.A': 3849,'S-1410-76.B': 12268,'S-1410-84.A': 16988,'S-1410-84.B': 19123,'S-1504-86.A': 15359},'BC0b': {'1001.2': 5935,'1010.1': 34253,'1010.2': 8016,'1018.2': 7557,'1021.2': 25213,'1026.1': 5462,'1026.2': 742,'1029.1': 11265,'1029.2': 2133,'1048.2': 7906,'1049.2': 27375,'1057.2': 7353,'1065.1': 8505,'1065.2': 18643,'1067.1': 22754,'1067.2': 7441,'1071.1': 15505,'1071.2': 18355,'1072.1': 9349,'1072.2': 21841,'1073.1': 8474,'1073.2': 6273,'1080.2': 7820,'1086.2': 2231,'2018.1': 1978,'2018.2': 6516,'2028.2': 12559,'2031.2': 2537,'2034.1': 13301,'2034.2': 3128,'2036.1': 12757,'2050.2': 17553,'3038.1': 1286,'3038.2': 13197,'3041.1': 11141,'3041.2': 19938,'3042.1': 11437,'3042.2': 12662,'A-1501-03.A': 36185,'A-1501-03.B': 38130,'A-1505-99.A': 16054,'A-1505-99.B': 14821,'A-1507-168.B': 26276,'M-1506-106.B': 9390,'M-1506-108.A': 8822,'M-1506-108.B': 10076,'M-1506-110.A': 32208,'M-1506-110.B': 31193,'M-1507-113.B': 72979,'M-1507-118.A': 5367,'M-1507-118.B': 4684,'M-1507-119.A': 66612,'M-1507-119.B': 13348,'M-1507-120.A': 33713,'M-1507-120.B': 8025,'M-1507-124.A': 16145,'M-1507-125.B': 28633,'M-1507-126.A': 18907,'M-1507-133.B': 4885,'M-1507-134.B': 42507,'M-1507-136.A': 17102,'M-1507-147.A': 5015,'M-1507-147.B': 114078,'M-1507-154.A': 16207,'M-1507-154.B': 12175,'M-1507-155.A': 9714,'M-1507-155.B': 14034,'S-1409-41.B': 4865,'S-1409-52.B': 2994,'S-1409-57.A': 3328,'S-1410-61.B': 12545,'S-1410-62.A': 12655,'S-1410-62.B': 3591,'S-1410-67.A': 3109,'S-1410-70.A': 14745,'S-1410-70.B': 16226,'S-1410-71.A': 3979,'S-1410-71.B': 2415,'S-1410-72.A': 25956,'S-1410-72.B': 19851,'S-1410-75.A': 5027,'S-1410-75.B': 1560,'S-1410-76.A': 28781,'S-1410-76.B': 11400,'S-1410-84.A': 15048,'S-1410-84.B': 5149,'S-1504-86.A': 15101},'BC1': {'1001.2': 289306,'1010.1': 161496,'1010.2': 69964,'1018.2': 47739,'1021.2': 124848,'1026.1': 68643,'1026.2': 17652,'1029.1': 35123,'1029.2': 131421,'1048.2': 178320,'1049.2': 97477,'1057.2': 72803,'1065.1': 66952,'1065.2': 116619,'1067.1': 70103,'1067.2': 60019,'1071.1': 355549,'1071.2': 253142,'1072.1': 72188,'1072.2': 181514,'1073.1': 132905,'1073.2': 123315,'1080.2': 105312,'1086.2': 26294,'2018.1': 63484,'2018.2': 341118,'2028.2': 65058,'2031.2': 140441,'2034.1': 84939,'2034.2': 49515,'2036.1': 145002,'2050.2': 103857,'3038.1': 13941,'3038.2': 79499,'3041.1': 42726,'3041.2': 124551,'3042.1': 40899,'3042.2': 40565,'A-1501-03.A': 137056,'A-1501-03.B': 166252,'A-1505-99.A': 202502,'A-1505-99.B': 61795,'A-1507-168.B': 144826,'M-1506-106.B': 92482,'M-1506-108.A': 134468,'M-1506-108.B': 167032,'M-1506-110.A': 101216,'M-1506-110.B': 100130,'M-1507-113.B': 156389,'M-1507-118.A': 178511,'M-1507-118.B': 114049,'M-1507-119.A': 127491,'M-1507-119.B': 96218,'M-1507-120.A': 227099,'M-1507-120.B': 97021,'M-1507-124.A': 63121,'M-1507-125.B': 126164,'M-1507-126.A': 62577,'M-1507-133.B': 33532,'M-1507-134.B': 104437,'M-1507-136.A': 25218,'M-1507-147.A': 180090,'M-1507-147.B': 67312,'M-1507-154.A': 78943,'M-1507-154.B': 73775,'M-1507-155.A': 111586,'M-1507-155.B': 146237,'S-1409-41.B': 134373,'S-1409-52.B': 122611,'S-1409-57.A': 139990,'S-1410-61.B': 326334,'S-1410-62.A': 360458,'S-1410-62.B': 448642,'S-1410-67.A': 250696,'S-1410-70.A': 72425,'S-1410-70.B': 192340,'S-1410-71.A': 113476,'S-1410-71.B': 160124,'S-1410-72.A': 82178,'S-1410-72.B': 118238,'S-1410-75.A': 70348,'S-1410-75.B': 277236,'S-1410-76.A': 130693,'S-1410-76.B': 256835,'S-1410-84.A': 103621,'S-1410-84.B': 56052,'S-1504-86.A': 108613},'BC10': {'1001.2': 33754,'1010.1': 16476,'1010.2': 8935,'1018.2': 1221,'1021.2': 80753,'1026.1': 716,'1026.2': 334,'1029.1': 1008,'1029.2': 1008,'1048.2': 8115,'1049.2': 27643,'1057.2': 6681,'1065.1': 1020,'1065.2': 1269,'1067.1': 5771,'1067.2': 1800,'1071.1': 4905,'1071.2': 6513,'1072.1': 12068,'1072.2': 22154,'1073.1': 889,'1073.2': 380,'1080.2': 5208,'1086.2': 1359,'2018.1': 1577,'2018.2': 5306,'2028.2': 2675,'2031.2': 1284,'2034.1': 2081,'2034.2': 1229,'2036.1': 2548,'2050.2': 4020,'3038.1': 334,'3038.2': 943,'3041.1': 429,'3041.2': 3665,'3042.1': 845,'3042.2': 1700,'A-1501-03.A': 2357,'A-1501-03.B': 2798,'A-1505-99.A': 3528,'A-1505-99.B': 1093,'A-1507-168.B': 13760,'M-1506-106.B': 2380,'M-1506-108.A': 8673,'M-1506-108.B': 7305,'M-1506-110.A': 12250,'M-1506-110.B': 9403,'M-1507-113.B': 56811,'M-1507-118.A': 9763,'M-1507-118.B': 3591,'M-1507-119.A': 15052,'M-1507-119.B': 2156,'M-1507-120.A': 13108,'M-1507-120.B': 3872,'M-1507-124.A': 5413,'M-1507-125.B': 15676,'M-1507-126.A': 5279,'M-1507-133.B': 5271,'M-1507-134.B': 1087,'M-1507-136.A': 3641,'M-1507-147.A': 4673,'M-1507-147.B': 1316,'M-1507-154.A': 6519,'M-1507-154.B': 3229,'M-1507-155.A': 5602,'M-1507-155.B': 10569,'S-1409-41.B': 2057,'S-1409-52.B': 1593,'S-1409-57.A': 13432,'S-1410-61.B': 7979,'S-1410-62.A': 5516,'S-1410-62.B': 1783,'S-1410-67.A': 6542,'S-1410-70.A': 4102,'S-1410-70.B': 6543,'S-1410-71.A': 22790,'S-1410-71.B': 12016,'S-1410-72.A': 11283,'S-1410-72.B': 1319,'S-1410-75.A': 1938,'S-1410-75.B': 2400,'S-1410-76.A': 2461,'S-1410-76.B': 10497,'S-1410-84.A': 5015,'S-1410-84.B': 1202,'S-1504-86.A': 57069},'BC100': {'1001.2': 3286,'1010.1': 10947,'1010.2': 7687,'1018.2': 2765,'1021.2': 38762,'1026.1': 33677,'1026.2': 13154,'1029.1': 5563,'1029.2': 9174,'1048.2': 13501,'1049.2': 23698,'1057.2': 3075,'1065.1': 18148,'1065.2': 24837,'1067.1': 8748,'1067.2': 11369,'1071.1': 4122,'1071.2': 41568,'1072.1': 67820,'1072.2': 42658,'1073.1': 12069,'1073.2': 38510,'1080.2': 25111,'1086.2': 3059,'2018.1': 11287,'2018.2': 46781,'2028.2': 12872,'2031.2': 43654,'2034.1': 12293,'2034.2': 4663,'2036.1': 7529,'2050.2': 26721,'3038.1': 5641,'3038.2': 27469,'3041.1': 18354,'3041.2': 24639,'3042.1': 14481,'3042.2': 19247,'A-1501-03.A': 39180,'A-1501-03.B': 58928,'A-1505-99.A': 16431,'A-1505-99.B': 17782,'A-1507-168.B': 26221,'M-1506-106.B': 2450,'M-1506-108.A': 35528,'M-1506-108.B': 45668,'M-1506-110.A': 47633,'M-1506-110.B': 76327,'M-1507-113.B': 16337,'M-1507-118.A': 2003,'M-1507-118.B': 80529,'M-1507-119.A': 33802,'M-1507-119.B': 59446,'M-1507-120.A': 29498,'M-1507-120.B': 24406,'M-1507-124.A': 2117,'M-1507-125.B': 42188,'M-1507-126.A': 12968,'M-1507-133.B': 15551,'M-1507-134.B': 72534,'M-1507-136.A': 5452,'M-1507-147.A': 311,'M-1507-147.B': 102750,'M-1507-154.A': 18811,'M-1507-154.B': 14461,'M-1507-155.A': 4477,'M-1507-155.B': 6166,'S-1409-41.B': 3421,'S-1409-52.B': 6475,'S-1409-57.A': 140259,'S-1410-61.B': 17793,'S-1410-62.A': 74388,'S-1410-62.B': 21366,'S-1410-67.A': 5405,'S-1410-70.A': 65814,'S-1410-70.B': 155655,'S-1410-71.A': 10657,'S-1410-71.B': 8941,'S-1410-72.A': 6123,'S-1410-72.B': 24783,'S-1410-75.A': 2620,'S-1410-75.B': 337,'S-1410-76.A': 45598,'S-1410-76.B': 5859,'S-1410-84.A': 9742,'S-1410-84.B': 20088,'S-1504-86.A': 5997},'BC101': {'1001.2': 12807,'1010.1': 16089,'1010.2': 28135,'1018.2': 7601,'1021.2': 29398,'1026.1': 8161,'1026.2': 2873,'1029.1': 3002,'1029.2': 23365,'1048.2': 24211,'1049.2': 12970,'1057.2': 18223,'1065.1': 5070,'1065.2': 15738,'1067.1': 11808,'1067.2': 18002,'1071.1': 10263,'1071.2': 20660,'1072.1': 30239,'1072.2': 23493,'1073.1': 9962,'1073.2': 9259,'1080.2': 57914,'1086.2': 10509,'2018.1': 8176,'2018.2': 13699,'2028.2': 23270,'2031.2': 11688,'2034.1': 7456,'2034.2': 4892,'2036.1': 5897,'2050.2': 24212,'3038.1': 822,'3038.2': 8498,'3041.1': 3023,'3041.2': 12399,'3042.1': 3497,'3042.2': 11858,'A-1501-03.A': 2538,'A-1501-03.B': 4517,'A-1505-99.A': 3186,'A-1505-99.B': 6374,'A-1507-168.B': 9800,'M-1506-106.B': 9571,'M-1506-108.A': 7972,'M-1506-108.B': 12036,'M-1506-110.A': 21647,'M-1506-110.B': 18908,'M-1507-113.B': 13387,'M-1507-118.A': 20837,'M-1507-118.B': 35744,'M-1507-119.A': 6812,'M-1507-119.B': 13414,'M-1507-120.A': 24473,'M-1507-120.B': 9791,'M-1507-124.A': 30794,'M-1507-125.B': 29891,'M-1507-126.A': 9887,'M-1507-133.B': 7909,'M-1507-134.B': 3400,'M-1507-136.A': 5674,'M-1507-147.A': 74549,'M-1507-147.B': 2853,'M-1507-154.A': 5432,'M-1507-154.B': 23947,'M-1507-155.A': 5888,'M-1507-155.B': 4172,'S-1409-41.B': 14725,'S-1409-52.B': 19069,'S-1409-57.A': 12003,'S-1410-61.B': 21601,'S-1410-62.A': 18475,'S-1410-62.B': 21780,'S-1410-67.A': 30727,'S-1410-70.A': 3549,'S-1410-70.B': 21779,'S-1410-71.A': 185412,'S-1410-71.B': 78044,'S-1410-72.A': 8771,'S-1410-72.B': 1100,'S-1410-75.A': 6739,'S-1410-75.B': 53418,'S-1410-76.A': 24381,'S-1410-76.B': 68226,'S-1410-84.A': 12141,'S-1410-84.B': 4067,'S-1504-86.A': 12996},'BC102': {'1001.2': 11219,'1010.1': 6212,'1010.2': 5608,'1018.2': 771,'1021.2': 4191,'1026.1': 1567,'1026.2': 378,'1029.1': 584,'1029.2': 534,'1048.2': 3344,'1049.2': 27804,'1057.2': 179,'1065.1': 2359,'1065.2': 6919,'1067.1': 2335,'1067.2': 301,'1071.1': 1975,'1071.2': 4508,'1072.1': 6226,'1072.2': 6786,'1073.1': 5397,'1073.2': 1984,'1080.2': 422,'1086.2': 261,'2018.1': 700,'2018.2': 298,'2028.2': 1575,'2031.2': 4126,'2034.1': 5626,'2034.2': 972,'2036.1': 773,'2050.2': 9878,'3038.1': 462,'3038.2': 1571,'3041.1': 5059,'3041.2': 3557,'3042.1': 35644,'3042.2': 9463,'A-1501-03.A': 997,'A-1501-03.B': 8698,'A-1505-99.A': 1430,'A-1505-99.B': 2856,'A-1507-168.B': 2674,'M-1506-106.B': 380,'M-1506-108.A': 6261,'M-1506-108.B': 4569,'M-1506-110.A': 3987,'M-1506-110.B': 2212,'M-1507-113.B': 391,'M-1507-118.A': 411,'M-1507-118.B': 2462,'M-1507-119.A': 2932,'M-1507-119.B': 5597,'M-1507-120.A': 8961,'M-1507-120.B': 4788,'M-1507-124.A': 2628,'M-1507-125.B': 2169,'M-1507-126.A': 3431,'M-1507-133.B': 4702,'M-1507-134.B': 667,'M-1507-136.A': 5041,'M-1507-147.A': 152,'M-1507-147.B': 819,'M-1507-154.A': 233,'M-1507-154.B': 8963,'M-1507-155.A': 2123,'M-1507-155.B': 1654,'S-1409-41.B': 1625,'S-1409-52.B': 2464,'S-1409-57.A': 3097,'S-1410-61.B': 4198,'S-1410-62.A': 4627,'S-1410-62.B': 652,'S-1410-67.A': 682,'S-1410-70.A': 1507,'S-1410-70.B': 2692,'S-1410-71.A': 8955,'S-1410-71.B': 3057,'S-1410-72.A': 5078,'S-1410-72.B': 4436,'S-1410-75.A': 2217,'S-1410-75.B': 76,'S-1410-76.A': 2578,'S-1410-76.B': 1591,'S-1410-84.A': 430,'S-1410-84.B': 3293,'S-1504-86.A': 1022},'BC103': {'1001.2': 47,'1010.1': 4121,'1010.2': 2117,'1018.2': 2568,'1021.2': 25001,'1026.1': 2212,'1026.2': 1535,'1029.1': 4580,'1029.2': 1152,'1048.2': 8221,'1049.2': 27980,'1057.2': 1380,'1065.1': 11013,'1065.2': 23179,'1067.1': 3541,'1067.2': 2241,'1071.1': 9779,'1071.2': 15474,'1072.1': 8528,'1072.2': 10340,'1073.1': 5147,'1073.2': 14658,'1080.2': 2112,'1086.2': 406,'2018.1': 5170,'2018.2': 30349,'2028.2': 6192,'2031.2': 5190,'2034.1': 12565,'2034.2': 2884,'2036.1': 6942,'2050.2': 17112,'3038.1': 540,'3038.2': 11259,'3041.1': 4713,'3041.2': 14039,'3042.1': 31265,'3042.2': 23319,'A-1501-03.A': 6818,'A-1501-03.B': 14055,'A-1505-99.A': 8326,'A-1505-99.B': 9245,'A-1507-168.B': 6074,'M-1506-106.B': 2501,'M-1506-108.A': 19348,'M-1506-108.B': 40895,'M-1506-110.A': 26179,'M-1506-110.B': 10612,'M-1507-113.B': 7493,'M-1507-118.A': 2033,'M-1507-118.B': 17832,'M-1507-119.A': 69373,'M-1507-119.B': 10119,'M-1507-120.A': 10813,'M-1507-120.B': 1515,'M-1507-124.A': 5464,'M-1507-125.B': 3609,'M-1507-126.A': 1456,'M-1507-133.B': 6704,'M-1507-134.B': 22506,'M-1507-136.A': 2103,'M-1507-147.A': 2314,'M-1507-147.B': 40975,'M-1507-154.A': 10102,'M-1507-154.B': 9181,'M-1507-155.A': 8766,'M-1507-155.B': 3702,'S-1409-41.B': 2009,'S-1409-52.B': 4647,'S-1409-57.A': 12433,'S-1410-61.B': 10995,'S-1410-62.A': 24021,'S-1410-62.B': 15828,'S-1410-67.A': 1468,'S-1410-70.A': 28608,'S-1410-70.B': 24070,'S-1410-71.A': 9840,'S-1410-71.B': 1263,'S-1410-72.A': 10484,'S-1410-72.B': 13375,'S-1410-75.A': 3502,'S-1410-75.B': 207,'S-1410-76.A': 14776,'S-1410-76.B': 659,'S-1410-84.A': 9886,'S-1410-84.B': 8042,'S-1504-86.A': 14472},'BC104': {'1001.2': 0,'1010.1': 35,'1010.2': 313,'1018.2': 35,'1021.2': 9,'1026.1': 288,'1026.2': 1,'1029.1': 0,'1029.2': 0,'1048.2': 12,'1049.2': 415,'1057.2': 37,'1065.1': 162,'1065.2': 241,'1067.1': 47,'1067.2': 22,'1071.1': 6,'1071.2': 0,'1072.1': 1,'1072.2': 7,'1073.1': 1,'1073.2': 0,'1080.2': 0,'1086.2': 0,'2018.1': 10,'2018.2': 129,'2028.2': 127,'2031.2': 57,'2034.1': 12,'2034.2': 0,'2036.1': 64,'2050.2': 64,'3038.1': 0,'3038.2': 449,'3041.1': 0,'3041.2': 20,'3042.1': 3,'3042.2': 7620,'A-1501-03.A': 21,'A-1501-03.B': 49,'A-1505-99.A': 17,'A-1505-99.B': 0,'A-1507-168.B': 0,'M-1506-106.B': 0,'M-1506-108.A': 767,'M-1506-108.B': 407,'M-1506-110.A': 1,'M-1506-110.B': 3,'M-1507-113.B': 2069,'M-1507-118.A': 2,'M-1507-118.B': 110,'M-1507-119.A': 3081,'M-1507-119.B': 1222,'M-1507-120.A': 1340,'M-1507-120.B': 151,'M-1507-124.A': 0,'M-1507-125.B': 1,'M-1507-126.A': 0,'M-1507-133.B': 1012,'M-1507-134.B': 8,'M-1507-136.A': 8,'M-1507-147.A': 385,'M-1507-147.B': 4,'M-1507-154.A': 205,'M-1507-154.B': 24,'M-1507-155.A': 0,'M-1507-155.B': 349,'S-1409-41.B': 3,'S-1409-52.B': 640,'S-1409-57.A': 3,'S-1410-61.B': 3,'S-1410-62.A': 226,'S-1410-62.B': 12,'S-1410-67.A': 33,'S-1410-70.A': 0,'S-1410-70.B': 2965,'S-1410-71.A': 0,'S-1410-71.B': 0,'S-1410-72.A': 4,'S-1410-72.B': 54,'S-1410-75.A': 218,'S-1410-75.B': 170,'S-1410-76.A': 99,'S-1410-76.B': 481,'S-1410-84.A': 0,'S-1410-84.B': 9,'S-1504-86.A': 0},'BC105': {'1001.2': 0,'1010.1': 3813,'1010.2': 5828,'1018.2': 232,'1021.2': 412,'1026.1': 4,'1026.2': 13,'1029.1': 95,'1029.2': 8,'1048.2': 2127,'1049.2': 9956,'1057.2': 18,'1065.1': 469,'1065.2': 2961,'1067.1': 988,'1067.2': 439,'1071.1': 14668,'1071.2': 3951,'1072.1': 415,'1072.2': 182,'1073.1': 1288,'1073.2': 74,'1080.2': 122,'1086.2': 696,'2018.1': 21,'2018.2': 173,'2028.2': 3865,'2031.2': 59,'2034.1': 1097,'2034.2': 255,'2036.1': 1710,'2050.2': 8870,'3038.1': 307,'3038.2': 445,'3041.1': 984,'3041.2': 5100,'3042.1': 1481,'3042.2': 139,'A-1501-03.A': 351,'A-1501-03.B': 1183,'A-1505-99.A': 11933,'A-1505-99.B': 4359,'A-1507-168.B': 8246,'M-1506-106.B': 980,'M-1506-108.A': 699,'M-1506-108.B': 4191,'M-1506-110.A': 3477,'M-1506-110.B': 36780,'M-1507-113.B': 8960,'M-1507-118.A': 543,'M-1507-118.B': 324,'M-1507-119.A': 7886,'M-1507-119.B': 1448,'M-1507-120.A': 15303,'M-1507-120.B': 1669,'M-1507-124.A': 9443,'M-1507-125.B': 3264,'M-1507-126.A': 430,'M-1507-133.B': 176,'M-1507-134.B': 952,'M-1507-136.A': 163,'M-1507-147.A': 310,'M-1507-147.B': 16023,'M-1507-154.A': 1068,'M-1507-154.B': 2703,'M-1507-155.A': 71589,'M-1507-155.B': 351,'S-1409-41.B': 956,'S-1409-52.B': 85,'S-1409-57.A': 168,'S-1410-61.B': 1492,'S-1410-62.A': 1215,'S-1410-62.B': 562,'S-1410-67.A': 218,'S-1410-70.A': 925,'S-1410-70.B': 643,'S-1410-71.A': 87,'S-1410-71.B': 428,'S-1410-72.A': 232,'S-1410-72.B': 973,'S-1410-75.A': 11737,'S-1410-75.B': 21,'S-1410-76.A': 1169,'S-1410-76.B': 1833,'S-1410-84.A': 108,'S-1410-84.B': 6469,'S-1504-86.A': 13818}}
X = pd.DataFrame(data) + 1
# Shape of X where rows are samples and columns are features
n,m = X.shape
index_samples = X.index
index_features = X.columns
X_values = X.values
# A = np.empty((n, m**2 - m))
# A[:] = np.nan
# Not the most efficient way but the best way to show what I'm trying to do
A = defaultdict(dict)
for row_pos, a in enumerate(X_values):
id_sample = index_samples[row_pos]
# col_pos = 0
for i in range(m):
a_i = a[i]
i_feature = index_features[i]
for j in range( m):
if i != j:
a_j = a[j]
j_feature = index_features[j]
# A[row_pos, col_pos] = a_i - a_j
A[id_sample]["{}/{}".format(i_feature, j_feature)] = np.log(a_i/a_j)
# col_pos += 1
df = pd.DataFrame(A).T
df.iloc[:5,:5]
BC0a/BC0b BC0a/BC1 BC0a/BC10 BC0a/BC100 BC0a/BC101
1001.2 -0.364454 -4.250907 -2.102548 0.226606 -1.133489
1010.1 -1.621745 -3.172429 -0.889908 -0.481099 -0.866140
1010.2 0.381012 -1.785419 0.272488 0.422915 -0.874474
1018.2 -0.263715 -2.106878 1.558403 0.741490 -0.269520
1021.2 -0.975581 -2.575287 -2.139590 -1.405648 -1.129143
You can use np.triu_indices to get all possible combinations by indexing thusly:
i_index, j_index = np.triu_indices(m, 1)
Using the fact that logarithms were invented to be able to do log(a / b) = log(a) - log(b), you can now do:
df = pd.DataFrame(data=np.log(X_values[:, i_index]) - np.log(X_values[:, j_index]),
index=X.index.copy(), columns=[f'{a}/{b}' for a, b in zip(X.columns[i_index], X.columns[j_index])])
While it's more elegant in my opinion, you can just as easily keep the original formulation of the ratio:
np.log(X_values[:, i_index] / X_values[:, j_index])
Notice that this is half of the data that your answer contains. The other half is just the inverse ratios, which appear as negatives in the log table. If you absolutely insist on computing those (even though it's quite wasteful), you can adjust i_index and j_index accordingly:
i_index = np.repeat(np.arange(m), m - 1)
j_index = np.delete(np.tile(np.arange(m), m), slice(None, None, m + 1))
Either way, your entire code can be expressed in about three lines.

Normalize data for colormap

I am plotting color for geopandas shape with 2 array data:
Here's my first array.
newI =
array([ -467, -415, -414, -1001, -246, -147, -523, -327, -583,
-541, -290, -415, -453, -505, -791, -812, -672, -558,
-559, -1055, -327, -703, -419, -499, -273, -574, -802,
-450, -743, -221, -1282, -704, -352, -734, -430, -353,
-515, -1121, -664, -586, -171, -881, -402, -1024, -543,
-527, -384, -775, -931, -1380, -1662, -1069, -952, -435,
-1051, -921, -1211, -794, -547, -313, -511, -993, -430,
-262, -255, -675, -793, -1053, -702, -967, -1016, -230,
-405, -869, -689, -935, -190, -1473, -883, -1233, -240,
-607, -339, -1130, -909, -836, -667, -457, -847, -538,
-606, -457, -800, -322, -1339, -691, -627, -689, -365,
-600, -289, -810, -577, -187, -375, -574, -426, -662,
-695, -1003, -40, -1012, -279, -966, -587, -641, -753,
-461, -563, -604, -1013, -625, -506, -416, -1385, -459,
-760, -347, -308, -555, -325, -1588, -566, -533, -843,
-501, -448, -1022, -654, -602, -1201, -814, -754, -361,
-325, -1141, -725, -256, -601, -379, -496, -1099, -1101,
-598, -442, -773, -295, -1292, -558, -1234, -868, -1135,
-251, -1398, -453, -563, -1306, -693, -560, -512, -935,
-1168, -482, -431, -1199, -1249, -1199, -413, -1018, -194,
-375, -932, -1028, -436, -955, -463, -1303, -676, -554,
-601, -875, -661, -791, -443, -89, -879, -606, -577,
-475, -802, -734, -660, -684, -174, -902, -1241, -1320,
-575, -855, -222, -890, -701, -1082, -531, -693, -1008,
-1357, -433, -379, -192, -343, -477, -230, -938, -675,
-798, -259, -398, -778, -484, -817, -453, -564, -536,
-1599, -968, -547, -845, -1592, -256, -1139, -229, -926,
-474, -392, -990, -295, -558, -465, -497, -395, -468,
-310, -507, -1205, -705, -739, -609, -809, -610, -421,
-1057, -2023, -1105, -618, -466, -1291, -616, -620, -571,
-904, -383, -544, -688, -461, -769, -990, -664, -405,
-419, -852, -435, -298, -782, -758, -371, -813, -421,
-594, -259, -284, -215, -452, -430, -936, -994, -981,
-502, -510, -671, -721, -829, -523, -288, -653, -493,
-983, -1205, -722])
and Here's my second array:
array([-2407, -1992, -3400, -4826, -1544, -820, -3120, -1469, -2869,
-3622, -1738, -2122, -2773, -2939, -3558, -3575, -3082, -2494,
-3591, -5022, -1619, -2608, -3371, -3054, -1596, -2538, -3566,
-2035, -3490, -522, -5362, -3055, -1517, -4107, -2039, -2497,
-2302, -5513, -3876, -4303, -831, -4457, -2027, -5083, -2716,
-2284, -1288, -3781, -4707, -6903, -8592, -5763, -4644, -1999,
-4894, -3190, -6263, -3484, -3090, -1899, -2640, -3940, -2919,
-629, -2018, -4228, -4075, -5249, -2794, -4061, -4089, -1500,
-2434, -3867, -3359, -4070, -1472, -7334, -4367, -5422, -1563,
-3092, -1803, -4664, -4096, -3875, -3061, -1181, -4098, -2850,
-4356, -2239, -3102, -1498, -6458, -3495, -2863, -3568, -1752,
-3422, -1768, -3675, -2061, -919, -1452, -2512, -1924, -3668,
-3931, -4348, -284, -6232, -1065, -4261, -2739, -3392, -3962,
-2369, -2508, -3156, -4759, -3012, -3345, -2566, -7910, -2215,
-3581, -1357, -2155, -2643, -1420, -7449, -3023, -2982, -4913,
-2835, -1748, -4679, -2950, -2951, -5515, -4195, -3568, -1746,
-1437, -5429, -3246, -1556, -2635, -1534, -3553, -4451, -5655,
-2616, -2724, -4445, -1642, -6640, -2869, -5211, -5014, -4909,
-1103, -5658, -2096, -2427, -5719, -3152, -2717, -2544, -4226,
-4813, -2319, -2261, -4844, -5383, -5057, -2981, -5448, -1526,
-1749, -3550, -3736, -1893, -5812, -2686, -5923, -3145, -3569,
-2523, -4586, -2931, -4104, -2301, -666, -4402, -3201, -3171,
-2598, -4279, -3765, -3024, -3085, -468, -3732, -5899, -6464,
-3993, -4583, -1126, -4193, -4214, -3902, -2132, -3712, -4879,
-6907, -1524, -1987, -1444, -2086, -3229, -1316, -4331, -3150,
-4449, -1700, -1486, -3650, -2478, -4166, -2618, -3308, -2458,
-7441, -4452, -2438, -4722, -6949, -1712, -4727, -792, -4193,
-1610, -1951, -3965, -1410, -2958, -2167, -2050, -2035, -2152,
-2236, -3235, -5999, -4024, -3111, -3196, -3881, -2647, -2579,
-6387, -9912, -4677, -2983, -1913, -7547, -3166, -2990, -2183,
-3401, -2080, -3056, -2225, -2546, -4421, -3867, -2975, -1552,
-2090, -3871, -1768, -2032, -3564, -3273, -1579, -4338, -1371,
-3600, -1253, -2083, -1439, -2281, -2045, -4406, -4380, -4129,
-2520, -2529, -2108, -3081, -3561, -2601, -843, -3069, -1852,
-5888, -5730, -3386])
The code to plot those array data is as shown below.
area_gpd = gpd.read_file("....shp")
area_gpd['population'] = newI
plt.rcParams.update({'font.size':32})
west,south,east,north = area.unary_union.bounds
fig,ax = plt.subplots(figsize=(40,40))
cmap = LinearSegmentedColormap.from_list('mycmap', [ 'green','white'])
melbourne_gpd.plot(ax=ax, column='population',legend=False,cmap=cmap,zorder=3)
sm = plt.cm.ScalarMappable(cmap=cmap,\
norm=plt.Normalize(vmin=-9912,
vmax=-284))
It keeps normalizing things so the intensity shows now different.
Is there any function to normalize this data?
I want the map to be darker for those with a larger value. Can anyone give me some recommendations?
Thanks so much
I found a nice solution for the question from a guy on stackoverflow:
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
class MidpointNormalize(mpl.colors.Normalize):
def __init__(self, vmin, vmax, midpoint=0, clip=False):
self.midpoint = midpoint
mpl.colors.Normalize.__init__(self, vmin, vmax, clip)
def __call__(self, value, clip=None):
normalized_min = max(0, 1 / 2 * (1 - abs((self.midpoint - self.vmin) / (self.midpoint - self.vmax))))
normalized_max = min(1, 1 / 2 * (1 + abs((self.vmax - self.midpoint) / (self.midpoint - self.vmin))))
normalized_mid = 0.5
x, y = [self.vmin, self.midpoint, self.vmax], [normalized_min, normalized_mid, normalized_max]
return sp.ma.masked_array(sp.interp(value, x, y))
vals = sp.array([[-5., 0], [5, 10]])
vmin = -1225
vmax = 669
norm = MidpointNormalize(vmin=vmin, vmax=vmax, midpoint=0)
It will nicely complete the job for your color map.
Here is the link to the place I found the solution : Colorplot that distinguishes between positive and negative values

Python Matrix/Vector Operations

I have an array with shape = (2, 257) and want to use each column which are vectors of shape = (2,) to create an array with shape = (2, 2) for each column.
Previously I did this by selecting each column by iterating through my input array
import numpy as np
for counter in input
x = np.array([input[0][counter], input[1][counter]])
y = np.conj(x)
y = y.T
E = x[:, None] * y
corr_matr = np.where(self.iterator == 1, E, alpha * self.altes_E[counter] + (1 - alpha) * E)
self.altes_E[counter] = corr_matr
However this is very slow and I would like to vectorize this calculation so in the end I will have an array containing my E variables for each column of my input variable. I tried to do so but I get broadcasting errors I am not able to solve.. So it will be great if someone helped me out!
Self.iterator will be removed and replaced by the first element of the new array containing all E arrays
My goal now is to have an Array which has the shape (257, 2, 2) and contains 257 2x2 corr_matr matrices. The n-th corr_matr depends on the n-1-th
Self.altes_E is this matrix I am looking for but I cant create it with my vectorized approach. Maybe you guys have an idea how I can create it vectorized without a for-loop.
Test data:
input = array([[ 3.94351315e-02+0.00000000e+00j, -1.50913336e-02+6.03795651e-04j,\n 1.99272113e-04-8.07005910e-04j, -4.67793985e-04+8.33903992e-04j,\n -2.64236148e-03+2.77521785e-05j, -1.49792915e-03+7.36359583e-04j,\n 1.50533594e-03-6.15859179e-04j, -6.54810392e-05-5.01831397e-04j,\n -1.01095434e-03-1.70553920e-04j, 5.81738784e-04+7.12800200e-04j,\n -3.11310287e-04-9.01545559e-04j, -5.86002908e-05-9.55615603e-04j,\n 1.44156235e-04+1.09251279e-03j, -4.87454341e-04+8.03194960e-05j,\n 3.78562845e-04+1.29788540e-04j, -4.87558912e-04+6.55677040e-04j,\n -4.87274113e-04-8.31101470e-04j, 8.16597471e-04+3.81774926e-04j,\n 5.89999582e-04-7.40645680e-05j, -7.03418446e-05-4.16067625e-04j,\n -1.02284759e-03+2.56541860e-04j, -7.25162530e-05-2.12897828e-04j,\n 2.86242195e-04+2.15252463e-04j, -6.97098238e-04-5.35675945e-04j,\n 4.49257188e-04+4.96744002e-04j, 2.86015111e-04+9.92285825e-05j,\n -6.63212048e-05-1.97287145e-04j, -4.96012767e-05+1.68083300e-04j,\n -3.68913382e-04-1.76126405e-04j, 3.05618600e-04-2.13305860e-05j,\n -1.22923172e-04-3.58717400e-04j, -3.92479536e-04+1.02063591e-03j,\n 6.45622389e-04-8.53094144e-04j, -3.14203107e-04+1.47936574e-04j,\n 1.54020776e-05-2.45868608e-05j, 2.78312174e-04+2.11224838e-04j,\n -1.70668244e-04+4.57545662e-04j, 1.89143085e-04-1.62612861e-04j,\n -5.05276967e-04-7.33565277e-04j, 3.87931183e-04+6.84968797e-05j,\n -3.88693353e-04+3.29574348e-04j, 1.88775042e-05+3.06450544e-04j,\n -1.02881416e-04-6.28814378e-04j, 1.50437664e-04-4.64790639e-05j,\n 6.80136794e-05+7.07755678e-04j, 4.29081846e-04-6.60769121e-06j,\n -3.89883869e-05+9.94456323e-05j, -2.88405737e-04-3.90610565e-04j,\n 2.89706554e-04-4.61313935e-04j, 8.53534820e-05+3.45993148e-04j,\n -6.48341994e-04+1.61728688e-05j, 7.08075756e-04+4.18876357e-04j,\n -2.41676738e-04-6.57686042e-04j, -4.52960231e-05+4.69549856e-04j,\n -2.98667220e-04+3.69428944e-04j, 3.09898762e-04-5.55573884e-04j,\n 3.16198618e-05-2.18262971e-04j, -2.43962041e-05+6.14800458e-04j,\n 1.22281179e-04-4.27259031e-05j, -2.02764807e-04-3.04080095e-04j,\n -2.60131161e-05-2.72507038e-04j, -3.48411552e-04-1.65106382e-04j,\n 3.60262912e-04+1.15929180e-03j, 6.44188577e-04-6.57529271e-04j,\n -6.64472633e-04+7.34788284e-05j, -2.53962823e-04-8.64577990e-05j,\n 4.12548109e-04-4.30493761e-04j, -3.89652217e-04+1.10427049e-03j,\n 2.19613546e-04-6.47350601e-04j, -3.01855256e-04-1.70512519e-04j,\n 3.52232474e-04-5.97253780e-04j, -2.46726574e-04+8.25901553e-05j,\n 5.01323354e-04+4.97268616e-04j, -3.87112186e-04+2.31773757e-04j,\n -5.51833095e-04+5.28582216e-04j, 1.52037945e-04-9.87780746e-04j,\n 1.80951699e-04-1.64293165e-04j, 9.81853960e-04+8.24736454e-04j,\n -3.08003998e-04+4.10959821e-05j, -7.18984896e-04+9.56216393e-05j,\n 9.23759625e-06-5.16799160e-04j, 3.62720586e-04-4.27438243e-04j,\n 4.54825689e-04+6.60654467e-04j, -8.45689094e-04+3.84855215e-04j,\n -2.67177134e-05-7.42870583e-04j, -1.92531972e-04-3.02859614e-04j,\n 7.90923909e-04+2.13245532e-04j, -7.46436347e-04+4.59160357e-04j,\n 2.42516064e-04+5.79452623e-04j, 2.76445921e-04-1.07193028e-03j,\n -5.72620393e-04+1.96574790e-04j, 1.34058726e-03+2.34585361e-04j,\n -3.57420647e-04-1.70451007e-04j, -2.82518121e-04+2.07459060e-04j,\n -4.28715245e-05-3.11639838e-04j, -2.31994207e-04-3.50393413e-04j,\n 1.79812526e-04+9.46405559e-06j, 6.45826858e-06-7.27244722e-05j,\n -6.54773349e-04+9.59387600e-04j, 3.84335152e-04-2.72657471e-04j,\n -4.87276832e-04-3.96541032e-04j, 6.77017368e-04+4.47778225e-04j,\n 4.45890499e-04+1.29559357e-04j, -6.51094304e-04+1.86624435e-04j,\n 3.98953747e-05-4.33256129e-04j, -2.22111375e-04-2.04651458e-04j,\n 7.55794351e-04-5.85769035e-04j, -3.94789819e-04+1.04401607e-03j,\n -1.58222133e-04+1.11208833e-05j, 9.23486664e-05-7.75775861e-04j,\n -3.43146772e-04+1.17239880e-04j, 2.84564728e-04+6.11844429e-06j,\n 4.70805410e-04+3.47810038e-04j, 1.69658330e-04-6.84370728e-06j,\n -5.22356227e-04+6.50334373e-04j, -1.73679813e-04+3.63334974e-05j,\n 4.98834691e-04-1.01761885e-03j, 5.87157344e-04+1.20334407e-04j,\n -9.03141283e-04+5.43231194e-04j, 2.12604011e-04+4.25936737e-05j,\n 1.20421975e-04+1.99248114e-04j, 1.36291525e-04-1.74378105e-04j,\n -3.22569755e-04-3.90555480e-04j, -8.56585373e-04-1.36390458e-05j,\n 8.21771053e-04+5.11195915e-04j, -1.32706058e-04-6.24514006e-05j,\n -5.38444724e-04-7.24478095e-04j, 1.28496700e-03+2.93993678e-05j,\n -6.10961533e-04+1.10329922e-04j, -3.67753624e-04+2.46454903e-04j,\n -1.97998194e-04+4.12632455e-04j, 4.80604477e-04-2.31270841e-04j,\n 1.92777568e-05-4.07627748e-04j, 2.86666234e-04+7.01966268e-05j,\n 1.15379387e-04-1.33773849e-04j, -3.17265283e-04+5.00725722e-04j,\n 1.36599939e-04-6.75427006e-04j, 6.37731351e-05+7.04635115e-04j,\n -1.96653870e-04+3.25483834e-04j, -5.86921673e-04-1.23419379e-03j,\n 3.14143574e-04+1.00360594e-04j, 3.31560079e-04+1.07206559e-03j,\n 4.92924636e-05-5.68876368e-04j, -8.41042140e-04+3.22939688e-04j,\n 5.49575067e-04-5.15098419e-05j, -3.93180240e-04-4.42074142e-05j,\n 5.60555298e-04-2.85327349e-04j, -2.38831244e-04+1.91581065e-05j,\n -7.45519046e-05+1.27549869e-04j, -5.89318659e-05-4.21902661e-04j,\n 3.75275146e-04+1.82785513e-05j, -2.34356665e-04+4.60409956e-04j,\n -1.92879655e-04+2.25686712e-05j, 2.29916609e-04-3.98052727e-04j,\n 1.14636467e-04+3.40506254e-04j, 3.66346397e-04+4.71324904e-05j,\n -3.50054041e-04-5.13589144e-04j, -2.54987713e-04+1.99572441e-04j,\n 5.45736142e-07-5.96936864e-05j, -1.24422570e-04+9.28100584e-05j,\n -3.07471848e-04-4.81938971e-05j, 8.54350904e-04+8.01221802e-05j,\n -1.36143168e-03+1.05957395e-04j, 4.67515499e-04+1.18207109e-04j,\n 2.22224539e-04-7.87005141e-04j, 2.06836573e-04+7.33415318e-04j,\n 4.11857186e-05-2.78641304e-04j, 8.46935368e-05+2.47412699e-05j,\n 9.63134514e-05-6.25919599e-04j, -1.74852058e-04+6.83190116e-04j,\n 5.17474224e-05-2.10492739e-05j, 5.02637722e-05+8.55532060e-06j,\n -1.48067521e-04+1.27482971e-04j, -3.17709988e-04-3.19972013e-04j,\n 7.93806547e-04+7.24172271e-04j, 3.71753847e-04-2.96357705e-04j,\n -7.16045744e-04-2.45445209e-04j, 1.85488700e-04-7.80975779e-05j,\n -6.33931296e-04-3.79990485e-04j, 4.23058885e-04+5.71413970e-04j,\n -1.96111954e-04-1.64805179e-04j, -2.39387453e-04+4.67926668e-04j,\n 4.48049475e-04-4.39783397e-04j, -4.57884754e-04-8.64764107e-05j,\n 2.36198689e-04-1.81618919e-04j, -3.98041496e-04+5.49284505e-04j,\n 7.71543104e-04+2.33418707e-04j, -5.37125816e-04-1.39968077e-04j,\n 1.21667266e-04+2.94272358e-05j, -4.22621149e-05-4.03145881e-04j,\n 5.15855772e-04+6.12452186e-05j, -1.75597310e-04+3.06332086e-04j,\n 9.80239412e-05-5.22006358e-04j, -3.91105404e-04+7.24365490e-04j,\n 5.41880105e-04-6.62486843e-04j, -1.44683949e-04+2.83522226e-04j,\n -1.91702886e-04+1.31554681e-04j, 5.24558737e-05-1.08982522e-04j,\n 6.74327223e-04+1.10659354e-04j, -5.79971252e-04+1.77968960e-04j,\n -2.51067236e-04-1.07853197e-04j, 3.11939498e-04-1.38577070e-04j,\n -6.04476470e-05+8.52540539e-05j, 1.34265204e-04+8.21556997e-04j,\n -1.17370999e-04-7.91857871e-04j, 2.79181388e-04-7.31803351e-04j,\n -6.90453886e-04+7.46784829e-04j, -1.16164963e-04-4.57167257e-04j,\n -4.69417951e-04-4.41735735e-04j, 1.35998487e-05+1.12007021e-03j,\n 3.29877562e-04+1.48284571e-04j, 5.83769268e-04+2.49004599e-04j,\n -7.52982150e-04-1.68075249e-03j, -3.38846404e-04+9.63951937e-04j,\n 3.55617024e-04-1.27152987e-04j, 8.13345170e-04-4.35530692e-04j,\n 2.21017421e-04+3.81957629e-04j, -2.29169840e-04+4.31749297e-04j,\n -3.49092860e-04-5.49892781e-04j, 1.47060146e-05-4.94382810e-04j,\n 8.50208905e-04+7.47663086e-05j, -1.45781683e-04+3.05060633e-04j,\n -5.68202031e-04+2.25181950e-04j, -6.70225923e-05-4.04705073e-04j,\n 5.49901664e-04+1.00974501e-03j, 1.09107837e-05-4.09919474e-04j,\n -7.08992001e-04-6.93567150e-04j, 2.38546368e-05+3.33138967e-04j,\n 6.59087722e-04+4.15920176e-04j, -9.24723091e-04+5.72433162e-04j,\n 3.16802067e-04-1.97599886e-04j, 9.59921563e-04-7.20312263e-04j,\n -2.04590275e-04-7.58383004e-04j, -9.83521376e-04+1.39303955e-03j,\n 1.00191560e-03-1.06808718e-03j, -1.44975474e-04+2.10013067e-04j,\n 1.57355072e-04-8.06148227e-05j, -9.77899528e-04+1.01124440e-03j,\n 1.40629188e-04+3.07843307e-04j, 4.58437822e-04-1.54270986e-03j,\n -3.58655124e-06+3.05346109e-04j, -8.51095471e-04+9.50186675e-04j,\n 6.49841806e-04-3.69990669e-04j, -2.42669267e-04+1.97887318e-04j,\n 1.08560919e-03-5.57172096e-04j, -1.38075404e-03+3.06128065e-04j,\n -2.99248592e-04-1.96865567e-04j, 4.34882427e-04+6.50054051e-05j,\n 1.09891678e-03+1.84768495e-04j, -7.36401037e-04-1.54611504e-03j,\n 1.95455637e-04+1.59373547e-03j, -1.65580093e-04-1.90926799e-04j,\n -1.13688576e-04+4.12506434e-04j, 1.12544155e-03+4.77414267e-04j,\n -1.31365139e-03-1.41451042e-03j, 5.79908017e-04+1.48045447e-04j,\n -4.82300426e-04+0.00000000e+00j],\n [ 3.94302788e-02+0.00000000e+00j, -1.50769610e-02+6.62688618e-04j,\n 1.63792293e-04-9.09965691e-04j, -4.20457549e-04+7.67888474e-04j,\n -2.62611773e-03+1.79287284e-04j, -1.39992458e-03+8.62646142e-04j,\n 1.43622367e-03-8.15390156e-04j, -1.32725278e-04-5.22439730e-04j,\n -1.02788776e-03-2.84542025e-05j, 7.04202642e-04+5.75840478e-04j,\n -4.85970329e-04-8.42626503e-04j, -2.85902831e-04-9.33336830e-04j,\n 3.85726455e-04+1.01063584e-03j, -4.54619102e-04+1.89852072e-04j,\n 3.96938522e-04+4.87940712e-06j, -2.64396836e-04+7.56253750e-04j,\n -7.20372356e-04-6.38146395e-04j, 8.97949930e-04+7.69071706e-05j,\n 5.23039447e-04-3.00850918e-04j, -2.30101044e-04-3.83685318e-04j,\n -8.45559655e-04+6.25181058e-04j, -1.55582013e-04-1.67400948e-04j,\n 3.48307983e-04+6.57074044e-05j, -8.67179548e-04-1.65800957e-04j,\n 6.26144481e-04+2.31985901e-04j, 2.96950721e-04-5.87318674e-05j,\n -1.59847453e-04-1.45575757e-04j, 4.17422830e-05+1.62311722e-04j,\n -4.06935839e-04+4.60837714e-05j, 2.40112226e-04-1.88702128e-04j,\n -3.12380034e-04-2.25416888e-04j, 2.83110337e-04+1.05608322e-03j,\n 1.69140411e-06-1.07341915e-03j, -1.61321781e-04+3.08879494e-04j,\n -7.12735790e-06-2.89430707e-05j, 3.44852190e-04-2.42375302e-05j,\n 1.77649387e-04+4.46179235e-04j, 3.00653183e-05-2.58839835e-04j,\n -8.75480076e-04-1.78086957e-04j, 3.15614668e-04-2.23862597e-04j,\n -3.28856251e-05+5.09215677e-04j, 2.42580737e-04+1.91265064e-04j,\n -5.38095552e-04-3.36387834e-04j, 5.89000296e-05-1.38800403e-04j,\n 5.93041602e-04+3.91920304e-04j, 2.58312359e-04-3.52047888e-04j,\n 5.41664911e-05+7.84828629e-05j, -4.87040484e-04+2.00465143e-07j,\n -2.24098958e-04-5.00037983e-04j, 3.25842394e-04+1.16315659e-04j,\n -3.33712131e-04+5.56801846e-04j, 7.20763407e-04-3.98413351e-04j,\n -6.98367513e-04-1.21573550e-04j, 3.84986333e-04+2.61687346e-04j,\n 1.91783081e-04+4.26843301e-04j, -3.61446144e-04-5.30072060e-04j,\n -1.90875135e-04-1.22069126e-04j, 5.47142772e-04+2.66855906e-04j,\n 4.08724905e-06-1.39431456e-04j, -3.61842034e-04+7.01928105e-05j,\n -2.68947677e-04-7.08310314e-05j, -2.73181365e-04+2.79827782e-04j,\n 1.21452700e-03+1.36751774e-05j, -4.48800651e-04-8.19776275e-04j,\n -1.19266297e-04+6.53314340e-04j, -1.48156234e-04+2.20506208e-04j,\n -3.25802814e-04-5.00083015e-04j, 9.93835264e-04+6.10726403e-04j,\n -5.92139594e-04-3.50046309e-04j, -2.20551315e-04+2.66541088e-04j,\n -5.38101746e-04-4.33232244e-04j, 4.53140432e-05+2.67048105e-04j,\n 5.45584100e-04-4.36233554e-04j, 1.89055956e-04+4.05146241e-04j,\n 4.91230583e-04+5.80214622e-04j, -9.70303336e-04-2.07957665e-04j,\n -1.54105042e-04-1.78764985e-04j, 8.29454691e-04-9.70980630e-04j,\n 2.93041276e-05+2.99636023e-04j, 1.11761881e-04+7.06579832e-04j,\n -5.13429489e-04+1.03407490e-05j, -4.51280720e-04-3.28230045e-04j,\n 6.09306297e-04-5.08877271e-04j, 4.69052361e-04+7.90693011e-04j,\n -7.29652303e-04+1.16791352e-04j, -2.63583592e-04+2.40084959e-04j,\n 7.80339281e-05-8.09166755e-04j, 5.91733367e-04+6.46003586e-04j,\n 5.18742468e-04-3.69670376e-04j, -1.10819540e-03-2.98322967e-05j,\n 3.35961109e-04+5.08219023e-04j, -1.37905601e-04-1.35428372e-03j,\n -7.54841905e-05+3.93744885e-04j, 2.75316936e-04+2.05765280e-04j,\n -2.85925096e-04+1.42533512e-04j, -2.49244553e-04+3.44507517e-04j,\n -5.49966896e-05-1.61934566e-04j, -6.94024634e-05+3.21277311e-05j,\n 1.14427279e-03+2.14503629e-04j, -4.06256577e-04-2.42573698e-04j,\n -1.37008788e-04+6.09181906e-04j, 9.41131852e-05-8.08853747e-04j,\n -1.05763981e-04-4.56058417e-04j, 4.74122378e-04+4.69608012e-04j,\n -3.95122830e-04+1.79715038e-04j, -5.41657307e-05+2.94641051e-04j,\n -8.99698699e-04-3.08409334e-04j, 1.08313369e-03-2.52154929e-04j,\n 9.61555026e-05+1.16751009e-04j, -6.78396810e-04+3.85853518e-04j,\n 3.08973305e-04+2.04145634e-04j, -1.66564595e-04-2.23731139e-04j,\n -3.64032556e-05-5.80136045e-04j, -1.25077116e-04-1.17649763e-04j,\n 8.22473217e-04-5.10403627e-05j, 1.35846968e-04+9.04349589e-05j,\n -1.08069909e-03+3.56424784e-04j, -3.42539982e-04-4.83310914e-04j,\n 1.02127798e-03+2.27836680e-04j, -1.38094505e-04-1.76668123e-04j,\n 2.64921676e-05-2.31094625e-04j, -2.30827528e-04+4.76342119e-05j,\n 1.46863589e-07+5.09299510e-04j, 6.74095347e-04+5.26744232e-04j,\n -3.63377125e-04-9.05099052e-04j, 6.26177364e-05+1.23244300e-04j,\n 4.31863072e-05+8.96514097e-04j, -1.06310512e-03-7.14346108e-04j,\n 5.75517243e-04+2.35967322e-04j, 4.41244980e-04-2.65860383e-05j,\n 3.70694639e-04-2.71684200e-04j, -5.39649217e-04-2.89578543e-05j,\n -2.05237670e-04+3.55566693e-04j, -2.29304581e-04-1.82601791e-04j,\n -1.63278707e-04+8.28089685e-05j, 4.85237610e-04-3.27131430e-04j,\n -3.85198889e-04+5.78130634e-04j, 1.91039368e-04-6.73331290e-04j,\n 2.83290401e-04-2.36600115e-04j, 1.56155251e-04+1.35530242e-03j,\n -2.58546407e-04-1.92172329e-04j, -1.64289988e-05-1.12061403e-03j,\n -2.06761243e-04+5.39404115e-04j, 8.90146217e-04-1.10273389e-04j,\n -5.53419489e-04-8.01172163e-05j, 3.69800922e-04+1.19385537e-04j,\n -6.06753749e-04+1.74073233e-04j, 2.37762330e-04+2.21509768e-05j,\n 9.04367392e-05-1.17178154e-04j, 6.65735029e-06+4.23161474e-04j,\n -3.66355661e-04-5.69644277e-05j, 2.72752455e-04-4.36970254e-04j,\n 1.89332418e-04-1.28477791e-05j, -2.48450704e-04+3.85021186e-04j,\n -1.06569421e-04-3.41965661e-04j, -3.70428389e-04-4.26724302e-05j,\n 3.58595638e-04+5.14301117e-04j, 2.51269109e-04-2.08539926e-04j,\n 4.88939967e-06+5.71210855e-05j, 1.18578623e-04-1.05802288e-04j,\n 3.11344492e-04+1.01891390e-05j, -8.56978432e-04+1.74893270e-05j,\n 1.33232876e-03-2.98487141e-04j, -4.85805120e-04-5.94291833e-05j,\n -7.76562206e-05+8.03680105e-04j, -3.43743448e-04-6.84465578e-04j,\n 1.92205105e-05+2.78271830e-04j, -8.62407096e-05-6.06097609e-06j,\n 7.51468262e-05+6.27872523e-04j, -1.25139166e-05-7.07494831e-04j,\n -4.26531031e-05+3.33935842e-05j, -4.93983638e-05+6.62065104e-06j,\n 9.65211897e-05-1.71035129e-04j, 4.09508943e-04+1.81017834e-04j,\n -1.00836515e-03-3.78649449e-04j, -2.28902765e-04+4.29769144e-04j,\n 7.55644169e-04-6.15984149e-05j, -1.31806876e-04+1.50093610e-04j,\n 7.41534101e-04+5.25070731e-05j, -6.37489984e-04-3.17623167e-04j,\n 2.50510928e-04+4.72741634e-05j, -3.12101871e-05-5.30198666e-04j,\n -1.61833058e-04+6.04663509e-04j, 4.33069517e-04-1.76875910e-04j,\n -9.68597935e-05+2.72233313e-04j, 1.29069366e-05-6.86698274e-04j,\n -7.70844850e-04+2.59679250e-04j, 5.06334114e-04-2.08281614e-04j,\n -1.23040807e-04+4.96912636e-05j, 2.81721303e-04+2.82239777e-04j,\n -4.32856798e-04+2.85045100e-04j, -7.18904719e-05-3.42817657e-04j,\n 2.82159126e-04+4.51272457e-04j, -2.22012038e-04-7.93265681e-04j,\n 8.25420573e-05+8.53400594e-04j, -1.00706437e-04-2.98939601e-04j,\n 3.14683041e-05-2.27519261e-04j, 4.36941077e-05+1.13381143e-04j,\n -5.18682202e-04+4.48501226e-04j, 2.30610517e-04-5.52246756e-04j,\n 2.34490912e-04-1.26264973e-04j, -7.97095177e-05+3.35952614e-04j,\n -3.45318332e-05-9.44128409e-05j, -7.57633937e-04-3.43506355e-04j,\n 7.20500401e-04+3.51295162e-04j, 4.74556384e-04+6.27668922e-04j,\n -2.81101536e-04-9.78262731e-04j, 4.46874372e-04+1.24837907e-04j,\n 6.05163789e-04-2.12023275e-04j, -1.01407397e-03-5.16929477e-04j,\n -2.98164906e-04+2.33126469e-04j, -4.86355494e-04+4.33123201e-04j,\n 1.83462356e-03-7.71867376e-07j, -7.69209261e-04-7.01104884e-04j,\n -3.18641790e-05+3.73050143e-04j, 1.15613125e-04+9.09600948e-04j,\n -4.28446346e-04+7.86494004e-05j, -3.34852105e-04-3.47817544e-04j,\n 6.26099520e-04-1.68044497e-04j, 4.68369045e-04+1.42514209e-04j,\n -2.83908873e-04+7.92399750e-04j, -2.49184244e-04-2.18710226e-04j,\n -9.19001071e-05-6.04839544e-04j, 4.08264053e-04+9.93217421e-06j,\n -1.08943196e-03+3.64046481e-04j, 4.09723216e-04+8.49350013e-05j,\n 7.86130478e-04-6.09249721e-04j, -3.38645074e-04-2.27244838e-05j,\n -4.77324227e-04+6.16456017e-04j, -5.01373021e-04-9.55310274e-04j,\n 1.70573740e-04+3.42094097e-04j, 6.89184712e-04+9.91871858e-04j,\n 7.74817539e-04-2.00708515e-04j, -1.39524815e-03-9.81590142e-04j,\n 1.09255944e-03+9.77000557e-04j, -2.06580095e-04-1.44088734e-04j,\n 1.00169291e-04+1.44579525e-04j, -1.08783213e-03-8.86595885e-04j,\n -2.96315620e-04+1.88823864e-04j, 1.58771473e-03+2.66780661e-04j,\n -2.99713732e-04+3.14700680e-05j, -1.08078974e-03-6.78289646e-04j,\n 4.79116024e-04+5.81652824e-04j, -2.43603471e-04-1.88258714e-04j,\n 7.93634703e-04+9.36679925e-04j, -6.29229943e-04-1.26267628e-03j,\n 9.93210429e-05-3.33132766e-04j, 5.00382390e-05+4.41575210e-04j,\n 1.61111886e-04+1.10747772e-03j, 1.23195666e-03-1.20419700e-03j,\n -1.43246313e-03+7.18458426e-04j, 1.23366572e-04-2.18045537e-04j,\n -4.21981246e-04+5.80438962e-05j, 2.37621151e-05+1.23103317e-03j,\n 7.49466857e-04-1.79312642e-03j, 1.24531095e-04+4.69038221e-04j,\n -8.82536309e-04+0.00000000e+00j]])
self.altes_E = np.zeros((257, 2, 2), dtype = complex)
alpha = 0.8

findessentialmat gives different results for the same set of feature points

I am extracting matching feature points from a camera pair. I have a bit more than 200 feature points, by eye ~90% of them are really matching ...
I try to reconstruct the relative position and orientation of the two cameras, using the opencv's findessentialmat function.
A minimal running example is here:
minimal.py
import numpy as np
import cv2
DIM=(1280, 960)
K0 =np.array([[741.33782999, 0., 682.12142279],
[ 0., 742.80374714, 464.20413311],
[ 0., 0., 1. ]])
def readfile(filename):
q = []
p = []
with open(filename) as fp:
for cnt, line in enumerate(fp):
px, py, qx, qy = line.split()
p = np.append(p, [float(px), float(py)])
q = np.append(q, [float(qx), float(qy)])
n = int(len(p)/2)
p = np.reshape(p, (n, 2))
q = np.reshape(q, (n, 2))
return p, q
p12, q12 = readfile("d_temp")
retval, mask = cv2.findEssentialMat(p12, q12, K0, method=cv2.RANSAC)
NN, R, t, _ = cv2.recoverPose(retval, p12, q12, K0, mask=mask)
dst, jacobian = cv2.Rodrigues(R)
print("rotation vector")
print(np.degrees(dst))
print("translation vector")
print(t)
sample data file is here ...
(sorry for the long file, but in order of reproducibility, I think it is a good idea to provide this ...)
# d.dat
447.7982482910156 783.8470458984375 844.46875 478.64984130859375
472.2575988769531 783.9226684570312 861.17919921875 463.9976806640625
471.056884765625 785.6572265625 861.0101928710938 463.9947814941406
679.7252197265625 613.1166381835938 835.0584716796875 162.0465087890625
444.4239196777344 773.3471069335938 834.8787231445312 474.52978515625
476.6434631347656 548.7996215820312 695.3291625976562 295.1576232910156
316.2449035644531 876.6978149414062 767.7656860351562 608.9740600585938
28.56636619567871 835.704833984375 449.8716125488281 713.1991577148438
482.4017028808594 755.1663208007812 850.0111083984375 430.626220703125
471.2223205566406 795.663818359375 868.9219360351562 471.4686279296875
585.5891723632812 928.5010986328125 1024.892333984375 481.9182434082031
134.27027893066406 438.8541564941406 1023.7425537109375 448.80987548828125
560.4802856445312 782.7987060546875 934.5108642578125 399.1592102050781
437.55364990234375 783.9776611328125 835.0953979492188 485.783203125
558.8355102539062 783.1337280273438 934.3226928710938 401.5900573730469
493.6907958984375 811.2030639648438 899.3700561523438 465.0192565917969
333.5005187988281 474.5987548828125 514.41748046875 354.7676696777344
281.6152648925781 854.6431274414062 725.10009765625 614.118896484375
489.6645812988281 819.7667846679688 901.4575805664062 474.939697265625
567.0088500976562 793.2717895507812 946.3422241210938 409.54779052734375
473.9570007324219 741.4544677734375 835.43359375 427.68084716796875
263.5504455566406 593.8671875 308.0599670410156 864.5149536132812
329.52447509765625 759.8671875 982.2100219726562 759.9042358398438
223.50721740722656 877.8092041015625 697.9076538085938 710.441162109375
448.5132751464844 783.9352416992188 845.5458984375 478.6185302734375
166.29356384277344 703.2822875976562 575.96337890625 587.17431640625
195.16452026367188 645.3009033203125 548.0439453125 573.9312133789062
679.7011108398438 613.3897094726562 835.2294311523438 162.2067413330078
560.1014404296875 783.4053344726562 934.7252807617188 400.22491455078125
260.0428466796875 682.58203125 988.1619262695312 737.9358520507812
173.69033813476562 726.2459716796875 244.25367736816406 609.8120727539062
193.5665283203125 495.71881103515625 493.33575439453125 457.4891052246094
318.1491394042969 455.9480895996094 518.5922241210938 354.5550537109375
245.27215576171875 839.358642578125 686.4036865234375 623.212646484375
743.0471801757812 889.39306640625 1060.811767578125 363.8262023925781
99.52837371826172 455.26544189453125 503.113525390625 550.7799682617188
276.1446838378906 699.6495971679688 751.2548217773438 852.3948974609375
190.50099182128906 745.3849487304688 614.2245483398438 600.6458740234375
189.45762634277344 399.43841552734375 471.45098876953125 468.6031799316406
324.149169921875 851.31103515625 753.8226928710938 590.1079711914062
477.1978759765625 545.6710205078125 691.1409301757812 295.8692626953125
399.811767578125 455.1125793457031 349.0351257324219 853.0255126953125
150.71499633789062 637.7068481445312 127.8241958618164 617.8019409179688
174.24513244628906 443.05633544921875 460.3077697753906 445.8348388671875
290.4751892089844 563.4282836914062 1177.7535400390625 647.7413330078125
35.504730224609375 375.5752868652344 1132.147216796875 732.7445678710938
570.9328002929688 891.2274780273438 430.9983825683594 598.0726928710938
129.2855224609375 447.6126403808594 269.48297119140625 518.7491455078125
916.583984375 654.3966674804688 131.51922607421875 544.0574340820312
453.64923095703125 909.1268920898438 310.0630798339844 873.9638061523438
272.41595458984375 779.4100952148438 242.86692810058594 574.124267578125
145.89622497558594 426.38616943359375 839.9185791015625 446.7758483886719
353.2640075683594 649.1328125 568.7075805664062 853.6813354492188
336.7197570800781 477.26580810546875 522.921630859375 354.66552734375
465.9938659667969 766.7280883789062 1245.819091796875 328.0860595703125
603.0092163085938 918.9746704101562 1035.0767822265625 462.84014892578125
177.19642639160156 786.0979614257812 1025.5509033203125 709.3068237304688
743.3534545898438 889.3507080078125 1060.8709716796875 362.4633483886719
424.6748962402344 745.2859497070312 805.2852172851562 469.1532897949219
666.6295776367188 846.3238525390625 1186.088134765625 331.71063232421875
265.49560546875 617.9306640625 1212.5106201171875 742.3143920898438
235.90284729003906 899.5637817382812 237.75428771972656 630.4393920898438
309.32379150390625 528.5013427734375 557.26025390625 405.9731140136719
365.8267822265625 794.9527587890625 1054.2913818359375 461.01287841796875
479.682373046875 811.927978515625 887.924560546875 476.56243896484375
546.8660888671875 825.3713989257812 954.0406494140625 419.4182434082031
263.32550048828125 719.295166015625 938.8235473632812 842.3818969726562
410.2393798828125 752.4403076171875 802.4618530273438 483.38238525390625
322.66204833984375 894.9562377929688 781.970458984375 615.1595458984375
84.81143188476562 472.9810485839844 1218.040771484375 608.5279541015625
198.11459350585938 504.37188720703125 500.67315673828125 641.6554565429688
148.35653686523438 638.7088623046875 186.9049835205078 736.7140502929688
303.63336181640625 844.1828002929688 730.8097534179688 596.8998413085938
187.0508575439453 509.2354431152344 490.9916076660156 470.39593505859375
973.512939453125 734.4038696289062 730.7944946289062 682.7640991210938
317.968994140625 850.9186401367188 748.3575439453125 593.0419311523438
547.7904663085938 864.0103149414062 959.0444946289062 599.9322509765625
264.11163330078125 719.9434814453125 939.9754638671875 844.283203125
667.170166015625 850.118408203125 1074.2694091796875 365.3814392089844
480.75238037109375 556.4450073242188 699.0028686523438 298.0946960449219
264.4339294433594 696.676513671875 619.2904052734375 537.121826171875
239.04403686523438 301.0240478515625 189.99940490722656 249.11346435546875
398.0354309082031 789.0571899414062 994.8922729492188 768.0885009765625
311.0710754394531 763.4730834960938 568.8160400390625 637.556884765625
87.1875 889.8743286132812 775.7293090820312 556.4700927734375
190.0430145263672 742.0386352539062 602.095703125 597.9042358398438
227.24232482910156 776.474609375 115.55091094970703 617.3444213867188
265.21966552734375 707.8191528320312 639.8690795898438 541.1021118164062
162.44972229003906 723.4267578125 245.604736328125 615.490478515625
437.36431884765625 785.8866577148438 836.6463623046875 487.3155517578125
793.1463623046875 923.9131469726562 1126.0709228515625 637.1099243164062
206.6471405029297 476.3389892578125 264.1603698730469 445.1524353027344
903.2003173828125 625.0552978515625 897.1513061523438 67.21099853515625
256.93646240234375 801.38671875 672.7518310546875 598.4820556640625
88.26525115966797 470.6452331542969 666.8927612304688 731.1099853515625
282.2829284667969 762.2633056640625 693.4093017578125 528.4248046875
121.02236938476562 770.4916381835938 698.5695190429688 358.8071594238281
486.1945495605469 880.182861328125 650.6167602539062 584.8759765625
442.5867614746094 771.2802124023438 831.2958374023438 474.41204833984375
352.16180419921875 797.5498657226562 578.2386474609375 631.1714477539062
183.8833465576172 510.9300231933594 280.1349182128906 506.5831604003906
380.51165771484375 810.257080078125 1074.3548583984375 473.5334777832031
231.75296020507812 884.1380615234375 837.761474609375 427.0298767089844
80.79380798339844 894.5822143554688 1132.9962158203125 519.955078125
211.58810424804688 484.9515075683594 500.9017639160156 450.52032470703125
264.9154968261719 618.31689453125 130.1840362548828 479.39434814453125
118.52188110351562 681.4915771484375 1222.2967529296875 677.895751953125
481.3524475097656 817.30810546875 900.7264404296875 140.29383850097656
243.51730346679688 306.3136291503906 493.9825439453125 470.5849609375
474.4856872558594 373.6757507324219 576.774658203125 452.58154296875
419.7844543457031 747.1199951171875 555.3731689453125 639.4868774414062
245.72634887695312 793.8189697265625 583.1657104492188 758.9775390625
182.6488494873047 582.034423828125 515.431884765625 759.8704223632812
162.671875 815.442138671875 733.5533447265625 537.5822143554688
450.0556335449219 843.4663696289062 885.4121704101562 515.9638061523438
148.79116821289062 442.63946533203125 981.3590087890625 775.7106323242188
126.88723754882812 545.5296630859375 1071.9697265625 753.0889282226562
199.48191833496094 498.1930236816406 489.1456298828125 450.1411437988281
275.2535705566406 578.6431884765625 579.5006103515625 462.88641357421875
481.34197998046875 871.9025268554688 1218.254150390625 642.8507080078125
76.59978485107422 468.64898681640625 245.7015838623047 645.285400390625
459.75018310546875 854.2384033203125 603.11865234375 587.4470825195312
471.3988037109375 914.074462890625 164.49220275878906 722.4299926757812
214.27532958984375 494.5464172363281 950.4194946289062 667.5511474609375
198.86412048339844 502.3232116699219 914.5925903320312 797.0887451171875
126.89524841308594 660.0762939453125 1235.8087158203125 671.393310546875
431.3277893066406 752.6722412109375 815.523681640625 468.5042724609375
319.27752685546875 758.914794921875 622.181884765625 631.2896728515625
272.63970947265625 778.7651977539062 530.503662109375 608.778564453125
324.4476013183594 658.4978637695312 881.9677124023438 485.70660400390625
257.4388732910156 852.4768676757812 703.123046875 623.9972534179688
212.24095153808594 485.63641357421875 953.366943359375 512.15478515625
232.307861328125 600.2705078125 1039.3995361328125 467.665283203125
255.8918914794922 752.2881469726562 255.54869079589844 561.2832641601562
282.0978088378906 789.4028930664062 371.9212951660156 511.0465087890625
578.9715576171875 206.7810516357422 612.0197143554688 628.6561889648438
226.20420837402344 776.3167114257812 119.6030044555664 427.47601318359375
342.3455505371094 885.6651000976562 384.51922607421875 433.4626770019531
397.6732482910156 863.8599243164062 172.97869873046875 619.8243408203125
360.3802185058594 652.3429565429688 456.40509033203125 599.084716796875
650.9277954101562 856.9739379882812 1062.89306640625 382.1123352050781
661.31396484375 865.3259887695312 946.0584716796875 516.408447265625
224.0924835205078 734.0776977539062 139.72430419921875 593.4146118164062
650.9525146484375 868.1492919921875 1142.61474609375 520.051513671875
673.8118896484375 306.71563720703125 141.61410522460938 578.706298828125
333.21051025390625 853.7525634765625 1175.6201171875 748.06884765625
679.0411376953125 341.63543701171875 227.8588104248047 713.6207885742188
426.1820068359375 745.2628173828125 257.1972351074219 396.6258544921875
662.1176147460938 876.2039794921875 1041.9356689453125 518.449462890625
301.7644348144531 518.9301147460938 564.2955932617188 411.1322326660156
237.16954040527344 689.663818359375 802.1461181640625 716.6635131835938
626.2874755859375 825.3125610351562 604.45947265625 545.74755859375
134.82618713378906 685.4896850585938 1221.602783203125 682.282958984375
315.6167297363281 736.1107177734375 143.47314453125 591.7796020507812
223.23167419433594 776.7254638671875 646.8034057617188 599.654052734375
96.94023895263672 334.7595520019531 1205.7244873046875 444.9001159667969
1074.5599365234375 744.3084106445312 442.6503601074219 719.4102172851562
751.9534301757812 897.6613159179688 605.0469360351562 553.6414184570312
344.93878173828125 656.6336669921875 275.20733642578125 510.4526062011719
493.5873718261719 904.2405395507812 662.9373168945312 674.652099609375
247.3878936767578 783.7380981445312 654.9146118164062 592.4580688476562
237.5862274169922 804.0629272460938 662.11279296875 608.0201416015625
490.70050048828125 820.9711303710938 902.7584228515625 475.5335388183594
323.59918212890625 826.238525390625 731.7511596679688 577.90283203125
268.9367370605469 832.5117797851562 708.8573608398438 608.2982177734375
459.1673889160156 853.7330932617188 832.7071533203125 640.3046875
185.94142150878906 716.7340087890625 952.4401245117188 713.91796875
169.84353637695312 792.5083618164062 463.1822814941406 554.0010986328125
376.000244140625 917.5015258789062 568.4920654296875 854.6214599609375
294.2245178222656 751.673095703125 194.80746459960938 735.2972412109375
409.14154052734375 775.4378662109375 897.9807739257812 853.47705078125
346.73614501953125 863.1689453125 777.9972534179688 586.1721801757812
775.0552978515625 913.116455078125 1099.552001953125 519.9706420898438
75.65567779541016 468.3360900878906 247.00537109375 563.03955078125
365.0856018066406 509.4056091308594 446.0355529785156 584.685546875
261.2919616699219 607.8782348632812 983.4957275390625 599.6174926757812
258.0390625 850.2388305664062 701.110107421875 621.8951416015625
158.16346740722656 721.5205688476562 566.798828125 601.413330078125
398.6167907714844 461.20989990234375 943.6886596679688 454.4562683105469
398.7389831542969 453.9648132324219 492.7894592285156 294.4828186035156
228.9871826171875 677.8126831054688 683.387451171875 613.234375
211.7838592529297 701.2118530273438 264.15472412109375 510.5503845214844
433.7532043457031 776.8671875 828.3287963867188 482.2206115722656
302.5193786621094 836.0662231445312 727.1840209960938 592.915283203125
260.25701904296875 837.8589477539062 696.3265991210938 615.0262451171875
680.8944091796875 899.5816040039062 760.4712524414062 587.1380615234375
696.654296875 347.95928955078125 1158.426513671875 883.6439208984375
187.51544189453125 631.2157592773438 1091.4049072265625 518.4441528320312
260.2186279296875 683.5731811523438 989.5481567382812 736.9423828125
310.6062927246094 763.869140625 686.1119384765625 547.693359375
232.9293212890625 884.1865234375 640.79736328125 634.771484375
131.82093811035156 584.59716796875 141.35507202148438 880.2279052734375
513.0730590820312 798.75 906.6400146484375 447.0837097167969
208.50296020507812 499.74163818359375 1221.7332763671875 703.2756958007812
151.7079315185547 538.6076049804688 1171.26611328125 744.8568115234375
455.8720397949219 923.6216430664062 293.6156311035156 520.2632446289062
755.9539184570312 425.693359375 721.1444702148438 557.8267822265625
236.97491455078125 803.6630249023438 345.28497314453125 573.0845336914062
256.1427917480469 751.1841430664062 815.1627807617188 575.2886352539062
150.6051483154297 808.0634765625 1100.4906005859375 376.1284484863281
451.0987548828125 783.4356689453125 845.8777465820312 475.60662841796875
190.6558074951172 726.7108764648438 198.06886291503906 806.1127319335938
253.4427947998047 811.1024169921875 680.5772094726562 602.2901611328125
474.2422180175781 889.94970703125 1053.8233642578125 782.5803833007812
255.00839233398438 917.341552734375 513.7374877929688 640.0439453125
1222.5206298828125 622.4488525390625 134.43798828125 701.2150268554688
159.8446807861328 635.2228393554688 117.02838134765625 618.8228759765625
292.8087158203125 783.5065307617188 885.8173217773438 486.5264587402344
270.91424560546875 630.530029296875 1082.656494140625 474.8466491699219
169.22506713867188 690.652587890625 534.94384765625 663.4573364257812
184.35617065429688 718.0018920898438 575.8900756835938 588.4228515625
145.36634826660156 793.1689453125 241.65538024902344 558.2052612304688
335.16741943359375 476.04388427734375 700.72900390625 300.7030334472656
338.5652160644531 602.8555908203125 545.0261840820312 405.92352294921875
195.44569396972656 486.26318359375 1114.947998046875 454.03131103515625
444.54022216796875 505.8288879394531 1046.0294189453125 507.0823059082031
I run the code several times like this:
for i in `seq 10`; do sort -R d.dat > d_temp; python minimal.py; done
It means, that I use the same set of correspondet feature point point pairs but I shuffle them.
As a result I got reconstructed rotations (in this case) around the X axis between 14 and 23 degree,
around the Y axis 11 and 33 degree and around the Z axis -45 and -50 degree. The translation vector also varies a lot (x -> [-.99:0.49]. y -> [-0.11:0.15], z -> [-0.9:0] (I dont have an absolute measurement for the rotation but the translation should be in X ~42cm, Y and Z ~0cm)).
As far I understand this is due to the ransac and the 5-point algorithm. They are not deterministic (if there are more than 5 valid feature point correspondance (can you correct me?)).
But in this case how one could know which 'order' of the correspondent feature points gives the real essential matrix? Or is there any better algorithm to hunt down this problem?

calculating the area of an irregular shape from coordinates in a csv file using python

i am using Python to import a csv file with coordinates in it, passing it to a list and using the contained data to calculate the area of each irregular figure. The data within the csv file looks like this.
ID Name DE1 DN1 DE2 DN2 DE3 DN3
88637 Zack Fay -0.026841782 -0.071375637 0.160878583 -0.231788845 0.191811833 0.396593863
88687 Victory Greenfelder 0.219394372 -0.081932907 0.053054879 -0.048356016
88737 Lynnette Gorczany 0.043632299 0.118916157 0.005488698 -0.268612073
88787 Odelia Tremblay PhD 0.083147337 0.152277791 -0.039216388 0.469656787 -0.21725977 0.073797219
The code i am using is below - however it brings up an IndexError: as the first line doesn't have data in all columns. Is there a way to write the csv file so it only uses the colums with data in them ?
import csv
import math
def main():
try:
# ask user to open a file with coordinates for 4 points
my_file = raw_input('Enter the Irregular Differences file name and location: ')
file_list = []
with open(my_file, 'r') as my_csv_file:
reader = csv.reader(my_csv_file)
print 'my_csv_file: ', (my_csv_file)
reader.next()
for row in reader:
print row
file_list.append(row)
all = calculate(file_list)
save_write_file(all)
except IOError:
print 'File reading error, Goodbye!'
except IndexError:
print 'Index Error, Check Data'
# now do your calculations on the 'data' in the file.
def calculate(my_file):
return_list = []
for row in my_file:
de1 = float(row[2])
dn1 = float(row[3])
de2 = float(row[4])
dn2 = float(row[5])
de3 = float(row[6])
dn3 = float(row[7])
de4 = float(row[8])
dn4 = float(row[9])
de5 = float(row[10])
dn5 = float(row[11])
de6 = float(row[12])
dn6 = float(row[13])
de7 = float(row[14])
dn7 = float(row[15])
de8 = float(row[16])
dn8 = float(row[17])
de9 = float(row[18])
dn9 = float(row[19])
area_squared = abs((dn1 * de2) - (dn2 * de1)) + ((de3 * dn4) - (dn3 * de4)) + ((de5 * dn6) - (de6 * dn5)) + ((de7 * dn8) - (dn7 * de8)) + ((dn9 * de1) - (de9 * dn1))
area = area_squared / 2
row.append(area)
return_list.append(row)
return return_list
def save_write_file(all):
with open('output_task4B.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["ID", "Name", "de1", "dn1", "de2", "dn2", "de3", "dn3", "de4", "dn4", "de5", "dn5", "de6", "dn6", "de7", "dn7", "de8", "dn8", "de9", "dn9", "Area"])
writer.writerows(all)
if __name__ == '__main__':
main()
Any suggestions
Your problem appears to be in the calculate function.
You are trying to access various indexes of row without first confirming they exist. One naive approach might be to consider the values to be zero if they are not present, except that:
+ ((dn9 * de1) - (de9 * dn1)
is an attempt to wrap around, and this might invalidate your math since they would go to zero.
A better approach is probably to use a slice of the row, and use the sequence-iterating approach instead of trying to require a certain number of points. This lets your code fit the data.
coords = row[2:] # skip id and name
assert len(coords) % 2 == 0, "Coordinates must come in pairs!"
prev_de = coords[-2]
prev_dn = coords[-1]
area_squared = 0.0
for de, dn in zip(coords[:-1:2], coords[1::2]):
area_squared += (de * prev_dn) - (dn * prev_de)
prev_de, prev_dn = de, dn
area = abs(area_squared) / 2
The next problem will be dealing with variable length output. I'd suggest putting the area before the coordinates. That way you know it's always column 3 (or whatever).

Categories

Resources