def func():
    all_cas_d = load_obj('C:\weibodataset\\2011_08c_ind.pkl')
    outnei_d = load_obj(ON_PATH)
    exp = outnei_d.keys()[0]
    print exp,type(exp)


    comm_list = comm_l()
    len_comm_l = len(comm_list) - 1
    result_d = {}
    for rtmid in all_cas_d:

        if random.random() <= 0.9:
            continue

        rtmid_d = all_cas_d[rtmid]
        onexp_d = {} #onexp_d[outnei_id] ---> [adopters who are the outnei's active incoming neighbors]

        for adp_ind in rtmid_d:
            #print adp_ind,type(adp_ind)
            #raise
            if int(adp_ind) <= len_comm_l:
                comm_no_adp = comm_list[int(adp_ind)]
            else:
                comm_no_adp = -int(adp_ind) #singleton comm added to match V1

            adp_ind_str = str(adp_ind).replace('L','')

            if adp_ind_str in outnei_d:
                outneis = outnei_d[adp_ind_str]
                for outnei in outneis:
                    if outnei in onexp_d:
                        onexp_d[outnei].append(comm_no_adp)
                    else:
                        onexp_d[outnei] = [comm_no_adp]

        #print onexp_d


        for k,v in onexp_d.iteritems():
            an = len(v)
            ac = len(set(v))
            key = str(an)+','+str(ac)
            if key not in result_d:
                result_d[key] = 1
            else:
                result_d[key] += 1
        pass
    save_obj(result_d,RESULT_PATH)
Example #2
0
def func(pertss,cas_cent_path=cas_cent_path):
    fns = os.listdir(cas_cent_path)
    print fns
    for_hm = []
    for i in xrange(0,len(fns)):
        fn = fns[i]
        full_path = os.path.join(cas_cent_path,fn)
        cas_cent_list = load_obj(full_path)
        perts = pertss[i]

        for_hm_cent = [] # = [[# of nodes over thres1, #of nodes over thres2, # of nodes over thres3, # of nodes over thres4...]]

        for one_cas in cas_cent_list:
            temp_l = []
            for i in xrange(0,5):
                cent_thres = perts[i]
                #result = []

                nodes_over_thres = [x for x in one_cas if x >= cent_thres]
                number_nodes_over_thres = len(nodes_over_thres)

                #for_hm_cent.append()
                temp_l.append(number_nodes_over_thres)
            for_hm_cent.append(temp_l)

        for_hm.append(heatmap(for_hm_cent))

    return for_hm
def func(CKs):

    print 'Choose the time as %d, with lambda equal to %d' %(THRES_TIME,LAMBDA)

    #CK = 'com_k_shell'
    #print 'current centrality key is '+CK

    core_d_outnei = load_obj(CORE_PATH)
    #cm_d = comm_dict()

    #comm_dist = {}
    #comm_dist['size'] = THRES_SIZE
    #comm_dist['lbd'] = LAMBDA
    ind = 0

    #cent_list, perts = load_cents(CK)
    #perts = np.percentile(cent_list,pert_thres)

    of = open('E:\\SNAM_2015\\time_cent_percentiles.csv','wb')
    writer = csv.writer(of)
    titles = ['ind']
    for k in xrange(0,len(CKs)):
        ck = CKs[k]
        for j in xrange(0,len(pert_thres)):
            pert = str(pert_thres[j])
            title = ck+'_'+pert
            titles.append(title)
        titles.append(ck+'_avg')

    writer.writerow(titles)
    all_cent_lists = []
    pertss = []
    for CK in CKs:
        cent_list,perts = load_cents(CK)
        all_cent_lists.append(cent_list)
        pertss.append(perts)


    for rtmid in core_d_outnei:
        rtmid_d = core_d_outnei[rtmid]
        adps = []
        final_size = len(rtmid_d.keys())

        ind += 1


        #if final_size< THRES_SIZE:
        #    continue


        #tfr_list = [] # find the first THRES_SIZE adopters

        for adp_ in rtmid_d:
            t_diff = abs(int(rtmid_d[adp_][0]))+0.01*random.random()
            if t_diff <= THRES_TIME:
                adps.append(adp_)

        '''
        tfr_dict = {}
        for adp_ in rtmid_d:
            t_diff = abs(int(rtmid_d[adp_][0]))+0.01*random.random()
            #print 'time from root for this adp %d is %d, t_diif - T_FR = %d' %(int(adp_),t_diff,t_diff-T_FR)
            tfr_dict[int(adp_)] = t_diff

        tfr_list = tfr_dict.values()
        tfr_list.sort()

        #t_time = tfr_list[THRES_SIZE-1] - tfr_list[0]
        #avg_t_fr = float(sum(tfr_list[0:(THRES_SIZE-1)]))/THRES_SIZE #average time for one adoption behavior happen, not average exposure time for adopters
        for adp_ in tfr_dict:
            t_diff = tfr_dict[adp_]
            if tfr_list.index(t_diff) < THRES_SIZE:
                adps.append(adp_)
        '''


        #For the size version, we also need a time stamp to judge if the outnei is a NA or FNT
        #So I pick the time when the 50th adopter reposted the msg as time stamp
        #T_FR = tfr_list[THRES_SIZE-1]

        #cur_size = len(adps)
        set_adps = set(adps)
        '''
        cur_size = len(set_adps)
        if cur_size != THRES_SIZE:
            print cur_size
            raise 'the size is not correct'
        '''

        row = [ind]

        for x in xrange(0,len(all_cent_lists)):
            cent_list = all_cent_lists[x]
            perts = pertss[x]

            cent_vs = []
            for adp in set_adps:
                cent_vs.append(cent_list[adp])
            avg_cent_v = np.average(cent_vs)
            num_nodes_over_perts = []
            for i in xrange(0,len(perts)):
                num_nodes_over_perts.append(0)

            for v in cent_vs:
                for j in xrange(0,len(perts)):
                    if v > perts[j]:
                        num_nodes_over_perts[j] += 1
            num_nodes_over_perts.append(avg_cent_v)
            row.extend(num_nodes_over_perts)

            #print num_nodes_over_perts,ind

        writer.writerow(row)
Example #4
0
            for_hm_cent.append(temp_l)

        for_hm.append(heatmap(for_hm_cent))

    return for_hm

if __name__ == '__main__':

    pertss = []
    fns = os.listdir(cas_cent_path)
    #print fns
    for_hm = []
    for fn in fns:

        full_path = os.path.join(cas_cent_path,fn)
        cas_cent_list = load_obj(full_path)
        perts = list(percentiles(cas_cent_list))
        pertss.append(perts)

    hm_all = func(pertss,cas_cent_path=cas_cent_path)
    hm_viral = func(pertss,cas_cent_path=cas_cent_path+'_v')

    for i in xrange(0,len(os.listdir(cas_cent_path))):
        hm_cent = hm_all[i].reshape(-1)
        hm_cent_v = hm_viral[i].reshape(-1)
        result = []
        for i in xrange(0,len(hm_cent)):
            deno = hm_cent[i]
            enum = hm_cent_v[i]
            if deno == 0:
                result.append(0)
Example #5
0
__author__ = "rguo12"

path = "C:\\weibodataset\\heat_map_dv1_notact.pkl"
from asonam_caspre.tool_box import load_obj, save_obj
import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":
    d = load_obj(path)

    result = []

    for i in xrange(1, 6):
        row = []
        for j in xrange(1, 6):
            key = str(i) + "," + str(j)
            if key in d:
                row.append(d[key])
            else:
                row.append(0)

        result.append(row)
    print result
    result = np.asarray(result)
    """
    fig, ax = plt.subplots()
    plt.subplots_adjust(left=0.2, right=0.95, top=0.95, bottom=0.2)
    heatmap = ax.pcolor(result, cmap=plt.cm.Blues)
    cbar = plt.colorbar(heatmap)

    ax.tick_params(axis='both', which='major', labelsize=24)