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

Working Code #8

Open
wants to merge 9 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
1 change: 1 addition & 0 deletions Plot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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


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

return mean_nn_dist

def create_n_rand_pts(n):
n_pts = [(random.uniform(0,1), random.uniform(0,1)) for i in range(n)]
return n_pts

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(temp,marks)
mean_nn_dist.append(temp1)

return mean_nn_dist

def create_marked_rand_pts(n,marks=None):
n_pts=[]
for i in range(n):
chosen_mark=random.choice(marks)
temppt=point.Point(random.uniform(0,1), random.uniform(0,1),chosen_mark)
#print(temppt.x)
n_pts.append(temppt)
return n_pts
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(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 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
"""
24 changes: 24 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
import folium
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
129 changes: 129 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import math
from math import sqrt


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))
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):
return (self.x == secondPoint.getx()) and (self.y == secondPoint.gety())

def shiftPoint(self, x_shift, y_shift):
self.x += x_shift
self.y += y_shift
def getx(self):
return self.x
def gety(self):
return self.y

def get_mark(self):
return self.mark
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 euclidean_distance(a, b):
distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
return distance

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 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