"Axis greater than data dimensions" - Python - python

i have a simple program that i have pasted below.I have a problem because when I run the program I get an error. Here are my errors:
Traceback (most recent call last):
File "C:\Users\Anaconda3\lib\site-packages\pywt\_multilevel.py", line 90, in wavedec
axes_shape = data.shape[axis]
IndexError: tuple index out of range
During handling of the above exception, another exception occurred:
python
Traceback (most recent call last):
File "C:/Users/Main.py", line 10, in <module>
tree = pywt.wavedec(data=record, wavelet='db2', level=5, mode='symmetric')
File "C:\Users\Anaconda3\lib\site-packages\pywt\_multilevel.py", line 92, in wavedec
raise ValueError("Axis greater than data dimensions")
ValueError: Axis greater than data dimensions
And this is my code:
import wfdb
import pywt
import matplotlib.pyplot as plt
record = wfdb.rdsamp('230', sampto = 2000)
annotation = wfdb.rdann('230', 'atr', sampto = 2000)
wfdb.plotrec(record, annotation = annotation, title='Output record', timeunits = 'seconds')
tree = pywt.wavedec(data=record, wavelet='db2', level=5, mode='symmetric')
newTree = [tree[0], tree[1], tree[2], tree[3]*0, tree[4]*0, tree[5]*0]
recSignal = pywt.waverec(newTree, 'db2')
plt.plot(recSignal[:2000])
What, in your opinion, could change in the code to make the program work?

This is this code to line 90
def wavedec(data, wavelet, mode='symmetric', level=None, axis=-1):
"""
Multilevel 1D Discrete Wavelet Transform of data.
Parameters
----------
data: array_like
Input data
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see Modes (default: 'symmetric')
level : int, optional
Decomposition level (must be >= 0). If level is None (default) then it
will be calculated using the ``dwt_max_level`` function.
axis: int, optional
Axis over which to compute the DWT. If not given, the
last axis is used.
Returns
-------
[cA_n, cD_n, cD_n-1, ..., cD2, cD1] : list
Ordered list of coefficients arrays
where `n` denotes the level of decomposition. The first element
(`cA_n`) of the result is approximation coefficients array and the
following elements (`cD_n` - `cD_1`) are details coefficients arrays.
Examples
--------
>>> from pywt import wavedec
>>> coeffs = wavedec([1,2,3,4,5,6,7,8], 'db1', level=2)
>>> cA2, cD2, cD1 = coeffs
>>> cD1
array([-0.70710678, -0.70710678, -0.70710678, -0.70710678])
>>> cD2
array([-2., -2.])
>>> cA2
array([ 5., 13.])
"""
data = np.asarray(data)
if not isinstance(wavelet, Wavelet):
wavelet = Wavelet(wavelet)
try:
axes_shape = data.shape[axis]
except IndexError:
raise ValueError("Axis greater than data dimensions")
level = _check_level(axes_shape, wavelet.dec_len, level)
coeffs_list = []
a = data
for i in range(level):
a, d = dwt(a, wavelet, mode, axis)
coeffs_list.append(d)
coeffs_list.append(a)
coeffs_list.reverse()
return coeffs_list

Related

solve deflection curve using scipy.solve_ivp error: 'Required step size is less than spacing between numbers.'

I encountered a failure on solving deflection curve using scipy.solve_ivp when I used a big length for the beam(pipe). The following is my script.
# -*- coding: utf-8 -*-
#
# Calculate deflection curve under gravity
#
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# round pipe's sectional area
def area_round(D,t):
d = D - 2 * t
return np.pi*(D*D-d*d)/4.0
# area moment of inertia section properties of round pipe
def moi_round(D,t):
d = D - 2 * t
return np.pi/64.0*(D**4-d**4)
# gravity force per unit length for round pipe
def G_round(dens,D,t):
g = 9.8 # gravity coef, N/kg
A = area_round(D,t)
return A*dens*g
# Calculate 2D plane(xy) bending of a beam undering self-gravity
# minus y is the gravity direction
# - Young's modules E
# - area moment of inertia: Iz
# x: coordinate position along x-axis
# u=[y,dydx]: deflection at y direction , first order derivative on x
def deflection(x,u,E,Iz,G,LG):
y,dy_dx = u
MG = np.select([x<=LG,x>LG],[G*(0.5*LG**2+0.5*x**2-LG*x),0]) # distributed force
ddy_ddx = np.power(1.0+dy_dx*dy_dx,3.0/2.0)*MG/E/Iz
return [dy_dx,ddy_ddx]
# solve initial value problem using numerical method
# Given x list, calcuate y and y'
def solve_deflection(E,Iz,Gy,Lg,x_list):
# initial value
y0 = 0.0
dy_dx_x0 = 0.0
sol = solve_ivp(deflection,t_span=[0.0,Lg],y0=[y0,dy_dx_x0],method='RK45',t_eval=x_list,vectorized=True,args=(E,Iz,Gy,Lg),rtol=1.0e-3,atol=1.0e-4)
print("sol:",sol)
return sol.y[0,:],sol.y[1,:]
# test of solving deflection equation
# unit sytem: length mm force:N pressure:N/mm^2/MPa
def test():
dens = 7880.0e-9 # Kg/mm^3
E = 210e3 # elastic modulus, N/mm^2
# round pipe
Dp = 60.0 # mm, outer diameter
tp = 3.0 # mm ,thickness
print("Dp:",Dp,' tp:',tp)
Iz= moi_round(Dp,tp) # section const
Gy = -G_round(dens,Dp,tp) # gravity force per unit
print(" Iz:",Iz,' Gy:',Gy)
# Lg = 18799.1 # mm, this length is OK
Lg = 18799.2 # mm, this length leads to failure
print("Lg=",Lg)
xlist = np.linspace(0.0,Lg,num=100,endpoint=True)
res = solve_deflection(E, Iz, Gy=Gy, Lg=Lg, x_list=xlist)
plt.plot(xlist,res[0])
plt.show()
if __name__ == '__main__':
test()
pass
When the length is 10000mm, it can give correct result as the following graph
But when I changed it to 20000mm, it failed and output the following messages,
sol: message: 'Required step size is less than spacing between numbers.'
nfev: 944
njev: 0
nlu: 0
sol: None
status: -1
success: False
t: array([ 0. , 202.02020202, 404.04040404, 606.06060606,
808.08080808, 1010.1010101 , 1212.12121212, 1414.14141414,
1616.16161616, 1818.18181818, 2020.2020202 , 2222.22222222,
2424.24242424, 2626.26262626, 2828.28282828, 3030.3030303 ,
3232.32323232, 3434.34343434, 3636.36363636, 3838.38383838,
4040.4040404 , 4242.42424242, 4444.44444444, 4646.46464646,
4848.48484848, 5050.50505051, 5252.52525253, 5454.54545455,
5656.56565657, 5858.58585859, 6060.60606061, 6262.62626263,
6464.64646465, 6666.66666667, 6868.68686869, 7070.70707071,
7272.72727273, 7474.74747475, 7676.76767677, 7878.78787879,
8080.80808081, 8282.82828283, 8484.84848485, 8686.86868687,
8888.88888889])
t_events: None
y: array([[ 0.00000000e+00, -3.66155907e+00, -1.45618988e+01,
-3.25951385e+01, -5.76794150e+01, -8.97565181e+01,
-1.28794030e+02, -1.74786625e+02, -2.27730361e+02,
-2.87640987e+02, -3.54556806e+02, -4.28538678e+02,
-5.09670018e+02, -5.98056798e+02, -6.93827544e+02,
-7.97133338e+02, -9.08147821e+02, -1.02706718e+03,
-1.15411018e+03, -1.28951811e+03, -1.43356488e+03,
-1.58659673e+03, -1.74895654e+03, -1.92104924e+03,
-2.10335332e+03, -2.29642078e+03, -2.50087718e+03,
-2.71742163e+03, -2.94684282e+03, -3.19021861e+03,
-3.44843893e+03, -3.72265946e+03, -4.01447603e+03,
-4.32592470e+03, -4.65948174e+03, -5.01806360e+03,
-5.40502733e+03, -5.82494500e+03, -6.28452879e+03,
-6.79299154e+03, -7.36408180e+03, -8.02047137e+03,
-8.80421033e+03, -9.81478548e+03, -1.15113253e+04],
[ 0.00000000e+00, -3.61399978e-02, -7.16869806e-02,
-1.06770973e-01, -1.41512819e-01, -1.76024795e-01,
-2.10415154e-01, -2.44799321e-01, -2.79258778e-01,
-3.13875788e-01, -3.48738932e-01, -3.83943102e-01,
-4.19589504e-01, -4.55785660e-01, -4.92645404e-01,
-5.30288885e-01, -5.68842566e-01, -6.08439223e-01,
-6.49217947e-01, -6.91324142e-01, -7.34921284e-01,
-7.80235186e-01, -8.27461362e-01, -8.76842769e-01,
-9.28683414e-01, -9.83348358e-01, -1.04126372e+00,
-1.10291665e+00, -1.16888799e+00, -1.24022057e+00,
-1.31734604e+00, -1.40101902e+00, -1.49267854e+00,
-1.59444811e+00, -1.70913565e+00, -1.84023352e+00,
-1.99191927e+00, -2.17052401e+00, -2.38680965e+00,
-2.65797898e+00, -3.01398821e+00, -3.51651158e+00,
-4.31409773e+00, -5.92506745e+00, -1.41760305e+01]])
y_events: None
Traceback (most recent call last):
File "mwe.py", line 82, in <module>
test()
File "mwe.py", line 76, in test
plt.plot(xlist,res[0])
File "\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot
return gca().plot(
File "\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (100,) and (45,)
I have read similar question on solve_ivp error: 'Required step size is less than spacing between numbers.'
But I can not understand it.
I also tried different atol value and can not solve the issue.
I have struggled on this for several days.
Anyone can help me out? Thank you.

zero size array to reduction operation maximum which has no identity

Hi I am trying to work out why I am getting this Value Error. I am opening a table I created in Excel and assigning it to an array in Python. I have some satellite imagery that I want to find some statistics for. It is throwing up a value error, and I have checked that the image opens in Python beforehand and it does.
First I am opening the table in python
path = "D:\\Iceberg data\\RADARSAT\\"
Table_icebergs_all = pd.read_excel(path+'Image_index_SG_C.xlsx', '20160415')
# the last argument in the function selects the sheet inside the excel file
# HERE IT EXTRACTS THE ICEBERG POSITIONS IN THE TABLE
dim = Table_icebergs_all.shape
# GETS THE DIMENSION OF THE TABLES
dim_sat = iceberg_sat.shape[0]
# ALLOCATE THE COORDINATES TO NUMPY ARRAYS FOR SATELLITE POINTS
x_sat = np.zeros(dim_sat, dtype = 'int')
y_sat = np.zeros(dim_sat, dtype = 'int')
for i in range(0, dim_sat):
x_sat[i] = np.round(iceberg_sat['python x'][i])
y_sat[i] = np.round(iceberg_sat['python y'][i])
# HERE IT EXTRACTS THE CLUTTER POSITION IN THE TABLE
# cl1 is the upper left corner
# cl2 is the bottom right corner
############# FOR THE SATELLITE IMAGE
cl1_x_sat = np.zeros(dim_sat, dtype = 'int')
cl1_y_sat = np.zeros(dim_sat, dtype = 'int')
cl2_x_sat = np.zeros(dim_sat, dtype = 'int')
cl2_y_sat = np.zeros(dim_sat, dtype = 'int')
for i in range(0, dim_sat):
cl1_x_sat[i] = np.round(iceberg_sat['py x min'][i])
cl1_y_sat[i] = np.round(iceberg_sat['py y min'][i])
cl2_x_sat[i] = np.round(iceberg_sat['py x max'][i])
cl2_y_sat[i] = np.round(iceberg_sat['py y max'][i])
sat = T11_april15
gnd = gpri_april15
ice_sat_max = np.zeros(dim_sat)
cl_sat_max = np.zeros(dim_sat)
cl_sat_mean = np.zeros(dim_sat)
for i in range(dim_sat):
ice_sat_max[i] = sat[x_sat[i],y_sat[i]]
cl_sat_mean[i] = np.mean(sat[cl1_x_sat[i]:cl2_x_sat[i],cl1_y_sat[i]:cl2_y_sat[i]])
cl_sat_max[i] = np.max(sat[cl1_x_sat[i]:cl2_x_sat[i],cl1_y_sat[i]:cl2_y_sat[i]])
# we evalute the target to clutter ratio using the mean or the max of the clutter
TCR_sat_mean = ice_sat_max/cl_sat_mean
TCR_sat_max = ice_sat_max/cl_sat_max
Here is the error code:
Traceback (most recent call last):
File "<ipython-input-5-2483c16a03b9>", line 12, in <module>
cl_sat_max[i] = np.max(sat[cl1_x_sat[i]:cl2_x_sat[i],cl1_y_sat[i]:cl2_y_sat[i]])
File "<__array_function__ internals>", line 6, in amax
File "C:\Users\job2\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 2668, in amax
keepdims=keepdims, initial=initial, where=where)
File "C:\Users\job2\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 90, in _wrapreduction
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
ValueError: zero-size array to reduction operation maximum which has no identity
Does anyone know how to fix this? It is using the numpy mean and numpy max.

Error in Fitting a curve using curve_fit in python

I'm trying to fit the next function into some data using the Scipy Curve_fit function:
def sinugauss(x, A, B, C):
exponente = A*(np.sin(x-B))**2
return np.array([C/(np.exp(exponente))])
I have a data set of 33 points but I keep getting this error:
Traceback (most recent call last):\
File "D:Es_periodico_o_no.py", line 35, in <module>\
res, cov = curve_fit(sinugauss,datos['x'],datos['y'])\
File "D:\lib\site-packages\scipy\optimize\minpack.py", line 789, in curve_fit\
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)\
File "D:\lib\site-packages\scipy\optimize\minpack.py", line 414, in leastsq
raise TypeError(f"Improper input: func input vector length N={n} must"\
TypeError: Improper input: func input vector length N=3 must not exceed func output vector length M=1
This is the full code:
def sinugauss(x, Ventas, Inicio, Desv):
exponente = Desv*(np.sin(x-Inicio))**2
return np.array([Ventas/(np.exp(exponente))])
for index, row in real_df.iterrows():
datos_y = np.array([row]).transpose()
datos_x = np.array([range(len(datos_y))]).transpose()
datos = pd.DataFrame(np.column_stack([datos_x,datos_y]),columns=['x','y'])
res, cov = curve_fit(sinugauss,datos['x'],datos['y'])
print(res)
print(cov)
The error raises since the first iteration, all the rows has 33 not nan points. There may be zeros
Thank you
In the function sinugauss, change the return statement to:
return C/np.exp(exponente)
When you write np.array([C/(np.exp(exponente))]), you are wrapping the expression C/np.exp(exponente), which might be an array with shape, say, (3,), in a 2-d array with shape (1, 3). That is not the shape that curve_fit expects from your function.

scipy.optimize.leastsq error not an array of floats

This is my code
import os
import sys
import numpy as np
import scipy
from scipy.optimize import leastsq
def peval (inp_mat,p):
m0,m1,m2,m3,m4,m5,m6,m7 = p
out_mat = np.array(np.zeros(inp_mat.shape,dtype=np.float32))
mid = inp_mat.shape[0]/2
for xy in range(0,inp_mat.shape[0]):
if (xy<(inp_mat.shape[0]/2)):
out_mat[xy] = ( ( (inp_mat[xy+mid]*m0)+(inp_mat[xy]*m1)+ m2 ) /( (inp_mat[xy+mid]*m6)+(inp_mat[xy]*m7)+1 ) )
else:
out_mat[xy] = ( ( (inp_mat[xy]*m3)+(inp_mat[xy-mid]*m4)+ m5 ) /( (inp_mat[xy]*m6)+(inp_mat[xy-mid]*m7)+1 ) )
return np.array(out_mat)
def residuals(p, out_mat, inp_mat):
m0,m1,m2,m3,m4,m5,m6,m7 = p
err=np.array(np.zeros(inp_mat.shape,dtype=np.float32))
if (out_mat.shape == inp_mat.shape):
for xy in range(0,inp_mat.shape[0]):
err[xy] = err[xy]+ (out_mat[xy] -inp_mat[xy])
return np.array(err)
f = open('/media/anilil/Data/Datasets/repo/txt_op/vid.txt','r')
x = np.loadtxt(f,dtype=np.int16,comments='#',delimiter='\t')
nof = x.shape[0]/72 # Find the number of frames
x1 = x.reshape(-1,60,40)
x1_1= x1[0,:,:].flatten()
x1_2= x1[1,:,:].flatten()
x= []
y= []
for xy in range(1,50,1):
y.append(x1[xy,:,:].flatten())
x.append(x1[xy-1,:,:].flatten())
x=np.array(x,dtype=np.float32)
y=np.array(y,dtype=np.float32)
length = x1_1.shape#initail guess
p0 = np.array([1,1,1,1,1,1,1,1],dtype=np.float32)
abc=leastsq(residuals, p0,args=(y,x))
print ('Size of first matrix is '+str(x1_1.shape))
print ('Size of first matrix is '+str(x1_2.shape))
print ("Done with program")
I have tried adding np.array in most places with no use.
Could someone please help me ?
Another question here is do I give the output of the residuals() as a single value by adding all errorsnp.sum(err,axis=1). or leave it the way it is ?
When I return np.sum(err,axis=1) in the function residuals(). There is no change in the initial guess. It just remains the same.
I.E error is for each item in the input output mapping. or a combined error overall ?
Example data.
Output
ValueError: object too deep for desired array
Traceback (most recent call last):
File "/media/anilil/Data/charm/mv_clean/.idea/nose_reduction_mpeg.py", line 49, in <module>
abc=leastsq(residuals, p0,args=(y,x))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 378, in leastsq
gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.
leastsq requires a 1D array to be returned from your residuals function.
Currently you calculate the residuals for the whole image and return that as a 2D array.
The simple fix would be to flatten the array of residuals (turning your 2D array into a 1D one).
So instead of returning
return np.array(err)
Do this instead
return err.flatten()
Note that err is already a numpy array so doesn't need to be cast before the return (I guess that slipped in when you were trying to debug it!)

ValueError while computing convolution using scipy

from scipy import ndimage
import numpy as np
with open("Data.dat", "r+") as f:
content = f.readlines()
content = [s.strip() for s in content]
content = np.asarray(content)
weights = np.array([1, 2, 4, 2, 1])
final = np.empty(content)
ndimage.filters.convolve1d(content, weights, -1, final, 'reflect', 0.0, 0.0)
Here np is the numpy package. The length of content is 750, so tried to initialize the shape of the output array with np.empty() as I don't want any value in that.
But when I run I get this error:
Traceback (most recent call last):
File "C:\Users\Animesh\Desktop\Smoothing.py", line 18, in <module>
final = np.empty(content)
ValueError: sequence too large; must be smaller than 32
What should be done ?
To make final an empty array of the same shape and dtype as content, use:
final = np.empty_like(content)
Regarding the TypeError: integer argument expected, got float:
Although the docstring for convolve1d says
origin : scalar, optional
The `origin` parameter controls the placement of the filter.
Default 0.0.
the origin argument must be an integer, not a float.
Here is an example which runs without error:
import scipy.ndimage as ndimage
import numpy as np
# content = np.genfromtxt('Data.dat')
content = np.asarray(np.arange(750))
weights = np.array([1, 2, 4, 2, 1])
final = np.empty_like(content)
ndimage.convolve1d(content, weights, axis=-1, output=final, mode='reflect', cval=0.0,
origin=0
# origin=0.0 # This raises TypeError
)
print(final)
Uncommenting origin=0.0 raises the TypeError.
Regarding
with open("Data.dat", "r+") as f:
content = f.readlines()
content = [s.strip() for s in content]
content = np.asarray(content)
This makes content an array of strings. Since you are taking a convolution, you must want an array of numbers. So instead replace the above with
content = np.genfromtxt('Data.dat')

Categories