From cc500b98b506aaabffe5c856b66764c5bc4b1b34 Mon Sep 17 00:00:00 2001 From: MrEXV <853013493@qq.com> Date: Sun, 27 Mar 2016 23:02:34 -0700 Subject: [PATCH 1/3] Added files via upload late work --- point_pattern.py | 51 ++++++++++- tests.py | 219 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 tests.py diff --git a/point_pattern.py b/point_pattern.py index 1b0a5cb..0f26065 100644 --- a/point_pattern.py +++ b/point_pattern.py @@ -34,6 +34,9 @@ def read_geojson(input_file): # Please use the python json module (imported above) # to solve this one. gj = None + fp = open(input_file, 'r') + gj = json.loads(fp.read()) + fp.close() return gj @@ -58,6 +61,10 @@ def find_largest_city(gj): """ city = None max_population = 0 + for feature in gj["features"]: + if feature["properties"]["pop_max"]>max_population: + max_population=feature["properties"]["pop_max"] + city=feature["properties"]["nameascii"] return city, max_population @@ -73,8 +80,20 @@ def write_your_own(gj): Do not forget to write the accompanying test in tests.py! + + To find the average of pop_max and pop_min. + """ - return + + sum_pop_max=0 + sum_pop_min=0 + num=0 + for feature in gj["features"]: + sum_pop_max+=feature["properties"]["pop_max"] + sum_pop_min+=feature["properties"]["pop_min"] + num+=1 + + return float(sum_pop_max/num),float(sum_pop_min/num) def mean_center(points): """ @@ -95,6 +114,14 @@ def mean_center(points): """ x = None y = None + sum_x=[] + sum_y=[] + for x_tmp,y_tmp in points: + sum_x.append(x_tmp) + sum_y.append(y_tmp) + + x=float(sum(sum_x)/len(sum_x)) + y=float(sum(sum_y)/len(sum_y)) return x, y @@ -120,7 +147,17 @@ def average_nearest_neighbor_distance(points): p. 445-453. """ mean_d = 0 - + nearest_distances=[] + for point_i in points: + distance=[] + for point_j in points: + if point_i==point_j: + continue + else: + distance.append(euclidean_distance(point_i,point_j)) + nearest_distances.append(min(distance)) + + mean_d=float(sum(nearest_distances)/len(nearest_distances)) return mean_d @@ -141,6 +178,13 @@ def minimum_bounding_rectangle(points): mbr = [0,0,0,0] + x_list=[] + y_list=[] + for x,y in points: + x_list.append(x) + y_list.append(y) + mbr=[min(x_list),min(y_list),max(x_list),max(y_list)] + return mbr @@ -149,7 +193,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 @@ -174,6 +218,7 @@ def expected_distance(area, n): """ expected = 0 + expected =float((math.sqrt(area/n))/2) return expected diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..fe30dcf --- /dev/null +++ b/tests.py @@ -0,0 +1,219 @@ +import os +import random +import sys +import unittest + +sys.path.append(os.path.abspath('..')) + +from .. import point_pattern + + +class TestFilesAndDicts(unittest.TestCase): + """ + This set of tests is focused on reading some geojson + data in as a Python dictionary and then answering + some questions about the data. + """ + + @classmethod + def setUpClass(cls): + cls.gj = point_pattern.read_geojson('data/us_cities.geojson') + + def test_read_geojson(self): + self.assertIsInstance(self.gj, dict) + + def test_find_largest(self): + city, pop = point_pattern.find_largest_city(self.gj) + self.assertEqual(city, 'New York') + self.assertEqual(pop, 19040000) + + def test_write_your_own(self): + """ + Here you will write a test for the code you write in + point_pattern.py. + """ + avg_pop_max,avg_pop_min = point_pattern.write_your_own(self.gj) + self.assertTrue(avg_pop_max,308473.3217503218) + self.assertTrue(avg_pop_min,115237.50321750322) + +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)] + + def test_average_nearest_neighbor_distance(self): + mean_d = point_pattern.average_nearest_neighbor_distance(self.points) + self.assertAlmostEqual(mean_d, 7.629178, 5) + + def test_mean_center(self): + """ + Something to think about - What values would you + expect to see here and why? Why are the values + not what you might expect? + """ + x, y = point_pattern.mean_center(self.points) + self.assertEqual(x, 47.52) + self.assertEqual(y, 45.14) + + def test_minimum_bounding_rectangle(self): + mbr = point_pattern.minimum_bounding_rectangle(self.points) + self.assertEqual(mbr, [0,0,94,98]) + + def test_mbr_area(self): + mbr = [0,0,94,98] + area = point_pattern.mbr_area(mbr) + self.assertEqual(area, 9212) + + def test_expected_distance(self): + area = 9212 + npoints = 50 + expected = point_pattern.expected_distance(area, npoints) + self.assertAlmostEqual(expected, 6.7867518, 5) + + +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 + + def test_getx(self): + """ + A simple test to ensure that you understand how to access + the x coordinate in a tuple of coordinates. + + You do not need to make any changes to this test, + instead, in point_pattern.py, you must complete the + `getx` function so that the correct + values are returned. + """ + point = (1,2) + x = point_pattern.getx(point) + self.assertEqual(1, x) + + def test_gety(self): + """ + As above, except get the y coordinate. + + You do not need to make any changes to this test, + instead, in point_pattern.py, you must complete the + `gety` function so that the correct + values are returned. + """ + point = (3,2.5) + y = point_pattern.gety(point) + self.assertEqual(2.5, y) + + def test_shift_point(self): + """ + Test that a point is being properly shifted + when calling point_pattern.shift_point + """ + point = (0,0) + new_point = point_pattern.shift_point(point, 3, 4) + 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) + + def test_euclidean_distance(self): + """ + A test to ensure that the distance between points + is being properly computed. + + You do not need to make any changes to this test, + instead, in point_pattern.py, you must complete the + `eucliden_distance` function so that the correct + values are returned. + + Something to think about: Why might you want to test + different cases, e.g. all positive integers, positive + and negative floats, coincident points? + """ + point_a = (3, 7) + point_b = (1, 9) + distance = point_pattern.euclidean_distance(point_a, point_b) + self.assertAlmostEqual(2.8284271, distance, 4) + + point_a = (-1.25, 2.35) + point_b = (4.2, -3.1) + distance = point_pattern.euclidean_distance(point_a, point_b) + self.assertAlmostEqual(7.7074639, distance, 4) + + point_a = (0, 0) + point_b = (0, 0) + distance = point_pattern.euclidean_distance(point_b, point_a) + self.assertAlmostEqual(0.0, distance, 4) + + def test_manhattan_distance(self): + """ + A test to ensure that the distance between points + is being properly computed. + + You do not need to make any changes to this test, + instead, in point_pattern.py, you must complete the + `eucliden_distance` function so that the correct + values are returned. + + Something to think about: Why might you want to test + different cases, e.g. all positive integers, positive + and negative floats, coincident points? + """ + point_a = (3, 7) + point_b = (1, 9) + distance = point_pattern.manhattan_distance(point_a, point_b) + self.assertEqual(4.0, distance) + + point_a = (-1.25, 2.35) + point_b = (4.2, -3.1) + distance = point_pattern.manhattan_distance(point_a, point_b) + self.assertEqual(10.9, distance) + + point_a = (0, 0) + point_b = (0, 0) + distance = point_pattern.manhattan_distance(point_b, point_a) + self.assertAlmostEqual(0.0, distance, 4) + + def test_check_coincident(self): + """ + As above, update the function in point_pattern.py + + """ + point_a = (3, 7) + point_b = (3, 7) + coincident = point_pattern.check_coincident(point_a, point_b) + self.assertEqual(coincident, True) + + point_b = (-3, -7) + coincident = point_pattern.check_coincident(point_a, point_b) + self.assertEqual(coincident, False) + + point_a = (0, 0) + point_b = (0.0, 0.0) + coincident = point_pattern.check_coincident(point_b, point_a) + self.assertEqual(coincident, True) + + 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)] + + inlist = point_pattern.check_in((0,0), point_list) + self.assertTrue(inlist) + + inlist = point_pattern.check_in((6,4), point_list) + self.assertFalse(inlist) From f41ec6676be5d0e62d889b9dd8911127f5f5905c Mon Sep 17 00:00:00 2001 From: MrEXV <853013493@qq.com> Date: Tue, 29 Mar 2016 15:41:50 -0700 Subject: [PATCH 2/3] . --- tests/tests.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index 2518463..fe30dcf 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -32,8 +32,9 @@ def test_write_your_own(self): Here you will write a test for the code you write in point_pattern.py. """ - some_return = point_pattern.write_your_own(self.gj) - self.assertTrue(False) + avg_pop_max,avg_pop_min = point_pattern.write_your_own(self.gj) + self.assertTrue(avg_pop_max,308473.3217503218) + self.assertTrue(avg_pop_min,115237.50321750322) class TestIterablePointPattern(unittest.TestCase): """ From 0e64d088b41e4f8cb063afc3b256e50f38d496cf Mon Sep 17 00:00:00 2001 From: MrEXV <853013493@qq.com> Date: Tue, 29 Mar 2016 15:52:26 -0700 Subject: [PATCH 3/3] removed tests.py --- tests.py | 219 ------------------------------------------------------- 1 file changed, 219 deletions(-) delete mode 100644 tests.py diff --git a/tests.py b/tests.py deleted file mode 100644 index fe30dcf..0000000 --- a/tests.py +++ /dev/null @@ -1,219 +0,0 @@ -import os -import random -import sys -import unittest - -sys.path.append(os.path.abspath('..')) - -from .. import point_pattern - - -class TestFilesAndDicts(unittest.TestCase): - """ - This set of tests is focused on reading some geojson - data in as a Python dictionary and then answering - some questions about the data. - """ - - @classmethod - def setUpClass(cls): - cls.gj = point_pattern.read_geojson('data/us_cities.geojson') - - def test_read_geojson(self): - self.assertIsInstance(self.gj, dict) - - def test_find_largest(self): - city, pop = point_pattern.find_largest_city(self.gj) - self.assertEqual(city, 'New York') - self.assertEqual(pop, 19040000) - - def test_write_your_own(self): - """ - Here you will write a test for the code you write in - point_pattern.py. - """ - avg_pop_max,avg_pop_min = point_pattern.write_your_own(self.gj) - self.assertTrue(avg_pop_max,308473.3217503218) - self.assertTrue(avg_pop_min,115237.50321750322) - -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)] - - def test_average_nearest_neighbor_distance(self): - mean_d = point_pattern.average_nearest_neighbor_distance(self.points) - self.assertAlmostEqual(mean_d, 7.629178, 5) - - def test_mean_center(self): - """ - Something to think about - What values would you - expect to see here and why? Why are the values - not what you might expect? - """ - x, y = point_pattern.mean_center(self.points) - self.assertEqual(x, 47.52) - self.assertEqual(y, 45.14) - - def test_minimum_bounding_rectangle(self): - mbr = point_pattern.minimum_bounding_rectangle(self.points) - self.assertEqual(mbr, [0,0,94,98]) - - def test_mbr_area(self): - mbr = [0,0,94,98] - area = point_pattern.mbr_area(mbr) - self.assertEqual(area, 9212) - - def test_expected_distance(self): - area = 9212 - npoints = 50 - expected = point_pattern.expected_distance(area, npoints) - self.assertAlmostEqual(expected, 6.7867518, 5) - - -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 - - def test_getx(self): - """ - A simple test to ensure that you understand how to access - the x coordinate in a tuple of coordinates. - - You do not need to make any changes to this test, - instead, in point_pattern.py, you must complete the - `getx` function so that the correct - values are returned. - """ - point = (1,2) - x = point_pattern.getx(point) - self.assertEqual(1, x) - - def test_gety(self): - """ - As above, except get the y coordinate. - - You do not need to make any changes to this test, - instead, in point_pattern.py, you must complete the - `gety` function so that the correct - values are returned. - """ - point = (3,2.5) - y = point_pattern.gety(point) - self.assertEqual(2.5, y) - - def test_shift_point(self): - """ - Test that a point is being properly shifted - when calling point_pattern.shift_point - """ - point = (0,0) - new_point = point_pattern.shift_point(point, 3, 4) - 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) - - def test_euclidean_distance(self): - """ - A test to ensure that the distance between points - is being properly computed. - - You do not need to make any changes to this test, - instead, in point_pattern.py, you must complete the - `eucliden_distance` function so that the correct - values are returned. - - Something to think about: Why might you want to test - different cases, e.g. all positive integers, positive - and negative floats, coincident points? - """ - point_a = (3, 7) - point_b = (1, 9) - distance = point_pattern.euclidean_distance(point_a, point_b) - self.assertAlmostEqual(2.8284271, distance, 4) - - point_a = (-1.25, 2.35) - point_b = (4.2, -3.1) - distance = point_pattern.euclidean_distance(point_a, point_b) - self.assertAlmostEqual(7.7074639, distance, 4) - - point_a = (0, 0) - point_b = (0, 0) - distance = point_pattern.euclidean_distance(point_b, point_a) - self.assertAlmostEqual(0.0, distance, 4) - - def test_manhattan_distance(self): - """ - A test to ensure that the distance between points - is being properly computed. - - You do not need to make any changes to this test, - instead, in point_pattern.py, you must complete the - `eucliden_distance` function so that the correct - values are returned. - - Something to think about: Why might you want to test - different cases, e.g. all positive integers, positive - and negative floats, coincident points? - """ - point_a = (3, 7) - point_b = (1, 9) - distance = point_pattern.manhattan_distance(point_a, point_b) - self.assertEqual(4.0, distance) - - point_a = (-1.25, 2.35) - point_b = (4.2, -3.1) - distance = point_pattern.manhattan_distance(point_a, point_b) - self.assertEqual(10.9, distance) - - point_a = (0, 0) - point_b = (0, 0) - distance = point_pattern.manhattan_distance(point_b, point_a) - self.assertAlmostEqual(0.0, distance, 4) - - def test_check_coincident(self): - """ - As above, update the function in point_pattern.py - - """ - point_a = (3, 7) - point_b = (3, 7) - coincident = point_pattern.check_coincident(point_a, point_b) - self.assertEqual(coincident, True) - - point_b = (-3, -7) - coincident = point_pattern.check_coincident(point_a, point_b) - self.assertEqual(coincident, False) - - point_a = (0, 0) - point_b = (0.0, 0.0) - coincident = point_pattern.check_coincident(point_b, point_a) - self.assertEqual(coincident, True) - - 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)] - - inlist = point_pattern.check_in((0,0), point_list) - self.assertTrue(inlist) - - inlist = point_pattern.check_in((6,4), point_list) - self.assertFalse(inlist)