Skip to content

Assignment 7 #11

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 1 commit 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
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# assignment_07
assignment_07
==============
Empty file added __init__.py
Empty file.
228 changes: 228 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
from .utils import *

def find_largest_city(gj):
"""
Iterate through a geojson feature collection and
find the largest city. Assume that the key
to access the maximum population is 'pop_max'.

Parameters
----------
gj : dict
A GeoJSON file read in as a Python dictionary

Returns
-------
city : str
The largest city

population : int
The population of the largest city
"""
city = None
max_population = 0
for n in gj["features"]:
properties = n["properties"]
if (properties["pop_max"] > max_population):
max_population = properties["pop_max"]
city = properties["adm1name"]

return city, max_population


def alaska_points(gj):
# Find coordinates from Alaska
alaska_points = []
for n in gj["features"]:
properties = n["properties"]
state_name = properties["adm1name"]
if state_name == "Alaska":
alaska_points.append(n)
else:
continue

return alaska_points


def mean_center(points):
"""
Given a set of points, compute the mean center

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
x : float
Mean x coordinate

y : float
Mean y coordinate
"""
x = 0
y = 0
number_of_points = 0

for p in points:
x += p[0]
y += p[1]
number_of_points += 1

x = x / number_of_points
y = y / number_of_points

return x, y


def average_nearest_neighbor_distance(points, mark=None):
"""
Given a set of points, compute the average nearest neighbor.

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
mean_d : float
Average nearest neighbor distance

References
----------
Clark and Evan (1954 Distance to Nearest Neighbor as a
Measure of Spatial Relationships in Populations. Ecology. 35(4)
p. 445-453.
"""
mean_d = 0
temp_p = None

if mark == None:
for p in points:
for q in points:
if check_coincident(p, q):
continue
cached = euclidean_distance(p, q)
if temp_p is None:
temp_p = cached
elif temp_p > cached:
temp_p = cached

mean_d += temp_p
temp_p = None
else:
for p in points:
if p.mark == mark:
for q in points:
if check_coincident(p, q):
continue
cached = euclidean_distance(p, q)
if temp_p is None:
temp_p = cached
elif temp_p > cached:
temp_p = cached

mean_d += temp_p
temp_p = None





return mean_d / len(points)


def minimum_bounding_rectangle(points):
"""
Given a set of points, compute the minimum bounding rectangle.

Parameters
----------
points : list
A list of points in the form (x,y)

Returns
-------
: list
Corners of the MBR in the form [xmin, ymin, xmax, ymax]
"""

min_x = 1000000000
min_y = 1000000000
max_x = -1
max_y = -1

for n in points:
if n[0] < min_x:
min_x = n[0]
if n[0] > max_x:
max_x = n[0]
if n[1] < min_y:
min_y = n[1]
if n[1] > max_y:
max_y = n[1]

mbr = [min_x, min_y, max_x, max_y]

return mbr


def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = ((mbr[2] - mbr[0]) * (mbr[3] - mbr[1]))

return area

def expected_distance(area, n):
"""
Compute the expected mean distance given
some study area.

This makes lots of assumptions and is not
necessarily how you would want to compute
this. This is just an example of the full
analysis pipe, e.g. compute the mean distance
and the expected mean distance.

Parameters
----------
area : float
The area of the study area

n : int
The number of points
"""

expected = (0.5 * (math.sqrt(area/n)))
return expected


def create_random(n, mark=None):
random.seed()
random_points = [(random.randint(0,100), random.randint(0,100), mark) for i in range(n)]
return random_points


def permutations(p=99, n=100):
#Compute the mean nearest neighbor distance
permutationz = []
for i in range(p):
permutationz.append(average_nearest_neighbor_distance(create_random(n)))
return permutationz


def compute_critical(points):
lower_bound = min(points)
upper_bound = max(points)
return lower_bound, upper_bound


def check_significant(lower, upper, observed):
if observed > upper:
return True
if observed < lower:
return True
26 changes: 26 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from urllib.request import urlopen

def read_geojson(input_url):
"""
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
"""
# I still can't seem to open json locally so going the url route
# for now until I figure it out!
# with open(input_file, 'r') as f:
# gj = json.load(f)
response = urlopen(input_url).read().decode('utf8') #For Testing purposes
# response = urlopen("https://api.myjson.com/bins/4587l").read().decode('utf8')
gj = json.loads(response)

return gj
81 changes: 81 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from utils import *
import numpy as np

class Point(object):
def __init__(self, x, y, mark={}):
self.x = x
self.y = y
self.mark = mark

def __add__(self, other):
return self.x + other.x, self.y, other.y

def __div__(self, other):
return self.x / other.x, self.y, other.y

def __sub__(self, other):
return self.x - other.x, self.y, other.y

def create_random_marked_points(self, n, marks=[]):
list_o_random_points = []
for i in range(n):
random_point = Point(random.seed, random.seed, random.choice(marks))
list_o_random_points.append(random_point)
return list_o_random_points

def check_coincident(self, other):
return check_coincident((self.x, self.y), (other.x, other.y))

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

class PointPattern:
def __init__(self):
self.points = []

def average_nearest_neighbor_distance(self, mark=None):
return utils.average_nearest_neighbor_distance(self.points, mark)

def list_of_marks(self):
list_o_marks = []
for point in self.points:
if point.mark not in list_o_marks:
list_o_marks.append(point.mark)
return list_o_marks

def coincident_points(self):
number_of_coincidents = 0
list_o_coincidents = []
for point in range(len(self.points)):
for neighbor in range(len(self.points)):
if point in list_o_coincidents or point==neighbor:
continue
# This was easier to make work than check_coincident
if self.points[point] == self.points[neighbor]:
number_of_coincidents = count + 1
list_o_coincidents.append(neighbor)
return number_of_coincidents

def subset_of_points_by_mark_type(self, mark):
subset_list = []
for point in self.points:
if point.mark == mark:
subset.append(point)
return subset_list

def generate_n_random_points(self, n=None):
n_random_points = create_random_marked_points(n = len(self.points),marks = [])
return n_random_points

def generate_k_patterns(self, k):
return analytics.permutations(self.marks, k)

def nearest_neighbor_critical_points(self):
return analytics.compute_critical(self.generate_k_patterns(100))

def compute_g(self, nsteps):
ds = np.linspace(0, 100, nsteps)
sum_g = 0
for n in range(nsteps):
oi = ds[n]
# Kind of stuck where to go from here, going to come back
Empty file added tests/__init__.py
Empty file.
Loading