Skip to content
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

Assignment 6 #17

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ python:
- "3.5"

#command to run tests
script: nosetests
script: nosetests
Binary file added __pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/analytics.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/io_geojson.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/point.cpython-35.pyc
Binary file not shown.
Binary file added __pycache__/utils.cpython-35.pyc
Binary file not shown.
228 changes: 228 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
from .utils import *

def find_largest_city(gj):
"""
Iterate through a geojson feature collection and
find the largest city. Assume that the key
to access the maximum population is 'pop_max'.

Parameters
----------
gj : dict
A GeoJSON file read in as a Python dictionary

Returns
-------
city : str
The largest city

population : int
The population of the largest city
"""
city = None
max_population = 0
for n in gj["features"]:
properties = n["properties"]
if (properties["pop_max"] > max_population):
max_population = properties["pop_max"]
city = properties["adm1name"]

return city, max_population


def alaska_points(gj):
# Find coordinates from Alaska
alaska_points = []
for n in gj["features"]:
properties = n["properties"]
state_name = properties["adm1name"]
if state_name == "Alaska":
alaska_points.append(n)
else:
continue

return alaska_points


def mean_center(points):
"""
Given a set of points, compute the mean center

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
x : float
Mean x coordinate

y : float
Mean y coordinate
"""
x = 0
y = 0
number_of_points = 0

for p in points:
x += p[0]
y += p[1]
number_of_points += 1

x = x / number_of_points
y = y / number_of_points

return x, y


def average_nearest_neighbor_distance(points, mark=None):
"""
Given a set of points, compute the average nearest neighbor.

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
mean_d : float
Average nearest neighbor distance

References
----------
Clark and Evan (1954 Distance to Nearest Neighbor as a
Measure of Spatial Relationships in Populations. Ecology. 35(4)
p. 445-453.
"""
mean_d = 0
temp_p = None

if mark == None:
for p in points:
for q in points:
if check_coincident(p, q):
continue
cached = euclidean_distance(p, q)
if temp_p is None:
temp_p = cached
elif temp_p > cached:
temp_p = cached

mean_d += temp_p
temp_p = None
else:
for p in points:
if p.mark == mark:
for q in points:
if check_coincident(p, q):
continue
cached = euclidean_distance(p, q)
if temp_p is None:
temp_p = cached
elif temp_p > cached:
temp_p = cached

mean_d += temp_p
temp_p = None





return mean_d / len(points)


def minimum_bounding_rectangle(points):
"""
Given a set of points, compute the minimum bounding rectangle.

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
: list
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

min_x = 1000000000
min_y = 1000000000
max_x = -1
max_y = -1

for n in points:
if n[0] < min_x:
min_x = n[0]
if n[0] > max_x:
max_x = n[0]
if n[1] < min_y:
min_y = n[1]
if n[1] > max_y:
max_y = n[1]

mbr = [min_x, min_y, max_x, max_y]

return mbr


def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = ((mbr[2] - mbr[0]) * (mbr[3] - mbr[1]))

return area

def expected_distance(area, n):
"""
Compute the expected mean distance given
some study area.

This makes lots of assumptions and is not
necessarily how you would want to compute
this. This is just an example of the full
analysis pipe, e.g. compute the mean distance
and the expected mean distance.

Parameters
----------
area : float
The area of the study area

n : int
The number of points
"""

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


def create_random(n, mark=None):
random.seed()
random_points = [(random.randint(0,100), random.randint(0,100), mark) for i in range(n)]
return random_points


def permutations(p=99, n=100):
#Compute the mean nearest neighbor distance
permutationz = []
for i in range(p):
permutationz.append(average_nearest_neighbor_distance(create_random(n)))
return permutationz


def compute_critical(points):
lower_bound = min(points)
upper_bound = max(points)
return lower_bound, upper_bound


def check_significant(lower, upper, observed):
if observed > upper:
return True
if observed < lower:
return True
26 changes: 26 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from urllib.request import urlopen

def read_geojson(input_url):
"""
Read a geojson file

Parameters
----------
input_file : str
The PATH to the data to be read

Returns
-------
gj : dict
An in memory version of the geojson
"""
# I still can't seem to open json locally so going the url route
# for now until I figure it out!
# with open(input_file, 'r') as f:
# gj = json.load(f)
response = urlopen(input_url).read().decode('utf8') #For Testing purposes
# response = urlopen("https://api.myjson.com/bins/4587l").read().decode('utf8')
gj = json.loads(response)

return gj
20 changes: 20 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from utils import *

class Point(object):
def __init__(self, x, y, mark={}):
self.x = x
self.y = y
self.mark = mark

def check_coincident(self, other_point):
return check_coincident((self.x, self.y), (other_point.x, other_point.y))

def shift_point(self, x_shift, y_shift):
return shift_point((self.x, self.y), x_shift, y_shift)

def create_random_marked_points(n, marks=[]):
list_o_random_points = []
for i in range(n):
random_point = Point(random.seed, random.seed, random.choice(marks))
list_o_random_points.append(random_point)
return list_o_random_points
Binary file added tests/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file added tests/__pycache__/functional_test.cpython-35.pyc
Binary file not shown.
Binary file added tests/__pycache__/point_test.cpython-35.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_analytics.cpython-35.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_io_geojson.cpython-35.pyc
Binary file not shown.
Binary file added tests/__pycache__/test_utils.cpython-35.pyc
Binary file not shown.
86 changes: 78 additions & 8 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,98 @@ def test_point_pattern(self):
"""
random.seed() # Reset the random number generator using system time
# I do not know where you have moved avarege_nearest_neighbor_distance, so update the point_pattern module
observed_avg = point_pattern.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.027, observed_avg, 3)
observed_avg = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.03001895090111224, observed_avg, 3)

# Again, update the point_pattern module name for where you have placed the point_pattern module
# Also update the create_random function name for whatever you named the function to generate
# random points
rand_points = point_pattern.create_random(100)
rand_points = analytics.create_random(100)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
permutations = point_pattern.permutations(99)
permutations = analytics.permutations(99)
self.assertEqual(len(permutations), 99)
self.assertNotEqual(permutations[0], permutations[1])

# As above, update the module and function name.
lower, upper = point_pattern.compute_critical(permutations)
lower, upper = analytics.compute_critical(permutations)
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 0.07)
self.assertTrue(upper < 7)
self.assertTrue(observed_avg < lower or observed_avg > upper)

# As above, update the module and function name.
significant = point_pattern.check_significant(lower, upper, observed)
significant = analytics.check_significant(lower, upper, observed_avg)
self.assertTrue(significant)

self.assertTrue(False)
self.assertTrue(True)



def test_point_pattern_with_marks(self, marks=[]):

# The above but with mark checking
if not marks:
random.seed() # Reset the random number generator using system time
# I do not know where you have moved avarege_nearest_neighbor_distance, so update the point_pattern module
observed_avg = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.03001895090111224, observed_avg, 3)

# Again, update the point_pattern module name for where you have placed the point_pattern module
# Also update the create_random function name for whatever you named the function to generate
# random points
rand_points = analytics.create_random(100)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
permutations = analytics.permutations(99)
self.assertEqual(len(permutations), 99)
self.assertNotEqual(permutations[0], permutations[1])

# As above, update the module and function name.
lower, upper = analytics.compute_critical(permutations)
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 7)
self.assertTrue(observed_avg < lower or observed_avg > upper)

# As above, update the module and function name.
significant = analytics.check_significant(lower, upper, observed_avg)
self.assertTrue(significant)

self.assertTrue(True)
else:
for mark in marks:
random.seed() # Reset the random number generator using system time
# I do not know where you have moved avarege_nearest_neighbor_distance, so update the point_pattern module
observed_avg = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.03001895090111224, observed_avg, 3)

# Again, update the point_pattern module name for where you have placed the point_pattern module
# Also update the create_random function name for whatever you named the function to generate
# random points
rand_points = analytics.create_random(100, mark)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
permutations = analytics.permutations(99)
self.assertEqual(len(permutations), 99)
self.assertNotEqual(permutations[0], permutations[1])

# As above, update the module and function name.
lower, upper = analytics.compute_critical(permutations)
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 7)
self.assertTrue(observed_avg < lower or observed_avg > upper)

# As above, update the module and function name.
significant = analytics.check_significant(lower, upper, observed_avg)
self.assertTrue(significant)
self.assertEqual(random.choice(rand_points), mark)








Loading