def _incidence_matrix(polygon): x_min = round(polygon.bounds[0]) y_min = round(polygon.bounds[1]) x_max = math.ceil(polygon.bounds[2]) y_max = math.ceil(polygon.bounds[3]) incidence = sp.sparse.dok_matrix((x_max * y_max, x_max * y_max), dtype=bool) for tile_y in range(y_min, y_max): for tile_x in range(x_min, x_max): middle_center = tzf.thread_first((tile_x, tile_y), _tile_pt2center_pt, lambda pt: shapely.geometry.Point(*pt)) if not polygon.contains(middle_center): continue middle_ind = _incidence_ind(tile_x, tile_y, x_size=x_max) for move in ['W', 'S', 'A', 'D']: moved_center = _move_projection_center((tile_x, tile_y), move) if polygon.contains(moved_center): moved_tile = tzf.thread_first(moved_center, _shapely_point2pt, _center_pt2tile_pt) moved_ind = _incidence_ind(moved_tile[0], moved_tile[1], x_size=x_max) incidence[middle_ind, moved_ind] = True return incidence.tocsr()
def generate_slices(self, qn, response, vars=[], filt={}): # create the overall filter filt_fmla = u.fmla_for_filt(filt) # subset the rdf as necessary subs = subset_des_wexpr(self.rdf, filt_fmla) if len(filt) > 0 else self.rdf # create a formula for generating the cross-tabs/breakouts across # the selected vars lvl_f = Formula('~%s' % ' + '.join(vars)) if len(vars) > 0 else None # generate the crosstab/breakouts for the selected vars, # turn them into R selector expressions and concatenate # each non-empty selector with the R selector for the outer filter calls = thread_first( rstats.xtabs(lvl_f, subs), rbase.as_data_frame, pandas2ri.ri2py, (pd.DataFrame.query, "Freq > 0"), (pd.DataFrame.get, vars), lambda df: df.apply(lambda z: thread_last( z.to_dict(), lambda y: [(v, y[v]) for v in vars], list, lambda x: [tuple(x[:i + 1]) for i in range(len(x))], ), axis=1), (pd.DataFrame.to_records, False), list, concat, set, map(dict), list) if len(vars) > 0 else [] # setup the formula based on the qn and response # add the base case with empty slice filter # and dicts of qn/resp fmla, slice selector fmla, filt fmla res = [{ 'q': qn, 'r': response, 'f': filt, 's': s } for s in [{}, *calls]] return res
def _predict_action(state): mine = shapely.geometry.Polygon(state['desc']['mine_shell']) obstacles = [shapely.geometry.Polygon(sh) for sh in state['desc']['obstacle_shells']] obstacle = shapely.ops.unary_union(obstacles) situable = mine.difference(obstacle) wrappeds = [shapely.geometry.Polygon(sh) for sh in state['wrapped_shells']] wrapped = shapely.ops.unary_union(wrappeds) not_wrapped = situable.difference(wrapped) if not_wrapped.area < 1.0: return None, state last_move = state.get('last_move', 'W') for move in [last_move, 'W', 'S', 'A', 'D']: proj = _move_projection_center(state['worker']['pos'], move) if not_wrapped.contains(proj): return move, tzd.dissoc(state, 'path_pts_to_not_wrapped') if not state.get('path_pts_to_not_wrapped'): target_tile = tzf.thread_first(not_wrapped.representative_point(), _shapely_point2pt, _snap_to_tile) print('Finding shortest path from tile {} to {}'.format(state['worker']['pos'], target_tile)) if tzd.get_in(['cache', 'incidence_m'], state) is None: incidence_m = _incidence_matrix(situable) state = tzd.assoc_in(state, ['cache', 'incidence_m'], incidence_m) else: incidence_m = state['cache']['incidence_m'] target_vertex_ind = _incidence_ind(target_tile[0], target_tile[1], x_size=math.ceil(situable.bounds[2])) path_dists, path_predecessors = sp.sparse.csgraph.shortest_path(csgraph=incidence_m, directed=False, return_predecessors=True, unweighted=True, indices=target_vertex_ind) start_vertex_ind = _incidence_ind(state['worker']['pos'][0], state['worker']['pos'][1], x_size=math.ceil(situable.bounds[2])) path_inds = _path_inds(path_predecessors, start_vertex_ind) path_pts = [_incidence_pt(ind, x_size=math.ceil(situable.bounds[2])) for ind in path_inds] print('Found path: {}'.format(path_pts)) state = tzd.assoc(state, 'path_pts_to_not_wrapped', path_pts) path_move = _projection_pt_move(state['worker']['pos'], state['path_pts_to_not_wrapped'][0]) if path_move is not None: return path_move, tzd.update_in(state, ['path_pts_to_not_wrapped'], lambda p: p[1:]) return 'Z', state
def test_thread_first(): assert thread_first(2) == 2 assert thread_first(2, inc) == 3 assert thread_first(2, inc, inc) == 4 assert thread_first(2, double, inc) == 5 assert thread_first(2, (add, 5), double) == 14
def _move_projection_center(pos, move): return tzf.thread_first(pos, (_move_projection_tile, move), _tile_pt2center_pt, lambda pt: shapely.geometry.Point(*pt))