#!/usr/bin/env python # coding: utf-8 # # Marked Point Pattern # # 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: # # * Location pattern of the events are of interest # * Stochastic attribute attached to the events is of interest # # 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**. # In[1]: from pointpats import PoissonPointProcess, PoissonClusterPointProcess, Window, poly_from_bbox, PointPattern import libpysal as ps from libpysal.cg import shapely_ext get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt # In[2]: # open the virginia polygon shapefile va = ps.io.open(ps.examples.get_path("virginia.shp")) polys = [shp for shp in va] # In[3]: # Create the exterior polygons for VA from the union of the county shapes state = shapely_ext.cascaded_union(polys) # create window from virginia state boundary window = Window(state.parts) # In[4]: window.bbox # In[5]: window.centroid # In[6]: samples = PoissonPointProcess(window, 200, 1, conditioning=False, asPP=False) # In[7]: csr = PointPattern(samples.realizations[0]) # In[8]: cx, cy = window.centroid # In[9]: cx # In[10]: cy # In[11]: west = csr.points.x < cx south = csr.points.y < cy east = 1 - west north = 1 - south # #### Create an attribute named quad which has a value for each event. # In[12]: quad = 1 * east * north + 2 * west * north + 3 * west * south + 4 * east * south # In[13]: type(quad) # In[14]: quad # #### Attach the attribute quad to the point pattern # In[15]: csr.add_marks([quad], mark_names=['quad']) # In[16]: csr.df # #### Explode a marked point pattern into a sequence of individual point patterns. Since the mark quad has 4 unique values, the sequence will be of length 4. # In[17]: csr_q = csr.explode('quad') # In[18]: len(csr_q) # In[19]: csr # In[20]: csr.summary() # #### Plot the 4 individual sequences # In[21]: get_ipython().run_line_magic('pinfo', 'plt.xlim') # In[22]: plt.xlim() for ppn in csr_q: ppn.plot() # #### Plot the 4 unmarked point patterns using the same axes for a convenient comparison of locations # In[23]: x0, y0, x1, y1 = csr.mbb ylim = (y0, y1) xlim = (x0, x1) # In[24]: for ppn in csr_q: ppn.plot() plt.xlim(xlim) plt.ylim(ylim) # In[ ]: # In[ ]: