def add_variants(vids_to_add, variants, mutrel_posterior, mutrel_evidence, logprior, pbar, parallel): for vid in vids_to_add: assert vid in variants new_vids = mutrel_posterior.vids + vids_to_add # M: number of variants we have now # A: number of variants we added M = len(new_vids) A = len(vids_to_add) assert len(mutrel_posterior.vids) == len(mutrel_evidence.vids) == M - A variants = [ common.convert_variant_dict_to_tuple(variants[V]) for V in new_vids ] new_posterior = mutrel.init_mutrel(new_vids) new_evidence = mutrel.init_mutrel(new_vids) new_posterior.rels[:-A, :-A] = mutrel_posterior.rels new_evidence.rels[:-A, :-A] = mutrel_evidence.rels pairs = [(I, J) for I in range(M) for J in range(M - A, M)] return _compute_pairs( pairs, variants, logprior, new_posterior, new_evidence, pbar, parallel, )
def calc_posterior(variants, logprior, rel_type, parallel=1): M = len(variants) # Allow Numba use by converting to namedtuple. vids = common.extract_vids(variants) variants = [ common.convert_variant_dict_to_tuple(variants[V]) for V in vids ] posterior = mutrel.init_mutrel(vids) evidence = mutrel.init_mutrel(vids) pairs = list(itertools.combinations(range(M), 2)) + [(V, V) for V in range(M)] _compute = lambda pbar: _compute_pairs( pairs, variants, logprior, posterior, evidence, pbar, parallel, ) if parallel > 0: with tqdm.tqdm(total=len(pairs), desc='Computing %s relations' % rel_type, unit='pair', dynamic_ncols=True) as pbar: posterior, evidence = _compute(pbar) else: posterior, evidence = _compute(None) return (posterior, evidence)
def merge_variants(to_merge, evidence, logprior): assert np.all( np.array([V for group in to_merge for V in group]) < len(evidence.vids)) already_merged = set() for vidxs in to_merge: vidxs = set(vidxs) assert len(vidxs & already_merged) == 0 M_old = len(evidence.vids) merged_vid = ','.join([evidence.vids[V] for V in vidxs]) new_vids = evidence.vids + [merged_vid] new_evidence = mutrel.init_mutrel(new_vids) new_evidence.rels[:-1, :-1] = evidence.rels merged_row = np.sum(np.array([evidence.rels[V] for V in vidxs]), axis=0) assert merged_row.shape == (M_old, NUM_MODELS) merged_col = np.copy(merged_row) merged_col[:, Models.A_B] = merged_row[:, Models.B_A] merged_col[:, Models.B_A] = merged_row[:, Models.A_B] new_evidence.rels[-1, :-1] = merged_row new_evidence.rels[:-1, -1] = merged_col new_evidence.rels[-1, -1, :] = -np.inf new_evidence.rels[-1, -1, Models.cocluster] = 0 already_merged |= vidxs evidence = new_evidence evidence = mutrel.remove_variants_by_vidx(evidence, already_merged) posterior = make_full_posterior(evidence, logprior) return (posterior, evidence)
def add_garbage(posterior, garb_svids): if len(garb_svids) == 0: return posterior assert len(set(posterior.vids) & set(garb_svids)) == 0 new_vids = posterior.vids + garb_svids new_posterior = mutrel.init_mutrel(new_vids) G = len(garb_svids) M = len(new_posterior.vids) # Rather than carefully slicing and dicing the array to set it, just use a # series of carefully ordered overwrite operations to put it in the correct # state. new_posterior.rels[:] = 0 new_posterior.rels[:, :, Models.garbage] = 1 diag = range(M) new_posterior.rels[diag, diag, :] = 0 new_posterior.rels[diag, diag, Models.cocluster] = 1 new_posterior.rels[:-G, :-G, :] = posterior.rels mutrel.check_posterior_sanity(new_posterior.rels) return new_posterior