Skip to content

new updates + cases tested #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
74 changes: 65 additions & 9 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def read_geojson(input_file):
"""
# Please use the python json module (imported above)
# to solve this one.
gj = None
with open(input_file, 'r') as f:
gj = json.load(f)
return gj


Expand All @@ -56,8 +57,13 @@ def find_largest_city(gj):
population : int
The population of the largest city
"""
city = None
temp = gj['features']
city = ""
max_population = 0
for n in temp:
if (n['properties']['pop_max'] > max_population):
max_population = n['properties']['pop_max']
city = n['properties']['name']

return city, max_population

Expand All @@ -74,7 +80,15 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return

temp = gj['features']
citiesInArizona = [];
for n in temp:
if (n['properties']['adm1name'] == "Arizona"):
citiesInArizona.append(n['properties']['name'])

return citiesInArizona


def mean_center(points):
"""
Expand All @@ -93,8 +107,15 @@ def mean_center(points):
y : float
Mean y coordinate
"""
x = None
y = None
xSum = 0
ySum = 0

for n in points:
xSum += n[0]
ySum += n[1]

x = xSum/len(points)
y = ySum/len(points)

return x, y

Expand All @@ -119,7 +140,21 @@ def average_nearest_neighbor_distance(points):
Measure of Spatial Relationships in Populations. Ecology. 35(4)
p. 445-453.
"""
mean_d = 0
listOfDistances = []

for n in points:
lowest = 100
for x in points:
if euclidean_distance(n,x)!= 0 and euclidean_distance(n,x) < lowest:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the points are coincident (and valid)? You might have a distance of 0. You have two for loops, so you could check to see if the position (index) in the first for loop is the same as the index in the second. Check out enumerate to help do this.

lowest = euclidean_distance(n,x)
listOfDistances.append(lowest)

totalOfDistances = 0

for z in listOfDistances:
totalOfDistances += z

mean_d = totalOfDistances/len(points)

return mean_d

Expand All @@ -138,8 +173,22 @@ def minimum_bounding_rectangle(points):
: list
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

mbr = [0,0,0,0]
minX = 100
minY = 100
maxX = 0
maxY = 0

for n in points:
if n[0] < minX:
minX = n[0]
if n[0] > maxX:
maxX = n[0]
if n[1] < minY:
minY = n[1]
if n[1] > maxY:
maxY = n[1]

mbr = [minX,minY,maxX,maxY]

return mbr

Expand All @@ -148,8 +197,13 @@ def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""

area = 0

length = mbr[3]-mbr[1]
width = mbr[2] - mbr[0]

area = length * width
return area


Expand All @@ -172,8 +226,10 @@ def expected_distance(area, n):
n : int
The number of points
"""

expected = 0
expected = 0.5 * (math.sqrt(area/n))


return expected


Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_write_your_own(self):
Here you will write a test for the code you write in
point_pattern.py.
"""
some_return = point_pattern.write_your_own(self.gj)
self.assertTrue(False)
cities = point_pattern.write_your_own(self.gj)
self.assertEqual(cities[0], 'Glendale')

class TestIterablePointPattern(unittest.TestCase):
"""
Expand Down