Skip to content

Commit 6e6865b

Browse files
ricardoV94twiecki
authored andcommitted
Run black on core notebooks
1 parent a886030 commit 6e6865b

File tree

6 files changed

+85
-53
lines changed

6 files changed

+85
-53
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ repos:
4545
rev: 22.3.0
4646
hooks:
4747
- id: black
48+
- id: black-jupyter
4849
- repo: https://github.com/PyCQA/pylint
4950
rev: v2.14.0
5051
hooks:

docs/source/learn/core_notebooks/GLM_linear.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@
402402
"metadata": {},
403403
"outputs": [],
404404
"source": [
405-
"idata.posterior[\"y_model\"] = idata.posterior[\"Intercept\"] + idata.posterior[\"x\"]*xr.DataArray(x)"
405+
"idata.posterior[\"y_model\"] = idata.posterior[\"Intercept\"] + idata.posterior[\"x\"] * xr.DataArray(x)"
406406
]
407407
},
408408
{

docs/source/learn/core_notebooks/dimensionality.ipynb

+19-15
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
}
161161
],
162162
"source": [
163-
"random_sample = pm.Normal.dist(mu=[1,10,100], sigma=.0001).eval()\n",
163+
"random_sample = pm.Normal.dist(mu=[1, 10, 100], sigma=0.0001).eval()\n",
164164
"random_sample, random_sample.shape"
165165
]
166166
},
@@ -310,11 +310,11 @@
310310
],
311311
"source": [
312312
"with pm.Model() as pmodel:\n",
313-
" pm.Normal(\"scalar\") # shape=()\n",
314-
" pm.Normal(\"vector (implied)\", mu=[1,2,3])\n",
313+
" pm.Normal(\"scalar\") # shape=()\n",
314+
" pm.Normal(\"vector (implied)\", mu=[1, 2, 3])\n",
315315
" pm.Normal(\"vector (from shape)\", shape=(4,))\n",
316316
" pm.Normal(\"vector (from size)\", size=(5,))\n",
317-
" \n",
317+
"\n",
318318
"pm.model_to_graphviz(pmodel)"
319319
]
320320
},
@@ -401,7 +401,7 @@
401401
"with pm.Model() as pmodel:\n",
402402
" pm.Normal(\"red\", size=2, dims=\"B\")\n",
403403
"\n",
404-
" pm.Normal(\"one\", [1,2,3,4], dims=\"Dim_A\") # (4,)\n",
404+
" pm.Normal(\"one\", [1, 2, 3, 4], dims=\"Dim_A\") # (4,)\n",
405405
" pm.Normal(\"two\", dims=\"Dim_A\")\n",
406406
"\n",
407407
"\n",
@@ -421,10 +421,12 @@
421421
"metadata": {},
422422
"outputs": [],
423423
"source": [
424-
"with pm.Model(coords={\n",
425-
" \"year\": [2020, 2021, 2022],\n",
426-
"}) as pmodel:\n",
427-
" \n",
424+
"with pm.Model(\n",
425+
" coords={\n",
426+
" \"year\": [2020, 2021, 2022],\n",
427+
" }\n",
428+
") as pmodel:\n",
429+
"\n",
428430
" pm.Normal(\"Normal_RV\", dims=\"year\")\n",
429431
"\n",
430432
" pm.model_to_graphviz(pmodel)"
@@ -483,7 +485,7 @@
483485
}
484486
],
485487
"source": [
486-
"pm.MvNormal.dist(mu=[[1,2,3], [4,5,6]], cov=np.eye(3)*.0001).eval()"
488+
"pm.MvNormal.dist(mu=[[1, 2, 3], [4, 5, 6]], cov=np.eye(3) * 0.0001).eval()"
487489
]
488490
},
489491
{
@@ -587,9 +589,11 @@
587589
}
588590
],
589591
"source": [
590-
"with pm.Model(coords={\n",
591-
" \"year\": [2020, 2021, 2022],\n",
592-
"}) as pmodel:\n",
592+
"with pm.Model(\n",
593+
" coords={\n",
594+
" \"year\": [2020, 2021, 2022],\n",
595+
" }\n",
596+
") as pmodel:\n",
593597
" mv = pm.MvNormal(\"implied\", mu=[0, 0, 0], cov=np.eye(3))\n",
594598
" print(mv.shape.eval())\n",
595599
"\n",
@@ -598,11 +602,11 @@
598602
"\n",
599603
" mv = pm.MvNormal(\"with size\", mu=[0, 0], cov=np.eye(2), size=3, dims=(\"repeats\", \"implied\"))\n",
600604
" print(mv.shape.eval())\n",
601-
" \n",
605+
"\n",
602606
" # ⚠ Size dims are always __prepended__\n",
603607
" mv = pm.MvNormal(\"with shape\", mu=[0, 0], cov=np.eye(2), shape=(3, ...), dims=(\"repeats\", ...))\n",
604608
" print(mv.shape.eval())\n",
605-
" \n",
609+
"\n",
606610
" mv = pm.MvNormal(\"with coords\", mu=[0, 0], cov=np.eye(2), dims=(\"year\", ...))\n",
607611
" print(mv.shape.eval())\n",
608612
"\n",

docs/source/learn/core_notebooks/posterior_predictive.ipynb

+15-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"RANDOM_SEED = 58\n",
5757
"rng = np.random.default_rng(RANDOM_SEED)\n",
5858
"\n",
59+
"\n",
5960
"def standardize(series):\n",
6061
" \"\"\"Standardize a pandas series\"\"\"\n",
6162
" return (series - series.mean()) / series.std()"
@@ -939,17 +940,20 @@
939940
"source": [
940941
"_, ax = plt.subplots()\n",
941942
"\n",
942-
"ax.plot(predictor_scaled, mu_pp.mean((\"chain\", \"draw\")), label=\"Mean outcome\", color=\"C1\", alpha=0.6);\n",
943+
"ax.plot(\n",
944+
" predictor_scaled, mu_pp.mean((\"chain\", \"draw\")), label=\"Mean outcome\", color=\"C1\", alpha=0.6\n",
945+
")\n",
943946
"az.plot_lm(\n",
944-
" idata=idata, \n",
945-
" y=\"obs\", \n",
946-
" x=predictor_scaled, \n",
947+
" idata=idata,\n",
948+
" y=\"obs\",\n",
949+
" x=predictor_scaled,\n",
947950
" kind_pp=\"hdi\",\n",
948951
" y_kwargs={\"color\": \"C0\", \"marker\": \"o\", \"ms\": 4, \"alpha\": 0.4},\n",
949952
" y_hat_fill_kwargs=dict(fill_kwargs={\"alpha\": 0.8}, color=\"xkcd:jade\"),\n",
950953
" axes=ax,\n",
951954
")\n",
952-
"ax.set_xlabel(\"Predictor (stdz)\"); ax.set_ylabel(\"Outcome (stdz)\");"
955+
"ax.set_xlabel(\"Predictor (stdz)\")\n",
956+
"ax.set_ylabel(\"Outcome (stdz)\");"
953957
]
954958
},
955959
{
@@ -1221,7 +1225,12 @@
12211225
" pm.set_data({\"pred\": predictors_out_of_sample})\n",
12221226
" # use the updated values and predict outcomes and probabilities:\n",
12231227
" idata_2 = pm.sample_posterior_predictive(\n",
1224-
" idata_2, var_names=[\"p\"], return_inferencedata=True, predictions=True, extend_inferencedata=True, random_seed=rng,\n",
1228+
" idata_2,\n",
1229+
" var_names=[\"p\"],\n",
1230+
" return_inferencedata=True,\n",
1231+
" predictions=True,\n",
1232+
" extend_inferencedata=True,\n",
1233+
" random_seed=rng,\n",
12251234
" )"
12261235
]
12271236
},

docs/source/learn/core_notebooks/pymc_aesara.ipynb

+22-14
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@
6161
"import scipy.stats\n",
6262
"\n",
6363
"\n",
64-
"print(f\"\"\"\n",
64+
"print(\n",
65+
" f\"\"\"\n",
6566
"# Aesara version: {aesara.__version__}\n",
6667
"# PyMC version: {pm.__version__}\n",
67-
"\"\"\")"
68+
"\"\"\"\n",
69+
")"
6870
]
6971
},
7072
{
@@ -133,13 +135,15 @@
133135
"x = at.scalar(name=\"x\")\n",
134136
"y = at.vector(name=\"y\")\n",
135137
"\n",
136-
"print(f\"\"\"\n",
138+
"print(\n",
139+
" f\"\"\"\n",
137140
"x type: {x.type}\n",
138141
"x name = {x.name}\n",
139142
"---\n",
140143
"y type: {y.type}\n",
141144
"y name = {y.name}\n",
142-
"\"\"\")"
145+
"\"\"\"\n",
146+
")"
143147
]
144148
},
145149
{
@@ -331,7 +335,7 @@
331335
}
332336
],
333337
"source": [
334-
"w.eval({x: 0, y:[1, np.e]})"
338+
"w.eval({x: 0, y: [1, np.e]})"
335339
]
336340
},
337341
{
@@ -566,14 +570,16 @@
566570
}
567571
],
568572
"source": [
569-
"print(f\"\"\"\n",
573+
"print(\n",
574+
" f\"\"\"\n",
570575
"z type: {z.type}\n",
571576
"z name = {z.name}\n",
572577
"z owner = {z.owner}\n",
573578
"z owner inputs = {z.owner.inputs}\n",
574579
"z owner op = {z.owner.op}\n",
575580
"z owner output = {z.owner.outputs}\n",
576-
"\"\"\")"
581+
"\"\"\"\n",
582+
")"
577583
]
578584
},
579585
{
@@ -746,8 +752,8 @@
746752
},
747753
"outputs": [],
748754
"source": [
749-
"parent_of_w = w.owner.inputs[0] # get z tensor\n",
750-
"new_parent_of_w = at.exp(parent_of_w) # modify the parent of w\n",
755+
"parent_of_w = w.owner.inputs[0] # get z tensor\n",
756+
"new_parent_of_w = at.exp(parent_of_w) # modify the parent of w\n",
751757
"new_parent_of_w.name = \"exp(x + y)\""
752758
]
753759
},
@@ -878,7 +884,7 @@
878884
}
879885
],
880886
"source": [
881-
"new_w.eval({x: 0, y:[1, np.e]})"
887+
"new_w.eval({x: 0, y: [1, np.e]})"
882888
]
883889
},
884890
{
@@ -2050,7 +2056,7 @@
20502056
}
20512057
],
20522058
"source": [
2053-
" # Equivalent to rv_draw = pm.draw(rv, 3)\n",
2059+
"# Equivalent to rv_draw = pm.draw(rv, 3)\n",
20542060
"rv.rvs(3)"
20552061
]
20562062
},
@@ -2215,7 +2221,7 @@
22152221
"# element-wise log-probability of the model (we do not take te sum)\n",
22162222
"logp_graph = at.stack(model_2.logp(sum=False))\n",
22172223
"# evaluate by passing concrete values\n",
2218-
"logp_graph.eval({mu_value: 0, sigma_log_value: -10, x_value:0})"
2224+
"logp_graph.eval({mu_value: 0, sigma_log_value: -10, x_value: 0})"
22192225
]
22202226
},
22212227
{
@@ -2251,11 +2257,13 @@
22512257
}
22522258
],
22532259
"source": [
2254-
"print(f\"\"\"\n",
2260+
"print(\n",
2261+
" f\"\"\"\n",
22552262
"mu_value -> {scipy.stats.norm.logpdf(x=0, loc=0, scale=2)}\n",
22562263
"sigma_log_value -> {- 10 + scipy.stats.halfnorm.logpdf(x=np.exp(-10), loc=0, scale=3)} \n",
22572264
"x_value -> {scipy.stats.norm.logpdf(x=0, loc=0, scale=np.exp(-10))}\n",
2258-
"\"\"\")\n"
2265+
"\"\"\"\n",
2266+
")"
22592267
]
22602268
},
22612269
{

docs/source/learn/core_notebooks/pymc_overview.ipynb

+27-17
Original file line numberDiff line numberDiff line change
@@ -3218,7 +3218,7 @@
32183218
}
32193219
],
32203220
"source": [
3221-
"test_scores = pd.read_csv(pm.get_data('test_scores.csv'), index_col=0)\n",
3221+
"test_scores = pd.read_csv(pm.get_data(\"test_scores.csv\"), index_col=0)\n",
32223222
"test_scores.head()"
32233223
]
32243224
},
@@ -3295,7 +3295,7 @@
32953295
"metadata": {},
32963296
"outputs": [],
32973297
"source": [
3298-
"D0 = int(D/2)"
3298+
"D0 = int(D / 2)"
32993299
]
33003300
},
33013301
{
@@ -3347,7 +3347,7 @@
33473347
"source": [
33483348
"import aesara.tensor as at\n",
33493349
"\n",
3350-
"with pm.Model(coords={\"predictors\":X.columns.values}) as test_score_model:\n",
3350+
"with pm.Model(coords={\"predictors\": X.columns.values}) as test_score_model:\n",
33513351
"\n",
33523352
" # Prior on error SD\n",
33533353
" sigma = pm.HalfNormal(\"sigma\", 25)\n",
@@ -3357,12 +3357,14 @@
33573357
" # Local shrinkage prior\n",
33583358
" lam = pm.HalfStudentT(\"lam\", 2, dims=\"predictors\")\n",
33593359
" c2 = pm.InverseGamma(\"c2\", 1, 0.1)\n",
3360-
" z = pm.Normal(\"z\", 0., 1., dims=\"predictors\")\n",
3360+
" z = pm.Normal(\"z\", 0.0, 1.0, dims=\"predictors\")\n",
33613361
" # Shrunken coefficients\n",
3362-
" beta = pm.Deterministic(\"beta\", z * tau * lam * at.sqrt(c2 / (c2 + tau**2 * lam**2)), dims=\"predictors\")\n",
3362+
" beta = pm.Deterministic(\n",
3363+
" \"beta\", z * tau * lam * at.sqrt(c2 / (c2 + tau**2 * lam**2)), dims=\"predictors\"\n",
3364+
" )\n",
33633365
" # No shrinkage on intercept\n",
3364-
" beta0 = pm.Normal(\"beta0\", 100, 25.)\n",
3365-
" \n",
3366+
" beta0 = pm.Normal(\"beta0\", 100, 25.0)\n",
3367+
"\n",
33663368
" scores = pm.Normal(\"scores\", beta0 + at.dot(X.values, beta), sigma, observed=y.values)"
33673369
]
33683370
},
@@ -3579,8 +3581,19 @@
35793581
}
35803582
],
35813583
"source": [
3582-
"az.plot_dist(test_scores[\"score\"].values, kind=\"hist\", color=\"C1\", hist_kwargs=dict(alpha=0.6), label=\"observed\")\n",
3583-
"az.plot_dist(prior_samples.prior_predictive[\"scores\"], kind=\"hist\", hist_kwargs=dict(alpha=0.6), label=\"simulated\");\n",
3584+
"az.plot_dist(\n",
3585+
" test_scores[\"score\"].values,\n",
3586+
" kind=\"hist\",\n",
3587+
" color=\"C1\",\n",
3588+
" hist_kwargs=dict(alpha=0.6),\n",
3589+
" label=\"observed\",\n",
3590+
")\n",
3591+
"az.plot_dist(\n",
3592+
" prior_samples.prior_predictive[\"scores\"],\n",
3593+
" kind=\"hist\",\n",
3594+
" hist_kwargs=dict(alpha=0.6),\n",
3595+
" label=\"simulated\",\n",
3596+
")\n",
35843597
"plt.xticks(rotation=45);"
35853598
]
35863599
},
@@ -3941,9 +3954,7 @@
39413954
"source": [
39423955
"with pm.Model() as disaster_model:\n",
39433956
"\n",
3944-
" switchpoint = pm.DiscreteUniform(\n",
3945-
" \"switchpoint\", lower=years.min(), upper=years.max()\n",
3946-
" )\n",
3957+
" switchpoint = pm.DiscreteUniform(\"switchpoint\", lower=years.min(), upper=years.max())\n",
39473958
"\n",
39483959
" # Priors for pre- and post-switch rates number of disasters\n",
39493960
" early_rate = pm.Exponential(\"early_rate\", 1.0)\n",
@@ -4071,7 +4082,7 @@
40714082
" labels = [label.get_text() for label in ax.get_xticklabels()]\n",
40724083
" ax.set_xticklabels(labels, rotation=45, ha=\"right\")\n",
40734084
" break\n",
4074-
"plt.draw()\n"
4085+
"plt.draw()"
40754086
]
40764087
},
40774088
{
@@ -4146,9 +4157,9 @@
41464157
"metadata": {},
41474158
"outputs": [],
41484159
"source": [
4149-
"\n",
41504160
"from aesara.compile.ops import as_op\n",
41514161
"\n",
4162+
"\n",
41524163
"@as_op(itypes=[at.lscalar], otypes=[at.lscalar])\n",
41534164
"def crazy_modulo3(value):\n",
41544165
" if value > 0:\n",
@@ -4218,7 +4229,7 @@
42184229
" raise NotImplementedError(\"Cannot sample from beta variable\")\n",
42194230
"\n",
42204231
"\n",
4221-
"beta = BetaRV()\n"
4232+
"beta = BetaRV()"
42224233
]
42234234
},
42244235
{
@@ -4232,11 +4243,10 @@
42324243
" rv_op = beta\n",
42334244
"\n",
42344245
" @classmethod\n",
4235-
" def dist(cls, mu=0, **kwargs): \n",
4246+
" def dist(cls, mu=0, **kwargs):\n",
42364247
" mu = at.as_tensor_variable(mu)\n",
42374248
" return super().dist([mu], **kwargs)\n",
42384249
"\n",
4239-
"\n",
42404250
" def logp(self, value):\n",
42414251
" mu = self.mu\n",
42424252
" return beta_logp(value - mu)\n",

0 commit comments

Comments
 (0)