def _rec_inner_join_helper(keycols, arr_list): '''All the dtype-wrangling and assertions for rec_inner_join''' assert len(arr_list), 'You must pass a string and one or more record arrays!' assert len(set(keycols)) == len(keycols), 'keycols must not contain duplicates!' #if jointype not in ['inner', 'outer', 'left']: # msg = '{} jointype is not implemented. Only inner, outer, and left join are implemented.' # raise Exception(msg.format(jointype)) names_list = [a.dtype.names for a in arr_list] dtypes_list = lmap(get_rec_dtypes, arr_list) names_and_dtypes_list = [lzip(names, dtypes) for names, dtypes in zip(names_list, dtypes_list)] _nd_dict = dict(zip(names_list[0], dtypes_list[0])) key_dtypes = [_nd_dict[name] for name in keycols] non_key_names_and_dtypes = [[(name, dt) for name, dt in name_dtype_list if name not in keycols] for name_dtype_list in names_and_dtypes_list] non_key_col_names = fL(non_key_names_and_dtypes)[:, :, 0] non_key_dtypes = fL(non_key_names_and_dtypes)[:, :, 1] output_dtype = lzip(keycols, key_dtypes) + flatten(non_key_names_and_dtypes) # Assertions to ensure bad things can't happen: msg = 'Each input array must have all the keycols' assert all([not (set(keycols) - set(arr.dtype.names)) for arr in arr_list]), msg msg = 'All arrays must have the same dtype for all keycols and may not share any other columns in common' _all_names = flatten(names_list) expected_num_cols = len(_all_names) - len(keycols) * (len(arr_list) - 1) assert expected_num_cols == len(output_dtype) == len(set(_all_names)), msg return non_key_col_names, output_dtype
def zipIntoPairs(l, cycle=False, offset=1): '''Form all adjacent pairs from a list If cycle is True, pair the end and beginning as well If offset is greater than 1, pair separated elements''' if cycle: return lzip(l, roll(l, -offset)) else: return lzip(l[:-offset], l[offset:])
def zipIntoPairs(l,cycle=False,offset=1): '''Form all adjacent pairs from a list If cycle is True, pair the end and beginning as well If offset is greater than 1, pair separated elements''' if cycle: return lzip(l,roll(l,-offset)) else: return lzip(l[:-offset],l[offset:])
def infer_transition_matrix_coefficient_from_data( self, source: str, target: str, state: Optional[str] = None, crop: Optional[str] = None, ): """ Infer the distribution of a particular transition matrix coefficient from data. Args: source: The source of the edge corresponding to the matrix element to infer. target: The target of the edge corresponding to the matrix element to infer. state: The state in South Sudan for which the transition matrix coefficient should be calculated. crop: The crop for which the transition matrix coefficient should be calculated. """ rows = engine.execute( f"select * from dssat where `Crop` like '{crop}'" f" and `State` like '{state}'" ) xs, ys = lzip(*[(r["Rainfall"], r["Production"]) for r in rows]) xs_scaled, ys_scaled = xs / np.mean(xs), ys / np.mean(ys) p, V = np.polyfit(xs_scaled, ys_scaled, 1, cov=True) self.edges[source, target]["βs"] = np.random.normal( p[0], np.sqrt(V[0][0]), self.res ) self.sample_from_prior()
def _rec_inner_join_helper(keycols, arr_list): '''All the dtype-wrangling and assertions for rec_inner_join''' assert len( arr_list), 'You must pass a string and one or more record arrays!' assert len( set(keycols)) == len(keycols), 'keycols must not contain duplicates!' #if jointype not in ['inner', 'outer', 'left']: # msg = '{} jointype is not implemented. Only inner, outer, and left join are implemented.' # raise Exception(msg.format(jointype)) names_list = [a.dtype.names for a in arr_list] dtypes_list = lmap(get_rec_dtypes, arr_list) names_and_dtypes_list = [ lzip(names, dtypes) for names, dtypes in zip(names_list, dtypes_list) ] _nd_dict = dict(zip(names_list[0], dtypes_list[0])) key_dtypes = [_nd_dict[name] for name in keycols] non_key_names_and_dtypes = [[(name, dt) for name, dt in name_dtype_list if name not in keycols] for name_dtype_list in names_and_dtypes_list] non_key_col_names = fL(non_key_names_and_dtypes)[:, :, 0] non_key_dtypes = fL(non_key_names_and_dtypes)[:, :, 1] output_dtype = lzip(keycols, key_dtypes) + flatten(non_key_names_and_dtypes) # Assertions to ensure bad things can't happen: msg = 'Each input array must have all the keycols' assert all([not (set(keycols) - set(arr.dtype.names)) for arr in arr_list]), msg msg = 'All arrays must have the same dtype for all keycols and may not share any other columns in common' _all_names = flatten(names_list) expected_num_cols = len(_all_names) - len(keycols) * (len(arr_list) - 1) assert expected_num_cols == len(output_dtype) == len(set(_all_names)), msg return non_key_col_names, output_dtype
def fill_and_plot(points, color_code): xs, ys = lzip(*points) ax.plot(xs, ys, linewidth=0.5, color="grey") ax.fill(xs, ys, color=colors[color_code])
def process_FEWSNET_IPC_data(shpfile: str, title: str): admin_boundaries_shapefile = "data/FEWSNET_World_Admin/FEWSNET_Admin2" sf_admin = shapefile.Reader(admin_boundaries_shapefile) colors = { 0: "white", 1: "#c3e2c3", 2: "#f3e838", 3: "#eb7d24", 4: "#cd2026", 5: "#5d060c", 66: "aqua", 88: "white", 99: "white", } sf = shapefile.Reader(shpfile) fig, ax = plt.subplots(figsize=(12, 12)) ax.set_aspect("equal") ax.set_title(title) plt.style.use("ggplot") def fill_and_plot(points, color_code): xs, ys = lzip(*points) ax.plot(xs, ys, linewidth=0.5, color="grey") ax.fill(xs, ys, color=colors[color_code]) fs_polygons = [] for i, sr in tqdm(enumerate(sf.shapeRecords())): nparts = len(sr.shape.parts) parts, points = sr.shape.parts, sr.shape.points CS = int(sr.record[0]) if nparts == 1: # fill_and_plot(points, CS) fs_polygons.append((Polygon(points), int(sr.record[0]))) else: for ip, part in enumerate(parts): if ip < nparts - 1: i1 = parts[ip + 1] - 1 else: i1 = len(points) # fill_and_plot(points[part : i1 + 1], CS), fs_polygons.append( (Polygon(points[part : i1 + 1]), int(sr.record[0])) ) south_sudan_srs = [ sr for sr in sf_admin.shapeRecords() if sr.record[3] == "South Sudan" ] lines = [] for sr in tqdm(south_sudan_srs, desc="South Sudan Counties"): county_polygon = Polygon(sr.shape.points) for fs_polygon in tqdm(fs_polygons, desc="fs_polygons"): if county_polygon.buffer(-0.05).intersects(fs_polygon[0]): centroid = county_polygon.centroid ax.text( centroid.x, centroid.y, sr.record[8], fontsize=6, horizontalalignment="center", ) xs, ys = lzip(*sr.shape.points) CS = int(fs_polygon[1]) fill_and_plot(sr.shape.points, CS) lines.append( "\t".join([str(x) for x in sr.record] + [str(CS)]) ) with open("ipc_data.tsv", "w") as f: f.write("\n".join(lines)) plt.savefig("shape.pdf")
def ziptranspose(l): '''Tranpose the two outer dimensions of a nest list (This is just a wrapper around zip(*l) that returns a list)''' return lzip(*l)
def fill_and_plot(points, sr): xs, ys = lzip(*points) ax.plot(xs, ys, linewidth=0.5, color="grey") ax.fill(xs, ys, color=colors[int(sr.record[0])])