From eb2d3554629bbb3959fa32dea61436fcce08a80b Mon Sep 17 00:00:00 2001 From: Ricardo Date: Thu, 16 Jun 2022 09:14:41 +0200 Subject: [PATCH 1/3] Override notebook kernel when building the docs Co-authored-by: Oriol Abril-Pla --- docs/source/conf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index dd867fd113..72ae364507 100755 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -144,13 +144,13 @@ "about/featured_testimonials.md", ] -# myst and panels config -jupyter_execute_notebooks = "force" +# myst config +nb_execution_mode = "force" +nb_kernel_rgx_aliases = {".*": "python3"} myst_enable_extensions = ["colon_fence", "deflist", "dollarmath", "amsmath", "substitution"] myst_substitutions = { "version_slug": rtd_version, } -panels_add_bootstrap_css = False myst_heading_anchors = None rediraffe_redirects = { From 97f31f55df7f28751b97703a9f11cd463f612f67 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Thu, 16 Jun 2022 09:21:19 +0200 Subject: [PATCH 2/3] Run black on core notebooks --- .pre-commit-config.yaml | 1 + .../learn/core_notebooks/GLM_linear.ipynb | 2 +- .../learn/core_notebooks/dimensionality.ipynb | 34 +++++++------- .../core_notebooks/posterior_predictive.ipynb | 21 ++++++--- .../learn/core_notebooks/pymc_aesara.ipynb | 36 +++++++++------ .../learn/core_notebooks/pymc_overview.ipynb | 44 ++++++++++++------- 6 files changed, 85 insertions(+), 53 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bb0e0af4ba..ec7867544b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -45,6 +45,7 @@ repos: rev: 22.3.0 hooks: - id: black + - id: black-jupyter - repo: https://github.com/PyCQA/pylint rev: v2.14.0 hooks: diff --git a/docs/source/learn/core_notebooks/GLM_linear.ipynb b/docs/source/learn/core_notebooks/GLM_linear.ipynb index 1765e4eef2..4816780b29 100644 --- a/docs/source/learn/core_notebooks/GLM_linear.ipynb +++ b/docs/source/learn/core_notebooks/GLM_linear.ipynb @@ -402,7 +402,7 @@ "metadata": {}, "outputs": [], "source": [ - "idata.posterior[\"y_model\"] = idata.posterior[\"Intercept\"] + idata.posterior[\"x\"]*xr.DataArray(x)" + "idata.posterior[\"y_model\"] = idata.posterior[\"Intercept\"] + idata.posterior[\"x\"] * xr.DataArray(x)" ] }, { diff --git a/docs/source/learn/core_notebooks/dimensionality.ipynb b/docs/source/learn/core_notebooks/dimensionality.ipynb index 8de975ee03..f58b74b59b 100644 --- a/docs/source/learn/core_notebooks/dimensionality.ipynb +++ b/docs/source/learn/core_notebooks/dimensionality.ipynb @@ -160,7 +160,7 @@ } ], "source": [ - "random_sample = pm.Normal.dist(mu=[1,10,100], sigma=.0001).eval()\n", + "random_sample = pm.Normal.dist(mu=[1, 10, 100], sigma=0.0001).eval()\n", "random_sample, random_sample.shape" ] }, @@ -310,11 +310,11 @@ ], "source": [ "with pm.Model() as pmodel:\n", - " pm.Normal(\"scalar\") # shape=()\n", - " pm.Normal(\"vector (implied)\", mu=[1,2,3])\n", + " pm.Normal(\"scalar\") # shape=()\n", + " pm.Normal(\"vector (implied)\", mu=[1, 2, 3])\n", " pm.Normal(\"vector (from shape)\", shape=(4,))\n", " pm.Normal(\"vector (from size)\", size=(5,))\n", - " \n", + "\n", "pm.model_to_graphviz(pmodel)" ] }, @@ -401,7 +401,7 @@ "with pm.Model() as pmodel:\n", " pm.Normal(\"red\", size=2, dims=\"B\")\n", "\n", - " pm.Normal(\"one\", [1,2,3,4], dims=\"Dim_A\") # (4,)\n", + " pm.Normal(\"one\", [1, 2, 3, 4], dims=\"Dim_A\") # (4,)\n", " pm.Normal(\"two\", dims=\"Dim_A\")\n", "\n", "\n", @@ -421,10 +421,12 @@ "metadata": {}, "outputs": [], "source": [ - "with pm.Model(coords={\n", - " \"year\": [2020, 2021, 2022],\n", - "}) as pmodel:\n", - " \n", + "with pm.Model(\n", + " coords={\n", + " \"year\": [2020, 2021, 2022],\n", + " }\n", + ") as pmodel:\n", + "\n", " pm.Normal(\"Normal_RV\", dims=\"year\")\n", "\n", " pm.model_to_graphviz(pmodel)" @@ -483,7 +485,7 @@ } ], "source": [ - "pm.MvNormal.dist(mu=[[1,2,3], [4,5,6]], cov=np.eye(3)*.0001).eval()" + "pm.MvNormal.dist(mu=[[1, 2, 3], [4, 5, 6]], cov=np.eye(3) * 0.0001).eval()" ] }, { @@ -587,9 +589,11 @@ } ], "source": [ - "with pm.Model(coords={\n", - " \"year\": [2020, 2021, 2022],\n", - "}) as pmodel:\n", + "with pm.Model(\n", + " coords={\n", + " \"year\": [2020, 2021, 2022],\n", + " }\n", + ") as pmodel:\n", " mv = pm.MvNormal(\"implied\", mu=[0, 0, 0], cov=np.eye(3))\n", " print(mv.shape.eval())\n", "\n", @@ -598,11 +602,11 @@ "\n", " mv = pm.MvNormal(\"with size\", mu=[0, 0], cov=np.eye(2), size=3, dims=(\"repeats\", \"implied\"))\n", " print(mv.shape.eval())\n", - " \n", + "\n", " # ⚠ Size dims are always __prepended__\n", " mv = pm.MvNormal(\"with shape\", mu=[0, 0], cov=np.eye(2), shape=(3, ...), dims=(\"repeats\", ...))\n", " print(mv.shape.eval())\n", - " \n", + "\n", " mv = pm.MvNormal(\"with coords\", mu=[0, 0], cov=np.eye(2), dims=(\"year\", ...))\n", " print(mv.shape.eval())\n", "\n", diff --git a/docs/source/learn/core_notebooks/posterior_predictive.ipynb b/docs/source/learn/core_notebooks/posterior_predictive.ipynb index 02022c3f89..037e0580c8 100644 --- a/docs/source/learn/core_notebooks/posterior_predictive.ipynb +++ b/docs/source/learn/core_notebooks/posterior_predictive.ipynb @@ -56,6 +56,7 @@ "RANDOM_SEED = 58\n", "rng = np.random.default_rng(RANDOM_SEED)\n", "\n", + "\n", "def standardize(series):\n", " \"\"\"Standardize a pandas series\"\"\"\n", " return (series - series.mean()) / series.std()" @@ -939,17 +940,20 @@ "source": [ "_, ax = plt.subplots()\n", "\n", - "ax.plot(predictor_scaled, mu_pp.mean((\"chain\", \"draw\")), label=\"Mean outcome\", color=\"C1\", alpha=0.6);\n", + "ax.plot(\n", + " predictor_scaled, mu_pp.mean((\"chain\", \"draw\")), label=\"Mean outcome\", color=\"C1\", alpha=0.6\n", + ")\n", "az.plot_lm(\n", - " idata=idata, \n", - " y=\"obs\", \n", - " x=predictor_scaled, \n", + " idata=idata,\n", + " y=\"obs\",\n", + " x=predictor_scaled,\n", " kind_pp=\"hdi\",\n", " y_kwargs={\"color\": \"C0\", \"marker\": \"o\", \"ms\": 4, \"alpha\": 0.4},\n", " y_hat_fill_kwargs=dict(fill_kwargs={\"alpha\": 0.8}, color=\"xkcd:jade\"),\n", " axes=ax,\n", ")\n", - "ax.set_xlabel(\"Predictor (stdz)\"); ax.set_ylabel(\"Outcome (stdz)\");" + "ax.set_xlabel(\"Predictor (stdz)\")\n", + "ax.set_ylabel(\"Outcome (stdz)\");" ] }, { @@ -1221,7 +1225,12 @@ " pm.set_data({\"pred\": predictors_out_of_sample})\n", " # use the updated values and predict outcomes and probabilities:\n", " idata_2 = pm.sample_posterior_predictive(\n", - " idata_2, var_names=[\"p\"], return_inferencedata=True, predictions=True, extend_inferencedata=True, random_seed=rng,\n", + " idata_2,\n", + " var_names=[\"p\"],\n", + " return_inferencedata=True,\n", + " predictions=True,\n", + " extend_inferencedata=True,\n", + " random_seed=rng,\n", " )" ] }, diff --git a/docs/source/learn/core_notebooks/pymc_aesara.ipynb b/docs/source/learn/core_notebooks/pymc_aesara.ipynb index 48e24053fe..93d212d4ed 100644 --- a/docs/source/learn/core_notebooks/pymc_aesara.ipynb +++ b/docs/source/learn/core_notebooks/pymc_aesara.ipynb @@ -61,10 +61,12 @@ "import scipy.stats\n", "\n", "\n", - "print(f\"\"\"\n", + "print(\n", + " f\"\"\"\n", "# Aesara version: {aesara.__version__}\n", "# PyMC version: {pm.__version__}\n", - "\"\"\")" + "\"\"\"\n", + ")" ] }, { @@ -133,13 +135,15 @@ "x = at.scalar(name=\"x\")\n", "y = at.vector(name=\"y\")\n", "\n", - "print(f\"\"\"\n", + "print(\n", + " f\"\"\"\n", "x type: {x.type}\n", "x name = {x.name}\n", "---\n", "y type: {y.type}\n", "y name = {y.name}\n", - "\"\"\")" + "\"\"\"\n", + ")" ] }, { @@ -331,7 +335,7 @@ } ], "source": [ - "w.eval({x: 0, y:[1, np.e]})" + "w.eval({x: 0, y: [1, np.e]})" ] }, { @@ -566,14 +570,16 @@ } ], "source": [ - "print(f\"\"\"\n", + "print(\n", + " f\"\"\"\n", "z type: {z.type}\n", "z name = {z.name}\n", "z owner = {z.owner}\n", "z owner inputs = {z.owner.inputs}\n", "z owner op = {z.owner.op}\n", "z owner output = {z.owner.outputs}\n", - "\"\"\")" + "\"\"\"\n", + ")" ] }, { @@ -746,8 +752,8 @@ }, "outputs": [], "source": [ - "parent_of_w = w.owner.inputs[0] # get z tensor\n", - "new_parent_of_w = at.exp(parent_of_w) # modify the parent of w\n", + "parent_of_w = w.owner.inputs[0] # get z tensor\n", + "new_parent_of_w = at.exp(parent_of_w) # modify the parent of w\n", "new_parent_of_w.name = \"exp(x + y)\"" ] }, @@ -878,7 +884,7 @@ } ], "source": [ - "new_w.eval({x: 0, y:[1, np.e]})" + "new_w.eval({x: 0, y: [1, np.e]})" ] }, { @@ -2050,7 +2056,7 @@ } ], "source": [ - " # Equivalent to rv_draw = pm.draw(rv, 3)\n", + "# Equivalent to rv_draw = pm.draw(rv, 3)\n", "rv.rvs(3)" ] }, @@ -2215,7 +2221,7 @@ "# element-wise log-probability of the model (we do not take te sum)\n", "logp_graph = at.stack(model_2.logp(sum=False))\n", "# evaluate by passing concrete values\n", - "logp_graph.eval({mu_value: 0, sigma_log_value: -10, x_value:0})" + "logp_graph.eval({mu_value: 0, sigma_log_value: -10, x_value: 0})" ] }, { @@ -2251,11 +2257,13 @@ } ], "source": [ - "print(f\"\"\"\n", + "print(\n", + " f\"\"\"\n", "mu_value -> {scipy.stats.norm.logpdf(x=0, loc=0, scale=2)}\n", "sigma_log_value -> {- 10 + scipy.stats.halfnorm.logpdf(x=np.exp(-10), loc=0, scale=3)} \n", "x_value -> {scipy.stats.norm.logpdf(x=0, loc=0, scale=np.exp(-10))}\n", - "\"\"\")\n" + "\"\"\"\n", + ")" ] }, { diff --git a/docs/source/learn/core_notebooks/pymc_overview.ipynb b/docs/source/learn/core_notebooks/pymc_overview.ipynb index d162258899..ab680fd0a4 100644 --- a/docs/source/learn/core_notebooks/pymc_overview.ipynb +++ b/docs/source/learn/core_notebooks/pymc_overview.ipynb @@ -3218,7 +3218,7 @@ } ], "source": [ - "test_scores = pd.read_csv(pm.get_data('test_scores.csv'), index_col=0)\n", + "test_scores = pd.read_csv(pm.get_data(\"test_scores.csv\"), index_col=0)\n", "test_scores.head()" ] }, @@ -3295,7 +3295,7 @@ "metadata": {}, "outputs": [], "source": [ - "D0 = int(D/2)" + "D0 = int(D / 2)" ] }, { @@ -3347,7 +3347,7 @@ "source": [ "import aesara.tensor as at\n", "\n", - "with pm.Model(coords={\"predictors\":X.columns.values}) as test_score_model:\n", + "with pm.Model(coords={\"predictors\": X.columns.values}) as test_score_model:\n", "\n", " # Prior on error SD\n", " sigma = pm.HalfNormal(\"sigma\", 25)\n", @@ -3357,12 +3357,14 @@ " # Local shrinkage prior\n", " lam = pm.HalfStudentT(\"lam\", 2, dims=\"predictors\")\n", " c2 = pm.InverseGamma(\"c2\", 1, 0.1)\n", - " z = pm.Normal(\"z\", 0., 1., dims=\"predictors\")\n", + " z = pm.Normal(\"z\", 0.0, 1.0, dims=\"predictors\")\n", " # Shrunken coefficients\n", - " beta = pm.Deterministic(\"beta\", z * tau * lam * at.sqrt(c2 / (c2 + tau**2 * lam**2)), dims=\"predictors\")\n", + " beta = pm.Deterministic(\n", + " \"beta\", z * tau * lam * at.sqrt(c2 / (c2 + tau**2 * lam**2)), dims=\"predictors\"\n", + " )\n", " # No shrinkage on intercept\n", - " beta0 = pm.Normal(\"beta0\", 100, 25.)\n", - " \n", + " beta0 = pm.Normal(\"beta0\", 100, 25.0)\n", + "\n", " scores = pm.Normal(\"scores\", beta0 + at.dot(X.values, beta), sigma, observed=y.values)" ] }, @@ -3579,8 +3581,19 @@ } ], "source": [ - "az.plot_dist(test_scores[\"score\"].values, kind=\"hist\", color=\"C1\", hist_kwargs=dict(alpha=0.6), label=\"observed\")\n", - "az.plot_dist(prior_samples.prior_predictive[\"scores\"], kind=\"hist\", hist_kwargs=dict(alpha=0.6), label=\"simulated\");\n", + "az.plot_dist(\n", + " test_scores[\"score\"].values,\n", + " kind=\"hist\",\n", + " color=\"C1\",\n", + " hist_kwargs=dict(alpha=0.6),\n", + " label=\"observed\",\n", + ")\n", + "az.plot_dist(\n", + " prior_samples.prior_predictive[\"scores\"],\n", + " kind=\"hist\",\n", + " hist_kwargs=dict(alpha=0.6),\n", + " label=\"simulated\",\n", + ")\n", "plt.xticks(rotation=45);" ] }, @@ -3941,9 +3954,7 @@ "source": [ "with pm.Model() as disaster_model:\n", "\n", - " switchpoint = pm.DiscreteUniform(\n", - " \"switchpoint\", lower=years.min(), upper=years.max()\n", - " )\n", + " switchpoint = pm.DiscreteUniform(\"switchpoint\", lower=years.min(), upper=years.max())\n", "\n", " # Priors for pre- and post-switch rates number of disasters\n", " early_rate = pm.Exponential(\"early_rate\", 1.0)\n", @@ -4071,7 +4082,7 @@ " labels = [label.get_text() for label in ax.get_xticklabels()]\n", " ax.set_xticklabels(labels, rotation=45, ha=\"right\")\n", " break\n", - "plt.draw()\n" + "plt.draw()" ] }, { @@ -4146,9 +4157,9 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "from aesara.compile.ops import as_op\n", "\n", + "\n", "@as_op(itypes=[at.lscalar], otypes=[at.lscalar])\n", "def crazy_modulo3(value):\n", " if value > 0:\n", @@ -4218,7 +4229,7 @@ " raise NotImplementedError(\"Cannot sample from beta variable\")\n", "\n", "\n", - "beta = BetaRV()\n" + "beta = BetaRV()" ] }, { @@ -4232,11 +4243,10 @@ " rv_op = beta\n", "\n", " @classmethod\n", - " def dist(cls, mu=0, **kwargs): \n", + " def dist(cls, mu=0, **kwargs):\n", " mu = at.as_tensor_variable(mu)\n", " return super().dist([mu], **kwargs)\n", "\n", - "\n", " def logp(self, value):\n", " mu = self.mu\n", " return beta_logp(value - mu)\n", From ff73b1c2388f51ef3847e27f0be20abc4225e0d7 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Fri, 17 Jun 2022 11:50:55 +0200 Subject: [PATCH 3/3] Add jupyter-sphinx dependency to dev environments Closes #5845 --- conda-envs/environment-dev-py37.yml | 1 + conda-envs/environment-dev-py38.yml | 1 + conda-envs/environment-dev-py39.yml | 1 + conda-envs/windows-environment-dev-py38.yml | 1 + requirements-dev.txt | 1 + 5 files changed, 5 insertions(+) diff --git a/conda-envs/environment-dev-py37.yml b/conda-envs/environment-dev-py37.yml index 0fbe5b9bf5..0f0e4ba949 100644 --- a/conda-envs/environment-dev-py37.yml +++ b/conda-envs/environment-dev-py37.yml @@ -14,6 +14,7 @@ dependencies: - h5py>=2.7 - ipython>=7.16 - jax +- jupyter-sphinx - myst-nb - numpy>=1.15.0 - numpydoc diff --git a/conda-envs/environment-dev-py38.yml b/conda-envs/environment-dev-py38.yml index b27ba13f3c..9c84be355e 100644 --- a/conda-envs/environment-dev-py38.yml +++ b/conda-envs/environment-dev-py38.yml @@ -14,6 +14,7 @@ dependencies: - h5py>=2.7 - ipython>=7.16 - jax +- jupyter-sphinx - myst-nb - numpy>=1.15.0 - numpydoc diff --git a/conda-envs/environment-dev-py39.yml b/conda-envs/environment-dev-py39.yml index f46c83edd0..1bcb8dd7f8 100644 --- a/conda-envs/environment-dev-py39.yml +++ b/conda-envs/environment-dev-py39.yml @@ -14,6 +14,7 @@ dependencies: - h5py>=2.7 - ipython>=7.16 - jax +- jupyter-sphinx - myst-nb - numpy>=1.15.0 - numpydoc diff --git a/conda-envs/windows-environment-dev-py38.yml b/conda-envs/windows-environment-dev-py38.yml index 4259d0091b..4deb41a676 100644 --- a/conda-envs/windows-environment-dev-py38.yml +++ b/conda-envs/windows-environment-dev-py38.yml @@ -12,6 +12,7 @@ dependencies: - cloudpickle - fastprogress>=0.2.0 - h5py>=2.7 +- jupyter-sphinx - numpy>=1.15.0 - pandas>=0.24.0 - pip diff --git a/requirements-dev.txt b/requirements-dev.txt index 7d7523e780..b8f7ceb771 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,6 +9,7 @@ cloudpickle fastprogress>=0.2.0 h5py>=2.7 ipython>=7.16 +jupyter-sphinx myst-nb numpy>=1.15.0 numpydoc