224 lines
6.7 KiB
Plaintext
224 lines
6.7 KiB
Plaintext
{
|
|
"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
|
|
}
|