def test_first_phase(): from glb import basepath import os k = 4 ipt = os.path.join(basepath, 'data/z_5/synfix_5.t01.edges') ipt_gc = os.path.join(basepath, 'data/z_5_r/synfix_5.t01.gc') ipt_comm = os.path.join(basepath, 'data/z_5/synfix_5.t01.comm1') opt = os.path.join(basepath, 'data/z_5_r/s5t01.graph.json') opt_hubs = os.path.join(basepath, 'data/z_5_r/s5t01.hubs.csv') gc.gen_gc(ipt, ipt_gc) g = Graph() sm.create_graph(ipt_gc, g) sm.load_cluster_info(g, ipt_comm) U, H, L, X = core.cdnmf(g.adjmatrix(), k) ce.getcluster_bycore(g, k, U, H) ce.getcluster_rest(g, k) ce.get_hubs(g, k, U, opt_hubs) nmi = sm.compute_nmi(g) print '===NMI===\n', nmi g.dump(opt) a = g.adjmatrix() opt = 'data/z_5_r/test.adj' writer = open(opt, 'w') for i in range(128 * 128): writer.write(str(a.item(i))) if (i + 1) % 128 == 0: writer.write('\n') else: writer.write(',')
def run(lmd): num_iter = 1 from glb import basepath iptdir = os.path.join(basepath, 'data/blog') optdir = os.path.join(basepath, 'data/result/blog') if not os.path.isdir(optdir): os.makedirs(optdir) list_nmi = [] first = True for i in range(1, 15): fmt = 'blog_timeSlice_%02d' % i log.info(fmt + '...') ipt_edges = os.path.join(iptdir, fmt + '.edges') path_gc = os.path.join(optdir, fmt+'.gc') ipt_comm1 = os.path.join(iptdir, fmt+'.comm1') path_hubs = os.path.join(optdir, fmt+'.hubs.csv') #gc.gen_gc(ipt_edges, path_gc) g = Graph() sm.create_graph(path_gc, g) idx_clst = sm.load_cluster_info(g, ipt_comm1) g.dump(os.path.join(optdir,'graph.json')) k = idx_clst.num_indices() if first: lpre = 2.0 ** 32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf(g.adjmatrix(), k) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i+1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i+1, LL[-1], lpre)) first = False else: X = core.adjust_xpre(gpre, g, X) lpre = 2.0 ** 32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf_dynamic(g.adjmatrix(), k, X, lmd) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i+1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i+1, LL[-1], lpre)) gpre = cp.deepcopy(g) ce.getcluster_bycore(g, k, U, H) ce.getcluster_rest(g, k) ce.get_hubs(g, k, U, path_hubs) nmi = sm.compute_nmi(g) list_nmi.append(nmi) print '===NMI===\n',nmi print '===RESULT===' for e in enumerate(list_nmi): print '{}: {}'.format(e[0] + 1, e[1])
def run(lmd, which, times): num_iter = 1 iptdir = os.path.join(basepath, 'data/bench/' + which) optdir = os.path.join(basepath, bd + '/' + which) if not os.path.isdir(optdir): os.makedirs(optdir) list_nmi = [] list_err = [] first = True list_errmat_expected = [] list_errmat_actual = [] for i in range(1, 11): fmt = '%s.t%02d' % (which, i) log.info(fmt + '...') ipt_edges = os.path.join(iptdir, fmt + '.edges') path_gc = os.path.join(optdir, fmt + '.gc') ipt_comm1 = os.path.join(iptdir, fmt + '.comm1') path_hubs = os.path.join(optdir, fmt + '.hubs.csv') #gc.gen_gc(ipt_edges, path_gc) g = Graph() #sm.create_graph(path_gc, g) sm.create_graph(ipt_edges, g) idx_clst = sm.load_cluster_info(g, ipt_comm1) k = idx_clst.num_indices() log.info('num clusters: %d' % k) if first: lpre = 2.0**32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf(g.adjmatrix(), k) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i + 1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i + 1, LL[-1], lpre)) first = False else: X = core.adjust_xpre(gpre, g, X) lpre = 2.0**32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf_dynamic(g.adjmatrix(), k, X, lmd) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i + 1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i + 1, LL[-1], lpre)) gpre = cp.deepcopy(g) ''' ce.getcluster_bycore(g, k, U, H) ce.getcluster_rest(g, k) #ce.get_hubs(g, k, U, path_hubs) ''' getcluster_byx(g, X) nmi = sm.compute_nmi(g) list_nmi.append(nmi) e, a = sm.get_errmat(g, k) list_errmat_expected.append(e) list_errmat_actual.append(a) list_err.append(sm.compute_error(g, k)) print '===NMI===\n', len(list_nmi), nmi d = {} d['errmat_expected'] = sm.cell(list_errmat_expected) d['errmat_actual'] = sm.cell(list_errmat_actual) sio.savemat(os.path.join(optdir, 'lmd{}_times{}.mat'.format(lmd, times)), d) print '===RESULT===' for e in enumerate(list_nmi): print '{}: {}'.format(e[0] + 1, e[1]) return list_nmi, list_err
def run(filename, lmd, times): num_iter = 1 if filename != 'syn_T_10_z_4_nC_3_bS_128_aD_16.mat': return None, None ipt = os.path.join(basepath, 'data/syn/' + filename) optdir = os.path.join(basepath, 'data/result/syn/{}'.format(filename[:-4])) if not os.path.isdir(optdir): os.makedirs(optdir) list_nmi = [] list_err = [] first = True list_errmat_expected = [] list_errmat_actual = [] As, list_clsts = load_mat(ipt) for idx in range(10): log.info('%d ...' % idx) A = As[idx] list_clst = list_clsts[idx] g = Graph() idx_clst = sm.load_graph(g, A, list_clst) k = idx_clst.num_indices() log.info('num clusters: %d' % k) if first: lpre = 2.0**32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf(g.adjmatrix(), k) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i + 1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i + 1, LL[-1], lpre)) first = False else: X = core.adjust_xpre(gpre, g, X) lpre = 2.0**32 for i in range(num_iter): UU, HH, LL, XX = core.cdnmf_dynamic(g.adjmatrix(), k, X, lmd) if LL[-1] < lpre: lpre = LL[-1] U, H, X = UU, HH, XX log.info('%02d -- %.4f %.4f CHANGED!' % (i + 1, LL[-1], lpre)) else: log.info('%02d -- %.4f %.4f' % (i + 1, LL[-1], lpre)) gpre = cp.deepcopy(g) ''' ce.getcluster_bycore(g, k, U, H) ce.getcluster_rest(g, k) #ce.get_hubs(g, k, U, path_hubs) ''' getcluster_byx(g, X) nmi = sm.compute_nmi(g) list_nmi.append(nmi) e, a = sm.get_errmat(g, k) list_errmat_expected.append(e) list_errmat_actual.append(a) list_err.append(sm.compute_error(g, k)) print '===NMI===\n', len(list_nmi), nmi d = {} d['errmat_expected'] = sm.cell(list_errmat_expected) d['errmat_actual'] = sm.cell(list_errmat_actual) sio.savemat(os.path.join(optdir, 'lmd{}_times{}.mat'.format(lmd, times)), d) print '===RESULT===' for e in enumerate(list_nmi): print '{}: {}'.format(e[0] + 1, e[1]) return list_nmi, list_err