Bases: set
| Parameters: |
|
|---|
>>> from __caspo__ import Conjunction, BooleanFamily, Setup
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> cs = [Conjunction.from_str('TNFa=Hsp27'), Conjunction.from_str('TNFa=p38')]
>>> family = BooleanFamily(setup, cs, False)
>>> len(family)
0
Adds a Boolean model to the family. Updates conjunction’s occurrences and GTTs. If the model has the input-output behavior of some previously added model, it’s added to the corresponding GTT. Otherwise, a new GTT is created with this model.
| Parameters: | model – __caspo__.BooleanModel instance to add |
|---|
>>> from __caspo__ import Conjunction, BooleanFamily, Setup, BooleanModel
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> c1 = Conjunction.from_str('TNFa=Hsp27')
>>> c2 = Conjunction.from_str('TNFa=ras')
>>> c3 = Conjunction.from_str('ras=Hsp27')
>>> c4 = Conjunction.from_str('TNFa=p38')
>>> cs = [c1, c2, c3, c4]
>>> family = BooleanFamily(setup, cs, True)
>>> len(family)
0
>>> len(family.gtts)
0
First, we add a model having only c1 and since the family is empty, a new GTT is created.
>>> family.add(BooleanModel([c1]))
>>> len(family)
1
>>> len(family.gtts)
1
Next, we add a model having c2 and c3 which is equivalent to the previous model.
>>> family.add(BooleanModel([c2,c3]))
>>> len(family)
2
>>> len(family.gtts)
1
Finally, if we add a model having a different input-output behavior, a new GTT is created.
>>> family.add(BooleanModel([c4]))
>>> len(family)
3
>>> len(family.gtts)
2
Now, let’s check the GTTs and their corresponding gathered models. Note that we convert family.gtts into a list only for testing (sets don’t preserve any order), but you don’t need to do it in your code.
>>> for gtt in sorted(family.gtts):
... print gtt.vector(), [m.vector() for m in gtt.models]
{'TNFa=Hsp27': 1} [{'TNFa=ras': 1, 'ras=Hsp27': 1}]
{'TNFa=p38': 1} []
Returns an iterator over the mutual inclusive/exclusive conjunctions.
| Parameters: |
|
|---|---|
| Returns: | iterator |
>>> from __caspo__ import Conjunction, BooleanFamily, Setup, BooleanModel
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> c1 = Conjunction.from_str('TNFa=Hsp27')
>>> c2 = Conjunction.from_str('TNFa=ras')
>>> c3 = Conjunction.from_str('ras=Hsp27')
>>> cs = [c1, c2, c3]
>>> family = BooleanFamily(setup, cs, False)
>>> family.add(BooleanModel([c1]))
>>> family.add(BooleanModel([c2,c3]))
>>> for m in family.combinatorics('exclusive'):
... print m['conjunction_A'], m['frequency_A'], m['conjunction_B'], m['frequency_B']
TNFa=Hsp27 0.5 TNFa=ras 0.5
TNFa=Hsp27 0.5 ras=Hsp27 0.5
>>> for m in family.combinatorics('inclusive'):
... print m['conjunction_A'], m['frequency_A'], m['conjunction_B'], m['frequency_B']
ras=Hsp27 0.5 TNFa=ras 0.5
Returns an iterator over tuples (h,f) where h is the string representation of a conjunction and f its frequency in the family.
| Returns: | iterator |
|---|
>>> from __caspo__ import Conjunction, BooleanFamily, Setup, BooleanModel
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> c1 = Conjunction.from_str('TNFa=Hsp27')
>>> c2 = Conjunction.from_str('TNFa=ras')
>>> c3 = Conjunction.from_str('ras=Hsp27')
>>> c4 = Conjunction.from_str('TNFa=p38')
>>> c5 = Conjunction.from_str('ras=p38')
>>> cs = [c1, c2, c3, c4, c5]
>>> family = BooleanFamily(setup, cs, False)
>>> family.add(BooleanModel([c1]))
>>> family.add(BooleanModel([c2,c3]))
>>> family.add(BooleanModel([c4]))
>>> family.add(BooleanModel([c2,c5]))
>>> print list(family.frequencies)
[('TNFa=Hsp27', 0.25), ('TNFa=ras', 0.5), ('ras=p38', 0.25), ('ras=Hsp27', 0.25), ('TNFa=p38', 0.25)]
Constructor from a matrix representation of logic models
| Parameters: |
|
|---|
>>> from __caspo__ import Conjunction, BooleanFamily, Setup
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> cs = [Conjunction.from_str('TNFa=Hsp27'), Conjunction.from_str('TNFa=p38')]
>>> matrix = [{'TNFa=Hsp27': '1', 'TNFa=p38': '0'}, {'TNFa=Hsp27': '0', 'TNFa=p38': '1'}]
>>> family = BooleanFamily.from_matrix(matrix, setup, cs, False)
>>> len(family)
2
>>> print list(family.frequencies)
[('TNFa=Hsp27', 0.5), ('TNFa=p38', 0.5)]
Constructor from a list of PyASP TermSet instances.
| Parameters: |
|
|---|---|
| Returns: |
Returns an iterator over all models in the family as vectors (key-value: conjunction-{0,1})
>>> from __caspo__ import Conjunction, BooleanFamily, Setup, BooleanModel
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> c1 = Conjunction.from_str('TNFa=Hsp27')
>>> c2 = Conjunction.from_str('TNFa=ras')
>>> c3 = Conjunction.from_str('ras=Hsp27')
>>> c4 = Conjunction.from_str('TNFa=p38')
>>> cs = [c1, c2, c3, c4]
>>> family = BooleanFamily(setup, cs, True)
>>> family.add(BooleanModel([c1]))
>>> family.add(BooleanModel([c2,c3]))
>>> family.add(BooleanModel([c2,c3,c4]))
>>> for model in sorted(family.to_matrix(), key=lambda v: v.values().count(1)):
... print model
{'TNFa=Hsp27': 1, 'TNFa=ras': 0, 'ras=Hsp27': 0, 'TNFa=p38': 0}
{'TNFa=Hsp27': 0, 'TNFa=ras': 1, 'ras=Hsp27': 1, 'TNFa=p38': 0}
{'TNFa=Hsp27': 0, 'TNFa=ras': 1, 'ras=Hsp27': 1, 'TNFa=p38': 1}
Compute the weighted MSE of the family. If GTTs were computed, each output prediction is weighted according to the number of models gathered by the corresponding GTT. Otherwise, output predictions are summed over all models in the family. If the family is complete, both computation are equivalent.
| Parameters: |
|
|---|
>>> from __caspo__ import Conjunction, BooleanFamily, Setup, BooleanModel
>>> setup = Setup(['TNFa'], ['p38'], ['Hsp27', 'p38'])
>>> c1 = Conjunction.from_str('TNFa=Hsp27')
>>> c2 = Conjunction.from_str('TNFa=ras')
>>> c3 = Conjunction.from_str('ras=Hsp27')
>>> c4 = Conjunction.from_str('TNFa=p38')
>>> cs = [c1, c2, c3, c4]
>>> family = BooleanFamily(setup, cs, True)
>>> family.add(BooleanModel([c1]))
>>> family.add(BooleanModel([c2,c3]))
>>> family.add(BooleanModel([c4]))
Now, let’s create a fake dataset and compute the family’s MSE
>>> from __caspo__ import Dataset, Experiment, Conjunction
>>> e0 = Experiment({'TNFa': 1, 'p38': 0}, {10: {'Hsp27':0.8, 'p38': 0}})
>>> e1 = Experiment({'TNFa': 0, 'p38': 1}, {10: {'Hsp27':0.2, 'p38':0}})
>>> dataset = Dataset([e0, e1], ['TNFa'], ['p38'], ['Hsp27', 'p38'], 10)
>>> print "%.4f" % family.weighted_mse(dataset) # ((2/3 - 0.8)^2 + (0 - 0.2)^2) / 4
0.0144
Bases: object
| Parameters: | conjunctions – an iterable of __caspo__.Conjunction instances |
|---|
>>> from __caspo__ import Conjunction, BooleanModel
>>> c1 = Conjunction.from_str('p38+traf6=Hsp27')
>>> c2 = Conjunction.from_str('!AKT=Hsp27')
>>> b = BooleanModel([c1,c2])
>>> b.has_conjunction(c1)
True
>>> b.targets('Hsp27')
True
>>> len(b['Hsp27'])
2
Read-only access to the list of conjunctions in the model
| Returns: | list |
|---|
>>> from __caspo__ import Conjunction, BooleanModel, Setup
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b = BooleanModel([c])
>>> [str(c) for c in b.conjunctions]
['TGFa+TNFa=Hsp27']
Constructor from a PyASP TermSet instance where each term in the TermSet is expected to describe a conjunction.
| Parameters: | termset – PyASP TermSet of conjunctions |
|---|---|
| Returns: | __caspo__.BooleanModel |
>>> from pyasp.asp import TermSet, Term
>>> from __caspo__ import BooleanModel
>>> c1 = Term('conjunction', [Term('set', ['"p38"',1, Term('set', ['"traf6"',1,'nil'])]), 2, '"Hsp27"'])
>>> c2 = Term('conjunction', [Term('set', ['"AKT"',-1, 'nil']), 2, '"Hsp27"'])
>>> b = BooleanModel.from_termset(TermSet([c1,c2]))
>>> [str(c) for c in b['Hsp27']]
['!AKT=Hsp27', 'p38+traf6=Hsp27']
Constructor from a vector representation
| Parameters: | vector – A mapping describing key-value pairs like (‘conjunction=target’,{‘0’,‘1’}) |
|---|---|
| Returns: | __caspo__.BooleanModel |
>>> from __caspo__ import BooleanModel
>>> b = BooleanModel.from_vector({'TNFa=CREB': '0', '!AKT=Hsp27': '1', 'p38+traf6=Hsp27': '1'})
>>> [str(c) for c in b['Hsp27']]
['!AKT=Hsp27', 'p38+traf6=Hsp27']
Returns whether the conjunction is present or not in the model
| Parameters: | conjunction – __caspo__.Conjunction instance |
|---|---|
| Returns: | bool |
>>> from __caspo__ import Conjunction, BooleanModel
>>> c1 = Conjunction.from_str('p38+traf6=Hsp27')
>>> c2 = Conjunction.from_str('!AKT=Hsp27')
>>> c3 = Conjunction.from_str('p38+traf6=Hsp27')
>>> b = BooleanModel([c1])
>>> b.has_conjunction(c1)
True
>>> b.has_conjunction(c2)
False
>>> b.has_conjunction(c3)
True
Proxy to standard iteritems function over mapping objects.
| Returns: | iterator of key-value pairs like: (target-node, conjunctions list) |
|---|
Compute Mean Squared Error with respect to a given dataset
| Parameters: |
|
|---|---|
| Returns: | float |
First, we create a fake dataset containing 2 simple experiments
>>> from __caspo__ import Dataset, Experiment, Conjunction, BooleanModel
>>> e0 = Experiment({'TNFa':1}, {10: {'Hsp27':1}})
>>> e1 = Experiment({'TNFa':0}, {10: {'Hsp27':1}})
>>> dataset = Dataset([e0, e1], ['TNFa'], [], ['Hsp27'], 10)
Now, we create a Boolean model and compute its MSE
>>> from __caspo__ import Conjunction, BooleanModel
>>> c = Conjunction.from_str('TNFa=Hsp27')
>>> b = BooleanModel([c])
>>> b.mse(dataset)
0.5
>>> b.mse(dataset, [0])
0.0
>>> b.mse(dataset, [1])
1.0
Resolve the Boolean value for a node under the given inputs.
| Parameters: |
|
|---|---|
| Returns: | bool |
>>> from __caspo__ import Conjunction, BooleanModel, Setup
>>> setup = Setup(['TNFa', 'TGFa'], [], ['Hsp27'])
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b = BooleanModel([c])
>>> b.resolve('Hsp27', {'TNFa':True, 'TGFa':False}, setup)
False
>>> b.resolve('Hsp27', {'TNFa':True, 'TGFa':True}, setup)
True
Returns whether the given node is a target in the model
| Parameters: | node – node name |
|---|---|
| Returns: | bool |
>>> from __caspo__ import Conjunction, BooleanModel
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b = BooleanModel([c])
>>> b.targets('Hsp27')
True
>>> b.targets('p38')
False
Converts the Boolean model instance to a TermSet instance
| Parameters: | ide – Optionally, an id can be included as the first argument in the terms describing the logic model |
|---|---|
| Returns: | TermSet |
>>> from __caspo__ import Conjunction, BooleanModel
>>> c1 = Conjunction.from_str('p38+traf6=Hsp27')
>>> c2 = Conjunction.from_str('!AKT=Hsp27')
>>> b = BooleanModel([c1,c2])
>>> ts = b.termset()
We have two predicates to describe a logic model: conjunction and in.
>>> conjunctions = [str(c) for c in ts if c.pred() == Conjunction.CONJUNCTION]
>>> print conjunctions
['conjunction(set("traf6",1,set("p38",1,nil)),2,"Hsp27")',
'conjunction(set("AKT",-1,nil),1,"Hsp27")']
>>> ins = [str(c) for c in b.termset() if c.pred() == Conjunction.IN]
>>> print ins
['in("AKT",-1,set("AKT",-1,nil))',
'in("p38",1,set("traf6",1,set("p38",1,nil)))',
'in("traf6",1,set("traf6",1,set("p38",1,nil)))']
Sometimes, adding an identifier to the model can be useful (to compute GTTs for example)
>>> tsi = b.termset(1)
>>> conjunctions_with_id = [str(c) for c in tsi if c.pred() == Conjunction.CONJUNCTION]
>>> print conjunctions_with_id
['conjunction(1,set("traf6",1,set("p38",1,nil)),2,"Hsp27")',
'conjunction(1,set("AKT",-1,nil),1,"Hsp27")']
Converts the Boolean model instance to a vector representation.
| Parameters: | v – An optional mapping to overwrite. |
|---|---|
| Returns: | dict |
>>> from __caspo__ import Conjunction, BooleanModel
>>> c1 = Conjunction.from_str('p38+traf6=Hsp27')
>>> c2 = Conjunction.from_str('!AKT=Hsp27')
>>> b = BooleanModel([c1,c2])
>>> b.vector()
{'!AKT=Hsp27': 1, 'p38+traf6=Hsp27': 1}
>>> v = {'!AKT=Hsp27': 0, 'p38+traf6=Hsp27': 0, 'TNFa=CREB': 0}
>>> b.vector(v)
{'TNFa=CREB': 0, '!AKT=Hsp27': 1, 'p38+traf6=Hsp27': 1}
>>> print v
{'TNFa=CREB': 0, '!AKT=Hsp27': 1, 'p38+traf6=Hsp27': 1}
Bases: __caspo__.BooleanModel.BooleanModel
| Parameters: | model – Boolean model representing the Global Truth Table |
|---|
>>> from __caspo__ import Conjunction, BooleanModel, GTT
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b = BooleanModel([c])
>>> g = GTT(b)
>>> len(g)
1
Adds a model to the GTT
| Parameters: | model – the boolean model to add |
|---|
>>> from __caspo__ import Conjunction, BooleanModel, GTT
>>> c1 = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b1 = BooleanModel([c1])
>>> g = GTT(b)
>>> c2 = Conjunction.from_str('TNFa=p38')
>>> b2 = BooleanModel([c2])
>>> g.add(b2)
>>> len(g)
2
Returns an iterator over all possible inputs and their corresponding outputs
| Parameters: | setup – __caspo__.Dataset.Setup instance |
|---|
>>> from __caspo__ import Conjunction, BooleanModel, Setup, GTT
>>> setup = Setup(['TNFa', 'TGFa'], [], ['Hsp27'])
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> gtt = GTT(BooleanModel([c]))
>>> for r in gtt.to_matrix(setup):
... print r
{'TGFa': 0, 'Hsp27': 0, 'TNFa': 0}
{'TGFa': 0, 'Hsp27': 0, 'TNFa': 1}
{'TGFa': 1, 'Hsp27': 0, 'TNFa': 0}
{'TGFa': 1, 'Hsp27': 1, 'TNFa': 1}
Bases: object
| Parameters: |
|
|---|
>>> from __caspo__ import Node, Conjunction
>>> c = Conjunction([Node('p38',1), Node('TNFa',-1)], 'Hsp27')
>>> len(c)
2
>>> print c
!TNFa+p38=Hsp27
Constructor from a string representation
| Parameters: | string – string representation of a conjunction |
|---|---|
| Returns: | __caspo__.Conjunction |
>>> from __caspo__ import Conjunction
>>> c1 = Conjunction.from_str('p38+traf6=Hsp27')
>>> c2 = Conjunction.from_str('!AKT=Hsp27')
>>> len(c1)
2
>>> len(c2)
1
>>> print [str(s) for s in c1.sources], c1.target
['p38', 'traf6'] Hsp27
>>> print [str(s) for s in c2.sources], c2.target
['!AKT'] Hsp27
Constructor from a PyASP Term
| Parameters: | term – PyASP Term instance |
|---|---|
| Returns: | __caspo__.Conjunction |
>>> from pyasp.asp import Term
>>> from __caspo__ import Conjunction
>>> t = Term('conjunction', [Term('set', ['"p38"',1, Term('set', ['"traf6"',1,'nil'])]), 2, '"Hsp27"'])
>>> c = Conjunction.from_term(t)
>>> len(c)
2
>>> print c
p38+traf6=Hsp27
Resolve the Boolean value under the given inputs.
| Parameters: |
|
|---|---|
| Returns: | bool |
>>> from __caspo__ import Conjunction, BooleanModel, Setup
>>> setup = Setup(['TNFa', 'TGFa'], [], ['Hsp27'])
>>> c = Conjunction.from_str('TNFa+TGFa=Hsp27')
>>> b = BooleanModel([c])
>>> c.resolve(b, {'TNFa':True, 'TGFa':False}, setup, [])
False
>>> c.resolve(b, {'TNFa':True, 'TGFa':True}, setup, [])
True
Converts the Conjunction instance to a TermSet instance
| Parameters: | ide – Optionally, an id can be included as the first argument in the terms describing the conjunction |
|---|---|
| Returns: | TermSet |
>>> from __caspo__ import Conjunction
>>> c = Conjunction.from_str('p38+traf6=Hsp27')
>>> ts = c.termset()
>>> conjunctions = [str(c) for c in ts if c.pred() == Conjunction.CONJUNCTION]
>>> print conjunctions
['conjunction(set("traf6",1,set("p38",1,nil)),2,"Hsp27")']
Bases: object
| Parameters: |
|
|---|
>>> from __caspo__ import Node
>>> n = Node('p38', 1)
>>> print n
p38
>>> n = Node('p38', -1)
>>> print n
!p38
Constructor from a string
| Parameters: | string – the signed node string representation |
|---|---|
| Returns: | __caspo__.Conjunction.Node |
>>> from __caspo__ import Node
>>> n = Node.from_str('!Hsp27')
>>> print n.name
Hsp27
>>> n.sign
-1
>>> n = Node.from_str('Hsp27')
>>> print n.name
Hsp27
>>> n.sign
1
Bases: list
| Parameters: |
|
|---|
Constructor from MIDAS file
| Parameters: | filepath – MIDAS filepath |
|---|---|
| Returns: | __caspo__.Dataset.Dataset |
Bases: object
| Parameters: |
|
|---|
Return boolean inputs
>>> from __caspo__ import Setup, Experiment
>>> setup = Setup(['TNFa', 'TGFa'], [], ['Hsp27', 'CREB'])
>>> e = Experiment({'TNFa': 1, 'TGFa': 0}, {10: {'Hsp27': 0.2, 'CREB': 0.9}})
>>> e.boolean_input(setup)
{'TGFa': False, 'TNFa': True}
Returns a key-value mapping of discrete observations
| Parameters: |
|
|---|---|
| Returns: | dict |
>>> from __caspo__ import Experiment
>>> e = Experiment({'TNFa': 1, 'TGFa': 0}, {10: {'Hsp27': 0.2, 'CREB': 0.953}})
>>> e.discrete_output(10, 100)
{'Hsp27': 20, 'CREB': 95}
Bases: object
| Parameters: |
|
|---|
Returns TermSet respresentation
| Returns: | TermSet |
|---|
>>> from __caspo__ import Setup
>>> setup = Setup(['TNFa', 'TGFa'], [], ['Hsp27', 'CREB'])
>>> setup.termset()
TermSet([Term('stimulus',['"TGFa"']), Term('readout',['"CREB"']), Term('stimulus',['"TNFa"']), Term('readout',['"Hsp27"'])])
Bases: object
| Parameters: |
|
|---|
Learn logic models
| Parameters: |
|
|---|---|
| Results : |
Run k-fold cross validation
| Parameters: |
|
|---|---|
| Returns: | iterator over the results |