Skip to content

Completed test cases! #7

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
12 changes: 12 additions & 0 deletions .idea/assignment_05.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

160 changes: 160 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import math
import random
from .utils import euclidean_distance, random_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 = None
#y = None

x = [i[0] for i in points]
y = [i[1] for i in points]

sumX = (sum(x) / len(points))
sumY = (sum(y) / len(points))

x = sumX
y = sumY

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.
"""
mean_d = 0

shortDistanceList = []

for firstPoint in points:
pointInList = 500
for secondPoint in points:
if firstPoint is not secondPoint:
distance = euclidean_distance(firstPoint, secondPoint)
if (pointInList > distance):
pointInList = distance

shortDistanceList.append(pointInList)

mean_d = sum(shortDistanceList) / len(points)

return mean_d


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]

xmin = 0
ymin = 0
xmax = 0
ymax = 0

for i in points:
if i[0] < xmin:
xmin = i[0]
if i[1] < ymin:
ymin = i[1]
if i[0] > xmax:
xmax = i[0]
if i[1] > ymax:
ymax = i[1]

mbr = [xmin,ymin,xmax,ymax]


return mbr


def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = 0

length = mbr[3] - mbr[1]
width = mbr[2] - mbr [0]
area = length * width

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

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

return expected

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

ListOfNum = []

for i in range(p):
ListOfNum.append(average_nearest_neighbor_distance(random_points(n)))

return ListOfNum
19 changes: 19 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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.
with open(input_file,'r') as jFile:
gj = json.load(jFile)
return gj
18 changes: 9 additions & 9 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)
observed_avg = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(0.027, 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)
self.assertEqual(100, len(rand_points))
random_points = utils.random_points(100)
self.assertEqual(100, len(random_points))

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

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

self.assertTrue(False)
self.assertTrue(True)
49 changes: 48 additions & 1 deletion tests/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
import os
import sys
import unittest
import random
import math

sys.path.insert(0, os.path.abspath('..'))

from .. import analytics

class TestAnalytics(unittest.TestCase):

def setUp(self):
pass
pass

@classmethod
def setUpClass(cls):
# Seed a random number generator so we get the same random values every time
random.seed(12345)
# A list comprehension to create 50 random points
cls.points = [(random.randint(0,100), random.randint(0,100)) for i in range(50)]

def test_average_nearest_neighbor_distance(self):
mean_d = analytics.average_nearest_neighbor_distance(self.points)
self.assertAlmostEqual(mean_d, 7.629178, 5)

def test_mean_center(self):
"""
Something to think about - What values would you
expect to see here and why? Why are the values
not what you might expect?
"""
x, y = analytics.mean_center(self.points)
self.assertEqual(x, 47.52)
self.assertEqual(y, 45.14)

def test_minimum_bounding_rectangle(self):
mbr = analytics.minimum_bounding_rectangle(self.points)
self.assertEqual(mbr, [0,0,94,98])

def test_mbr_area(self):
mbr = [0,0,94,98]
area = analytics.mbr_area(mbr)
self.assertEqual(area, 9212)

def test_expected_distance(self):
area = 9212
npoints = 50
expected = analytics.expected_distance(area, npoints)
self.assertAlmostEqual(expected, 6.7867518, 5)

def test_num_permutations(self):

ListOfNum = analytics.num_permutations(8,8)
self.assertEqual(len(ListOfNum), 8)

ListOfNum = analytics.num_permutations()
self.assertEqual(len(ListOfNum), 99)
1 change: 1 addition & 0 deletions tests/test_io_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

class TestIoGeoJson(unittest.TestCase):

@classmethod
def setUp(self):
pass
Loading