Material for ASPP 2024

This commit is contained in:
Pietro Berkes 2024-08-27 15:52:41 +03:00
commit 849682b13b
97 changed files with 8170 additions and 0 deletions

View file

@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:14:28.926189Z",
"start_time": "2022-08-12T12:14:28.923089Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:21:00.003395Z",
"start_time": "2022-08-12T12:20:59.997851Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"dict_ = {'a': 3.1, 'b': 4.2}\n",
"with open('my_class.json', 'w') as f:\n",
" json.dump(dict_, f)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:03.889266Z",
"start_time": "2022-08-12T12:23:03.883050Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"class MyClass:\n",
" \n",
" def __init__(self, a, b):\n",
" \"\"\"The basic constructor takes 'raw' values.\"\"\"\n",
" self.a = a\n",
" self.b = b\n",
" \n",
" @classmethod\n",
" def from_random_values(cls, random_state=np.random):\n",
" \"\"\"Create a MyClass instance with random parameters.\"\"\"\n",
" a = random_state.rand()\n",
" b = random_state.randn()\n",
" return cls(a, b)\n",
" \n",
" @classmethod\n",
" def from_json(cls, json_fname):\n",
" \"\"\"Create a MyClass instance with parameters read form a json file.\"\"\"\n",
" with open(json_fname, 'r') as f:\n",
" dict_ = json.load(f)\n",
" a = dict_['a']\n",
" b = dict_['b']\n",
" return cls(a, b)\n",
"\n",
"my_class = MyClass.from_random_values()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:12.242599Z",
"start_time": "2022-08-12T12:23:12.237477Z"
},
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 0.842940228048758, 'b': 0.2797222990193814}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_class = MyClass.from_random_values()\n",
"my_class.__dict__"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:44.439726Z",
"start_time": "2022-08-12T12:23:44.432540Z"
},
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 3.1, 'b': 4.2}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_class = MyClass.from_json('my_class.json')\n",
"my_class.__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python [conda env:bog]",
"language": "python",
"name": "conda-env-bog-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"toc": {
"nav_menu": {
"height": "12px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View file

@ -0,0 +1,287 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:13:12.648377Z",
"start_time": "2022-09-02T11:13:12.165387Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:14:10.246402Z",
"start_time": "2022-09-02T11:14:10.235135Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n",
" self.sigma_i = sigma_i\n",
" self.sigma_j = sigma_j\n",
" self.size = size\n",
"\n",
" if map_type == 'flat':\n",
" context_map = np.ones((size, size))\n",
" elif map_type == 'hills':\n",
" grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n",
" i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n",
" i_waves /= i_waves.max()\n",
" j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) + \\\n",
" np.sin(grid_jj / 10)\n",
" j_waves /= j_waves.max()\n",
" context_map = j_waves + i_waves\n",
" elif map_type == 'labyrinth':\n",
" context_map = np.ones((size, size))\n",
" context_map[50:100, 50:60] = 0\n",
" context_map[20:89, 80:90] = 0\n",
" context_map[90:120, 0:10] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
"\n",
" context_map[50:60, 50:200] = 0\n",
" context_map[179:189, 80:130] = 0\n",
" context_map[110:120, 0:190] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
" context_map /= context_map.sum()\n",
" self.context_map = context_map\n",
"\n",
" # Pre-compute a 2D grid of coordinates for efficiency\n",
" self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n",
"\n",
"walker = Walker(sigma_i=3, sigma_j=4, size=200, map_type='hills')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:15:44.357965Z",
"start_time": "2022-09-02T11:15:44.351558Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, context_map):\n",
" self.sigma_i = sigma_i\n",
" self.sigma_j = sigma_j\n",
" self.size = size\n",
" self.context_map = context_map\n",
" # Pre-compute a 2D grid of coordinates for efficiency\n",
" self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n",
"\n",
" @classmethod\n",
" def from_context_map_type(cls, sigma_i, sigma_j, size, map_type):\n",
" \"\"\" Create an instance of Walker with a context map defined by type.\"\"\"\n",
" if map_type == 'flat':\n",
" context_map = np.ones((size, size))\n",
" elif map_type == 'hills':\n",
" grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n",
" i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n",
" i_waves /= i_waves.max()\n",
" j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) +\\\n",
" np.sin(grid_jj / 10)\n",
" j_waves /= j_waves.max()\n",
" context_map = j_waves + i_waves\n",
" elif map_type == 'labyrinth':\n",
" context_map = np.ones((size, size))\n",
" context_map[50:100, 50:60] = 0\n",
" context_map[20:89, 80:90] = 0\n",
" context_map[90:120, 0:10] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
"\n",
" context_map[50:60, 50:200] = 0\n",
" context_map[179:189, 80:130] = 0\n",
" context_map[110:120, 0:190] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
"\n",
" context_map /= context_map.sum()\n",
" return cls(sigma_i, sigma_j, size, context_map)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:15:49.092194Z",
"start_time": "2022-09-02T11:15:49.086575Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"walker = Walker.from_context_map_type(sigma_i=3, sigma_j=4, size=200, map_type='hills')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:19:37.723607Z",
"start_time": "2022-09-02T11:19:37.717518Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def flat_context_map_builder(size):\n",
" \"\"\" A context map where all positions are equally likely. \"\"\"\n",
" return np.ones((size, size))\n",
"\n",
"\n",
"def hills_context_map_builder(size):\n",
" \"\"\" A context map with bumps and valleys. \"\"\"\n",
" grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n",
" i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n",
" i_waves /= i_waves.max()\n",
" j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) + \\\n",
" np.sin(grid_jj / 10)\n",
" j_waves /= j_waves.max()\n",
" context_map = j_waves + i_waves\n",
" return context_map\n",
"\n",
"\n",
"def labyrinth_context_map_builder(size):\n",
" \"\"\" A context map that looks like a labyrinth. \"\"\"\n",
" context_map = np.ones((size, size))\n",
" context_map[50:100, 50:60] = 0\n",
" context_map[20:89, 80:90] = 0\n",
" context_map[90:120, 0:10] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
"\n",
" context_map[50:60, 50:200] = 0\n",
" context_map[179:189, 80:130] = 0\n",
" context_map[110:120, 0:190] = 0\n",
" context_map[120:size, 30:40] = 0\n",
" context_map[180:190, 50:60] = 0\n",
"\n",
" return context_map"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:20:25.815725Z",
"start_time": "2022-09-02T11:20:25.811489Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"class Walker:\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, context_map):\n",
" self.sigma_i = sigma_i\n",
" self.sigma_j = sigma_j\n",
" self.size = size\n",
" self.context_map = context_map\n",
" # Pre-compute a 2D grid of coordinates for efficiency\n",
" self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n",
"\n",
" @classmethod\n",
" def from_context_map_builder(cls, sigma_i, sigma_j, size, context_map_builder):\n",
" \"\"\"Initialize the context map from an external builder.\n",
"\n",
" `builder` is a callable that takes a `size` as input parameter\n",
" and outputs a `size` x `size` numpy array of positive values.\n",
" \"\"\"\n",
" context_map = context_map_builder(size)\n",
" context_map /= context_map.sum()\n",
" return cls(sigma_i, sigma_j, size, context_map)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2022-09-02T11:20:26.367914Z",
"start_time": "2022-09-02T11:20:26.362287Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"walker = Walker.from_context_map_builder(\n",
" sigma_i=3, \n",
" sigma_j=4, \n",
" size=200, \n",
" context_map_builder=hills_context_map_builder,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python [conda env:bog]",
"language": "python",
"name": "conda-env-bog-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"toc": {
"nav_menu": {
"height": "12px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View file

@ -0,0 +1,223 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "8ad9fe94",
"metadata": {},
"outputs": [],
"source": [
"class Walker:\n",
" # ...\n",
" \n",
" def sample_next_step(self, current_i, current_j, random_state=np.random):\n",
" \"\"\" Sample a new position for the walker. \"\"\"\n",
" # Combine the next-step proposal with the context map to get a\n",
" # next-step probability map\n",
" next_step_map = self._next_step_proposal(current_i, current_j)\n",
" selection_map = self._compute_next_step_probability(next_step_map)\n",
"\n",
" # Draw a new position from the next-step probability map\n",
" r = random_state.rand()\n",
" cumulative_map = np.cumsum(selection_map)\n",
" cumulative_map = cumulative_map.reshape(selection_map.shape)\n",
" i_next, j_next = np.argwhere(cumulative_map >= r)[0]\n",
"\n",
" return i_next, j_next\n",
"\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" \"\"\" Create the 2D proposal map for the next step of the walker. \"\"\"\n",
" # 2D Gaussian distribution , centered at current position,\n",
" # and with different standard deviations for i and j\n",
" grid_ii, grid_jj = self._grid_ii, self._grid_jj\n",
" sigma_i, sigma_j = self.sigma_i, self.sigma_j\n",
"\n",
" rad = (\n",
" (((grid_ii - current_i) ** 2) / (sigma_i ** 2))\n",
" + (((grid_jj - current_j) ** 2) / (sigma_j ** 2))\n",
" )\n",
"\n",
" p_next_step = np.exp(-(rad / 2.0)) / (2.0 * np.pi * sigma_i * sigma_j)\n",
" return p_next_step / p_next_step.sum()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "95148e45",
"metadata": {},
"outputs": [],
"source": [
"class Walker:\n",
" # ...\n",
"\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" \"\"\" Create the 2D proposal map for the next step of the walker. \"\"\"\n",
" raise NotImplementedError(\"`_next_step_proposal` not implemented\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da34226f",
"metadata": {},
"outputs": [],
"source": [
"class GaussianWalker(Walker):\n",
" # ...\n",
"\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"class RectangularWalker(Walker):\n",
" # ...\n",
"\n",
" def _next_step_proposal(self, current_i, current_j):\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"class JumpingWalker(Walker):\n",
" # ...\n",
"\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfd90d16",
"metadata": {},
"outputs": [],
"source": [
"class Walker:\n",
" # ...\n",
"\n",
"\n",
" def _compute_next_step_probability(self, next_step_map):\n",
" \"\"\" Compute the next step probability map from next step proposal and\n",
" context map. \"\"\"\n",
" next_step_probability = next_step_map * self.context_map\n",
" next_step_probability /= next_step_probability.sum()\n",
" return next_step_probability\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72041675",
"metadata": {},
"outputs": [],
"source": [
"class GaussianWalkerWithProductInteraction(Walker):\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" # ...\n",
" def _compute_next_step_probability(self, next_step_map):\n",
" # ...\n",
"\n",
" \n",
"class GaussianWalkerWithSumInteraction(Walker):\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" # ...\n",
" def _compute_next_step_probability(self, next_step_map):\n",
" # ...\n",
"\n",
" \n",
"class RectangularWalkerWithProductInteraction(Walker):\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" # ...\n",
" def _compute_next_step_probability(self, next_step_map):\n",
" # ...\n",
"\n",
" \n",
"class RectangularWalkerWithSumInteraction(Walker):\n",
" def _next_step_proposal(self, current_i, current_j):\n",
" # ...\n",
" def _compute_next_step_probability(self, next_step_map):\n",
" # ...\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ee2e200",
"metadata": {},
"outputs": [],
"source": [
"class Walker:\n",
" def __init__(self, size, context_map, next_step_proposal, next_step_proposal_arguments):\n",
" self.next_step_proposal = next_step_proposal\n",
" # ...\n",
"\n",
" \n",
" def sample_next_step(self, current_i, current_j, random_state=np.random):\n",
" \"\"\" Sample a new position for the walker. \"\"\"\n",
" # Combine the next-step proposal with the context map to get a\n",
" # next-step probability map\n",
" next_step_map = self.next_step_proposal(current_i, current_j, **next_step_proposal_arguments)\n",
" selection_map = self._compute_next_step_probability(next_step_map)\n",
"\n",
" # Draw a new position from the next-step probability map\n",
" r = random_state.rand()\n",
" cumulative_map = np.cumsum(selection_map)\n",
" cumulative_map = cumulative_map.reshape(selection_map.shape)\n",
" i_next, j_next = np.argwhere(cumulative_map >= r)[0]\n",
"\n",
" return i_next, j_next\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View file

@ -0,0 +1,105 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2120045b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n",
" # ...\n",
"\n",
" def plot_trajectory(self, trajectory):\n",
" \"\"\" Plot a trajectory over a context map. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.matshow(self.context_map)\n",
" plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')\n",
" plt.show()\n",
"\n",
" def plot_trajectory_hexbin(self, trajectory):\n",
" \"\"\" Plot an hexagonal density map of a trajectory. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" with plt.rc_context({'figure.figsize': (4, 4), \n",
" 'axes.labelsize': 16, \n",
" 'xtick.labelsize': 14, \n",
" 'ytick.labelsize': 14}):\n",
" plt.hexbin(\n",
" trajectory[:, 1], trajectory[:, 0], \n",
" gridsize=30, extent=(0, 200, 0, 200), \n",
" edgecolors='none', cmap='Reds'\n",
" )\n",
" plt.gca().invert_yaxis()\n",
" plt.xlabel('X')\n",
" plt.ylabel('Y')\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8b1035c",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"\n",
"class Walker:\n",
" \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n",
"\n",
" def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n",
" # ...\n",
"\n",
" def plot_trajectory(self, trajectory):\n",
" \"\"\" Plot a trajectory over a context map. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.matshow(self.context_map)\n",
" plt.plot(trajectory[:, 1], trajectory[:, 0], color='r')\n",
" plt.show()\n",
"\n",
" def plot_trajectory_hexbin(self, trajectory):\n",
" \"\"\" Plot an hexagonal density map of a trajectory. \"\"\"\n",
" trajectory = np.asarray(trajectory)\n",
" plt.hexbin(\n",
" trajectory[:, 1], trajectory[:, 0], \n",
" gridsize=30, extent=(0, 200, 0, 200), \n",
" edgecolors='none', cmap='Reds'\n",
" )\n",
" plt.gca().invert_yaxis()\n",
" plt.xlabel('X')\n",
" plt.ylabel('Y')\n",
" \n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}