def chicago_northwest(): polys = chicago.get_chicago_side_polys() nw = polys['Northwest'] r = pp_models.SeppStochasticNn.from_pickle('/home/gabriel/data/chicago_northwest/burglary/vary_num_nn/nn_100_15.pickle') pp_plotting.multiplots(r) pp_plotting.prediction_heatmap(r, int(r.data.time.toarray()[-1]) + 1.0, poly=nw, dx=30, fmax=0.99, cmap='Reds') plt.axis('equal')
def plot_hit_rate_array(res, max_cover=0.2, min_difference=0.01, save=True): domains = chicago.get_chicago_side_polys(as_shapely=True) chicago_poly = chicago.compute_chicago_region( as_shapely=True).simplify(1000) for ct in CRIME_TYPES: fig, axs = plt.subplots(3, 3, figsize=(10, 8), sharex=False, sharey=False) for i, r in enumerate(REGIONS): ax_j = np.mod(i, 3) ax_i = i / 3 ax = axs[ax_i, ax_j] this_res = res[FILE_FRIENDLY_REGIONS[r]][ct] try: plot_wilcox_test_hit_rate(this_res, ax=ax, max_cover=max_cover, min_difference=min_difference) except KeyError: continue if ax_i != 2: ax.set_xticks([]) if ax_j != 0: ax.set_yticks([]) for i in range(len(REGIONS)): axs.flat[i].set_xlim([0., max_cover]) axs.flat[i].set_ylim([0., 1.]) big_ax = fig.add_subplot(111) big_ax.spines['top'].set_color('none') big_ax.spines['bottom'].set_color('none') big_ax.spines['left'].set_color('none') big_ax.spines['right'].set_color('none') big_ax.set_xticks([]) big_ax.set_yticks([]) big_ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') big_ax.set_xlabel('Coverage') big_ax.set_ylabel('Hit rate') big_ax.patch.set_visible(False) plt.tight_layout(pad=1.5, h_pad=0.25, w_pad=0.5) big_ax.set_position([0.05, 0.05, 0.95, 0.9]) inset_pad = 0.01 # proportion of parent axis width or height inset_width_ratio = 0.35 inset_height_ratio = 0.55 for i, r in enumerate(REGIONS): ax_j = np.mod(i, 3) ax_i = i / 3 ax = axs[ax_i, ax_j] ax_bbox = ax.get_position() inset_bbox = [ ax_bbox.x0 + inset_pad * ax_bbox.width, ax_bbox.y1 - (inset_pad + inset_height_ratio) * ax_bbox.height, inset_width_ratio * ax_bbox.width, inset_height_ratio * ax_bbox.height, ] inset_ax = fig.add_axes(inset_bbox) chicago.plot_domain(chicago_poly, sub_domain=domains[r], ax=inset_ax) if save: filename = 'chicago_ani_iso_kde_hit_rate_%s' % ct fig.savefig(filename + '.png', dpi=200) fig.savefig(filename + '.pdf')
def generate_angle_filters(phi): phi_width = [sum(t[1::2]) for t in phi] phi_filters = [phi_filter_factory(*t) for t in phi] return phi_filters, phi_width if __name__ == '__main__': max_d = 100 geos_simplification = 20 # metres tolerance factor n_sim = 100 start_date = datetime.date(2011, 3, 1) end_date = start_date + datetime.timedelta(days=366) domains = chicago.get_chicago_side_polys(as_shapely=True) # define a vector of threshold distances u = np.linspace(0, max_d, 400) phi = [((2 * i + 1) * np.pi / 8, np.pi / 4.) for i in range(8)] domain_mapping = { 'chicago_south': 'South', 'chicago_southwest': 'Southwest', 'chicago_west': 'West', 'chicago_northwest': 'Northwest', 'chicago_north': 'North', 'chicago_central': 'Central', 'chicago_far_north': 'Far North', 'chicago_far_southwest': 'Far Southwest', 'chicago_far_southeast': 'Far Southeast',
def plot_spatial_ellipse_array(res, icdf=0.95, max_d=800., save=True): """ Plot the ellipsoids containing icdf of the spatial triggering density :param res: Results dict, including model :param icdf: The proportion of spatial density in each dimension contained within the ellipse boundary :param max_d: The axis maximum value. This is increased automatically if it is too small to contain any of the ellipses. :return: """ icdf_two_tailed = 0.5 + icdf / 2. domains = chicago.get_chicago_side_polys(as_shapely=True) colour_mapping = { # 'ani_norm': 'k', 'ani_refl': 'r', 'iso_refl': 'k', } loc = plticker.MultipleLocator(base=400.0) # this locator puts ticks at regular intervals abbreviated_regions = { 'South': 'S', 'Southwest': 'SW', 'West': 'W', 'Northwest': 'NW', 'North': 'N', 'Central': 'C', 'Far North': 'FN', 'Far Southwest': 'FSW', 'Far Southeast': 'FSE', } for ct in CRIME_TYPES: fig, axs = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(8, 8)) for i, r in enumerate(REGIONS): ax_j = np.mod(i, 3) ax_i = i / 3 ax = axs[ax_i, ax_j] dom = domains[r] for m in ('ani_refl', 'iso_refl'): try: k = res[FILE_FRIENDLY_REGIONS[r]][ct][m]['model'].trigger_kde if k.ndim == 3: a = k.marginal_icdf(icdf_two_tailed, dim=1) b = k.marginal_icdf(icdf_two_tailed, dim=2) coords = get_ellipse_coords(a=a, b=b) max_d = max(max(a, b), max_d) else: a = k.marginal_icdf(icdf, dim=1) coords = get_ellipse_coords(a, a) max_d = max(a, max_d) ax.plot(coords[:, 0], coords[:, 1], colour_mapping[m]) except Exception as exc: print repr(exc) ax.text(0.04, 0.86, abbreviated_regions[r], fontsize=14, transform=ax.transAxes) for ax in axs.flat: ax.set_xlim([-max_d, max_d]) ax.set_ylim([-max_d, max_d]) ax.xaxis.set_major_locator(loc) ax.yaxis.set_major_locator(loc) ax.set_aspect('equal', adjustable='box-forced') plt.tight_layout(h_pad=0.2, w_pad=0.2) big_ax = fig.add_subplot(111) big_ax.spines['top'].set_color('none') big_ax.spines['bottom'].set_color('none') big_ax.spines['left'].set_color('none') big_ax.spines['right'].set_color('none') big_ax.set_xticks([]) big_ax.set_yticks([]) big_ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') big_ax.set_xlabel(r'$\Delta x$') big_ax.set_ylabel(r'$\Delta y$') big_ax.patch.set_visible(False) big_ax.set_position([0.05, 0.05, 0.95, 0.9]) if save: filename = "chicago_triggering_spatial_ellipse_%s" % ct fig.savefig(filename + '.png', dpi=200) fig.savefig(filename + '.pdf')
'max_delta_t': 90, 'max_delta_d': 500, 'bg_kde_kwargs': {'number_nn': [100, 15], 'min_bandwidth': None, 'strict': False}, 'trigger_kde_kwargs': {'number_nn': 15, 'min_bandwidth': [1., 50., 50.], 'strict': False}, 'estimation_function': lambda x, y: estimation.estimator_exp_gaussian(x, y, **estimate_kwargs), 'seed': 42, # doesn't matter what this is, just want it fixed 'remove_coincident_pairs': False } pred_include = ('full_static',) # only use this method for prediction domain = chicago.get_chicago_side_polys()[side_name] data, t0, cid = chicago.get_crimes_by_type(start_date=start_date, end_date=end_date, domain=domain) r = models.SeppStochasticNnReflected(data=data, **model_kwargs) ps = r.train(niter=niter) # marginal triggering t = np.linspace(0, 90, 500) x = np.linspace(-500, 500, 500) zt = r.trigger_kde.marginal_pdf(t, dim=0, normed=False) / float(r.ndata) zti = np.cumsum(zt) * (t[1] - t[0]) # numerical integral zx = r.trigger_kde.marginal_pdf(x, dim=1, normed=False) / float(r.ndata) fig = plt.figure(figsize=(6, 5))
__author__ = 'gabriel' from django.db import connection from analysis import chicago import datetime import numpy as np from time import time from database.models import Chicago from data.models import CartesianSpaceTimeData from kde import models as kde_models SRID = 2028 start_date = datetime.date(2011, 3, 1) end_date = start_date + datetime.timedelta(days=366) domain = chicago.get_chicago_side_polys(as_shapely=True)['South'].simplify(1) crime_type = 'burglary' max_d = 500 max_t = 90 class Stik(object): table_name = Chicago._meta.db_table kde_class = kde_models.VariableBandwidthNnKdeSeparable def __init__(self, max_t,
def anisotropy_array_plot(save=True, max_d=None, add_rose=True): """ Not really a flexible method, more a way to isolate this plotting function If max_d is not specified, the full range is used. """ OUTDIR = '/home/gabriel/Dropbox/research/output/ripley_anisotropy/' domains = chicago.get_chicago_side_polys(as_shapely=True) chicago_poly = chicago.compute_chicago_region( as_shapely=True).simplify(1000) CRIME_TYPES = ( 'burglary', 'assault', ) from matplotlib import pyplot as plt # Define phi combinations. This corresponds to inversion through the origin. combinations = [ (3, 7), (0, 4), (1, 5), (2, 6), ] colours = ('k', 'k', 'r', 'r') linestyles = ('solid', 'dashed', 'solid', 'dashed') for ct in CRIME_TYPES: k_obs_dict = collections.defaultdict(dict) k_sim_dict = collections.defaultdict(dict) fig, axs = plt.subplots(3, 3, figsize=(10, 8)) running_max = 0 for i, r in enumerate(chicago_consts.REGIONS): rff = chicago_consts.FILE_FRIENDLY_REGIONS[r] ax_j = np.mod(i, 3) ax_i = i / 3 ax = axs[ax_i, ax_j] infile = os.path.join(OUTDIR, 'ripley_%s_%s.pickle' % (rff, ct)) with open(infile, 'r') as f: res = dill.load(f) k_obs_dict[ct][r] = res['k_obs'] k_sim_dict[ct][r] = res['k_sim'] u = res['u'] if max_d is not None: idx = u <= max_d u = u[idx] for j, c in enumerate(combinations): ls = linestyles[np.mod(j, len(linestyles))] col = colours[np.mod(j, len(colours))] this_k_obs = res['k_obs'][:, c].sum(axis=1) if max_d is not None: this_k_obs = this_k_obs[idx] running_max = max(running_max, this_k_obs.max()) ax.plot(u, this_k_obs, c=col, linestyle=ls) # pick any simulated K - all angles look the same at this scale this_k_sim = res['k_sim'][:, :, :len(c)].sum(axis=2) if max_d is not None: this_k_sim = this_k_sim[:, idx] y0 = this_k_sim.min(axis=0) y1 = this_k_sim.max(axis=0) running_max = max(running_max, y1.max()) ax.fill_between(u, y0, y1, facecolor='k', edgecolor='none', alpha=0.4) if ax_i != 2: ax.set_xticklabels([]) if ax_j != 0: ax.set_yticklabels([]) for i in range(len(chicago_consts.REGIONS)): axs.flat[i].set_ylim([0, running_max * 1.02]) # axs.flat[i].text(3, 0.9 * running_max, domain_mapping[REGIONS[i]]) big_ax = fig.add_subplot(111) big_ax.spines['top'].set_color('none') big_ax.spines['bottom'].set_color('none') big_ax.spines['left'].set_color('none') big_ax.spines['right'].set_color('none') big_ax.set_xticks([]) big_ax.set_yticks([]) big_ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') big_ax.set_xlabel('Distance (m)') big_ax.set_ylabel('Anisotropic Ripley' 's K') big_ax.patch.set_visible(False) plt.tight_layout(pad=1.5, h_pad=0.05, w_pad=0.05) big_ax.set_position([0.05, 0.05, 0.95, 0.9]) inset_pad = 0.01 # proportion of parent axis width or height inset_width_ratio = 0.35 inset_height_ratio = 0.55 for i in range(len(chicago_consts.REGIONS)): ax_j = np.mod(i, 3) ax_i = i / 3 ax = axs[ax_i, ax_j] ax_bbox = ax.get_position() inset_bbox = [ ax_bbox.x0 + inset_pad * ax_bbox.width, ax_bbox.y1 - (inset_pad + inset_height_ratio) * ax_bbox.height, inset_width_ratio * ax_bbox.width, inset_height_ratio * ax_bbox.height, ] inset_ax = fig.add_axes(inset_bbox) chicago.plot_domain(chicago_poly, sub_domain=domains[chicago_consts.REGIONS[i]], ax=inset_ax) if add_rose: rose_width = 0.45 phi = [((2 * i + 1) * np.pi / 8, np.pi / 4.) for i in range(8)] ax = axs[0, 2] # top right axis ax_bbox = ax.get_position() inset_bbox = [ ax_bbox.x1 - (inset_pad + rose_width) * ax_bbox.width, ax_bbox.y1 - (inset_pad + rose_width) * ax_bbox.height, rose_width * ax_bbox.width, rose_width * ax_bbox.height, ] inset_ax = fig.add_axes(inset_bbox, projection='polar') rose_plot(phi, combinations, ax=inset_ax) if save: fig.savefig('ripley_k_anisotropic_with_inset_%s.png' % ct, dpi=150) fig.savefig('ripley_k_anisotropic_with_inset_%s.pdf' % ct)