Back to index

python-biopython  1.60
test_NNGeneral.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """General test for the NeuralNetwork libraries.
00003 
00004 This exercises various elements of the BackPropagation NeuralNetwork
00005 libraries.
00006 """
00007 # standard library
00008 import random
00009 import unittest
00010 
00011 # local stuff
00012 from Bio.NeuralNetwork.Training import TrainingExample, ExampleManager
00013 from Bio.NeuralNetwork.StopTraining import ValidationIncreaseStop
00014 
00015 
00016 class StopTrainingTest(unittest.TestCase):
00017     """Test functionality for stopping training networks.
00018     """
00019     def test_validation_increase_stop(self):
00020         """Stop training when the ValidationExamples increase.
00021         """
00022         stopper = ValidationIncreaseStop(max_iterations = 20,
00023                                          min_iterations = 2)
00024 
00025         stopper.last_error = 1.0
00026         do_stop = stopper.stopping_criteria(5, 1.0, 1.5)
00027         assert do_stop == 1, \
00028                "Did not tell us to stop when validation error increased."
00029 
00030         stopper.last_error = 1.0
00031         do_stop = stopper.stopping_criteria(1, 1.0, 1.5)
00032         assert do_stop == 0, \
00033                "Told us to stop before we reached the minimum iterations."
00034 
00035         stopper.last_error = 1.0
00036         do_stop = stopper.stopping_criteria(25, 1.0, 0.5)
00037         assert do_stop == 1, \
00038                "Did not tell us to stop when reaching maximum iterations."
00039 
00040 
00041 class ExampleManagerTest(unittest.TestCase):
00042     """Tests to make sure the example manager is working properly.
00043     """
00044     def setUp(self):
00045         self.num_examples = 500
00046         self.examples = []
00047         for make_example in range(self.num_examples):
00048             inputs = []
00049             for input_make in range(3):
00050                 inputs.append(random.randrange(1, 7))
00051             outputs = [random.randrange(1, 7)]
00052             self.examples.append(TrainingExample(inputs, outputs))
00053 
00054     def test_adding_examples(self):
00055         """Make sure test examples are added properly.
00056         """
00057         manager = ExampleManager()
00058 
00059         # figure out the expected number of examples in each category
00060         expected_train = manager.training_percent * self.num_examples
00061         expected_validation = manager.validation_percent * self.num_examples
00062         expected_test = self.num_examples - expected_train \
00063                         - expected_validation
00064 
00065         manager.add_examples(self.examples)
00066 
00067         for expect, actual in [(expected_train, len(manager.train_examples)),
00068                                (expected_validation,
00069                                 len(manager.validation_examples)),
00070                                (expected_test, len(manager.test_examples))]:
00071             
00072             wrong_percent = abs(expect - actual) / self.num_examples
00073             assert wrong_percent < .1, \
00074                    "Deviation in how examples were added, expect %s, got %s" \
00075                    % (expect, actual)
00076         
00077     def test_partioning_examples(self):
00078         """Test that we can change how to partition the test examples.
00079         """
00080         manager = ExampleManager(0, 0)
00081         manager.add_examples(self.examples)
00082         assert len(manager.test_examples) == self.num_examples, \
00083                "Did not partion correctly to test_examples."
00084 
00085         manager = ExampleManager(1.0, 0)
00086         manager.add_examples(self.examples)
00087         assert len(manager.train_examples) == self.num_examples, \
00088                "Did not partition correctly to train_examples."
00089 
00090         manager = ExampleManager(0, 1.0)
00091         manager.add_examples(self.examples)
00092         assert len(manager.validation_examples) == self.num_examples, \
00093                "Did not partition correctly to validation_examples."
00094 
00095 if __name__ == "__main__":
00096     runner = unittest.TextTestRunner(verbosity = 2)
00097     unittest.main(testRunner=runner)