Skip to content

Initial commit #12

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
6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

Empty file added __init__.py
Empty file.
149 changes: 149 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
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
37 changes: 37 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.
with open(input_file,'r') as f:
gj = json.load(f)
return gj

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 f:
gj = json.load(f)
return gj
40 changes: 40 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from . import utils
import random
import analytics
import numpy as np
import scipy.spatial as ss
import pysal as ps

class Point(object):
def __init__(self, x, y, mark={}):
self.x = x
self.y = y
self.mark = mark

#implement magic methods

def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)

def __eq__(self, other):
return self.x == other.x and self.y == other.y

def __neg__(self):
return Point(-self.x, -self.y)

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

def shiftPoint(self,xShift, yShift):
thePoint = (self.x, self.y)
self.x, self.y = utils.shift_point(thePoint,xShift,yShift)

def numpyPoint(self, x = 0, y = 0, n = 1000):
numpyArray = np.random.uniform(x, y, (n, 2))
list = []
marks = ['James', 'Sarah', 'Nick', 'Michael']
for i in range(len(numpyArray)):
list.append(Point(numpyArray[i][0]))
numpyArray[i][1], random.choice(marks)
return list
91 changes: 91 additions & 0 deletions pointPattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import math
import random
from . point import Point
from . import analytics
from . import utils
import numpy as np

class PointPattern(object):

def __init__(self):
self.thePoints = []

def averageNearestNeighborDistance(self, marks=None):
return analytics.average_nearest_neighbor_distance(self.points, marks)

def coincidentPoints(self):
counter = 0
coincidentList = []

for c in range(len(self.thePoints)):
for o in range(len(self.thePoints)):
if c in coincidentList:
continue
elif c == o:
continue
elif self.thePoints[c] == self.thePoints[o]:
counter = counter + 1;
coincidentList.append(o)

return counter

def listMarks(self):
markList = []

for points in self.thePoints:
if points.mark not in markList:
markList.append(points.mark)

return markList

def subsetPoints(self, mark):
subsetList = []

for points in self.thePoints:
if points.mark == mark:
subsetList.append(points)

return subsetList

def randomPoints(self, none = None):
randomList = []

if none is None:
none = len(self.thePoints)
self.marks = ['James', 'Paul', 'Sarah', 'Michael', 'Nancy', 'Henry']

for n in range(none):
randomList.append(Point(random.randint(1,50), random.randint(1,50), random.choice(self.marks)))

return randomList

def realizationPoints(self, k):
return analytics.num_permutations(self.marks, k)

def criticalPoints(self, marks):
return utils.critical_points(self.realizationPoints(50))

def add(self, points):
self.thePoints.append(points)

def Gfunction(self, nsteps):
ds = np.linspace(0, 50, nsteps)
sum = 0

for s in range(nsteps):
oI = ds[s]
minimumDistance = None
for g in range(len(ds)):
temp = abs(g - oI)

if g is not s:
continue
if minimumDistance is None:
minimumDistance = temp
if minimumDistance > temp:
minimumDistance = temp
else:
continue
sum = sum + minimumDistance

return sum / nsteps
Loading