Numba doesn't work for numpy.random function - python

I am trying to implement Numba on a simple Monte Carlo integrator.
import numpy as np
import numba
import time
#numba.njit
def integrator(integrand,
lower_bounds,
upper_bounds,
n):
if len(lower_bounds) != len(upper_bounds):
raise ValueError("Lower and upper bounds are of different dimensions.")
x = np.random.uniform(lower_bounds,
upper_bounds,
(n, len(lower_bounds)))
summation = 0
for i in x:
summation += integrand(i)
domain = np.prod(np.subtract(upper_bounds, lower_bounds))
integral = domain/n*summation
return integral
start = time.time()
integrator(numba.njit(lambda x: x**2),
np.array([-5]),
np.array([5]),
10000000)
end = time.time()
print(end-start)
However, I am getting the following error:
>>> integrator(numba.njit(lambda x: x**2),
... np.array([-5]),
... np.array([5]),
... 10000000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\core\dispatcher.py", line 468, in _compile_for_args
error_rewrite(e, 'typing')
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\core\dispatcher.py", line 409, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
←[1m←[1m←[1mNo implementation of function Function(<built-in method uniform of numpy.random.mtrand.RandomState object at 0x0000019A9E477040>) found for signature:
>>> uniform(array(int32, 1d, C), array(int32, 1d, C), UniTuple(int64 x 2))
There are 4 candidate implementations:
←[1m - Of which 4 did not match due to:
Overload in function '_OverloadWrapper._build.<locals>.ol_generated': File: numba\core\overload_glue.py: Line 129.
With argument(s): '(array(int32, 1d, C), array(int32, 1d, C), UniTuple(int64 x 2))':←[0m
←[1m Rejected as the implementation raised a specific error:
TypingError: ←[1mNo match←[0m←[0m
raised from C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\core\overload_glue.py:162
←[0m
←[0m←[1mDuring: resolving callee type: Function(<built-in method uniform of numpy.random.mtrand.RandomState object at 0x0000019A9E477040>)←[0m
←[0m←[1mDuring: typing of call at <stdin> (21)
←[0m
←[1m
File "<stdin>", line 21:←[0m
←[1m<source missing, REPL/exec in use?>←[0m
It seems that there is some issue with passing dimensions as a tuple argument to np.random.uniform(). However, I know that np.random.uniform() is supported by Numba and using tuples there is necessary. What is the problem here?

Related

How to use Lambda functions in SciPy Orthogonal Distance Regression (ODR)?

I am trying to fit different curves to the same data using SciPy's ODR. To make this easier, I have defined each candidate function as a Lambda function. This Lambda function then gets passed on to ODR through a function of my own, called odr_fit():
if trend_name == 'linear':
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
elif trend_name == 'quadratic':
eps = odr_fit(f=lambda x, p: p[0]*x**2+p[1]*x+p[2], xdata=x, ydata=y)
# and so on....
The odr_fit() function is defined as follows:
def odr_fit(f, xdata, ydata):
"""
Function to calculate orthogonal residuals given data and a function to fit
:param f: function in the format f(x, p), where p is a list of parameters
:param xdata: Pandas Series with independent variable
:param ydata: Pandas Series of same length with dependent variable
:return: eps: estimated orthogonal errors, same length as xdata and ydata
"""
f = np.vectorize(f, excluded=['p'])
model = Model(f)
data = RealData(xdata, ydata)
odr = ODR(data, model, beta0=[10]*3) # beta0 is an initial estimate. Should be the same length as p ideally as well
odr_result = odr.run()
return odr_result.eps
My problem: somewhere in this code, p (the parameters, the second argument of my Lambda function) is expected to be a scalar. I want it to be seen as a list or tuple.
IndexError: invalid index to scalar variable.
ODR also expects a list/tuple (see documentation here). Of course, I could just define each function separately, but this would be cumbersome and harder to expand. Is there a way of making ODR work using Lambda functions? Perhaps something that can force p to be a vector of a certain length.
Hope someone knows a solution to this!
Thanks,
Alex
EDIT: Here is the error in full:
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
Traceback (most recent call last):
File "C:\Users\...\features.py", line 296, in add_features
data = _add_trend_features(data, col_trends={'x':'TemperatureOutsideAvgNight', 'y':'TemperatureInsideAvgNight'})
File "C:\Users\...\trend_features.py", line 109, in _add_trend_features
return_orth_dist=True)
File "C:\Users\...\trend_features.py", line 88, in _add_trend_features
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
File "C:\Users\...\Documents\phil_anomaly_detection\phil_anomaly_detection\trend_features.py", line 23, in odr_fit
odr = ODR(data, model, beta0=[10]*3)
File "C:\Users\...\miniconda3\lib\site-packages\scipy\odr\odrpack.py", line 770, in __init__
self._check()
File "C:\Users\...\miniconda3\lib\site-packages\scipy\odr\odrpack.py", line 831, in _check
res = self.model.fcn(*arglist)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2091, in __call__
return self._vectorize_call(func=func, args=vargs)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2161, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2121, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2086, in func
return self.pyfunc(*the_args, **kwargs)
File "C:\Users\...\trend_features.py", line 88, in <lambda>
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
IndexError: invalid index to scalar variable.
Process finished with exit code 1

How to make discrete Fourier transform (FFT) in numba.njit?

Hello fellow programmers
I am trying to make a discrete Fourier transform in this minimal working example with the numba.njit decorator:
import numba
import numpy as np
import scipy
import scipy.fftpack
#numba.njit
def main():
wave = [[[0.09254795, 0.10001078, 0.10744892, 0.07755555, 0.08506225, 0.09254795],
[0.09907245, 0.10706145, 0.11502401, 0.08302302, 0.09105898, 0.09907245],
[0.09565098, 0.10336405, 0.11105158, 0.08015589, 0.08791429, 0.09565098],
[0.00181467, 0.001961, 0.00210684, 0.0015207, 0.00166789, 0.00181467]],
[[-0.45816267, - 0.46058367, - 0.46289091, - 0.45298182, - 0.45562851, -0.45816267],
[-0.49046506, - 0.49305676, - 0.49552669, - 0.48491893, - 0.48775223, -0.49046506],
[-0.47352483, - 0.47602701, - 0.47841162, - 0.46817027, - 0.4709057, -0.47352483],
[-0.00898358, - 0.00903105, - 0.00907629, - 0.008882, - 0.00893389, -0.00898358]],
[[0.36561472, 0.36057289, 0.355442, 0.37542627, 0.37056626, 0.36561472],
[0.39139261, 0.38599531, 0.38050268, 0.40189591, 0.39669325, 0.39139261],
[0.37787385, 0.37266296, 0.36736003, 0.38801438, 0.38299141, 0.37787385],
[0.00716892, 0.00707006, 0.00696945, 0.0073613, 0.00726601, 0.00716892]]]
new_fft = scipy.fftpack.fft(wave)
if __name__ == '__main__':
main()
Output:
C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
Traceback (most recent call last):
File "C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py", line 25, in <module>
main()
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'fft' of type Module(<module 'scipy.fftpack' from 'C:\\Users\\Artur\\Anaconda\\lib\\site-packages\\scipy\\fftpack\\__init__.py'>)
File "test2.py", line 21:
def main():
<source elided>
new_fft = scipy.fftpack.fft(wave)
^
[1] During: typing of get attribute at C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py (21)
File "test2.py", line 21:
def main():
<source elided>
new_fft = scipy.fftpack.fft(wave)
^
Process finished with exit code 1
Unfortunately scipy.fftpack.fft seems to be a legacy function that is not supported by numba. So I searched for alternatives. I found two:
1.
scipy.fft(wave) which is the updated version of the above mentioned legacy function. It produces this error output:
C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
Traceback (most recent call last):
File "C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py", line 25, in <module>
main()
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Module(<module 'scipy.fft' from 'C:\\Users\\Artur\\Anaconda\\lib\\site-packages\\scipy\\fft\\__init__.py'>) with parameters (list(list(list(float64))))
No type info available for Module(<module 'scipy.fft' from 'C:\\Users\\Artur\\Anaconda\\lib\\site-packages\\scipy\\fft\\__init__.py'>) as a callable.
[1] During: resolving callee type: Module(<module 'scipy.fft' from 'C:\\Users\\Artur\\Anaconda\\lib\\site-packages\\scipy\\fft\\__init__.py'>)
[2] During: typing of call at C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py (21)
File "test2.py", line 21:
def main():
<source elided>
new_fft = scipy.fft(wave)
^
Process finished with exit code 1
2.
np.fft.fft(wave) which seems to be supported but also produces an error:
C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py
Traceback (most recent call last):
File "C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py", line 25, in <module>
main()
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
error_rewrite(e, 'typing')
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
reraise(type(e), e, None)
File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'fft' of type Module(<module 'numpy.fft' from 'C:\\Users\\Artur\\Anaconda\\lib\\site-packages\\numpy\\fft\\__init__.py'>)
File "test2.py", line 21:
def main():
<source elided>
new_fft = np.fft.fft(wave)
^
[1] During: typing of get attribute at C:/Users/Artur/Desktop/RL_framework/help_functions/test2.py (21)
File "test2.py", line 21:
def main():
<source elided>
new_fft = np.fft.fft(wave)
^
Process finished with exit code 1
Do you know a fft function, that works with the numba.njit decorator?
If you are happy with a 1D DFT you might as well use an FFT.
Here, is reported a Numba-friendly implementation fft_1d() working on arbitrary input sizes:
import cmath
import numpy as np
import numba as nb
#nb.jit
def ilog2(n):
result = -1
if n < 0:
n = -n
while n > 0:
n >>= 1
result += 1
return result
#nb.njit(fastmath=True)
def reverse_bits(val, width):
result = 0
for _ in range(width):
result = (result << 1) | (val & 1)
val >>= 1
return result
#nb.njit(fastmath=True)
def fft_1d_radix2_rbi(arr, direct=True):
arr = np.asarray(arr, dtype=np.complex128)
n = len(arr)
levels = ilog2(n)
e_arr = np.empty_like(arr)
coeff = (-2j if direct else 2j) * cmath.pi / n
for i in range(n):
e_arr[i] = cmath.exp(coeff * i)
result = np.empty_like(arr)
for i in range(n):
result[i] = arr[reverse_bits(i, levels)]
# Radix-2 decimation-in-time FFT
size = 2
while size <= n:
half_size = size // 2
step = n // size
for i in range(0, n, size):
k = 0
for j in range(i, i + half_size):
temp = result[j + half_size] * e_arr[k]
result[j + half_size] = result[j] - temp
result[j] += temp
k += step
size *= 2
return result
#nb.njit(fastmath=True)
def fft_1d_arb(arr, fft_1d_r2=fft_1d_radix2_rbi):
"""1D FFT for arbitrary inputs using chirp z-transform"""
arr = np.asarray(arr, dtype=np.complex128)
n = len(arr)
m = 1 << (ilog2(n) + 2)
e_arr = np.empty(n, dtype=np.complex128)
for i in range(n):
e_arr[i] = cmath.exp(-1j * cmath.pi * (i * i) / n)
result = np.zeros(m, dtype=np.complex128)
result[:n] = arr * e_arr
coeff = np.zeros_like(result)
coeff[:n] = e_arr.conjugate()
coeff[-n + 1:] = e_arr[:0:-1].conjugate()
return fft_convolve(result, coeff, fft_1d_r2)[:n] * e_arr / m
#nb.njit(fastmath=True)
def fft_convolve(a_arr, b_arr, fft_1d_r2=fft_1d_radix2_rbi):
return fft_1d_r2(fft_1d_r2(a_arr) * fft_1d_r2(b_arr), False)
#nb.njit(fastmath=True)
def fft_1d(arr):
n = len(arr)
if not n & (n - 1):
return fft_1d_radix2_rbi(arr)
else:
return fft_1d_arb(arr)
Compared to the naïve DFT algorithm (dft_1d() which is fundamentally the same as this), you are gaining orders of magnitude, while you are still a typically a lot slower than np.fft.fft().
The relative speed varies greatly depending on the input sizes.
For power-of-2 inputs, this is typically within one order of magnitude of np.fft.fft().
For non-power-of-2, this is typically within two orders of magnitude of np.fft.fft().
For worst-case (prime numbers or so, here is power-of-2 + 1), this is a times as fast as np.fft.fft().
The non-linear behavior of the FFT timings are the result of the need for a more complex algorithm for arbitrary input sizes that are not power-of-2. This affects both this implementation and the one from np.fft.fft(), but np.fft.fft() contains a lot more optimizations which make it perform much better on average.
Alternate implementations of power-of-2 FFT are shown here.
The numba documentation mentioned that np.fft.fft is not support. A solution is to use the objmode context to call python functions that are not supported yet. Only the part inside the objmode context will run in object mode, and therefore can be slow. For you particular case, this part will not be that slow because np.fft.fft is already very fast as pointed by #tstanisl as the first comment of the question. Here is as example
from numba import njit
import numpy as np
#njit()
def compute_fft(x):
y = np.zeros(., dtype=np.complex128)
with objmode(y='type[:]'):
y = np.fft.fft(x)
return y
#njit()
def main():
...
x = np.random.randint(100)
fft_x = compute_fft(x)
...
I was able to find a workaround. Now, keep in mind that functions like numpy.fft.fft have lots of convenience operations, so if you're not stuck like me, you should use them.
Following njit function does a discrete fourier transform on a one dimensional array:
import numba
import numpy as np
import cmath
def dft(wave=None):
dft = np.fft.fft(wave)
return dft
#numba.njit
def dft_njit(wave=None):
N = len(wave)
dft_njit = np.zeros(N, dtype=np.complex128)
for i in range(N):
series_element = 0
for n in range(N):
series_element += wave[n] * cmath.exp(-2j * cmath.pi * i * n * (1 / N))
dft_njit[i] = series_element
return dft_njit
if __name__ == '__main__':
wave = [1,2,3,4,5]
wave = np.array(wave)
print(f' dft: \n{dft(wave=wave)}')
print(f' dft_njit: \n{dft_njit(wave=wave)}')
Output:
dft:
[15. +0.j -2.5+3.4409548j -2.5+0.81229924j -2.5-0.81229924j
-2.5-3.4409548j ]
dft_njit:
[15. +0.j -2.5+3.4409548j -2.5+0.81229924j -2.5-0.81229924j
-2.5-3.4409548j ]

Autograd breaks np.empty_like

I'm trying to take the gradient of a function in which I assign numpy array elements individually (assigning local forces to a global force vector in an FEA), but this appears to break Autograd -- if I use np.zeros for the global array I get ValueError: setting an array element with a sequence, while if I use np.empty I get NotImplementedError: VJP of empty_like wrt argnums (0,) not defined.
Example:
import autograd.numpy as np
from autograd import jacobian, grad
def test(input):
a = np.empty_like(input)
a[:] = input[:]
grad(test)(np.array([0.]))
Gives the error:
C:\Miniconda3\python.exe C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py
Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 31, in __init__
vjpmaker = primitive_vjps[fun]
KeyError: <function primitive.<locals>.f_wrapped at 0x000001AB1D0AA8C8>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 8, in <module>
grad(test)(np.array([0.]))
File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File "C:\Miniconda3\lib\site-packages\autograd\differential_operators.py", line 25, in grad
vjp, ans = _make_vjp(fun, x)
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 10, in make_vjp
end_value, end_node = trace(start_node, fun, x)
File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 10, in trace
end_box = fun(start_box)
File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
return fun(*subargs, **kwargs)
File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 5, in test
a = np.empty_like(input)
File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 45, in f_wrapped
node = node_constructor(ans, f_wrapped, argvals, kwargs, argnums, parents)
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 35, in __init__
.format(fun_name, parent_argnums))
NotImplementedError: VJP of empty_like wrt argnums (0,) not defined
Is there any way to use Autograd on a numpy array which is assembled element-wise?
Based on the tutorial https://github.com/HIPS/autograd/blob/master/docs/tutorial.md, it looks like array assignment is unfortunately not supported in autograd functions.

TypeError: 'float' object is not subscriptable during scipy minimize

I'm trying to estimate a maximum likelihood model in python. I set up both the likelihood function and the analytic jacobian. When I run scipy minimize, I get a bizarre error (displayed below). This error doesn't seem to occur when I omit the jacobian, but I can't figure out why.
from numpy import log,sum,var
from numba import njit
#njit
def log_likelihood(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
log_lik = -((surg_fx)**2 / (2*exp_var)) - .5*log(exp_var)
neg_sum_log_lik = -sum(log_lik)
print(mu_var)
print(neg_sum_log_lik)
if np.isnan(neg_sum_log_lik):
return 1e20
else:
return neg_sum_log_lik
#njit
def log_lik_jac(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
jc = -sum(((surg_fx)**2 / (2*(exp_var**2))) - (.5/exp_var))
print(mu_var)
print(jc)
return jc
x0 = [np.var(cost_params3)]
shrinkage_est = minimize(log_likelihood,x0,args=(cost_params3,cost_SEs3),jac=log_lik_jac,options={'disp':True},method='BFGS')
cost_params3 and cost_SEs3 are (205,)-shaped numpy arrays.
And the return is:
0.10423462356390442
-580.1534424527905
0.10423462356390442
-67.02947836460727
[ 1.11423462]
26.84532144252225
[ 1.11423462]
77.95606471086792
[ 0.3741784]
-54.28224588483895
[ 0.3741784]
150.90730570822998
[ 0.19152581]
-79.19268133113846
[ 0.19152581]
68.81484893304786
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
TypeError: 'float' object is not subscriptable
I'm not really sure why this runs for a few iterations and just fails, especially given that nothing is being subscripted here. I'm also not sure why it seems to become a list after the first iteration? I tried running it without numba but it stopped at the same place with a different error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
IndexError: invalid index to scalar variable.
Any help would be much appreciated!

Scipy Minimize uses a NoneType

I'm trying to code a multiple linear regression. Here's the line of code where my program raises an error:
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
xmat is a list of vectors: [[3,5,2],[3,1,6],[7,2,3], [9,-2,0]]. ylist is a list of the same length as xmat: [5,2,7,7]. coeff is the coefficient list, initially [mean(ylist), 0, 0, 0] ([constant, b_0, b_1, b_2]). resid is the list of residuals for each point, and residsq is the N2 norm of the residuals (sqrt of sum of squares).
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import linregtest
File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
out = linreg.multilinreg(xmat, ylist, True)
File "C:\Python33\lib\site-packages\linreg.py", line 120, in multilinreg
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 302, in minimize
RuntimeWarning)
File "C:\Python33\lib\idlelib\PyShell.py", line 60, in idle_showwarning
file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'
Where does file come from, and how do I suppress this error?
EDIT: Solve one problem, find another. Maybe you can help me determine where SciPy is calling a float?
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import linregtest
File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
out = linreg.multilinreg(xmat, ylist, True)
File "C:\Python33\lib\site-packages\linreg.py", line 123, in multilinreg
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sumresid(xmat, ylist, coeff)}), method = 'SLSQP') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 364, in minimize
constraints, **options)
File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in _minimize_slsqp
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in <listcomp>
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
TypeError: 'float' object is not callable
I just edited my python 3.2 IDLE, PyShell.py (fixing lines 59 and 62)
def idle_showwarning(message, category, filename, lineno,
file=None, line=None):
if file is None:
file = sys.stderr #warning_stream
try:
file.write(warnings.formatwarning(message, category, filename,
lineno, line=line))
use sys.stderr instead of the global warning_stream which uses sys.__stderr__. sys.__stderr__ is None in my case. I don't know why a global is used.
the call to warnings.formatwarning had an extra invalid file keyword.
Now, I get the warning printed, for example
>>> import numpy as np
>>> np.uint(1) - np.uint(2)
Warning (from warnings module):
File "C:\Programs\Python32\Lib\idlelib\idle.pyw", line 1
try:
RuntimeWarning: overflow encountered in ulong_scalars
>>> 4294967295
>>>
edit:
searching for python bug reports
http://bugs.python.org/issue12438 wrong file argument has been fixed
http://bugs.python.org/issue13582 problems with sys.__stderr__ is None is open

Categories