Ejemplo n.º 1
0
def validate_selfsim():
    ssfile = 'data/mesos0825_s0dot2/mesos0825_s0dot2_ssmode'
    movdata = 'data/hcl_mesos0825_sample0.2'
    bsmap = 'data/hcl_mesos0825_bm'
    ofname = 'data/mesos0825_s0dot2/mesos0825_s0dot2_ssmode_mg'

    users = {}
    i = 0
    for line in open(ssfile, 'rb'):
        if i == 0:
            i = 1
            continue

        parts = line.strip('\r\n').split(',')
        uid = int(parts[0])
        group = int(parts[1])
        clust = int(parts[2])
        dist = float(parts[3])
        selfdist = float(parts[4])
        mode = str(parts[5])

        users[uid] = (group, clust, dist, selfdist, mode)

    print len(users)

    ofile = open(ofname, 'wb')
    for person in movement_reader(open(movdata), BaseStationMap(bsmap)):
        if person.id not in users or person.distinct_loc_num() < 2:
            continue

        user = users[person.id]
        ofile.write('%d\t%d\t%d\t%.3f\t%.3f\t%s\t%s\n' %
                    (person.id, user[0], user[1], user[2], user[3], user[4],
                     dumps_mobgraph(person.convert2graph())))

    ofile.close()
Ejemplo n.º 2
0
def validate_selfsim():
    ssfile = 'data/mesos0825_s0dot2/mesos0825_s0dot2_ssmode'
    movdata = 'data/hcl_mesos0825_sample0.2'
    bsmap = 'data/hcl_mesos0825_bm'
    ofname = 'data/mesos0825_s0dot2/mesos0825_s0dot2_ssmode_mg'

    users = {}
    i = 0
    for line in open(ssfile, 'rb'):
        if i == 0:
            i = 1
            continue

        parts = line.strip('\r\n').split(',')
        uid = int(parts[0])
        group = int(parts[1])
        clust = int(parts[2])
        dist = float(parts[3])
        selfdist = float(parts[4])
        mode = str(parts[5])

        users[uid] = (group, clust, dist, selfdist, mode)

    print len(users)

    ofile = open(ofname, 'wb')
    for person in movement_reader(open(movdata), BaseStationMap(bsmap)):
        if person.id not in users or person.distinct_loc_num() < 2:
            continue

        user = users[person.id]
        ofile.write('%d\t%d\t%d\t%.3f\t%.3f\t%s\t%s\n' % (
            person.id, user[0], user[1], user[2], user[3], user[4],
            dumps_mobgraph(person.convert2graph())))

    ofile.close()
def top_compare():
    """ Compare top motif and mesostructure
    """
    datapath = "data/mesos0825_s0dot2"
    movdata = "data/hcl_mesos0825_sample0.2"
    bsmap = "data/hcl_mesos0825_bm"
    ofname = os.path.join(datapath, "mesos0825_s0dot2_top")

    mobgraphs = {}
    for person in movement_reader(open(movdata), BaseStationMap(bsmap)):
        if person.which_day() != "0825":
            continue

        nn = len(set(person.locations))
        if nn > 20:
            continue
        if nn not in mobgraphs:
            mobgraphs[nn] = {}

        mobgraphs[nn][person.id] = person.convert2graph()

    new_file = True
    for C in range(2, 16):
        for kn in range(1, 5):

            print C, kn

            # Read dist matrix for (group, cluster) users
            fileklab = os.path.join(datapath, "mesos0825_s0dot2_c%d_kn%d" % (C, kn))
            distmat = []
            i = 0
            for line in open(fileklab):
                if i == 0:
                    uids = [int(i) for i in line.strip("\r\n").split(",")]
                    i == 1
                    continue
                distmat.append([float(i) for i in line.strip("\r\n").split(",")])

            distmat = np.array(distmat)
            distvec = distmat.sum(1) / len(uids)
            uids_sorted = [x for (y, x) in sorted(zip(distvec, uids))]

            N = len(uids_sorted)
            print ("Total users %d: " % N)

            mgs = mobgraphs[C]
            mesos = Mesos(mgs[uids_sorted[0]], mgs[uids_sorted[1]])
            topmesos = mesos.mesos
            topmesos_sim = 1 - mesos.struct_dist()

            motifs = {}
            for i in range(N - 1):
                u1 = uids_sorted[i]
                u2 = uids_sorted[i + 1]
                g1 = mgs[u1]
                g2 = mgs[u2]
                mesos = Mesos(g1, g2).mesos
                found = False
                for key in motifs.keys():
                    if isomorphism.is_isomorphic(key, mesos):
                        motifs[key].append((mesos, i))
                        found = True
                    if found:
                        break
                if not found:
                    motifs[mesos] = [(mesos, i)]

            res = []
            for key, value in motifs.items():
                res.append((len(value), value[0][0]))
            res = sorted(res, key=lambda x: x[0], reverse=True)
            topmotif = res[0][1]
            topmotif_supp = 1.0 * res[0][0] / N

            if new_file:
                mode = "wb"
                new_file = False
            else:
                mode = "ab"
            ofile = open(ofname, mode)

            ofile.write("%d\t%d" % (C, kn))
            ofile.write("\t%.3f\t%.3f" % (topmesos_sim, topmotif_supp))
            ofile.write("\t%s" % dumps_mobgraph(topmesos))
            ofile.write("\t%s" % dumps_mobgraph(topmotif))
            ofile.write("\n")
            ofile.close()
Ejemplo n.º 4
0
def dumps_mesos(G):
    return dumps_mobgraph(G)
def top_compare():
    """ Compare top motif and mesostructure
    """
    datapath = 'data/mesos0825_s0dot2'
    movdata = 'data/hcl_mesos0825_sample0.2'
    bsmap = 'data/hcl_mesos0825_bm'
    ofname = os.path.join(datapath, 'mesos0825_s0dot2_top')

    mobgraphs = {}
    for person in movement_reader(open(movdata), BaseStationMap(bsmap)):
        if person.which_day() != '0825':
            continue

        nn = len(set(person.locations))
        if nn > 20:
            continue
        if nn not in mobgraphs:
            mobgraphs[nn] = {}

        mobgraphs[nn][person.id] = person.convert2graph()

    new_file = True
    for C in range(2, 16):
        for kn in range(1, 5):

            print C, kn

            # Read dist matrix for (group, cluster) users
            fileklab = os.path.join(datapath,
                                    'mesos0825_s0dot2_c%d_kn%d' % (C, kn))
            distmat = []
            i = 0
            for line in open(fileklab):
                if i == 0:
                    uids = [int(i) for i in line.strip('\r\n').split(',')]
                    i == 1
                    continue
                distmat.append(
                    [float(i) for i in line.strip('\r\n').split(',')])

            distmat = np.array(distmat)
            distvec = distmat.sum(1) / len(uids)
            uids_sorted = [x for (y, x) in sorted(zip(distvec, uids))]

            N = len(uids_sorted)
            print('Total users %d: ' % N)

            mgs = mobgraphs[C]
            mesos = Mesos(mgs[uids_sorted[0]], mgs[uids_sorted[1]])
            topmesos = mesos.mesos
            topmesos_sim = 1 - mesos.struct_dist()

            motifs = {}
            for i in range(N - 1):
                u1 = uids_sorted[i]
                u2 = uids_sorted[i + 1]
                g1 = mgs[u1]
                g2 = mgs[u2]
                mesos = Mesos(g1, g2).mesos
                found = False
                for key in motifs.keys():
                    if isomorphism.is_isomorphic(key, mesos):
                        motifs[key].append((mesos, i))
                        found = True
                    if found:
                        break
                if not found:
                    motifs[mesos] = [(mesos, i)]

            res = []
            for key, value in motifs.items():
                res.append((len(value), value[0][0]))
            res = sorted(res, key=lambda x: x[0], reverse=True)
            topmotif = res[0][1]
            topmotif_supp = 1.0 * res[0][0] / N

            if new_file:
                mode = 'wb'
                new_file = False
            else:
                mode = 'ab'
            ofile = open(ofname, mode)

            ofile.write('%d\t%d' % (C, kn))
            ofile.write('\t%.3f\t%.3f' % (topmesos_sim, topmotif_supp))
            ofile.write('\t%s' % dumps_mobgraph(topmesos))
            ofile.write('\t%s' % dumps_mobgraph(topmotif))
            ofile.write('\n')
            ofile.close()
Ejemplo n.º 6
0
def dumps_mesos(G):
    return dumps_mobgraph(G)