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

83.33
/cameo/strain_design/heuristic/evolutionary/multiprocess/migrators.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__ = ['MultiprocessingMigrator']
2×
18

19
import six.moves.queue
2×
20
from cameo.parallel import RedisQueue
2×
21
from uuid import uuid4
2×
22

23
import logging
2×
24
logger = logging.getLogger(__name__)
2×
25

26

27
class MultiprocessingMigrator(object):
2×
28
    """Migrate among processes on multiple machines. This is possible by
29
    having a Queue implemented on redis.
30

31
    This callable class allows individuals to migrate from one process
32
    to another. It maintains a queue of migrants whose maximum length
33
    can be fixed via the ``max_migrants`` parameter in the constructor.
34
    If the number of migrants in the queue reaches this value, new migrants
35
    are not added until earlier ones are consumed. The unreliability of a
36
    multiprocessing environment makes it difficult to provide guarantees.
37
    However, migrants are theoretically added and consumed at the same rate,
38
    so this value should determine the "freshness" of individuals, where
39
    smaller queue sizes provide more recency.
40

41
    An optional keyword argument in ``args`` requires the migrant to be
42
    evaluated by the current evolutionary computation before being inserted
43
    into the population. This can be important when different populations
44
    use different evaluation functions and you need to be able to compare
45
    "apples with apples," so to speak.
46

47
    The migration takes the current individual *I* out of the queue, if
48
    one exists. It then randomly chooses an individual *E* from the population
49
    to insert into the queue. Finally, if *I* exists, it replaces *E* in the
50
    population (re-evaluating fitness if necessary). Otherwise, *E* remains in
51
    the population and also exists in the queue as a migrant.
52

53
    Optional keyword arguments in args:
54

55
    - *evaluate_migrant* -- should new migrants be evaluated before
56
      adding them to the population (default False)
57

58
    """
59

60
    def __init__(self, max_migrants=1):
2×
61
        self.max_migrants = max_migrants
2×
62
        self.migrants = RedisQueue(uuid4())
2×
63
        self.__name__ = self.__class__.__name__
2×
64

65
    def __call__(self, random, population, args):
2×
66
        evaluate_migrant = args.setdefault('evaluate_migrant', False)
2×
67
        migrant_index = random.randint(0, len(population) - 1)
2×
68
        old_migrant = population[migrant_index]
2×
69
        try:
2×
70
            migrant = self.migrants.get(block=False)
2×
71
            if evaluate_migrant:
2×
UNCOV
72
                fit = args["_ec"].evaluator([migrant.candidate], args)
!
UNCOV
73
                migrant.fitness = fit[0]
!
UNCOV
74
                args["_ec"].num_evaluations += 1
!
75
            population[migrant_index] = migrant
2×
76
        except six.moves.queue.Empty:
2×
77
            logger.debug("Empty queue")
2×
78
        try:
2×
79
            self.migrants.put_nowait(old_migrant)
2×
NEW
80
        except six.moves.queue.Full:
!
81
            logger.debug("Full queue")
!
82
        return population
2×
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