PyMC3 Minibatch ADVI - python

I’m trying to use PyMC3 Minibatch ADVI for Bayesian Regression. The pm.fit function throws the following error and I’m not sure how to fix it.
It says that the ‘str’ object has no attribute ‘type’. What is any ‘str’ object from the error message here? I’ve mapped float tensors for more_replacements to the best of what I know.
advi = pm.ADVI()
tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
map_tensor_batch = {'x_tensor': pm.Minibatch(X_train, dtype=float),'y_tensor':pm.Minibatch(y_train['target'],dtype=float)}
approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.01), callbacks=[tracker], more_replacements = map_tensor_batch)
Your answers will be appreciated.
Addendum: If I just say
pm.Minibatch(np.array([tuple(y.iloc[i,[0]]) for i in train_index])).type()
(Or)
map_tensor_batch['y_tensor'].type()
I get the following result:
<TensorType(float64, matrix)>
Then why does it throw the attribute error below? Again, what’s my ‘str’ object?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-2824def803dc> in <module>
56 tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
57 map_tensor_batch = {'x_tensor': pm.Minibatch(X_train, dtype=float),'y_tensor': pm.Minibatch(y_train['target'],dtype=float)}
---> 58 approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.01), callbacks=[tracker], more_replacements = map_tensor_batch)
59 fig = plt.figure(figsize=(16, 9))
60 mu_ax = fig.add_subplot(221)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/inference.py in fit(self, n, score, callbacks, progressbar, **kwargs)
142 callbacks = []
143 score = self._maybe_score(score)
--> 144 step_func = self.objective.step_function(score=score, **kwargs)
145 if progressbar:
146 progress = progress_bar(range(n), display=progressbar)
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in step_function(self, obj_n_mc, tf_n_mc, obj_optimizer, test_optimizer, more_obj_params, more_tf_params, more_updates, more_replacements, total_grad_norm_constraint, score, fn_kwargs)
358 more_updates=more_updates,
359 more_replacements=more_replacements,
--> 360 total_grad_norm_constraint=total_grad_norm_constraint,
361 )
362 if score:
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in updates(self, obj_n_mc, tf_n_mc, obj_optimizer, test_optimizer, more_obj_params, more_tf_params, more_updates, more_replacements, total_grad_norm_constraint)
244 more_obj_params=more_obj_params,
245 more_replacements=more_replacements,
--> 246 total_grad_norm_constraint=total_grad_norm_constraint,
247 )
248 resulting_updates.update(more_updates)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in add_obj_updates(self, updates, obj_n_mc, obj_optimizer, more_obj_params, more_replacements, total_grad_norm_constraint)
284 more_replacements = dict()
285 obj_target = self(
--> 286 obj_n_mc, more_obj_params=more_obj_params, more_replacements=more_replacements
287 )
288 grads = pm.updates.get_or_compute_grads(obj_target, self.obj_params + more_obj_params)
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in __call__(self, nmc, **kwargs)
401 m = 1.0
402 a = self.op.apply(self.tf)
--> 403 a = self.approx.set_size_and_deterministic(a, nmc, 0, kwargs.get("more_replacements"))
404 return m * self.op.T(a)
405
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in set_size_and_deterministic(self, node, s, d, more_replacements)
1496 _node = node
1497 optimizations = self.get_optimization_replacements(s, d)
-> 1498 flat2rand = self.make_size_and_deterministic_replacements(s, d, more_replacements)
1499 node = theano.clone(node, optimizations)
1500 node = theano.clone(node, flat2rand)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in make_size_and_deterministic_replacements(self, s, d, more_replacements)
1470 flat2rand = collections.OrderedDict()
1471 for g in self.groups:
-> 1472 flat2rand.update(g.make_size_and_deterministic_replacements(s, d, more_replacements))
1473 flat2rand.update(more_replacements)
1474 return flat2rand
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in make_size_and_deterministic_replacements(self, s, d, more_replacements)
1186 initial = tt.patternbroadcast(initial, self.symbolic_initial.broadcastable)
1187 if more_replacements:
-> 1188 initial = theano.clone(initial, more_replacements)
1189 return {self.symbolic_initial: initial}
1190
/opt/conda/lib/python3.7/site-packages/theano/scan/utils.py in clone(output, replace, strict, share_inputs)
212 )
213 )
--> 214 tmp_replace = [(x, x.type()) for x, y in items]
215 new_replace = [(x, y) for ((_, x), (_, y)) in zip(tmp_replace, items)]
216 _, _outs, _ = rebuild_collect_shared(
/opt/conda/lib/python3.7/site-packages/theano/scan/utils.py in <listcomp>(.0)
212 )
213 )
--> 214 tmp_replace = [(x, x.type()) for x, y in items]
215 new_replace = [(x, y) for ((_, x), (_, y)) in zip(tmp_replace, items)]
216 _, _outs, _ = rebuild_collect_shared(
AttributeError: 'str' object has no attribute 'type'
A workaround seems to be to use a tuple instead of a dictionary. The following doesn’t throw the error as before.
advi = pm.ADVI()
tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
#map_tensor_batch = {'x_tensor': pm.Minibatch(x_tensor.eval()),'y_tensor': pm.Minibatch(y_tensor.eval())}
map_tensor_batch = (pm.Minibatch(x_tensor.eval()),pm.Minibatch(y_tensor.eval()))
approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.00001), callbacks=[tracker], more_replacements = map_tensor_batch)
But there’s another error as below:
TypeError: TensorType does not support iteration. Maybe you are using builtins.sum instead of theano.tensor.sum? (Maybe .max?)
Any fix to this?
This (https://alexioannides.com/2018/11/07/bayesian-regression-in-pymc3-using-mcmc-variational-inference/) is the example I’m trying to follow.

The blog post you are working from shows
import theano
y_tensor = theano.shared(train.y.values.astype('float64'))
x_tensor = theano.shared(train.x.values.astype('float64'))
map_tensor_batch = {y_tensor: pm.Minibatch(train.y.values, 100),
x_tensor: pm.Minibatch(train.x.values, 100)}
That is, map_tensor_batch should be a dict, but the keys are Theano tensors, not mere strings.

Related

Custom scaler transformer fit error only when used in ColumnTransformer pipeline

I have a df with some columns target time series which I need to scale as 1D array and as two thus defining custom wrapper to extend scikit-learn scalers with optional preprocessing as flatten. transform and inverse_transform are not posted for simplicity
import copy
class ScalerOptFlatten( TransformerMixin,
BaseEstimator ):
def __init__( self, instance, flatten , **kwargs):
#super().__init__(**kwargs)
self.scaler = instance
self.flatten = flatten
def get_params(self, deep=True):
cp = copy.copy(self.scaler)
cp.__class__ = type(self.scaler)
params = type(self.scaler).get_params(cp, deep)
return params
def fit( self, X, y = None ):
if self.flatten:
scale_in = X.reshape(-1).reshape(-1, 1)
else:
scale_in = X
if y==None:
self.scaler.fit(scale_in)
else:
self.scaler.fit(scale_in,y)
return self
It works when used on its own
scaler_transformer = ScalerOptFlatten( instance = StandardScaler(),
flatten = True )
scaler_transformer.fit(df_unstack)
But fails when stacked in
preprocessor = ColumnTransformer(
transformers=[
("ts", scaler_transformer, target_feature_names)
]
)
preprocessor.fit(df_unstack)
With
---------------------------------------------------------------------------
Empty Traceback (most recent call last)
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
826 try:
--> 827 tasks = self._ready_batches.get(block=False)
828 except queue.Empty:
~/anaconda3/envs/tf_2_2_0/lib/python3.8/queue.py in get(self, block, timeout)
166 if not self._qsize():
--> 167 raise Empty
168 elif timeout is None:
Empty:
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-494-0eb4b591d8a6> in <module>
----> 1 ts_scaler_step.fit(df_unstack)
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in fit(self, X, y)
492 # we use fit_transform to make sure to set sparse_output_ (for which we
493 # need the transformed data) to have consistent output type in predict
--> 494 self.fit_transform(X, y=y)
495 return self
496
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in fit_transform(self, X, y)
529 self._validate_remainder(X)
530
--> 531 result = self._fit_transform(X, y, _fit_transform_one)
532
533 if not result:
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in _fit_transform(self, X, y, func, fitted)
456 self._iter(fitted=fitted, replace_strings=True))
457 try:
--> 458 return Parallel(n_jobs=self.n_jobs)(
459 delayed(func)(
460 transformer=clone(trans) if not fitted else trans,
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/joblib/parallel.py in __call__(self, iterable)
1046 # remaining jobs.
1047 self._iterating = False
-> 1048 if self.dispatch_one_batch(iterator):
1049 self._iterating = self._original_iterator is not None
1050
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
836 big_batch_size = batch_size * n_jobs
837
--> 838 islice = list(itertools.islice(iterator, big_batch_size))
839 if len(islice) == 0:
840 return False
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/compose/_column_transformer.py in <genexpr>(.0)
458 return Parallel(n_jobs=self.n_jobs)(
459 delayed(func)(
--> 460 transformer=clone(trans) if not fitted else trans,
461 X=_safe_indexing(X, column, axis=1),
462 y=y,
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
70 FutureWarning)
71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 72 return f(**kwargs)
73 return inner_f
74
~/anaconda3/envs/tf_2_2_0/lib/python3.8/site-packages/sklearn/base.py in clone(estimator, safe)
86 for name, param in new_object_params.items():
87 new_object_params[name] = clone(param, safe=False)
---> 88 new_object = klass(**new_object_params)
89 params_set = new_object.get_params(deep=False)
90
TypeError: __init__() missing 2 required positional arguments: 'instance' and 'flatten'
I suspect that error is in get_param method

TypeError: Wrong number of dimensions: expected 0, got 1 with shape (6,)

I am performing a linear regression analysis using bayessian method.
X = data.drop(columns='Power')
Y = data['Power']
a_0=1.
b_0=1.
mu_0=(0.,0.,0.,0.,0.,0.)
row1=[1.,0.,0.,0.,0.,0.]
row2=[0.,1.,0.,0.,0.,0.]
row3=[0.,0.,1.,0.,0.,0.]
row4=[0.,0.,0.,1.,0.,0.]
row5=[0.,0.,0.,0.,1.,0.]
row6=[0.,0.,0.,0.,0.,1.]
Sigma_0 =np.array([row1,row2,row3,row4,row5,row6])
with pm.Model() as model:
sigma_square = 1.0 / pm.Gamma('sigma square', alpha=a_0, beta=b_0)
w = pm.MvNormal("w", mu=mu_0, cov=Sigma_0)
likelihood = pm.Normal('likelihood', mu=np.dot(X,w), cov=sigma_square + np.dot(X.T ,np.dot(Sigma_n, X)), observed=Y)
trace = sample(3000, return_inferencedata=True)
This returns the following error:
TypeError Traceback (most recent call last)
in ()
2 sigma_square = 1.0 / pm.Gamma('sigma square', alpha=a_0, beta=b_0)
3
----> 4 w = pm.MvNormal("w", mu=mu_0, cov=Sigma_0)
5 likelihood = pm.Normal('likelihood', mu=np.dot(X,w), cov=sigma_square + np.dot(X.T ,np.dot(Sigma_n, X)), observed=Y)
6 trace = sample(3000, return_inferencedata=True)
/usr/local/lib/python3.7/dist-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
120 else:
121 dist = cls.dist(*args, **kwargs)
--> 122 return model.Var(name, dist, data, total_size, dims=dims)
123
124 def getnewargs(self):
/usr/local/lib/python3.7/dist-packages/pymc3/model.py in Var(self, name, dist, data, total_size, dims)
1136 if getattr(dist, "transform", None) is None:
1137 with self:
-> 1138 var = FreeRV(name=name, distribution=dist, total_size=total_size, model=self)
1139 self.free_RVs.append(var)
1140 else:
/usr/local/lib/python3.7/dist-packages/pymc3/model.py in init(self, type, owner, index, name, distribution, total_size, model)
1667 self.distribution = distribution
1668 self.tag.test_value = (
-> 1669 np.ones(distribution.shape, distribution.dtype) * distribution.default()
1670 )
1671 self.logp_elemwiset = distribution.logp(self)
/usr/local/lib/python3.7/dist-packages/theano/graph/utils.py in setattr(self, attr, obj)
264
265 if getattr(self, "attr", None) == attr:
--> 266 obj = self.attr_filter(obj)
267
268 return object.setattr(self, attr, obj)
/usr/local/lib/python3.7/dist-packages/theano/tensor/type.py in filter(self, data, strict, allow_downcast)
180 if self.ndim != data.ndim:
181 raise TypeError(
--> 182 f"Wrong number of dimensions: expected {self.ndim},"
183 f" got {data.ndim} with shape {data.shape}."
184 )
TypeError: Wrong number of dimensions: expected 0, got 1 with shape (6,).
It seems my covariance matrix does not meet the required type\format. I don't know how to fix this.

Specifying linear regression in python

I tried to implement robust standard errors to my linear regression, but received the following error:
LinAlgError: Singular matrix
Here is the code that raises the error:
#Get robust standard errors
Robust=sm.OLS(y,X, M=sm.robust.norms.HuberT()).fit()
Robust.summary()
qq = sm.qqplot(Robust.resid, line = 'r')
Robust2=sm.RLM(y,X, M=sm.robust.norms.HuberT()).fit(cov="H2")
Robust2.summary()
shapiro(residual)
pred_val2 = Robust2.fittedvalues.copy()
true_val = regression_data_performance['CRSP_return_p1'].values.copy()
residual2 = true_val - pred_val2
shapiro(residual2)
data=['Sales','up_revision','pos_EPS','filing_range','time','DebttoAssets','ShareOverhang','VentureBacked','CRSP_ret_SP500_prior30days',
'CRSP_vola_SP500_prior30days','top_tier_dummy','ProceedsAmtInThisMktMil','Age','MainSICCode_industry48_y_Soda',
'MainSICCode_industry48_y_PerSv','MainSICCode_industry48_y_Rtail','MainSICCode_industry48_y_Rubbr','MainSICCode_industry48_y_Ships',
'MainSICCode_industry48_y_Smoke','MainSICCode_industry48_y_Steel','MainSICCode_industry48_y_Toys','MainSICCode_industry48_y_Txtls','MainSICCode_industry48_y_Whlsl','CRSP_return_p1']
regression_data_performance=regression_data_performance[~isNaN(regression_data_performance['filing_range'])]
data=regression_data_performance[data]
data1=data.fillna(0)
data2 = preprocessing.scale(data1)
In case it helps you here is the complete error message:
LinAlgError Traceback (most recent call last)
<ipython-input-33-1febd940375c> in <module>
3 Robust.summary()
4 qq = sm.qqplot(Robust.resid, line = 'r')
----> 5 Robust2=sm.RLM(y,X, M=sm.robust.norms.HuberT()).fit(cov="H2")
6 Robust2.summary()
7 shapiro(residual)
~\anaconda3\lib\site-packages\statsmodels\robust\robust_linear_model.py in fit(self, maxiter, tol, scale_est, init, cov, update_scale, conv, start_params)
299 converged = _check_convergence(criterion, iteration, tol, maxiter)
300 results = RLMResults(self, wls_results.params,
--> 301 self.normalized_cov_params, self.scale)
302
303 history['iteration'] = iteration
~\anaconda3\lib\site-packages\statsmodels\robust\robust_linear_model.py in __init__(self, model, params, normalized_cov_params, scale)
410 self.data_in_cache = ['sresid']
411
--> 412 self.cov_params_default = self.bcov_scaled
413 # TODO: "pvals" should come from chisq on bse?
414
pandas\_libs\properties.pyx in pandas._libs.properties.CachedProperty.__get__()
~\anaconda3\lib\site-packages\statsmodels\robust\robust_linear_model.py in bcov_scaled(self)
453 W = np.dot(model.M.psi_deriv(self.sresid) * model.exog.T,
454 model.exog)
--> 455 W_inv = np.linalg.inv(W)
456 # [W_jk]^-1 = [SUM(psi_deriv(Sr_i)*x_ij*x_jk)]^-1
457 # where Sr are the standardized residuals
<__array_function__ internals> in inv(*args, **kwargs)
~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in inv(a)
545 signature = 'D->D' if isComplexType(t) else 'd->d'
546 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 547 ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
548 return wrap(ainv.astype(result_t, copy=False))
549
~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in _raise_linalgerror_singular(err, flag)
95
96 def _raise_linalgerror_singular(err, flag):
---> 97 raise LinAlgError("Singular matrix")
98
99 def _raise_linalgerror_nonposdef(err, flag):
LinAlgError: Singular matrix
Does anyone know how I can solve the error? If you need more information please let me know.

Bokeh Geoviews use Lat/Long or UTM?

I am trying to plot the Zillow dataset with Bokeh using Geoviews and Datashader but I am having the damnedest time getting it to work. I am able to plot the data on a Cartesian plane fine but when I attempt to overlay the data with a map I run into errors.
I have used code adapted from the census-hv example on the datashader github. I believe my problem is that it is looking for the coordinates to be in UTM not Lat/Long. Because the code works when I have my coordinates multiplied by a few thousand. The points are then put above the map in white space. If i attempt to plot the proper lat/long coordinates I get the following errors.
Can someone please point me in the direction of a map that uses Lat/Long
>>>props.head()
longitude latitude
0 -118.654084 34.144442
1 -118.625364 34.140430
2 -118.394633 33.989359
3 -118.437206 34.148863
4 -118.385816 34.194168
import pandas as pd
import holoviews as hv
import geoviews as gv
import datashader as ds
from bokeh.models import WMTSTileSource
from holoviews.operation.datashader import datashade, dynspread
hv.notebook_ex
tension('bokeh')
%%opts Overlay [width=900 height=525 xaxis=None yaxis=None]
geomap = gv.WMTS(WMTSTileSource(url=\
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'))
points = hv.Points(gv.Dataset(props, kdims=['longitude', 'latitude']))
# color_key = {'w':'aqua', 'b':'lime', 'a':'red', 'h':'fuchsia', 'o':'yellow' }
race = datashade(points, x_sampling=50, y_sampling=50,
element_type=gv.Image)
geomap * race
RETURNS ERROR:
WARNING:root:dynamic_operation: Exception raised in callable
'dynamic_operation' of type 'function'.
Invoked as dynamic_operation(height=400, scale=1.0, width=400, x_range=None, y_range=None)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
305 pass
306 else:
--> 307 return printer(obj)
308 # Finally look for special method names
309 method = get_real_method(obj, self.print_method)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in pprint_display(obj)
255 if not ip.display_formatter.formatters['text/plain'].pprint:
256 return None
--> 257 return display(obj, raw=True)
258
259
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in display(obj, raw, **kwargs)
241 elif isinstance(obj, (HoloMap, DynamicMap)):
242 with option_state(obj):
--> 243 html = map_display(obj)
244 else:
245 return repr(obj) if raw else IPython.display.display(obj, **kwargs)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in wrapped(element)
127 try:
128 html = fn(element,
--> 129 max_frames=OutputMagic.options['max_frames'])
130
131 # Only want to add to the archive for one display hook...
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in map_display(vmap, max_frames)
196 return None
197
--> 198 return render(vmap)
199
200
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/ipython/display_hooks.py in render(obj, **kwargs)
57 if renderer.fig == 'pdf':
58 renderer = renderer.instance(fig='png')
---> 59 return renderer.html(obj, **kwargs)
60
61
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in html(self, obj, fmt, css, comm, **kwargs)
253 code to initialize a Comm, if the plot supplies one.
254 """
--> 255 plot, fmt = self._validate(obj, fmt)
256 figdata, _ = self(plot, fmt, **kwargs)
257 if css is None: css = self.css
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in _validate(self, obj, fmt)
189 if isinstance(obj, tuple(self.widgets.values())):
190 return obj, 'html'
--> 191 plot = self.get_plot(obj, renderer=self)
192
193 fig_formats = self.mode_formats['fig'][self.mode]
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/renderer.py in get_plot(self_or_cls, obj, renderer)
164 """
165 # Initialize DynamicMaps with first data item
--> 166 initialize_dynamic(obj)
167
168 if not isinstance(obj, Plot) and not displayable(obj):
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/plotting/util.py in initialize_dynamic(obj)
173 continue
174 if not len(dmap):
--> 175 dmap[dmap._initial_key()]
176
177
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __getitem__(self, key)
942 # Not a cross product and nothing cached so compute element.
943 if cache is not None: return cache
--> 944 val = self._execute_callback(*tuple_key)
945 if data_slice:
946 val = self._dataslice(val, data_slice)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in _execute_callback(self, *args)
791
792 with dynamicmap_memoization(self.callback, self.streams):
--> 793 retval = self.callback(*args, **kwargs)
794 return self._style(retval)
795
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __call__(self, *args, **kwargs)
489 # Nothing to do for callbacks that accept no arguments
490 (inargs, inkwargs) = (args, kwargs)
--> 491 if not args and not kwargs: return self.callable()
492 inputs = [i for i in self.inputs if isinstance(i, DynamicMap)]
493 streams = []
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/overlay.py in dynamic_mul(*args, **kwargs)
27 from .spaces import Callable
28 def dynamic_mul(*args, **kwargs):
---> 29 element = other[args]
30 return self * element
31 callback = Callable(dynamic_mul, inputs=[self, other])
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __getitem__(self, key)
942 # Not a cross product and nothing cached so compute element.
943 if cache is not None: return cache
--> 944 val = self._execute_callback(*tuple_key)
945 if data_slice:
946 val = self._dataslice(val, data_slice)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in _execute_callback(self, *args)
791
792 with dynamicmap_memoization(self.callback, self.streams):
--> 793 retval = self.callback(*args, **kwargs)
794 return self._style(retval)
795
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/spaces.py in __call__(self, *args, **kwargs)
519
520 try:
--> 521 ret = self.callable(*args, **kwargs)
522 except:
523 posstr = ', '.join(['%r' % el for el in inargs]) if inargs else ''
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/util.py in dynamic_operation(*key, **kwargs)
101 self.p.kwargs.update(kwargs)
102 obj = map_obj[key] if isinstance(map_obj, HoloMap) else map_obj
--> 103 return self._process(obj, key)
104 else:
105 def dynamic_operation(*key, **kwargs):
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/util.py in _process(self, element, key)
87 kwargs = {k: v for k, v in self.p.kwargs.items()
88 if k in self.p.operation.params()}
---> 89 return self.p.operation.process_element(element, key, **kwargs)
90 else:
91 return self.p.operation(element, **self.p.kwargs)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/operation.py in process_element(self, element, key, **params)
133 """
134 self.p = param.ParamOverrides(self, params)
--> 135 return self._process(element, key)
136
137
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/operation/datashader.py in _process(self, element, key)
357
358 def _process(self, element, key=None):
--> 359 agg = aggregate._process(self, element, key)
360 shaded = shade._process(self, agg, key)
361 return shaded
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/operation/datashader.py in _process(self, element, key)
226 agg = getattr(cvs, glyph)(data, x, y, self.p.aggregator)
227 if agg.ndim == 2:
--> 228 return self.p.element_type(agg, **params)
229 else:
230 return NdOverlay({c: self.p.element_type(agg.sel(**{column: c}),
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/geoviews/element/geo.py in __init__(self, data, **kwargs)
81 elif crs:
82 kwargs['crs'] = crs
---> 83 super(_Element, self).__init__(data, **kwargs)
84
85
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/element/raster.py in __init__(self, data, bounds, extents, xdensity, ydensity, **params)
242 if bounds is None:
243 xvals = self.dimension_values(0, False)
--> 244 l, r, xdensity, _ = util.bound_range(xvals, xdensity)
245 yvals = self.dimension_values(1, False)
246 b, t, ydensity, _ = util.bound_range(yvals, ydensity)
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/holoviews/core/util.py in bound_range(vals, density)
1373 using significant digits reported by sys.float_info.dig.
1374 """
-> 1375 low, high = vals.min(), vals.max()
1376 invert = False
1377 if vals[0] > vals[1]:
/home/mcamp/anaconda3/envs/py3.6/lib/python3.6/site-packages/numpy/core/_methods.py in _amin(a, axis, out, keepdims)
27
28 def _amin(a, axis=None, out=None, keepdims=False):
---> 29 return umr_minimum(a, axis, None, out, keepdims)
30
31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
ValueError: zero-size array to reduction operation minimum which has no identity
Out[54]:
b':DynamicMap []'
I think the problem here is two-fold, first of all since the coordinates are latitudes and longitudes and you specify xsampling/ysampling values of 50 the datashaded image ends up with a tiny or zero shape, which causes this error. My suggestion would be to cast the coordinates to Google Mercator first. In future this PR will let you do so very simply by calling this:
import cartopy.crs as ccrs
projected = gv.operation.project(points, projection=ccrs.GOOGLE_MERCATOR)
...
To do this manually for now you can use the cartopy projection directly:
coords = ccrs.GOOGLE_MERCATOR.transform_points(ccrs.PlateCarree(), lons, lats)
projected = gv.Points(coords, crs=ccrs.GOOGLE_MERCATOR)
...

Integrating functions of derivatives in Sympy

I'm tring to do some variational calculus in Python with sympy and I have runned to a problem, and I do not quite understand the origin. I am using the Anaconda suite, Jupyter Notebook and IPython version 5.3.0, with python 3.6.
If I writte
from sympy import Funtion, symbols, integrate
from IPython.display import display
t, s = symbols('t s')
f = Function('f')
c = Function('c')
compute the following expressions (they will be the elements of the part that fails)
display(f(t, s))
display(c(t, s))
display(f(c(t, s)))
display(f(c(t, s), c(t, s).diff(t)))
display(integrate( f(t, s), t))
display(integrate( f(c(t, s)), t))
display(integrate( f(c(t, s).diff(t)), t))
display(integrate( f(c(t, s),c(s,t) ), t))
the expected (latex) expressions appear. I see that it can handle derivation and integration of multiple variables, but when I try to put some things togheter, as in
display(integrate( f(c(t, s), c(t, s).diff(t)), t))
the following error is raised
Can't calculate 1st derivative wrt Derivative(_x1, t).
On the other hand,
display(integrate( f(c(t,s), c(t,s).diff(t)), s))
displays the correct output. Any ideas how one might fix this?
Thank you in advance
EDIT : .doit() hasn't helped.
The error is preceeded by
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-66abd1a98aff> in <module>()
11 display(integrate( f(c(t,s).diff(t)), t))
12 display(integrate( f(c(t,s), c(s,t)), t))
---> 13 display(integrate( f(c(t,s), c(t,s).diff(t)), t))
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\integrals.py in integrate(*args, **kwargs)
1278 if isinstance(integral, Integral):
1279 return integral.doit(deep=False, meijerg=meijerg, conds=conds,
-> 1280 risch=risch, manual=manual)
1281 else:
1282 return integral
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\integrals.py in doit(self, **hints)
484 function, xab[0],
485 meijerg=meijerg1, risch=risch, manual=manual,
--> 486 conds=conds)
487 if antideriv is None and meijerg1 is True:
488 ret = try_meijerg(function, xab)
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\integrals.py in _eval_integral(self, f, x, meijerg, risch, manual, conds)
885 try:
886 if conds == 'piecewise':
--> 887 h = heurisch_wrapper(g, x, hints=[])
888 else:
889 h = heurisch(g, x, hints=[])
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in heurisch_wrapper(f, x, rewrite, hints, mappings, retries, degree_offset, unnecessary_permutations)
128
129 res = heurisch(f, x, rewrite, hints, mappings, retries, degree_offset,
--> 130 unnecessary_permutations)
131 if not isinstance(res, Basic):
132 return res
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in heurisch(f, x, rewrite, hints, mappings, retries, degree_offset, unnecessary_permutations)
672 else:
673 if retries >= 0:
--> 674 result = heurisch(f, x, mappings=mappings, rewrite=rewrite, hints=hints, retries=retries - 1, unnecessary_permutations=unnecessary_permutations)
675
676 if result is not None:
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in heurisch(f, x, rewrite, hints, mappings, retries, degree_offset, unnecessary_permutations)
672 else:
673 if retries >= 0:
--> 674 result = heurisch(f, x, mappings=mappings, rewrite=rewrite, hints=hints, retries=retries - 1, unnecessary_permutations=unnecessary_permutations)
675
676 if result is not None:
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in heurisch(f, x, rewrite, hints, mappings, retries, degree_offset, unnecessary_permutations)
672 else:
673 if retries >= 0:
--> 674 result = heurisch(f, x, mappings=mappings, rewrite=rewrite, hints=hints, retries=retries - 1, unnecessary_permutations=unnecessary_permutations)
675
676 if result is not None:
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in heurisch(f, x, rewrite, hints, mappings, retries, degree_offset, unnecessary_permutations)
451 mapping = list(mapping)
452 mapping = mapping + unnecessary_permutations
--> 453 diffs = [ _substitute(dcache.get_diff(g)) for g in terms ]
454 denoms = [ g.as_numer_denom()[1] for g in diffs ]
455 if all(h.is_polynomial(*V) for h in denoms) and _substitute(f).is_rational_function(*V):
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in <listcomp>(.0)
451 mapping = list(mapping)
452 mapping = mapping + unnecessary_permutations
--> 453 diffs = [ _substitute(dcache.get_diff(g)) for g in terms ]
454 denoms = [ g.as_numer_denom()[1] for g in diffs ]
455 if all(h.is_polynomial(*V) for h in denoms) and _substitute(f).is_rational_function(*V):
....\Anaconda2\envs\Py3\lib\site-packages\sympy\integrals\heurisch.py in _substitute(expr)
446
447 def _substitute(expr):
--> 448 return expr.subs(mapping)
449
450 for mapping in mappings:
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\basic.py in subs(self, *args, **kwargs)
900 rv = self
901 for old, new in sequence:
--> 902 rv = rv._subs(old, new, **kwargs)
903 if not isinstance(rv, Basic):
904 break
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\cache.py in wrapper(*args, **kwargs)
91 def wrapper(*args, **kwargs):
92 try:
---> 93 retval = cfunc(*args, **kwargs)
94 except TypeError:
95 retval = func(*args, **kwargs)
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\basic.py in _subs(self, old, new, **hints)
1014 rv = self._eval_subs(old, new)
1015 if rv is None:
-> 1016 rv = fallback(self, old, new)
1017 return rv
1018
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\basic.py in fallback(self, old, new)
986 if not hasattr(arg, '_eval_subs'):
987 continue
--> 988 arg = arg._subs(old, new, **hints)
989 if not _aresame(arg, args[i]):
990 hit = True
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\cache.py in wrapper(*args, **kwargs)
91 def wrapper(*args, **kwargs):
92 try:
---> 93 retval = cfunc(*args, **kwargs)
94 except TypeError:
95 retval = func(*args, **kwargs)
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\basic.py in _subs(self, old, new, **hints)
1012 return new
1013
-> 1014 rv = self._eval_subs(old, new)
1015 if rv is None:
1016 rv = fallback(self, old, new)
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\function.py in _eval_subs(self, old, new)
1340 variables = self_vars_front + self_vars
1341 return Derivative(new, *variables)
-> 1342 return Derivative(*(x._subs(old, new) for x in self.args))
1343
1344 def _eval_lseries(self, x, logx):
....\Anaconda2\envs\Py3\lib\site-packages\sympy\core\function.py in __new__(cls, expr, *variables, **assumptions)
1068 ordinal = 'st' if last_digit == 1 else 'nd' if last_digit == 2 else 'rd' if last_digit == 3 else 'th'
1069 raise ValueError(filldedent('''
-> 1070 Can\'t calculate %s%s derivative wrt %s.''' % (count, ordinal, v)))
1071
1072 if all_zero and not count == 0:
ValueError:
Can't calculate 1st derivative wrt Derivative(_x1, t).
​
I would use Integral for displaying integrals, which only constructs them and does not attempt to compute them (unless your perform a doit()). The following works
display(Integral( f(c(t, s), c(t, s).diff(t)), t))

Categories