Skip to content

Completed assignment 5 tasks #1

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 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ target/

#Ipython Notebook
.ipynb_checkpoints

# XML Files
*.xml
12 changes: 12 additions & 0 deletions .idea/assignment_05.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

535 changes: 535 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from . import utils


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 item in gj["features"]:
props = item["properties"]
if props["pop_max"] > max_population:
max_population = props["pop_max"]
city = props["adm1name"]

return city, max_population


def average_nearest_neighbor_distance(points):
"""
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_nearest_neighbor = None
# Average the nearest neighbor distance of all points.
for i, point in enumerate(points):
# Find the nearest neighbor to this point.
for j, otherPoint in enumerate(points):
# You are not your own neighbor.
if i == j:
continue
# To avoid multiple calculations, we'll cache the result.
current_distance = utils.euclidean_distance(point, otherPoint)
# nearest neighbor will be None if this is the first neighbor we have iterated over.
if temp_nearest_neighbor is None:
temp_nearest_neighbor = current_distance
elif temp_nearest_neighbor > current_distance:
temp_nearest_neighbor = current_distance
# At this point, we've found point's nearest neighbor distance.
# Add in that distance.
mean_d += temp_nearest_neighbor
temp_nearest_neighbor = None

# Divide by number of points.
mean_d /= len(points)

return mean_d


def permutations(p = 99):
n = 100
to_return = []
for i in range(p):
to_return.append(
average_nearest_neighbor_distance(
utils.create_random(n)
)
)
return to_return


def compute_critical(p):
"""
Calculates the critical points (lowest distance and greatest distance) in a set of
randomly generated permutations (created using permutations(p)).
"""
return min(p), max(p)
23 changes: 23 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json


def read_geojson(input_file):
"""
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
"""
# Please use the python json module (imported above)
# to solve this one.
with open(input_file, 'r') as fp:
gj = json.load(fp)
return gj

13 changes: 7 additions & 6 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,29 @@ 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)
observed_avg = analytics.average_nearest_neighbor_distance(self.points)

self.assertAlmostEqual(0.027, 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 = utils.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(observed_avg < lower or observed_avg > upper)

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

self.assertTrue(False)
self.assertTrue(True)
10 changes: 9 additions & 1 deletion tests/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@

from .. import analytics


class TestAnalytics(unittest.TestCase):

def setUp(self):
pass
pass

def test_permutations(self):
self.assertEqual(len(analytics.permutations(300)), 300)

def test_critical(self):
criticals = analytics.compute_critical([0.5, 1.0, 0.99, 3.14, 0.987, 0.102])
self.assertTrue(criticals[0] == 0.102 and criticals[1] == 3.14)
1 change: 1 addition & 0 deletions tests/test_io_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .. import io_geojson


class TestIoGeoJson(unittest.TestCase):

def setUp(self):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@

from .. import utils


class TestUtils(unittest.TestCase):

def setUp(self):
pass
pass

def test_create_random(self):
self.assertEqual(len(utils.create_random(1000)), 1000)

def test_check_significant(self):
self.assertTrue(utils.check_significant(10, 30, 9.9))
self.assertFalse(utils.check_significant(9.9, 30, 10))
Loading