Changeset 173
- Timestamp:
- 05/05/08 16:21:36 (7 months ago)
- Files:
-
- projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf (modified) (3 diffs)
- projects/AsynCluster/trunk/svpmc/model.py (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/params.py (modified) (2 diffs)
- projects/AsynCluster/trunk/svpmc/pmc.py (modified) (7 diffs)
- projects/AsynCluster/trunk/svpmc/project.py (modified) (5 diffs)
- projects/AsynCluster/trunk/svpmc/test/svpmc.conf (added)
- projects/AsynCluster/trunk/svpmc/test/test_pmc.py (modified) (1 diff)
- projects/AsynCluster/trunk/svpmc/test/test_project.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf
r171 r173 57 57 a Distribution Loc Scale a b 58 58 ------------------------------------------------------------------------------- 59 : beta 0.00 0.0 4 2 459 : beta 0.00 0.005 2 5 60 60 61 61 … … 73 73 d Distribution Loc Scale a b 74 74 ------------------------------------------------------------------------------- 75 : beta 0.0 0. 3 2 375 : beta 0.0 0.4 2 2 76 76 77 77 … … 89 89 fr Distribution Loc Scale a b 90 90 ------------------------------------------------------------------------------- 91 : beta 0.0 0.9 2 391 : beta 0.0 0.9 2 2 92 92 93 93 94 94 g Distribution Loc Scale a b 95 95 ------------------------------------------------------------------------------- 96 : beta -0.9 1.8 2296 : beta -0.9 1.8 3 2 projects/AsynCluster/trunk/svpmc/model.py
r172 r173 44 44 """ 45 45 remoteMode = False 46 timeout = 4046 timeout = 80 47 47 48 48 nodecode = """ projects/AsynCluster/trunk/svpmc/params.py
r172 r173 348 348 return paramContainer 349 349 350 def proposal(self, paramContainer, wiggle): 350 # <-- TODO -> 351 def proposal(self, paramContainer, v, vMixInfo): 351 352 """ 352 353 Returns a new instance of L{ParameterContainer} with a random-walk 353 354 proposal based on the supplied I{paramContainer}, my priors, and the 354 supplied I{wiggle}.355 supplied jump deviation I{v}. 355 356 356 357 The returned parameter container object will have a copy of the 357 358 supplied instance's latent parameter array I{z} and new values of I{Lp} and 358 359 I{Lj} corresponding to the proposal. 360 361 The value of I{Lj} is 359 362 """ 360 363 if wiggle < 0.0: … … 379 382 break 380 383 else: 381 print "\nWARNING: Failed to generate a valid proposal"384 #print "\nWARNING: Failed to generate a valid proposal" 382 385 return paramContainer 383 386 Lp += s.log(pPriors).sum() projects/AsynCluster/trunk/svpmc/pmc.py
r172 r173 47 47 self.rMax = N - self.rMin*(self.P-1) 48 48 self.updateAllocations() 49 50 def allocations(self): 51 """ 52 Returns a list of 2-tuples containing each jump deviation and the 53 number of population members currently allocated to it. 54 """ 55 result = [] 56 for k, v in enumerate(self.V): 57 result.append((v, self.R[k])) 58 return result 49 59 50 60 def subsetIndex(self, k): … … 186 196 def weight(paramContainer): 187 197 if s.isfinite(paramContainer.Lx): 188 L = paramContainer.Lx + paramContainer.Lp - paramContainer.Lj 198 Lj = 199 L = paramContainer.Lx + paramContainer.Lp - Lj 189 200 print "-+"[int(L>-1000)], 190 201 else: … … 213 224 214 225 @defer.deferredGenerator 215 def run(self, N_iter , V=None):216 """ 217 Does a PMC run with I{N_iter} iterations and the s upplied sequence I{V}218 of jump deviations. The portion of the population members allocated to219 each jump variance will go up or down, depending on the performance for220 that setting.226 def run(self, N_iter): 227 """ 228 Does a PMC run with I{N_iter} iterations and the sequence of jump 229 deviations in my I{V} attribute. The portion of the population members 230 allocated to each jump variance will go up or down, depending on the 231 performance for that setting. 221 232 222 233 Returns a deferred that fires when the run is done. No output value is … … 226 237 @param N_iter: The number of iterations to produce after burn-in. 227 238 228 @param V: A sequence of variance values to use instead of the default.229 230 239 """ 231 240 if self.socket: … … 233 242 yield wfd; wfd.getResult() 234 243 N_members = self.pm.m 235 # The variance settings (default of 6) 236 if V is None: 237 V = self.V 238 allocator = Allocator(N_members, V) 244 allocator = Allocator(N_members, self.V) 239 245 # Initialize some arrays 240 246 X = self.initialPopulation(N_members) … … 245 251 print "\n%03d:" % (i+1,), 246 252 dList, resultList = [], [] 253 self.mixInfo = allocator.allocations() 247 254 for v, I, d in allocator.assembler(resultList): 248 255 d.addCallback(lambda _: self.weightedProposals(X[I], v)) … … 252 259 if i > 0: 253 260 self.pm.writeParams(X) 261 # "Wait" here for all the proposals to be evaluated 254 262 wfd = defer.waitForDeferred(defer.DeferredList(dList)) 255 263 yield wfd 256 264 wfd.getResult() 257 265 XP, W = resultList 258 #print "\n %s" % \ 259 # (" ".join(["%9d" % ii for ii in s.arange(N_members)]),) 260 #print "W: %s" % (" ".join(["%+9.2g" % w for w in W]),) 261 W = W[s.isfinite(W).nonzero()[0]] 262 if len(W): 266 I0 = s.isfinite(W).nonzero()[0] 267 if len(I0): 263 268 # Resample everything together 264 I = self.resampler(W, N_members) 265 X = XP[I] 266 #print "I: %s\n" % (" ".join(["%9d" % ii for ii in I]),) 269 I1 = self.resampler(W[I0], N_members) 270 X = XP[I0][I1] 267 271 allocator.updateAllocations(I) 268 272 else: 269 273 # Nothing in the proposed population had any plausibility 270 274 allocator.updateAllocations() 271 #print "I []"272 275 wfd = defer.waitForDeferred(self.pm.done()) 273 276 yield wfd projects/AsynCluster/trunk/svpmc/project.py
r172 r173 42 42 def shutdown(self): 43 43 return self.defer.succeed(None) 44 45 46 class CDF_Wrapper(object): 47 """ 48 With python2.5, as of 2008-05-01, accessing a NetCDF variable object with a 49 slice raises a 'Floating Point Exception'! So I am a wrapper for the poor 50 vulnerable little thing... 51 52 B{YAGNI Strikes Again!!!!} 53 54 Just installed the latest development version (2.7.8) of Scientific from 55 http://sourcesup.cru.fr/frs/download.php/1835/ScientificPython-2.7.8.tar.gz 56 and problem solved. 57 """ 58 class VariableDict_Wrapper(object): 59 """ 60 I emulate the I{variables} dict-like object of an open NetCDF file 61 object. 62 """ 63 class Variable_Wrapper(object): 64 """ 65 I emulate a single variable of an open NetCDF file object. 66 """ 67 def __init__(self, var, *dims): 68 self.var = var 69 self.dims = dims 70 71 def __getitem__(self, key): 72 # TODO 73 pass 74 75 def __setitem__(self, key, value): 76 print "SI", key, value 77 78 79 def __init__(self, variables, dimensions): 80 self._variables = {} 81 self.variables = variables 82 self.dimensions = dimensions 83 84 def __getitem__(self, name): 85 if name not in self._variables: 86 var = self.variables[name] 87 dims = [self.dimensions[dimName] for dimName in var.dimensions] 88 self._variables[name] = self.Variable_Wrapper(var, *dims) 89 return self._variables[name] 90 91 92 def __init__(self, cdf): 93 self.cdf = cdf 94 self.variables = self.VariableDict_Wrapper( 95 self.cdf.variables, self.cdf.dimensions) 96 97 def __getattr__(self, name): 98 return getattr(self.cdf, name) 99 100 def __setattr__(self, name, value): 101 if name not in ('cdf', 'variables'): 102 setattr(self.cdf, name, value) 103 else: 104 object.__setattr__(self, name, value) 44 105 45 106 … … 91 152 self.p, self.n = tsData.shape 92 153 paramTitles, dimensions = self._setupParams() 154 self.mgr = model.ModelManager(self, tsData) 93 155 self._setupCDF( 94 156 os.path.join(specDir, ncFileName), 95 157 tsData, seriesTitles, paramTitles, dimensions) 96 self.mgr = model.ModelManager(self, tsData) 97 158 # Write the observation data 159 for k, title in enumerate(seriesTitles): 160 obs = self.cdf.variables['observations'] 161 obs[k,:] = tsData[k,:].astype('f') 162 setattr(obs, "series_%02d" % (k,), title) 163 98 164 def _parseSpec(self, filePath): 99 165 tables = {} … … 206 272 """ 207 273 """ 208 cdf = self.cdf = NetCDF.NetCDFFile(filePath, 'w') 274 cdf = NetCDF.NetCDFFile(filePath, 'w') 275 self.cdf = cdf 276 #self.cdf = CDF_Wrapper(cdf) 209 277 cdf.title = self.tables['project'][0][0] 210 278 # Dimensions … … 217 285 obs.long_name = "Observations, %d time series " % self.p +\ 218 286 "with %d samples each" % self.n 219 for k, title in enumerate(sTitles):220 obs[k,:] = tsData[k,:].astype('f')221 setattr(obs, "series_%02d" % (k,), title)222 287 # Last-Sample Log-Volatility 223 288 logVol = cdf.createVariable( … … 234 299 # Done setting up, save what we've got thus far 235 300 cdf.sync() 236 return cdf237 301 238 302 def writeParams(self, parameters): projects/AsynCluster/trunk/svpmc/test/test_pmc.py
r169 r173 41 41 def test_init(self): 42 42 self.failUnlessElementsEqual(self.allocator.R, [34, 33, 33]) 43 44 def test_allocations(self): 45 self.failUnlessEqual( 46 self.allocator.allocations(), 47 [(0.1,34), (0.01,33), (0.001,33)]) 43 48 44 49 def test_subsetIndex(self): projects/AsynCluster/trunk/svpmc/test/test_project.py
r168 r173 43 43 44 44 def _parseSpec(self): 45 specFile = os.path.join(self.specDir, " project-spec.txt")45 specFile = os.path.join(self.specDir, "svpmc.conf") 46 46 self.mgr.tables = self.mgr._parseSpec(specFile) 47 47 return self.mgr.tables … … 89 89 tsData, seriesTitles = self.mgr._setupTimeSeries(self.specDir) 90 90 paramTitles, dimensions = self.mgr._setupParams() 91 cdf =self.mgr._setupCDF(91 self.mgr._setupCDF( 92 92 cdfPath, tsData, seriesTitles, paramTitles, dimensions) 93 93 # Make sure we can write to the open file 94 cdf = self.mgr.cdf 94 95 var = cdf.variables['a'] 95 96 x = s.rand(self.mgr.p, 2).astype('f') … … 114 115 projectDir = os.path.dirname(__file__) 115 116 self.cdfPath = os.path.join(projectDir, "svpmc.nc") 116 self.specFile = os.path.join(projectDir, " project-spec.txt")117 self.specFile = os.path.join(projectDir, "svpmc.conf") 117 118 self.mgr = project.ProjectManager(self.specFile, m=5) 118 119
