Changeset 826


Ignore:
Timestamp:
09/18/15 09:44:18 (8 months ago)
Author:
mmckerns
Message:

remove old equals None; add suppressed and clipped; fix import in svctools

Location:
mystic
Files:
4 edited
2 moved

Legend:

Unmodified
Added
Removed
  • mystic/mystic/forward_model.py

    r776 r826  
    118118    #XXX: addModelNew is a work in progress... 
    119119    ''' 
    120     def addModelNew(self, model, name, outputFilter = Identity, inputChecker = NullChecker): 
     120    def addModelNew(self, model, name, outputFilter=Identity, inputChecker=NullChecker): 
    121121        """ 
    122122Adds a forward model factory to the cost factory. 
     
    199199        return _ 
    200200 
    201     def getCostFunction(self, evalpts, observations, sigma = None, metric = lambda x: sum(x*x)): 
     201    def getCostFunction(self, evalpts, observations, sigma=None, metric=lambda x: sum(x*x)): 
    202202        """ 
    203203Get a cost function that allows simultaneous evaluation of all forward models 
     
    249249                    x = x + ofilt(Gm(evalpts))  
    250250                ind = ind+n 
    251             if sigma == None: 
     251            if sigma is None: 
    252252                x = x - observations 
    253253            else: 
  • mystic/mystic/svctools.py

    r776 r826  
    2626 
    2727def SupportVectors(alpha, y=None, eps = 0): 
    28     import mystic.svmtools 
    29     sv = svmtools.SupportVectors(alpha,eps) 
    30     if y == None: 
     28    import mystic.svmtools as svm 
     29    sv = svm.SupportVectors(alpha,eps) 
     30    if y is None: 
    3131        return sv 
    3232    else: 
  • mystic/mystic/termination.py

    r784 r826  
    136136 
    137137# Factories that give termination conditions 
    138 def VTR(tolerance = 0.005, target = 0.0): 
     138def VTR(tolerance=0.005, target=0.0): 
    139139    """cost of last iteration is < tolerance: 
    140140 
     
    152152    return _VTR 
    153153 
    154 def ChangeOverGeneration(tolerance = 1e-6, generations = 30): 
     154def ChangeOverGeneration(tolerance=1e-6, generations=30): 
    155155    """change in cost is < tolerance over a number of generations: 
    156156 
     
    169169    return _ChangeOverGeneration 
    170170 
    171 def NormalizedChangeOverGeneration(tolerance = 1e-4, generations = 10): 
     171def NormalizedChangeOverGeneration(tolerance=1e-4, generations=10): 
    172172    """normalized change in cost is < tolerance over number of generations: 
    173173 
     
    188188    return _NormalizedChangeOverGeneration 
    189189               
    190 def CandidateRelativeTolerance(xtol = 1e-4, ftol = 1e-4): 
     190def CandidateRelativeTolerance(xtol=1e-4, ftol=1e-4): 
    191191    """absolute difference in candidates is < tolerance: 
    192192 
     
    214214    return _CandidateRelativeTolerance 
    215215 
    216 def SolutionImprovement(tolerance = 1e-5):   
     216def SolutionImprovement(tolerance=1e-5):   
    217217    """sum of change in each parameter is < tolerance: 
    218218 
     
    234234    return _SolutionImprovement 
    235235 
    236 def NormalizedCostTarget(fval = None, tolerance = 1e-6, generations = 30): 
     236def NormalizedCostTarget(fval=None, tolerance=1e-6, generations=30): 
    237237    """normalized absolute difference from given cost value is < tolerance: 
    238238(if fval is not provided, then terminate when no improvement over g iterations) 
     
    248248        else: info = bool 
    249249        hist = inst.energy_history 
    250         if generations and fval == None: 
     250        if generations and fval is None: 
    251251            lg = len(hist) 
    252252            #XXX: throws error when hist is shorter than generations ? 
     
    254254                return info(doc) 
    255255            return info(null) 
    256         if not generations and fval == None: return info(doc) 
     256        if not generations and fval is None: return info(doc) 
    257257        if abs(hist[-1]-fval) <= abs(tolerance * fval): return info(doc) 
    258258        return info(null) 
     
    260260    return _NormalizedCostTarget 
    261261 
    262 def VTRChangeOverGeneration(ftol = 0.005, gtol = 1e-6, generations = 30, 
    263                                                             target = 0.0): 
     262def VTRChangeOverGeneration(ftol=0.005, gtol=1e-6, generations=30, target=0.0): 
    264263    """change in cost is < gtol over a number of generations, 
    265264or cost of last iteration is < ftol: 
     
    281280    return _VTRChangeOverGeneration 
    282281 
    283 def PopulationSpread(tolerance = 1e-6): 
     282def PopulationSpread(tolerance=1e-6): 
    284283    """normalized absolute deviation from best candidate is < tolerance: 
    285284 
     
    299298    return _PopulationSpread 
    300299 
    301 def GradientNormTolerance(tolerance = 1e-5, norm = Inf):  
     300def GradientNormTolerance(tolerance=1e-5, norm=Inf):  
    302301    """gradient norm is < tolerance, given user-supplied norm: 
    303302 
     
    326325    return _GradientNormTolerance 
    327326 
    328 def EvaluationLimits(generations = None, evaluations = None): 
     327def EvaluationLimits(generations=None, evaluations=None): 
    329328    """number of iterations is > generations, 
    330329or number of function calls is > evaluations: 
  • mystic/mystic/tools.py

    r807 r826  
    3535    - partial: generate a function where some input has fixed values 
    3636    - insert_missing: return a sequence with the 'missing' elements inserted 
     37    - clipped: generate a function where values outside of bounds are clipped 
     38    - supressed: generate a function where values less than tol are supressed 
     39    - supress: supress small values less than tol 
    3740    - unpair: convert a 1D array of N pairs to two 1D arrays of N values 
    3841    - src: extract source code from a python code object 
     
    561564        func.__doc__ = f.__doc__ 
    562565        func.mask = mask 
     566        return func 
     567    return dec 
     568 
     569 
     570def supress(x, tol=1e-8, clip=True): 
     571    """supress small values less than tol""" 
     572    from numpy import asarray, abs 
     573    x = asarray(list(x)) 
     574    mask = abs(x) < tol 
     575    if not clip: 
     576        # preserve sum by spreading supressed values to the non-zero elements 
     577        x[mask==False] = (x + sum(x[mask])/(len(mask)-sum(mask)))[mask==False] 
     578    x[mask] = 0.0 
     579    return x.tolist() 
     580 
     581def supressed(tol=1e-8, exit=False, clip=True): 
     582    """generate a function, where values less than tol are supressed 
     583 
     584For example, 
     585    >>> @supressed(1e-8) 
     586    ... def square(x): 
     587    ...     return [i**2 for i in x] 
     588    ...  
     589    >>> square([1e-8, 2e-8, 1e-9]) 
     590    [1.00000000e-16, 4.00000000e-16, 0.00000000e+00] 
     591    >>>  
     592    >>> from mystic.math.measures import normalize 
     593    >>> @supressed(1e-8, exit=True, clip=False) 
     594    ... def norm(x): 
     595    ...     return normalize(x, mass=1) 
     596    ...  
     597    >>> norm([1e-8, 2e-8, 1e-16, 5e-9]) 
     598    [0.28571428585034014, 0.5714285707482993, 0.0, 0.14285714340136055] 
     599    >>> sum(_) 
     600    1.0 
     601    """ 
     602    def dec(f): 
     603        if exit: 
     604            def func(x, *args, **kwds): 
     605                return supress(f(x, *args, **kwds), tol, clip) 
     606        else: 
     607            def func(x, *args, **kwds): 
     608                return f(supress(x, tol, clip), *args, **kwds) 
     609        func.__wrapped__ = f 
     610        func.__doc__ = f.__doc__ 
     611        return func 
     612    return dec 
     613 
     614def clipped(min=None, max=None, exit=False): 
     615    """generate a function, where values outside of bounds are clipped 
     616    """ 
     617    from numpy import clip 
     618    def dec(f): 
     619        if exit: 
     620            def func(x, *args, **kwds): 
     621                return clip(f(x, *args, **kwds), min, max).tolist() 
     622        else: 
     623            def func(x, *args, **kwds): 
     624                return f(clip(x, min, max).tolist(), *args, **kwds) 
     625        func.__wrapped__ = f 
     626        func.__doc__ = f.__doc__ 
    563627        return func 
    564628    return dec 
Note: See TracChangeset for help on using the changeset viewer.