for ct in crime_types: print "Started %s, getting data..." % ct data, t0, cid = cad.get_crimes_from_dump('monsuru_cad_%s' % ct) cid = np.array(cid) # filter by end date idx = np.where(data[:, 0] < (MAX_DAY_NUMBER + 1))[0] data = data[idx] cid = cid[idx] cids[ct] = cid jiggled = cad.jiggle_all_points_on_grid(data[:, 1], data[:, 2]) b_jiggled = [np.all(a != b) for a, b in zip(data[:, 1:], jiggled)] snapped, fail = NetworkData.from_cartesian(net, jiggled, return_failure_idx=True) keep_idx = [k for k in range(len(cid)) if k not in fail] cid = cid[keep_idx] times = data[keep_idx, 0] failed[ct] = fail print "Done. writing data to a file." fields = [ 'my_idx', 'original_idx', 'days_since_1_mar_2011', 'x', 'y', 'jiggled?' ] xy = snapped.to_cartesian() out_data = [] for k in range(cid.size): out_data.append(
from data.models import CartesianData, DataArray, NetworkData from matplotlib import pyplot as plt itn_net = load_test_network() nodes = np.array([t['loc'] for t in itn_net.g.node.values()]) xmin, ymin, xmax, ymax = itn_net.extent targets, n_per_edge = utils.network_walker_uniform_sample_points( itn_net, 10) # lay down some random points within that box num_pts = 100 x_pts = np.random.rand(num_pts) * (xmax - xmin) + xmin y_pts = np.random.rand(num_pts) * (ymax - ymin) + ymin xy = CartesianData.from_args(x_pts, y_pts) sources = NetworkData.from_cartesian( itn_net, xy, grid_size=50) # grid_size defaults to 50 # not all points necessarily snap successfully num_pts = sources.ndata radius = 200. nw = utils.NetworkWalker(itn_net, targets, max_distance=radius, max_split=1e4) k = NetworkKernelEqualSplitLinear(sources.getone(0), 200.) k.set_walker(nw) z = k.pdf() zn = z / max(z) # plt.figure() # itn_net.plot_network()
def create_network_with_crime_counts( start_date=datetime.date(2011, 3, 1), domain_name='South'): # load network, count crimes in 6mo and 12mo window, output shapefile domains = chicago.get_chicago_side_polys() domain = domains[domain_name] end_date = start_date + datetime.timedelta(days=365) crime_types = ( 'THEFT', 'BURGLARY', 'HOMICIDE', 'BATTERY', 'ARSON', 'MOTOR VEHICLE THEFT', 'ASSAULT', 'ROBBERY', ) time_window_filters = { '6mo': lambda t: t <= 183, '12mo': lambda t: t <= 365, } # get crime data data, t0, cid = chicago.get_crimes_by_type(crime_type=crime_types, start_date=start_date, end_date=end_date, domain=domain) # get network osm_file = os.path.join( DATA_DIR, 'osm_chicago', '%s_clipped.net' % consts.FILE_FRIENDLY_REGIONS[domain_name]) net = osm.OSMStreetNet.from_pickle(osm_file) # snap crime data to network with maximum distance cutoff netdata, failed = NetworkData.from_cartesian(net, data[:, 1:], radius=50, return_failure_idx=True) # get non-failed times idx = sorted(set(range(data.shape[0])) - set(failed)) netdata = DataArray(data[idx, 0]).adddim(netdata, type=NetworkSpaceTimeData) # run over edges, count crimes in the two time windows filters = {} for filt_name, filt_func in time_window_filters.items(): filters[filt_name] = filt_func(netdata.toarray(0)).astype(int) # add count attributes to all edges for e in net.edges(): e.attrs['crimes_6mo'] = 0 e.attrs['crimes_12mo'] = 0 edge_counts = {} for i, t in enumerate(netdata.space.toarray()): t.edge.attrs['crimes_6mo'] += filters['6mo'][i] t.edge.attrs['crimes_12mo'] += filters['12mo'][i] net.save(consts.FILE_FRIENDLY_REGIONS[domain_name] + '_network_crime_counts', fmt='shp')