Struggling to Understand Error Message - python

I'm trying to run the code below, where Bwavelength, throughput and newflux are lists.
def ABconversion(Bwavelength, throughput):
ABconstant=[]
c=3e18
i=0
for i in range(0, len(Bwavelength)):
ABconstant.append(((3e18/((Bwavelength[i])**2))*throughput[i]))
i+=1
print len(Bwavelength), len(ABconstant), ABconstant
a=Bwavelength[0]
b=Bwavelength[-1]
h=((b-a)/len(Bwavelength))
ABflux = numpy.trapz(Bwavelength, ABconstant, h)
return ABflux
def ABmagnitude(newflux, ABflux):
ABmagarray=[]
for i in range(len(newflux)):
ABmag = -2.5*log10((newflux[i])/ABflux) - 48.6
ABmagarray.append(ABmag)
return ABmagarray
ABflux1 = ABconversion(Bwavelength, throughput)
print ABflux1
ABmagarray = ABmagnitude(z, ABflux1)
print epoch, ABmagarray
z is defined earlier in the file and is also a list.
However, when I run this I get the message:
Traceback (most recent call last):
File "Rewrite17.11.2014.py", line 196, in <module>
ABflux1 = ABconversion(Bwavelength, throughput)
File "Rewrite17.11.2014.py", line 186, in ABconversion
ABflux = numpy.trapz(Bwavelength, ABconstant, h)
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py, line 3234, in trapz
ret = add.reduce(d * (y[slice1]+y[slice2]/2.0, axis)
ValueError: Operands could not be broadcast together with shapes (0,) (444,)
I don't quite understand the error (I'm fairly new to programming), but I think it means the two "shapes" don't have the same dimensions. I'm not sure why this is.
Thanks in advance.

According to this documentation the parameters to trapz(y, x, dx, axis) are:
y - Array like - input array to integrate.
x - Optional array - If x is None, then spacing between all y elements is dx.
dx - Optional scalar - If x is None, spacing given by dx is assumed. Default is 1.
axis - Optional Int - specify the axis.
So you shouldn't specify both x and dx - one of them should be None.
Perhaps this is what you want: trapz(Bwavelength, None, h).
See this answer for more details on the error message and NumPy's "braodcasting rule".

Replace:
numpy.trapz(Bwavelength, ABconstant, h)
with:
numpy.trapz(np.array(Bwavelength)[:,np.newaxis], ABconstant, h)

Related

TypeErroer: 'NoneType' is not iterable

I'm trying to build an autonomous driving car with the Raspberry Pi - Therefore I try to learn from Udacity's Nanodegree examples.
The following Code is from some GitHub repositories and I just changed the code to work with the PI-CAM. Because the Udacity example Codes work all with .mp4 videos.
When I try to run the following code on the Raspberry PI with the Thonny IDE, sometimes it works for a few seconds or a minute and sometimes it won't even start running.
You can see the whole program here.
def draw_lines(img, lines, thickness=5):
global rightSlope, leftSlope, rightIntercept, leftIntercept
rightColor=[0,0,255]
leftColor=[255,0,0]
#this is used to filter out the outlying lines that can affect the average
#We then use the slope we determined to find the y-intercept of the filtered lines by solving for b in y=mx+b
for line in lines:
for x1,y1,x2,y2 in line:
slope = (y1-y2)/(x1-x2)
if slope > 0.3:
if x1 > 500 :
yintercept = y2 - (slope*x2)
rightSlope.append(slope)
rightIntercept.append(yintercept)
else: None
elif slope < -0.3:
if x1 < 600:
yintercept = y2 - (slope*x2)
leftSlope.append(slope)
leftIntercept.append(yintercept)
...
lines are defined in this part:
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
"""
`img` should be the output of a Canny transform.
"""
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
draw_lines(line_img, lines)
return line_img
def linedetect(img):
return hough_lines(img, 1, np.pi/180, 10, 20, 100)
This is the error I get when I execute the code :
/usr/local/lib/python3.5/dist-packages/numpy/core/fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
out=out, **kwargs)
/usr/local/lib/python3.5/dist-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
ret = ret.dtype.type(ret / rcount)
version1_for_PI.py:160: RuntimeWarning: divide by zero encountered in int_scalars
slope = (y1-y2)/(x1-x2)
Traceback (most recent call last):
File "/home/pi/Desktop/version-1/version1_for_PI.py", line 244, in <module>
myline = hough_lines(canny, 1, np.pi/180, 10, 20, 5)
File "/home/pi/Desktop/version-1/version1_for_PI.py", line 209, in hough_lines
draw_lines(line_img, lines)
File "/home/pi/Desktop/version-1/version1_for_PI.py", line 158, in draw_lines
for line in lines:
TypeError: 'NoneType' object is not iterable
Your "lines" parameter is None - which is not an "iterable" typed object in python (such as lists, sets, etc).
You should either make sure that the "lines" you pass to the method are not None - or add some logic to ignore it:
if not lines: # means that lines == None
return 0 # or return something else
Another good option is to capture an exception and handle it properly.

Python: float() argument must be a string or a number, not 'interp2d'

this code returns the error "float() argument must be a string or a number, not 'interp2d'". I'm attempting to learn how to interpolate values to fill an array given a few of the values in the array (sorry, bad phrasing). Am I messing up the syntax for the interp2d function or what?
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
import scipy as sp
GCM_file = '/Users/Robert/Documents/Python Scripts/GCMfiles/ATM_echc0003_1979_2008.nc'
fh = Dataset(GCM_file, mode = 'r')
pressure = fh.variables['lev'][:]
lats = fh.variables['lat'][:]
temp = np.mean(fh.variables['t'][0,:,:,:,:], axis = (0, 3))
potential_temp = np.zeros((np.size(temp,axis=0), np.size(temp,axis=1)))
P0 = pressure[0]
#plt.figure(0)
for j in range(0, 96):
potential_temp[:,j] = temp[:, j] * (P0/ pressure[:]) ** .238
potential_temp_view = potential_temp.view()
temp_view = temp.view()
combo_t_and_pt = np.dstack((potential_temp_view,temp_view))
combo_view = combo_t_and_pt.view()
pt_and_t_flat=np.reshape(combo_view, (26*96,2))
t_flat = temp.flatten()
pt_flat = potential_temp.flatten()
temp_grid = np.zeros((2496,96))
for j in range(0, 2496):
if j <= 95:
temp_grid[j,j] = t_flat[j]
else:
temp_grid[j, j % 96] = t_flat[j]
'''Now you have the un-interpolated grid of all your values of t as a function of potential temp and latitude, so you have to interpolate the rest somehow....?'''
xlist = lats
ylist = pt_flat
X,Y = np.meshgrid(xlist,ylist)
temp_cubic = sp.interpolate.interp2d(xlist,ylist, temp_grid, kind = 'cubic')
#temp_linear= griddata(temp_grid, (X,Y), method = 'linear')
#temp_quintic = griddata(temp_grid, (X,Y), method = 'cubic')
plt.figure(0)
plt.contourf(X,Y, temp_cubic, 20)
EDIT: The error with this was pointed out to me. I changed the code from the interpolating line down into this, and I'm still getting an error, which reads "ValueError: Invalid input data". Here's the traceback:
runfile('C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py', wdir='C:/Users/Robert/Documents/Python Scripts')
Traceback (most recent call last):
File "<ipython-input-27-1ffd3fcc3aa1>", line 1, in <module>
runfile('C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py', wdir='C:/Users/Robert/Documents/Python Scripts')
File "C:\Users\Robert\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\Robert\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Robert/Documents/Python Scripts/attempt at defining potential temperature.py", line 62, in <module>
Z = temp_cubic(xlist,ylist)
File "C:\Users\Robert\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 292, in __call__
z = fitpack.bisplev(x, y, self.tck, dx, dy)
File "C:\Users\Robert\Anaconda3\lib\site-packages\scipy\interpolate\fitpack.py", line 1048, in bisplev
raise ValueError("Invalid input data")":
temp_cubic = sp.interpolate.interp2d(xlist, ylist, temp_grid, kind = 'cubic')
ylist = np.linspace(np.min(pt_flat), np.max(pt_flat), .01)
X,Y = np.meshgrid(xlist,ylist)
Z = temp_cubic(xlist,ylist)
plt.contourf(X,Y, Z, 20)
The problem is in the following line. interp2d returns an interpolation function. However, you used it in place of the Z argument to countourf, which is supposed to be a float matrix. See the contourf doc for details.
In particular:
contour(X,Y,Z,N)
make a contour plot of an array Z.
X, Y specify the (x, y) coordinates of the surface
X and Y must both be 2-D with the same shape as Z,
or they must both be 1-D such that
len(X) is the number of columns in Z and
len(Y) is the number of rows in Z.
contour up to N automatically-chosen levels.
In short, I believe that you want to apply the function to X and Y to generate the array you pass in as the third argument.
Credit to both the matplotlib documentation and kindall for showing the conceptual error of my other possibilities.

Python - scipy fmin, giving the arguments to fmin

I'm a bit of a newbie in Python.
I'm writing a little piece of code in order to find the minimum of a function:
import os,sys,matplotlib,pylab
import numpy as np
from scipy.optimize import fmin
par = [2., 0.5, 0.008]
x1 = 0.4
f2_2 = lambda x, param: param[0] * x**2 + param[1] * x + param[2]
xmin = fmin(f2_2,x1,args = (par))
print xmin
it should be very simple, however I am getting this error:
"Traceback (most recent call last):
File "prova.fmin.py", line 9, in <module>
xmin = fmin(f2_2,x1,args = (par))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 257, in fmin
fsim[0] = func(x0)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 176, in function_wrapper
return function(x, *args)
TypeError: <lambda>() takes exactly 2 arguments (4 given)"
Could someone help me in understanding this please?
I just tried this out. Looks like you need to say (par,) and not just (par). Note that (par,) is a tuple, with the variable par as a single element, whereas (par) just evaluates to par: no tuple. The "args" keyword of fmin expects to find a tuple, not par, which in this case is a list.
Edit:
Well, actually, it would seem that args doesn't mind receiving a list either. But then, inside of fmin, when the function f2_2 is called, args is unpacked, meaning its contents are now passed as arguments to f2_2. This means that f2_2 ends up getting four arguments, viz. x, 2, 0.5 and 0.008 in this case, as opposed to getting just the two arguments x and [2, 0.5, 0.008].
You need to define the lambda function to accept more arguments, like this:
f2_2 = lambda x, *param: param[0] * x**2 + param[1] * x + param[2]

Plotting currents data on a map : Too many value to unpack error

So here is my code :
from netCDF4 import *
import datetime as dt
import numpy as np
import numpy.ma as ma
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt
nc=Dataset('datasets/essai.nc')
time_var = nc.variables[str('forecast_time0')]
wave_var = nc.variables['DIST_GDS0_SFC']
lat = nc.variables['g0_lat_1'][:]
lon = nc.variables['g0_lon_2'][:]
uin = nc.variables['UOGRD_GDS0_DBSL'][:]
vin = nc.variables['VOGRD_GDS0_DBSL'][:]
plt.quiver(lon[::5], lat[::5], uin[::5], vin[::5], scale=200)
And here is the error I get :
Traceback (most recent call last):
File "nctry.py", line 37, in <module>
plt.quiver(lon[::5], lat[::5], uin[::5], vin[::5], scale=200)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2877, in quive
r
ret = ax.quiver(*args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 6627, in quiver
q = mquiver.Quiver(self, *args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\quiver.py", line 394, in __init
__
X, Y, U, V, C = _parse_args(*args)
File "C:\Python27\lib\site-packages\matplotlib\quiver.py", line 356, in _parse
_args
nr, nc = U.shape
ValueError: too many values to unpack
I think the problem comes from the UOGRD and VOGRD which has many values but don't know how manipulate it?
Here is how uogrd looks like :
the forecast_time up as far as a number of 4.
Ok so I used the np.array and shape functions, here is what i've got :
C:\Python27>python nctry.py
(321,) //lat
(720,) //lon
(4, 321, 720) //uogrd
(4, 321, 720) //vogrd
Then I tried this code :
but it only shows one arrow. Can you tell me why?
So now I have some modelling which looks like this :
But this image doesn't change, for [0, :, :], 1, 2 or 3.. What's wrong?
Furthermore, all the arrows are going to the same direction, not really true when we're talking about currents' ocean. I use the deg2rad function.
Finally, I can't imagine the coastlines, is that normal?
Thank you
The problem is that UOGRD (and probably VOGRD too) contains too many dimensions. You need to reduce the number of dimensions by slicing it. However, given the current information, I cannot instruct you which dimension to remove.
However, you should convert the lat, lon, UOGRD and VOGRD to np.array using the command and print out each array's shape
UOGRD = np.array(UOGRD)
print UOGRD.shape
Do this for all 4 of your arrays. This will print out tuples (d1_length, d2_length, ..., dn_length).
If we know the dimensionality of lat=N and lon=M, we should hope that the dimensions of UOGRD will be something like (N, M, x, y, z) and we can slice out the dimensions we don't need.
EDIT:
From the shape functions in your, it shows us that uin and vin are both 3 dimensional. And you can see that len(lat) = 321, len(lon) = 720 and both uin and vin have dimensions (x, 321, 720). That means we want to plot the last two dimensions of uin and vin. Therefore, try this
uin = nc.variables['UOGRD_GDSO_DBSL']
uin = np.array(uin)
uin = uin[0, :, :]
vin = nc.variables['vOGRD_GDSO_DBSL']
vin = np.array(vin)
vin = vin[0, :, :]
Do not do uin = uin.shape as that will set uin = (4, 321, 720) instead of your data. The only thing is, we have to figure out is which index we should put in the slicing. Try [0,:,:] then [1,:,:] ... [3,:,:].

root minuit2 contours with parameter limits

I'm trying to produce contour plots for parameters with physical limits using the Minuit2 minimizer which is a part of the ROOT data analysis framework. Unfortunately, Minuit2 seems intent on drifting the parameters into regions outside of their limits when I try to produce contour plots:
>>> from minuit2 import Minuit2
>>> def f(x,y):
... if x < 0 or y < 0:
... print 'x = %.2f, y = %.2f' % (x,y)
... raise Exception
... return x**2 + y**2
...
>>> m = Minuit2(f)
>>> m.limits['x'] = 0, 10
>>> m.limits['y'] = 0, 10
>>> m.migrad()
>>> xy = m.contour('x','y',3)
Info in <Minuit2>: MnMinos UP value has changed, need to update FunctionMinimum class
x = -9.95, y = 0.00
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in f
Exception
Has anybody else dealt with this or a similar problem? Are there any workarounds?
I've already asked this question on the ROOT forums, but I thought there might also be some stack overflow users who have dealt with this or a similar issue.
Try your example without raising an exception
def f(x,y):
return x ** 2 + y ** 2
and you will get reasonable xy contour points (i.e. within 1e-3 of the true contour).
Note that the parameter sigmas=3 in your contour call m.contour('x', 'y', 3) means that the contour for sigmas ** 2 == 9 will be computed and that contour points along the parameter limits are computed. As far as I can see this is not mentioned in the contour() pyminuit documentation).
In your example the contour starts at (0, 0), goes up to (3, 0), along the circle to (0, 3), and back to (0, 0).
A common method is to implement parameter limits (arbitrary shapes, not only min / max) in your cost function by returning very high values for excluded parameters:
def f(x,y):
if x < 0 or y < 0:
return 1e10
return x ** 2 + y ** 2
This does throw the optimizer out of the forbidden regions, but it does not prevent it to probe them sometimes (i.e. evaluate f there).
I don't know why contour() should strictly respect the limits you set via
m.limits['x'] = 0, 10
m.limits['y'] = 0, 10
Here's a short description of the contour algorithm used by Minuit (and Minuit2) and here is the documentation for the Minuit2 code in ROOT, I did not manage to find the actual C file showing the implementation.

Categories

Resources