{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Marked Point Pattern\n", "\n", "In addition to the [unmarked point pattern](pointpattern.ipynb), non-binary attributes might be associated with each point, leading to the so-called marked point pattern. The charactertistics of a marked point pattern are:\n", "\n", "* Location pattern of the events are of interest\n", "* Stochastic attribute attached to the events is of interest\n", "\n", "Unmarked point pattern can be modified to be a marked point pattern using the method **add_marks** while the method **explode** could decompose a marked point pattern into a sequence of unmarked point patterns. Both methods belong to the class **PointPattern**." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pointpats import PoissonPointProcess, PoissonClusterPointProcess, Window, poly_from_bbox, PointPattern\n", "import libpysal as ps\n", "from libpysal.cg import shapely_ext\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# open the virginia polygon shapefile\n", "va = ps.io.open(ps.examples.get_path(\"virginia.shp\"))\n", "polys = [shp for shp in va]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Create the exterior polygons for VA from the union of the county shapes\n", "state = shapely_ext.cascaded_union(polys)\n", "# create window from virginia state boundary\n", "window = Window(state.parts)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-83.67526245117188, 36.541481018066406, -75.24258422851562, 39.45690155029297]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "window.bbox" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-78.85183583334933, 37.51851209850039)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "window.centroid" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "samples = PoissonPointProcess(window, 200, 1, conditioning=False, asPP=False)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "csr = PointPattern(samples.realizations[0])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "cx, cy = window.centroid" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-78.85183583334933" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cx" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "37.51851209850039" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cy" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "west = csr.points.x < cx\n", "south = csr.points.y < cy\n", "east = 1 - west\n", "north = 1 - south" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create an attribute named quad which has a value for each event." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "quad = 1 * east * north + 2 * west * north + 3 * west * south + 4 * east * south" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.series.Series" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(quad)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 2\n", "1 3\n", "2 3\n", "3 4\n", "4 1\n", "5 3\n", "6 1\n", "7 4\n", "8 2\n", "9 3\n", "10 4\n", "11 4\n", "12 3\n", "13 1\n", "14 1\n", "15 1\n", "16 4\n", "17 1\n", "18 1\n", "19 3\n", "20 4\n", "21 3\n", "22 1\n", "23 3\n", "24 1\n", "25 1\n", "26 4\n", "27 1\n", "28 3\n", "29 3\n", " ..\n", "170 2\n", "171 2\n", "172 3\n", "173 3\n", "174 1\n", "175 4\n", "176 3\n", "177 3\n", "178 4\n", "179 1\n", "180 1\n", "181 3\n", "182 3\n", "183 1\n", "184 2\n", "185 3\n", "186 1\n", "187 2\n", "188 4\n", "189 1\n", "190 4\n", "191 1\n", "192 3\n", "193 3\n", "194 4\n", "195 3\n", "196 1\n", "197 1\n", "198 4\n", "199 4\n", "Length: 200, dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Attach the attribute quad to the point pattern " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "csr.add_marks([quad], mark_names=['quad'])" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | x | \n", "y | \n", "quad | \n", "
---|---|---|---|
0 | \n", "-79.603948 | \n", "37.791190 | \n", "2 | \n", "
1 | \n", "-80.079205 | \n", "37.396681 | \n", "3 | \n", "
2 | \n", "-79.464397 | \n", "36.625981 | \n", "3 | \n", "
3 | \n", "-76.437205 | \n", "36.884895 | \n", "4 | \n", "
4 | \n", "-78.545956 | \n", "37.992603 | \n", "1 | \n", "
5 | \n", "-80.241928 | \n", "36.991135 | \n", "3 | \n", "
6 | \n", "-77.898430 | \n", "37.716846 | \n", "1 | \n", "
7 | \n", "-76.498353 | \n", "37.321863 | \n", "4 | \n", "
8 | \n", "-79.900184 | \n", "37.854658 | \n", "2 | \n", "
9 | \n", "-81.102672 | \n", "36.633735 | \n", "3 | \n", "
10 | \n", "-77.281811 | \n", "36.976553 | \n", "4 | \n", "
11 | \n", "-77.083152 | \n", "37.303132 | \n", "4 | \n", "
12 | \n", "-83.035154 | \n", "36.618248 | \n", "3 | \n", "
13 | \n", "-77.997824 | \n", "38.483939 | \n", "1 | \n", "
14 | \n", "-76.576682 | \n", "37.932985 | \n", "1 | \n", "
15 | \n", "-77.048965 | \n", "37.697935 | \n", "1 | \n", "
16 | \n", "-78.330254 | \n", "37.421786 | \n", "4 | \n", "
17 | \n", "-78.420495 | \n", "38.125428 | \n", "1 | \n", "
18 | \n", "-77.379883 | \n", "38.801099 | \n", "1 | \n", "
19 | \n", "-82.102351 | \n", "36.838275 | \n", "3 | \n", "
20 | \n", "-77.139652 | \n", "36.681456 | \n", "4 | \n", "
21 | \n", "-81.411197 | \n", "37.009286 | \n", "3 | \n", "
22 | \n", "-78.377109 | \n", "38.110156 | \n", "1 | \n", "
23 | \n", "-82.348030 | \n", "37.260970 | \n", "3 | \n", "
24 | \n", "-78.540305 | \n", "37.759264 | \n", "1 | \n", "
25 | \n", "-78.708559 | \n", "38.778273 | \n", "1 | \n", "
26 | \n", "-77.119341 | \n", "37.441159 | \n", "4 | \n", "
27 | \n", "-77.532402 | \n", "37.827257 | \n", "1 | \n", "
28 | \n", "-81.025392 | \n", "36.963752 | \n", "3 | \n", "
29 | \n", "-81.234484 | \n", "37.187202 | \n", "3 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "
170 | \n", "-79.175687 | \n", "38.412747 | \n", "2 | \n", "
171 | \n", "-79.466336 | \n", "37.677601 | \n", "2 | \n", "
172 | \n", "-80.503082 | \n", "36.784620 | \n", "3 | \n", "
173 | \n", "-79.654007 | \n", "36.620456 | \n", "3 | \n", "
174 | \n", "-78.283744 | \n", "38.436626 | \n", "1 | \n", "
175 | \n", "-76.917112 | \n", "36.713404 | \n", "4 | \n", "
176 | \n", "-82.234255 | \n", "36.978863 | \n", "3 | \n", "
177 | \n", "-81.012936 | \n", "37.186895 | \n", "3 | \n", "
178 | \n", "-77.979839 | \n", "36.758554 | \n", "4 | \n", "
179 | \n", "-77.979268 | \n", "38.366633 | \n", "1 | \n", "
180 | \n", "-75.550882 | \n", "37.940486 | \n", "1 | \n", "
181 | \n", "-81.031949 | \n", "37.194569 | \n", "3 | \n", "
182 | \n", "-80.957128 | \n", "37.150309 | \n", "3 | \n", "
183 | \n", "-77.766785 | \n", "37.647643 | \n", "1 | \n", "
184 | \n", "-79.411096 | \n", "37.705193 | \n", "2 | \n", "
185 | \n", "-80.691844 | \n", "36.673324 | \n", "3 | \n", "
186 | \n", "-78.752009 | \n", "38.137110 | \n", "1 | \n", "
187 | \n", "-79.160647 | \n", "38.047687 | \n", "2 | \n", "
188 | \n", "-78.244262 | \n", "37.137316 | \n", "4 | \n", "
189 | \n", "-77.641999 | \n", "38.612349 | \n", "1 | \n", "
190 | \n", "-77.961376 | \n", "37.257348 | \n", "4 | \n", "
191 | \n", "-77.664101 | \n", "37.612087 | \n", "1 | \n", "
192 | \n", "-82.073349 | \n", "37.151563 | \n", "3 | \n", "
193 | \n", "-80.029721 | \n", "36.650720 | \n", "3 | \n", "
194 | \n", "-77.015099 | \n", "36.892940 | \n", "4 | \n", "
195 | \n", "-81.938727 | \n", "36.759000 | \n", "3 | \n", "
196 | \n", "-78.653617 | \n", "38.637913 | \n", "1 | \n", "
197 | \n", "-78.353159 | \n", "38.009389 | \n", "1 | \n", "
198 | \n", "-76.652473 | \n", "36.918815 | \n", "4 | \n", "
199 | \n", "-76.806542 | \n", "37.477773 | \n", "4 | \n", "
200 rows × 3 columns
\n", "