Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

biosustain / cameo / 963

15 Feb 2016 - 14:59 coverage decreased (-4.3%) to 62.885%
963

Pull #41

travis-ci

Daaf0308d36e60bacd269a6f70b6dd70?size=18&default=identiconphantomas1234
Merge branch 'devel' into structural_analysis_2
Pull Request #41: Second try on implementing the enumeration of shortest elementary flux modes and MCS

431 of 908 new or added lines in 38 files covered. (47.47%)

678 existing lines in 28 files now uncovered.

3143 of 4998 relevant lines covered (62.89%)

1.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.37
/cameo/strain_design/heuristic/evolutionary/variators.py
1
# Copyright 2014 Novo Nordisk Foundation Center for Biosustainability, DTU.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
from __future__ import absolute_import, print_function
2×
16

17
__all__ = ['set_mutation', 'set_indel']
2×
18

19
from six.moves import range
2×
20

21
from inspyred.ec.variators import mutator, crossover
2×
22
from ordered_set import OrderedSet
2×
23
from cameo.strain_design.heuristic.evolutionary.genomes import MultipleChromosomeGenome
2×
24
from numpy import float32 as float
2×
25

26
import logging
2×
27
logger = logging.getLogger(__name__)
2×
28

29

30
def _do_set_n_point_crossover(representation, mom, dad, points, random, candidate_size):
2×
31
    chunks = []
2×
32
    i = 0
2×
33
    for point in points:
2×
34
        chunks.append(representation[i:point])
2×
35
        i = point
2×
36
    chunks.append(representation[i:])
2×
37

38
    bro = OrderedSet()
2×
39
    sis = OrderedSet()
2×
40

41
    cross = True
2×
42
    for variables in chunks:
2×
43
        for v in variables:
2×
44
            if v in mom:
2×
45
                bro.append(v) if cross else sis.append(v)
2×
46
            if v in dad:
2×
47
                sis.append(v) if cross else bro.append(v)
2×
48
        cross = not cross
2×
49

50
    if len(bro) > candidate_size:
2×
UNCOV
51
        bro = random.sample(bro, candidate_size)
!
52

53
    if len(sis) > candidate_size:
2×
UNCOV
54
        sis = random.sample(sis, candidate_size)
!
55
    return bro, sis
2×
56

57

58
@crossover
2×
59
def set_n_point_crossover(random, mom, dad, args):
60
    representation = list(set(mom).union(set(dad)))
2×
61
    crossover_rate = args.setdefault('crossover_rate', 1.0)
2×
62
    num_crossover_points = args.setdefault('num_crossover_points', 1)
2×
63
    candidate_size = args.setdefault('candidate_size', 9)
2×
64
    children = []
2×
65
    if random.random() <= crossover_rate:
2×
66
        points = random.sample(representation, num_crossover_points)
2×
67
        bro, sis = _do_set_n_point_crossover(representation, mom, dad, points, random, candidate_size)
2×
68

69
        # ensure number of knockouts > 0 or do not add individual
70
        if len(bro) > 0:
2×
71
            children.append(bro)
2×
72
        if len(sis) > 0:
2×
73
            children.append(sis)
2×
74
    else:
UNCOV
75
        children.append(mom)
!
76
        children.append(dad)
!
77
    return children
2×
78

79

80
@mutator
2×
81
def set_mutation(random, individual, args):
82
    """
83
    Mutates a given set based on the entries available on the representation.
84

85
    Parameters
86
    ----------
87

88
    random: Random
89
    individual: list
90
        with unique integers
91
    args: dict
92
        must contain the representation
93

94
    Returns
95
    -------
96
    list
97
        created based on an ordered set
98

99
    """
100
    representation = args.get('representation')
2×
101
    new_individual = []
2×
102
    mutation_rate = float(args.get('mutation_rate', .1))
2×
103
    for index in individual:
2×
104
        if random.random() < mutation_rate:
2×
105
            new_individual.append(random.randint(0, len(representation) - 1))
2×
106
        else:
UNCOV
107
            new_individual.append(index)
!
108

109
    return list(OrderedSet(new_individual))
2×
110

111

112
@mutator
2×
113
def set_indel(random, individual, args):
114
    """
115
    Creates a random insertion or deletion in the individual.
116

117
    Parameters
118
    ----------
119

120
    random: Random
121
    individual: list
122
        with unique integers
123
    args: dict
124
        must contain the representation
125

126
    Returns
127
    -------
128
    list
129
        created based on an ordered set
130

131
    """
132
    max_size = args.get("max_size", 9)
2×
133
    representation = args.get('representation')
2×
134
    indel_rate = float(args.get('indel_rate', .1))
2×
135
    new_individual = list(individual)
2×
136
    if random.random() < indel_rate:
2×
137
        logger.info("Applying indel mutation")
2×
138
        if random.random() > 0.5 and len(new_individual) < max_size:
2×
139
            new_individual.append(random.sample(range(len(representation)), 1)[0])
2×
140
        else:
UNCOV
141
            if len(new_individual) > 1:
!
142
                new_individual = random.sample(new_individual, len(new_individual) - 1)
!
143

144
    return list(OrderedSet(new_individual))
2×
145

146

147
@mutator
2×
148
def multiple_chromosome_set_mutation(random, individual, args):
149
    """
150
    Mutates a given set based on the entries available on the representation.
151

152
    Parameters
153
    ----------
154

155
    random: Random
156
    individual: MultipleChromosomeGenome
157
        with unique integers in each chromosome
158
    args: dict
159
        must contain the representation of each chromosome
160

161
    Returns
162
    -------
163
    MultipleChromosomeGenome
164
        A mutated individual
165

166
    """
167
    new_individual = MultipleChromosomeGenome(keys=individual.keys)
2×
168

169
    for key in individual.keys:
2×
170
        representation = args.get('%s_representation' % key)
2×
171
        mutation_rate = args.get('%s_mutation_rate' % key, .1)
2×
172
        for index in individual[key]:
2×
173
            if random.random() < mutation_rate:
2×
174
                new_individual[key].append(random.randint(0, len(representation) - 1))
2×
175
            else:
UNCOV
176
                new_individual[key].append(index)
!
177

178
    return new_individual
2×
179

180

181
@mutator
2×
182
def multiple_chromosome_set_indel(random, individual, args):
183
    """
184
    Creates a random insertion or deletion in the individual.
185

186
    Parameters
187
    ----------
188

189
    random: Random
190
    individual: MultipleChromosomeGenome
191
        with unique integers in each chromosome
192
    args: dict
193
        must contain the representation of each chromosome
194

195
    Returns
196
    -------
197
    MultipleChromosomeGenome
198
        A mutated individual
199
    """
200
    new_individual = individual.copy()
2×
201
    max_size = args.get("max_size", 9)
2×
202
    for key in individual.keys:
2×
203
        representation = args.get('%s_representation' % key)
2×
204
        indel_rate = args.get('%s_indel_rate' % key, .1)
2×
205
        if random.random() < indel_rate:
2×
206
            if random.random() > 0.5 and len(new_individual[key]) < max_size:
2×
207
                new_individual[key].append(random.sample(range(len(representation)), 1)[0])
2×
208
            else:
209
                if len(individual[key]) > 1:
2×
210
                    new_individual[key] = random.sample(new_individual[key], len(new_individual[key]) - 1)
2×
211

212
    return new_individual
2×
213

214

215
@crossover
2×
216
def multiple_chromosome_n_point_crossover(random, mom, dad, args):
UNCOV
217
    children = MultipleChromosomeGenome(keys=mom.keys)
!
218
    for key in children.keys:
!
219
        children[key] = set_n_point_crossover(random, [mom[key], dad[key]], args)
!
220

UNCOV
221
    return children
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, Inc