File: discrete_markov.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (107 lines) | stat: -rw-r--r-- 3,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy

from cogent.recalculation.definition import (NonParamDefn, CalcDefn, 
    EvaluatedCell, PartitionDefn, ConstCell, ConstDefn,
    DictArrayTemplate)
    
from cogent.evolve.substitution_model import _SubstitutionModel

__author__ = "Peter Maxwell"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Peter Maxwell"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Peter Maxwell"
__email__ = "pm67nz@gmail.com"
__status__ = "Production"

class PsubMatrixDefn(PartitionDefn):
    "Square 2D array made of 1D partitions"
    
    numeric = False # well, not scalar anyway
    const_by_default = False
    independent_by_default = True
    
    def __init__(self, default=None, name=None, dimensions=None,
            dimension=None, size=None, **kw):
        PartitionDefn.__init__(self, default, name, dimensions,
            dimension, size, **kw)
        
        (dim_name, dim_cats) = self.internal_dimension
        self.internal_dimensions = (dim_name, dim_name+"2")
        self.array_template = DictArrayTemplate(dim_cats, dim_cats)
    
    def _makeDefaultValue(self):
        # Purely flat default doesn't work well so start at approx t=0.5
        flat = numpy.ones([self.size, self.size], float) / self.size
        diag = numpy.identity(self.size, float)
        return (flat + diag) / 2

    def checkValueIsValid(self, value, is_const):
        if value.shape != (self.size,self.size):
            raise ValueError("Wrong array shape %s for %s, expected (%s,%s)" % 
                    (value.shape, self.name, self.size, self.size))
        for part in value:
            PartitionDefn.checkValueIsValid(self, part, is_const)
    
    def makeCells(self, input_soup={}, variable=None):
        uniq_cells = []
        all_cells = []
        for (i, v) in enumerate(self.uniq):
            if v is None:
                raise ValueError("input %s not set" % self.name)
            assert hasattr(v, 'getDefaultValue'), v
            value = v.getDefaultValue()
            assert hasattr(value, 'shape'), value
            assert value.shape == (self.size,self.size)
            scope = [key for key in self.assignments
                    if self.assignments[key] is v]
            if v.is_const or (variable is not None and variable is not v):
                matrix = ConstCell(self.name, value)
            else:
                rows = []
                for part in value:
                    (ratios, partition) = self._makePartitionCell(
                            self.name+'_part', scope, part)
                    all_cells.extend(ratios)
                    rows.append(partition)
                all_cells.extend(rows)
                matrix = EvaluatedCell(self.name, lambda *x:numpy.array(x), 
                        rows)
            all_cells.append(matrix)
            uniq_cells.append(matrix)
        return (all_cells, uniq_cells)

class DiscreteSubstitutionModel(_SubstitutionModel):
    _default_expm_setting = None
    
    def _isInstantaneous(self, x, y):
        return True
    
    def getParamList(self):
        return []
        
    def makePsubsDefn(self, **kw):
        motifs = tuple(self.getAlphabet())
        psub = PsubMatrixDefn(
            name="psubs", dimension = ('motif', motifs), default=None, 
            dimensions=('locus', 'edge'))
        return psub
        
    def makeFundamentalParamControllerDefns(self, bin_names):                
        (input_probs, word_probs, mprobs_matrix) = \
                self.mprob_model.makeMotifWordProbDefns()
        
        # XXX absorb 1 unneeded input
        word_probs = CalcDefn(
                lambda x,y:x, name='hack')(
                word_probs, mprobs_matrix)
        
        defns = {
            'motif_probs': input_probs,  
            'word_probs': word_probs,
            'bprobs': None,
            }
        return defns