Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 72 lines (54 sloc) 2.135 kb
8641712 Committing all Genetic Algorithm and Neural Network code plus tests.
chapmanb authored
1 #!/usr/bin/env python
2 """Tests for Genetic Algorithm Repair code.
3
4 This tests classes which are designed for repairing organisms after
5 mutation and crossover.
6 """
7 # standard library
675ed08 Rename all unittest test methods from t_* to test_* so that run_tests.py
mdehoon authored
8 import unittest
8641712 Committing all Genetic Algorithm and Neural Network code plus tests.
chapmanb authored
9
10 # biopython
11 from Bio.Seq import MutableSeq
12
13 # local stuff
14 from Bio.NeuralNetwork.Gene.Schema import Schema
15 from Bio.GA.Organism import Organism
16 from Bio.GA.Repair.Stabilizing import AmbiguousRepair
17
18
19
20 class TestAlphabet:
21 """Simple test alphabet.
22 """
23 alphabet_matches = {"1": "1",
24 "2": "2",
25 "3": "3",
26 "*": "123"}
27
28 letters = ["1", "2", "3", "*"]
29
30 def test_fitness(genome):
31 """Simple class for calculating fitnesses.
32 """
33 return 1
34
35 class AmbiguousRepairTest(unittest.TestCase):
36 """Test for the ability to repair too many ambiguous genes in a genome.
37 """
38 def setUp(self):
39 alphabet = TestAlphabet()
40 test_genome = MutableSeq("11*22*33*", alphabet)
41 self.organism = Organism(test_genome, test_fitness)
42
43 self.ambig_info = Schema(alphabet.alphabet_matches)
44
675ed08 Rename all unittest test methods from t_* to test_* so that run_tests.py
mdehoon authored
45 def test_single_repair(self):
8641712 Committing all Genetic Algorithm and Neural Network code plus tests.
chapmanb authored
46 """Test repair of a single ambiguous position in a genome.
47 """
48 repairer = AmbiguousRepair(self.ambig_info, 2)
49
50 for repair_attempt in range(5):
51 new_org = repairer.repair(self.organism)
52 new_genome_seq = new_org.genome.toseq()
53
54 assert new_genome_seq.data.count("*") == 2, \
55 "Did not repair genome, got %s" % new_genome_seq.data
56
675ed08 Rename all unittest test methods from t_* to test_* so that run_tests.py
mdehoon authored
57 def test_multiple_repair(self):
8641712 Committing all Genetic Algorithm and Neural Network code plus tests.
chapmanb authored
58 """Test repair of multiple ambiguous positions in a genome.
59 """
60 repairer = AmbiguousRepair(self.ambig_info, 0)
61
62 for repair_attempt in range(5):
63 new_org = repairer.repair(self.organism)
64 new_genome_seq = new_org.genome.toseq()
65
66 assert new_genome_seq.data.count("*") == 0, \
67 "Did not repair genome, got %s" % new_genome_seq.data
68
69 if __name__ == "__main__":
675ed08 Rename all unittest test methods from t_* to test_* so that run_tests.py
mdehoon authored
70 runner = unittest.TextTestRunner(verbosity = 2)
71 unittest.main(testRunner=runner)
Something went wrong with that request. Please try again.