def test_community_1(): """ idea: regard a community as a sub-matrix, generate them respectively. :return: """ node = 1000 lmd = 1.2 dmin = 1 dmax = 40 comu = 2 com_size = [int(node / comu) for _ in range(comu)] com_size[-1] += node - sum(com_size) i_list = [0] * node j_list = [0] * node total_edges = 0 img = np.zeros((node, node)) start_i, start_j = 0, 0 for size in com_size: is_even = size % 2 == 1 power_law = NGPowerLaw(lmd, dmin, dmax, size) for i in range(size): d_out = power_law.get_d() for _ in range(d_out): j = power_law.get_j() j = transform(is_even, j, size-1) a_i = start_i + i a_j = start_j + j if img[a_i, a_j] == 0: total_edges += 1 i_list[a_i] += 1 j_list[a_j] += 1 img[a_i, a_j] = 0.9 start_i += size start_j += size print('total edges: ', total_edges) show_plot(i_list, math.log, dmin, dmax, title='out degree distribution') show_plot(j_list, math.log, dmin, dmax, title='in degree distribution') plt.imshow(img, cmap='gray') plt.show()
def statistic_relation_data(self): """ show plot & show matrix thumbnail :return: """ for rel in self.relation_ins: if self.format == 'CSR': row_name = 'row_offset.csr' col_name = 'col_index.csr' row_file = os.path.join(self.base_dir, rel.label, row_name) col_file = os.path.join(self.base_dir, rel.label, col_name) # show degree distribution out_degree_list, in_degree_list = get_degree_list( row_file, 'CSR', rel.node1, rel.node2, col_file) if rel.out_distribution.get_d_type() == 'power_law': show_plot(out_degree_list, math.log, rel.out_distribution.dmin, \ rel.out_distribution.dmax, 'out-degree distribution') else: show_plot(out_degree_list, lambda x: x, rel.out_distribution.dmin, \ rel.out_distribution.dmax, 'out-degree distribution') if rel.in_distribution.get_d_type() == 'power_law': show_plot(in_degree_list, math.log, rel.in_distribution.dmin, \ rel.in_distribution.dmax, 'in-degree distribution') else: show_plot(in_degree_list, lambda x: x, rel.in_distribution.dmin, \ rel.in_distribution.dmax, 'in-degree distribution') # show matrix thumbnail show_matrix_thumbnail(row_file, 'CSR', rel.node1, rel.node2, col_file) else: data_name = 'data.' + self.format.lower() data_file = os.path.join(self.base_dir, rel.label, data_name) # show degree distribution out_degree_list, in_degree_list = get_degree_list( data_file, self.format, rel.node1, rel.node2) if rel.out_distribution.get_d_type() == 'power_law': show_plot(out_degree_list, math.log, rel.out_distribution.dmin, \ rel.out_distribution.dmax, 'out-degree distribution') else: show_plot(out_degree_list, lambda x: x, rel.out_distribution.dmin, \ rel.out_distribution.dmax, 'out-degree distribution') if rel.in_distribution.get_d_type() == 'power_law': show_plot(in_degree_list, math.log, rel.in_distribution.dmin, \ rel.in_distribution.dmax, 'in-degree distribution') else: show_plot(in_degree_list, lambda x: x, rel.in_distribution.dmin, \ rel.in_distribution.dmax, 'in-degree distribution') # show matrix thumbnail show_matrix_thumbnail(data_file, self.format, rel.node1, rel.node2)
def test_overlap_community(): """ idea: regard a community as a sub-matrix, generate them respectively. when d_out > threshold * dmax, add some noise the degree of noise: [1, dmax] x: degree f(x): the probability of degree x f(x) = a * exp(-x / c) procedure: generate noise degree y = random(0, 1) d = -c * ln(exp(-1/c) - y * ( (exp(-1/c) - exp(-dmax/c) ) ) parameter description: c: > 0, the bigger c, the more noise :return: """ node = 1000 lmd = 1.2 dmin = 1 dmax = 40 comu = 3 threshold = 0.5 overlap = 0.1 param_c = 10 overlap_size = int(node * overlap / 2) com_size = [int(node / comu) for _ in range(comu)] com_size[-1] += node % comu i_list = [0] * node j_list = [0] * node total_edges = 0 extra_edges = 0 expect_edges = 0 img = np.zeros((node, node)) start_i, start_j = 0, 0 threshold = round(threshold * dmax) const_a = math.exp(-1/param_c) const_b = const_a - math.exp(-dmax/param_c) overlap_pl = NGPowerLaw(lmd*2, 1, int((dmin+dmax)/4), overlap_size) ol_even = overlap_size % 2 == 1 for size in com_size: is_even = size % 2 == 1 power_law = NGPowerLaw(lmd, dmin, dmax, size) for i in range(size): d_out = power_law.get_d() expect_edges += d_out a_i = start_i + i for _ in range(d_out): j = power_law.get_j() j = transform(is_even, j, size - 1) a_j = start_j + j if img[a_i, a_j] == 0: total_edges += 1 i_list[a_i] += 1 j_list[a_j] += 1 img[a_i, a_j] = 0.9 if d_out > threshold: y = random.random() d_extra = int(-param_c * math.log(const_a - y * const_b)) extra_edges += d_extra for _ in range(d_extra): j = power_law.get_j() j = transform(is_even, j, size - 1) j = scale(j, 0, size - 1, 0, node - size - 1) if j > start_j: j = j + size if img[a_i, j] == 0: total_edges += 1 i_list[a_i] += 1 j_list[j] += 1 img[a_i, j] = 0.9 if start_j > overlap_size and overlap_size < size: o_start_i = start_i o_start_j = start_j - overlap_size for _i in range(overlap_size): d_over = overlap_pl.get_d() expect_edges += d_over a_i = _i + o_start_i for _ in range(d_over): j = overlap_pl.get_j() j = transform(ol_even, j, overlap_size-1) a_j = j + o_start_j if img[a_i, a_j] == 0: total_edges += 1 img[a_i, a_j] = 0.9 i_list[a_i] += 1 j_list[a_j] += 1 if start_i > overlap_size and overlap_size < size: o_start_i = start_i - overlap_size o_start_j = start_j for _i in range(overlap_size): d_over = overlap_pl.get_d() expect_edges += d_over a_i = _i + o_start_i for _ in range(d_over): j = overlap_pl.get_j() j = transform(ol_even, j, overlap_size-1) a_j = j + o_start_j if img[a_i, a_j] == 0: total_edges += 1 img[a_i, a_j] = 0.9 i_list[a_i] += 1 j_list[a_j] += 1 start_i += size start_j += size print('total edges: ', total_edges) print('extra edges: ', extra_edges) print('expect edges: ', expect_edges) show_plot(i_list, math.log, dmin, dmax, title='out degree distribution') show_plot(j_list, math.log, dmin, dmax, title='in degree distribution') plt.imshow(img, cmap='gray') plt.show()
def test_add_noise(): """ idea: regard a community as a sub-matrix, generate them respectively. add noise: when d_out > threshold, add some noise :return: """ node = 1000 lmd = 1.2 dmin = 1 dmax = 40 comu = 3 threshold = 0.5 com_size = [int(node / comu) for _ in range(comu)] com_size[-1] += node % comu i_list = [0] * node j_list = [0] * node total_edges = 0 extra_edges = 0 expect_edges = 0 img = np.zeros((node, node)) start_i, start_j = 0, 0 threshold *= dmax e1 = math.exp(1) ed = math.exp(dmax) for size in com_size: is_even = size % 2 == 1 power_law = NGPowerLaw(lmd, dmin, dmax, size) for i in range(size): d_out = power_law.get_d() expect_edges += d_out a_i = start_i + i for _ in range(d_out): j = power_law.get_j() j = transform(is_even, j, size-1) a_j = start_j + j if img[a_i, a_j] == 0: total_edges += 1 i_list[a_i] += 1 j_list[a_j] += 1 img[a_i, a_j] = 0.9 if d_out > threshold: y = random.random() d_extra = int(-math.log((1-y)/e1 + y/ed)) extra_edges += d_extra for _ in range(d_extra): j = power_law.get_j() j = transform(is_even, j, size-1) j = scale(j, 0, size-1, 0, node-size-1) if j > start_j: j = j + size if img[a_i, j] == 0: total_edges += 1 i_list[a_i] += 1 j_list[j] += 1 img[a_i, j] = 0.9 start_i += size start_j += size print('total edges: ', total_edges) print('extra edges: ', extra_edges) print('expect edges: ', expect_edges) show_plot(i_list, math.log, dmin, dmax, title='out degree distribution') show_plot(j_list, math.log, dmin, dmax, title='in degree distribution') plt.imshow(img, cmap='gray') plt.show()
def test_community(): node = 1000 lmd = 1.23 dmin = 1 dmax = 20 power_law = NGPowerLaw(lmd, dmin, dmax, node) # two communities d_list = [0] * node i_list = [0] * node j_list = [0] * node for i in range(node): d_list[i] = power_law.get_d() img = np.zeros((node, node)) is_even = node % 2 == 1 step0 = int(node / 2) size = int(node / 2) for i in range(step0): if d_list[i] >= size: extra = d_list[i] - size for j in range(size): img[i, j] = 0.5 j_list[j] += 1 i_list[i] += size for _ in range(extra): j = power_law.get_j() j = transform(is_even, j, node-1) j = scale(j, 0, node-1, size, node-1) if img[i, j] == 0: i_list[i] += 1 img[i, j] = 0.5 j_list[j] += 1 else: for _ in range(d_list[i]): j = power_law.get_j() j = transform(is_even, j, node-1) j = scale(j, 0, node-1, 0, size-1) if img[i, j] == 0: i_list[i] += 1 img[i, j] = 0.5 j_list[j] += 1 for i in range(step0, node): if d_list[i] >= size: extra = d_list[i] - size for j in range(step0, node): img[i, j] = 0.5 j_list[j] += 1 i_list[i] += size for _ in range(extra): j = power_law.get_j() j = transform(is_even, j, node-1) j = scale(j, 0, node-1, 0, size-1) if img[i, j] == 0: i_list[i] += 1 img[i, j] = 0.5 j_list[j] += 1 else: for _ in range(d_list[i]): j = power_law.get_j() j = transform(is_even, j, node-1) j = scale(j, 0, node-1, step0, node-1) if img[i, j] == 0: i_list[i] += 1 img[i, j] = 0.5 j_list[j] += 1 show_plot(i_list, math.log, dmin, dmax, title='out degree distribution') show_plot(j_list, math.log, dmin, dmax, title='in degree distribution') plt.imshow(img, cmap='gray') plt.show()
points.append((6, (x, y, z + .15))) # x = float(sys.argv[1]) y = float(sys.argv[2]) z = float(sys.argv[3]) # example 0, 0, 0.438 # later on, need to input initial angles, or the first 6 inputs, plus the 3 inputs of the coordinates to get to # def handler(signum, frame): raise Exception("end of time") # for i in range(0, 20): # for j in range(0, 20): # for k in range(0, 20): # signal.signal(signal.SIGALRM, handler) # signal.alarm(20) # try: # angles = run_km(i/10, j/10, k/10) # print(i/10, j/10, k/10, "OK") # except Exception as exc: # print(i/10, j/10, k/10, "timed out") for i in range(1, 5): angles = run_km(x/4*i, y/4*i, z/4*i) print(110+i) visualization.show(math.radians(angles[1]), math.radians(angles[0]), math.radians(angles[3]), math.radians(angles[2]), x, y, z) visualization.show_plot()
def statistic(self, filename, n, fmt): out_degree_list, in_degree_list = get_degree_list(filename, fmt, n, n) show_plot(out_degree_list, lambda x: x, self.min_outd, self.max_outd, 'out-degree distribution') show_plot(in_degree_list, lambda x: x, self.min_ind, self.max_ind, 'in-degree distribution')