From 7c316213674ca65ad9c1c81ca483f4ed0d8207ec Mon Sep 17 00:00:00 2001 From: al Date: Sat, 27 Feb 2016 12:59:18 -0700 Subject: [PATCH 1/3] Figured out what I was doing wrong with points but still can't read the data file locally for whatever reason. Hopefully Travis has no problem --- point_pattern.py | 75 ++++++++++++++++++++++++++++++++++++++++++------ tests/tests.py | 8 ++++++ 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/point_pattern.py b/point_pattern.py index 1b0a5cb..c28b1d7 100644 --- a/point_pattern.py +++ b/point_pattern.py @@ -1,5 +1,6 @@ import math # I am guessing that you will need to use the math module import json # I would like you to use the JSON module for reading geojson (for now) + """ Like last assignment, we are going to be working with point patterns. The readings focused on iteration, sequences, and @@ -33,7 +34,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 @@ -58,10 +60,14 @@ def find_largest_city(gj): """ 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 write_your_own(gj): """ Here you will write your own code to find @@ -74,7 +80,17 @@ def write_your_own(gj): Do not forget to write the accompanying test in tests.py! """ - return + # 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): """ @@ -93,8 +109,17 @@ def mean_center(points): y : float Mean y coordinate """ - x = None - y = None + 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 @@ -120,8 +145,25 @@ def average_nearest_neighbor_distance(points): p. 445-453. """ mean_d = 0 + temp_p = None + last_p = None + + for p in points: + for neighbor in p: + + if p == neighbor: + continue + elif temp_p is None: + temp_p = euclidean_distance(p, neighbor) + elif temp_p > euclidean_distance(p, neighbor): + temp_p = euclidean_distance(p, neighbor) + else: + continue + + + mean_d = ((mean_d + temp_p) / len(points)) - return mean_d + return mean_d def minimum_bounding_rectangle(points): @@ -139,7 +181,22 @@ def minimum_bounding_rectangle(points): Corners of the MBR in the form [xmin, ymin, xmax, ymax] """ - mbr = [0,0,0,0] + 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 @@ -148,7 +205,7 @@ def mbr_area(mbr): """ Compute the area of a minimum bounding rectangle """ - area = 0 + area = ((mbr[2] - mbr[0]) * (mbr[3] - mbr[1])) return area @@ -173,7 +230,7 @@ def expected_distance(area, n): The number of points """ - expected = 0 + expected = (0.5 * (math.sqrt(area/n))) return expected diff --git a/tests/tests.py b/tests/tests.py index 2518463..74ad695 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -17,6 +17,8 @@ class TestFilesAndDicts(unittest.TestCase): @classmethod def setUpClass(cls): + # REMINDER: Trying to finish my tests but the computer I'm on now can't find this file for some reason + # Why is that... cls.gj = point_pattern.read_geojson('data/us_cities.geojson') def test_read_geojson(self): @@ -216,3 +218,9 @@ def test_check_in(self): inlist = point_pattern.check_in((6,4), point_list) self.assertFalse(inlist) + + + def test_alaska_points(self): + gj = point_pattern.read_geojson('data/us_cities.geojson') + alaska_points = point_pattern.write_your_own(gj) + self.assertTrue(alaska_points[0]["properties"]["adm1name"] == 'Alaska') \ No newline at end of file From c74f4d06e0a1f522dd627f89568f95b8a4fa0626 Mon Sep 17 00:00:00 2001 From: al Date: Sat, 27 Feb 2016 13:24:10 -0700 Subject: [PATCH 2/3] That would probably be good --- tests/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.py b/tests/tests.py index 74ad695..39df393 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -35,7 +35,7 @@ def test_write_your_own(self): point_pattern.py. """ some_return = point_pattern.write_your_own(self.gj) - self.assertTrue(False) + self.assertEqual(some_return[0]["properties"]["adm1name"],"Alaska") class TestIterablePointPattern(unittest.TestCase): """ From e5b2a149a55dacfc77c7a02503531e2cb98c2b16 Mon Sep 17 00:00:00 2001 From: al Date: Tue, 1 Mar 2016 17:09:24 -0700 Subject: [PATCH 3/3] Switched to a url so I can actually test the data from my computer, not sure why it's acting funny locally but glad I've got something to work with. Fixed the average_neighbor finally too thanks to jason's advice --- point_pattern.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/point_pattern.py b/point_pattern.py index c28b1d7..0d6bf67 100644 --- a/point_pattern.py +++ b/point_pattern.py @@ -1,5 +1,6 @@ import math # I am guessing that you will need to use the math module import json # I would like you to use the JSON module for reading geojson (for now) +from urllib.request import urlopen """ Like last assignment, we are going to be working with point @@ -34,8 +35,13 @@ def read_geojson(input_file): """ # Please use the python json module (imported above) # to solve this one. - with open(input_file, 'r') as f: - gj = json.load(f) + # with open(input_file, 'r') as f: + # gj = json.load(f) + + response = urlopen("https://api.myjson.com/bins/4587l").read().decode('utf8') + gj = json.loads(response) + print(gj) + return gj @@ -144,26 +150,24 @@ def average_nearest_neighbor_distance(points): Measure of Spatial Relationships in Populations. Ecology. 35(4) p. 445-453. """ - mean_d = 0 - temp_p = None - last_p = None - for p in points: - for neighbor in p: - - if p == neighbor: - continue - elif temp_p is None: - temp_p = euclidean_distance(p, neighbor) - elif temp_p > euclidean_distance(p, neighbor): - temp_p = euclidean_distance(p, neighbor) - else: + mean_d = 0 + temp_p = 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 - mean_d = ((mean_d + temp_p) / len(points)) + return mean_d / len(points) - return mean_d def minimum_bounding_rectangle(points):