Changeset 225


Ignore:
Timestamp:
05/13/10 09:27:25 (6 years ago)
Author:
mmckerns
Message:

added LoggingSow?, a monitor that writes to a file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/tools.py

    r219 r225  
    2323    - Sow: A class whose instances are callable (to be used as monitors) 
    2424    - VerboseSow: A verbose version of the basic Sow 
     25    - LoggingSow: A version of the basic Sow that logs to a file 
    2526    - CustomSow: A customizable 'n-variable' version of the basic Sow 
    2627    - random_seed: sets the seed for calls to 'random()' 
     
    171172            print "Generation %d has best fit parameters:\n %s" % (self._step, x) 
    172173        self._step += 1 
     174        return 
     175    pass 
     176 
     177class LoggingSow(Sow): 
     178    """A version of the basic Sow that writes to a file at specified intervals. 
     179 
     180Logs ChiSq and parameters to a file every 'interval' 
     181    """ 
     182    import numpy 
     183    def __init__(self, interval=1, filename='log.txt', new=False): 
     184        import datetime 
     185        Sow.__init__(self) 
     186        self._filename = filename 
     187        self._step = 0 
     188        self._yinterval = interval 
     189        self._xinterval = interval 
     190        if new: ind = 'w' 
     191        else: ind = 'a' 
     192        self._file = open(self._filename,ind) 
     193        self._file.write("%s\n" % datetime.datetime.now().ctime() ) 
     194        self._file.write("_#_  __ChiSq__  __params__\n") 
     195        self._file.close() 
     196        return 
     197    def __call__(self, x, y): 
     198        self._file = open(self._filename,'a') 
     199        from numpy import ndarray 
     200        Sow.__call__(self, x, y) 
     201        if isinstance(y,(list,ndarray)): 
     202            y = y[0] #XXX: get the "best" fit... which should be in y[0] 
     203        if isinstance(x[0],(list,ndarray)): #XXX: x should always be iterable 
     204            x = x[0] #XXX: get the "best" fit... which should be in x[0] 
     205        if int(self._step % self._yinterval) == 0: 
     206            self._file.write(" %d   %f   %s\n" % (self._step, y, x)) 
     207        self._step += 1 
     208        self._file.close() 
    173209        return 
    174210    pass 
Note: See TracChangeset for help on using the changeset viewer.