diff --git a/.travis.yml b/.travis.yml index 9aa63d2..d4d7f04 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,4 @@ python: - "3.5" #command to run tests -script: nosetests +script: nosetests \ No newline at end of file diff --git a/__pycache__/__init__.cpython-35.pyc b/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..f2f8d8c Binary files /dev/null and b/__pycache__/__init__.cpython-35.pyc differ diff --git a/__pycache__/analytics.cpython-35.pyc b/__pycache__/analytics.cpython-35.pyc new file mode 100644 index 0000000..7ed0daf Binary files /dev/null and b/__pycache__/analytics.cpython-35.pyc differ diff --git a/__pycache__/io_geojson.cpython-35.pyc b/__pycache__/io_geojson.cpython-35.pyc new file mode 100644 index 0000000..2a40d65 Binary files /dev/null and b/__pycache__/io_geojson.cpython-35.pyc differ diff --git a/__pycache__/point.cpython-35.pyc b/__pycache__/point.cpython-35.pyc new file mode 100644 index 0000000..ecbabc9 Binary files /dev/null and b/__pycache__/point.cpython-35.pyc differ diff --git a/__pycache__/utils.cpython-35.pyc b/__pycache__/utils.cpython-35.pyc new file mode 100644 index 0000000..37733a9 Binary files /dev/null and b/__pycache__/utils.cpython-35.pyc differ diff --git a/analytics.py b/analytics.py index e69de29..5b43446 100644 --- a/analytics.py +++ b/analytics.py @@ -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 \ No newline at end of file diff --git a/io_geojson.py b/io_geojson.py index e69de29..2619650 100644 --- a/io_geojson.py +++ b/io_geojson.py @@ -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 diff --git a/point.py b/point.py index e69de29..fc83c2d 100644 --- a/point.py +++ b/point.py @@ -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 \ No newline at end of file diff --git a/tests/__pycache__/__init__.cpython-35.pyc b/tests/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..f989786 Binary files /dev/null and b/tests/__pycache__/__init__.cpython-35.pyc differ diff --git a/tests/__pycache__/functional_test.cpython-35.pyc b/tests/__pycache__/functional_test.cpython-35.pyc new file mode 100644 index 0000000..79e43f8 Binary files /dev/null and b/tests/__pycache__/functional_test.cpython-35.pyc differ diff --git a/tests/__pycache__/point_test.cpython-35.pyc b/tests/__pycache__/point_test.cpython-35.pyc new file mode 100644 index 0000000..466d00c Binary files /dev/null and b/tests/__pycache__/point_test.cpython-35.pyc differ diff --git a/tests/__pycache__/test_analytics.cpython-35.pyc b/tests/__pycache__/test_analytics.cpython-35.pyc new file mode 100644 index 0000000..db4e090 Binary files /dev/null and b/tests/__pycache__/test_analytics.cpython-35.pyc differ diff --git a/tests/__pycache__/test_io_geojson.cpython-35.pyc b/tests/__pycache__/test_io_geojson.cpython-35.pyc new file mode 100644 index 0000000..d46c1d3 Binary files /dev/null and b/tests/__pycache__/test_io_geojson.cpython-35.pyc differ diff --git a/tests/__pycache__/test_utils.cpython-35.pyc b/tests/__pycache__/test_utils.cpython-35.pyc new file mode 100644 index 0000000..fd867c0 Binary files /dev/null and b/tests/__pycache__/test_utils.cpython-35.pyc differ diff --git a/tests/functional_test.py b/tests/functional_test.py index 596af78..6a6cf61 100644 --- a/tests/functional_test.py +++ b/tests/functional_test.py @@ -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) \ No newline at end of file + 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) + + + + + + + + \ No newline at end of file diff --git a/tests/point_test.py b/tests/point_test.py new file mode 100644 index 0000000..71ff927 --- /dev/null +++ b/tests/point_test.py @@ -0,0 +1,42 @@ +import os +import sys +import random +import unittest +sys.path.insert(0, os.path.abspath('..')) + +from .. import point + +class TestPoint(unittest.TestCase): + + def setUp(self): + self.x = 3 + self.y = 1 + self.marks = [] + + def set_x_and_y_correctly(self): + self.assertEqual(self.x, 3) + self.assertEqual(self.y, 1) + + def should_catch_coincident_point(self): + self.assertTrue(self.check_coincident(self, self)) + + def should_catch_noncoincident_point(self): + not_self = Point(1, 4, ['Pizza', 'BBQ', 'Calzones']) + self.assertFalse(self.check_coincident(self, not_self)) + + def shift_the_point(self): + old_x = self.x + new_x = self.shift_point(self, 1, 2) + self.assertFalse(self.assertEqual(old_x, new_x)) + + def mark_creation(self): + random_n = random.seed + list_o_marks = ['Bernie Sanders', 'Ted Cruz', 'Donald Trump', 'Hillary Clinton', 'John Kasich', 'Jill Stein'] + random_mark = random.choice(list_o_marks) + list_o_points = [Point() for i in range(random_n)] + for point in list_o_points: + point.mark = random_mark + self.assertEqual(random_n, len(list_o_points)) + + def create_random_marked_points(self): + self.assertEqual(100, len(self.create_random_marked_points(100, ['Water', 'Fire', 'Earth']))) diff --git a/tests/test_analytics.py b/tests/test_analytics.py new file mode 100644 index 0000000..1b9eae7 --- /dev/null +++ b/tests/test_analytics.py @@ -0,0 +1,24 @@ +import os +import sys +import unittest +import numpy +sys.path.insert(0, os.path.abspath('..')) + +from .. import analytics + +class TestAnalytics(unittest.TestCase): + + def setUp(self): + self.permutations = analytics.permutations(666) + self.random = analytics.create_random(666) + + def test_compute_critical(self): + test_batch = [2, 3, 100, 5, 401, 502, 44, 90] + self.lower, self.upper = analytics.compute_critical(test_batch) + self.assertTrue(self.lower == 2, self.upper == 502) + + def test_permutations(self): + self.assertEqual(len(analytics.permutations(100)), 100) + + def test_create_random(self): + self.assertEqual(len(self.random), 666) \ No newline at end of file diff --git a/tests/test_io_geojson.py b/tests/test_io_geojson.py new file mode 100644 index 0000000..2d0b40c --- /dev/null +++ b/tests/test_io_geojson.py @@ -0,0 +1,17 @@ +import os +import sys +import unittest +sys.path.insert(0, os.path.abspath('..')) + +from .. import io_geojson, analytics + +class TestIoGeoJson(unittest.TestCase): + + def setUp(self): + self.input_url = "https://api.myjson.com/bins/4587l" + + + def test_read_geojson(self): + response = io_geojson.read_geojson(self.input_url) + largest_city = analytics.find_largest_city(response) + self.assertEqual(largest_city[0], "New York") \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..54a79f6 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,16 @@ +import os +import sys +import unittest +sys.path.insert(0, os.path.abspath('..')) + +from .. import utils + +class TestUtils(unittest.TestCase): + + def setUp(self): + self.points_a = (1,4) + self.points_b = (1,4) + + + def check_coincident(self): + self.assertEqual(check_coincident(self.points_a), check_coincident(self.points_b)) \ No newline at end of file diff --git a/utils.py b/utils.py index e69de29..bdb1239 100644 --- a/utils.py +++ b/utils.py @@ -0,0 +1,157 @@ +import math +import random + +def manhattan_distance(a, b): + """ + Compute the Manhattan distance between two points + + Parameters + ---------- + a : tuple + A point in the form (x,y) + + b : tuple + A point in the form (x,y) + + Returns + ------- + distance : float + The Manhattan distance between the two points + """ + distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) + return distance + + + +def euclidean_distance(a, b): + """ + Compute the Euclidean distance between two points + + Parameters + ---------- + a : tuple + A point in the form (x,y) + + b : tuple + A point in the form (x,y) + + Returns + ------- + + distance : float + The Euclidean distance between the two points + """ + distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2) + return distance + + +def shift_point(point, x_shift, y_shift): + """ + Shift a point by some amount in the x and y directions + + Parameters + ---------- + point : tuple + in the form (x,y) + + x_shift : int or float + distance to shift in the x direction + + y_shift : int or float + distance to shift in the y direction + + Returns + ------- + new_x : int or float + shited x coordinate + + new_y : int or float + shifted y coordinate + + Note that the new_x new_y elements are returned as a tuple + + Example + ------- + >>> point = (0,0) + >>> shift_point(point, 1, 2) + (1,2) + """ + x = getx(point) + y = gety(point) + + x += x_shift + y += y_shift + + return x, y + + +def check_coincident(a, b): + """ + Check whether two points are coincident + Parameters + ---------- + a : tuple + A point in the form (x,y) + + b : tuple + A point in the form (x,y) + + Returns + ------- + equal : bool + Whether the points are equal + """ + return a == b + + +def check_in(point, point_list): + """ + Check whether point is in the point list + + Parameters + ---------- + point : tuple + In the form (x,y) + + point_list : list + in the form [point, point_1, point_2, ..., point_n] + """ + return point in point_list + + +def getx(point): + """ + A simple method to return the x coordinate of + an tuple in the form(x,y). We will look at + sequences in a coming lesson. + + Parameters + ---------- + point : tuple + in the form (x,y) + + Returns + ------- + : int or float + x coordinate + """ + return point[0] + + +def gety(point): + """ + A simple method to return the x coordinate of + an tuple in the form(x,y). We will look at + sequences in a coming lesson. + + Parameters + ---------- + point : tuple + in the form (x,y) + + Returns + ------- + : int or float + y coordinate + """ + return point[1] \ No newline at end of file