From 0eae78d3b5144ec537ffb9fa270ffada6f91e956 Mon Sep 17 00:00:00 2001 From: pkadambi Date: Tue, 23 Feb 2016 22:45:08 -0700 Subject: [PATCH 1/3] Added necessary Changes --- .project | 17 ++++++++++ .pydevproject | 8 +++++ point_pattern.py | 83 +++++++++++++++++++++++++++++++++++++----------- tests/tests.py | 8 ++--- 4 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 .project create mode 100644 .pydevproject diff --git a/.project b/.project new file mode 100644 index 0000000..c02f67c --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Assignment 4 + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 0000000..c3434da --- /dev/null +++ b/.pydevproject @@ -0,0 +1,8 @@ + + + +/${PROJECT_DIR_NAME} + +python 3.0 +Default + diff --git a/point_pattern.py b/point_pattern.py index 1b0a5cb..1674a5e 100644 --- a/point_pattern.py +++ b/point_pattern.py @@ -15,12 +15,14 @@ 7. Compute the area of a MBR 8. Compute the expected mean distance for a given point pattern """ +from pyexpat import features +from math import sqrt def read_geojson(input_file): """ Read a geojson file - + Parameters ---------- input_file : str @@ -31,9 +33,13 @@ def read_geojson(input_file): gj : dict An in memory version of the geojson """ + + with open(input_file, 'r') as f: + gj=json.load(f) + # Please use the python json module (imported above) # to solve this one. - gj = None + return gj @@ -56,10 +62,14 @@ def find_largest_city(gj): population : int The population of the largest city """ - city = None - max_population = 0 - - return city, max_population + maximum=0; + features=gj['features'] + + for i in features: + if (i['properties']['pop_max']>maximum): + maximum=i['properties']['pop_max'] + city=i['properties']['nameascii'] + return city, maximum def write_your_own(gj): @@ -74,7 +84,14 @@ def write_your_own(gj): Do not forget to write the accompanying test in tests.py! """ - return + #Calculate the number of citues with two-word names + features=gj['features'] + count = 0 + for i in features: + if(' ' in i['properties']['name']): + count= count+1 + + return count def mean_center(points): """ @@ -93,8 +110,15 @@ def mean_center(points): y : float Mean y coordinate """ - x = None - y = None + x_tot=0 + y_tot=0 + + for i in points: + x_tot+=i[0] + y_tot+=i[1] + + x = x_tot/len(points) + y = y_tot/len(points) return x, y @@ -120,7 +144,16 @@ def average_nearest_neighbor_distance(points): p. 445-453. """ mean_d = 0 - + for i in points: + dist_nearest=1e9 + for j in points: + dist = euclidean_distance(i, j) + if i==j: + continue + elif dist < dist_nearest: + dist_nearest = dist; + mean_d += dist_nearest; + mean_d=mean_d/(len(points)) return mean_d @@ -138,8 +171,25 @@ def minimum_bounding_rectangle(points): : list Corners of the MBR in the form [xmin, ymin, xmax, ymax] """ - - mbr = [0,0,0,0] + #set initial params + xmin=points[1][0] + ymin=points[1][1] + xmax=points[1][0] + ymax=points[1][1] + + for i in points: + curr_x=i[0] + curr_y=i[1] + if curr_x < xmin: + xmin= curr_x + elif curr_x > xmax: + xmax= curr_x + + if curr_y < ymin: + ymin= curr_y + elif curr_y > ymax: + ymax= curr_y + mbr = [xmin,ymin,xmax,ymax] return mbr @@ -148,9 +198,7 @@ def mbr_area(mbr): """ Compute the area of a minimum bounding rectangle """ - area = 0 - - return area + return (mbr[3]-mbr[1])*(mbr[2]-mbr[0]) def expected_distance(area, n): @@ -172,9 +220,8 @@ def expected_distance(area, n): n : int The number of points """ - - expected = 0 - return expected + + return 0.5*(sqrt(area/n)) """ diff --git a/tests/tests.py b/tests/tests.py index 2518463..202a7fb 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -4,8 +4,8 @@ import unittest sys.path.append(os.path.abspath('..')) - -from .. import point_pattern +#from .. +import point_pattern class TestFilesAndDicts(unittest.TestCase): @@ -17,7 +17,7 @@ class TestFilesAndDicts(unittest.TestCase): @classmethod def setUpClass(cls): - cls.gj = point_pattern.read_geojson('data/us_cities.geojson') + cls.gj = point_pattern.read_geojson('us_cities.geojson') def test_read_geojson(self): self.assertIsInstance(self.gj, dict) @@ -33,7 +33,7 @@ def test_write_your_own(self): point_pattern.py. """ some_return = point_pattern.write_your_own(self.gj) - self.assertTrue(False) + self.assertEqual(187,some_return) class TestIterablePointPattern(unittest.TestCase): """ From 0884f5ae20fdfca6687817eb8c224dfa96754c33 Mon Sep 17 00:00:00 2001 From: pkadambi Date: Tue, 23 Feb 2016 22:50:12 -0700 Subject: [PATCH 2/3] wrote code --- tests/tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index 202a7fb..5c7a1db 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -4,8 +4,7 @@ import unittest sys.path.append(os.path.abspath('..')) -#from .. -import point_pattern +from .. import point_pattern class TestFilesAndDicts(unittest.TestCase): From 99c6a08f91dd14206e014d047f32e2c42d96a5ae Mon Sep 17 00:00:00 2001 From: pkadambi Date: Tue, 23 Feb 2016 22:57:36 -0700 Subject: [PATCH 3/3] Added code --- tests/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests.py b/tests/tests.py index 5c7a1db..4d8f34b 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -16,7 +16,7 @@ class TestFilesAndDicts(unittest.TestCase): @classmethod def setUpClass(cls): - cls.gj = point_pattern.read_geojson('us_cities.geojson') + cls.gj = point_pattern.read_geojson('data/us_cities.geojson') def test_read_geojson(self): self.assertIsInstance(self.gj, dict)