Skip to content

my changes #30

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 4 commits 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
172 changes: 172 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import math
from .utils import *


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
sum_x=[]
sum_y=[]
for x_tmp,y_tmp in points:
sum_x.append(x_tmp)
sum_y.append(y_tmp)

x=float(sum(sum_x)/len(sum_x))
y=float(sum(sum_y)/len(sum_y))

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
length=len(points)
nearest_distances=[]
for i in range(length):
distance=[]
for j in range(length):
if i==j:
continue
else:
distance.append(euclidean_distance(points[i],points[j]))
nearest_distances.append(min(distance))

mean_d=float(sum(nearest_distances)/len(nearest_distances))
return mean_d


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 permutation(p=99, n=100):
"""
Return the mean nearest neighbor distance of p permutations.

Parameters
----------
p : integer
n : integer

Returns
-------
permutations : list
the mean nearest neighbor distance list.

"""
permutation_list=[]
for i in range(p):
permutation_list.append(average_nearest_neighbor_distance(generate_random_points(n)))
return permutation_list

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]

x_list=[]
y_list=[]
for x,y in points:
x_list.append(x)
y_list.append(y)
mbr=[min(x_list),min(y_list),max(x_list),max(y_list)]

return mbr


def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
area = 0
area=(mbr[2]-mbr[0])*(mbr[3]-mbr[1])
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 =float((math.sqrt(area/n))/2)
return expected
79 changes: 79 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import json


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.
gj = None
fp = open(input_file, 'r')
gj = json.loads(fp.read())
fp.close()
return gj

def find_largest_city(gj):
"""
Iterate through a geojson feature collection and
find the largest city. Assume that the key
to access the maximum population is 'pop_max'.

Parameters
----------
gj : dict
A GeoJSON file read in as a Python dictionary

Returns
-------
city : str
The largest city

population : int
The population of the largest city
"""
city = None
max_population = 0
for feature in gj["features"]:
if feature["properties"]["pop_max"]>max_population:
max_population=feature["properties"]["pop_max"]
city=feature["properties"]["nameascii"]

return city, max_population


def write_your_own(gj):
"""
Here you will write your own code to find
some attribute in the supplied geojson file.

Take a look at the attributes available and pick
something interesting that you might like to find
or summarize. This is totally up to you.

Do not forget to write the accompanying test in
tests.py!

To find the average of pop_max and pop_min.

"""

sum_pop_max=0
sum_pop_min=0
num=0
for feature in gj["features"]:
sum_pop_max+=feature["properties"]["pop_max"]
sum_pop_min+=feature["properties"]["pop_min"]
num+=1

return float(sum_pop_max/num),float(sum_pop_min/num)
15 changes: 8 additions & 7 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from .. import io_geojson
from .. import utils


class TestFunctionalPointPattern(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -40,28 +39,30 @@ 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)
rand_points = utils.generate_random_points(100)
self.assertEqual(100, len(rand_points))

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

self.assertTrue(False)
self.assertTrue(True)


68 changes: 67 additions & 1 deletion tests/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,77 @@
import os
import sys
import unittest
import random
sys.path.insert(0, os.path.abspath('..'))

from .. import analytics

class TestAnalytics(unittest.TestCase):

def setUp(self):
pass
random.seed(12345)
# A list comprehension to create 50 random points
self.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_euclidean_distance(self):
"""
A test to ensure that the distance between points
is being properly computed.
You do not need to make any changes to this test,
instead, in point_pattern.py, you must complete the
`eucliden_distance` function so that the correct
values are returned.
Something to think about: Why might you want to test
different cases, e.g. all positive integers, positive
and negative floats, coincident points?
"""
point_a = (3, 7)
point_b = (1, 9)
distance = analytics.euclidean_distance(point_a, point_b)
self.assertAlmostEqual(2.8284271, distance, 4)

point_a = (-1.25, 2.35)
point_b = (4.2, -3.1)
distance = analytics.euclidean_distance(point_a, point_b)
self.assertAlmostEqual(7.7074639, distance, 4)

point_a = (0, 0)
point_b = (0, 0)
distance = analytics.euclidean_distance(point_b, point_a)
self.assertAlmostEqual(0.0, distance, 4)


def test_permutation(self):

permutations = analytics.permutation(88)
self.assertEqual(len(permutations), 88)
self.assertNotEqual(permutations[0], permutations[1])
Loading