CASCI-lab / CANA

CANAlization: Control & Redundancy in Boolean Networks
https://casci-lab.github.io/CANA/
MIT License
22 stars 15 forks source link

constant nodes breaking state_transtion_graph and other functions #18

Closed stas-g closed 1 year ago

stas-g commented 3 years ago

hello, i found some bad behaviour. adding constants seem to be breaking state_transition_graph function and making attractor calculating functions to run ad infimum. additionally, i noticed that adding constants by hand does not add them to the constants attribute of a BN object. simple reproducible example:

W *= W
Y *= Y
Z *= W
X *= (Y and Z)

code:

P = boolean_network.BooleanNetwork.from_file("/home/xxx.txt", type = 'logical', keep_constants = True)
P.constants
Out[43]: {}

P.Nconstants
Out[44]: 0

P.set_constant(2, value = 0)

P.Nconstants
Out[46]: 1

P.constants
Out[47]: {}

x0 = '1110'

STG = P.state_transition_graph()
Traceback (most recent call last):

  File "<ipython-input-51-42bcf3ddc551>", line 1, in <module>
    STG = P.state_transition_graph()

  File "/home/nfg/anaconda3/lib/python3.8/site-packages/cana/boolean_network.py", line 570, in state_transition_graph
    self._stg.add_nodes_from((i, {'label': self.num2bin(i)}) for i in range(self.Nstates))

  File "/home/nfg/anaconda3/lib/python3.8/site-packages/networkx/classes/digraph.py", line 459, in add_nodes_from
    for n in nodes_for_adding:

  File "/home/nfg/anaconda3/lib/python3.8/site-packages/cana/boolean_network.py", line 570, in <genexpr>
    self._stg.add_nodes_from((i, {'label': self.num2bin(i)}) for i in range(self.Nstates))

  File "/home/nfg/anaconda3/lib/python3.8/site-packages/cana/boolean_network.py", line 746, in <lambda>
    self.num2bin = lambda sn: binstate_to_constantbinstate(

  File "cana/cutils.pyx", line 207, in cana.cutils.binstate_to_constantbinstate

IndexError: string index out of range

P.attractor(initial = x0) ##freezes

P.trajectory_to_attractor(initial = x0) ##freezes
rionbr commented 3 years ago

Thanks for the message @stas-g, this looks like a bug. We will try to look at this asap.

rionbr commented 3 years ago

Hey @stas-g, Sorry this took longer than I expected. I think I have solved the issue. Please try this:

from cana.boolean_network import BooleanNetwork
P = BooleanNetwork.from_file("/home/xxx.txt", type='logical', keep_constants=True)
P.set_constant(node=2, constant=True, state=0)  # <this is NEW>
P.get_constants()  # <this is NEW>
>>> {2: <cana.boolean_node.BooleanNode at 0xa1bc39b50>}

STG = P.state_transition_graph()
STG.number_of_nodes()
>>> 8

x0 = '1110'
P.attractor(initial=x0)
>>> [6]

P.trajectory_to_attractor(initial=x0)
>>> ['1110', '1101', '1100']

Just note that the STG is a variable that is stored internally on P (for efficiency), so if you modify constants on the fly you need to make sure the STG is updated.

I haven't sent a build to pipy because I still want to do more testing on this with the other authors to make sure nothing broke elsewhere. But you can install the latest version pushed to github.

Again, thank for raising this issue! With kind regards,

stas-g commented 3 years ago

excellent! thank you, will give this a try this upcoming week!

stas-g commented 2 years ago

hi again,

just got about to properly test this and i don't think the changes you have made do not filter through as i get:

Traceback (most recent call last):

  File "/tmp/ipykernel_19411/3364207077.py", line 1, in <module>
    P.set_constant(node=2, constant=True, state=0)  # <this is NEW>

TypeError: set_constant() got an unexpected keyword argument 'constant'

P.get_constants()
Traceback (most recent call last):

  File "/tmp/ipykernel_19411/2667334193.py", line 1, in <module>
    P.get_constants()

AttributeError: 'BooleanNetwork' object has no attribute 'get_constants'

i used both pip install git+git://github.com/rionbr/CANA and pip install cana in hope that if there are any changes to the module (which i know there have been), a newer version would be installed but clearly this doesn't happen.

i believe i run cana version 0.1.2.

stas-g commented 2 years ago

hi Rion,

update: uninstalled and reinstalled cana and it all works now. many thanks for your help!

best, stas

austin-marcus commented 1 year ago

I was able to reproduce the correct output above here. So I am marking this as resolved.