Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions emptySpace/emptyspace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial import distance
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
Expand Down Expand Up @@ -73,24 +71,20 @@ def scale(self):

scaled_ghost = scaled_data[len(self.data) : ]
return_scaled_data = scaled_data[:len(self.data)]
return return_scaled_data, scaled_ghost
return return_scaled_data.tolist(), scaled_ghost.tolist()


def plot(self):
if self.data.shape[1] == 2:
ax = self.gabriel.plot(editable_outside=True)
for coord_pair in self.ghost_points:
ax.scatter(coord_pair[0], coord_pair[1], marker="*")
plt.show()
elif self.data.shape[1] == 3:
ax = self.gabriel.plot(editable_outside=True)
for coords in self.ghost_points:
ax.scatter(coords[0], coords[1], coords[2], marker="*")
plt.show()

def plot_scaled(self, scaled_data, scaled_ghost):
fig = plt.figure()
ax = fig.add_subplot(111)
[ax.scatter(data_point[0], data_point[1], c='red', marker='o') for data_point in scaled_data]
[ax.scatter(ghost_point[0], ghost_point[1], c='blue', marker='x') for ghost_point in scaled_ghost]
plt.show()
def toReactVisFormat(self, data, sizes=None):
"""This function returns a list with data formatted for a react-vis chart

Args:
data (ndarray): the data to re-format
"""
rv_list = list()
if sizes!=None:
for idx, coord_set in enumerate(data):
rv_list.append({'x':coord_set[0], 'y':coord_set[1], 'size':sizes[idx]})
else:
for coord_set in data:
rv_list.append({'x':coord_set[0], 'y':coord_set[1]})
return rv_list

94 changes: 2 additions & 92 deletions emptySpace/gabriel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Class for generating a gabriel graph from a dataset or delaunay triangulation
# NOTE: this will apply only to 2d datasets and will eventually be extended to nd
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.patches import Circle
from scipy.spatial import Delaunay, distance
from math import sqrt
Expand Down Expand Up @@ -68,16 +66,8 @@ def generate_gabriel(self, interactive=False):

self.delaunay_graph = Delaunay(self.data)
self.__generate_point_graph()
if interactive:
# TODO change this back
# self.__prune_edges_interactive()
self.plot()
self.__prune_edges()
self.__remove_edges()
self.plot()
else:
self.__prune_edges()
self.__remove_edges()
self.__prune_edges()
self.__remove_edges()

def __generate_point_graph(self):
"""This function will generate a graph of points and their edges
Expand Down Expand Up @@ -121,96 +111,16 @@ def __is_valid_edge(self, point1, point2):
return False
return True

def __is_valid_edge_interactive(self, ax, point1, point2):
diameter = distance.euclidean(point1.coordinates, point2.coordinates)
radius = diameter / 2.0
center = self.get_center(point1, point2)
for temp_point in self.point_graph:
if temp_point is not point1 and temp_point is not point2:
if distance.euclidean(center, temp_point.coordinates) < radius:
print(f"edge from {point1.p_id} to {point2.p_id} is removed")
return False
print(f"edge from {point1.p_id} to {point2.p_id} is valid")
return True

def __draw_circle(self, point1, point2, ax):
diameter = distance.euclidean(point1.coordinates, point2.coordinates)
radius = diameter / 2.0
center = self.get_center(point1, point2)
circle = Circle(center, radius=radius, fill=False, linewidth=1, linestyle='solid')
ax.add_artist(circle)
plt.draw()
return circle

def __prune_edges(self):
for temp_point in self.point_graph:
for point in temp_point.edges:
if not self.__is_valid_edge(temp_point, point):
temp_point.remove_edge(point)

def __prune_edges_interactive(self):
fig = plt.figure()
ax = fig.add_subplot(111)
self.__plot_nodes(ax)
self.__plot_edges(ax)
plt.show(block=False)
for temp_point in self.point_graph:
for point in temp_point.edges:
circle = self.__draw_circle(temp_point, point, ax)
while plt.waitforbuttonpress() is False:
pass
if not self.__is_valid_edge_interactive(ax, temp_point, point):
print(f"removing edge from point {temp_point.coordinates} to {point.coordinates}")
temp_point.remove_edge(point)
circle.remove()
plt.draw()

def __remove_edges(self):
"""This function removes edges marked for removal
"""
for point in self.point_graph:
point.edges[:] = [edge for edge in point.edges if not point.removed_edges[edge]]


def plot(self, editable_outside=False):
fig = None
ax = None
if self.n_dim > 3:
print("data must be 2-D or less to plot")
return
elif self.n_dim == 3:
fig = plt.figure()
ax = plt.axes(projection='3d')
elif self.n_dim == 2:
fig = plt.figure()
ax = fig.add_subplot(111)

self.__plot_nodes(ax)
self.__plot_edges(ax)

if editable_outside:
return ax
else:
plt.show()

def __plot_nodes(self, ax):
if self.n_dim == 3:
for temp_point in self.point_graph:
ax.scatter3D(temp_point.coordinates[0], temp_point.coordinates[1], temp_point.coordinates[2])
elif self.n_dim ==2:
for temp_point in self.point_graph:
ax.scatter(temp_point.coordinates[0], temp_point.coordinates[1], zorder=2)

def __plot_edges(self, ax):
if self.n_dim == 2:
for temp in self.point_graph:
for edge in temp.edges:
if not temp.removed_edges[edge]:
xs, ys = zip(temp.coordinates, edge.coordinates)
ax.plot(xs, ys, zorder=1)
if self.n_dim == 3:
for temp in self.point_graph:
for edge in temp.edges:
if not temp.removed_edges[edge]:
xs, ys, zs = zip(temp.coordinates, edge.coordinates)
ax.plot(xs, ys, zs, zorder=1)
Loading