Skip to content

Code added #15

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 3 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
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment 4</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions .pydevproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 3.0</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
83 changes: 65 additions & 18 deletions point_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if euclidean_distance were an expensive call. Would you keep it here?

if i==j:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out enumerate. It will let you know what positional index the iterator is at in the list. You can use this to check if both iterators are in the same place - thereby not skipping coincident points.

continue
elif dist < dist_nearest:
dist_nearest = dist;
mean_d += dist_nearest;
mean_d=mean_d/(len(points))
return mean_d


Expand All @@ -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

Expand All @@ -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):
Expand All @@ -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))


"""
Expand Down
3 changes: 1 addition & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import unittest

sys.path.append(os.path.abspath('..'))

from .. import point_pattern


Expand Down Expand Up @@ -33,7 +32,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):
"""
Expand Down