- Timestamp:
- 09/18/15 09:44:18 (8 months ago)
- Location:
- mystic
- Files:
-
- 4 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/forward_model.py
r776 r826 118 118 #XXX: addModelNew is a work in progress... 119 119 ''' 120 def addModelNew(self, model, name, outputFilter = Identity, inputChecker =NullChecker):120 def addModelNew(self, model, name, outputFilter=Identity, inputChecker=NullChecker): 121 121 """ 122 122 Adds a forward model factory to the cost factory. … … 199 199 return _ 200 200 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)): 202 202 """ 203 203 Get a cost function that allows simultaneous evaluation of all forward models … … 249 249 x = x + ofilt(Gm(evalpts)) 250 250 ind = ind+n 251 if sigma ==None:251 if sigma is None: 252 252 x = x - observations 253 253 else: -
mystic/mystic/svctools.py
r776 r826 26 26 27 27 def SupportVectors(alpha, y=None, eps = 0): 28 import mystic.svmtools 29 sv = svm tools.SupportVectors(alpha,eps)30 if y ==None:28 import mystic.svmtools as svm 29 sv = svm.SupportVectors(alpha,eps) 30 if y is None: 31 31 return sv 32 32 else: -
mystic/mystic/termination.py
r784 r826 136 136 137 137 # Factories that give termination conditions 138 def VTR(tolerance = 0.005, target =0.0):138 def VTR(tolerance=0.005, target=0.0): 139 139 """cost of last iteration is < tolerance: 140 140 … … 152 152 return _VTR 153 153 154 def ChangeOverGeneration(tolerance = 1e-6, generations =30):154 def ChangeOverGeneration(tolerance=1e-6, generations=30): 155 155 """change in cost is < tolerance over a number of generations: 156 156 … … 169 169 return _ChangeOverGeneration 170 170 171 def NormalizedChangeOverGeneration(tolerance = 1e-4, generations =10):171 def NormalizedChangeOverGeneration(tolerance=1e-4, generations=10): 172 172 """normalized change in cost is < tolerance over number of generations: 173 173 … … 188 188 return _NormalizedChangeOverGeneration 189 189 190 def CandidateRelativeTolerance(xtol = 1e-4, ftol =1e-4):190 def CandidateRelativeTolerance(xtol=1e-4, ftol=1e-4): 191 191 """absolute difference in candidates is < tolerance: 192 192 … … 214 214 return _CandidateRelativeTolerance 215 215 216 def SolutionImprovement(tolerance =1e-5):216 def SolutionImprovement(tolerance=1e-5): 217 217 """sum of change in each parameter is < tolerance: 218 218 … … 234 234 return _SolutionImprovement 235 235 236 def NormalizedCostTarget(fval = None, tolerance = 1e-6, generations =30):236 def NormalizedCostTarget(fval=None, tolerance=1e-6, generations=30): 237 237 """normalized absolute difference from given cost value is < tolerance: 238 238 (if fval is not provided, then terminate when no improvement over g iterations) … … 248 248 else: info = bool 249 249 hist = inst.energy_history 250 if generations and fval ==None:250 if generations and fval is None: 251 251 lg = len(hist) 252 252 #XXX: throws error when hist is shorter than generations ? … … 254 254 return info(doc) 255 255 return info(null) 256 if not generations and fval ==None: return info(doc)256 if not generations and fval is None: return info(doc) 257 257 if abs(hist[-1]-fval) <= abs(tolerance * fval): return info(doc) 258 258 return info(null) … … 260 260 return _NormalizedCostTarget 261 261 262 def VTRChangeOverGeneration(ftol = 0.005, gtol = 1e-6, generations = 30, 263 target = 0.0): 262 def VTRChangeOverGeneration(ftol=0.005, gtol=1e-6, generations=30, target=0.0): 264 263 """change in cost is < gtol over a number of generations, 265 264 or cost of last iteration is < ftol: … … 281 280 return _VTRChangeOverGeneration 282 281 283 def PopulationSpread(tolerance =1e-6):282 def PopulationSpread(tolerance=1e-6): 284 283 """normalized absolute deviation from best candidate is < tolerance: 285 284 … … 299 298 return _PopulationSpread 300 299 301 def GradientNormTolerance(tolerance = 1e-5, norm =Inf):300 def GradientNormTolerance(tolerance=1e-5, norm=Inf): 302 301 """gradient norm is < tolerance, given user-supplied norm: 303 302 … … 326 325 return _GradientNormTolerance 327 326 328 def EvaluationLimits(generations = None, evaluations =None):327 def EvaluationLimits(generations=None, evaluations=None): 329 328 """number of iterations is > generations, 330 329 or number of function calls is > evaluations: -
mystic/mystic/tools.py
r807 r826 35 35 - partial: generate a function where some input has fixed values 36 36 - 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 37 40 - unpair: convert a 1D array of N pairs to two 1D arrays of N values 38 41 - src: extract source code from a python code object … … 561 564 func.__doc__ = f.__doc__ 562 565 func.mask = mask 566 return func 567 return dec 568 569 570 def 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 581 def supressed(tol=1e-8, exit=False, clip=True): 582 """generate a function, where values less than tol are supressed 583 584 For 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 614 def 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__ 563 627 return func 564 628 return dec
Note: See TracChangeset
for help on using the changeset viewer.