Skip to content

master #21

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
77 changes: 68 additions & 9 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ 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


def find_largest_city(gj):
"""
Iterate through a geojson feature collection and
Expand All @@ -56,8 +56,19 @@ def find_largest_city(gj):
population : int
The population of the largest city
"""
city = None
max_population = 0
list_cities = []
list_pop = []

for d in kj['features']:
pop_max = d['properties']['pop_max']
citys = d['properties']['ls_name']
list_cities.append(citys)
list_pop.append(pop_max)

max_population = max(list_pop)
index_pop = list_pop.index(max_population)

city = list_cities[index_pop]

return city, max_population

Expand All @@ -74,7 +85,22 @@ def write_your_own(gj):
Do not forget to write the accompanying test in
tests.py!
"""
return
list_cities2 = []
home_state = []

for s in gj['features']:
citys2 = s['properties']['ls_name']
statenm = s['properties']['adm1name']
list_cities2.append(citys2)
home_state.append(statenm)

v = raw_input('Type a city name to return its state...')
citynumber = list_cities2.index(v)
stateans = home_state[citynumber]

return stateans



def mean_center(points):
"""
Expand All @@ -93,13 +119,19 @@ def mean_center(points):
y : float
Mean y coordinate
"""
x = None
y = None
x = 0
y = 0
for coor in points:
x += coor[0]/len(points)
y += coor[1]/len(points)

return x, y

sub = [(1,2), (3,4)]


def average_nearest_neighbor_distance(points):

"""
Given a set of points, compute the average nearest neighbor.

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

return mean_d
for p_one in points:
distance = []
for p_two in points:
if math.sqrt((p_one[0] - p_two[0])**2 + (p_one[1] - p_two[1])**2)==0:
continue
distance.append(math.sqrt((p_one[0] - p_two[0])**2 + (p_one[1] - p_two[1])**2))
shortest_path.append(min(distance))

mean_d = sum(shortest_path)/len(shortest_path)
return mean_d

def minimum_bounding_rectangle(points):
"""
Expand All @@ -139,7 +180,18 @@ def minimum_bounding_rectangle(points):
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

mbr = [0,0,0,0]
mbr = []
x_list = []
y_list = []

for point in points:
x_list.append(point[0])
y_list.append(point[1])

mbr.append(min(x_list))
mbr.append(min(y_list))
mbr.append(max(x_list))
mbr.append(max(y_list))

return mbr

Expand All @@ -150,6 +202,11 @@ def mbr_area(mbr):
"""
area = 0

length = abs(mbr[0] - mbr[2])
width = abs(mbr[1] - mbr[3])

area = length * width

return area


Expand All @@ -174,6 +231,8 @@ def expected_distance(area, n):
"""

expected = 0

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


Expand Down
29 changes: 17 additions & 12 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ def test_write_your_own(self):
"""
Here you will write a test for the code you write in
point_pattern.py.

Teagan: how would I test a function that used user input?
"""
some_return = point_pattern.write_your_own(self.gj)
self.assertTrue(False)


class TestIterablePointPattern(unittest.TestCase):
"""
This set of tests is focused on iterables and sequences. Once
passing, you will have the foundation of some point pattern analysis
functionality.
"""

# This is like the standard setup, except it is only called once
@classmethod
def setUpClass(cls):
# Seed a random number generator so we get the same random values every time
random.seed(12345)
# A list comprehension to create 50 random points
cls.points = [(random.randint(0,100), random.randint(0,100)) for i in range(50)]
cls.points = [(random.randint(0, 100), random.randint(0, 100)) for i in range(50)]

def test_average_nearest_neighbor_distance(self):
mean_d = point_pattern.average_nearest_neighbor_distance(self.points)
Expand All @@ -65,10 +69,10 @@ def test_mean_center(self):

def test_minimum_bounding_rectangle(self):
mbr = point_pattern.minimum_bounding_rectangle(self.points)
self.assertEqual(mbr, [0,0,94,98])
self.assertEqual(mbr, [0, 0, 94, 98])

def test_mbr_area(self):
mbr = [0,0,94,98]
mbr = [0, 0, 94, 98]
area = point_pattern.mbr_area(mbr)
self.assertEqual(area, 9212)

Expand All @@ -84,6 +88,7 @@ class TestPointPattern(unittest.TestCase):
These are the tests that you got working in assignment 3.
You should not need to alter these at all.
"""

def setUp(self):
pass

Expand All @@ -97,7 +102,7 @@ def test_getx(self):
`getx` function so that the correct
values are returned.
"""
point = (1,2)
point = (1, 2)
x = point_pattern.getx(point)
self.assertEqual(1, x)

Expand All @@ -110,7 +115,7 @@ def test_gety(self):
`gety` function so that the correct
values are returned.
"""
point = (3,2.5)
point = (3, 2.5)
y = point_pattern.gety(point)
self.assertEqual(2.5, y)

Expand All @@ -119,13 +124,13 @@ def test_shift_point(self):
Test that a point is being properly shifted
when calling point_pattern.shift_point
"""
point = (0,0)
point = (0, 0)
new_point = point_pattern.shift_point(point, 3, 4)
self.assertEqual((3,4), new_point)
self.assertEqual((3, 4), new_point)

point = (-2.34, 1.19)
new_point = point_pattern.shift_point(point, 2.34, -1.19)
self.assertEqual((0,0), new_point)
self.assertEqual((0, 0), new_point)

def test_euclidean_distance(self):
"""
Expand Down Expand Up @@ -208,11 +213,11 @@ def test_check_in(self):
"""
As above, update the function in point_pattern.py
"""
point_list = [(0,0), (1,0.1), (-2.1, 1),
(2,4), (1,1), (3.5, 2)]
point_list = [(0, 0), (1, 0.1), (-2.1, 1),
(2, 4), (1, 1), (3.5, 2)]

inlist = point_pattern.check_in((0,0), point_list)
inlist = point_pattern.check_in((0, 0), point_list)
self.assertTrue(inlist)

inlist = point_pattern.check_in((6,4), point_list)
inlist = point_pattern.check_in((6, 4), point_list)
self.assertFalse(inlist)