Skip to content

Winging it. #10

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 14 commits into
base: master
Choose a base branch
from
118 changes: 118 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
created feb 29th, 2016
By Eric Friesenhahn


List of stuff to do

compute_critical(p)
Find_largest_city(gj)
check_significant(lower,upper,observed)
euclidean_distance(a, b)
mean_center(points)
average_nearest_neighbor_distance(points)
minimum_bounding_rectangle(points)
mbr_area(mbr)
expected_distance(area, n)
check_significant(lower,upper,observed)
...among other things

"""

import math


def compute_critical(p):
Copy link
Contributor

Choose a reason for hiding this comment

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

A doc string would be a great addition here:

"""
Parameters
----------------
p : list
     A list of something

Returns
----------
lower : type
            about lower
upper : type
            about upper
"""

lower = min(p)
upper = max(p)

return lower, upper

def check_significant(lower, upper, observed):

return observed < lower or observed > upper

def euclidean_distance(a, b):

distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)

return distance

def find_largest_city(gj):

max_population = 0
for feat in gj['features']:
test_max_pop = feat['properties']['pop_max']
if test_max_pop > max_population:
max_population = test_max_pop
city = feat['properties']['name']

return city, max_population

def mean_center(points):
sumx= 0.0
sumy= 0.0
for coord in points:
sumx+=coord[0]
sumy+=coord[1]
x= sumx/len(points)
y=sumy/len(points)

return x, y

def average_nearest_neighbor_distance(points):
min_dist_sum = 0
for coord_x in points:
first = True
for coord_y in points:
if coord_x == coord_y:
continue
else:
d = euclidean_distance(coord_x, coord_y)
if first:
min_dist = d
first = False
else:
if d < min_dist:
min_dist = d
min_dist_sum += min_dist

mean_d = min_dist_sum / len(points)

return mean_d

def minimum_bounding_retangle(points):
xmin = 0
xmax = 0
ymin = 0
ymax = 0
for coord in points:
if coord[0] < xmin:
xmin = coord[0]
elif coord[0] > xmax:
xmax = coord[0]

if coord[1] < ymin:
ymin = coord[1]
elif coord[1] > ymax:
ymax = coord[1]

xcorner = xmax - xmin
ycorner = ymax - ymin
mbr = [0,0,xcorner,ycorner]

return mbr

def mbr_area(mbr):
length = mbr[3] - mbr[1]
width = mbr[2] - mbr[0]
area = length * width

return area

def expected_distance(area, n):
expected = .5 * math.sqrt(area/n)

return expected


7 changes: 7 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json

def read_geojson(input_file):

with open(input_file, 'r') as fp:
gj = json.load(fp)
return gj
14 changes: 7 additions & 7 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ 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 = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.03001895090111224, 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 = 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, 101)
self.assertTrue(significant)

self.assertTrue(False)
self.assertTrue(True)
16 changes: 14 additions & 2 deletions tests/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
import sys
import unittest
sys.path.insert(0, os.path.abspath('..'))

"""
check yourself before you wreck yourself!
"""
from .. import analytics

class TestAnalytics(unittest.TestCase):

def setUp(self):
pass
vals = [1,2,3,4,5,6,7,8,9]
self.lower, self.upper = analytics.compute_critical(vals)

def test_compute_critical(self):
self.assertTrue(self.lower == 1 and self.upper == 9)

def test_check_significant(self):
significant = [analytics.check_significant(self.lower, self.upper, 9.01),
analytics.check_significant(self.lower, self.upper, 7.77)]
self.assertTrue(significant[0])
self.assertFalse(significant[1])
7 changes: 6 additions & 1 deletion tests/test_io_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
class TestIoGeoJson(unittest.TestCase):

def setUp(self):
pass
pass

def test_read_geojson(self):
pass

"hardest test to make"
12 changes: 11 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@

from .. import utils


class TestUtils(unittest.TestCase):

def setUp(self):
pass

self.rand_points = utils.create_random(100)
self.permutations = utils.permutations(99)

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

def test_permutations(self):
self.assertEqual(len(self.permutations), 99)
self.assertNotEqual(self.permutations[0], self.permutations[1])
75 changes: 75 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
same date as other,
same guy as other.
y'know, german last name?

Stuff to do here

manhattan_distance
euclidean_distance
getx
gety
shift_point
check_coincident
check_in
create_random
permutations

...at least, I hope

"""
import math
import random
from .analytics import average_nearest_neighbor_distance

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

return distance

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

return distance

def getx(point):

return point[0]

def gety(point):

return point[1]

def shift_point(point, x_shift, y_shift):

x = getx(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 create_random(n, seed=None):
random.seed(seed)
points = [(random.uniform(0,1), random.uniform(0,1)) for i in range(n)]

return points

def permutations(p=99, n=100):

perm = []
for x in range(p):
points = create_random(n, None)
avg_nnd = average_nearest_neighbor_distance(points)
perm.append(avg_nnd)

return perm