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

.... is this thing even on? #13

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
146 changes: 146 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import math
from assignment_06 import utils

def check_significant(lower, upper, observed_avg):
significance = False
if(upper - lower <= observed_avg):
significance = True
return significance

def compute_critical(listOfAvgNNDistances):
criticalPoints = []
criticalPoints[0] = min(listOfAvgNNDistances)
criticalPoints[1] = max(listOfAvgNNDistances)
return criticalPoints

def permutations(p):
listOfAvgNNDistances = []

for x in range(p):
tmpList = []
tmpList.append(create_random_points(100))
listOfAvgNNDistances.append(average_nearest_neighbor_distance(tmpList))

return listOfAvgNNDistances

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.
"""
listOfDistances = []

for n in points:
lowest = 100
for x in points:
if euclidean_distance(n,x)!= 0 and euclidean_distance(n,x) < lowest:
lowest = euclidean_distance(n,x)
listOfDistances.append(lowest)

totalOfDistances = 0

for z in listOfDistances:
totalOfDistances += z

mean_d = totalOfDistances/len(points)

return mean_d

def euclidean_distance(a, b):
Copy link
Contributor

Choose a reason for hiding this comment

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

TravisCI is complaining about an indentation error here. Indentation looks good though. I see this sometimes if I use a text editor for a bit (with 4 spaces) and then switch to an IDE using tab. Not sure if that is the case, and not a big deal. More just an FYI incase you see that error in the future for your own work.

"""
Compute the Euclidean distance between two points
Parameters
----------
a : tuple
A point in the form (x,y)
b : tuple
A point in the form (x,y)
Returns
-------
distance : float
The Euclidean distance between the two points
"""
distance = math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
return distance


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]
"""
minX = 100
minY = 100
maxX = 0
maxY = 0

for n in points:
if n[0] < minX:
minX = n[0]
if n[0] > maxX:
maxX = n[0]
if n[1] < minY:
minY = n[1]
if n[1] > maxY:
maxY = n[1]

mbr = [minX,minY,maxX,maxY]

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 = 0.5 * (math.sqrt(area/n))


return expected


13 changes: 13 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Point(object):
def __init__(self,x,y,mark={}):
self.x = x
self.y = y
self.mark = mark

def check_coincident(self,point2):
check_this_point = (self.x,self.y)
return check_coincident(point1, point2)

def shift_point(self, x_shift, y_shift):
point = (self.x, self.y)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a legitimate indentation issue. Be careful as Python is able to avoid {} and ; by enforcing strick indentation rules.

self.x, self.y = shift_point(point, x_shift, y_shift)
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)
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))
create_random_points = utils.create_random(100)
self.assertEqual(100, len(create_random_points))

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

self.assertTrue(False)
self.assertTrue(True)
46 changes: 46 additions & 0 deletions tests/point_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
import random

from .. import point
from collections import Counter

class TestPoint(unittest.TestCase):

point = Point(2,4)

def point_Test(self):
self.assertEqual(2, point.x)
self.assertEqual(4, point.y)

def coincidence_Test(self):
self.assertTrue(point.check_coincidence(2,4))
self.assertFalse(point.check_coincidence(3,5))

def shift_point_Test(self):
new_point = Point(2,1)
self.assertEqual((4,5), (point.x,point.y))


def marks_Test(self):
random.seed(12345)
marks = ['periwinkle', 'eggshell', 'lavender', 'chiffon']
i = 0

points = []
countArray = []

while i < 15:
new_added_point = Point(random.randint(0,9), random.randint(0,9), random.choice(marks))
points.append(new_point)

for counter in points:

Counter(marks)
Counter({'periwinkle': 1, 'eggshell': 2, 'lavender': 3, 'chiffon': 4})







Loading