{ "cells": [ { "cell_type": "markdown", "id": "c77a6289-edb0-4d1a-ae91-7fa79d81a49b", "metadata": {}, "source": [ "# Demo of `MotionBuilder`" ] }, { "cell_type": "code", "execution_count": null, "id": "9c9ac5c9", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "id": "6a9472a9-8f67-42aa-90c7-a70e461def74", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import re\n", "import sys\n", "import xarray as xr" ] }, { "cell_type": "code", "execution_count": null, "id": "9d562822-3b7b-47c1-b65a-3fa0a679e921", "metadata": {}, "outputs": [], "source": [ "try:\n", " from bapsf_motion.motion_builder import MotionBuilder\n", "except ModuleNotFoundError:\n", " from pathlib import Path\n", "\n", " HERE = Path().cwd()\n", " BAPSF_MOTION = (HERE / \"..\" / \"..\" / \"..\" ).resolve()\n", " sys.path.append(str(BAPSF_MOTION))\n", " \n", " from bapsf_motion.motion_builder import MotionBuilder" ] }, { "cell_type": "code", "execution_count": null, "id": "8ec7cf78-0667-4080-b9e8-7accd884a2d3", "metadata": {}, "outputs": [], "source": [ "plt.rcParams.update(\n", " {\n", " # \"figure.figsize\": [12, 0.56 * 12],\n", " \"figure.figsize\": [10, 0.8 * 10],\n", " \"font.size\": 16,\n", " }\n", ")" ] }, { "cell_type": "markdown", "id": "35ac7d9d-e264-44e0-b680-c5fa8ca8acf6", "metadata": {}, "source": [ "Let's set up a typical rectangular grid for a probe on the East port." ] }, { "cell_type": "code", "execution_count": null, "id": "c1f46a44-007c-4769-91e5-31fc8f84d3df", "metadata": {}, "outputs": [], "source": [ "mb = MotionBuilder(\n", " space=[\n", " {\"label\": \"x\", \"range\": [-55, 55], \"num\": 221},\n", " {\"label\": \"y\", \"range\": [-55, 55], \"num\": 221},\n", " ],\n", " exclusions=[\n", " {\"type\": \"lapd_xy\", \"port_location\": \"E\", \"cone_full_angle\": 60},\n", " {\"type\": \"circle\", \"radius\": 5, \"exclude\": \"inside\"},\n", " ],\n", " layers=[\n", " {\"type\": \"grid\", \"limits\": [[0, 30], [-30, 30]], \"steps\": [11, 21]},\n", " ],\n", ")\n", "\n", "mb.mask.plot(x=\"x\", y=\"y\");\n", "\n", "points = mb._ds[\"point_layer1\"].data\n", "flat_ax = np.prod(points.shape[:-1])\n", "points = np.reshape(points, (flat_ax, points.shape[-1]))\n", "pt1 = points\n", "# print(pt1.shape)\n", "\n", "plt.scatter(\n", " points[...,0],\n", " points[..., 1], s=4**2,\n", " color=\"red\",\n", ")\n", "\n", "valid_points = mb.motion_list\n", "\n", "plt.scatter(\n", " valid_points[..., 0],\n", " valid_points[..., 1],\n", " linewidth=1,\n", " s=6**2,\n", " facecolors=\"deepskyblue\",\n", " edgecolors=\"black\",\n", ");" ] }, { "cell_type": "code", "execution_count": null, "id": "836fd665-575f-4439-a256-d209745886f5", "metadata": {}, "outputs": [], "source": [ "mb._ds" ] }, { "cell_type": "markdown", "id": "60d81785-97fd-4f42-913b-161f6a25ac78", "metadata": {}, "source": [ "Let's say we want the same setup as before, but with a probe on the 45 degree port. This is easily accomplished by just specifying the `port_location` as an angle for the LaPD exclusion layer." ] }, { "cell_type": "code", "execution_count": null, "id": "06fc142a-a336-49a6-bc00-7af9dd0d7169", "metadata": {}, "outputs": [], "source": [ "mb = MotionBuilder(\n", " space=\"lapd_xy\",\n", " exclusions=[\n", " {\"type\": \"lapd_xy\", \"port_location\": 45, \"cone_full_angle\": 60},\n", " ],\n", " layers=[\n", " {\"type\": \"grid\", \"limits\": [[0, 30], [-30, 30]], \"steps\": [11, 21]},\n", " ],\n", ")\n", "\n", "mb.mask.plot(x=\"x\", y=\"y\");\n", "\n", "points = mb._ds[\"point_layer1\"].data\n", "flat_ax = np.prod(points.shape[:-1])\n", "points = np.reshape(points, (flat_ax, points.shape[-1]))\n", "pt1 = points\n", "# print(pt1.shape)\n", "\n", "plt.scatter(\n", " points[...,0],\n", " points[..., 1], s=4**2,\n", " color=\"red\",\n", ")\n", "\n", "valid_points = mb.motion_list\n", "\n", "plt.scatter(\n", " valid_points[..., 0],\n", " valid_points[..., 1],\n", " linewidth=1,\n", " s=6**2,\n", " facecolors=\"deepskyblue\",\n", " edgecolors=\"black\",\n", ");" ] }, { "cell_type": "markdown", "id": "4f871085-db05-471e-8b66-24337bb19463", "metadata": {}, "source": [ "Let's se we just want a vertical line for a target in the top port." ] }, { "cell_type": "code", "execution_count": null, "id": "179b6a73-6435-417f-bd27-cc1c8330ff2f", "metadata": {}, "outputs": [], "source": [ "mb = MotionBuilder(\n", " space=\"lapd_xy\",\n", " exclusions=[\n", " {\"type\": \"lapd_xy\", \"port_location\": \"T\", \"cone_full_angle\": 60},\n", " ],\n", " layers=[\n", " {\"type\": \"grid\", \"limits\": [[0, 0], [-20, 20]], \"steps\": [1, 11]},\n", " ],\n", ")\n", "\n", "mb.mask.plot(x=\"x\", y=\"y\");\n", "\n", "points = mb._ds[\"point_layer1\"].data\n", "flat_ax = np.prod(points.shape[:-1])\n", "points = np.reshape(points, (flat_ax, points.shape[-1]))\n", "pt1 = points\n", "# print(pt1.shape)\n", "\n", "plt.scatter(\n", " points[...,0],\n", " points[..., 1], s=4**2,\n", " color=\"red\",\n", ")\n", "\n", "valid_points = mb.motion_list\n", "\n", "plt.scatter(\n", " valid_points[..., 0],\n", " valid_points[..., 1],\n", " linewidth=1,\n", " s=6**2,\n", " facecolors=\"deepskyblue\",\n", " edgecolors=\"black\",\n", ");" ] }, { "cell_type": "markdown", "id": "be4dd748-700e-4217-b868-23723667879e", "metadata": {}, "source": "Let's say we have a probe on the West port and want to take two grids of data where the first grid has a low density of points over a large area and the seconde gride has a high density of points over small area. This is easily accomplished by just defining the two grids as separate layers. The order in with the points will be taken depends on the order in which the layers are specified. In this example, the low density grid is completely measured first and then followed by the high density grid." }, { "cell_type": "code", "execution_count": null, "id": "93be86c7-30d7-4389-9d1e-939be0d40fda", "metadata": {}, "outputs": [], "source": [ "mb = MotionBuilder(\n", " space=\"lapd_xy\",\n", " exclusions=[\n", " {\"type\": \"lapd_xy\", \"port_location\": \"W\", \"cone_full_angle\": 60},\n", " ],\n", " layers=[\n", " {\"type\": \"grid\", \"limits\": [[-30, 30], [-20, 20]], \"steps\": [21, 11]},\n", " {\"type\": \"grid\", \"limits\": [[-20, 0], [-10, 10]], \"steps\": [15, 11]},\n", " ],\n", ")\n", "\n", "mb.mask.plot(x=\"x\", y=\"y\");\n", "\n", "for player in mb.layers:\n", " points = player.points.data\n", " flat_ax = np.prod(points.shape[:-1])\n", " points = np.reshape(points, (flat_ax, points.shape[-1]))\n", "\n", " plt.scatter(\n", " points[...,0],\n", " points[..., 1], s=4**2,\n", " color=\"red\",\n", " )\n", "\n", "valid_points = mb.motion_list\n", "\n", "plt.scatter(\n", " valid_points[..., 0],\n", " valid_points[..., 1],\n", " linewidth=1,\n", " s=6**2,\n", " facecolors=\"deepskyblue\",\n", " edgecolors=\"black\",\n", ");" ] }, { "cell_type": "code", "execution_count": null, "id": "6aeab532-3cc9-4fc9-a27a-96cca57819f6", "metadata": {}, "outputs": [], "source": [ "mb.config" ] }, { "cell_type": "code", "execution_count": null, "id": "dd2f6db5-b155-4606-80ec-c100aae924ba", "metadata": {}, "outputs": [], "source": [ "import tomli_w\n", "from pprint import pprint" ] }, { "cell_type": "code", "execution_count": null, "id": "32f276d4-3a29-4058-a7bb-6f3ce5f3ba04", "metadata": {}, "outputs": [], "source": [ "cstr = tomli_w.dumps(mb.config, multiline_strings=False)\n", "pprint(cstr)" ] }, { "cell_type": "code", "execution_count": null, "id": "73c8c04a-d2ce-48c5-94e6-79a8f49dbf5a", "metadata": {}, "outputs": [], "source": [ "for key, val in mb.config[\"exclusion\"][\"0\"].items():\n", " print(key, val, type(val))" ] }, { "cell_type": "code", "execution_count": null, "id": "c0469c9a-b143-4cea-80e8-05e01eae5cf8", "metadata": {}, "outputs": [], "source": [ "isinstance(mb.config[\"exclusion\"][\"0\"][\"pivot_radius\"], np.generic)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c261c459-c255-4974-91c9-a83ecbf01407", "metadata": {}, "outputs": [], "source": [] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }