From e8d4503c3c4b714437af8ecd2b0c540befd1376c Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:04:14 -0700 Subject: [PATCH 01/21] Update test_utils.py --- tests/test_utils.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index bcfcb35..a761a19 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,11 +1,19 @@ import os import sys +import random import unittest -sys.path.insert(0, os.path.abspath('..')) -from .. import utils +from ..import utils -class TestUtils(unittest.TestCase): +class TestUtils(unittest.TestCase): def setUp(self): - pass \ No newline at end of file + self.random_points = utils.create_random_points(100) + self.permutations = utils.permutation(99) + + def test_create_random(self): + self.assertEqual(100, len(self.random_points)) + + def test_permutation(self): + self.assertEqual(len(self.permutation), 99) + self.assertNotEqual(self.permutation[0], self.permutation[1]) From 91890c3fc4575a851a2a22a6f5c78d03134626e2 Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:04:54 -0700 Subject: [PATCH 02/21] Update test_io_geojson.py --- tests/test_io_geojson.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_io_geojson.py b/tests/test_io_geojson.py index 5394cd2..0896923 100644 --- a/tests/test_io_geojson.py +++ b/tests/test_io_geojson.py @@ -1,11 +1,13 @@ -import os -import sys import unittest -sys.path.insert(0, os.path.abspath('..')) +import io_geojson -from .. import io_geojson class TestIoGeoJson(unittest.TestCase): - def setUp(self): - pass \ No newline at end of file + @classmethod + def setUp(cls): + cls.gj = io_geojson.read_geojson('data/us_cities.geojson') + + def read_geojson(self): + self.assertIsInstance(self.gj, dict) + From e2091f2544d0adb342abc1c496990b70aac1fcf4 Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:05:27 -0700 Subject: [PATCH 03/21] Update test_analytics.py --- tests/test_analytics.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 9714da3..baf7ab7 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -1,11 +1,26 @@ import os import sys import unittest + +from numpy.core.defchararray import upper, lower + sys.path.insert(0, os.path.abspath('..')) from .. import analytics + class TestAnalytics(unittest.TestCase): def setUp(self): - pass \ No newline at end of file + pass + + def test_compute_critical(self): + self.assertTrue(lower == 1) + self.assertTrue(upper == 0) + + def test_check_significant(self): + + significant = [analytics.check_significant(self.lower, self.upper, 3)] + + self.assertTrue(significant[0]) + self.assertFalse(significant[1]) From 4affa71c44ba9282987ae2a60028280506e840fb Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:06:42 -0700 Subject: [PATCH 04/21] Update utils.py --- utils.py | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/utils.py b/utils.py index e69de29..e111527 100644 --- a/utils.py +++ b/utils.py @@ -0,0 +1,109 @@ +import math +import random + +""" + 1. Organize the functions that were in point_pattern.py into the appropriate modules. + 2. Update the tests to reflect the new structure. + 3. Write a function to generate $n$ random points, where the user defines $n$. + 4. Write a function that takes $p$ an integer number of permutations. For each + permutation, create $n$ random points and compute the mean nearest neighbor distance. + Let this function default to p=99 and n=100. Make sure that the user can alter these + values if they like. + 5. Write a function to compute the critical points in the results returned by the + function you wrote in 4. If p = 99, then the function in 4 should return a list of 99 + distances. This function will take that list and find the smallest and largest distances. + These are the critical points of the Monte Carlo test. + 6. Write a function that takes the critical points of the Monte Carlo simulation and the + observed value and returns True is the observed distance is significant, i.e., less than + or greater than the observed. Otherwise, return False. + 7. Write tests for items 3, 4, 5, and 6. + 8. Look at the file, functional_test.py. In that file I have written a single functional + test that ties together all of your previous work. For this test, you should replace the + module and function names with your own values. For example, I assume that all the + necessary methods are in the point_pattern module. Since you refactored the structure of + the code in completing #1, the point_pattern module does not exist. Instead, analytics + and utils do. Additionally,for #3 - 5, you wrote functions. I guessed at what you might + name these, but leave it to you to update the functional test with the names you selected. +""" + + +def manhattan_distance(a, b): + + distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) + return distance + + +def euclidean_distance(a, b): + + distance = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) + return distance + + +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 getx(point): + + return point[0] + + +def gety(point): + + return point[1] + + +def average_nearest_neighbor_distance(points): + + mean_d = 0 + nearest_neighbor = None + + for point in points: + for otherPoint in points: + if check_coincident(point, otherPoint): + continue + current_distance = euclidean_distance(point, otherPoint) + if nearest_neighbor is None: + nearest_neighbor = current_distance + elif nearest_neighbor > current_distance: + nearest_neighbor = current_distance + + mean_d += nearest_neighbor + nearest_neighbor = None + + mean_d /= len(points) + + return mean_d + + +def create_random_points(n): + + random_points = [(random.uniform(0, 1), random.uniform(0, 1)) in range(n)] + + return random_points + + +def permutation(p=99, n=100): + + per = [] + for x in range(p): + per.append(average_nearest_neighbor_distance(create_random_points(n))) + + return per From 3d64b973ccf5928daa86bd80dbbddb490f1135d4 Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:07:11 -0700 Subject: [PATCH 05/21] Update io_geojson.py --- io_geojson.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/io_geojson.py b/io_geojson.py index e69de29..06b320b 100644 --- a/io_geojson.py +++ b/io_geojson.py @@ -0,0 +1,24 @@ +import json + + +def read_geojson(input_file): + + with open(input_file, 'r') as f: + gj = json.load(f) + + return gj + + +def find_largest_city(gj): + + city = None + max_population = 0 + + for i in gj['features']: + if i['properties']['pop_max'] > max_population: + max_population = i['properties']['pop_max'] + city = i['properties']['name'] + + return city, max_population + + From 1508485f08324e9e0452957d378b0458972fc88f Mon Sep 17 00:00:00 2001 From: aciepli Date: Tue, 15 Mar 2016 23:07:47 -0700 Subject: [PATCH 06/21] Update analytics.py --- analytics.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/analytics.py b/analytics.py index e69de29..61130c9 100644 --- a/analytics.py +++ b/analytics.py @@ -0,0 +1,98 @@ +import math +import random + + +""" + 1. Organize the functions that were in point_pattern.py into the appropriate modules. + 2. Update the tests to reflect the new structure. + 3. Write a function to generate $n$ random points, where the user defines $n$. + 4. Write a function that takes $p$ an integer number of permutations. For each + permutation, create $n$ random points and compute the mean nearest neighbor distance. + Let this function default to p=99 and n=100. Make sure that the user can alter these + values if they like. + 5. Write a function to compute the critical points in the results returned by the + function you wrote in 4. If p = 99, then the function in 4 should return a list of 99 + distances. This function will take that list and find the smallest and largest distances. + These are the critical points of the Monte Carlo test. + 6. Write a function that takes the critical points of the Monte Carlo simulation and the + observed value and returns True is the observed distance is significant, i.e., less than + or greater than the observed. Otherwise, return False. + 7. Write tests for items 3, 4, 5, and 6. + 8. Look at the file, functional_test.py. In that file I have written a single functional + test that ties together all of your previous work. For this test, you should replace the + module and function names with your own values. For example, I assume that all the + necessary methods are in the point_pattern module. Since you refactored the structure of + the code in completing #1, the point_pattern module does not exist. Instead, analytics + and utils do. Additionally,for #3 - 5, you wrote functions. I guessed at what you might + name these, but leave it to you to update the functional test with the names you selected. +""" + + +def mean_center(points): + + x = 0 + y = 0 + + for i in points: + x += i[0] + y += i[1] + + x /= len(points) + y /= len(points) + + return x, y + + +def minimum_bounding_rectangle(points): + + mbr = [0, 0, 0, 0] + x_min = 0 + x_max = 0 + y_min = 0 + y_max = 0 + + for p in points: + if p[0] < x_min: + x_min = p[0] + if p[0] > x_max: + x_max = p[0] + if p[1] < y_min: + y_min = p[1] + if p[1] > y_max: + y_max = p[1] + mbr = [x_min,y_min,x_max, y_max] + + return mbr + + +def mbr_area(mbr): + + l = mbr[2] - mbr[0] + w = mbr[3] - mbr[1] + area = l * w + + return area + + +def expected_distance(area, n): + + expected = 0.5 * (math.sqrt(area / n)) + + return expected + + +def compute_critical(p): + lower = min(p) + upper = max(p) + return lower, upper + + +def check_significant(lower, upper, observed): + + if lower < observed < upper: + result = True + else: + result = False + + return result + From e96f4926cd5caa392700697bc228dfb1591188f8 Mon Sep 17 00:00:00 2001 From: aciepli Date: Wed, 16 Mar 2016 11:38:06 -0700 Subject: [PATCH 07/21] Update functional_test.py --- tests/functional_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/functional_test.py b/tests/functional_test.py index 596af78..3f652c8 100644 --- a/tests/functional_test.py +++ b/tests/functional_test.py @@ -6,7 +6,7 @@ from .. import utils -class TestFunctionalPointPattern(unittest.TestCase): +class TestFunctionalutils(unittest.TestCase): def setUp(self): random.seed(12345) @@ -30,7 +30,7 @@ def setUp(self): if i == 100: break - def test_point_pattern(self): + def test_utils(self): """ This test checks that the code can compute an observed mean nearest neighbor distance and then use Monte Carlo simulation to @@ -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 = utils.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) + 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 = utils.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 = utils.check_significant(lower, upper, observed) self.assertTrue(significant) - self.assertTrue(False) \ No newline at end of file + self.assertTrue(False) From f3c750fe536d47dc6c218b9b07bea8d75efb6acf Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 20 Mar 2016 17:48:13 -0700 Subject: [PATCH 08/21] Update analytics.py --- analytics.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/analytics.py b/analytics.py index 61130c9..13101e6 100644 --- a/analytics.py +++ b/analytics.py @@ -1,6 +1,6 @@ import math -import random +import utils """ 1. Organize the functions that were in point_pattern.py into the appropriate modules. @@ -60,7 +60,7 @@ def minimum_bounding_rectangle(points): y_min = p[1] if p[1] > y_max: y_max = p[1] - mbr = [x_min,y_min,x_max, y_max] + mbr = [x_min, y_min, x_max, y_max] return mbr @@ -81,15 +81,17 @@ def expected_distance(area, n): return expected -def compute_critical(p): - lower = min(p) - upper = max(p) +def compute_critical(points): + + lower = min(points) + upper = max(points) + return lower, upper def check_significant(lower, upper, observed): - if lower < observed < upper: + if (lower < observed) or (observed < upper): result = True else: result = False From fd8682c6a1ac1d403abeb24c38eac6225fc2aa1b Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 20 Mar 2016 17:49:13 -0700 Subject: [PATCH 09/21] Update test_analytics.py --- tests/test_analytics.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_analytics.py b/tests/test_analytics.py index baf7ab7..522cf53 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -12,15 +12,17 @@ class TestAnalytics(unittest.TestCase): def setUp(self): + pass def test_compute_critical(self): - self.assertTrue(lower == 1) - self.assertTrue(upper == 0) + + self.assertTrue(self.lower == 0) + self.assertTrue(self.upper == 9) def test_check_significant(self): - significant = [analytics.check_significant(self.lower, self.upper, 3)] + significant = [analytics.check_significant(self.lower, self.upper, self.observed)] self.assertTrue(significant[0]) self.assertFalse(significant[1]) From 77173490620e05b487bd6119ec5d1d09dd2de8fb Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 20 Mar 2016 17:49:49 -0700 Subject: [PATCH 10/21] Update utils.py --- utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils.py b/utils.py index e111527..314f41a 100644 --- a/utils.py +++ b/utils.py @@ -107,3 +107,4 @@ def permutation(p=99, n=100): per.append(average_nearest_neighbor_distance(create_random_points(n))) return per + From a6ae03d6a7d6ce6260218a094439728e6f4b6bf9 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:08:12 -0700 Subject: [PATCH 11/21] Update analytics.py --- analytics.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/analytics.py b/analytics.py index 13101e6..4bd7ef9 100644 --- a/analytics.py +++ b/analytics.py @@ -1,6 +1,4 @@ -import math - -import utils +from utils import * """ 1. Organize the functions that were in point_pattern.py into the appropriate modules. @@ -83,15 +81,15 @@ def expected_distance(area, n): def compute_critical(points): - lower = min(points) - upper = max(points) + l = min(points) + u = max(points) - return lower, upper + return l, u -def check_significant(lower, upper, observed): +def check_significant(l, u, observed): - if (lower < observed) or (observed < upper): + if (l < observed) or (observed < u): result = True else: result = False From 1d704cbe6f364555946704b5816c8082bd8b33ff Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:09:10 -0700 Subject: [PATCH 12/21] Update test_analytics.py --- tests/test_analytics.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 522cf53..26d05c6 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -2,8 +2,6 @@ import sys import unittest -from numpy.core.defchararray import upper, lower - sys.path.insert(0, os.path.abspath('..')) from .. import analytics @@ -17,12 +15,13 @@ def setUp(self): def test_compute_critical(self): - self.assertTrue(self.lower == 0) - self.assertTrue(self.upper == 9) + self.assertTrue(self.l == 0) + self.assertTrue(self.u == 9) def test_check_significant(self): - significant = [analytics.check_significant(self.lower, self.upper, self.observed)] + significant = [analytics.check_significant(self.l, self.u, self.observed)] self.assertTrue(significant[0]) self.assertFalse(significant[1]) + From 1a0f913de544bf7be6a63f5f3897ed1f4279c138 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:09:46 -0700 Subject: [PATCH 13/21] Update test_utils.py --- tests/test_utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index a761a19..92e3639 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,3 @@ -import os -import sys -import random import unittest from ..import utils From b2922b9b2f28f593009a16bc42733eadb2355428 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:10:32 -0700 Subject: [PATCH 14/21] Update utils.py --- utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.py b/utils.py index 314f41a..27dfc6a 100644 --- a/utils.py +++ b/utils.py @@ -85,7 +85,7 @@ def average_nearest_neighbor_distance(points): elif nearest_neighbor > current_distance: nearest_neighbor = current_distance - mean_d += nearest_neighbor + mean_d = mean_d + nearest_neighbor nearest_neighbor = None mean_d /= len(points) From b96e1a1fdb08a41954be8237848d9055ec0f574c Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:22:26 -0700 Subject: [PATCH 15/21] Update analytics.py --- analytics.py | 100 ++++++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 44 deletions(-) diff --git a/analytics.py b/analytics.py index 4bd7ef9..27dfc6a 100644 --- a/analytics.py +++ b/analytics.py @@ -1,4 +1,5 @@ -from utils import * +import math +import random """ 1. Organize the functions that were in point_pattern.py into the appropriate modules. @@ -26,73 +27,84 @@ """ -def mean_center(points): +def manhattan_distance(a, b): - x = 0 - y = 0 + distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) + return distance - for i in points: - x += i[0] - y += i[1] - x /= len(points) - y /= len(points) +def euclidean_distance(a, b): + + distance = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) + return distance + + +def shift_point(point, x_shift, y_shift): + + x = getx(point) + y = gety(point) + + x += x_shift + y += y_shift return x, y -def minimum_bounding_rectangle(points): +def check_coincident(a, b): + + return a == b + + +def check_in(point, point_list): + + return point in point_list + + +def getx(point): - mbr = [0, 0, 0, 0] - x_min = 0 - x_max = 0 - y_min = 0 - y_max = 0 + return point[0] - for p in points: - if p[0] < x_min: - x_min = p[0] - if p[0] > x_max: - x_max = p[0] - if p[1] < y_min: - y_min = p[1] - if p[1] > y_max: - y_max = p[1] - mbr = [x_min, y_min, x_max, y_max] - return mbr +def gety(point): + return point[1] -def mbr_area(mbr): - l = mbr[2] - mbr[0] - w = mbr[3] - mbr[1] - area = l * w +def average_nearest_neighbor_distance(points): - return area + mean_d = 0 + nearest_neighbor = None + for point in points: + for otherPoint in points: + if check_coincident(point, otherPoint): + continue + current_distance = euclidean_distance(point, otherPoint) + if nearest_neighbor is None: + nearest_neighbor = current_distance + elif nearest_neighbor > current_distance: + nearest_neighbor = current_distance -def expected_distance(area, n): + mean_d = mean_d + nearest_neighbor + nearest_neighbor = None - expected = 0.5 * (math.sqrt(area / n)) + mean_d /= len(points) - return expected + return mean_d -def compute_critical(points): +def create_random_points(n): - l = min(points) - u = max(points) + random_points = [(random.uniform(0, 1), random.uniform(0, 1)) in range(n)] - return l, u + return random_points -def check_significant(l, u, observed): +def permutation(p=99, n=100): - if (l < observed) or (observed < u): - result = True - else: - result = False + per = [] + for x in range(p): + per.append(average_nearest_neighbor_distance(create_random_points(n))) - return result + return per From aff2d3ae58186d8c6dd3ccb3404cfe3769903191 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:24:25 -0700 Subject: [PATCH 16/21] Update test_analytics.py --- tests/test_analytics.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 26d05c6..19a6fd6 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -24,4 +24,3 @@ def test_check_significant(self): self.assertTrue(significant[0]) self.assertFalse(significant[1]) - From 24c33150426465b9df6f186f69719bebb223b7e5 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:39:59 -0700 Subject: [PATCH 17/21] Update analytics.py --- analytics.py | 101 ++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/analytics.py b/analytics.py index 27dfc6a..f5e4cdd 100644 --- a/analytics.py +++ b/analytics.py @@ -1,5 +1,4 @@ -import math -import random +from utils import * """ 1. Organize the functions that were in point_pattern.py into the appropriate modules. @@ -27,84 +26,72 @@ """ -def manhattan_distance(a, b): +def mean_center(points): - distance = abs(a[0] - b[0]) + abs(a[1] - b[1]) - return distance + x = 0 + y = 0 + for i in points: + x += i[0] + y += i[1] -def euclidean_distance(a, b): - - distance = math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) - return distance - - -def shift_point(point, x_shift, y_shift): - - x = getx(point) - y = gety(point) - - x += x_shift - y += y_shift + x /= len(points) + y /= len(points) return x, y -def check_coincident(a, b): - - return a == b - - -def check_in(point, point_list): - - return point in point_list - - -def getx(point): - - return point[0] +def minimum_bounding_rectangle(points): + mbr = [0, 0, 0, 0] + x_min = 0 + x_max = 0 + y_min = 0 + y_max = 0 -def gety(point): + for p in points: + if p[0] < x_min: + x_min = p[0] + if p[0] > x_max: + x_max = p[0] + if p[1] < y_min: + y_min = p[1] + if p[1] > y_max: + y_max = p[1] + mbr = [x_min, y_min, x_max, y_max] - return point[1] + return mbr -def average_nearest_neighbor_distance(points): +def mbr_area(mbr): - mean_d = 0 - nearest_neighbor = None + l = mbr[2] - mbr[0] + w = mbr[3] - mbr[1] + area = l * w - for point in points: - for otherPoint in points: - if check_coincident(point, otherPoint): - continue - current_distance = euclidean_distance(point, otherPoint) - if nearest_neighbor is None: - nearest_neighbor = current_distance - elif nearest_neighbor > current_distance: - nearest_neighbor = current_distance + return area - mean_d = mean_d + nearest_neighbor - nearest_neighbor = None - mean_d /= len(points) +def expected_distance(area, n): - return mean_d + expected = 0.5 * (math.sqrt(area / n)) + return expected -def create_random_points(n): - random_points = [(random.uniform(0, 1), random.uniform(0, 1)) in range(n)] +def compute_critical(points): - return random_points + l = min(points) + u = max(points) + return l, u -def permutation(p=99, n=100): - per = [] - for x in range(p): - per.append(average_nearest_neighbor_distance(create_random_points(n))) +def check_significant(l, u, observed): - return per + if (l < observed) or (observed < u): + result = True + else: + result = False + return result From 659b6f07b7e835abe65501f11b1e1e4de8d51fc0 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:41:48 -0700 Subject: [PATCH 18/21] Update test_io_geojson.py --- tests/test_io_geojson.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_io_geojson.py b/tests/test_io_geojson.py index 0896923..f2ebc8f 100644 --- a/tests/test_io_geojson.py +++ b/tests/test_io_geojson.py @@ -1,4 +1,5 @@ import unittest + import io_geojson From 56638c2a5584f971d5160ecd41282cf31cc4be92 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 16:43:09 -0700 Subject: [PATCH 19/21] Update utils.py --- utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils.py b/utils.py index 27dfc6a..c6ac74e 100644 --- a/utils.py +++ b/utils.py @@ -73,7 +73,7 @@ def gety(point): def average_nearest_neighbor_distance(points): mean_d = 0 - nearest_neighbor = None + nearest_neighbor = math.inf for point in points: for otherPoint in points: @@ -85,7 +85,7 @@ def average_nearest_neighbor_distance(points): elif nearest_neighbor > current_distance: nearest_neighbor = current_distance - mean_d = mean_d + nearest_neighbor + mean_d += nearest_neighbor nearest_neighbor = None mean_d /= len(points) @@ -108,3 +108,4 @@ def permutation(p=99, n=100): return per + From b5e7292cd7e2fe857887e46c6cb7f02b93436086 Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 17:04:49 -0700 Subject: [PATCH 20/21] Update functional_test.py --- tests/functional_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/functional_test.py b/tests/functional_test.py index 3f652c8..176ddad 100644 --- a/tests/functional_test.py +++ b/tests/functional_test.py @@ -1,12 +1,12 @@ -import random import unittest +import random -from .. import analytics from .. import io_geojson +from .. import analytics from .. import utils -class TestFunctionalutils(unittest.TestCase): +class TestFunctionalPointPattern(unittest.TestCase): def setUp(self): random.seed(12345) @@ -30,7 +30,7 @@ def setUp(self): if i == 100: break - def test_utils(self): + def test_point_pattern(self): """ This test checks that the code can compute an observed mean nearest neighbor distance and then use Monte Carlo simulation to @@ -41,7 +41,7 @@ def test_utils(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 = utils.average_nearest_neighbor_distance(self.points) - self.assertAlmostEqual(0.027, observed_avg, 3) + self.assertAlmostEqual(0.030, 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 @@ -61,7 +61,7 @@ def test_utils(self): self.assertTrue(observed_avg < lower or observed_avg > upper) # As above, update the module and function name. - significant = utils.check_significant(lower, upper, observed) + significant = utils.check_significant(lower, upper, observed_avg) self.assertTrue(significant) self.assertTrue(False) From d57c495d6ddef93c14db0de6940935fc1273ecbc Mon Sep 17 00:00:00 2001 From: aciepli Date: Sun, 27 Mar 2016 17:06:50 -0700 Subject: [PATCH 21/21] Update utils.py --- utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/utils.py b/utils.py index c6ac74e..b7edc11 100644 --- a/utils.py +++ b/utils.py @@ -108,4 +108,3 @@ def permutation(p=99, n=100): return per -