How to remove a list inside a multi dimensional list? - python

I have a list of shape (8,20,400). How can I remove that 8 or is it possible just to specify (20,400) as input dimension? thank you
Here is the numpy file
[array([[-2.95904625e-04, 6.40423603e-04, 6.00454747e-04, ...,
-2.27658681e-03, 4.57428713e-02, 2.19978483e-02],
[-6.20380223e-05, 4.70707138e-04, -8.86019713e-03, ...,
2.79925674e-02, 1.03238509e-02, 3.22103941e-02],
[-2.43267652e-04, 4.08970515e-04, -1.00202744e-02, ...,
1.91848757e-03, 8.25676575e-03, 2.52707651e-02],
...,
[ 1.36244897e-04, 1.28135132e-04, -2.24724998e-03, ...,
1.00482352e-02, -5.16041088e-03, 1.67575841e-02],
[-4.56173476e-04, 4.15697708e-04, -2.91441041e-03, ...,
6.27152348e-02, 5.86943632e-02, 3.60700986e-02],
[ 2.97331762e-04, 3.08865978e-04, -2.51690796e-03, ...,
1.01896998e-01, 1.18917078e-02, -1.39016820e-02]]), array([[ 2.01335059e-05, 9.97422893e-05, 1.33110081e-04, ...,
-6.56137938e-01, 2.19010739e-01, -5.99931850e-01],
[ 4.70135697e-05, 6.19096340e-05, 2.01980818e-03, ...,
2.08184651e-01, -2.32254140e-02, 2.59060825e-01],
[ 1.58539360e-04, 5.70696560e-05, 3.40425631e-03, ...,
2.40470642e-02, 3.40782234e-02, 4.70127584e-02],
...,
[ 1.47612366e-04, 3.13279823e-05, 3.65396557e-03, ...,
3.69262189e-03, 8.34838873e-02, -3.89654154e-03],
[ 2.46732289e-05, 3.03740157e-05, 9.34068810e-04, ...,
9.21966957e-02, -4.49042892e-02, 1.08487106e-01],
[-4.69590970e-05, 3.79340001e-05, -1.73730729e-03, ...,
7.42343781e-02, 2.04405703e-01, 6.21275782e-02]]), array([[ 6.48131747e-04, 3.30629899e-04, -1.79023262e-02, ...,
1.80307755e-02, 2.02918323e-02, 2.66390833e-02],
[ 7.42727070e-04, 2.43741144e-04, -3.09066842e-02, ...,
-1.43030498e-02, -8.39430839e-04, -2.06224047e-02],
[ 7.91216410e-04, 1.74964451e-04, -3.36439668e-03, ...,
1.24927181e-03, -3.01857133e-03, 1.68265128e-02],
...,
There are 8 such arrays.

As pointed out in comments,
import numpy as np
A = np.ndarray(shape=(8, 20, 400))
print (A.shape) ## Prints (8, 20, 400)
print (A[0].shape) ## Prints (20, 400)

Related

find Camera Center with opencv

I'm trying to get the camera center from a calibrated camera.
I have 4 measured 3D objectPoints and its images and trying to get the center (translation) from the projective matrix with no acceptable results.
Any advise regarding the accuracy I should expect with opencv? Should I increase the number of points?
These are the results I got:
TrueCenter in mm for XYZ
[[4680.]
[5180.]
[1621.]]
Center
[[-2508.791]
[ 6015.98 ]
[-1096.674]]
import numpy as np
import cv2
from scipy.linalg import inv
TrueCameraCenter = np.array([4680., 5180, 1621]).reshape(-1,1)
objectPoints = np.array(
[[ 0., 5783., 1970.],
[ 0., 5750., 1261.],
[ 0., 6412., 1968.],
[1017., 9809., 1547.]], dtype=np.float32)
imagePoints=np.array(
[[ 833.75, 1097.25],
[ 798. , 1592.25],
[1323. , 1133.5 ],
[3425.5 , 1495.5 ]], dtype=np.float32)
cameraMatrix= np.array(
[[3115.104, -7.3 , 2027.605],
[ 0. , 3077.283, 1504.034],
[ 0. , 0. , 1. ]])
retval, rvec, tvec = cv2.solvePnP(objectPoints, imagePoints,cameraMatrix,None, None, None, False, cv2.SOLVEPNP_ITERATIVE)
R,jac= cv2.Rodrigues(rvec)
imagePoints2,jac= cv2.projectPoints(objectPoints, rvec, tvec, cameraMatrix,None)
print('TrueCenter in mm for XYZ\n', TrueCameraCenter, '\nCenter\n', -inv(R).dot(tvec))
I've found this interesting presentation regarding the Location Determination Problem by Bill Wolfe. Perspective View Of 3 Points
So, using 4 non-coplanar points (non 3 colinear) the solution improved.
import numpy as np
import cv2
from scipy.linalg import inv,norm
TrueCameraCenter = np.array([4680., 5180, 1621])
objectPoints = np.array(
[[ 0., 5783., 1970.],
[ 0., 5750., 1261.],
[ 0., 6412., 1968.],
[ 0., 6449., 1288.]])
imagePoints=np.array(
[[ 497.5 , 674.75],
[ 523.75, 1272.5 ],
[1087.75, 696.75],
[1120. , 1212.5 ]])
cameraMatrix= np.array(
[[3189.096, 0. , 2064.431],
[ 0. , 3177.615, 1482.859],
[ 0. , 0. , 1. ]])
dist_coefs=np.array([[ 0.232, -1.215, -0.002, 0.011, 1.268]])
retval, rvec, tvec = cv2.solvePnP(objectPoints, imagePoints,cameraMatrix,dist_coefs,
None, None, False, cv2.SOLVEPNP_ITERATIVE)
R,_= cv2.Rodrigues(rvec)
C=-inv(R).dot(tvec).flatten()
print('TrueCenter in mm for XYZ\n', TrueCameraCenter, '\nCenter\n',C.astype(int) )
print('Distance:', int(norm(TrueCameraCenter-C)))

Empty 2d histogram, plotly

I use this code:
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
z = []
c = []
for i in range(0,100,2):
z.append(embedding[i])
c.append(embedding2[i])
x = np.asarray(z)
y = np.asarray(c)
print (x)
print (y)
data = [
go.Histogram2d(
x=x,
y=y
)
]
py.iplot(data)
Embeddings are two np.ndarray with shape (10242,10242). The data is passed to the histogram, below is x and y printed out. The loop is just to reduce the data for this test.
[[ -1.07009469e-03 -4.14190844e-05 2.83694127e-05 ..., 3.80232710e-49 1.11805697e-48 1.35691326e-49]
[ -2.51900739e-04 -1.24530108e-04 1.06271439e-04 ..., -1.70912532e-49 3.49318016e-49 -6.71164371e-50]
[ 4.75347102e-04 -2.99087401e-04 7.38855263e-06 ..., -1.77945437e-50 2.50268997e-49 1.32159499e-49]
...,
[ 7.19245324e-05 3.62139711e-06 6.79869681e-05 ..., 9.24108834e-37 2.77809808e-36 -1.77212812e-36]
[ 1.49892464e-04 8.55360952e-05 8.07507441e-05 ..., -4.85739967e-49 3.32748802e-49 -1.22085056e-49]
[ 3.94947451e-05 -7.60262599e-05 -1.83208907e-05 ..., 7.93846393e-50 3.08010941e-50 8.93558373e-51]]
[[ -1.07009469e-03 -4.14190844e-05 2.83694127e-05 ..., 3.80232710e-49 1.11805697e-48 1.35691326e-49]
[ -2.51900739e-04 -1.24530108e-04 1.06271439e-04 ..., -1.70912532e-49 3.49318016e-49 -6.71164371e-50]
[ 4.75347102e-04 -2.99087401e-04 7.38855263e-06 ..., -1.77945437e-50 2.50268997e-49 1.32159499e-49]
...,
[ 7.19245324e-05 3.62139711e-06 6.79869681e-05 ..., 9.24108834e-37 2.77809808e-36 -1.77212812e-36]
[ 1.49892464e-04 8.55360952e-05 8.07507441e-05 ..., -4.85739967e-49 3.32748802e-49 -1.22085056e-49]
[ 3.94947451e-05 -7.60262599e-05 -1.83208907e-05 ..., 7.93846393e-50 3.08010941e-50 8.93558373e-51]]
But produced output is empty:
Why does this happen? And how can I display the data?

How to vectorize a 'for' loop which calls a function (that takes a 2-Dimensional array as argument) over a 3-Dimensional numpy array

I have a numpy array containing the XYZ coordinates of the k-neighboors (k=10) points from a point cloud:
k_neighboors
Out[53]:
array([[[ 2.51508147e-01, 5.60274944e-02, 1.98303187e+00],
[ 2.48552352e-01, 5.95569573e-02, 1.98319519e+00],
[ 2.56611764e-01, 5.36767729e-02, 1.98236740e+00],
...,
[ 2.54520357e-01, 6.23480231e-02, 1.98255634e+00],
[ 2.57603496e-01, 5.19787706e-02, 1.98221457e+00],
[ 2.43914440e-01, 5.68424985e-02, 1.98352253e+00]],
[[ 9.72352773e-02, 2.06699912e-02, 1.99344850e+00],
[ 9.91205871e-02, 2.36056261e-02, 1.99329960e+00],
[ 9.59625840e-02, 1.71508361e-02, 1.99356234e+00],
...,
[ 1.03216261e-01, 2.19752081e-02, 1.99304521e+00],
[ 9.65025574e-02, 1.44127617e-02, 1.99355054e+00],
[ 9.59930867e-02, 2.72080526e-02, 1.99344873e+00]],
[[ 1.76408485e-01, 2.81930678e-02, 1.98819435e+00],
[ 1.78670138e-01, 2.81904750e-02, 1.98804617e+00],
[ 1.80372953e-01, 3.05109434e-02, 1.98791444e+00],
...,
[ 1.81960404e-01, 2.47725621e-02, 1.98785996e+00],
[ 1.74499243e-01, 3.50728296e-02, 1.98826015e+00],
[ 1.83470801e-01, 2.70808022e-02, 1.98774099e+00]],
...,
[[ 1.78178743e-01, -4.60980982e-02, -1.98792374e+00],
[ 1.77953839e-01, -4.73701134e-02, -1.98792756e+00],
[ 1.77889392e-01, -4.75468598e-02, -1.98793030e+00],
...,
[ 1.79924294e-01, -5.08776568e-02, -1.98772371e+00],
[ 1.76720902e-01, -5.11409082e-02, -1.98791265e+00],
[ 1.83644593e-01, -4.64747548e-02, -1.98756230e+00]],
[[ 2.00245917e-01, -2.33091787e-03, -1.98685515e+00],
[ 2.02384919e-01, -5.60011715e-04, -1.98673022e+00],
[ 1.97325528e-01, -1.03301927e-03, -1.98705769e+00],
...,
[ 1.95464164e-01, -6.23105839e-03, -1.98713481e+00],
[ 1.98985338e-01, -8.39920342e-03, -1.98688531e+00],
[ 1.95959195e-01, 2.68006674e-03, -1.98713303e+00]],
[[ 1.28851235e-01, -3.24527062e-02, -1.99127460e+00],
[ 1.26415789e-01, -3.27731185e-02, -1.99143147e+00],
[ 1.25985757e-01, -3.24910432e-02, -1.99146211e+00],
...,
[ 1.28296465e-01, -3.92388329e-02, -1.99117136e+00],
[ 1.34895295e-01, -3.64872888e-02, -1.99083793e+00],
[ 1.29047096e-01, -3.97952795e-02, -1.99111152e+00]]])
With this shape:
k_neighboors.shape
Out[54]: (2999986, 10, 3)
And I have this function which applies a Principal Component Analysis to some data provided as 2-Dimensional array:
def PCA(data, correlation=False, sort=True):
""" Applies Principal Component Analysis to the data
Parameters
----------
data: array
The array containing the data. The array must have NxM dimensions, where each
of the N rows represents a different individual record and each of the M columns
represents a different variable recorded for that individual record.
array([
[V11, ... , V1m],
...,
[Vn1, ... , Vnm]])
correlation(Optional) : bool
Set the type of matrix to be computed (see Notes):
If True compute the correlation matrix.
If False(Default) compute the covariance matrix.
sort(Optional) : bool
Set the order that the eigenvalues/vectors will have
If True(Default) they will be sorted (from higher value to less).
If False they won't.
Returns
-------
eigenvalues: (1,M) array
The eigenvalues of the corresponding matrix.
eigenvector: (M,M) array
The eigenvectors of the corresponding matrix.
Notes
-----
The correlation matrix is a better choice when there are different magnitudes
representing the M variables. Use covariance matrix in any other case.
"""
#: get the mean of all variables
mean = np.mean(data, axis=0, dtype=np.float64)
#: adjust the data by substracting the mean to each variable
data_adjust = data - mean
#: compute the covariance/correlation matrix
#: the data is transposed due to np.cov/corrcoef sintaxis
if correlation:
matrix = np.corrcoef(data_adjust.T)
else:
matrix = np.cov(data_adjust.T)
#: get the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(matrix)
if sort:
#: sort eigenvalues and eigenvectors
sort = eigenvalues.argsort()[::-1]
eigenvalues = eigenvalues[sort]
eigenvectors = eigenvectors[:,sort]
return eigenvalues, eigenvectors
So the question is: how can I apply the PCA function mentioned above over each of the 2999986 10x3 arrays in a way that doesn't take for ever like this one:
data = np.empty((2999986, 3))
for i in range(len(k_neighboors)):
w, v = PCA(k_neighboors[i])
data[i] = v[:,2]
break #: I break the loop in order to don't have to wait for ever.
data
Out[64]:
array([[ 0.10530792, 0.01028906, 0.99438643],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]])
Thanks to #Divakar and #Eelco comments.
Using the function that Divakar post on this answer
def vectorized_app(data):
diffs = data - data.mean(1,keepdims=True)
return np.einsum('ijk,ijl->ikl',diffs,diffs)/data.shape[1]
And using what Eelco pointed on his comment, I end up with this.
k_neighboors.shape
Out[48]: (2999986, 10, 3)
#: THE (ASSUMED)VECTORIZED ANSWER
data = np.linalg.eig(vectorized_app(k_neighboors))[1][:,:,2]
data
Out[50]:
array([[ 0.10530792, 0.01028906, 0.99438643],
[ 0.06462 , 0.00944352, 0.99786526],
[ 0.0654035 , 0.00860751, 0.99782177],
...,
[-0.0632175 , 0.01613551, 0.99786933],
[-0.06449399, 0.00552943, 0.99790278],
[-0.06081954, 0.01802078, 0.99798609]])
Wich gives the same results as the for loop, without taking forever (althought still takes a while):
data2 = np.empty((2999986, 3))
for i in range(len(k_neighboors)):
if i > 10:
break #: I break the loop in order to don't have to wait for ever.
w, v = PCA(k_neighboors[i])
data2[i] = v[:,2]
data2
Out[52]:
array([[ 0.10530792, 0.01028906, 0.99438643],
[ 0.06462 , 0.00944352, 0.99786526],
[ 0.0654035 , 0.00860751, 0.99782177],
...,
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ],
[ 0. , 0. , 0. ]])
I don't know if there could be a better way to do this, so I'm going to keep the question open.

Python optimize leastsq error Result from function call is not a proper array of floats

I have array d with my data:
--> d
array([[ 60.41202301, 58.39997156, 55.3667636 , ..., -84.87512796,
-86.79190447, -86.19353546],
[ 60.10975935, 58.05402795, 55.3898762 , ..., -86.60428129,
-88.12205283, -89.45247056],
[ 60.14155715, 58.09627498, 55.7903388 , ..., -89.18612111,
-90.50667213, -92.67073841],
...,
[ 85.91138845, 84.50241834, 82.63664476, ..., -71.04692203,
-87.06297169, -100.36565732],
[ 82.74086966, 81.80343637, 79.96785709, ..., -64.08451752,
-79.18141593, -99.36881913],
[ 79.15278282, 78.42730549, 77.1980837 , ..., -58.38254858,
-75.93821858, -93.55287173]])
and 2 more arrays, with the values of the radius and angle in each pixel:
--> radius
array([[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 41.03656906, 40.31128874, 39.59797975, ..., 36.23534186,
36.87817783, 37.53664876],
...,
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 43.13930922, 42.44997055, 41.77319715, ..., 38.60051813,
39.20459157, 39.8246155 ]])
--> angle
array([[ 3.92699082, 3.94393835, 3.96145992, ..., 5.3664717 ,
5.38712992, 5.40712726],
[ 3.91004329, 3.92699082, 3.94453288, ..., 5.38291054,
5.40372591, 5.42384811],
[ 3.89252172, 3.90944876, 3.92699082, ..., 5.40006024,
5.42101525, 5.44124371],
...,
[ 2.37314202, 2.35619449, 2.33865243, ..., 0.90027477,
0.8794594 , 0.8593372 ],
[ 2.35619449, 2.33924696, 2.32172539, ..., 0.9167136 ,
0.89605538, 0.87605805],
[ 2.33980252, 2.32287349, 2.30539079, ..., 0.93247652,
0.91199029, 0.89213384]])
I also have p0, the object I want to optimize:
--> p0
[1.0, 500.0, 0.5, 0.5, 5.2, 0.4]
and definitions of my functions:
def rotation(B,radiusref,angleref):
return B[0] + (B[1] * radiusref * np.cos(angleref-B[2]) * np.sin(B[3]) * np.cos(B[3])**B[5] ) / (radiusref**2 * ( np.sin(angleref-B[2])**2 + np.cos(B[3])**2*np.cos(angleref-B[2])**2 ) + B[4]**2*np.cos(B[3])**2 )**B[5]/2.0
def errfunc(p0,d,radius,angle):
return rotation(p0,radius,angle) - d
Then, I try to run optimize.leastsq to find the best solution, and I get following error message:
--> fit = optimize.leastsq(errfunc,p0,args=(d,radius,angle))
ValueError: object too deep for desired array
Traceback (innermost last):
File "<console>", line 1, in <module>
File "/home/luisgdh/Ureka/python/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 379, in leastsq
gtol, maxfev, epsfcn, factor, diag)
error: Result from function call is not a proper array of floats.
Why does this happen?
If I try to run errfunc I get this
--> errfunc(p0,d,radius,angle)
array([[-311.96251984, -307.50362989, -301.89956899, ..., 121.84425081,
128.7017239 , 132.9161849 ],
[-312.24524019, -307.79621255, -302.61362546, ..., 127.21717127,
133.70343473, 139.86845861],
[-312.81581149, -308.43470253, -303.66703887, ..., 133.56598817,
139.87929082, 146.89601654],
...,
[-156.59031196, -150.97315566, -144.78021889, ..., 305.55786014,
324.82275173, 341.21407515],
[-149.69283006, -144.54525672, -138.38451089, ..., 297.67836311,
316.06244785, 339.38095877],
[-142.47209078, -137.53819725, -131.98946431, ..., 291.05301549,
311.92874028, 332.71166956]])
I solved the problem my self, if someone finds this problem again.. leastsq wasn't dealing well with 2d data, so I transformed the 2d data in 1d data via:
d1radius = radius.ravel()
d1angle=angle.ravel()
d1data=d.ravel()
and redefined the function:
def errfunc(p0,d1data,d1radius,d1angle):
return rotation(p0,d1radius,d1angle) - d1data
And finally:
fit = optimize.leastsq(errfunc,p0,args=(d1data,d1radius,d1angle))

plotting a list of arrays with matplotlib

I have a list raws of arrays that I would like to plot in ipython notebook. Here is the code I am trying to get working:
fig, axes = subplots(len(raws),1, sharex=True, tight_layout=True, figsize=(12, 6), dpi=72)
for r in range(len(raws)):
axes[r].plot(raws)
I've been lost for hours if not days trying to figure out how to index the list raws, such that I can plot each mxn array on it's own axis where n is the number of time points, i.e., x-axis and m is the number of time-series functions sampled at each point.
When I code:
for r in range(len(raws)):
axes[r].plot(raws[r])
I get an ValueError: setting an array element with a sequence.
For your information:
len(raws) = 2
type(raws) = 'list'
np.shape(raws[0][0]) = (306, 10001)
raws =
[(array([[ -4.13211217e-12, -4.13287303e-12, -4.01705259e-12, ...,
1.36386023e-12, 1.65182851e-12, 2.00368966e-12],
[ 1.08914129e-12, 1.47828466e-12, 1.82257607e-12, ...,
-2.70151520e-12, -2.48631967e-12, -2.28625548e-12],
[ -7.80962369e-14, -1.27119591e-13, -1.73610315e-13, ...,
-1.13219629e-13, -1.15031720e-13, -1.12106621e-13],
...,
[ 2.52774254e-12, 2.32293195e-12, 2.02644002e-12, ...,
4.20064191e-12, 3.94858906e-12, 3.69495394e-12],
[ -4.38122146e-12, -4.96229676e-12, -5.47782145e-12, ...,
3.93820033e-12, 4.18850823e-12, 4.34950629e-12],
[ -1.07284424e-13, -9.23447993e-14, -7.89852400e-14, ...,
7.92079631e-14, 5.60172215e-14, 3.04448868e-14]]), array([ 60. , 60.001, 60.002, ..., 69.998, 69.999, 70. ])), (array([[ -6.71363108e-12, -5.80501003e-12, -4.95944514e-12, ...,
-3.25087343e-12, -2.68982494e-12, -2.13637448e-12],
[ -5.04818633e-12, -4.65757005e-12, -4.16084140e-12, ...,
-4.26120531e-13, 2.20744290e-13, 7.81245614e-13],
[ 1.97329506e-13, 1.64543867e-13, 1.32679812e-13, ...,
2.11645494e-13, 1.94795729e-13, 1.75781773e-13],
...,
[ 3.04245661e-12, 2.28376461e-12, 1.54118900e-12, ...,
-1.14020908e-14, -8.04647589e-13, -1.52676489e-12],
[ -1.83485962e-13, -5.22949893e-13, -8.60038852e-13, ...,
7.70312553e-12, 7.20825156e-12, 6.58362857e-12],
[ -7.26357906e-14, -7.11700989e-14, -6.88759767e-14, ...,
-1.04171843e-13, -1.03084861e-13, -9.68462427e-14]]), array([ 60. , 60.001, 60.002, ..., 69.998, 69.999, 70. ]))]
Just so I can post code, I am responding here.
Looks like your data is nested in the form
[ ( array1, array2, ..., arrayN ) ]
This could be handled in one of two ways:
In [2]: raws = [np.random.rand(20, 100), np.random.rand(20, 100)]
In [3]: raws = raws[0]
In [4]: f, axes = plt.subplots(len(raws), 1)
In [5]: for i in range(len(raws)):
...: axes[i].plot(raws[i])
Or
In [3]: raws = [(np.random.rand(20, 100), np.random.rand(20, 100))]
In [4]: f, axes = plt.subplots(len(raws[0]), 1)
In [5]: for i in range(len(raws[0])):
...: axes[i].plot(raws[0][i])
If you have a list of arrays such as abac below, you can plot as following:
import numpy as np
a = np.array(range(20))
b = a * 2
c = a * 3
abac = a,b,a,c
plt.plot(*abac)

Categories

Resources