From aa458495712841fb0e6191e634896dcb22c5f76a Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 24 Nov 2024 14:56:36 -0800 Subject: [PATCH 1/3] Remove ipywidgets --- lectures/matsuyama.md | 47 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 38 deletions(-) diff --git a/lectures/matsuyama.md b/lectures/matsuyama.md index 7a22f82f..c4b1fbfd 100644 --- a/lectures/matsuyama.md +++ b/lectures/matsuyama.md @@ -3,8 +3,10 @@ jupytext: text_representation: extension: .md format_name: myst + format_version: 0.13 + jupytext_version: 1.16.4 kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 --- @@ -38,11 +40,10 @@ In particular, as trade costs fall and international competition increases, inno Let's start with some imports: -```{code-cell} ipython +```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt from numba import jit -from ipywidgets import interact ``` ### Background @@ -335,7 +336,7 @@ These are the `@jit` statements that you see below (review [this lecture](https: Here's the main body of code -```{code-cell} python3 +```{code-cell} ipython3 @jit(nopython=True) def _hj(j, nk, s1, s2, θ, δ, ρ): """ @@ -651,9 +652,9 @@ The time series share parameters but differ in their initial condition. Here's the function -```{code-cell} python3 +```{code-cell} ipython3 def plot_timeseries(n1_0, n2_0, s1=0.5, θ=2.5, - δ=0.7, ρ=0.2, ax=None, title=''): + δ=0.7, ρ=0.2, ax=None, title=''): """ Plot a single time series with initial conditions """ @@ -735,7 +736,8 @@ Replicate the figure {ref}`shown above ` by coloring initial conditions ```{solution-start} matsuyama_ex1 :class: dropdown ``` -```{code-cell} python3 + +```{code-cell} ipython3 def plot_attraction_basis(s1=0.5, θ=2.5, δ=0.7, ρ=0.2, npts=250, ax=None): if ax is None: fig, ax = plt.subplots() @@ -784,36 +786,5 @@ fig.suptitle("Synchronized versus Asynchronized 2-cycles", plt.show() ``` -Additionally, instead of just seeing 4 plots at once, we might want to -manually be able to change $\rho$ and see how it affects the plot -in real-time. Below we use an interactive plot to do this. - -Note, interactive plotting requires the [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) module to be installed and enabled. - -```{code-cell} python3 -def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250): - # Create the figure and axis that we will plot on - fig, ax = plt.subplots(figsize=(12, 10)) - - # Create model and attraction basis - s1, θ, δ = 0.5, 2.5, 0.75 - model = MSGSync(s1, θ, δ, ρ) - ab = model.create_attraction_basis(maxiter=maxiter, npts=npts) - - # Color map with colormesh - unitrange = np.linspace(0, 1, npts) - cf = ax.pcolormesh(unitrange, unitrange, ab, cmap="viridis") - cbar_ax = fig.add_axes([0.95, 0.15, 0.05, 0.7]) - plt.colorbar(cf, cax=cbar_ax) - plt.show() - return None -``` - -```{code-cell} python3 -fig = interact(interact_attraction_basis, - ρ=(0.0, 1.0, 0.05), - maxiter=(50, 5000, 50), - npts=(25, 750, 25)) -``` ```{solution-end} ``` From 2235e945120cf37648088d4b84ef834c440f2555 Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 24 Nov 2024 18:02:34 -0800 Subject: [PATCH 2/3] add no-execute for widgets --- lectures/matsuyama.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lectures/matsuyama.md b/lectures/matsuyama.md index c4b1fbfd..fc0b6d47 100644 --- a/lectures/matsuyama.md +++ b/lectures/matsuyama.md @@ -786,5 +786,44 @@ fig.suptitle("Synchronized versus Asynchronized 2-cycles", plt.show() ``` +Additionally, instead of just seeing 4 plots at once, we might want to +manually be able to change $\rho$ and see how it affects the plot +in real-time. Below we use an interactive plot to do this. + +Note, interactive plotting requires the [ipywidgets](https://github.com/jupyter-widgets/ipywidgets) module to be installed and enabled. + +```{note} +This interactive plot is disabled on this static webpage. +In order to use this, we recommend to run this notebook locally. +``` + +```{code-cell} python3 +:class: no-execute + +def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250): + # Create the figure and axis that we will plot on + fig, ax = plt.subplots(figsize=(12, 10)) + # Create model and attraction basis + s1, θ, δ = 0.5, 2.5, 0.75 + model = MSGSync(s1, θ, δ, ρ) + ab = model.create_attraction_basis(maxiter=maxiter, npts=npts) + # Color map with colormesh + unitrange = np.linspace(0, 1, npts) + cf = ax.pcolormesh(unitrange, unitrange, ab, cmap="viridis") + cbar_ax = fig.add_axes([0.95, 0.15, 0.05, 0.7]) + plt.colorbar(cf, cax=cbar_ax) + plt.show() + return None +``` + +```{code-cell} python3 +:class: no-execute + +fig = interact(interact_attraction_basis, + ρ=(0.0, 1.0, 0.05), + maxiter=(50, 5000, 50), + npts=(25, 750, 25)) +``` + ```{solution-end} ``` From 967d2c3eb983847ca616b5a265ffdfb6a0784b8e Mon Sep 17 00:00:00 2001 From: kp992 Date: Sun, 24 Nov 2024 18:41:32 -0800 Subject: [PATCH 3/3] Add import --- lectures/matsuyama.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lectures/matsuyama.md b/lectures/matsuyama.md index fc0b6d47..c464db57 100644 --- a/lectures/matsuyama.md +++ b/lectures/matsuyama.md @@ -44,6 +44,7 @@ Let's start with some imports: import numpy as np import matplotlib.pyplot as plt from numba import jit +from ipywidgets import interact ``` ### Background @@ -797,7 +798,7 @@ This interactive plot is disabled on this static webpage. In order to use this, we recommend to run this notebook locally. ``` -```{code-cell} python3 +```{code-cell} ipython3 :class: no-execute def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250): @@ -816,7 +817,7 @@ def interact_attraction_basis(ρ=0.2, maxiter=250, npts=250): return None ``` -```{code-cell} python3 +```{code-cell} ipython3 :class: no-execute fig = interact(interact_attraction_basis,