def build_VF_propagation_backward(LT, t_b=0, t_e=200, neighb_size=20, dist_max=200, nb_proc = 8, Ws = [4, 1, 0]): ''' Main function that performs the backward propagation. The SVF is saved in LT.VF Args: LT: lineageTree t_b: int, first time point to process (note that t_e < t_b since we are propagating backward) t_e: int, last time point to process neighb_size: int, number of neighbors to look at to build the neighborhood density dist_max: float, distance max from which a vector is considered. Notice that the value used is so big that in that case it does not discard any cell. This parameter is historical nb_proc: int, number of processes to run in parallel: 1: no parallelisation negative number: as many as possible (default) 1 < *nb*: *nb* parallel processes. Ws: [int, int, int], list of weights for the neighborhood ''' if (not hasattr(LT, 'VF')) or LT.VF == None: LT.VF = lineageTree(None, None, None) starting_cells = LT.time_nodes[t_b] unique_id = 0 LT.VF.time_nodes = {t_b: []} for C in starting_cells: i = LT.VF.get_next_id() LT.VF.nodes.append(i) LT.VF.time_nodes[t_b].append(i) LT.VF.roots.append(i) LT.VF.pos[i]=LT.pos[C] gg_line = '\r%03d: GG done (%.2f s); ' prop_line = 'P done (%.2f s); ' add_line = 'Add done (%.2f s); ' fusion_line = 'F done (%.2f s); ' nb_cells_line = '#C: %06d' full_length = len(gg_line) + 3 + 5 +\ len(prop_line) + 5 +\ len(add_line) + 5 + \ len(fusion_line) + 5 + \ len(nb_cells_line) + 6 for t in range(t_b, t_e, -1): tic = time() to_check_VF = LT.VF.time_nodes[t] idx3d, to_check_LT = LT.get_idx3d(t) LT.VF.time_nodes[t-1] = [] Gabriel_graph = LT.get_gabriel_graph(t) GG_pred = LT.get_gabriel_graph(t-1) if t_e < t else {} GG_succ = LT.get_gabriel_graph(t+1) if t < t_b else {} gg_time = time() - tic tagged = set() cell_mapping_LT_VF = {} mapping = [] for C in to_check_VF: C_LT = to_check_LT[idx3d.query(LT.VF.pos[C])[1]] cell_mapping_LT_VF.setdefault(C_LT, []).append(C) if not C_LT in tagged: C_LT_pred = set(LT.predecessor.get(C_LT, [])) C_LT_pred_2 = set([ci for pred in LT.predecessor.get(C_LT, []) for ci in GG_pred.get(pred, set())]) C_LT_succ = set(LT.successor.get(C_LT, [])) C_LT_succ_2 = set([ci for succ in LT.successor.get(C_LT, []) for ci in GG_succ.get(succ, set())]) N = list(Gabriel_graph.get(C_LT, set())) weight_mat = np.ones_like(N) N_pred = {n: LT.predecessor.get(n, []) for n in N} N_succ = {n: LT.successor.get(n, []) for n in N} for i, n in enumerate(N): for n_predii in N_pred[n]: if C_LT_pred.intersection(GG_pred.get(n_predii, set())): W = Ws[0] elif C_LT_pred_2.intersection(GG_pred.get(n_predii, set())): W = Ws[1] else: W = Ws[2] for n_succii in N_succ[n]: if C_LT_succ.intersection(GG_succ.get(n_succii, set())): W += Ws[0] elif C_LT_succ_2.intersection(GG_succ.get(n_succii, set())): W += Ws[1] else: W += Ws[2] weight_mat[i] = W if (weight_mat == 0).all(): weight_mat[:] = 1 mapping += [(C_LT, np.array(list(N)), dist_max, weight_mat)] tagged.add(C_LT) out = [] if nb_proc == 1: for params in mapping: out += [single_cell_propagation(params)] else: if nb_proc < 1: pool = Pool() else: pool = Pool(processes=nb_proc) out = pool.map(single_cell_propagation, mapping) pool.terminate() pool.close() for C_LT, med in out: for C_VF in cell_mapping_LT_VF[C_LT]: LT.VF.add_node(t-1, C_VF, LT.VF.pos[C_VF] + med, reverse = True) idx3d, to_check_LT = LT.get_idx3d(t-1) to_check_VF = LT.VF.time_nodes[t-1] sys.stdout.write('\b'*(full_length) + ' '*(full_length)) sys.stdout.flush() sys.stdout.write(gg_line%(t, gg_time)) sys.stdout.flush() sys.stdout.write(prop_line%(time() - tic)) sys.stdout.flush() if not LT.spatial_density.has_key(to_check_LT[0]): LT.compute_spatial_density(t-1, t-1, neighb_size) idx3d, to_check_VF = LT.VF.get_idx3d(t-1)[:2] dist_to_VF, equivalence = idx3d.query([LT.pos[c] for c in to_check_LT], 1) tmp = np.array([dist_to_VF[i]/LT.spatial_density[c] for i, c in enumerate(to_check_LT)]) to_add = [to_check_LT[i] for i in np.where(tmp>1.25)[0]] for C in to_add: LT.VF.add_node(t-1, None, LT.pos[C]) sys.stdout.write(add_line%(time() - tic)) sys.stdout.flush() sys.stdout.write(nb_cells_line%i) sys.stdout.flush() LT.VF.t_b = t_e LT.VF.t_e = t_b return LT.VF
Yb_f = interpolate.InterpolatedUnivariateSpline(times, Yb, k=1) Zb_f = interpolate.InterpolatedUnivariateSpline(times, Zb, k=1) Ti = np.arange(tb - 1, te + 2) barycenters_interp = dict(zip(Ti, zip(Xb_f(Ti), Yb_f(Ti), Zb_f(Ti)))) return barycenters_interp, barycenters if __name__ == '__main__': (path_LT, path_VF, path_mask, t, path_out_am, labels, DS, path_DB, path_div, path_bary, label_names, ani, invert) = read_param_file() if not os.path.exists(path_out_am): os.makedirs(path_out_am) if not os.path.exists('.mask_images/'): os.makedirs('.mask_images/') VF = lineageTree(path_VF) tb = VF.t_b te = VF.t_e if path_bary is not None: try: barycenters, b_dict = get_barycenter(path_bary, tb, te) except Exception as e: print "Wrong file path to barycenter, please specify the path to the .csv file." print "The process will continue as if no barycenter were provided," print "disabling the computation of the spherical coordinates" print "error raised: ", e path_bary = None im = imread(path_mask) for l in labels:
if not os.path.exists(path_out): os.makedirs(path_out) if not os.path.exists(path_out + 'Amira_SVF/'): os.makedirs(path_out + 'Amira_SVF/') if not os.path.exists(path_out + 'Amira_TGMM/'): os.makedirs(path_out + 'Amira_TGMM/') if p_TGMM is None or not os.path.exists(p_TGMM): files = [f for f in os.listdir(path_to_xml) if '.xml' in f] pos_time = len(os.path.commonprefix(files)) times = [int(file.split('.')[0][pos_time:]) for file in files] if tb < 0: tb = min(times) + 10 if te < 0: te = max(times) - 10 LT_main = lineageTree(file_format = path_to_xml + '/GMEMfinalResult_frame%04d.xml', tb = tb, te = te, z_mult = anisotropy) LT_main.to_binary(path_out + 'TGMM.bin') else: LT_main = lineageTree(file_format = p_TGMM) tb = LT_main.t_b te = LT_main.t_e LT = LT_main if not os.path.exists(path_out + 'GG.bin'): LT = LT_main tic = time() parallel_gabriel_graph_preprocess(LT_main) GG_to_bin(LT_main.Gabriel_graph, path_out + 'GG.bin') print 'Gabriel graph pre-processing:', time() - tic else: LT_main.Gabriel_graph = GG_from_bin(path_out + 'GG.bin')