Skip to content

added code #7

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 13 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
112 changes: 112 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import random
import math

from . import utils

def p_perms(p=99,n=100,mark=None):
mean_nn_dist = []
for i in range(p):
temp=utils.create_n_rand_pts(100)
temp1=average_nearest_neighbor_distance(temp)
mean_nn_dist.append(temp1);

return mean_nn_dist

def p_perms_marks(p=99,n=100,marks=None):
marks=['mercury', 'venus', 'earth', 'mars']
mean_nn_dist = []
for i in range(p):
temp=utils.create_marked_rand_pts(100,marks)
#print(temp.)
temp1=average_nearest_neighbor_distance_marks(temp,marks)
mean_nn_dist.append(temp1)

return mean_nn_dist

def monte_carlo_critical_bound_check(lb,ub,obs):
return obs<lb or obs>ub

def minimum_bounding_rectangle(points):
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

def find_largest_city(gj):
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):
features=gj['features']
count = 0
for i in features:
if(' ' in i['properties']['name']):
count= count+1

return count

def mean_center(points):
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

def average_nearest_neighbor_distance_marks(points,mark=None):
mean_d = 0
for i in range(len(points)):
dist_nearest=math.inf
for j in range(len(points)):
temp_p1 = (points[i].x, points[i].y)
temp_p2 = (points[j].x, points[j].y)
dist = utils.euclidean_distance(temp_p1, temp_p2)
if temp_p1 == temp_p2:
continue
elif dist < dist_nearest:
dist_nearest = dist;
mean_d += dist_nearest;
mean_d=mean_d/(len(points))
return mean_d

def average_nearest_neighbor_distance(points):
mean_d = 0
for i in points:
dist_nearest=math.inf
for j in points:
dist = utils.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
23 changes: 23 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
def read_geojson(input_file):
"""
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
"""

with open(input_file, 'r') as f:
gj=json.load(f)

# Please use the python json module (imported above)
# to solve this one.

return gj
135 changes: 135 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
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 math import sqrt
from . import utils

class Point():
def __init__(self, x=0, y=0, mark=[]):
self.x = x
self.y = y
self.mark = mark

def read_geojson(self,input_file):
with open(input_file, 'r') as f:
gj=json.load(f)

# Please use the python json module (imported above)
# to solve this one.

return gj

def check_if_coincident(self,secondPoint):
return utils.check_coincident((self.x,self.y),secondPoint)
Copy link
Contributor

Choose a reason for hiding this comment

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

How you used utils.check_coincident is precisely how you could path the read_geojson method in.


def shiftPoint(self, x_shift, y_shift):
return utils.shift_point((self.x,self.y),x_shift,y_shift)

def shift_point(self,x_shift, y_shift):
self.x += x_shift
self.y += y_shift
return self.x, self.y

def find_largest_city(gj):
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):
#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):
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

def average_nearest_neighbor_distance(points):
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

def minimum_bounding_rectangle(points):
#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

def mbr_area(mbr):
return (mbr[3]-mbr[1])*(mbr[2]-mbr[0])

def expected_distance(area, n):
return 0.5*(sqrt(area/n))

def manhattan_distance(a, b):
distance = abs(a[0] - b[0]) + abs(a[1] - b[1])
return distance

def euclidean_distance(a, b):
distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
return distance

def shift_point(point, x_shift, y_shift):
x = point
y = gety(point)

x += x_shift
y += y_shift

return x, y

def check_coincident(a, b):
return a == b

def check_in(point, point_list):
return point in point_list

def getx(point):
return point[0]

def gety(point):
return point[1]
61 changes: 46 additions & 15 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import random
import unittest

from .. import analytics
from .. import io_geojson
from .. import utils

from .. import analytics
from .. import io_geojson
from .. import utils
from .. import point

class TestFunctionalPointPattern(unittest.TestCase):

def setUp(self):
random.seed(12345)
i = 0
self.points = []
self.marks=['mercury','venus','earth','mars']
while i < 100:
seed = (round(random.random(),2), round(random.random(),2))
seed = (round(random.random(),2), round(random.random(),2),random.choice(self.marks))
self.points.append(seed)
n_additional = random.randint(5,10)
i += 1
Expand All @@ -22,14 +23,13 @@ def setUp(self):
for j in range(n_additional):
x_offset = random.randint(0,10) / 100
y_offset = random.randint(0,10) / 100
pt = (round(seed[0] + x_offset, 2), round(seed[1] + y_offset,2))
pt = (round(seed[0] + x_offset, 2), round(seed[1] + y_offset,2),random.choice(self.marks))
self.points.append(pt)
i += 1
if i == 100:
break
if i == 100:
break

def test_point_pattern(self):
"""
This test checks that the code can compute an observed mean
Expand All @@ -38,30 +38,61 @@ def test_point_pattern(self):
nearest neighbor distance computed using a random realization of
the point process.
"""
random.seed() # Reset the random number generator using system time
random.seed(3673673) # 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.037507819095864134, 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 = utils.create_n_rand_pts(100)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
permutations = analytics.p_perms(99)
self.assertEqual(len(permutations), 99)
self.assertNotEqual(permutations[0], permutations[1])

# As above, update the module and function name.
lower, upper = utils.critical_pts(permutations)
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 0.07)
self.assertTrue(observed_avg < lower or observed_avg > upper)

# As above, update the module and function name.
significant = analytics.monte_carlo_critical_bound_check(lower, upper, 0)
self.assertTrue(significant)

self.assertTrue(True)

def test_marks(self):
random.seed(942323) # 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.037507819095864134, observed_avg, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Altering this test tells me that you are not checking for coincident points correctly.


# 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 = utils.create_marked_rand_pts(100,self.marks)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
permutations = point_pattern.permutations(99)
permutations = analytics.p_perms_marks(99,self.marks)
self.assertEqual(len(permutations), 99)
#print(permutations)
self.assertNotEqual(permutations[0], permutations[1])

# As above, update the module and function name.
lower, upper = point_pattern.compute_critical(permutations)
lower, upper = utils.critical_pts(permutations)
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 0.07)
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.monte_carlo_critical_bound_check(lower, upper, 0)
self.assertTrue(significant)

self.assertTrue(False)
self.assertTrue(True)

Loading