Skip to content
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

adding #7

Open
wants to merge 2 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
115 changes: 115 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import random
import math

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 critical_pts(distances):
return min(distances), max(distances)

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=1e9
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=1e9
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
101 changes: 101 additions & 0 deletions functional_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import random
import unittest

import analytics
import utils
from point_pattern import PointPattern
from point import Point
class TestFunctionalPointPattern(unittest.TestCase):

def setUp(self):
self.marks=[]
self.marks.append('r')
self.marks.append('b')
self.pattern=PointPattern()
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))
self.pattern.add_pt(Point(1,1,'r'))



self.pattern.add_pt(Point(1,2,'b'))
self.pattern.add_pt(Point(1,3,'b'))
self.pattern.add_pt(Point(1,4,'b'))

def test_num_coincident(self):
self.assertEqual(self.pattern.number_coincident_points(),7)

def test_list_mark(self):
self.assertEquals(self.point_pattern.list_marks(),self.marks)

def test_point_pattern(self):
"""
This test checks that the code can compute an observed mean
nearest neighbor distance and then use Monte Carlo simulation to
generate some number of permutations. A permutation is the mean
nearest neighbor distance computed using a random realization of
the point process.
"""
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 = 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_marked_rand_pts(100,self.marks)
self.assertEqual(100, len(rand_points))

# As above, update the module and function name.
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 = 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)

152 changes: 152 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import math
from math import sqrt
import utils

class Point():
def __init__(self, x=0, y=0, mark=[]):
self.x = x
self.y = y
self.magnitude = euclidean_distance((self.x,self.y), (0,0))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice inclusion.

self.mark = mark
def __add__(self, val):
return Point(self.x + val, self.y + val)

def __radd__(self, val):
return Point(self.x + val, self.y + val)

def __mul__(self,val):
return Point(self.x*val, self.y*val)
def __rmul__(self, val):
return Point(self.x*val, self.y*val)

def __neg__(self):
return Point(-self.x, -self.y)

def check_if_coincident(self,secondPoint):
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to utilize a magic method for this?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I realized just now that it would be easy to use eq() to check coincidence.

return utils.check_coincident((self.x,self.y),secondPoint)

def shift_point(self, x_shift, y_shift):
(self.x,self.y)=utils.shift_point((self.x,self.y),x_shift,y_shift)
return Point(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,mark=None):
mean_d = 0

if(mark==None):
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))
else:
for i in range(len(points)):
dist_nearest=math.inf
for j in range(len(points)):
dist = utils.euclidean_distance((points[i].x, points[i].y), (points[j].x,points[j].y))
if temp_p1 == temp_p2:
continue
elif dist < dist_nearest and temp_p1==temp_p2:
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 = 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]
Loading