def test2(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) z = np.cos(x) c1 = Curve({x[i]: y[i] for i in range(len(x))}) c2 = Curve({x[i]: z[i] for i in range(len(y))}) curves = {"sine": c1, "cosine": c2} epsintersection1 = ei.get_eps_intersection(1.6, 6.2, c1, c2) epsintersection2 = ei.get_eps_intersection(0, 6.2, c1, c2) assert (epsintersection1 == 0.8250928589434148) assert (epsintersection2 == 0.49978680152075255)
def test2(): c1 = Curve({0: 0, 1: 2, 4: 5, 5: 1, 7: 10, 10: 7}) c2 = Curve({0: 0, 3: 2, 6: 4, 10: 5}) curves = {"1": c1, "2": c2} DAG1 = get_extremalDAG(curves) assert (DAG1 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('1', 'max'), ('1', 'min'), ('2', 'min'), ('2', 'max')], [(0, 5.0), (1, 2.0), (2, 2.0), (3, 5.0), (4, 1.5), (5, 2.5), (6, 2.5)], [((0, 1), 2.0), ((0, 2), 2.0), ((0, 3), 5.0), ((0, 4), 1.5), ((0, 6), 1.0), ((1, 2), 2.0), ((1, 3), 2.0), ((1, 4), 1.5), ((1, 6), 0.5), ((2, 3), 2.0), ((2, 4), 1.5), ((2, 6), 0), ((3, 4), 1.5), ((3, 6), 0), ((5, 1), 0), ((5, 2), 1.0), ((5, 3), 1.0), ((5, 4), 1.5), ((5, 6), 2.5)])) c1 = Curve({0: .5, 7: 9, 10: 6}) c2 = Curve({0: 0, 3: 2, 6: 4, 10: 5}) curves = {"1": c1, "2": c2} DAG2 = get_extremalDAG(curves) assert (DAG2 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('2', 'min'), ('2', 'max')], [(0, 4.25), (1, 4.25), (2, 1.5), (3, 2.5), (4, 2.5)], [((0, 1), 4.25), ((0, 2), 1.5), ((0, 4), 0), ((1, 2), 1.5), ((1, 4), 0), ((3, 1), 0), ((3, 2), 1.5), ((3, 4), 2.5)])) names = ['1', '2'] supergraph = get_supergraph(names, DAG1, DAG2) supergraph_round = (supergraph[0], supergraph[1], round(supergraph[2], 8)) assert (supergraph_round == ([(('1', 'min'), (5.0, 4.25)), (('1', 'max'), (2.0, 0)), (('1', 'min'), (2.0, 0)), (('1', 'max'), (5.0, 4.25)), (('1', 'min'), (1.5, 1.5)), (('2', 'min'), (2.5, 2.5)), (('2', 'max'), (2.5, 2.5)) ], [((0, 1), (2.0, 0)), ((0, 2), (2.0, 0)), ((0, 3), (5.0, 4.25)), ((0, 4), (1.5, 1.5)), ((0, 6), (1.0, 0)), ((1, 2), (2.0, 0)), ((1, 3), (2.0, 0)), ((1, 4), (1.5, 0)), ((1, 6), (0.5, 0)), ((2, 3), (2.0, 0)), ((2, 4), (1.5, 0)), ((3, 4), (1.5, 1.5)), ((5, 2), (1.0, 0)), ((5, 3), (1.0, 0)), ((5, 4), (1.5, 1.5)), ((5, 6), (2.5, 2.5))], 22.75))
def test1(): c1 = Curve({0: 1, 5: 10, 10: 3}) c2 = Curve({0: 8, 5: 3, 10: 1}) curves = {"1": c1, "2": c2} DAG1 = get_extremalDAG(curves) assert (DAG1 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('2', 'max'), ('2', 'min')], [(0, 4.5), (1, 4.5), (2, 3.5), (3, 3.5), (4, 3.5)], [((0, 1), 4.5), ((0, 2), 3.5), ((0, 4), 0), ((1, 2), 3.5), ((1, 4), 0), ((3, 1), 0), ((3, 2), 0), ((3, 4), 3.5)])) c1 = Curve({0: 1, 5: 7, 9: 1, 10: 2}) c2 = Curve({0: 10, 5: 2, 10: 0}) curves = {"1": c1, "2": c2} DAG2 = get_extremalDAG(curves) assert (DAG2 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('1', 'max'), ('2', 'max'), ('2', 'min')], [(0, 3.0), (1, 3.0), (2, 3.0), (3, 0.5), (4, 5.0), (5, 5.0)], [((0, 1), 3.0), ((0, 2), 3.0), ((0, 3), 0.5), ((0, 5), 0), ((1, 2), 3.0), ((1, 3), 0.5), ((1, 5), 0), ((2, 3), 0.5), ((2, 5), 0), ((4, 1), 0), ((4, 2), 0), ((4, 3), 0.5), ((4, 5), 5.0)])) names = ['1', '2'] supergraph = get_supergraph(names, DAG1, DAG2) supergraph_round = (supergraph[0], supergraph[1], round(supergraph[2], 8)) assert (supergraph_round == ([(('1', 'min'), (4.5, 3.0)), (('1', 'max'), (4.5, 3.0)), (('1', 'min'), (3.5, 3.0)), (('1', 'max'), (0, 0.5)), (('2', 'max'), (3.5, 5.0)), (('2', 'min'), (3.5, 5.0))], [((0, 1), (4.5, 3.0)), ((0, 2), (3.5, 3.0)), ((0, 3), (0, 0.5)), ((1, 2), (3.5, 3.0)), ((1, 3), (0, 0.5)), ((2, 3), (0, 0.5)), ((4, 3), (0, 0.5)), ((4, 5), (3.5, 5.0))], 13.0))
def test1(): c1 = Curve({0: 1, 5: 10, 10: 3}) c2 = Curve({0: 8, 5: 3, 10: 1}) epssup1 = ei.get_eps_sup(0, c1, 4.5) assert (epssup1 == (0, 5)) epssup2 = ei.get_eps_sup(0, c1, 4.6) assert (epssup2 == (0, 10)) checkintersection = ei.intervals_intersect(0, 10, c1, c2, 1) assert (checkintersection == True) epsjumps1 = ei.get_eps_jumps(0, c1.curve) assert (epsjumps1 == [0, 4.5]) epsjumps2 = ei.get_eps_jumps(0, c2.curve) assert (epsjumps2 == [0, 2.5, 3.5]) epsintersection = ei.get_eps_intersection(0, 10, c1, c2) assert (epsintersection == 0)
def test1(): c1 = Curve({0: 1, 5: 10, 10: 3}) c2 = Curve({0: 8, 5: 3, 10: 1}) nodesA = ds.get_nodes('A', c1.curve) nodesB = ds.get_nodes('B', c2.curve) assert (nodesA == [(0, ('A', 'min')), (5, ('A', 'max')), (10, ('A', 'min'))]) assert (nodesB == [(0, ('B', 'max')), (10, ('B', 'min'))]) curves = {'A': c1, 'B': c2} DAGskeleton = ds.get_DAGskeleton(curves) assert (DAGskeleton == ([(0, ('A', 'min')), (5, ('A', 'max')), (10, ('A', 'min')), (0, ('B', 'max')), (10, ('B', 'min'))], [(0, 1), (0, 2), (0, 4), (1, 2), (1, 4), (3, 1), (3, 2), (3, 4)]))
def calculate_poset(params, networks): ''' Construct partial orders of extrema from time series files :param params: A dictionary with the keys 'timeseriesfname' : a .csv or .tsv file name 'tsfile_is_row_format' : True if time series are in rows, False if they are in columns. "epsilons" : a list of noise values between 0.0 and 0.5 :param networks: a list of DSGRN network specifications :return: (1) A dictionary of partially ordered sets keyed by sets of node names in network files. Any input network that has a node name that is not in the time series file will not be analyzed. (2) The list of pruned networks. (3) The set of names that were missing from the time series files. ''' data, times = readrow(params['timeseriesfname'] ) if params['tsfile_is_row_format'] else readcol( params['timeseriesfname']) posets = {} new_networks = [] missing_names = set([]) for networkspec in networks: network = DSGRN.Network(networkspec) names = tuple(sorted([network.name(k) for k in range(network.size())])) missing_names = missing_names.union( set(name for name in names if name not in data)) if set(names).intersection(missing_names): continue if names not in posets.keys(): curves = [Curve(data[name], times, True) for name in names] pos = eps_posets(dict(zip(names, curves)), params["epsilons"]) if pos is None: raise ValueError("poset is None!") posets[names] = pos new_networks.append(networkspec) return posets, new_networks, missing_names
def getcurves(filename, filestyle, names, start_time, end_time): ''' :param filename: Name of the time series data file. Include absolute or relative path to file. :param filestyle: "row" if the time points lie in a single row, or "col" if they lie in a column :param names: list of gene names on which to create curve. If "all" (default), then all genes in the file are used :param start_time: front trim for time series in time units :param end_time: back trim for time series in time units :return: list of curve objects ''' if filestyle == "row": curves = row(os.path.expanduser(filename)) elif filestyle == "col": curves = col(os.path.expanduser(filename)) else: raise ValueError("Filestyle not recognized.") subset_curves = deepcopy(curves) for name in curves: if names != "all" and name not in names: subset_curves.pop(name) if start_time is not None and end_time is not None: subset_curves = { name: Curve(curve.trim(start_time, end_time)) for name, curve in subset_curves.items() } return subset_curves
def row(filename): times,data = extractdata(filename) times = [float(n) for n in times] names = data[:,0] curves = [Curve(data[k,1:],times,True) for k in range(data.shape[0])] # plt.plot(times,data[0,1:],marker="o") # plt.show() return dict(zip(names,curves))
def test4(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) c4 = Curve({x[i]: y[i] for i in range(len(x))}) nodelives = get_node_lives(c4) assert (nodelives == { 0.0: 0.49978680152075255, 4.7: 0.999748430302803, 1.6: 0.999748430302803, 6.2: 0.4584169273733022 })
def get_node_lives(curve): ''' Calculates the node lives for all the local extrema in curve. :param curve: curve object :return: dict of node lives for each local extrema in the curve ''' curve_max = Curve(curve.reflect()) min_lives = get_min_lives(curve.curve) max_lives = get_min_lives(curve_max.curve) node_lives = {**min_lives, **max_lives} return (node_lives)
def test2(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) z = np.cos(x) c1 = Curve({x[i]: y[i] for i in range(len(x))}) c2 = Curve({x[i]: z[i] for i in range(len(y))}) sinnodes = ds.get_nodes('sine', c1.curve) cosnodes = ds.get_nodes('cosine', c2.curve) assert (sinnodes == [(0.0, ('sine', 'min')), (1.6, ('sine', 'max')), (4.7, ('sine', 'min')), (6.2, ('sine', 'max'))]) assert (cosnodes == [(0.0, ('cosine', 'max')), (3.1, ('cosine', 'min')), (6.2, ('cosine', 'max'))]) curves = {'sine': c1, 'cosine': c2} DAGskeleton = ds.get_DAGskeleton(curves) assert (DAGskeleton == ([ (0.0, ('sine', 'min')), (1.6, ('sine', 'max')), (4.7, ('sine', 'min')), (6.2, ('sine', 'max')), (0.0, ('cosine', 'max')), (3.1, ('cosine', 'min')), (6.2, ('cosine', 'max')) ], [(0, 1), (0, 2), (0, 3), (0, 5), (0, 6), (1, 2), (1, 3), (1, 5), (1, 6), (2, 3), (2, 6), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 2), (5, 3), (5, 6)]))
def test4(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) c4 = Curve({x[i]: y[i] for i in range(len(x))}) nodelives = get_node_lives(c4) nodelives_round = {u: round(nodelives[u], 8) for u in nodelives.keys()} assert (nodelives_round == { 0.0: 0.4997868, 4.7: 0.99974843, 1.6: 0.99974843, 6.2: 0.45841693 })
def test3(): c1 = Curve({0: 1, 5: 10, 10: 3}) c2 = Curve({0: 8, 5: 3, 10: 1}) c3 = Curve({0: 2, 5: 9, 10: 4}) curves = {"1": c1, "2": c2, "3": c3} DAG1 = get_extremalDAG(curves) assert (DAG1 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('2', 'max'), ('2', 'min'), ('3', 'min'), ('3', 'max'), ('3', 'min')], [(0, 4.5), (1, 4.5), (2, 3.5), (3, 3.5), (4, 3.5), (5, 3.5), (6, 3.5), (7, 2.5)], [((0, 1), 4.5), ((0, 2), 3.5), ((0, 4), 0), ((0, 6), 0), ((0, 7), 0), ((1, 2), 3.5), ((1, 4), 0), ((1, 7), 0), ((3, 1), 0), ((3, 2), 0), ((3, 4), 3.5), ((3, 6), 0), ((3, 7), 0), ((5, 1), 0), ((5, 2), 0), ((5, 4), 0), ((5, 6), 3.5), ((5, 7), 2.5), ((6, 2), 0), ((6, 4), 0), ((6, 7), 2.5)])) c1 = Curve({0: 1, 5: 7, 9: 1, 10: 2}) c2 = Curve({0: 10, 5: 2, 10: 0}) c3 = Curve({0: 2.5, 5: 9, 10: 4}) curves = {"1": c1, "2": c2, "3": c3} DAG2 = get_extremalDAG(curves) assert (DAG2 == ([('1', 'min'), ('1', 'max'), ('1', 'min'), ('1', 'max'), ('2', 'max'), ('2', 'min'), ('3', 'min'), ('3', 'max'), ('3', 'min')], [(0, 3.0), (1, 3.0), (2, 3.0), (3, 0.5), (4, 5.0), (5, 5.0), (6, 3.25), (7, 3.25), (8, 2.5)], [((0, 1), 3.0), ((0, 2), 3.0), ((0, 3), 0.5), ((0, 5), 0), ((0, 7), 0), ((0, 8), 0), ((1, 2), 3.0), ((1, 3), 0.5), ((1, 5), 0), ((1, 8), 0), ((2, 3), 0.5), ((2, 5), 0), ((2, 8), 0), ((4, 1), 0), ((4, 2), 0), ((4, 3), 0.5), ((4, 5), 5.0), ((4, 7), 0), ((4, 8), 0), ((6, 1), 0), ((6, 2), 0), ((6, 3), 0.5), ((6, 5), 0), ((6, 7), 3.25), ((6, 8), 2.5), ((7, 2), 0), ((7, 3), 0), ((7, 5), 0), ((7, 8), 2.5)])) names = ['1', '2', '3'] supergraph = get_supergraph(names, DAG1, DAG2) assert (supergraph == ([ (('1', 'min'), (4.5, 3.0)), (('1', 'max'), (4.5, 3.0)), (('1', 'min'), (3.5, 3.0)), (('1', 'max'), (0, 0.5)), (('2', 'max'), (3.5, 5.0)), (('2', 'min'), (3.5, 5.0)), (('3', 'min'), (3.5, 3.25)), (('3', 'max'), (3.5, 3.25)), (('3', 'min'), (2.5, 2.5)) ], [((0, 1), (4.5, 3.0)), ((0, 2), (3.5, 3.0)), ((0, 3), (0, 0.5)), ((1, 2), (3.5, 3.0)), ((1, 3), (0, 0.5)), ((2, 3), (0, 0.5)), ((4, 3), (0, 0.5)), ((4, 5), (3.5, 5.0)), ((6, 3), (0, 0.5)), ((6, 7), (3.5, 3.25)), ((6, 8), (2.5, 2.5)), ((7, 8), (2.5, 2.5))], 3.992179855667828))
def test3(): curve6 = Curve({0: 2, 1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 2, 7: 2}) curve7 = Curve({0: 2, 1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 2}) curve8 = Curve({0: 2, 1: 2, 2: 2, 3: 3, 4: 2, 5: 2, 6: 2}) eps = 0.1 def part(curve): n = curve.normalize() r = curve.normalize_reflect() merge_tree_mins = tmt.births_only(n) merge_tree_maxs = tmt.births_only(r) # time_ints_mins = ss.minimal_time_ints(merge_tree_mins,n,eps) # time_ints_maxs = ss.minimal_time_ints(merge_tree_maxs,r,eps) time_ints_mins = ss.get_sublevel_sets(merge_tree_mins, n, eps) time_ints_maxs = ss.get_sublevel_sets(merge_tree_maxs, r, eps) labeled_mins = sorted([(v, ("name", "min")) for _, v in time_ints_mins.items()]) labeled_maxs = sorted([(v, ("name", "max")) for _, v in time_ints_maxs.items()]) # When eps is close to (b-a)/2 for max b and min a, then the intervals can be identical. Annihilate them. all_extrema = labeled_mins + labeled_maxs nodes = annihilate(sorted(all_extrema)) return all_extrema, nodes all_extrema, nodes = part(curve6) assert (len(all_extrema) == 2) assert (len(nodes) == 0) all_extrema, nodes = part(curve7) assert (len(all_extrema) == 2) assert (len(nodes) == 0) nodes, edges = get_total_order("name", curve6, eps) assert (len(nodes) == 0) nodes, edges = get_total_order("name", curve7, eps) assert (len(nodes) == 0) nodes, edges = get_total_order("name", curve8, eps) assert (len(nodes) == 3) posets = eps_posets({"curve6": curve6}, [eps]) assert (posets == [(eps, ([], set()))])
def get_eps_sup(a, curve, eps): ''' Computes epsilon support interval for a local extremum. :param curve: curve object :param eps: float threshold (noise level) For normalized curves, 0 < eps < 1. :param a: the x-coordinate of a point in curve :return: epsilon support interval if a is a local extremum, otherwise returns FALSE ''' ex_type_a = get_extremum_type(a, curve.curve) if ex_type_a == 'min': births_only_merge_tree = births_only(curve.curve) eps_sup_a = gssnr(births_only_merge_tree, curve.curve, eps, eps_restriction=False) elif ex_type_a == 'max': new_curve = Curve(curve.reflect()) births_only_merge_tree = births_only(new_curve.curve) eps_sup_a = gssnr(births_only_merge_tree, new_curve.curve, eps, eps_restriction=False) else: return False return eps_sup_a[a]
def test(): curve = Curve({0: -2, 1: 2, 2: 0, 3: 1, 4: -2, 5: 1, 6: -7}) assert (curve.curve == Curve(curve.reflect()).reflect()) assert (curve.normalize() == Curve(curve.normalize()).normalize()) assert (curve.normalize_reflect() == Curve(curve.normalize()).reflect()) assert (min([c for k, c in curve.normalize().items()]) == -0.5) assert (max([c for k, c in curve.normalize().items()]) == 0.5)
def test4(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) z = np.cos(x) c1 = Curve({x[i]: y[i] for i in range(len(x))}) c2 = Curve({x[i]: z[i] for i in range(len(y))}) curves = {"sine": c1, "cosine": c2} DAG1 = get_extremalDAG(curves) assert (DAG1 == ([('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max')], [(0, 0.49978680152075255), (1, 0.999748430302803), (2, 0.999748430302803), (3, 0.4584169273733022), (4, 0.9995675751366397), (5, 0.9995675751366397), (6, 0.9978386236482485) ], [((0, 1), 0.49978680152075255), ((0, 2), 0.49978680152075255), ((0, 3), 0.4584169273733022), ((0, 5), 0.4927248649942301), ((0, 6), 0.49978680152075255), ((1, 2), 0.999748430302803), ((1, 3), 0.4584169273733022), ((1, 5), 0.12693419543239254), ((1, 6), 0.8250928589434148), ((2, 3), 0.4584169273733022), ((2, 6), 0.11357938500405684), ((4, 1), 0.11757890635775581), ((4, 2), 0.8331380106399122), ((4, 3), 0.4584169273733022), ((4, 5), 0.9995675751366397), ((4, 6), 0.9978386236482485), ((5, 2), 0.12156038112808631), ((5, 3), 0.45653760300917207), ((5, 6), 0.9978386236482485)])) # Adding small noise to curves c1.curve[6.0] = 0 c2.curve[0.4] = 1 curves = {"sine": c1, "cosine": c2} DAG2 = get_extremalDAG(curves) assert (DAG2 == ([('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max')], [ (0, 0.49978680152075255), (1, 0.999748430302803), (2, 0.999748430302803), (3, 0.4999616287820504), (4, 0.09108125213604751), (5, 0.0495365507272993), (6, 0.9995675751366397), (7, 0.02233175543719701), (8, 0.02233175543719701), (9, 0.9995675751366397), (10, 0.9978386236482485) ], [((0, 1), 0.49978680152075255), ((0, 2), 0.49978680152075255), ((0, 3), 0.49978680152075255), ((0, 4), 0.09108125213604751), ((0, 5), 0.0495365507272993), ((0, 7), 0.012365044357817823), ((0, 8), 0.02233175543719701), ((0, 9), 0.4927248649942301), ((0, 10), 0.49978680152075255), ((1, 2), 0.999748430302803), ((1, 3), 0.4999616287820504), ((1, 4), 0.09108125213604751), ((1, 5), 0.0495365507272993), ((1, 9), 0.12693419543239254), ((1, 10), 0.8250928589434148), ((2, 3), 0.4999616287820504), ((2, 4), 0.09108125213604751), ((2, 5), 0.0495365507272993), ((2, 10), 0.11357938500405684), ((3, 4), 0.09108125213604751), ((3, 5), 0.0495365507272993), ((3, 10), 0), ((4, 5), 0.0495365507272993), ((4, 10), 0), ((6, 1), 0.11757890635775581), ((6, 2), 0.8331380106399122), ((6, 3), 0.4999616287820504), ((6, 4), 0.09108125213604751), ((6, 5), 0.0495365507272993), ((6, 7), 0.02233175543719701), ((6, 8), 0.02233175543719701), ((6, 9), 0.9995675751366397), ((6, 10), 0.9978386236482485), ((7, 1), 0.02233175543719701), ((7, 2), 0.02233175543719701), ((7, 3), 0.02233175543719701), ((7, 4), 0.02233175543719701), ((7, 5), 0.02233175543719701), ((7, 8), 0.02233175543719701), ((7, 9), 0.02233175543719701), ((7, 10), 0.02233175543719701), ((8, 1), 0.02233175543719701), ((8, 2), 0.02233175543719701), ((8, 3), 0.02233175543719701), ((8, 4), 0.02233175543719701), ((8, 5), 0.02233175543719701), ((8, 9), 0.02233175543719701), ((8, 10), 0.02233175543719701), ((9, 2), 0.12156038112808631), ((9, 3), 0.49337324340519445), ((9, 4), 0.09108125213604751), ((9, 5), 0.0495365507272993), ((9, 10), 0.9978386236482485)])) names = ['sine', 'cosine'] supergraph = get_supergraph(names, DAG1, DAG2) assert (supergraph == ([ (('sine', 'min'), (0.49978680152075255, 0.49978680152075255)), (('sine', 'max'), (0.999748430302803, 0.999748430302803)), (('sine', 'min'), (0.999748430302803, 0.999748430302803)), (('sine', 'max'), (0.4584169273733022, 0.4999616287820504)), (('sine', 'min'), (0, 0.09108125213604751)), (('sine', 'max'), (0, 0.0495365507272993)), (('cosine', 'max'), (0.9995675751366397, 0.9995675751366397)), (('cosine', 'min'), (0, 0.02233175543719701)), (('cosine', 'max'), (0, 0.02233175543719701)), (('cosine', 'min'), (0.9995675751366397, 0.9995675751366397)), (('cosine', 'max'), (0.9978386236482485, 0.9978386236482485)) ], [((0, 1), (0.49978680152075255, 0.49978680152075255)), ((0, 2), (0.49978680152075255, 0.49978680152075255)), ((0, 3), (0.4584169273733022, 0.49978680152075255)), ((0, 4), (0, 0.09108125213604751)), ((0, 5), (0, 0.0495365507272993)), ((0, 7), (0, 0.012365044357817823)), ((0, 8), (0, 0.02233175543719701)), ((0, 9), (0.4927248649942301, 0.4927248649942301)), ((0, 10), (0.49978680152075255, 0.49978680152075255)), ((1, 2), (0.999748430302803, 0.999748430302803)), ((1, 3), (0.4584169273733022, 0.4999616287820504)), ((1, 4), (0, 0.09108125213604751)), ((1, 5), (0, 0.0495365507272993)), ((1, 9), (0.12693419543239254, 0.12693419543239254)), ((1, 10), (0.8250928589434148, 0.8250928589434148)), ((2, 3), (0.4584169273733022, 0.4999616287820504)), ((2, 4), (0, 0.09108125213604751)), ((2, 5), (0, 0.0495365507272993)), ((2, 10), (0.11357938500405684, 0.11357938500405684)), ((3, 4), (0, 0.09108125213604751)), ((3, 5), (0, 0.0495365507272993)), ((4, 5), (0, 0.0495365507272993)), ((6, 1), (0.11757890635775581, 0.11757890635775581)), ((6, 2), (0.8331380106399122, 0.8331380106399122)), ((6, 3), (0.4584169273733022, 0.4999616287820504)), ((6, 4), (0, 0.09108125213604751)), ((6, 5), (0, 0.0495365507272993)), ((6, 7), (0, 0.02233175543719701)), ((6, 8), (0, 0.02233175543719701)), ((6, 9), (0.9995675751366397, 0.9995675751366397)), ((6, 10), (0.9978386236482485, 0.9978386236482485)), ((7, 1), (0, 0.02233175543719701)), ((7, 2), (0, 0.02233175543719701)), ((7, 3), (0, 0.02233175543719701)), ((7, 4), (0, 0.02233175543719701)), ((7, 5), (0, 0.02233175543719701)), ((7, 8), (0, 0.02233175543719701)), ((7, 9), (0, 0.02233175543719701)), ((7, 10), (0, 0.02233175543719701)), ((8, 1), (0, 0.02233175543719701)), ((8, 2), (0, 0.02233175543719701)), ((8, 3), (0, 0.02233175543719701)), ((8, 4), (0, 0.02233175543719701)), ((8, 5), (0, 0.02233175543719701)), ((8, 9), (0, 0.02233175543719701)), ((8, 10), (0, 0.02233175543719701)), ((9, 2), (0.12156038112808631, 0.12156038112808631)), ((9, 3), (0.45653760300917207, 0.49337324340519445)), ((9, 4), (0, 0.09108125213604751)), ((9, 5), (0, 0.0495365507272993)), ((9, 10), (0.9978386236482485, 0.9978386236482485))], 0.312731099172106))
0.00013, 0.00000, 0.00000, 0.00000, 0.00000, 0.47695, 0.96319, 0.99744, 0.99987, 1.00000, 1.00000, 1.00000, 1.00000, 1.00000, 0.99994, 0.99962, 0.99756, 0.98532, 0.91946, 0.67207, 0.28714, 0.07560, 0.01584, 0.00308, 0.00058, 0.00013, 0.00000, 0.00000, 0.00000], [0.00000, 0.00107, 0.35730, 0.71137, 0.87486, 0.94705, 0.97871, 0.99257, 0.99857, 1.00000, 0.99171, 0.93604, 0.72910, 0.41881, 0.20017, 0.08941, 0.03903, 0.01689, 0.00728, 0.00313, 0.00134, 0.00056, 0.00023, 0.00009, 0.00002, 0.00000, 0.00614, 0.41342, 0.73857, 0.88694, 0.95236, 0.98103, 0.99359, 0.99898, 0.99978, 0.98900, 0.92145, 0.69287, 0.38598, 0.18221, 0.08105, 0.03532, 0.01528, 0.00659, 0.00283, 0.00121, 0.00051, 0.00021, 0.00008, 0.00002]]) names = ["A", "B", "C", "D", "E", "F"] curves = [Curve(data[k, :]/sum(data[k, :]), times, True) for k in range(data.shape[0])] c = dict(zip(names, curves)) def test1(): eps = 0.05 solns = { "A" : [((0.0, 4.0), ('A', 'min')),((0.0, 8.0), ('A', 'max')), ((24.0, 104.0), ('A', 'min')),((100.0, 108.0), ('A', 'max')), ((120.0, 196.0), ('A', 'min'))], "B" : [((0.0, 4.0), ('B', 'min')), ((4.0, 32.0), ('B', 'max')), ((76.0, 104.0), ('B', 'min')), ((104.0, 132.0), ('B', 'max')), ((176.0, 196.0), ('B', 'min'))], "C" : [((0.0, 4.0), ('C', 'min')), ((4.0, 20.0), ('C','max')), ((28.0, 104.0), ('C', 'min')), ((104.0, 120.0), ('C', 'max')), ((128.0, 196.0), ('C', 'min'))], "D" : [((0.0, 4.0), ('D', 'min')), ((20.0, 36.0), ('D', 'max')), ((56.0, 104.0), ('D', 'min')), ((120.0, 136.0), ('D', 'max')), ((156.0, 196.0), ('D', 'min'))], "E" : [((0.0, 4.0), ('E', 'min')), ((4.0, 60.0), ('E', 'max')), ((64.0, 104.0), ('E', 'min')), ((104.0, 160.0), ('E','max')), ((164.0, 196.0), ('E', 'min'))], "F" : [((0.0, 8.0), ('F', 'min')), ((16.0, 48.0), ('F', 'max')), ((56.0, 108.0), ('F', 'min')), ((116.0, 148.0), ('F', 'max')), ((156.0, 196.0), ('F', 'min'))] } for k in ["A","B","C","D","E","F"]: assert(get_total_order(k,c[k],eps)[0] == solns[k]) eps = 0.51
def test(): # integer curve 1 curve = Curve({0:-2, 1:2, 2:0, 3:3, 4:-4, 5:1, 6:-7}) tmt = {0:(3,6),1:(1,0),2:(1,0),3:(3,6),4:(5,6),5:(5,6),6:(6,6)} assert(tmt == compute_merge_tree(curve.curve)) assert(tmt == compute_merge_tree(curve.normalize())) tMt = {0:(0,3),1:(2,3),2:(2,3),3:(3,3),4:(4,3),5:(4,3),6:(6,3)} assert(tMt == compute_merge_tree(curve.reflect())) assert(tMt == compute_merge_tree(curve.normalize_reflect())) # integer curve 2 curve2 = Curve({0:0, 1:-1, 2:-2, 3:1, 4:3, 5:6, 6:2}) tmt2 = {0:(0,2),1:(1,2),2:(2,2),3:(3,2),4:(4,2),5:(5,2),6:(5,2)} assert(tmt2 == compute_merge_tree(curve2.curve)) assert(tmt2 == compute_merge_tree(curve2.normalize())) tMt2 = {0:(2,5),1:(1,0),2:(2,5),3:(3,5),4:(4,5),5:(5,5),6:(6,5)} assert(tMt2 == compute_merge_tree(curve2.reflect())) assert(tMt2 == compute_merge_tree(curve2.normalize_reflect())) import numpy as np # discretized smooth curve # round entries to avoid round-off errors x = np.arange(-2.5, 5.01, 0.01) y = -0.25 * x ** 4 + 4.0/3 * x ** 3 + 0.5 * x ** 2 - 4.0 * x curve = Curve({round(t,2): round(v,10) for (t, v) in zip(x, y)}) tmt = {-2.5: (-2.5, -2.5), 5.0: (4.0, -2.5), 1.0: (-1.0, -2.5)} assert(tmt == births_only(curve.curve)) # discretized symmetric smooth curve # round entries to avoid round-off errors # identical depth minima are labeled by first occurrence x = np.arange(-0.75,0.76,0.01) y = 0.25*x**2*(x**2 - 0.5) curve = Curve({round(t,2): round(v,10) for (t, v) in zip(x, y)}) tmt = {-0.5 : (-0.5,-0.5), 0.5 : (0.0,-0.5)} assert(tmt == births_only(curve.curve)) tMt = {-0.75 : (-0.75,-0.75), 0.0 : (-0.5,-0.75), 0.75 : (0.5,-0.75)} print(births_only(curve.reflect())) assert (tMt == births_only(curve.reflect()))
def test2(): c2 = Curve({0: 8, 5: 3, 10: 1}) nodelives = get_node_lives(c2) assert (nodelives == {10: 3.5, 0: 3.5})
def test4(): x = np.arange(0, 2 * np.pi, 0.1) # start,stop,step y = np.sin(x) z = np.cos(x) c1 = Curve({x[i]: y[i] for i in range(len(x))}) c2 = Curve({x[i]: z[i] for i in range(len(y))}) curves = {"sine": c1, "cosine": c2} DAG1 = get_extremalDAG(curves) DAG1_nodes_round = [(DAG1[1][i][0], round(DAG1[1][i][1], 8)) for i in range(len(DAG1[1]))] DAG1_edges_round = [(DAG1[2][i][0], round(DAG1[2][i][1], 8)) for i in range(len(DAG1[2]))] DAG1_round = (DAG1[0], DAG1_nodes_round, DAG1_edges_round) assert (DAG1_round == ([('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max')], [(0, 0.4997868), (1, 0.99974843), (2, 0.99974843), (3, 0.45841693), (4, 0.99956758), (5, 0.99956758), (6, 0.99783862)], [((0, 1), 0.4997868), ((0, 2), 0.4997868), ((0, 3), 0.45841693), ((0, 5), 0.49272486), ((0, 6), 0.4997868), ((1, 2), 0.99974843), ((1, 3), 0.45841693), ((1, 5), 0.1269342), ((1, 6), 0.82509286), ((2, 3), 0.45841693), ((2, 6), 0.11357939), ((4, 1), 0.11757891), ((4, 2), 0.83313801), ((4, 3), 0.45841693), ((4, 5), 0.99956758), ((4, 6), 0.99783862), ((5, 2), 0.12156038), ((5, 3), 0.4565376), ((5, 6), 0.99783862)])) # Adding small noise to curves c1.curve[6.0] = 0 c2.curve[0.4] = 1 curves = {"sine": c1, "cosine": c2} DAG2 = get_extremalDAG(curves) DAG2_nodes_round = [(DAG2[1][i][0], round(DAG2[1][i][1], 8)) for i in range(len(DAG2[1]))] DAG2_edges_round = [(DAG2[2][i][0], round(DAG2[2][i][1], 8)) for i in range(len(DAG2[2]))] DAG2_round = (DAG2[0], DAG2_nodes_round, DAG2_edges_round) assert (DAG2_round == ([('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('sine', 'min'), ('sine', 'max'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max'), ('cosine', 'min'), ('cosine', 'max')], [(0, 0.4997868), (1, 0.99974843), (2, 0.99974843), (3, 0.49996163), (4, 0.09108125), (5, 0.04953655), (6, 0.99956758), (7, 0.02233176), (8, 0.02233176), (9, 0.99956758), (10, 0.99783862)], [((0, 1), 0.4997868), ((0, 2), 0.4997868), ((0, 3), 0.4997868), ((0, 4), 0.09108125), ((0, 5), 0.04953655), ((0, 7), 0.01236504), ((0, 8), 0.02233176), ((0, 9), 0.49272486), ((0, 10), 0.4997868), ((1, 2), 0.99974843), ((1, 3), 0.49996163), ((1, 4), 0.09108125), ((1, 5), 0.04953655), ((1, 9), 0.1269342), ((1, 10), 0.82509286), ((2, 3), 0.49996163), ((2, 4), 0.09108125), ((2, 5), 0.04953655), ((2, 10), 0.11357939), ((3, 4), 0.09108125), ((3, 5), 0.04953655), ((3, 10), 0), ((4, 5), 0.04953655), ((4, 10), 0), ((6, 1), 0.11757891), ((6, 2), 0.83313801), ((6, 3), 0.49996163), ((6, 4), 0.09108125), ((6, 5), 0.04953655), ((6, 7), 0.02233176), ((6, 8), 0.02233176), ((6, 9), 0.99956758), ((6, 10), 0.99783862), ((7, 1), 0.02233176), ((7, 2), 0.02233176), ((7, 3), 0.02233176), ((7, 4), 0.02233176), ((7, 5), 0.02233176), ((7, 8), 0.02233176), ((7, 9), 0.02233176), ((7, 10), 0.02233176), ((8, 1), 0.02233176), ((8, 2), 0.02233176), ((8, 3), 0.02233176), ((8, 4), 0.02233176), ((8, 5), 0.02233176), ((8, 9), 0.02233176), ((8, 10), 0.02233176), ((9, 2), 0.12156038), ((9, 3), 0.49337324), ((9, 4), 0.09108125), ((9, 5), 0.04953655), ((9, 10), 0.99783862)])) names = ['sine', 'cosine'] supergraph = get_supergraph(names, DAG1, DAG2) supergraph_round1 = [(supergraph[0][i][0], (round(supergraph[0][i][1][0], 8), round(supergraph[0][i][1][1], 8))) for i in range(len(supergraph[0]))] supergraph_round2 = [(supergraph[1][i][0], (round(supergraph[1][i][1][0], 8), round(supergraph[1][i][1][1], 8))) for i in range(len(supergraph[1]))] supergraph_round3 = round(supergraph[2], 8) supergraph_round = (supergraph_round1, supergraph_round2, supergraph_round3) assert (supergraph_round == ([ (('sine', 'min'), (0.4997868, 0.4997868)), (('sine', 'max'), (0.99974843, 0.99974843)), (('sine', 'min'), (0.99974843, 0.99974843)), (('sine', 'max'), (0.45841693, 0.49996163)), (('sine', 'min'), (0, 0.09108125)), (('sine', 'max'), (0, 0.04953655)), (('cosine', 'max'), (0.99956758, 0.99956758)), (('cosine', 'min'), (0, 0.02233176)), (('cosine', 'max'), (0, 0.02233176)), (('cosine', 'min'), (0.99956758, 0.99956758)), (('cosine', 'max'), (0.99783862, 0.99783862)) ], [((0, 1), (0.4997868, 0.4997868)), ((0, 2), (0.4997868, 0.4997868)), ((0, 3), (0.45841693, 0.4997868)), ((0, 4), (0, 0.09108125)), ((0, 5), (0, 0.04953655)), ((0, 7), (0, 0.01236504)), ((0, 8), (0, 0.02233176)), ((0, 9), (0.49272486, 0.49272486)), ((0, 10), (0.4997868, 0.4997868)), ((1, 2), (0.99974843, 0.99974843)), ((1, 3), (0.45841693, 0.49996163)), ((1, 4), (0, 0.09108125)), ((1, 5), (0, 0.04953655)), ((1, 9), (0.1269342, 0.1269342)), ((1, 10), (0.82509286, 0.82509286)), ((2, 3), (0.45841693, 0.49996163)), ((2, 4), (0, 0.09108125)), ((2, 5), (0, 0.04953655)), ((2, 10), (0.11357939, 0.11357939)), ((3, 4), (0, 0.09108125)), ((3, 5), (0, 0.04953655)), ((4, 5), (0, 0.04953655)), ((6, 1), (0.11757891, 0.11757891)), ((6, 2), (0.83313801, 0.83313801)), ((6, 3), (0.45841693, 0.49996163)), ((6, 4), (0, 0.09108125)), ((6, 5), (0, 0.04953655)), ((6, 7), (0, 0.02233176)), ((6, 8), (0, 0.02233176)), ((6, 9), (0.99956758, 0.99956758)), ((6, 10), (0.99783862, 0.99783862)), ((7, 1), (0, 0.02233176)), ((7, 2), (0, 0.02233176)), ((7, 3), (0, 0.02233176)), ((7, 4), (0, 0.02233176)), ((7, 5), (0, 0.02233176)), ((7, 8), (0, 0.02233176)), ((7, 9), (0, 0.02233176)), ((7, 10), (0, 0.02233176)), ((8, 1), (0, 0.02233176)), ((8, 2), (0, 0.02233176)), ((8, 3), (0, 0.02233176)), ((8, 4), (0, 0.02233176)), ((8, 5), (0, 0.02233176)), ((8, 9), (0, 0.02233176)), ((8, 10), (0, 0.02233176)), ((9, 2), (0.12156038, 0.12156038)), ((9, 3), (0.4565376, 0.49337324)), ((9, 4), (0, 0.09108125)), ((9, 5), (0, 0.04953655)), ((9, 10), (0.99783862, 0.99783862))], 1.73724564))
def test3(): c3 = Curve({0: 0, 1: 2, 4: 5, 5: 1, 7: 10, 10: 7}) nodelives = get_node_lives(c3) assert (nodelives == {0: 5.0, 5: 2.0, 10: 1.5, 4: 2.0, 7: 5.0})
def test1(): c1 = Curve({0: 1, 5: 10, 10: 3}) nodelives = get_node_lives(c1) assert (nodelives == {0: 4.5, 10: 3.5, 5: 4.5})
0.99994, 0.99962, 0.99756, 0.98532, 0.91946, 0.67207, 0.28714, 0.07560, 0.01584, 0.00308, 0.00058, 0.00013, 0.00000, 0.00000, 0.00000 ], [ 0.00000, 0.00107, 0.35730, 0.71137, 0.87486, 0.94705, 0.97871, 0.99257, 0.99857, 1.00000, 0.99171, 0.93604, 0.72910, 0.41881, 0.20017, 0.08941, 0.03903, 0.01689, 0.00728, 0.00313, 0.00134, 0.00056, 0.00023, 0.00009, 0.00002, 0.00000, 0.00614, 0.41342, 0.73857, 0.88694, 0.95236, 0.98103, 0.99359, 0.99898, 0.99978, 0.98900, 0.92145, 0.69287, 0.38598, 0.18221, 0.08105, 0.03532, 0.01528, 0.00659, 0.00283, 0.00121, 0.00051, 0.00021, 0.00008, 0.00002 ]]) names = ["A", "B", "C", "D", "E", "F"] curves = [ Curve(data[k, :] / sum(data[k, :]), times, True) for k in range(data.shape[0]) ] c = dict(zip(names, curves)) def test1(): eps = 0.05 solns = { "A": [((0.0, 4.0), ('A', 'min')), ((0.0, 8.0), ('A', 'max')), ((24.0, 104.0), ('A', 'min')), ((100.0, 108.0), ('A', 'max')), ((120.0, 196.0), ('A', 'min'))], "B": [((0.0, 4.0), ('B', 'min')), ((4.0, 32.0), ('B', 'max')), ((76.0, 104.0), ('B', 'min')), ((104.0, 132.0), ('B', 'max')), ((176.0, 196.0), ('B', 'min'))],
def col(filename): names,data = extractdata(filename) times = data[:,0] curves = [Curve(data[1:,k],times,True) for k in range(data.shape[1])] return dict(zip(names,curves))
def test(): # integer curve 1 curve = Curve({0: -2, 1: 2, 2: 0, 3: 3, 4: -4, 5: 1, 6: -7}) births_only_merge_tree = tmt.births_only(curve.curve) ti = get_sublevel_sets(births_only_merge_tree, curve.curve, 0.75) assert (ti == {0: (0, 1), 2: (1, 3), 4: (3, 5), 6: (5, 6)}) ti = get_sublevel_sets(births_only_merge_tree, curve.curve, 2) assert (ti == {0: (0, 1), 4: (3, 5), 6: (5, 6)}) ti = get_sublevel_sets(births_only_merge_tree, curve.curve, 3) assert (ti == {6: (5, 6)}) # integer curve 2 curve2 = Curve({0: 0, 1: -1, 2: -2, 3: 1, 4: 3, 5: 6, 6: 2}) births_only_merge_tree = tmt.births_only(curve2.curve) time_ints = get_sublevel_sets(births_only_merge_tree, curve2.curve, 0.75) assert (time_ints == {2: (0, 3), 6: (5, 6)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve2.curve, 1) assert (time_ints == {2: (0, 3), 6: (5, 6)}) ti = get_sublevel_sets(births_only_merge_tree, curve2.curve, 3) assert (ti == {2: (0, 5)}) # curve 3 with 3 equal points curve3 = Curve({ 0: 2, 1: 0, 2: 0, 3: 0, 4: 2, 5: 3, 6: 3, 7: 3, 8: 1.5, 9: 0 }) births_only_merge_tree = tmt.births_only(curve3.curve) time_ints = get_sublevel_sets(births_only_merge_tree, curve3.curve, 0.5) assert (time_ints == {1: (0, 4), 9: (8, 9)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve3.curve, 1.1) assert (time_ints == {1: (0, 5), 9: (7, 9)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve3.curve, 1.5) assert (time_ints == {1: (0, 5), 9: (7, 9)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve3.curve, 1.6) assert (time_ints == {1: (0, 9)}) # find maxima curve4 = Curve(curve3.reflect()) births_only_merge_tree = tmt.births_only(curve4.curve) time_ints = get_sublevel_sets(births_only_merge_tree, curve4.curve, 0.5) assert (time_ints == {0: (0, 1), 5: (4, 8)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve4.curve, 1.1) assert (time_ints == {5: (3, 9)}) # curve 3 with 2 equal points curve5 = Curve({0: 2, 1: 0, 2: 0, 3: 2, 4: 3, 5: 3, 6: 1.5, 7: 0}) births_only_merge_tree = tmt.births_only(curve5.curve) time_ints = get_sublevel_sets(births_only_merge_tree, curve5.curve, 0.5) assert (time_ints == {1: (0, 3), 7: (6, 7)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve5.curve, 1.1) assert (time_ints == {1: (0, 4), 7: (5, 7)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve5.curve, 1.5) assert (time_ints == {1: (0, 4), 7: (5, 7)}) time_ints = get_sublevel_sets(births_only_merge_tree, curve5.curve, 1.6) assert (time_ints == {1: (0, 7)}) # constant curve curve6 = Curve({0: 2, 1: 2, 2: 2, 3: 2, 4: 2, 5: 2, 6: 2, 7: 2}) births_only_merge_tree = tmt.births_only(curve6.curve) time_ints = get_sublevel_sets(births_only_merge_tree, curve6.curve, 0.5) assert (time_ints == {0: (0, 7)}) print(time_ints)
def test(): curve = Curve({0: -2, 1: 2, 2: 0, 3: 1, 4: -2, 5: 1, 6: -7}) assert (curve.curve == Curve(curve.reflect()).reflect()) assert (curve.normalize() == Curve(curve.normalize()).normalize()) assert (curve.normalize_reflect() == Curve(curve.normalize()).reflect()) assert (min([c for k, c in curve.normalize().items()]) == -0.5) assert (max([c for k, c in curve.normalize().items()]) == 0.5) curve = Curve({7: -2, 8: 2, 9: 0, 10: 1, 11: -2, 12: 1, 13: -7}) tcurve = curve.trim(8.5, 11) assert (min(tcurve.keys()) == 9) assert (max(tcurve.keys()) == 11)