jcrozum / pystablemotifs

Python library for attractor identification and control in Boolean networks
MIT License
28 stars 7 forks source link

Rule of the form "() & (X)" during a reduction #29

Closed jgtz closed 4 years ago

jgtz commented 4 years ago

During network reduction, a function has a he form "() & (X)", which causes problems downstream.

Here is an example:

N=500
K=2
p=sm.RandomBooleanNetworks.get_criticality_p_Kauffman(K)[0] 
N_ensemble=10
seed=1000
rbn_ensemble=sm.RandomBooleanNetworks.Random_Boolean_Network_Ensemble_Kauffman(N,K,p,N_ensemble,seed=seed,write_Boolean_network=True)

start=default_timer()
rules=rbn_ensemble[9]
rules = sm.Format.booleannet2bnet(rules)
primes = PyBoolNet.FileExchange.bnet2primes(rules)
PyBoolNet.PrimeImplicants._percolation(primes,True)
end=default_timer()
print("Time (s) creating reduced networks:",end-start)
print("Reduced network size: ",str(len(primes)))
#sm.Format.pretty_print_prime_rules(primes)

start=default_timer()
diag = sm.Succession.build_succession_diagram(primes)
end=default_timer()
print("Time (s) finding atttactors:",end-start)
print("Number of attractors: ",str(len(diag.attractor_fixed_nodes_list)))
diag.attractor_candidate_summary()

Here is the error:

Time (s) creating reduced networks: 0.1052130560000002
Reduced network size:  243
(  ) & ( n107 | !n108 | n116 | !n126 | !n145 | !n151 | n155 | n169 | !n197 | n209 | n24 | !n263 | n279 | !n28 | !n309 | !n313 | !n328 | !n341 | !n352 | n356 | n360 | !n369 | !n374 | !n381 | !n397 | n399 | !n406 | !n41 | n429 | n465 | n469 | n483 | n7 | n75 | n85 | !n88 | !n90 ) & ( !n106 | n107 | !n108 | n116 | n119 | !n126 | !n137 | !n145 | !n151 | !n152 | n155 | !n165 | n169 | n209 | !n214 | n24 | !n263 | n279 | !n28 | !n309 | !n313 | !n328 | n33 | n332 | !n341 | n349 | !n352 | n356 | n360 | !n369 | !n374 | !n376 | n377 | !n381 | !n390 | n392 | !n397 | n399 | !n401 | !n406 | !n41 | n429 | n465 | n469 | !n480 | n483 | n485 | n7 | n75 | n85 | !n88 | !n90 | !n92 ) & ( !n106 | n107 | !n108 | n116 | !n126 | !n137 | !n145 | !n151 | n155 | n169 | n209 | n24 | !n263 | n279 | !n28 | !n309 | !n313 | !n328 | !n341 | !n352 | n356 | n360 | !n369 | !n374 | n377 | !n381 | n392 | !n397 | n399 | !n406 | !n41 | n429 | n465 | n469 | n483 | n485 | !n52 | n7 | n75 | n85 | !n88 | !n90 )
b''
b'/Users/jgtz/anaconda3/envs/py37_SM/lib/python3.7/site-packages/PyBoolNet/Dependencies/eqntott/eqntott_mac64: 0: syntax error\n... \x00Test = (  ) & ( n107 | !n108 | n116 | !n126 | !n145 | !n151 | n155 | n1 ...\n               ^\n'

Call to "eqntott" resulted in return code 1
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-3-b36833cd75ee> in <module>
     10 
     11 start=default_timer()
---> 12 diag = sm.Succession.build_succession_diagram(primes)
     13 end=default_timer()
     14 print("Time (s) finding atttactors:",end-start)

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Succession.py in build_succession_diagram(primes, fixed, motif_history, diagram, merge_equivalent_motifs, max_simulate_size, prioritize_source_motifs)
    585     if fixed is None:
    586         fixed = {}
--> 587     myMotifReduction=sm_reduction.MotifReduction(motif_history,fixed.copy(),primes,max_simulate_size=max_simulate_size,prioritize_source_motifs=prioritize_source_motifs)
    588     if diagram is None:
    589         diagram = SuccessionDiagram()

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Reduction.py in __init__(self, motif_history, fixed, reduced_primes, max_simulate_size, prioritize_source_motifs)
    254                     break
    255             if self.terminal == "possible":
--> 256                 self.rspace_constraint = sm_format.pretty_print_rspace(self.rspace)
    257                 self.reduced_rspace_constraint = sm_rspace.reduce_rspace_string(self.rspace_constraint,self.fixed_rspace_nodes)
    258                 self.rspace_update_primes = reduce_primes(self.fixed_rspace_nodes,self.reduced_primes)[0]

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Format.py in pretty_print_rspace(L, simplify, silent)
    164     if simplify:
    165         print(s)
--> 166         s = PyBoolNet.BooleanLogic.minimize_espresso(s)
    167     if not silent:
    168         print(s)

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/PyBoolNet/BooleanLogic.py in minimize_espresso(Expression, Outputfile, Merge, Equiv, Exact, Reduce)
    152         eqntott_cmd += ['/dev/stdin']
    153         eqntott_in = Expression
--> 154         eqntott_out = run_eqntott(eqntott_cmd, eqntott_in)
    155 
    156         if int(re.search(r'\.p\s\d+', eqntott_out).group().strip(".p ")) != 0:

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/PyBoolNet/BooleanLogic.py in run_eqntott(eqntott_cmd, eqntott_in)
     75         eqntott_out, eqntott_err = eqntott.communicate(input=eqntott_in.encode())
     76         eqntott.stdin.close()
---> 77         _eqntott_error(eqntott, eqntott_out, eqntott_err)
     78         return(eqntott_out.decode())
     79 

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/PyBoolNet/BooleanLogic.py in _eqntott_error(eqntott, eqntott_out, eqntott_err)
     57                 print(eqntott_err)
     58                 print('\nCall to "eqntott" resulted in return code %i'%eqntott.returncode)
---> 59                 raise Exception
     60 
     61 

Exception: 
jgtz commented 4 years ago

Note: I added a print statement before the "PyBoolNet.BooleanLogic.minimize_espresso" error to see the function,

jcrozum commented 4 years ago

It looks like this error is in the rspace construction part, not the network reduction part. I'll look into it though. Thanks for the detailed report!

jcrozum commented 4 years ago

I think I've got it. It was that I wasn't handling the case when we get information from the TR stable motifs, but not the single-node drivers. Since it was such a small bugfix (2 lines), I've pushed directly to the master branch.

The example you gave now runs. I'll leave it to you to close the issue when you're convinced the bug isn't popping up in your other tests.

jgtz commented 4 years ago

Found a problem that, I think, is related to the fix (I think when the list you added to fix the problem is empty?)

Code:

start=default_timer()
rules=rbn_ensemble[0]
rules = sm.Format.booleannet2bnet(rules)
primes = PyBoolNet.FileExchange.bnet2primes(rules)
PyBoolNet.PrimeImplicants._percolation(primes,True)
end=default_timer()
print("Time (s) creating reduced networks:",end-start)
print("Reduced network size: ",str(len(primes)))
#sm.Format.pretty_print_prime_rules(primes)

N=1000
K=2
p=sm.RandomBooleanNetworks.get_criticality_p_Kauffman(K)[0] 
N_ensemble=10
seed=1000
rbn_ensemble=sm.RandomBooleanNetworks.Random_Boolean_Network_Ensemble_Kauffman(N,K,p,N_ensemble,seed=seed,write_Boolean_network=True)
start=default_timer()
diag = sm.Succession.build_succession_diagram(primes)
end=default_timer()
print("Time (s) finding atttactors:",end-start)
print("Number of attractors: ",str(len(diag.attractor_fixed_nodes_list)))
diag.attractor_candidate_summary()

Error

Time (s) creating reduced networks: 0.25272280399999403
Reduced network size:  355
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-5ac000e2c334> in <module>
     10 
     11 start=default_timer()
---> 12 diag = sm.Succession.build_succession_diagram(primes)
     13 end=default_timer()
     14 print("Time (s) finding atttactors:",end-start)

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Succession.py in build_succession_diagram(primes, fixed, motif_history, diagram, merge_equivalent_motifs, max_simulate_size, prioritize_source_motifs)
    608                 diagram, merge_equivalent_motifs=merge_equivalent_motifs,
    609                 max_simulate_size=max_simulate_size,
--> 610                 prioritize_source_motifs=prioritize_source_motifs)
    611     return diagram
    612 

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Succession.py in build_succession_diagram(primes, fixed, motif_history, diagram, merge_equivalent_motifs, max_simulate_size, prioritize_source_motifs)
    585     if fixed is None:
    586         fixed = {}
--> 587     myMotifReduction=sm_reduction.MotifReduction(motif_history,fixed.copy(),primes,max_simulate_size=max_simulate_size,prioritize_source_motifs=prioritize_source_motifs)
    588     if diagram is None:
    589         diagram = SuccessionDiagram()

~/anaconda3/envs/py37_SM/lib/python3.7/site-packages/StableMotifs/Reduction.py in __init__(self, motif_history, fixed, reduced_primes, max_simulate_size, prioritize_source_motifs)
    247             #self.fixed_rspace_nodes =sm_rspace.fixed_rspace_nodes(self.rspace,self.reduced_primes)
    248             self.rspace = sm_rspace.reduce_rspace(self.rspace,self.reduced_primes)
--> 249             self.fixed_rspace_nodes = self.rspace[0][0]
    250 
    251             for motif in self.stable_motifs:

IndexError: list index out of range
jcrozum commented 4 years ago

Okay, I think I fixed it for real this time.

I also fixed another small bug that appeared in this example; the formatter that passes rules to espresso wasn't handling trivial rules (e.g., n26*=0) properly in certain circumstances during reduction.

jgtz commented 4 years ago

I have not found any more problems with this when testing, so I think I will close it.