Skip to content
Browse files

downloading and pmi seem to work, setting up accounts

  • Loading branch information...
1 parent 24a1500 commit 46383a760268c3b45731494a3142d6f1c7eab30d @friendofrobots committed Apr 20, 2011
View
19 air/air_explorer/views.py
@@ -1 +1,18 @@
-# Create your views here.
+from django.shortcuts import render_to_response, get_object_or_404
+
+from toolkit.models import Entity, Link, PMI
+
+def home(request, template_name="toolkit/downloading.html"):
+ return render_to_response(template_name, context_instance=RequestContext(request))
+
+def friends(request, template_name="toolkit/friends.html"):
+ friends = Entity.objects.filter(linksFrom__relation="likes").distinct()
+ return render_to_response(template_name, {
+ 'friends' : friends,
+ }, context_instance=RequestContext(request))
+
+def likes(request, template_name="toolkit/friends.html"):
+ likes = Link.objects.annotate(entity_activity=Count('toEntity__linksTo')).filter(entity_activity__gt=1,relation="likes")
+ return render_to_response(template_name, {
+ 'friends' : friends,
+ }, context_instance=RequestContext(request))
View
2 air/fbauth/urls.py
@@ -2,5 +2,5 @@
urlpatterns = patterns('fbauth.views',
url(r'^logout/?$', 'logout', name='logout'),
- url(r'^$', 'login', name='login'),
+ url(r'^login$', 'login', name='login'),
)
View
1 air/fbauth/views.py
@@ -17,6 +17,7 @@ def login(request):
redirect_uri=request.build_absolute_uri(),
scope=settings.FB_PERMS,
)
+ raise Exception
if request.GET.__contains__("code"):
args["client_secret"] = settings.FB_SECRET
args["code"] = request.GET.get("code")
View
9 air/settings.py
@@ -20,14 +20,7 @@
MANAGERS = ADMINS
DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
- 'NAME': 'dev.db', # Or path to database file if using sqlite3.
- 'USER': '', # Not used with sqlite3.
- 'PASSWORD': '', # Not used with sqlite3.
- 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
- 'PORT': '', # Set to empty string for default. Not used with sqlite3.
- }
+ 'default': secrets.getDatabaseSecrets()
}
# Local time zone for this installation. Choices can be found here:
View
20 air/templates/air_explorer/home.html
@@ -0,0 +1,20 @@
+{% extends "base.html" %}
+
+{% block head_title %}Home{% endblock %}
+
+{% block content %}
+<h1>Welcome!</h1>
+<div id="about">
+ This is a demonstration of the AIR Toolkit.
+</div>
+{% if user.is_authenticated %}
+<div>
+ <a href="/explore/">Get Started</a>
+</div>
+{% else %}
+<div>
+ <a href="/auth/login">login with facebook</a>
+</div>
+{% endif %}
+
+{% endblock %}
View
2 air/templates/base.html
@@ -1,6 +1,6 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}" xml:lang="{{ LANGUAGE_CODE }}" lang="{{ LANGUAGE_CODE }}">
+<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Air Toolkit - {% block head_title %}{% endblock %}</title>
View
67 air/toolkit/air/djair.py
@@ -1,38 +1,10 @@
-import json, airfb, airtoolkit, operator, facebook, sys, time
+import json, airfb, airtoolkit, operator, facebook, sys, time, itertools
from django.contrib.auth.models import User
+from django.db.models import Count
from fbauth.models import Profile
+from toolkit.models import Link,Entity
from toolkit import tasks
from celery.task.sets import TaskSet
-
-def createUserFromDataFile(filename, pk, fbid):
- graph = airfb.FBGraph()
- graph.load(filename)
- unfiltereds = json.dumps(graph.graph, ensure_ascii=False)
- print len(graph.graph),'objects before filtering'
- activity = graph.filterByActivity(3)
- print len(graph.graph),'objects after filtering'
- likes = graph.linksOfRel('likes')
- print 'number of likes',len(likes)
- pmi = airtoolkit.PMIMatrix(likes)
-
- print 'dumping json'
- filtereds = json.dumps(graph.graph, ensure_ascii=False)
- lookups = json.dumps(graph.lookup, ensure_ascii=False)
- p = [(graph.getName(x),
- sorted([(graph.getName(z),y[z]) for z in y],key=operator.itemgetter(1), reverse=True))
- for x,y in pmi.matrix.iteritems()]
- pmis = json.dumps(p,ensure_ascii=False)
-
- print 'creating django objects'
- user = User.objects.get(pk=pk)
- profile = Profile.objects.get(user=user)#, defaults={'fbid':fbid,'name':graph.lookup[fbid]})
- #profile = Profile.objects.get_or_create(user=user, defaults={'fbid':fbid,'name':graph.lookup[fbid]})
- data = Data.objects.get_or_create(profile=profile,defaults={
- 'graph':unfiltereds,
- 'lookup':lookups,
- 'filtered_graph':filtereds,
- 'pmi_matrix':pmis,
- })
def download(access_token):
print 'Starting download'
@@ -43,7 +15,18 @@ def download(access_token):
result = TaskSet(tasks=[tasks.dlUser.subtask((graphapi,fbid)) for fbid in friendIds]).apply_async()
return result
-def printStatus(result):
+def printStatus(result,message='loading',done='done.'):
+ count = 0
+ while (not result.ready()):
+ print message+''.join(['.' for x in xrange(count)]), '\r',
+ count += 1
+ if count > 5:
+ count = 0
+ sys.stdout.flush()
+ time.sleep(1)
+ print done
+
+def printSetStatus(result):
while (not result.ready()):
r = 1.0 * result.completed_count() / result.total
bars = int(70*r)
@@ -58,16 +41,14 @@ def printStatus(result):
time.sleep(1)
print '/'+''.join(['-' for n in xrange(70)]),'/','100%'
- return result
-
-def trySaveData(result):
- r = tasks.saveData.delay((result.join()))
+def saveData(result):
+ r = tasks.saveUserData.delay((result.join()))
+ printStatus(r,'saving data','saved.')
+def calcPmis():
+ likes = Link.objects.annotate(entity_activity=Count('toEntity__linksTo')).filter(entity_activity__gt=1,relation="likes")
+ pr = tasks.calcPMIs.delay((likes))
count = 0
- while (not r.ready()):
- print 'loading'+''.join(['.' for x in xrange(count)]), '\r',
- count += 1
- if count > 5:
- count = 0
- sys.stdout.flush()
- time.sleep(1)
+ printStatus(pr,'calculating pmis')
+ return pr
+
View
7 air/toolkit/models.py
@@ -2,13 +2,15 @@
from fbauth.models import Profile
class Entity(models.Model):
+ owner = models.ForeignKey(Profile)
fbid = models.CharField(max_length=30)
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Link(models.Model):
+ owner = models.ForeignKey(Profile)
relation = models.CharField(max_length=100)
fromEntity = models.ForeignKey(Entity,related_name='linksFrom')
toEntity = models.ForeignKey(Entity,related_name='linksTo')
@@ -21,6 +23,7 @@ def __unicode__(self):
self.toEntity.name))
class PMI(models.Model):
+ owner = models.ForeignKey(Profile)
fromEntity = models.ForeignKey(Entity,related_name='pmiFrom')
toEntity = models.ForeignKey(Entity,related_name='pmiTo')
value = models.FloatField()
@@ -29,12 +32,14 @@ def __unicode__(self):
return self.fromEntity.name +","+ self.toEntity.name +"=>"+ unicode(self.value)
class Category(models.Model):
- name = models.CharField(max_length=200)
+ owner = models.ForeignKey(Profile)
+ name = models.CharField(max_length=200,unique=True)
def __unicode__(self):
return self.name
class CategoryScore(models.Model):
+ owner = models.ForeignKey(Profile)
category = models.ForeignKey(Category)
entity = models.ForeignKey(Entity)
value = models.FloatField()
View
49 air/toolkit/tasks.py
@@ -1,4 +1,5 @@
from celery.decorators import task
+from celery.task.sets import TaskSet
from toolkit.models import Entity, Link, PMI
import json, urllib2, urllib, math, facebook
@@ -44,37 +45,46 @@ def dlUser(graphapi,fbid):
return entities, links
except (ValueError,IOError,facebook.GraphAPIError,urllib2.URLError), exc:
print exc
- #dlUser.retry(exc=exc)
+ dlUser.retry(exc=exc)
-@task()
-def saveData(join, callback=None):
+@task(ignore_result=True)
+def saveUserData(join, profile, callback=None):
"""
Creates django objects and saves them
+ TODO: filter for entities with less than 2 like links
"""
entities = set()
- [entities.update(e) for (e,l) in join]
- [Entity.objects.get_or_create(fbid=fbid,name=name) for (fbid, name) in entities]
+ for e,l in join:
+ entities.update(e)
+ for fbid,name in entities:
+ Entity.objects.get_or_create(
+ owner=profile,
+ fbid=fbid,
+ name=name)
for entity, links in join:
for link in links:
try:
- Link.objects.create(fromEntity=Entity.objects.get(fbid=link[0]),
- relation=link[1],
- weight=link[2],
- toEntity=Entity.objects.get(fbid=link[3]))
+ Link.objects.create(
+ owner=profile,
+ fromEntity=Entity.objects.get(fbid=link[0]),
+ relation=link[1],
+ weight=link[2],
+ toEntity=Entity.objects.get(fbid=link[3]))
except Exception, e:
print e
print link
raise Exception()
-@task()
-def calculatePMIs(links, callback=None):
+
+@task(ignore_result=True)
+def calcPMIs(links, callback=None):
linkedBy = {}
for link in links:
if link.toEntity.fbid not in linkedBy:
linkedBy[link.toEntity.fbid] = set()
linkedBy[link.toEntity.fbid].add(link.fromEntity.fbid)
"""
- PMI(i1,i2) = log(Pr(i1,i2) / Pr(i1)Pr(i2))
+ Pmi(i1,i2) = log(Pr(i1,i2) / Pr(i1)Pr(i2))
= log(num(i1,i2)*totalLinks / num(i1)num(i2))
"""
@@ -83,13 +93,14 @@ def calculatePMIs(links, callback=None):
lower id to the one with the higher id (note: ids are strings,
so the sort is alphabetical in this case)
"""
- lblist = [(fbid,lb) for fbid,lb in linkedBy.iteritems() if len(lb1) > 1]
- for fbid1,lb1 in lblist:
- for fbid2,lb2 in lblist:
- if fbid1 <= fbid2 and len(lb1.intersection(lb2)) > 0:
- PMI.objects.create(fromEntity=Entity.objects.get(fbid=fbid1),
- toEntity=Entity.objects.get(fbid=fbid2),
- value=math.log(len(lb1.intersection(lb2))*len(links)/(len(lb1)*len(lb2)),2))
+ for fbid1,lb1 in linkedBy.iteritems():
+ for fbid2,lb2 in linkedBy.iteritems():
+ if fbid1 <= fbid2 and len(lb1.intersection(lb2))>0:
+ PMI.objects.get_or_create(
+ owner=profile,
+ fromEntity=Entity.objects.get(fbid=fbid1),
+ toEntity=Entity.objects.get(fbid=fbid2),
+ value=math.log(len(lb1.intersection(lb2))*len(links)/(len(lb1)*len(lb2)),2))
def parseInfo(fbid,data):
entities,links = set(),set()
View
24 air/toolkit/views.py
@@ -59,27 +59,3 @@ def status(request, task_id):
"total": result.status(),
}
}
-
-def pmi_all(request, greg=True, template_name="toolkit/pmi.html"):
- if greg:
- data = User.objects.get(pk=1).profile_set.all()[0].data_set.all()[0]
- if request.user.is_authenticated():
- data = request.user.profile_set.all()[0].data_set.all()[0]
- # These may not be here yet, check for that
- print 'loading json'
- pmi = json.loads(data.pmi_matrix)
- graph = json.loads(data.filtered_graph)
- print 'rendering html'
- print pmi
- return render_to_response(template_name, {
- "objects" : pmi.keys()[:100],
- "lookup" : lookup,
- }, context_instance=RequestContext(request))
-
-def getPmiVector(request, fbid):
- """
- load pmi matrix
- vector = pmi[fbid]
- filter our 0s
- """
- return HttpResponse(json.dumps(VECTOR),mimetype="application/json")
View
2 air/urls.py
@@ -11,6 +11,6 @@
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
- (r'^login/', include('fbauth.urls')),
+ (r'^auth/', include('fbauth.urls')),
(r'^$', include('toolkit.urls')),
)

0 comments on commit 46383a7

Please sign in to comment.
Something went wrong with that request. Please try again.