Skip to content

Commit

Permalink
added new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
FarMcKon committed Jul 12, 2010
1 parent ba31a22 commit d281110
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 89 deletions.
5 changes: 3 additions & 2 deletions python/FakeFloorData.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@


floor_data = [{
floor_data = [
{
"name" : "FarLand",
"vertices": [[0, 0],[5, 0],[5,5],[2,6],[0,5]],
"floor": 1
},
{
"name" : "cptn_corner",
"vertices": [[5, 0],[10, 0],[10,10],[5,10]],
"vertices": [[5, 0],[10, 0],[10,15],[5,10]],
"floor": 1
}
]
Expand Down
98 changes: 52 additions & 46 deletions python/HopeAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,89 @@
import re
import urllib2

#> This is our base URI for The Next HOPE instance of an OpenAMD server
g_openAMD_URI = "http://api.hope.net/"

#> This is a dictionary of 'data slices' as well as the section of the
#> location on the data server where the data resides. Z.b. info on 'talks'
#> data is at "http://api.hope.net/api/talks"
g_slices = {
'locations' : 'api/locations',
'speakers' : 'api/speakers',
'talks' : 'api/talks',
'interests' : 'api/interests',
'users': '/api/users',
'stats': '/api/stats',
}

#> These are lists of fields in each slice.
g_slice_fields = {
class HopeAPI:

#> These are lists of fields in each slice.
slice_fields = {
'locations' : ('area','time','x','y','user','button'),
'speakers' : ('name','bio'),
'talks' : ('speakers','title','abstract','time','track','interests'),
'interests' : (), #this is a free-from set of tags
'users': ('name','x','y','interests'),
'stats': ('poptalks','age'), #TRICKY:TODO: examples, there are not set yet
}
}

#> This is a dictionary of 'data slices' as well as the section of the
#> location on the data server where the data resides. Z.b. info on 'talks'
#> data is at "http://api.hope.net/api/talks"
slices = {
'locations' : 'api/locations',
'speakers' : 'api/speakers',
'talks' : 'api/talks',
'interests' : 'api/interests',
'users': '/api/users',
'stats': '/api/stats',
}
#> This is our base URI for The Next HOPE instance of an OpenAMD server
openAMD_URI = "http://api.hope.net/"

def JSON_string_at_uri(uri):
""" simple function to return the JSON string at a requested URI. """
data = urllib2.urlopen(uri).read()
return data


def dictFromJSON(stringOfJSON):
""" Returns a python dict created from the passed JSON string. """
return json.loads(stringOfJSON, parse_float=True, parse_int=True, encoding='UTF-8')


class HopeAPI:

def uri_for_slice(self, sliceName):
""" simple funciton to return a complete URI for a data slice. """
return g_openAMD_URI + g_slices[sliceName]
return self.openAMD_URI + self.slices[sliceName]

def filter_for_slice_uri(self, slice):
def filter_for_slice_uri(self, slice, filterDict):
"""Returns a valid query string paramaeters list from a slice,
and a list of query and value pairs. """
uri = self.uri_for_slice(slice)
#> This magic takes a dict, and turns it into a query string
query_string = '&'.join(
[k+'='+urllib2.quote(str(v)) for (k,v) in self.filterDict.items()])
[k+'='+urllib2.quote(str(v)) for (k,v) in filterDict.items()])

#TODO: test other filters, esp. list's passed in the filter dict

#> using 'x'.join is a faster and sleeker string builder than the + operator in python
return '?'.join( (uri,query_string) )

def __init__(self, filter = None):
self.filterDict = filter
pass

@classmethod
def JSON_string_at_uri(cls, uri):
""" simple function to return the JSON string at a requested URI. """
data = urllib2.urlopen(uri).read()
return data

def json_from_slice(self, slice, filterDict):
uri = self.uri_for_slice(slice)
if(filterDict and filterDict != None):
query_string = '&'.join(
[k+'='+urllib2.quote(str(v)) for (k,v) in filterDict.items()])
uri = '?'.join( (uri,query_string) )

return self.JSON_string_at_uri(uri)

def __getattr__(self, slice):
""" This function does magic to allow a user to call
appInstance.sliceName(filterDict) in order get JSON for a slice as
filtered for the intersection of all dictionary terms.
See dev notes in the code for more info
"""
# -- getattr is 'only called when no funciion or variable was matched'

def __getattr__(self, slice):
"""This is an example of a fast and smart way to grab data from the API."""
if not g_slices.has_key(slice):
# -- if we do not have a slice of that name, exception
if not self.slices.has_key(slice):
raise Exception('Unsupported slice name: ' + slice)

if self.filterDict == None:
# -- fetch the uri for the whole talks slice
uri = self.uri_for_slice(slice)
else:
# -- fetch the uri for the data in a slice that matches filters.
uri = self.filter_for_slice_uri(slice)
# -- fetch the JSON string for the whole slice
data = JSON_string_at_uri(uri)
# -- turn that JSON string into a python list
return dictFromJSON(data)
# -- otherwise return a function that takes 0 or 1 filterDict entries
return lambda filterDict=None : self.json_from_slice(slice,filterDict)

if __name__ == '__main__':
api = HopeAPI()
print api.locations
print api.locations()

api.filterDict = {'track': 'Tesla'}
print api.talks
print api.talks({'track': 'Tesla'})
56 changes: 56 additions & 0 deletions python/LastHopeJaccardExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
import networkx as nx
import csv
import matplotlib.pylab as plt
import os.path


## Example by Rob Zinkov
## minor updates by Far McKon



def jaccard(a, b):
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))

def show_talk_graph():
reader = csv.reader(file("talk_presence.csv"))
headers = reader.next()

talks_seen = {}
G = nx.Graph()

for line in reader:
if line[0] not in talks_seen:
talks_seen[line[0]] = set(line[1])
else: talks_seen[line[0]].add(line[1])
for t in talks_seen:
for u in talks_seen:
if t is not u:
weight = jaccard(talks_seen[t],
talks_seen[u])
if weight > 0.4:
G.add_edge(t,u,weight=weight)
pos = nx.spring_layout(G,weighted=True)
nx.draw(G,
pos,
edge_color=[float(G[e1][e2]['weight']+0.1) for e1,e2 in G.edges()],
width=4,
node_size=40,
edge_cmap=plt.cm.Blues,
with_labels=False,

node_color=[float(G.degree(v)) for v in G],
cmap=plt.cm.Blues,
)
print "Nodes:%d Edges: %d\n" % (len(G.nodes()),len(G.edges()))
plt.show()

if __name__ == "__main__":

if not os.path.exists("talk_presence.csv"):
print "please download the Last Hope data set unto the same directory as this file"
print "the last hope data set can be found at http://crawdad.org/meta.php?name=hope/amd "
else :
show_talk_graph()
59 changes: 59 additions & 0 deletions python/LastHopeLocationExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
import networkx as nx
import csv
import matplotlib.pylab as plt
import os.path

## Example by Rob Zinkov
## minor updates by Far McKon

def load_location_graph():
reader = csv.reader(file("position_snapshot.csv"))
headers = reader.next()

last_time = ""

zones = {}
edges = {}
nodes = {}

for line in reader:
nodes[line[1]] = nodes.get(line[1],0)+1
if line[0] != last_time:
for z in zones:
for i in zones[z]:
for j in filter(lambda x: x!=i,zones[z]):
edges[(i,j)] = edges.get((i,j),0)+1
edges[(j,i)] = edges.get((j,i),0)+1
last_time = line[0]
zones = {}
else:
zones[line[2]] = zones.get(line[2],set()).union([line[1]])
G = nx.Graph()
for (e1,e2) in edges:
weight = edges[(e1,e2)]/(nodes[e1]+nodes[e2]-edges[(e1,e2)])
#alternative weight
# weight = edges[(e1,e2)]/min(nodes[e1],nodes[e2])
if weight > 0.08:
G.add_edge(e1,e2,weight=weight)
print "Nodes:%d Edges: %d\n" % (len(G.nodes()),len(G.edges()))

pos = nx.spring_layout(G,weighted=True)
nx.draw(G,
pos,
node_size=40,
with_labels=False,
alhpa = 0.5,
node_color=[float(G.degree(v)) for v in G],
cmap=plt.cm.Greens,
)
plt.show()

if __name__ == "__main__":

if not os.path.exists("position_snapshot.csv"):
print "please download the Last Hope data set unto the same directory as this file"
print "the last hope data set can be found at http://crawdad.org/meta.php?name=hope/amd "
else :
show_ping_graph()

15 changes: 15 additions & 0 deletions python/LastHopeOther.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

def print_cliques():

cliques = sorted(nx.find_cliques(G),key=len,reverse=True)[:10]
for c in cliques:
c = filter(lambda x: name[x] is not "",c)
print map(lambda x: name[x], c)


def print_reachable():
parts = nx.connected_components(G)
for p in parts[:10]:
p = filter(lambda x: name[x] is not "",p)
print map(lambda x: name[x], p)

35 changes: 35 additions & 0 deletions python/LastHopePingsExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python
import networkx as nx
import csv
import matplotlib.pylab as plt
import os.path

## Example by Rob Zinkov
## minor updates by Far McKon


def show_ping_graph():
reader = csv.reader(file("ping.csv"))
headers = reader.next()

G = nx.Graph()

for line in reader:
G.add_edge(line[0],line[1])

nx.draw_spring(G,
node_color=[float(G.degree(v)) for v in G],
node_size=40,
with_labels=False,
cmap=plt.cm.Reds,
)
plt.show()

if __name__ == "__main__":

if not os.path.exists("ping.csv"):
print "please download the Last Hope data set unto the same directory as this file"
print "the last hope data set can be found at http://crawdad.org/meta.php?name=hope/amd "
else :
show_ping_graph()

2 changes: 2 additions & 0 deletions python/PygameTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print "pygames will not install on my 10.6 snowlepoard machine."
print "a pygames test/interface/show is left as an exercize to the reader."
47 changes: 6 additions & 41 deletions python/TestMatPlotLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,11 @@

#HACK: since floor data is NOT AVAILABLE, I'm creating floor data
#here as a static string.
from FakeFloorData import floor_data
from FakeFloorData import floor_data, FloorTest

hacky_scale_factor = 20.0


class TestFloor():

floorDict = {}

def __init__(self, floorDict):
self.floorDict = floorDict

def allVertices(self):
x = []
for area in self.floorDict:
if 'vertices' in area:
x.extend(area['vertices'])
return x

def allVerticesX(self):
self.allVertices()
return [ x for x,y in self.allVertices()]
def allVerticesY(self):
return [ y for x,y in self.allVertices()]

def verticies(self):
minX = min(self.allVerticesX())
minY = min(self.allVerticesY())
maxX = max(self.allVerticesX())
maxY = max(self.allVerticesY())
return ((minX,minY),(maxX,minY),(maxX,maxY),(minX,maxY))

class TestRoom(lines.Line2D):

def __init__(self, amdRoomDict, *args, **kwargs):
Expand Down Expand Up @@ -89,22 +62,14 @@ def draw(self, renderer):
lines.Line2D.draw(self, renderer)
self.text.draw(renderer)




"""
fig = plt.figure()
ax = fig.add_subplot(111)
floor = FloorTest(floor_data)
floorBoundaries = floor.bounds()
line = TestRoom(floor_data, mfc='red', ms=12, label='line label')
#line.text.set_text('line label')
line.text.set_color('red')
line.text.set_fontsize(16)
#line.text.set_color('red')
#line.text.set_fontsize(16)

ax.add_line(line)
#plt.show()
"""
f = TestFloor(floor_data)
print f.verticies()
plt.show()
Loading

0 comments on commit d281110

Please sign in to comment.