def test_combinations_of_deps(): graph = quoted_data( 'reactant(a,1;c,2;b,3;d,3). reaction(1;2;3). product(b,1;d,2;e,3).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = { frozenset('ac'), frozenset('ad'), frozenset('bc'), frozenset('bd') } assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e')) assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e', compute_optimal_solutions=True)) assert {frozenset('abcd')} == set( search_seeds(graph, forbidden_seeds='e', targets='e', enum_mode='union')) assert {frozenset()} == set( search_seeds(graph, forbidden_seeds='e', targets='e', enum_mode='intersection'))
def test_forbidden_intermediate_nodes_parallelized(): graph = quoted_data('reactant((a;b;c),1). product(d,1). reaction(1).' 'reactant(d,2). product(e,2). reaction(2).' 'reactant((m;n;o),3). product(e,3). reaction(3).') utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('d'), frozenset('abc'), frozenset('mno')} assert seeds_sets == set(search_seeds(graph, targets='e', explore_pareto=True, prerun=True)) seeds_sets = {frozenset('mno'), frozenset('abc'), frozenset('e')} assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', prerun=False))
def test_simple_reaction_chain(): "A -> C -> T: C cannot be chosen, because it does not maximize the scope as much as A" graph = quoted_data('reactant(a,1). product(c,1). reaction(1). reactant(c,2). product(t,2). reaction(2).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('a')} assert seeds_sets == set(search_seeds(graph, targets='t', forbidden_seeds='t', explore_pareto=True, greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='t', explore_pareto=True, greedy=True)) seeds_sets = {frozenset('a')} assert seeds_sets == set(search_seeds(graph, targets='t', forbidden_seeds='t', explore_pareto=True, greedy=False, verbose=True)) assert seeds_sets == set(search_seeds(graph, targets='t', explore_pareto=True, greedy=False))
def test_simple_reaction_chain_with_double_root(): "Same as previous, but with A+B -> C, allowing pareto front to choose either A+B or C." graph = quoted_data('reactant((a;b),1). product(c,1). reaction(1). reactant(c,2). product(t,2). reaction(2).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('c'), frozenset('ab')} assert seeds_sets == set(search_seeds(graph, targets='t', forbidden_seeds='t', explore_pareto=True, greedy=True)) seeds_sets = {frozenset('c'), frozenset('ab')} assert seeds_sets == set(search_seeds(graph, targets='t', forbidden_seeds='t', explore_pareto=True, greedy=False)) seeds_sets = {frozenset('c')} assert seeds_sets == set(search_seeds(graph, targets='t', forbidden_seeds='t', explore_pareto=True, greedy=False, verbose=True, compute_optimal_solutions=True))
def test_non_optimal_local(): graph = quoted_data( 'reactant((a;b),1). product(c,1). reaction(1). reactant((c;d),2). product(e,2). reaction(2). reactant(e,3). product(c,3). reaction(3).' ) # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('abd'), frozenset('cd')} assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e')) expected_seeds_sets = {frozenset('cd')} assert expected_seeds_sets == set( search_seeds(graph, start_seeds='d', forbidden_seeds='e', targets='e', compute_optimal_solutions=True)) expected_seeds_sets = {frozenset('abd')} assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='ce', targets='e')) expected_seeds_sets = {frozenset('abd'), frozenset('adc')} assert expected_seeds_sets == set( search_seeds(graph, start_seeds='a', forbidden_seeds='e', targets='e')) expected_seeds_sets = {frozenset('abcd')} assert expected_seeds_sets == set( search_seeds(graph, start_seeds='a', forbidden_seeds='e', targets='e', enum_mode='union')) expected_seeds_sets = {frozenset('da')} assert expected_seeds_sets == set( search_seeds(graph, start_seeds='a', forbidden_seeds='e', targets='e', enum_mode='intersection')) expected_seeds_sets = {frozenset('d')} assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e', enum_mode='intersection', compute_optimal_solutions=False)) expected_seeds_sets = {frozenset('dc')} assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e', enum_mode='intersection', compute_optimal_solutions=True)) assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='e', targets='e', enum_mode='intersection', compute_optimal_solutions=True, greedy=True))
def test_simple_division(): graph = quoted_data('reactant(a,1). product(d,1). reaction(1).' 'reactant(c,2). product(e,2). reaction(2).' 'reactant(b,3). product((d;e),3). reaction(3).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('ac'), frozenset('b')} assert expected_seeds_sets == set( search_seeds(graph, targets='de', forbidden_seeds='de')) expected_seeds_sets = {frozenset('b')} assert expected_seeds_sets == set( search_seeds(graph, targets='de', forbidden_seeds='de', compute_optimal_solutions=True))
def test_impossible_case(): "the code should return nothing when no solution is found, or raise ValueError when dubious question is asked" graph = quoted_data( 'reactant((a;b),1). product((d;e),1). reaction(1). reactant((b;c),2). product((e;f),2). reaction(2).' ) # utils.render_network(graph, 'todel.png') # uncomment to help debugging assert not tuple(search_seeds( graph, forbidden_seeds='a')) # impossible to activate a without a assert not tuple( search_seeds(graph, forbidden_seeds={'a', 'd'}, targets={'d'})) with pytest.raises(ValueError): list( search_seeds(graph, start_seeds='a', forbidden_seeds='a', targets='d'))
def test_long_reaction_chain(): "L -> O -> N -> G -> E -> R: only L can be chosen, because it maximizes the scope" graph = quoted_data('reactant(l,1). product(o,1). reaction(1).' 'reactant(o,2). product(n,2). reaction(2).' 'reactant(n,3). product(g,3). reaction(3).' 'reactant(g,4). product(e,4). reaction(4).' 'reactant(e,5). product(r,5). reaction(5).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('l')} assert seeds_sets == set(search_seeds(graph, prerun=False)) assert seeds_sets == set(search_seeds(graph, targets='r', explore_pareto=True, greedy=True, prerun=False)) assert seeds_sets == set(search_seeds(graph, targets='r', explore_pareto=True, greedy=False, prerun=False)) # Proof that the forbidden_seeds support has hard-to-manage side-effects: seeds_sets = {frozenset('o')} assert seeds_sets == set(search_seeds(graph, targets='r', forbidden_seeds='l', explore_pareto=True, greedy=True, prerun=False)) # If prerun is forgot, this one will fail, because no model is generated: assert seeds_sets == set(search_seeds(graph, targets='r', forbidden_seeds='l', explore_pareto=True, greedy=False, prerun=True))
def test_infinite_loop_of_hypothesis(): graph = quoted_data('reactant((a;c),1). product(b,1). reaction(1).' 'reactant((b;d),2). product(c,2). reaction(2).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('bd')} assert expected_seeds_sets == set( search_seeds(graph, targets='c', explore_pareto=True, forbidden_seeds='c')) expected_seeds_sets = {frozenset('bd'), frozenset('c')} assert expected_seeds_sets == set( search_seeds(graph, targets='c', explore_pareto=True)) expected_seeds_sets = {frozenset('bd'), frozenset('c')} assert expected_seeds_sets == set( search_seeds(graph, targets='c', explore_pareto=True))
def test_hierarchical_case(): graph = quoted_data( 'reactant((a;b),1). product(c,1). reaction(1). reactant(a,2). product(e,2). reaction(2). reactant((d;e),3). product(b,3). reaction(3).' ) # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('ab'), frozenset('ad')} assert expected_seeds_sets == set( search_seeds(graph, forbidden_seeds='c', targets='c')) expected_seeds_sets = {frozenset('c')} assert expected_seeds_sets == set( search_seeds(graph, targets='c', compute_optimal_solutions=True)) expected_seeds_sets = { frozenset('ab'), frozenset('ad'), frozenset('bc'), frozenset('cde') } assert expected_seeds_sets == set(search_seeds(graph, targets='bc')) expected_seeds_sets = {frozenset('abcde')} assert expected_seeds_sets == set( search_seeds(graph, targets='bc', enum_mode='union')) expected_seeds_sets = {frozenset('ab'), frozenset('ad'), frozenset('bc')} assert expected_seeds_sets == set( search_seeds(graph, targets='bc', compute_optimal_solutions=True)) expected_seeds_sets = {frozenset('abcde')} assert expected_seeds_sets == set( search_seeds(graph, targets='bc', compute_optimal_solutions=True, enum_mode='union')) assert expected_seeds_sets == set( search_seeds(graph, targets='bc', compute_optimal_solutions=False, enum_mode='union')) expected_seeds_sets = {frozenset()} assert expected_seeds_sets == set( search_seeds(graph, targets='bc', compute_optimal_solutions=True, enum_mode='intersection')) assert expected_seeds_sets == set( search_seeds(graph, targets='bc', compute_optimal_solutions=False, enum_mode='intersection'))
def test_double_yielder(): graph = quoted_data('reactant(a,1). product(c,1). reaction(1).' 'reactant(a,2). product(d,2). reaction(2).' 'reactant(b,3). product(c,3). reaction(3).' 'reactant(c,r3). product(b,r3). reaction(r3).' 'reactant(d,4). product(e,4). reaction(4).' 'reactant(e,r4). product(d,r4). reaction(r4).' 'reactant(e,5). product(g,5). reaction(5).' 'reactant(b,6). product(f,6). reaction(6).') graph_7 = graph + quoted_data('reactant(d,7). product(c,7). reaction(7).') # utils.render_network(graph_7 + 'dot_property("7",style,dashed).', 'todel.png') # uncomment to help debugging expected_seeds_sets = { frozenset('a'), frozenset('cd'), frozenset('ce'), frozenset('bd'), frozenset('be') } assert expected_seeds_sets == set( search_seeds(graph, targets='fg', forbidden_seeds='fg')) expected_seeds_sets = {frozenset('a'), frozenset('d'), frozenset('e')} assert expected_seeds_sets == set( search_seeds(graph_7, targets='fg', forbidden_seeds='fg')) # optimal search expected_seeds_sets = {frozenset('a')} assert expected_seeds_sets == set( search_seeds(graph, targets='fg', forbidden_seeds='de', compute_optimal_solutions=True)) expected_seeds_sets = {frozenset('a'), frozenset('d'), frozenset('e')} assert expected_seeds_sets == set( search_seeds(graph_7, targets='fg', forbidden_seeds='fg', compute_optimal_solutions=True))
def test_forbidden_intermediate_nodes(): "Test the handling of middle SCC that are completely forbidden" graph = quoted_data('reactant((a;b;c),1). product(d,1). reaction(1).' 'reactant(d,2). product(e,2). reaction(2).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('e'), frozenset('abc'), frozenset('ae'), frozenset('be'), frozenset('ce')} assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', explore_pareto=True, greedy=True, prerun=False)) assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', explore_pareto=True, greedy=True, prerun=False)) seeds_sets = {frozenset('e')} assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', prerun=False, compute_optimal_solutions=True)) seeds_sets = {frozenset('d'), frozenset('abc')} assert seeds_sets == set(search_seeds(graph, targets='e', explore_pareto=True, prerun=True)) # d as seeds covers more metabolites + less targets as seeds assert seeds_sets == set(search_seeds(graph, targets='e', explore_pareto=True, prerun=False)) seeds_sets = {frozenset('e'), frozenset('abc')} assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', prerun=False)) seeds_sets = {frozenset('e'), frozenset('abc')} assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', explore_pareto=True, greedy=True, prerun=False, verbose=True)) assert seeds_sets == set(search_seeds(graph, targets='e', forbidden_seeds='d', explore_pareto=True, prerun=True, verbose=True))
def test_one_scc(): """This show the local limits: whenever solutions are in the same SCC, only the very best are yielded. As a consequence, both pareto and without pareto loose some interesting (though non-optimal) solutions This problem has been fixed by implementing iterative pareto. (see last tests) """ graph = quoted_data("""reactant(a,1). product(b,1). reaction(1). reactant((b;c),2). product(d,2). reaction(2). reactant(d,3). product(a,3). reaction(3). reactant((d;e),4). product(f,4). reaction(4). reactant(f,5). product(c,5). reaction(5).""") # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('d'), frozenset('de')} # 'af' would have been welcome, but does not cover as much as 'de' assert expected_seeds_sets == set(search_seeds(graph, targets='d', explore_pareto=True, greedy=True, avoid_targets_as_seeds=True)) assert expected_seeds_sets == set(search_seeds(graph, targets='d', explore_pareto=True, greedy=True, avoid_targets_as_seeds=False)) # invariant expected_seeds_sets = {frozenset('d')} # this is… optimized assert expected_seeds_sets == set(search_seeds(graph, targets='d', explore_pareto=False)) # to get a real sens of diversity, we need to forbids the obvious solutions # (here, just 'd', but it could be much more subtle). expected_seeds_sets = {frozenset('ac'), frozenset('af'), frozenset('bc'), frozenset('bf')} assert expected_seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=False)) expected_seeds_sets = {frozenset('af'), frozenset('bf'), frozenset('ace'), frozenset('aef'), frozenset('bce'), frozenset('bef')} assert expected_seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True, greedy=True)) # hence, this test ask a question: do another metric for the greedy search # in an SCC is needed ? Probably. # Something like a constraint providing *a maximal number/ratio of seed*. # This is approached using a mix of iterative and pareto solution: # while using greedy and pareto. expected_seeds_sets = {frozenset('af'), frozenset('bf'), frozenset('ace'), frozenset('aef'), frozenset('bce'), frozenset('bef')} assert expected_seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True, greedy=False, verbose=True, compute_optimal_solutions=False, filter_included_solutions=False)) expected_seeds_sets = {frozenset('af'), frozenset('bf'), frozenset('ace'), frozenset('bce')} assert expected_seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True, greedy=False, verbose=True, compute_optimal_solutions=False, filter_included_solutions=True)) expected_seeds_sets = {frozenset('af'), frozenset('bf')} assert expected_seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True, greedy=False, verbose=True, compute_optimal_solutions=True, filter_included_solutions=True))
def test_forbidden_sources(): "Test the handling of root SCC that are completely forbidden" graph = quoted_data('reactant(a,1). product(b,1). reaction(1).' 'reactant(b,2). product(a,2). reaction(2).' 'reactant(b,3). product(c,3). reaction(3).' 'reactant(c,4). product(d,4). reaction(4).') # utils.render_network(graph, 'todel.png') # uncomment to help debugging seeds_sets = {frozenset('a'), frozenset('b')} assert seeds_sets == set(search_seeds(graph)) assert seeds_sets == set(search_seeds(graph, greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', explore_pareto=True)) seeds_sets = {frozenset('a'), frozenset('b'), frozenset('d')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='c', greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='c')) seeds_sets = {frozenset('c'), frozenset('d')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='ab', greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='ab')) seeds_sets = {frozenset('c')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abd', greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abd')) seeds_sets = {frozenset('d')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abc', greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abc')) seeds_sets = set() assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abcd', greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='abcd')) # But with forbidden seeds, it's not that obvious: # once arrived at SCC a (containing a and b), it is impossible to do anything. seeds_sets = {frozenset('a'), frozenset('b')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True, greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='d', explore_pareto=True)) seeds_sets = {frozenset('a')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='b', explore_pareto=True, greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='b', explore_pareto=True, compute_optimal_solutions=True)) # seeds_sets = {frozenset('c'), frozenset('a')} # pareto exploration find optimals before compute_optimal_solutions post-process assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='b', explore_pareto=True, compute_optimal_solutions=False)) seeds_sets = {frozenset('b')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='a', explore_pareto=True, greedy=True)) assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='a', explore_pareto=True, compute_optimal_solutions=True)) # seeds_sets = {frozenset('c'), frozenset('b')} # pareto exploration find optimals before compute_optimal_solutions post-process assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='a', explore_pareto=True, compute_optimal_solutions=False)) seeds_sets = {frozenset('c')} assert seeds_sets == set(search_seeds(graph, targets='d', forbidden_seeds='ab', explore_pareto=True))
def test_lattice_reaction(): graph = quoted_data( 'reactant((a;b),1). product((d;e),1). reaction(1). reactant((b;c),2). product((e;f),2). reaction(2).' ) # utils.render_network(graph, 'todel.png') # uncomment to help debugging expected_seeds_sets = {frozenset('bc'), frozenset('fab')} assert expected_seeds_sets == set( search_seeds(graph, targets='ef', forbidden_seeds='e')) expected_seeds_sets = {frozenset('bc')} assert expected_seeds_sets == set( search_seeds(graph, targets='ef', forbidden_seeds='e', compute_optimal_solutions=True)) assert expected_seeds_sets == set( search_seeds(graph, targets='f', forbidden_seeds='f')) expected_seeds_sets = {frozenset('ab'), frozenset('de'), frozenset('dbc')} assert expected_seeds_sets == set(search_seeds(graph, targets='de')) expected_seeds_sets = {frozenset('ab'), frozenset('de')} assert expected_seeds_sets == set( search_seeds(graph, targets='de', compute_optimal_solutions=True)) expected_seeds_sets = {frozenset('ab')} assert expected_seeds_sets == set( search_seeds(graph, targets='d', forbidden_seeds='d')) expected_seeds_sets = {frozenset('ab'), frozenset('bc')} assert expected_seeds_sets == set( search_seeds(graph, targets='e', forbidden_seeds='e')) assert {frozenset('abc')} == set( search_seeds(graph, targets='e', forbidden_seeds='e', enum_mode='union')) assert {frozenset('b')} == set( search_seeds(graph, targets='e', forbidden_seeds='e', enum_mode='intersection')) assert {frozenset('abc') } == set(search_seeds(graph, targets='def', forbidden_seeds='def')) assert {frozenset('ab') } == set(search_seeds(graph, targets='e', forbidden_seeds='ce'))
def test_network_example(): seeds_sets = set(search_seeds(graph)) # utils.render_network(graph, 'todel.png') # uncomment to help debugging assert seeds_sets == expected_seeds_sets
def test_toy1(): "Usage of only graph_filename as parameter, enabling the use of networkx to search for SCCs" seeds_sets = set(predator.search_seeds(graph_filename='./networks/toy_1.sbml')) assert seeds_sets == set(map(frozenset, ({'s', 'b'}, {'s', 'c'}, {'s', 'd'}, {'s', 'e'})))