<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import itertools <EOL> import signal <EOL> import sys <EOL> import time <EOL> from climin . mathadapt import isnan <EOL> from . . compat import basestring <EOL> class AfterNIterations ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , max_iter ) : <EOL> """<STR_LIT>""" <EOL> self . max_iter = max_iter <EOL> def __call__ ( self , info ) : <EOL> return info [ '<STR_LIT>' ] >= self . max_iter - <NUM_LIT:1> <EOL> class ModuloNIterations ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n ) : <EOL> """<STR_LIT>""" <EOL> self . n = n <EOL> def __call__ ( self , info ) : <EOL> return info [ '<STR_LIT>' ] % self . n == <NUM_LIT:0> <EOL> class TimeElapsed ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sec ) : <EOL> """<STR_LIT>""" <EOL> self . sec = sec <EOL> self . start = time . time ( ) <EOL> def __call__ ( self , info ) : <EOL> if '<STR_LIT>' in info : <EOL> return info [ '<STR_LIT>' ] > self . sec <EOL> else : <EOL> return time . time ( ) - self . start > self . sec <EOL> def All ( criterions ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , criterions ) : <EOL> self . criterions = criterions <EOL> def __call__ ( self , info ) : <EOL> return all ( c ( info ) for c in self . criterions ) <EOL> class Any ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , criterions ) : <EOL> self . criterions = criterions <EOL> def __call__ ( self , info ) : <EOL> return any ( c ( info ) for c in self . criterions ) <EOL> class NotBetterThanAfter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , minimal , after , key = '<STR_LIT>' ) : <EOL> self . minimal = minimal <EOL> self . after = after <EOL> self . key = key <EOL> def __call__ ( self , info ) : <EOL> return info [ '<STR_LIT>' ] > self . after and info [ self . key ] >= self . minimal <EOL> class IsNaN ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keys = [ ] ) : <EOL> self . keys = keys <EOL> def __call__ ( self , info ) : <EOL> return any ( [ isnan ( info . get ( key , <NUM_LIT:0> ) ) for key in self . keys ] ) <EOL> class Patience ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func_or_key , initial , grow_factor = <NUM_LIT:1.> , grow_offset = <NUM_LIT:0.> , <EOL> threshold = <NUM_LIT> ) : <EOL> if grow_factor == <NUM_LIT:1> and grow_offset == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . func_or_key = func_or_key <EOL> self . patience = initial <EOL> self . grow_factor = grow_factor <EOL> self . grow_offset = grow_offset <EOL> self . threshold = threshold <EOL> self . best_iter = <NUM_LIT:0> <EOL> self . best_loss = float ( '<STR_LIT>' ) <EOL> self . count = itertools . count ( ) <EOL> def __call__ ( self , info ) : <EOL> i = info [ '<STR_LIT>' ] <EOL> if isinstance ( self . func_or_key , basestring ) : <EOL> loss = info [ self . func_or_key ] <EOL> elif isinstance ( self . func_or_key , ( tuple , list ) ) : <EOL> loss = info <EOL> for j in self . func_or_key : <EOL> loss = loss [ j ] <EOL> else : <EOL> loss = self . func_or_key ( ) <EOL> if loss < self . best_loss : <EOL> if ( self . best_loss - loss ) > self . threshold and i > <NUM_LIT:0> : <EOL> self . patience = max ( i * self . grow_factor + self . grow_offset , <EOL> self . patience ) <EOL> self . best_iter = i <EOL> self . best_loss = loss <EOL> return i >= self . patience <EOL> class OnUnixSignal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sig = signal . SIGINT ) : <EOL> """<STR_LIT>""" <EOL> self . sig = sig <EOL> self . stopped = False <EOL> self . _register ( ) <EOL> def _register ( self ) : <EOL> self . prev_handler = signal . signal ( self . sig , self . handler ) <EOL> def handler ( self , signal , frame ) : <EOL> self . stopped = True <EOL> def __call__ ( self , info ) : <EOL> res , self . stopped = self . stopped , False <EOL> return res <EOL> def __del__ ( self ) : <EOL> signal . signal ( self . sig , self . prev_handler ) <EOL> def __setstate__ ( self , dct ) : <EOL> self . __dict__ . update ( dct ) <EOL> self . _register ( ) <EOL> class OnWindowsSignal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sig = None ) : <EOL> """<STR_LIT>""" <EOL> self . sig = [ <NUM_LIT:0> , <NUM_LIT:1> ] if sig is None else sig <EOL> self . stopped = False <EOL> self . _register ( ) <EOL> def _register ( self ) : <EOL> import win32api <EOL> win32api . SetConsoleCtrlHandler ( self . handler , <NUM_LIT:1> ) <EOL> def handler ( self , ctrl_type ) : <EOL> if ctrl_type in self . sig : <EOL> self . stopped = True <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def __call__ ( self , info ) : <EOL> res , self . stopped = self . stopped , False <EOL> return res <EOL> def __setstate__ ( self , dct ) : <EOL> self . __dict__ . update ( dct ) <EOL> self . _register ( ) <EOL> OnSignal = OnWindowsSignal if sys . platform == '<STR_LIT:win32>' else OnUnixSignal <EOL> def never ( info ) : <EOL> return False <EOL> def always ( info ) : <EOL> return True </s>
