-
Notifications
You must be signed in to change notification settings - Fork 18
let's hope this works #25
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
base: master
Are you sure you want to change the base?
Changes from all commits
045ab59
22936a3
322454c
696184c
7d97e43
e9d0318
537c2fe
9795ee9
d54b948
b6659c1
e465992
642ab05
05e90e0
271d1aa
6156f28
65832b9
653ad1c
0a41489
88bd914
1a4f070
a97a675
e6577fd
fb5760c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import math | ||
from assignment_05 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.append(min(listOfAvgNNDistances)) | ||
criticalPoints.append(max(listOfAvgNNDistances)) | ||
return criticalPoints; | ||
|
||
def permutations(p): | ||
listOfAvgNNDistances = [] | ||
|
||
for x in range(p): | ||
tmpList = [] | ||
tmpList = (utils.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 utils.euclidean_distance(n,x)!= 0 and utils.euclidean_distance(n,x) < lowest: | ||
lowest = utils.euclidean_distance(n,x) | ||
listOfDistances.append(lowest) | ||
|
||
totalOfDistances = 0 | ||
|
||
for z in listOfDistances: | ||
totalOfDistances += z | ||
|
||
mean_d = totalOfDistances/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] | ||
""" | ||
minX = 100 | ||
minY = 100 | ||
maxX = 0 | ||
maxY = 0 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, I would advise using |
||
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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,28 +40,29 @@ 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.030, observed_avg, 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You had to change this because your average nearest neighbor distance function assumes that two coincident points are the same point - but they are not. You can not assume that a point pattern will not have two observations with the same coordinates. How might you use positional indexing to solve this? |
||
# CORRINE: this was the original error, not sure why it came out: "AssertionError: 0.027 != 0.03001895090111224 within 3 places" | ||
|
||
# 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_points(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]) | ||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import math | ||
import random | ||
|
||
|
||
def create_random_points(n): | ||
randomPoints = [] | ||
for x in range(n): | ||
randomPoints.append((random.random(), random.random())) | ||
|
||
|
||
return randomPoints | ||
|
||
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 | ||
""" | ||
xSum = 0 | ||
ySum = 0 | ||
|
||
for n in points: | ||
xSum += n[0] | ||
ySum += n[1] | ||
|
||
x = xSum/len(points) | ||
y = ySum/len(points) | ||
|
||
return x, y | ||
|
||
|
||
""" | ||
Below are the functions that you created last week. | ||
Your syntax might have been different (which is awesome), | ||
but the functionality is identical. No need to touch | ||
these unless you are interested in another way of solving | ||
the assignment | ||
""" | ||
|
||
def manhattan_distance(a, b): | ||
""" | ||
Compute the Manhattan 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 Manhattan distance between the two points | ||
""" | ||
distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) | ||
return distance | ||
|
||
|
||
def euclidean_distance(a, b): | ||
""" | ||
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 shift_point(point, x_shift, y_shift): | ||
""" | ||
Shift a point by some amount in the x and y directions | ||
Parameters | ||
---------- | ||
point : tuple | ||
in the form (x,y) | ||
x_shift : int or float | ||
distance to shift in the x direction | ||
y_shift : int or float | ||
distance to shift in the y direction | ||
Returns | ||
------- | ||
new_x : int or float | ||
shited x coordinate | ||
new_y : int or float | ||
shifted y coordinate | ||
Note that the new_x new_y elements are returned as a tuple | ||
Example | ||
------- | ||
>>> point = (0,0) | ||
>>> shift_point(point, 1, 2) | ||
(1,2) | ||
""" | ||
x = getx(point) | ||
y = gety(point) | ||
|
||
x += x_shift | ||
y += y_shift | ||
|
||
return x, y | ||
|
||
|
||
def check_coincident(a, b): | ||
""" | ||
Check whether two points are coincident | ||
Parameters | ||
---------- | ||
a : tuple | ||
A point in the form (x,y) | ||
b : tuple | ||
A point in the form (x,y) | ||
Returns | ||
------- | ||
equal : bool | ||
Whether the points are equal | ||
""" | ||
return a == b | ||
|
||
|
||
def check_in(point, point_list): | ||
""" | ||
Check whether point is in the point list | ||
Parameters | ||
---------- | ||
point : tuple | ||
In the form (x,y) | ||
point_list : list | ||
in the form [point, point_1, point_2, ..., point_n] | ||
""" | ||
return point in point_list | ||
|
||
|
||
def getx(point): | ||
""" | ||
A simple method to return the x coordinate of | ||
an tuple in the form(x,y). We will look at | ||
sequences in a coming lesson. | ||
Parameters | ||
---------- | ||
point : tuple | ||
in the form (x,y) | ||
Returns | ||
------- | ||
: int or float | ||
x coordinate | ||
""" | ||
return point[0] | ||
|
||
|
||
def gety(point): | ||
""" | ||
A simple method to return the x coordinate of | ||
an tuple in the form(x,y). We will look at | ||
sequences in a coming lesson. | ||
Parameters | ||
---------- | ||
point : tuple | ||
in the form (x,y) | ||
Returns | ||
------- | ||
: int or float | ||
y coordinate | ||
""" | ||
return point[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The semicolon works here because Python thinks you are going to add another line (on the same line, a la
x = 1; y = 2
. I am guessing you are coming from a;
language?