Skip to content

First Changes #4

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
Binary file added Open_Folder.ico
Binary file not shown.
44 changes: 44 additions & 0 deletions files/Plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys
from PyQt4 import QtGui

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt

import random


class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)

# a figure instance to plot on

self.fig = plt.subplots()
self.ax = plt.subplots()

# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.fig)

# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)

# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)



def plot(self, x, y, label, color):

# plot data
self.ax.plot(x, y, label=label, color=color)

# refresh canvas
self.canvas.draw()

self.ax.legend(loc=0)
1 change: 1 addition & 0 deletions files/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

118 changes: 118 additions & 0 deletions files/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import math
import random
from .utils import euclidean_distance
from .utils import generate_random



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
"""

temp = 0
city = ""
max_population = 0
for i in gj['features']:
if i['properties']['pop_max'] > temp:
temp = i['properties']['pop_max']
city = i['properties']['name']
max_population = temp

return city, max_population


def average_nearest_neighbor_distance(points, mark=None):
"""
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.
"""

sum_nn_dis = 0

for point_1 in points:
first = True
for point_2 in points:
if point_1 == point_2:
continue
else:
distance = euclidean_distance(point_1, point_2)
if first:
nn_dis = distance
first = False
elif distance < nn_dis:
nn_dis = distance

sum_nn_dis += nn_dis
mean_distance = sum_nn_dis / len(points)
return mean_distance

def avg_nn_marks(points, marks=None):

if marks is not None:
marked_points = list()
for point in points:
if point.mark is marks:
marked_points.append(point)
else:
continue
return average_nearest_neighbor_distance(marked_points)
else:
combined_points = list((point) for point in points)

return average_nearest_neighbor_distance(combined_points)




def permutations(p = 99, marks=None):
n = 100
permutations = []
for i in range(p):
points_list = generate_random(n)
permutations.append(average_nearest_neighbor_distance(utils.generate_random(n), marks))

return permutations


def critical_points(permutations):
"""
Lowest distance and greatest distance of points.
"""

lower = min(permutations)
upper = max(permutations)
return lower, upper


def significant_distance(lower, upper, observed):
if (lower > observed) or (upper < observed):
result = True
else:
result = False

return result
Binary file added files/exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions files/io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 ('data/us_cities.geojson', 'r') as f:
gj = json.load(f)

return gj

def read_twitter(input_file):

with open('F:/GitHub GIS 321/assignment_10/tweets.json', 'r') as fp:
twitter_gj = json.load(fp)
return twitter_gj
34 changes: 34 additions & 0 deletions files/point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from . import utils



class Point(object):

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

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

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

def __ne__(self, other):
return self.x != other.x or self.y != other.y

def __str__(self):
return ("({0}, {1}").format(self.x, self.y)

def array(self):
return [self.x, self.y]

def point_check_coincident(self, test):
return utils.check_coincident((self.x, self.y), test)

def point_shift_point(self, x_shift, y_shift):
test = utils.shift_point((self.x, self.y), x_shift, y_shift)
self.x = utils.getx(test)
self.y = utils.gety(test)

160 changes: 160 additions & 0 deletions files/point_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
from . import point
from . import analytics
from .utils import euclidean_distance
import random
import numpy as np
import scipy.spatial as ss


class PointPattern(object):
def __init__(self):
self.points = []

def add_point(self, point):
self.points.append(point)

def remove_point(self, index):
try:
del(self.points[index])
except:
pass

def average_nearest_neighbor_distance(self, mark=None, points_list=None):

mean_d = 0
if points_list == None:
temp_points = self.points
else:
temp_points = points_list

if mark != None:
temp_points = [point for point in self.points if point.mark == mark]

list = len(temp_points)

nn_dis = []
for i in range(list):
distance = []

for j in range(list):
if i == j:
continue
else:
distance.append(self.euclidian_distance((temp_points[i].x, temp_points[i].y),(temp_points[j].x, temp_points[j].y)))
nn_dis.append(min(distance))

mean_d = (sum(nn_dis)/len(nn_dis))
return mean_d

def average_nearest_neighbor_distance_kdtree(self):
point_list = []
points = self.points
for point in points:
point_list.append(point.array())
stack = np.vstack(point_list)

nn_dis = []
kdtree = ss.KDTree(stack)
for p in stack:
nearest_neighbor_distance, nearest_neighbor = kdtree.query(p, k=2)
nn_dis.append(nearest_neighbor_distance[1])
nn_distances = np.array(nn_dis)

return(np.mean(nn_distances))


def average_nearest_neighbor_distance_numpy(self):
point_list = []
for point in self.points:
point_list.append(point.array())
ndarray = np.array(point_list)
nearest_neighbor = []
temp_nearest_neighbor = None

for i, point_1 in enumerate(ndarray):
for j, point_2 in enumerate(ndarray):
if i == j:
continue
distance = ss.distance.euclidean(point_1, point_2)

if temp_nearest_neighbor is None:
temp_nearest_neighbor = distance
elif temp_nearest_neighbor > distance:
temp_nearest_neighbor = distance

nearest_neighbor.append(temp_nearest_neighbor)
temp_nearest_neighbor = None



def number_of_coincident(self):

nc = 0
indexed = []

for i in range(len(self.points)):
for j in range(len(self.points)):
if j in indexed:
continue
if i == j:
continue
if self.points[i] == self.points[j]:
nc += 1
indexed.append(j)
return nc

def list_marks(self):

marks = []
for point in self.points:
if point.mark is not None and point.mark not in marks:
marks.append(point.mark)
return marks

def subsets_with_mark(self, mark):

marked_points = []
for points in self.points:
if points.mark == mark:
marked_points.append(points)
return marked_points

def generate_random_points(self, n=None):

if n is None:
n = len(self.points)
random_points = []
self.marks = ['Blue', 'Rare', 'Medium-Rare', 'Medium', 'Medium-Well', 'Well-Done']

for i in range(n):
random_points.append(Point(round(random.random(), 2), round(random.random(), 2), random.choice(self.marks)))
return random_points

def generate_realizations(self, k):

return analytics.permutations(k)

def get_critical_points(self):

return analytics.compute_critical(self.generate_realizations(100))

def compute_g(self, nsteps):

disc_step = np.linspace(0, 1, nsteps)
sum = 0
for i in range(nsteps):
i_step = disc_step[i]
min_dist = None
for j in range(len(disc_step)):
if i == j:
continue
if min_dist is None:
min_dist = abs(disc_step[j] - i_step)
else:
if abs(disc_step[j] - i_step) < min_dist:
min_dist = abs(disc_step[j] - i_step)
else:
min_dist = min_dist
sum += min_dist

return sum / nsteps
Loading