In [28]:
fu = open('users-rome.csv', 'r')

In [29]:
fout = open('users-rome-renum.txt', 'w')

In [30]:
idmap = {}

In [31]:
newid = 0

In [32]:
for line in fu:
    uid, uname = map(int, line[:-1].split(','))
    idmap[uid] = newid
    fout.write('%d,%d\n' % (newid, newid))
    newid += 1

In [33]:
fu.close()
fout.close()

# Filter friendships present in SF, write only renumbered friendship relations


In [18]:
ffr = open('FoursquareFriendship.csv', 'r')

In [19]:
ffr.readline() # Skip header


Out[19]:
'user1,user2\r\n'

In [20]:
ffrnew = open('FoursquareFriendship-rome.csv', 'w')

In [21]:
org_uids = set(idmap.keys())
for line in ffr:
    u1, u2 = map(int, line[:-2].split(','))
    if u1 in org_uids and u2 in org_uids:
        ffrnew.write('%d,%d\n' % (idmap[u1], idmap[u2]))

In [22]:
ffr.close()
ffrnew.close()

# Rewrite cascades with renumbered user IDs


In [23]:
fcheck = open('FoursquareCheckins20110101-20111231_near_rome.csv', 'r')

In [24]:
fcheck_new = open('FoursquareCheckins20110101-20111231_near_rome-renum.csv', 'w')

In [25]:
fcheck_new.write(fcheck.readline()) # Rewrite header

In [26]:
for line in fcheck:
    us, lat, lng, tm, loc = line[:-1].split(',')
    fcheck_new.write('%d,%s,%s,%s,%s\n' % (idmap[int(us)], lat, lng, tm, loc))

In [27]:
fcheck.close()
fcheck_new.close()

# Dump mapping into some file


In [9]:
fmap = open('rome/users-rome-mapping.txt', 'w')

In [10]:
for key in sorted(list(idmap.keys())):
    fmap.write('%d,%d\n' % (key, idmap[key]))

In [11]:
fmap.close()

In [ ]: