Skip to content

Last assignment ! #6

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 Positive Tweets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added all tweets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions io_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import json
import random

def read_tweet_json(input_file):
"""
Read a tweet json file

Parameters
----------
input_file : str
The PATH to the data to be read

Returns
-------
gj : dict
An in memory version of the geojson
"""
with open(input_file, 'r') as fp:
tweets = json.loads(fp.read())
return tweets


def ingest_twitter_data(twitter_data):
"""
Ingest a tweet data and return the dict of needed data.

Parameters
----------
twitter_data : str
The tweet data to be ingest

Returns
-------
need_data : dict

"""

if twitter_data['geo']==None:

x_list=[value[1] for value in (twitter_data["place"]["bounding_box"]["coordinates"][0])]

y_list=[value[0] for value in (twitter_data["place"]["bounding_box"]["coordinates"][0])]
x=random.uniform(min(x_list),max(x_list))
y=random.uniform(min(y_list),max(y_list))
else:
x=twitter_data['geo']['coordinates'][0]
y=twitter_data['geo']['coordinates'][1]

need_data={"point":(x,y),"text":twitter_data["text"],"id_str":twitter_data["id_str"],"lang":twitter_data["lang"],"source":twitter_data["source"],"created_time":twitter_data["created_at"]}
return need_data

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)
29 changes: 29 additions & 0 deletions point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@



class Point():

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


def check_coincident(self, peer_p):

return (self.x == peer_p.x and self.y == peer_p.y and self.mark == peer_p.mark)

def shift_point(self, x_shift, y_shift):

self.x += x_shift
self.y += y_shift

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

def __str__(self):
return "x=%f,y=%f,mark=%s"%(self.x,self.y,self.mark)

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

193 changes: 193 additions & 0 deletions point_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
from point import Point
import math
import random
import numpy as np


class PointPattern():

def __init__(self,points):
self.points = points[:]


def average_nearest_neighbor_distance(self,mark=None):

"""
Given a set of points, compute the average nearest neighbor.

Parameters
----------
points : list
A list of points
mark : str

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
if mark==None:
points_tmp=self.points
else:
points_tmp=[ point for point in self.points if point.mark==mark]

length=len(points_tmp)

#print(self.points)
nearest_distances=[]
for i in range(length):
distance=[]
for j in range(length):
if i==j:
continue
else:
distance.append(self.euclidean_distance((points_tmp[i].x,points_tmp[i].y),(points_tmp[j].x,points_tmp[j].y)))
nearest_distances.append(min(distance))

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

def number_of_coincident_points(self):

length=len(self.points)
number_of_coincident_points=0
for i in range(length):
for j in range(length):
if i==j:
continue
else:
if self.check_coincident((self.points[i].x,self.points[i].y),(self.points[j].x,self.points[j].y)):
number_of_coincident_points+=1
return number_of_coincident_points

def list_marks(self):

points_marks=set()
for point in self.points:
if point.mark!=None:
points_marks.add(point.mark)

return list(points_marks)

def list_subset_of_points(self,mark):

return [point for point in self.points if point.mark==mark]

def create_random_marked_points(self, n=None,domain_min=0,domain_max=1,marks=[]):

if n == None:
n=len(self.points)

randoms_list=np.random.uniform(domain_min, domain_max, (n,2))
points_list=[]
for i in range(n):
if len(marks)!=0:
points_list.append(Point(randoms_list[i][0], randoms_list[i][1],random.choice(marks)))
else:
points_list.append(Point(randoms_list[i][0], randoms_list[i][1]))
return points_list

def create_realizations(self, k):

return self.permutations(k)

def permutations(self,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(self.average_nearest_neighbor_distance())
return permutation_list

def critical_points(self,permutations):
"""
Return the mean nearest neighbor distance of p permutations.

Parameters
----------
permutations : list
the mean nearest neighbor distance list.
Returns
-------
smallest : float
largest : float

"""

return min(permutations),max(permutations)

def compute_g(self,nsteps):

ds = np.linspace(0, 1, nsteps)
min_list=[]
for i_index,di in enumerate(ds):
tmp_list=[]
for j_index,dj in enumerate(ds):
if i_index != j_index:
tmp_list.append(np.abs(di-dj))

min_list.append(np.min(tmp_list))

return np.mean(min_list)



def check_coincident(self,a, b):
"""
Check whether two points are coincident
Parameters
----------
a : tuple
A point in the form (x,y)

b : tuple
A point in the form (x,y)

Returns
-------
equal : bool
Whether the points are equal
"""
return a == b

def euclidean_distance(self,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


Binary file added the dialog containing the G-Function plot..jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading