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

First Commit #9

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

features = gj['features']
for i in features:
if(i['properties']['pop_max']>max_population):
max_population = i['properties']['pop_max']
city = i['properties']['name']

return city, max_population

def write_your_own(gj):
"""
Here you will write your own code to find
some attribute in the supplied geojson file.

Take a look at the attributes available and pick
something interesting that you might like to find
or summarize. This is totally up to you.

Do not forget to write the accompanying test in
tests.py!
"""

#I chose the least creative route of finding the average population among all cities

sum_population = 0
n = 0

features = gj['features']
for i in features:
sum_population = i['properties']['pop_max']+sum_population
n = n+1

return sum_population/n

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
n = 0

for i in points:
x = x+i[0]
y = y+i[1]
n = n+1

x = x/n
y = y/n
return x, y


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

# #create empty list of distances
# shortestDistList = []

# for i in points:
# shortest = 9999999999
# for j in points:
# if i!=j:
# current = euclidean_distance(i,j)
# if(current<shortest):
# shortest = current
# shortestDistList.append(shortest)

# n = 0
# mean_d = 0
# for i in shortestDistList:
# mean_d = mean_d+i
# n = n+1

# return mean_d/(n)

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

mbr = [0,0,0,0]

x_max = -99999999999
x_min = 999999999999
y_max = -99999999999
y_min = 999999999999
Copy link
Contributor

Choose a reason for hiding this comment

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

You can not assume that these numbers are large enough. What if my point pattern are cities and my units are millimeters?


for i in points:
if i[0] > x_max:
x_max = i[0]
if i[0] < x_min:
x_min = i[0]
if i[1] > y_max:
y_max = i[1]
if i[1] < y_min:
y_min = i[1]

mbr = [x_min,y_min,x_max,y_max]

return mbr

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

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 compute_critical(points):
"""
Compute the "critical" points for the Monte Carlo
simulation as the minimum and maximum of the points

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

(min,max) : int
The minimum and maximum list
"""

return min(points), max(points)

def check_significant(input_min,input_max,X):
"""
Compute the "critical" points for the Monte Carlo
simulation as the minimum and maximum of the points

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

n : int
The number of points
"""

flag = False;

if X>input_max:
flag = True
elif X<input_min:
flag = True

return flag
25 changes: 25 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json # I would like you to use the JSON module for reading geojson (for now)


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
"""
# Please use the python json module (imported above)
# to solve this one.

gj = None

with open(input_file, 'r') as f:
gj = json.load(f)
return gj
20 changes: 20 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#from utils import check_coincident
#import utils
from .utils import *
Copy link
Contributor

Choose a reason for hiding this comment

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

While * imports work, please do not use them. They pollute the namespace in ways that can be hard to predict.


class Point(object):

#Create a point class with three attributes, x, y, and a keyword argument mark. Please place the point pattern class in point.py.
def __init__(self, x, y, mark={}):
self.x = x
self.y = y
self.mark = mark

#Add a method to the Point class to chec if another point, passed as an argument, is coincident. Remember that you already wrote this logic.
def check_coincident(self,other):
return check_coincident((self.x, self.y), (other.x, other.y))

#Add a method to shift the point in some direction. This logic is also already written.
def shift_point(self, dx, dy):
return shift_point((self.x,self.y),dx,dy)

36 changes: 29 additions & 7 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,50 @@ def test_point_pattern(self):
"""
random.seed() # 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 = utils.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.027, observed_avg, 2)

# 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_random(100)
self.assertEqual(100, len(rand_points))

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

# As above, update the module and function name.
lower, upper = point_pattern.compute_critical(permutations)
lower, upper = analytics.compute_critical(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.check_significant(lower, upper, observed_avg)
self.assertTrue(significant)

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

def test_create_random(self):
rand_points = utils.create_random(100)
self.assertEqual(100, len(rand_points))

def test_permutations(self):
permutations = utils.permutations(99)
self.assertEqual(len(permutations), 99)
self.assertNotEqual(permutations[0], permutations[1])

def test_compute_critical(self):
observed_avg = utils.average_nearest_neighbor_distance(self.points)
lower, upper = analytics.compute_critical(utils.permutations(99))
self.assertTrue(lower > 0.03)
self.assertTrue(upper < 0.07)
self.assertTrue(observed_avg < lower or observed_avg > upper)

def test_check_significant(self):
observed_avg = utils.average_nearest_neighbor_distance(self.points)
lower, upper = analytics.compute_critical(utils.permutations(99))
significant = analytics.check_significant(lower, upper, observed_avg)
self.assertTrue(significant)
Loading