def process_all(name, num_betas, res): new_dir = head_dir + name + "/" hmap_opts = np.zeros((num_betas, res)) hmap_scores_orig = np.zeros((num_betas, res)) hmap_scores = np.zeros((num_betas, res)) for i in range(num_betas): f = open(new_dir + str(i) + "_" + name + ".txt", "r") param_vals = [] opts = [] scores_orig = [] scores = [] for x in f.readline().strip().split("\t"): param_vals.append(float(x)) for x in f.readline().strip().split("\t"): opts.append(float(x)) for x in f.readline().strip().split("\t"): scores_orig.append(float(x)) for x in f.readline().strip().split("\t"): scores.append(float(x)) f.close() if len(opts) == len(hmap_opts[i]): hmap_opts[i] = opts hmap_scores_orig[i] = scores_orig hmap_scores[i] = scores return param_vals, hmap_opts, hmap_scores_orig, hmap_scores
def cartesian(x, y=None, z=None): max_dim = 1 for val in (x, y, z): if is_array(type(val)): if val.size > max_dim: max_dim = val.size if max_dim != 1: if is_scalar(type(x)) or (is_array(type(x)) and x.shape == (1,)): x = ones(max_dim)*x if is_scalar(type(y)) or (is_array(type(y)) and y.shape == (1,)): y = ones(max_dim)*y if is_scalar(type(z)) or (is_array(type(z)) and z.shape == (1,)): z = ones(max_dim)*z if type(x) == type(None): x = zeros(max_dim) if type(y) == type(None): y = zeros(max_dim) if type(z) == type(None): z = zeros(max_dim) if x.ndim == y.ndim == z.ndim == 1: if x.shape == y.shape == z.shape: return vstack((x,y,z)).T print 'EE: CoordinateSystem.cartesian() - This should not happen!'
def spherical(r, theta=None, phi=None): # def repetiveStuff(): # self.type = 'spherical' # self.shape = self.p.shape max_dim = 1 for val in (r, theta, phi): if is_array(type(val)): if val.size > max_dim: max_dim = val.size if max_dim != 1: if is_scalar(type(r)) or (is_array(type(r)) and r.shape == (1,)): r = ones(max_dim)*r if is_scalar(type(theta)) or (is_array(type(theta)) and theta.shape == (1,)): theta = ones(max_dim)*theta if is_scalar(type(phi)) or (is_array(type(phi)) and phi.shape == (1,)): phi = ones(max_dim)*phi if type(r) == type(None): r = zeros(max_dim) if type(theta) == type(None): theta = zeros(max_dim) if type(phi) == type(None): phi = zeros(max_dim) if r.ndim == theta.ndim == phi.ndim == 1: if r.shape == theta.shape == phi.shape: p = vstack((r,theta,phi)).T # repetiveStuff() return p print 'EE: CoordinateSystem.spherical() - This should not happen!'
def cartesian(x, y=None, z=None): max_dim = 1 for val in (x, y, z): if is_array(type(val)): if val.size > max_dim: max_dim = val.size if max_dim != 1: if is_scalar(type(x)) or (is_array(type(x)) and x.shape == (1, )): x = ones(max_dim) * x if is_scalar(type(y)) or (is_array(type(y)) and y.shape == (1, )): y = ones(max_dim) * y if is_scalar(type(z)) or (is_array(type(z)) and z.shape == (1, )): z = ones(max_dim) * z if type(x) == type(None): x = zeros(max_dim) if type(y) == type(None): y = zeros(max_dim) if type(z) == type(None): z = zeros(max_dim) if x.ndim == y.ndim == z.ndim == 1: if x.shape == y.shape == z.shape: return vstack((x, y, z)).T print 'EE: CoordinateSystem.cartesian() - This should not happen!'
def all_core_periphery(networks_orig, networks_opt): output = np.zeros((10, 15, 4)) output_std = np.zeros((10, 15, 4)) for j in range(10): classifications, per_comm_assignments, _, _ = core_periphery_analysis( networks_orig[j]) for i in range(15): classified_vals, _ = classify_vals(classifications, networks_orig[j], networks_opt[j][i], per_comm_assignments) for k in range(len(classified_vals)): output[j][i][k] = np.mean(classified_vals[k]) output_std[j][i][k] = np.std(classified_vals[k]) return output, output_std
def pickleable_cost_func(input, comps, comps_c, inv_labels, inv_labels_c, beta, A_target, include_nonexistent, KL=True, weighted_net=None): B = np.zeros((len(A_target), len(A_target))) for i in range(len(comps)): for x in comps[i]: row, col = inv_labels[x] B[row][col], B[col][row] = input[i], input[i] if include_nonexistent: for i in range(len(comps_c)): for x in comps_c[i]: row, col = inv_labels_c[x] B[row][col], B[col][row] = input[len(comps) + i], input[len(comps) + i] if KL: return KL_score_external(B, beta, A_target, weighted_net=weighted_net) else: return uniformity_cost(A_target, B, beta)
def get_complement_graph(A): B = np.zeros((len(A), len(A))) B[A == 0] = 1 B[A == 1] = 0 for i in range(len(A)): B[i][i] = 0 return B
def create_modular_toy(edges, modular_edges): adjMatrix = np.zeros((15, 15)) module_1, module_2, module_3 = np.arange(5), np.arange(5, 10), np.arange( 10, 15) modules = [] modules.append(module_1), modules.append(module_2), modules.append( module_3) def add_modular_edge(): randomComm = seeded_rng.randint(0, 3) randEdge = seeded_rng.choice(modules[randomComm], 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0 or adjMatrix[ randEdge[1]][randEdge[0]] != 0: randomComm = seeded_rng.randint(0, 3) randEdge = seeded_rng.choice(modules[randomComm], 2, replace=False) adjMatrix[randEdge[0]][randEdge[1]] += 1 adjMatrix[randEdge[1]][randEdge[0]] += 1 def add_cross_edge(): #adds edge outside modules randEdge = seeded_rng.choice(15, 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0 or adjMatrix[randEdge[1]][randEdge[0]] != 0 or \ (randEdge[0] in module_1 and randEdge[1] in module_1) \ or (randEdge[0] in module_2 and randEdge[1] in module_2) or \ (randEdge[0] in module_3 and randEdge[1] in module_3): randEdge = seeded_rng.choice(15, 2, replace=False) adjMatrix[randEdge[0]][randEdge[1]] += 1 adjMatrix[randEdge[1]][randEdge[0]] += 1 for i in range(modular_edges): add_modular_edge() for i in range(edges - modular_edges): add_cross_edge() return adjMatrix
def s2c(p): # if self.type != 'spherical': # print 'EE: Huh? The coordinates are not spherical...' # return if p.shape[1] == 1: print 'EE: Can\'t do much with a single coordinate...' return elif p.shape[1] == 2: r = p[:, 0] theta = p[:, 1] # phi = zeros(self.p[:,3] # rho = z = r * cos(theta) y = zeros(z.size) x = r * sin(theta) p[:] = vstack((x, y, z)).T elif p.shape[1] == 3: r = p[:, 0] theta = p[:, 1] phi = p[:, 2] rho = r * sin(theta) z = r * cos(theta) y = rho * sin(phi) x = rho * cos(phi) p = Coordinate(x=x, y=y, z=z) return p
def stoch_block_parameterized(blocks, p_cc, p_in): pMatrix = np.zeros((len(blocks), len(blocks))) comms = {} k = 0 for i in range(len(blocks)): for j in range(blocks[i]): comms[k] = i k += 1 for i in range(len(pMatrix)): for j in range(len(pMatrix)): if i == j: pMatrix[i][j] = p_in else: pMatrix[i][j] = p_cc G = nx.generators.stochastic_block_model(blocks, pMatrix) A = np.array(nx.to_numpy_matrix(G)) def parameterized(cc_weight): B = deepcopy(A) for i in range(len(A)): for j in range(len(A)): if comms[i] is not comms[j] and A[i][j] != 0: B[i][j], B[j][i] = cc_weight, cc_weight return B return A, parameterized
def s2c(p): # if self.type != 'spherical': # print 'EE: Huh? The coordinates are not spherical...' # return if p.shape[1] == 1: print 'EE: Can\'t do much with a single coordinate...' return elif p.shape[1] == 2: r = p[:,0] theta = p[:,1] # phi = zeros(self.p[:,3] # rho = z = r*cos(theta) y = zeros(z.size) x = r*sin(theta) p[:] = vstack((x,y,z)).T elif p.shape[1] == 3: r = p[:,0] theta = p[:,1] phi = p[:,2] rho = r*sin(theta) z = r*cos(theta) y = rho*sin(phi) x = rho*cos(phi) p = Coordinate(x=x,y=y,z=z) return p
def save_combined_key_points_imgs(key_points_bank, resized_imgs_bank, show_img = False): """ Input: key_points_bank: A two dimension list contains all key points indexs resized_imgs_bank: A one dimension list contain resized images Output: None """ loc = "../task2_img" + "/combined_keypoints_imgs/" for i in range(len(key_points_bank)): resized_img = resized_imgs_bank[i] img_black = np.asarray(mnp.zeros(resized_img.shape[0], resized_img.shape[1])) set_pts = set() for j in range(len(key_points_bank[i])): set_pts = set_pts.union(set(key_points_bank[i][j])) name = "octave_" + str(i+1) +"_keypoints_img" img_clone = np.copy(resized_img) img_black_clone = np.copy(img_black) img_clone[[i for i in zip(*set_pts)]] = 255 img_black_clone[[i for i in zip(*set_pts)]] = 255 cv2.imwrite(loc + name + ".jpg", img_clone) cv2.imwrite(loc + name + "_black.jpg", img_black_clone) if show_img: cv2.namedWindow(name, cv2.WINDOW_NORMAL) cv2.imshow(name, img_clone) cv2.waitKey(0) cv2.destroyAllWindows()
def texture_filtering(img_gray, kernel): """ Purpose: use to filter the gray image given the kernel Input: img_gray: an two dimension ndarray matrix, dtype:usually is uint8 representint the gray image. kernel: a two dimension ndarray matrix Output: The filtered image without padding around. """ row_pad = math.floor(kernel.shape[0] / 2) col_pad = math.floor(kernel.shape[1] / 2) img_gray = np.ndarray.tolist(img_gray) img_gray = np.asarray( mnp.pad(img_gray, row_pad, row_pad, col_pad, col_pad, 0)) img_res = np.asarray(mnp.zeros(img_gray.shape[0], img_gray.shape[1])) flipped_kernel = np.asarray((mnp.flip(np.ndarray.tolist(kernel)))) for i in range(row_pad, img_gray.shape[0] - row_pad): for j in range(col_pad, img_gray.shape[1] - col_pad): patch = mnp.inner_product( img_gray[i - row_pad:i + row_pad + 1, j - col_pad:j + col_pad + 1], flipped_kernel) img_res[i, j] = mnp.sum_all(patch) return img_res[row_pad:img_res.shape[0] - row_pad, col_pad:img_res.shape[1] - col_pad]
def cost_func_zipped(input, P_0, pi, beta, J, I): iu = np.triu_indices(len(P_0), k=1) A = np.zeros((len(P_0), len(P_0))) A[iu] = input A = np.maximum(A, A.T) cost = cost_func(P_0, pi, A, beta, J, I) print(cost) return cost
def grad_zipped(input, P_0, pi, beta, J, I): iu = np.triu_indices(len(P_0), k=1) A = np.zeros((len(P_0), len(P_0))) eta = np.exp(-beta) A[iu] = input A = np.maximum(A, A.T) output = gradient(P_0, pi, A, eta, J, I) return output[iu]
def create_network(N, edges): adjMatrix = np.zeros((N, N), dtype=float) for i in range(edges): randEdge = seeded_rng.choice(N, 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0: randEdge = seeded_rng.choice(N, 2, replace=False) adjMatrix[randEdge[0]][randEdge[1]] += 1.0 return adjMatrix
def symmetry_toy(): A = np.zeros((15, 15)) for i in range(5): for j in range(i + 1, 6): A[i][j], A[j][i] = 1, 1 for i in range(8, 15): A[i][6], A[6][i] = 1, 1 A[i][7], A[7][i] = 1, 1 A[5][6], A[6][5] = 1, 1 return A
def lca(data_sum, Y_AVG ): Ny = data_sum.shape[0] Nx = data_sum.shape[1] Nw = data_sum.shape[2] # Select the window that yield the least power if Y_AVG == 0: l = range(0,Ny) m = range(0,Nx) l,m = np.meshgrid(range(0,Nx),range(0,Ny)) selected_window = np.abs(data_sum).argmin(2) return data_sum[m,l,selected_window] else: # img_lca_all_i[i,:,:] = # For any center pixel, select the window that yields the lowest variance # for the local segment of pixels - i.e. use the same window on all and see # who yields the lowest variance. # This is the same as iterating over all windows and sum up the range # interval, and select the window with the lowest sum. # Equvalently, one may also define the range of y's, iterate of each of # these and select the window that yields the lowest sum. OFFSET = Y_AVG # WIN_SIZE = 2*OFFSET+1 img_y = np.zeros((Nw,Ny+2*OFFSET), dtype=complex) new_y = np.zeros((Ny,), dtype=complex) new_w = np.zeros((Ny,), dtype=int) new_img = np.zeros((Ny,Nx), dtype=complex) new_win = np.zeros((Ny,Nx), dtype=int) img_y_segment_abs2 = np.zeros((Nw,2*OFFSET), dtype=float) img_y_segment_abs2_sum = np.zeros((Nw,), dtype=float) for x in range (0,Nx): img_y = data_sum[:,x,:].T img_y_abs2 = abs(img_y)**2 # Why abs^2? img_y_segment_abs2 = img_y_abs2[:,0:2*OFFSET+1] img_y_segment_abs2_sum = img_y_segment_abs2.sum(1) idx = img_y_segment_abs2_sum.argmin(0) new_y[0] = img_y[idx,0] new_w[0] = idx for y in range(1+OFFSET,Ny-OFFSET): img_y_segment_abs2_sum = img_y_segment_abs2_sum - img_y_abs2[:,y-OFFSET-1] + img_y_abs2[:,y+OFFSET] idx = img_y_segment_abs2_sum.argmin(0) new_y[y-OFFSET] = img_y[idx,y-OFFSET] new_w[y-OFFSET] = idx new_img[:,x] = new_y new_win[:,x] = new_w return new_img, new_win
def WS_trials(N_tot, k, p_res, trials, beta): with np.errstate(invalid='ignore'): p_vals = np.logspace(0, 4, p_res) p_vals /= p_vals[-1] print(p_vals) opts = np.zeros(p_res) scores_orig = np.zeros(p_res) scores_s = np.zeros(p_res) for i in range(p_res): for j in range(trials): network, parameterized = gg.small_world_parameterized( N_tot, k, p_vals[i]) network_s, opt, score_orig, score_s = optimize_one_param_learnability( network, parameterized, beta) opts[i] += opt scores_orig[i] += score_orig scores_s[i] += score_s opts /= trials scores_orig /= trials scores_s /= trials return p_vals, opts, scores_orig, scores_s
def parameterized(input): B = np.zeros((len(A), len(A))) for i in range(len(comps)): for x in comps[i]: row, col = inv_labels[x] B[row][col], B[col][row] = input[i], input[i] if include_nonexistent: for i in range(len(comps_c)): for x in comps_c[i]: row, col = inv_labels_c[x] B[row][col], B[col][row] = input[len(comps) + i], input[len(comps)+i] return B
def SBM_trials(N_tot, N_comm, edges, alpha, frac_res, trials, beta): with np.errstate(invalid='ignore'): frac_modules = np.linspace(.2, 1, frac_res, endpoint=False) mod_opts, hMod_opts = np.zeros(frac_res), np.zeros(frac_res) scores_mod_orig, scores_hMod_orig = np.zeros(frac_res), np.zeros( frac_res) scores_mod_s, scores_hMod_s = np.zeros(frac_res), np.zeros(frac_res) for i in range(frac_res): for j in range(trials): mod, parMod = gg.get_random_modular(N_tot, N_comm, edges, frac_modules[i]) hMod, parhMod = gg.get_hierarchical_modular( N_tot, N_comm, edges, frac_modules[i], alpha) mod_s, mod_opt, score_mod_orig, score_mod_s = optimize_one_param_learnability( mod, parMod, beta) hMod_s, hMod_opt, score_hMod_orig, score_hMod_s = optimize_one_param_learnability( hMod, parhMod, beta) mod_opts[i] += mod_opt hMod_opts[i] += hMod_opt scores_mod_orig[i] += score_mod_orig scores_hMod_orig[i] += score_hMod_orig scores_mod_s[i] += score_mod_s scores_hMod_s[i] += score_hMod_s mod_opts /= trials hMod_opts /= trials scores_mod_orig /= trials scores_hMod_orig /= trials scores_mod_s /= trials scores_hMod_s /= trials return frac_modules, mod_opts, hMod_opts, scores_mod_orig, scores_hMod_orig, scores_mod_s, scores_hMod_s
def reduced_cost_func(input, comps, comps_c, inv_labels, inv_labels_c, beta, A_target, indices_c): B = np.zeros((len(A_target), len(A_target))) for i in range(len(comps)): for x in comps[i]: row, col = inv_labels[x] B[row][col], B[col][row] = input[i], input[i] for i in range(len(indices_c)): for x in comps_c[indices_c[i]]: row, col = inv_labels_c[x] B[row][col], B[col][row] = input[len(comps) + i], input[len(comps) + i] return KL_score_external(B, beta, A_target)
def create_undirected_network(N, edges): adjMatrix = np.zeros((N, N), dtype=float) perm = np.arange(N) for i in range(N - 1): adjMatrix[perm[i]][perm[i + 1]] = 1.0 adjMatrix[perm[i + 1]][perm[i]] = 1.0 for i in range(edges - N + 1): randEdge = seeded_rng.choice(N, 2, replace=False) while adjMatrix[randEdge[0]][randEdge[1]] != 0: randEdge = seeded_rng.choice(N, 2, replace=False) #print("STUCK") adjMatrix[randEdge[0]][randEdge[1]] = 1.0 adjMatrix[randEdge[1]][randEdge[0]] = 1.0 return adjMatrix
def spherical(r, theta=None, phi=None): # def repetiveStuff(): # self.type = 'spherical' # self.shape = self.p.shape max_dim = 1 for val in (r, theta, phi): if is_array(type(val)): if val.size > max_dim: max_dim = val.size if max_dim != 1: if is_scalar(type(r)) or (is_array(type(r)) and r.shape == (1, )): r = ones(max_dim) * r if is_scalar(type(theta)) or (is_array(type(theta)) and theta.shape == (1, )): theta = ones(max_dim) * theta if is_scalar(type(phi)) or (is_array(type(phi)) and phi.shape == (1, )): phi = ones(max_dim) * phi if type(r) == type(None): r = zeros(max_dim) if type(theta) == type(None): theta = zeros(max_dim) if type(phi) == type(None): phi = zeros(max_dim) if r.ndim == theta.ndim == phi.ndim == 1: if r.shape == theta.shape == phi.shape: p = vstack((r, theta, phi)).T # repetiveStuff() return p print 'EE: CoordinateSystem.spherical() - This should not happen!'
def regularized_sierpinski(n, p): A = np.zeros((p**n + p**(n - 1), p**n + p**(n - 1))) A_nonreg = sierpinski_generator(n, p) A_nonreg2 = sierpinski_generator(n - 1, p) for i in range(len(A_nonreg)): for j in range(len(A_nonreg)): A[i][j] = A_nonreg[i][j] for i in range(len(A_nonreg2)): for j in range(len(A_nonreg2)): A[len(A_nonreg) + i][len(A_nonreg) + j] = A_nonreg2[i][j] for i in range(p): e0 = len(A_nonreg) + i * (p**(n - 1) - 1) // (p - 1) e1 = i * (p**(n) - 1) // (p - 1) A[e0][e1], A[e1][e0] = 1, 1 return A
def sierpinski_generator( n, p ): #generates a generalized Sierpinski graph with n hierarchical levels and p communities A = np.zeros((p**n, p**n)) for k in range(n): for i in range(p): for j in range(p): for m in range(p**(n - k - 1)): if i != j: e0 = m * (p**(k + 1)) + i * (p**k) + j * ( (p**k) - 1) // (p - 1) e1 = m * (p**(k + 1)) + j * (p**k) + i * ( (p**k) - 1) // (p - 1) A[e0][e1] = 1 return A
def modular_toys_general(N_tot, N_comms, cc_bias, b_bias): A = np.zeros((N_tot, N_tot)) N_in = N_tot // N_comms b = [] #boundary nodes for i in range(N_comms): for j in range(i * N_in, (i + 1) * N_in - 1): for k in range(j + 1, (i + 1) * N_in): A[j][k], A[k][j] = 1.0, 1.0 A[i * N_in][(i + 1) * N_in - 1], A[(i + 1) * N_in - 1][i * N_in] = 0, 0 for i in range(N_comms): b.append(i * N_in) b.append((i + 1) * N_in - 1) for j in [i * N_in, (i + 1) * N_in - 1]: for k in range(i * N_in + 1, (i + 1) * N_in - 1): A[j][k], A[k][j] = b_bias, b_bias for i in range(1, len(b) // 2): A[b[2 * i - 1]][b[2 * i]], A[b[2 * i]][b[2 * i - 1]] = cc_bias, cc_bias A[b[0]][b[len(b) - 1]], A[b[len(b) - 1]][b[0]] = cc_bias, cc_bias return A
def gaussin_kernel_gen(sigma, size=7): """ Purpose: compute the gaussin kernel given the sigma and kernel size Input: sigma: a real number size: int, the size of kernel Output: a gaussin kernel """ if(size % 2 == 0): raise Exception("kernel size should be odd number") mat = np.asarray(mnp.zeros(size,size)) pad = int(size/2) dividend = 0 for i in range(size): for j in range(size): mat[i,j] = gaussin_val(j-pad, pad-i, sigma) dividend += mat[i,j] return mat / dividend
def compute_line_graph_details(A): edge_labels = dict() count = 0 for r in range(len(A)-1): for c in range(r+1, len(A)): if A[r][c] == 1.0: edge_labels[(r,c)] = count count += 1 inv_labels = {v: k for k, v in edge_labels.items()} keys = list(edge_labels.keys()) edges = int(np.sum(A) / 2) for k in keys: r,c = k edge_labels[(c,r)] = edge_labels[(r,c)] output = np.zeros((edges, edges)) for i in range(edges - 1): a, b = inv_labels[i] for j in range(i + 1, edges): c, d = inv_labels[j] if a == c or a == d or b == c or b == d: if not ((a == d and b == c) or (a == c and b == d)): output[i][j], output[j][i] = 1, 1 return edge_labels, inv_labels, output
def modular_toy_paper( ): ##constructs the three-community network, often used in studying human information processing result = np.zeros((15, 15)) for i in range(5): for j in range(5): result[i][j] += 1.0 for i in range(5, 10): for j in range(5, 10): result[i][j] += 1.0 for i in range(10, 15): for j in range(10, 15): result[i][j] += 1.0 for i in range(15): result[i][i] = 0 result[0][4], result[4][0] = 0, 0 result[0][14], result[14][0] = 1, 1 result[5][9], result[9][5] = 0, 0 result[4][5], result[5][4] = 1, 1 result[9][10], result[10][9] = 1, 1 result[10][14], result[14][10] = 0, 0 return result
def loadFile(self, group, index, *args): '''Load configuration from a predefined list of .mat files''' from Coordinate import Coordinate from Window import Window from Medium import Medium from Signal import Signal from System import System from Data import Data from Array import Array from Image import Image import os PHDCODE_ROOT = os.environ['COS_ROOT'] Ni = len(args) if type(index) == int: idx = copy(index) index = [idx] for i in index: if Ni > 0: origin = fileLUT(group, i, args) else: origin = fileLUT(group, i) if origin.type == 'ultrasound multi file': print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file #print 'Loading ' + PHDCODE_ROOT + origin.path + origin.info_file info_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.info_file) mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) NFrames = info_file['NFrames'][0,0] NElements = info_file['NElements'][0,0] angles = info_file['angles'] ranges = info_file['ranges'] tmp = mat_file['frame%d'%origin.index].shape Xd = np.zeros((tmp[0], tmp[1], tmp[2]), dtype=np.complex64) Xd[:,:,:] = mat_file['frame%d'%i] s = System() s.data = Data() s.data.Xd = Xd s.data.angles = angles s.data.ranges = ranges s.data.M = NElements s.data.NFrames = NFrames return s elif origin.type == 'ultrasound_simulation': import tables as tb s = System() s.data = Data() print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file file = tb.openFile( (PHDCODE_ROOT + origin.path + origin.file), 'r' ) root = file.getNode( file.root ) s.data.M = root.frame_1.N_rx.read() s.data.angles = root.frame_1.theta.read()[0] s.data.ranges = root.frame_1.range.read() d = root.frame_1.datacube.read().shape s.data.Xd = np.zeros((100,d[1],d[2],d[0]),dtype=np.complex64) for i in range(100): tmp = np.transpose(getattr(root, 'frame_%d'%(i+1)).datacube.read(), (1, 2, 0)) s.data.Xd[i,:,:,:] = tmp file.close() return s else: print 'WW DataInput() - No method implemented for '+origin.type
def loadFile(self, group, index, *args): '''Load configuration from a predefined list of .mat files''' from Coordinate import Coordinate from Window import Window from Medium import Medium from Signal import Signal from System import System from Data import Data from Array import Array from Image import Image import os PHDCODE_ROOT = os.environ['COS_ROOT'] Ni = len(args) if type(index) == int: idx = copy(index) index = [idx] for i in index: if Ni > 0: origin = fileLUT(group, i, args) else: origin = fileLUT(group, i) # The dataset 'type' (defined by me) selects the "import method": if origin.type == 'hugin_delayed': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) BF = mat_file['p'][0, 0]['BF'][0, 0] s = System() s.data = Data() s.data.Xd = Ndarray(mat_file['dataCube'].transpose((1,0,2))) s.data.n = Ndarray(BF['mtaxe_re' ].T) s.data.Nt = Ndarray(BF['Nt'].T) s.data.fs = 40e3 s.data.fc = 100e3 s.data.M = 32 s.data.d = 0.0375 s.data.c = 1500 s.desc = 'HUGIN raw data' return s elif origin.type == 'focus_dump_type_A' or origin.type == 'focus_dump_type_B': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) # Load the 'Sonar' dictionary and get rid of empty dimensions: Sonar = mat_file['Sonar'][0,0] s = System() s.medium = Medium() s.medium.c = Ndarray(Sonar['c'][0,0]) # Propagation speed # self.B = 0 # Assume narrowband # The 'rx_x','rx_y' and 'rx_z' coordinates in the Sonar struct represents # the TX elements' location relative to the HUGIN body. We're going to # rotate this coordination system around the x-axis such that the # y-coordinate is 0 for all elements (22 degrees). rx_x = Sonar['rx_x'][:,1] # RX x-coordinates (HUGIN body) rx_y = Sonar['rx_y'][:,1] # RX y-coordinates (HUGIN body) rx_z = Sonar['rx_z'][:,1] # RX z-coordinates (HUGIN body) rx_yz = np.sqrt(rx_y ** 2 + rx_z ** 2) # RX y-z distance (new z-axis) p = Coordinate(x=rx_x, y=None, z=rx_yz)# New RX coordinates [x, axis] p.setAxesDesc(template='hugin') s.array = Array() s.array.p = p # desc='RX coordinates', # shape_desc=('x','axis')) s.signal = Signal() s.signal.desc = 'upchirp' s.signal.fc = Ndarray(Sonar['fc'][0,0]) if origin.type == 'focus_dump_type_A': s.data = Data() s.data.X = Ndarray(mat_file['mfdata'].T.copy()) s.data.t = Ndarray(mat_file['mtaxe' ][0]) s.image = Image() s.image.xarr = Ndarray(mat_file['xarr'].T.copy()) s.image.yarr = Ndarray(mat_file['yarr'].T.copy()) else: s.data = Data() s.data.X = Ndarray(mat_file['data1'].T.copy()) s.data.t = Ndarray(Sonar['T_pri'][0,0]) s.image = Image() # s.image.xarr = Ndarray(mat_file['xarr'].T.copy()) # s.image.yarr = Ndarray(mat_file['yarr'].T.copy()) return s elif origin.type == 'focus_sas_dump_type_A_parameters': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) # Load the 'Sonar' dictionary and get rid of empty dimensions: Sonar = mat_file['Sonar'][0,0] s = System() s.medium = Medium() s.medium.c = Ndarray(Sonar['c'][0,0]) # Propagation speed # self.B = 0 # Assume narrowband # The 'rx_x','rx_y' and 'rx_z' coordinates in the Sonar struct represents # the TX elements' location relative to the HUGIN body. We're going to # rotate this coordination system around the x-axis such that the # y-coordinate is 0 for all elements (22 degrees). rx_x = Sonar['rx_x'][:,1] # RX x-coordinates (HUGIN body) rx_y = Sonar['rx_y'][:,1] # RX y-coordinates (HUGIN body) rx_z = Sonar['rx_z'][:,1] # RX z-coordinates (HUGIN body) rx_yz = np.sqrt(rx_y ** 2 + rx_z ** 2) # RX y-z distance (new z-axis) p = Coordinate(x=rx_x, y=None, z=rx_yz)# New RX coordinates [x, axis] p.setAxesDesc(template='hugin') s.array = Array() s.array.p = p # desc='RX coordinates', # shape_desc=('x','axis')) s.signal = Signal() s.signal.desc = 'upchirp' s.signal.fc = Ndarray(Sonar['fc'][0,0]) return s elif origin.type == 'focus_sas_dump_type_A_data': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) s = System() s.data = Data() s.data.X = Ndarray(mat_file['mfdata'].copy()) s.data.t = Ndarray(mat_file['mtaxe' ][0]) return s elif origin.type == 'focus_sss_dump_type_A': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) s = System() # s.a = Data() s.img = Ndarray(mat_file['sss_image'].copy()) s.Nping = 140 s.Ny = 4000 s.Nx = 15 s.y_lim = [60,160] return s elif origin.type == 'hisas_sim': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) BF = mat_file['p'][0, 0]['BF'][0, 0] s = System() s.medium = Medium() s.medium.c = Ndarray(BF['c'][0, 0]) # Propagation speed s.array = Array() s.array.desc = 'ula' s.array.M = Ndarray(BF['n_hydros'][0, 0]) # Number of hydrophones s.array.d = Ndarray(BF['d' ][0, 0]) # Element distance s.data = Data() s.data.Xd = Ndarray(mat_file['dataCube']).transpose((1,0,2)) # Ndarray(, # axes = ['y','x','m'], # desc = 'Delayed and matched filtered data') # desc='Delayed and matched filtered data', # shape_desc = ('y','x','m'), # shape_desc_verbose = ('Range','Azimuth','Channel')) s.data.t = Ndarray(BF['mtaxe'].T) # Ndarray(, # axes = ['N'], # desc = 'Time axis') # desc = 'Corresponding time', # shape_desc = ('N','1')) s.phi_min = mat_file['p'][0,0]['phi_min'][0,0] s.phi_max = mat_file['p'][0,0]['phi_max'][0,0] s.R_min = mat_file['p'][0,0]['R_min'][0,0] s.R_max = mat_file['p'][0,0]['R_max'][0,0] s.signal = Signal() s.signal.desc = 'upchirp' s.signal.fc = Ndarray(BF['fc' ][0, 0]) # Carrier frequency return s # Some datasets simply do not fall into a general category. We'll handle these # individually... elif origin.type == 'barge': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) # Load the 'Sonar' dictionary and get rid of empty dimensions: Sonar = mat_file['Sonar'][0, 0] # See http://projects.scipy.org/numpy/wiki/ZeroRankArray s = System() # s.medium = Medium() # s.medium.c = Sonar['c'][0, 0] # Propagation speed # # # Format: Sonar[dictionary key][hydrophone coordinates, bank] # rx_x = Sonar['rx_x'][:, 1] # RX x-coordinates bank 2 (HUGIN body) # rx_y = Sonar['rx_y'][:, 1] # RX y-coordinates bank 2 (HUGIN body) # rx_z = Sonar['rx_z'][:, 1] # RX z-coordinates bank 2 (HUGIN body) # rx_yz = sqrt(rx_y ** 2 + rx_z ** 2) # RX y-z distance (new z-axis) # p = Coordinate(x=rx_x, y=None, z=rx_yz)# New RX coordinates [x, axis] ## p = vstack((rx_x, rx_yz)).T # New RX coordinates [x, axis] # # s.array = Array() # s.array.p = p ## , ## desc='RX coordinates', ## shape_desc=('x','axis')) s.data = Data() s.data.X = Ndarray(mat_file['data1']) s.signal = Signal() s.signal.fc = Ndarray(Sonar['fc'][0,0]) s.medium = Medium() s.medium.c = Ndarray(Sonar['c'][0,0]) # Propagation speed # s.signal = Signal() # s.signal.fc = Sonar['fc'][0, 0] # Carrier frequency # s.signal.bw = Sonar['bw'][0, 0] # s.signal.desc = 'upchirp'# Waveform return s elif origin.type == 'ultrasound multi file': print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file #print 'Loading ' + PHDCODE_ROOT + origin.path + origin.info_file info_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.info_file) mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) NFrames = info_file['NFrames'][0,0] NElements = info_file['NElements'][0,0] angles = info_file['angles'] ranges = info_file['ranges'] tmp = mat_file['frame%d'%origin.index].shape Xd = np.zeros((tmp[0], tmp[1], tmp[2]), dtype=np.complex64) Xd[:,:,:] = mat_file['frame%d'%i] s = System() s.data = Data() s.data.Xd = Xd s.data.angles = angles s.data.ranges = ranges s.data.M = NElements s.data.NFrames = NFrames return s elif origin.type == 'csound': # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later < on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) if origin.index == 1: beamformedCycle = mat_file['beamformedcycle'][0,0] #beamformedFrames = beamformedCycle[0] beamformedCubes = beamformedCycle[1] NFrames = beamformedCycle[2] NFrames = NFrames[0, 0] NElements = beamformedCycle[3] NElements = NElements[0, 0] params = beamformedCycle[4] del beamformedCycle params = params[0, 0] #delays = params[0] angles = params[1]*np.pi/180 ranges = params[2] #phaseFactor = params[3] del params tmp = beamformedCubes[0,0].shape Xd = np.zeros((NFrames,tmp[0],tmp[1],tmp[2]),dtype=np.complex64) for i in range(NFrames): Xd[i,:,:,:] = beamformedCubes[i,0][:,:,:] elif origin.index == 2 or origin.index == 3: if origin.index == 2: NFrames = 10 else: NFrames = 60 NElements = mat_file['NElements'][0,0] angles = mat_file['angles'] ranges = mat_file['ranges'].T tmp = mat_file['frame%d'%1].shape Xd = np.zeros((NFrames, tmp[0], tmp[2], tmp[1]), dtype=np.complex64) for i in range(NFrames): Xd[i,:,:,:] = np.transpose(mat_file['frame%d'%(i+1)], (0, 2, 1)) else: pass # s = System() s.data = Data() s.data.Xd = Xd s.data.angles = angles s.data.ranges = ranges s.data.M = NElements return s elif origin.type == 'ultrasound_simulation': import tables as tb s = System() s.data = Data() # Using scipy.io.loadmat() to load .mat files. The result is a nparray, where structures # in the array are dictionaries (the data is treated later on..). print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file file = tb.openFile( (PHDCODE_ROOT + origin.path + origin.file), 'r' ) root = file.getNode( file.root ) # s.data.fc = root.frame_1.fc.read() # s.data.c = root.frame_1.c.read() s.data.M = root.frame_1.N_rx.read() s.data.angles = root.frame_1.theta.read()[0] # Only first row makes sence to me :p s.data.ranges = root.frame_1.range.read() d = root.frame_1.datacube.read().shape s.data.Xd = np.zeros((100,d[1],d[2],d[0]),dtype=np.complex64) for i in range(100): tmp = np.transpose(getattr(root, 'frame_%d'%(i+1)).datacube.read(), (1, 2, 0)) s.data.Xd[i,:,:,:] = tmp file.close() return s else: print 'WW DataInput() - No method implemented for '+origin.type
def loadFile(self, group, index, *args): '''Load configuration from a predefined list of .mat files''' from Coordinate import Coordinate from Window import Window from Medium import Medium from Signal import Signal from System import System from Data import Data from Array import Array from Image import Image import os PHDCODE_ROOT = os.environ['COS_ROOT'] Ni = len(args) if type(index) == int: idx = copy(index) index = [idx] for i in index: if Ni > 0: origin = fileLUT(group, i, args) else: origin = fileLUT(group, i) if origin.type == 'ultrasound multi file': print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file #print 'Loading ' + PHDCODE_ROOT + origin.path + origin.info_file info_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.info_file) mat_file = io.loadmat(PHDCODE_ROOT + origin.path + origin.file) NFrames = info_file['NFrames'][0, 0] NElements = info_file['NElements'][0, 0] angles = info_file['angles'] ranges = info_file['ranges'] tmp = mat_file['frame%d' % origin.index].shape Xd = np.zeros((tmp[0], tmp[1], tmp[2]), dtype=np.complex64) Xd[:, :, :] = mat_file['frame%d' % i] s = System() s.data = Data() s.data.Xd = Xd s.data.angles = angles s.data.ranges = ranges s.data.M = NElements s.data.NFrames = NFrames return s elif origin.type == 'ultrasound_simulation': import tables as tb s = System() s.data = Data() print 'Loading ' + PHDCODE_ROOT + origin.path + origin.file file = tb.openFile((PHDCODE_ROOT + origin.path + origin.file), 'r') root = file.getNode(file.root) s.data.M = root.frame_1.N_rx.read() s.data.angles = root.frame_1.theta.read()[0] s.data.ranges = root.frame_1.range.read() d = root.frame_1.datacube.read().shape s.data.Xd = np.zeros((100, d[1], d[2], d[0]), dtype=np.complex64) for i in range(100): tmp = np.transpose( getattr(root, 'frame_%d' % (i + 1)).datacube.read(), (1, 2, 0)) s.data.Xd[i, :, :, :] = tmp file.close() return s else: print 'WW DataInput() - No method implemented for ' + origin.type
def W(p=None, w=None, k=None, fc=100e3, c=1480, N=500): if p==None: error('\'p\' are missing. Aborting.') return if k!=None: k_abs = k elif p!=None and fc!=None and c!=None: k_abs = 2*pi*fc/c else: error('Either \'k\', or \'c\' and \'fc\' must be supplied. Aborting.') return theta = np.linspace(-0.5,0.5,N)*pi k = Coordinate(r=k_abs, theta=theta, phi=None) def compute_W(ax,ay,az, w): Wx = dot( ax, w ) Wy = dot( ay, w ) Wz = dot( az, w ) Wxyz = vstack((Wx,Wy,Wz)).T return Wxyz # pT = p.T.copy() ax = exp(1j*outer( k[:,0], p[:,0])) ay = exp(1j*outer( k[:,1], p[:,1])) az = exp(1j*outer( k[:,2], p[:,2])) if type(w) == list: W = [] for wi in w: W.append(compute_W(ax,ay,az, wi)) elif is_np_array_type(w.__class__): if w.ndim == 1: W = [] W.append( compute_W(ax,ay,az,w) ) elif w.ndim == 2: W1 = compute_W(ax,ay,az,w[0]) W = np.zeros((w.shape[0],W1.shape[0],W1.shape[1]),dtype=W1.dtype) W[0] = W1 for i in range(1,w.shape[0]): W[i] = compute_W(ax,ay,az, w[i]) elif w.ndim == 3: W1 = compute_W(ax,ay,az,w[0,0]) # Not exactly efficient, but shouldn't be noticeable W = np.zeros((w.shape[0],w.shape[1],W1.shape[0],W1.shape[1]),dtype=W1.dtype) for i in range(0,w.shape[0]): for j in range(0,w.shape[1]): W[i,j] = compute_W(ax,ay,az, w[i,j]) else: print 'EE: Not implemented.' else: W = [] W.append( compute_W(ax,ay,az,w) ) return W
def get_hierarchical_modular(n, modules, edges, p, alpha, getCommInfo=False): pairings = {} assignments = np.zeros(n, dtype=int) cross_module_edges = [] weights = np.array([(1 + i)**-alpha for i in range(n)]) dists = [] module_dist = np.zeros(modules) for i in range(modules): pairings[i] = [] A = np.zeros((n, n)) for i in range(n): randomModule = seeded_rng.randint(0, modules) pairings[randomModule].append(i) assignments[i] = randomModule for j in range(modules): dist = np.array([weights[i] for i in pairings[j]]) module_dist[j] = np.sum(dist) dist /= np.sum(dist) dists.append(dist) module_dist /= np.sum(module_dist) # nodesPerMod = n // modules # for i in range(modules): # for j in range(nodesPerMod): # pairings[i].append(nodesPerMod * i + j) # assignments[nodesPerMod *i + j] = i # for i in range(modules - 1): # if len(pairings[i]) < 3 or len(pairings[i+1]) < 3: # return None, None # e0, e1 = seeded_rng.choice(pairings[i], 1), seeded_rng.choice(pairings[i+1], 1) # A[e0, e1], A[e1, e0] = 1, 1 # cross_module_edges.append((e0, e1)) def add_modular_edge(): randomComm = seeded_rng.choice(modules, p=module_dist) while len(pairings[randomComm]) < 2: randomComm = seeded_rng.choice(modules, p=module_dist) selection = seeded_rng.choice(pairings[randomComm], 2, replace=False, p=dists[randomComm]) while A[selection[0], selection[1]] != 0: randomComm = seeded_rng.choice(modules, p=module_dist) while len(pairings[randomComm]) < 2: randomComm = seeded_rng.choice(modules, p=module_dist) selection = seeded_rng.choice(pairings[randomComm], 2, replace=False, p=dists[randomComm]) A[selection[0], selection[1]] += 1 A[selection[1], selection[0]] += 1 def add_between_edge(): randomComm, randomComm2, e0, e1 = 0, 0, 0, 0 while randomComm == randomComm2 or A[e0, e1] != 0: randomComm, randomComm2 = seeded_rng.choice( modules, p=module_dist), seeded_rng.choice(modules, p=module_dist) e0 = seeded_rng.choice(pairings[randomComm], 1, replace=False, p=dists[randomComm]) e1 = seeded_rng.choice(pairings[randomComm2], 1, replace=False, p=dists[randomComm2]) A[e0, e1] += 1 A[e1, e0] += 1 cross_module_edges.append((e0, e1)) inModuleEdges = int(round(edges * p)) betweenEdges = edges - inModuleEdges # betweenEdges = edges - inModuleEdges - modules + 1 # if betweenEdges < 0: # print("NEGATIVE") for i in range(inModuleEdges): add_modular_edge() for i in range(betweenEdges): add_between_edge() def parameterized(cc_weight): B = deepcopy(A) for e in cross_module_edges: B[e[0], e[1]], B[e[1], e[0]] = cc_weight, cc_weight return B if getCommInfo: return A, parameterized, pairings, assignments else: return A, parameterized
def get_random_modular(n, modules, edges, p, getCommInfo=False): pairings = {} assignments = np.zeros(n, dtype=int) cross_module_edges = [] for i in range(modules): pairings[i] = [] A = np.zeros((n, n)) for i in range(n): randomModule = seeded_rng.randint(0, modules) pairings[randomModule].append(i) assignments[i] = randomModule # nodesPerMod = n // modules # for i in range(modules): # for j in range(nodesPerMod): # pairings[i].append(nodesPerMod * i + j) # assignments[nodesPerMod *i + j] = i # for i in range(modules - 1): # if len(pairings[i]) < 3 or len(pairings[i+1]) < 3: # return None, None # e0, e1 = seeded_rng.choice(pairings[i], 1), seeded_rng.choice(pairings[i+1], 1) # A[e0, e1], A[e1, e0] = 1, 1 # cross_module_edges.append((e0, e1)) def add_modular_edge(): randomComm = seeded_rng.randint(0, modules) while len(pairings[randomComm]) < 2: randomComm = seeded_rng.randint(0, modules) selection = seeded_rng.choice(pairings[randomComm], 2, replace=False) while A[selection[0], selection[1]] != 0: randomComm = seeded_rng.randint(0, modules) while len(pairings[randomComm]) < 2: randomComm = seeded_rng.randint(0, modules) selection = seeded_rng.choice(pairings[randomComm], 2, replace=False) A[selection[0], selection[1]] += 1 A[selection[1], selection[0]] += 1 def add_between_edge(): randEdge = seeded_rng.choice(n, 2, replace=False) while A[randEdge[0], randEdge[1]] != 0 or assignments[ randEdge[0]] == assignments[randEdge[1]]: randEdge = seeded_rng.choice(n, 2, replace=False) A[randEdge[0], randEdge[1]] += 1 A[randEdge[1], randEdge[0]] += 1 cross_module_edges.append(randEdge) inModuleEdges = int(round(edges * p)) betweenEdges = edges - inModuleEdges # betweenEdges = edges - inModuleEdges - modules + 1 # if betweenEdges < 0: # print("NEGATIVE") for i in range(inModuleEdges): add_modular_edge() for i in range(betweenEdges): add_between_edge() def parameterized(cc_weight): B = deepcopy(A) for e in cross_module_edges: B[e[0], e[1]], B[e[1], e[0]] = cc_weight, cc_weight return B if getCommInfo: return A, parameterized, pairings, assignments else: return A, parameterized