def test_face_hash(self): """Test Event Hash""" dst_1 = space_time.generate_flat_spacetime(2, 2) f0_1, *_ = face.faces(dst_1) dst_2 = space_time.generate_flat_spacetime(2, 2) f0_2, *_ = face.faces(dst_2) assert hash(f0_1) == hash(f0_2)
def decrease(st, node): """merges node.left in to node""" # cut out the sub_space that will be effected by the move left = node.left sub_space = st.pop([node, left]) # identify the nodes that will be merged in the sub_space left_s = event.Event(sub_space, left.key) node_s = event.Event(sub_space, node.key) # ------- modify the nodes ------- # the new neighbors of the merged node new_future = set(left_s.future).union(node_s.future) new_past = set(left_s.past).union(node_s.past) new_left = left_s.left # connect new neighbors to node and remove connections from left event.connect_spatial(new_left, node_s) event.connect_temporal(node_s, past=new_past, future=new_future) event.connect_temporal(left_s, past=set(), future=set()) # ------- modify the faces ------- # only two faces are "removed" while some others need to be relabeled. faces_containing_left = [] faces_for_deletion = [] for f in face.faces(sub_space): if left_s.key in f.nodes: if node.key in f.nodes: faces_for_deletion.append(f) else: faces_containing_left.append(f) for f in face.faces(sub_space, faces_containing_left): new_f_nodes = set(f.nodes) new_f_nodes.remove(left_s.key) new_f_nodes.add(node_s.key) sub_space.face_nodes[f.key] = frozenset(new_f_nodes) # connect the newly adjacent faces and remove the old face that used to be between them for f in face.faces(sub_space, faces_for_deletion): face.connect_spatial(f.left, f.right) sub_space.remove_face(f.key) sub_space.remove_key(left_s.key) # push the modified sub_space back in to the space_time st.push(sub_space)
def test_face_pass_thru_getattr(self): """Test event getattr behavior for passthru attributes""" dst = space_time.generate_flat_spacetime(3, 3) face_list = [5, 2, 10] e0, e1, e2 = face.faces(dst, face_list) assert e0.left == e1 assert e1.right == e0 assert e0.temporal_neighbor == e2 assert e0.type == 1
def plot_2d(st, offset=2 * pi / 600., get_coords=get_naive_coords, labels=False): theta_x, theta_t = get_coords(st) coords = {} for n in event.events(st, st.nodes): v = theta_x[n.key] u = theta_t[n.key] coords[n.key] = (v, u) face_coordinate = {} face_is_display = {} for face in Face.faces(st): face_lst = list(face.nodes) xx, yy = [], [] for n in face_lst: xx.append(theta_x[n]) yy.append(theta_t[n]) avg_x = np.mean(xx) avg_y = np.mean(yy) xx = [p - (p - avg_x) / 2 for p in xx] yy = [p - (p - avg_y) / 2 for p in yy] face_coordinate[face] = (avg_x, avg_y) face_is_display[face] = False if max([abs(theta_t[face_lst[0]] - theta_t[face_lst[1]]), abs(theta_t[face_lst[0]] - theta_t[face_lst[2]])]) < pi: if max([abs(theta_x[face_lst[0]] - theta_x[face_lst[1]]), abs(theta_x[face_lst[0]] - theta_x[face_lst[2]])]) < pi: face_is_display[face] = True c = (.5, 0, 0, .6) if face.type == 0: c = (0, 0, .5, .6) plt.fill(xx, yy, c=c) if labels: plt.annotate(face.key, (avg_x, avg_y), va="center", ha="center", c="white") face_right_connections = [] face_left_connections = [] face_time_connections = [] for face in Face.faces(st): x, y = face_coordinate[face] x_r, y_r = face_coordinate[face.right] x_l, y_l = face_coordinate[face.left] x_t, y_t = face_coordinate[face.temporal_neighbor] # right if face_is_display[face] and face_is_display[face.right]: if abs(y - y_r) < 10: if abs(x - x_r) < 10: face_right_connections.append([(x, y-offset), (x_r, y_r-offset), ]) # left if face_is_display[face] and face_is_display[face.left]: if abs(y - y_l) < 10: if abs(x - x_l) < 10: face_left_connections.append([(x, y+offset), (x_l, y_l+offset), ]) # temporal if face_is_display[face] and face_is_display[face.temporal_neighbor]: if abs(y - y_t) < 10: if abs(x - x_t) < 10: if face.type == 0: face_time_connections.append([(x+offset/2., y), (x_t+offset/2., y_t), ]) else: face_time_connections.append([(x-offset/2., y), (x_t-offset/2., y_t), ]) edges_past_pointing, edges_future_pointing, edges_left_pointing, edges_right_pointing = [], [], [], [] for e in event.events(st, st.nodes): past_asj = None for adjacent in e.past: if past_asj == adjacent: print(adjacent) past_asj = adjacent if abs(theta_t[e.key] - theta_t[adjacent.key]) < pi: if abs(theta_x[e.key] - theta_x[adjacent.key]) < pi: edges_past_pointing.append([coords[e.key] + np.array([0, offset]), coords[adjacent.key] + np.array([0, offset]), ]) for adjacent in e.future: if abs(theta_t[e.key] - theta_t[adjacent.key]) < pi: if abs(theta_x[e.key] - theta_x[adjacent.key]) < pi: edges_future_pointing.append([coords[e.key] - np.array([0, offset]), coords[adjacent.key] - np.array([0, offset]), ]) if abs(theta_t[e.key] - theta_t[e.left.key]) < pi: if abs(theta_x[e.key] - theta_x[e.left.key]) < pi: edges_left_pointing.append([coords[e.key] + np.array([0, offset]), coords[e.left.key] + np.array([0, offset]), ]) if abs(theta_t[e.key] - theta_t[e.right.key]) < pi: if abs(theta_x[e.key] - theta_x[e.right.key]) < pi: edges_right_pointing.append([coords[e.key] + np.array([0, -offset]), coords[e.right.key] + np.array([0, -offset]), ]) plt.gca().add_collection(LineCollection(edges_future_pointing, color=(1, 0, 0, 1), antialiaseds=True, linewidth=0.6, )) plt.gca().add_collection(LineCollection(edges_past_pointing, color=(0, 0, 1, 1), antialiaseds=True, linewidth=0.6, )) plt.gca().add_collection(LineCollection(edges_left_pointing, color=(0, 1, 0, 1), antialiaseds=True, linewidth=0.6, )) plt.gca().add_collection(LineCollection(edges_right_pointing, color=(.5, 0, .5, 1), antialiaseds=True, linewidth=0.6, )) plt.gca().add_collection(LineCollection(face_right_connections, color=(1, 0, 0, 1), antialiaseds=True, linewidth=.6, )) plt.gca().add_collection(LineCollection(face_left_connections, color=(0, 0, 1, 1), antialiaseds=True, linewidth=.6, )) plt.gca().add_collection(LineCollection(face_time_connections, color=(0, 1, 0, 1), antialiaseds=True, linewidth=.6, )) s = 100 if labels == True: for n in st.nodes: plt.annotate(n, coords[n], va="center", ha="center", c="black") s = 600 x = [coords[n][0] for n in st.nodes] y = [coords[n][1] for n in st.nodes] plt.scatter(x, y, color="white", zorder=2, s=s, edgecolors="black") plt.axis("off") plt.show()
def test_neighbors(self): """Test neighbors""" dst = space_time.generate_flat_spacetime(3, 3) face_list = [5, 2, 3, 15] e0, e1, e2, e3 = face.faces(dst, face_list) assert e1.neighbors == {e0, e2, e3}
def test_spatial_neighbors(self): """Test spatial neighbors""" dst = space_time.generate_flat_spacetime(3, 3) face_list = [5, 2, 3] e0, e1, e2 = face.faces(dst, face_list) assert e1.spatial_neighbors == {e0, e2}
def increase(st, node, future, past): """ A move should add one node and 2 faces. we can pop all the structures to be modified out of the dicts and then push them back in once they've been modified. This mean we need to know what could get modfified in any given move. """ # remove the sub_space that is going to be modified layer = node.layer sub_space = st.pop([node]) future_s = event.Event(sub_space, future) # Need these two because they have been "popped" out of the original spacetime past_s = event.Event(sub_space, past) # increment the total node counter new_node_num = max(st.nodes.union(sub_space.nodes)) + 1 sub_space.add_key(new_node_num, layer=layer) # create a node object for easy manipulation. This also automatically adds the node to the sub_space new_s = event.Event(sub_space, new_node_num) node_s = event.Event(sub_space, node) left_s = event.Event(sub_space, node_s.left) left = node_s.left right = node_s.right # spatial changes event.connect_spatial(new_s, node_s) # new_s.right = node_s and node_s.left = new_s event.connect_spatial(left_s, new_s) # new_s.left = left_s and left_s.right = new_s # future changes # TODO examine algorithm concept of connection vs Spacetime (e.g. after popping a node out, what does asking for "left" mean?) new_future_set = {future_s} f = future_s.left while f in node_s.future and not f.is_gluing_point: new_future_set.add(f) sub_space.node_future[node.key].remove(f.key) # TODO cleanup the event key coercion by figuring out workaround for node.future.remove() sub_space.node_past[f.key].remove(node.key) f = f.left event.connect_temporal(new_s, future=new_future_set) old_future_set = node_s.future.difference(new_future_set).union({future_s}) event.connect_temporal(node_s, future=old_future_set) # sub_space.node_past[future].append(new_node) # past changes new_past_set = {past_s} p = past_s.left while p in node_s.past: new_past_set.add(p) sub_space.node_past[node_s.key].remove(p.key) sub_space.node_future[p.key].remove(node_s.key) p = p.left event.connect_temporal(new_s, past=new_past_set) old_past_set = node_s.past.difference(new_past_set).union({past_s}) event.connect_temporal(node_s, past=old_past_set) # sub_space.node_future[past].append(new_node) # face changes for f in face.faces(sub_space): if node_s.key in f.nodes: modified = [i.key for i in list(new_future_set | new_past_set | {node_s, left})] if all(item in modified for item in f.nodes): new_nodes = set(f.nodes) new_nodes.remove(node_s.key) new_nodes.add(new_s.key) sub_space.face_nodes[f.key] = frozenset(new_nodes) sub_space.faces_containing[node_s.key].remove(f.key) sub_space.faces_containing[new_s.key].add(f.key) f1r = face.Face(sub_space, (set(sub_space.faces_containing[new_s.key]) & set(sub_space.faces_containing[future_s.key])).pop()) f1l = face.Face(sub_space, (set(sub_space.faces_containing[node_s.key]) & set(sub_space.faces_containing[future_s.key])).pop()) f2r = face.Face(sub_space, (set(sub_space.faces_containing[new_s.key]) & set(sub_space.faces_containing[past_s.key])).pop()) f2l = face.Face(sub_space, (set(sub_space.faces_containing[node_s.key]) & set(sub_space.faces_containing[past_s.key])).pop()) new_face_key = max(st.faces.union(sub_space.faces)) + 1 f_new_1 = face.Face(sub_space, sub_space.add_face(frozenset({new_s.key, node_s.key, future_s.key}), new_face_key)) sub_space.face_type[f_new_1.key] = 0 f_new_2 = face.Face(sub_space, sub_space.add_face(frozenset({new_s.key, node_s.key, past_s.key}), new_face_key + 1)) sub_space.face_type[f_new_2.key] = 1 face.connect_spatial(f1r, f_new_1) face.connect_spatial(f_new_1, f1l) face.connect_spatial(f2r, f_new_2) face.connect_spatial(f_new_2, f2l) # face.connect_temporal(f_new_1, f_new_2) st.push(sub_space)