Skip to content

make updates to notebook #11

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
42 changes: 1 addition & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1 @@
# Week 12 Deliverables (E8) - Due 4/12/16
For this week make sure that you have completed the following:

* Fork Assignment 9 to your own github repository.
* You can access assignment 9
[HERE](https://github.com/Geospatial-Python/assignment_09)
* Clone the repository locally

## Deliverables
1. Create a GUI application that can be launched from a script named `view.py`.
The GUI should include:
* A single window (QMainWindow or QWidget)
* A `File` menu with at least one entry - `Quit`. The quit item should
exit the program.
* The code should be presented using the following conventions:
a. Include an `if '__name__' == __main__:` block
b. Write your GUI in a class, inhereting from QMainWindow, QWidget, or
another appropriate parent. You should have at least an `__init__` method and
a `init_ui` method.
c. Call a function called `main` when the application is launched and
an instance of your GUI class is created
* A central widget. This can be an empty widget, a text box, a graphic -
we will replace this next week with something else, so the important part
for now is having something in the window.

Note: For the above requirements, the linked readings offer step-by-step
examples of how to achieve this.
1. If you wrote a script for last week demonstrating your point pattern
analysis code, please move that code into an iPython notebook. Extend the
notebook to:
* Plot the point pattern. To do this, please write a function that takes
two vectors (x, y) as arguments and then generates a plot.
* Plot the results of the G-function. You can see an example of this in a
previous week's readings. In addition to plotting the observed function,
plot the upper and lower confidence thresholds in a different line color. For
example, if the observed value is red, then the upper and lower thresholds
(with p=0.05 maybe) could be blue.

Hint: To get plots to automatically display in a notebook add `%pylab
inline` to the first cell (where your other imports are)
1. Update any other support code as necessary.
# assignment_07
Empty file added __init__.py
Empty file.
176 changes: 176 additions & 0 deletions analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import point
import math
import statistics
import random

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
"""
total = len(points)
y = 0
x = 0
for point in points:
x += point[0]
y += point[1]

x = x/total
y = y/total
return x, y

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]
"""
x_list = []
y_list = []

for p in points:
x_list.append(p[0])
y_list.append(p[1])

mbr = [0,0,0,0]
mbr[0] = min(x_list)
mbr[1] = min(y_list)
mbr[2] = max(x_list)
mbr[3] = max(y_list)

return mbr


def mbr_area(mbr):
"""
Compute the area of a minimum bounding rectangle
"""
width = mbr[2] - mbr[0]
length = mbr[3] - mbr[1]
area = width * length

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.5 * (math.sqrt( area / n ))
return expected


"""
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 create_random_marked_points(n, marks = None):
point_list = []
rand = random.Random()
for i in range(n):
rand_x = round(rand.uniform(0,1),2)
rand_y = round(rand.uniform(0,1),2)
if marks is None:
point_list.append(point.Point(rand_x, rand_y))
else:
rand_mark = random.choice(marks)
point_list.append(point.Point(rand_x, rand_y, rand_mark))
return point_list

def euclidean_distance(a, b):
distance = math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2)
return distance

def average_nearest_neighbor_distance(points, mark = None):
new_points = []
if mark is None:
new_points = points
else:
for point in points:
if point.mark is mark:
new_points.append(point)

dists = []
for num1, point in enumerate(new_points):
dists.append(None)
for num2, point2 in enumerate(new_points):
if num1 is not num2:
new_dist = euclidean_distance(point, point2)
if dists[num1] == None:
dists[num1] = new_dist
elif dists[num1] > new_dist:
dists[num1] = new_dist

return sum(dists)/len(points)

def permutations(p=99, n=100, marks=None):
neighbor_perms = []
for i in range(p):
neighbor_perms.append(average_nearest_neighbor_distance(create_random_marked_points(n),
marks))
return neighbor_perms

def compute_critical(perms):
return max(perms), min(perms)

def check_significant(lower, upper, observed):
return(lower <= observed or observed <= upper)
78 changes: 78 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 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
"""
max_population = 0
city = None
features_list = gj.get('features')
x = 0

for f in features_list:
if f['properties']['pop_max'] > max_population:
max_population = f['properties']['pop_max']
city = f['properties']['name']

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!
"""
#find the largest city west of the Mississippi River

largest_western_city = None
features_list = gj.get('features')
for f in features_list:
if f['properties']['longitude'] < -95.202:
largest_western_city = f['properties']['longitude']


return largest_western_city
Loading