I am asking because I encounter something very strange. The following method throws an exception when called from within a script:
def gaussian_blur(self, in_array, size):
# expand in_array to fit edge of kernel
padded_array = np.pad(in_array, (10,), 'reflect')
# build kernel
x, y = np.mgrid[-size:size + 1, -size:size + 1]
g = np.exp(-(x**2 / float(size) + y**2 / float(size)))
g = (g / g.sum()).astype(in_array.dtype)
# do the Gaussian blur
return fftconvolve(padded_array, g, mode='valid')
Traceback (most recent call last):
File "raster_tools/smooth.py", line 130, in <module>
blurred = ri.gaussian_blur(my_array, 10)
File "raster_tools/smooth.py", line 80, in gaussian_blur
padded_array = np.pad(in_array, (10,), 'reflect')
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/arraypad.py", line 1358, in pad
kwargs)
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 91, in apply_along_axis
res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
TypeError: 'unicode' object is not callable
When I set a breakpoint using ipdb and then call
padded_array = np.pad(in_array, (10,), 'reflect')
(and all other lines of codes in the function as well by the way) the error won't occur. As the python script is being called within an virtual environment I could imagine that ipdb is using a different interpreter. Even though running the code outside the virtual env gives the same behavior.
Related
I am tackling a bayesian inference problem and am having trouble using a pymc3 sampler provided by pypesto on my windows laptop. To make sure I can run with the sampler I create a simple dummy objective to use.
I install create a conda (I tried both 3.7 & 3.8) environment and install the pymc3 and theano modules using pip3/pip. I've tried several different versions of both pymc3/theano and managed to import them succesfully. However, there is an error message I cannot figure out how to go around. I have tried looking online for a solution but was not able to find it either. I currently have the latest versions of pymc3 and theano installed (3.11.0 and 1.0.5 respectively). This is the final line of the message
theano.graph.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute sigmoid(x2_interval__), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.
Here is the full message:
Sampling 1 chain for 1_000 tune and 100 draw iterations (1_000 + 100 draws total) took 7 seconds.
Traceback (most recent call last):
File "samplingPymc3.py", line 70, in <module>
result2 = sample.sample(problem1, 100, sampler2, x0=np.array([0,0]))
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\pypesto\sample\sample.py", line 68, in sample
sampler.sample(n_samples=n_samples)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\pypesto\sample\pymc3.py", line 102, in sample
**self.options)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\pymc3\sampling.py", line 637, in sample
idata = arviz.from_pymc3(trace, **ikwargs)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\arviz\data\io_pymc3.py", line 559, in from_pymc3
density_dist_obs=density_dist_obs,
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\arviz\data\io_pymc3.py", line 163, in __init__
self.observations, self.multi_observations = self.find_observations()
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\arviz\data\io_pymc3.py", line 176, in find_observations
multi_observations[key] = val.eval() if hasattr(val, "eval") else val
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\graph\basic.py", line 554, in eval
self._fn_cache[inputs] = theano.function(inputs, self)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\compile\function\__init__.py", line 350, in function
output_keys=output_keys,
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\compile\function\pfunc.py", line 532, in pfunc
output_keys=output_keys,
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\compile\function\types.py", line 1978, in orig_function
name=name,
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\compile\function\types.py", line 1584, in __init__
fgraph, additional_outputs = std_fgraph(inputs, outputs, accept_inplace)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\compile\function\types.py", line 188, in std_fgraph
fgraph = FunctionGraph(orig_inputs, orig_outputs, update_mapping=update_mapping)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\graph\fg.py", line 162, in __init__
self.import_var(output, reason="init")
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\graph\fg.py", line 330, in import_var
self.import_node(var.owner, reason=reason)
File "C:\Users\germa\anaconda3\envs\sampling\lib\site-packages\theano\graph\fg.py", line 383, in import_node
raise MissingInputError(error_msg, variable=var)
theano.graph.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute sigmoid(x2_interval__), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.
I read somewhere that the issue may lie with the version of arviz used but that does not appear to be the issue in my case.
I wanted to include the script I am running. Here is the code for the script:
import numpy as np
import scipy as sp
import scipy.optimize as so
from scipy.stats import multivariate_normal
import pypesto
import pypesto.sample as sample
from pypesto import Objective
A = np.array([[2.0, 0.0], [0.0, 1.0]])
b = np.array([2.0, 1.0])
x_init = np.array([3.4302, 2.915])
x_true = np.array([1.0, 1.0])
temp = lambda x: A.dot(x) - b
f = lambda x: .5 * np.linalg.norm(temp(x))
A_t = A.transpose()
K = np.dot(A_t, A)
df = lambda x: K.dot(x) - A_t.dot(b)
def obj1(x):
# f_val = f(x)
# grad = df(x)
return (f(x), df(x))
objfun = lambda x: obj1(x)
dim_full = 2
lb = -10 * np.ones((dim_full, 1))
ub = 10 * np.ones((dim_full, 1))
x_names = ['x1', 'x2']
# step_fcn = pymc3.step_methods.hmc.hmc.HamiltonianMC
objective = pypesto.Objective(fun=objfun, grad=True, hess=False)
problem1 = pypesto.Problem(objective=objective, lb=lb, ub=ub, x_names=x_names)
sampler = sample.AdaptiveMetropolisSampler()
print('function val: ', objfun(x_init))
sampler2 = sample.Pymc3Sampler()
result2 = sample.sample(problem1, 100, sampler2, x0=np.array([0, 0]))
print('Done sampling!')
Thank you in advance for any help!
pymc3 support of pypesto is at the moment limited, as it was implemented at a time when theano was discontinued in favor of aesara in pymc3. Thus, pypesto only supports specific version of the involved tools, specifically
arviz >= 0.8.1, < 0.9.0
theano >= 1.0.4
packaging >= 20.0
pymc3 >= 3.8, < 3.9.2
(see https://github.com/ICB-DCM/pyPESTO/blob/main/setup.cfg#L111).
The switch to full aesara and later pymc3 version support is underway, but not out yet.
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.
I installed Fenics using Conda on Ubuntu 18.04, and receive the following error while running their ft06_elasticity.py example.
I have tried to find a solution or workaround to this in the documentation, but I cannot even find a nabla_div() function description anywhere.
The Fenics documentation states the following:
nabla_grad
The gradient and divergence operators now have a prefix nabla_. This
is strictly not necessary in the present problem, but recommended in
general for vector PDEs arising from continuum mechanics, if you
interpret ∇ as a vector in the PDE notation; see the box about
nabla_grad in the section Variational formulation.
"""
FEniCS tutorial demo program: Linear elastic problem.
-div(sigma(u)) = f
The model is used to simulate an elastic beam clamped at
its left end and deformed under its own weight.
"""
from __future__ import print_function
from fenics import *
# Scaled variables
L = 1; W = 0.2
mu = 1
rho = 1
delta = W/L
gamma = 0.4*delta**2
beta = 1.25
lambda_ = beta
g = gamma
# Create mesh and define function space
mesh = BoxMesh(Point(0, 0, 0), Point(L, W, W), 10, 3, 3)
V = VectorFunctionSpace(mesh, 'P', 1)
# Define boundary condition
tol = 1E-14
def clamped_boundary(x, on_boundary):
return on_boundary and x[0] < tol
bc = DirichletBC(V, Constant((0, 0, 0)), clamped_boundary)
# Define strain and stress
def epsilon(u):
return 0.5*(nabla_grad(u) + nabla_grad(u).T)
#return sym(nabla_grad(u))
def sigma(u):
return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)
# Define variational problem
u = TrialFunction(V)
d = u.geometric_dimension() # space dimension
v = TestFunction(V)
f = Constant((0, 0, -rho*g))
T = Constant((0, 0, 0))
a = inner(sigma(u), epsilon(v))*dx
L = dot(f, v)*dx + dot(T, v)*ds
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Plot solution
plot(u, title='Displacement', mode='displacement')
# Plot stress
s = sigma(u) - (1./3)*tr(sigma(u))*Identity(d) # deviatoric stress
von_Mises = sqrt(3./2*inner(s, s))
V = FunctionSpace(mesh, 'P', 1)
von_Mises = project(von_Mises, V)
plot(von_Mises, title='Stress intensity')
# Compute magnitude of displacement
u_magnitude = sqrt(dot(u, u))
u_magnitude = project(u_magnitude, V)
plot(u_magnitude, 'Displacement magnitude')
print('min/max u:',
u_magnitude.vector().array().min(),
u_magnitude.vector().array().max())
# Save solution to file in VTK format
File('elasticity/displacement.pvd') << u
File('elasticity/von_mises.pvd') << von_Mises
File('elasticity/magnitude.pvd') << u_magnitude
# Hold plot
interactive()
Traceback (most recent call last):
File "fenics_ft06_elasticity.py", line 48, in <module>
a = inner(sigma(u), epsilon(v))*dx
File "fenics_ft06_elasticity.py", line 40, in sigma
return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u)
NameError: name 'nabla_div' is not defined
I found that replacing 'nabla_div(u)' with just 'div(u)' solved that error. However, it did lead straight to the next error:
Traceback (most recent call last):
File "fenics_ft06_elasticity.py", line 56, in <module>
plot(u, title='Displacement', mode='displacement')
File "/home/ron/miniconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/common/plotting.py", line 438, in plot
return _plot_matplotlib(object, mesh, kwargs)
File "/home/ron/miniconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/common/plotting.py", line 282, in _plot_matplotlib
ax.set_aspect('equal')
File "/home/ron/miniconda3/envs/fenicsproject/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 1281, in set_aspect
'It is not currently possible to manually set the aspect '
NotImplementedError: It is not currently possible to manually set the aspect on 3D axes
Simply add these two lines to the beginning of your code to use the nabla_grad and nabla_div:
from ufl import nabla_grad
from ufl import nabla_div
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!
I am interesting in solving differential equation using fipy.
The following code is working correctly when I am using Grid2D.
from fipy import *
mesh = Grid2D(nx=3, ny=3)
#mesh = Grid3D(nx=3, ny=3, nz=3)
phi = CellVariable(name='solution variable', mesh=mesh, value=0.)
phi.constrain(0, mesh.facesLeft)
phi.constrain(10, mesh.facesRight)
coeff = CellVariable(mesh=mesh, value=1.)
eq = DiffusionTerm(coeff) == 0
eq.solve(var=phi)
When I am using Grid3D instead of Grid2D (commented line), I get following error:
Traceback (most recent call last):
File "/home/user/Programming/python/fdms/forSo.py", line 11, in <module>
eq.solve(var=phi)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/terms/term.py", line 211, in solve
solver = self._prepareLinearSystem(var, solver, boundaryConditions, dt)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/terms/term.py", line 169, in _prepareLinearSystem
diffusionGeomCoeff=self._getDiffusionGeomCoeff(var),
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/terms/abstractDiffusionTerm.py", line 458, in _getDiffusionGeomCoeff
return self._getGeomCoeff(var)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/terms/term.py", line 465, in _getGeomCoeff
self.geomCoeff = self._calcGeomCoeff(var)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/terms/abstractDiffusionTerm.py", line 177, in _calcGeomCoeff
tmpBop = (coeff * FaceVariable(mesh=mesh, value=mesh._faceAreas) / mesh._cellDistances)[numerix.newaxis, :]
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/variables/variable.py", line 1151, in __mul__
return self._BinaryOperatorVariable(lambda a,b: a*b, other)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/variables/variable.py", line 1116, in _BinaryOperatorVariable
if not v.unit.isDimensionless() or len(v.shape) > 3:
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/variables/variable.py", line 255, in _getUnit
return self._extractUnit(self.value)
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/variables/variable.py", line 538, in _getValue
value = self._calcValue()
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/variables/cellToFaceVariable.py", line 48, in _calcValue
alpha = self.mesh._faceToCellDistanceRatio
File "/home/user/Programs/miniconda2/envs/FipyEnv2/lib/python3.6/site-packages/fipy/meshes/uniformGrid3D.py", line 269, in _faceToCellDistanceRatio
XZdis[..., 0,...] = 1
IndexError: an index can only have a single ellipsis ('...')
I installed fipy using «Recomended method» from https://www.ctcms.nist.gov/fipy/INSTALLATION.html. I tried to install using Miniconda for both Pthon 3.6 and Python 2.7 and got same errors.
How to solve equations using Grid3D?
This is because newer versions of numpy are less tolerant of our sloppy syntax. You can either checkout our develop source branch or make this change to your code.