Skip to content

Instantly share code, notes, and snippets.

@jbochi
Last active December 18, 2015 23:48
Show Gist options
  • Save jbochi/5863623 to your computer and use it in GitHub Desktop.
Save jbochi/5863623 to your computer and use it in GitHub Desktop.
Exponencial Random Graph Model for Facebook friends
# http://badhessian.org/2012/09/lessons-on-exponential-random-graph-modeling-from-greys-anatomy-hook-ups/
library(ergm)
path <- "/Users/juarez.bochi/Dropbox/mestrado/inf2035/Random Graphs/fbergm"
friends_path <- paste(path, "friends_manual.tsv", sep="/")
relationships_path <- paste(path, "relationships.tsv", sep="/")
friends <- read.table(friends_path, sep="\t", header=T, stringsAsFactors=F, strip.white=F, as.is=T)
relationships <- as.matrix(read.table(relationships_path, sep="\t", header=T, row.names=1))
net<-network(relationships, vertex.attr=friends, vertex.attrnames=colnames(friends), directed=F, hyper=F, loops=F, multiple=F, bipartite=F)
plot(net, vertex.col=c("blue","pink")[1+(get.vertex.attribute(net, "gender")=="female")], label=get.vertex.attribute(net, "name"), label.cex=0.35, label.color="grey")
base<-ergm(net~edges+nodematch("gender")+nodematch("work")+nodematch("state")+dsp(5))
summary(base)
plot(simulate(base), vertex.col=c("blue","pink")[1+(get.vertex.attribute(net, "gender")=="female")])
s
import requests
import json
FB = 'https://graph.facebook.com'
TOKEN = 'INCLUDE_TOKEN_API_HERE'
params = {
'method': 'GET',
'format': 'json',
'access_token': TOKEN
}
def graph(path, extra_params={}):
p = params.copy()
p.update(extra_params)
resp = requests.get(FB + path, params=p).json()
print resp
return resp['data']
def my_friends():
return graph('/me/friends', {'fields': 'birthday,gender,work,name'})
def mutual_friends(uid):
return graph('/me/mutualfriends/%s' % uid, {'fields': 'id'})
def main():
friends = my_friends()
for friend in friends:
print friend['id']
friend['mutualfriends'] = mutual_friends(friend['id'])
with open('friends.json', 'w') as f:
json.dump(friends, f)
if __name__ == '__main__':
main()
import json
ME = False
def writeln(f, fields):
f.write(u'\t'.join(fields).encode('utf-8'))
f.write('\n')
def work(friend):
return friend.get('work', [{}])[0].get('employer', {}).get('name') or ''
def main():
with open('friends.json') as f:
friends = json.load(f)
with open('friends.tsv', 'w') as f:
writeln(f, ['id', 'gender', 'name'])
if ME:
writeln(f, ['0', 'female', 'Eu'])
for i, friend in enumerate(friends):
writeln(f, [friend['id'],
friend.get('gender', '?'),
'"%s"' % friend['name'],
#work(friend)
])
with open('relationships.tsv', 'w') as f:
#Header
if ME:
f.write('\t0')
for friend in friends:
f.write('\t')
f.write(friend['id'])
f.write('\n')
if ME:
f.write('0')
f.write('\t')
f.write('0')
for friend in friends:
f.write('\t1')
f.write('\n')
#Matriz
for i, friend1 in enumerate(friends):
f.write(friend1['id'])
if ME:
f.write('\t')
f.write('1')
#Friends
mutual_friends = [fr['id'] for fr in friend1['mutualfriends']]
for friend2 in friends:
mutual = friend2['id'] in mutual_friends
f.write('\t')
f.write('1' if mutual else '0')
f.write('\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment