ó
"ˆIc           @   s«   d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z d e j f d „  ƒ  YZ	 d e j f d „  ƒ  YZ
 e d	 k r§ e j d
 d ƒ Z e j d e ƒ n  d S(   s   General test for the NeuralNetwork libraries.

This exercises various elements of the BackPropagation NeuralNetwork
libraries.
iÿÿÿÿN(   t   TrainingExamplet   ExampleManager(   t   ValidationIncreaseStopt   StopTrainingTestc           B   s   e  Z d  Z d „  Z RS(   s7   Test functionality for stopping training networks.
    c         C   s»   t  d d d d ƒ } d | _ | j d d d ƒ } | d k sK t d	 ƒ ‚ d | _ | j d d d ƒ } | d
 k s t d ƒ ‚ d | _ | j d d d ƒ } | d k s· t d ƒ ‚ d S(   s<   Stop training when the ValidationExamples increase.
        t   max_iterationsi   t   min_iterationsi   g      ð?i   g      ø?i   s8   Did not tell us to stop when validation error increased.i    s9   Told us to stop before we reached the minimum iterations.i   g      à?s9   Did not tell us to stop when reaching maximum iterations.N(   R   t
   last_errort   stopping_criteriat   AssertionError(   t   selft   stoppert   do_stop(    (    s   test_NNGeneral.pyt   test_validation_increase_stop   s    						(   t   __name__t
   __module__t   __doc__R   (    (    (    s   test_NNGeneral.pyR      s   t   ExampleManagerTestc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s@   Tests to make sure the example manager is working properly.
    c         C   s”   d |  _  g  |  _ x{ t |  j  ƒ D]j } g  } x- t d ƒ D] } | j t j d d ƒ ƒ q; Wt j d d ƒ g } |  j j t | | ƒ ƒ q" Wd  S(   Niô  i   i   i   (   t   num_examplest   examplest   ranget   appendt   randomt	   randrangeR    (   R	   t   make_examplet   inputst
   input_maket   outputs(    (    s   test_NNGeneral.pyt   setUp,   s    		c         C   sÔ   t  ƒ  } | j |  j } | j |  j } |  j | | } | j |  j ƒ xƒ | t | j ƒ f | t | j ƒ f | t | j	 ƒ f g D]E \ } } t
 | | ƒ |  j } | d k  s‡ t d | | f ƒ ‚ q‡ Wd S(   s4   Make sure test examples are added properly.
        gš™™™™™¹?s7   Deviation in how examples were added, expect %s, got %sN(   R   t   training_percentR   t   validation_percentt   add_examplesR   t   lent   train_examplest   validation_examplest   test_examplest   absR   (   R	   t   managert   expected_traint   expected_validationt   expected_testt   expectt   actualt   wrong_percent(    (    s   test_NNGeneral.pyt   test_adding_examples6   s    	"c         C   sÍ   t  d d ƒ } | j |  j ƒ t | j ƒ |  j k sC t d ƒ ‚ t  d d ƒ } | j |  j ƒ t | j ƒ |  j k s† t d ƒ ‚ t  d d ƒ } | j |  j ƒ t | j ƒ |  j k sÉ t d ƒ ‚ d S(   sD   Test that we can change how to partition the test examples.
        i    s+   Did not partion correctly to test_examples.g      ð?s.   Did not partition correctly to train_examples.s3   Did not partition correctly to validation_examples.N(	   R   R   R   R   R"   R   R   R    R!   (   R	   R$   (    (    s   test_NNGeneral.pyt   test_partioning_examplesM   s    		(   R   R   R   R   R+   R,   (    (    (    s   test_NNGeneral.pyR   )   s   	
	t   __main__t	   verbosityi   t
   testRunner(   R   R   t   unittestt   Bio.NeuralNetwork.TrainingR    R   t   Bio.NeuralNetwork.StopTrainingR   t   TestCaseR   R   R   t   TextTestRunnert   runnert   main(    (    (    s   test_NNGeneral.pyt   <module>   s   6