/
GridCell.py
97 lines (74 loc) · 3.22 KB
/
GridCell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class GridCell:
"""Grid cell class"""
# We create the density_threshold variable outside of the constructor
# because we want to share this value amongst ALL instantiations of
# the Grid Cell class.
min_den = 1
def __init__(self, min_den, xVals, yVals, xPos, yPos):
"""Constructor for Grid cell class."""
# Instance Variables
self.cluster = -1 # Cluster cell belongs to.
self.items = [] # Array of objects contained in cell.
self.min_den = min_den # Minimum density of cell.
self.xVals = xVals # Tuple containing the minimum and maximum x-values for cell.
self.yVals = yVals # Tuple containing the minimum and maximum y-values for cell.
self.xPos = xPos # X-Position of the cell.
self.yPos = yPos # Y-Position of the cell.
# X-Bin of the cell.
self.xBin = "[" + str(xVals[0]) + ".." + str(xVals[1]) + "]"
# Y-Bin of the cell.
self.yBin = "[" + str(yVals[0]) + ".." + str(yVals[1]) + "]"
def isWithinValueRange(self, data):
"""Checks if an attribute value falls within the min and max value range of cell."""
withinXRange = False
withinYRange = False
if data['x'] >= self.xVals[0] and data['x'] <= self.xVals[1]:
withinXRange = True
if data['y'] >= self.yVals[0] and data['y'] <= self.yVals[1]:
withinYRange = True
return withinXRange and withinYRange
def addItem(self, item, xAttr, yAttr):
"""Adds item to cell."""
item["xVal"] = item[xAttr]
item["yVal"] = item[yAttr]
item[xAttr] = self.xBin
item[yAttr] = self.yBin
self.items.append(item)
def assignToCluster(self, cluster):
"""Assigns cell to a cluster"""
self.cluster = cluster
def getDensityCount(self):
"""Returns a count of the cell's density."""
return len(self.items)
def getPosition(self):
"""Returns the cell's position."""
return [self.xPos, self.yPos]
def isAssignedToCluster(self):
"""Returns whether or not cell is assigned to cluster."""
return self.cluster > 0
def isDense(self):
"""Returns whether or not grid cell is dense."""
return len(self.items) >= self.min_den
def isAdjacentCell(self, cell):
"""Checks if the input dense-cell is adjacent to itself."""
xPos = cell.getPosition()[0]
yPos = cell.getPosition()[1]
# Check if input cell is above cell
if xPos == self.xPos and (yPos - 1) == self.yPos:
return True
# Check if input cell is below cell
if xPos == self.xPos and (yPos + 1) == self.yPos:
return True
# Check if input cell is to the right of cell
if (xPos - 1) == self.xPos and yPos == self.yPos:
return True
# Check if input cell is to the left of cell
if (xPos + 1) == self.xPos and yPos == self.yPos:
return True
return False
def getCellCluster(self):
return self.cluster
def getCellItems(self):
return self.items
def getBins(self):
return { "xBin": self.xBin, "yBin": self.yBin }