Changeset 169

Show
Ignore:
Timestamp:
04/30/08 23:00:30 (8 months ago)
Author:
edsuom
Message:

Putting together svpmc example

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/AsynCluster/trunk/svpmc/pmc.py

    r168 r169  
    2828 
    2929import params 
    30 from sample import resample 
     30from sample import Resampler 
    3131 
    3232 
     
    6262        to that deviation value, and a fresh deferred already fired with 
    6363        C{None}. You must add a callback to the deferred for each iteration 
    64         that returns one or more 1-D arrays of the same length as the yielded 
    65         index array. 
     64        that returns a tuple of 1-D arrays, each being the same length as 
     65        the yielded index array. 
    6666         
    6767        Each returned array for each subset will be assembled back into a 
     
    7070        """ 
    7171        def gotResults(results, I): 
     72            if not isinstance(results, tuple): 
     73                results = (results,) 
    7274            if not resultList: 
    7375                for result in results: 
    74                     if isinstance(result, (int, float)): 
     76                    if isinstance(result, list) and len(result) == len(I): 
     77                        resultList.append(s.empty(self.N)) 
     78                    elif isinstance(result, s.ndarray): 
    7579                        resultList.append(s.empty(self.N)) 
    7680                    else: 
     
    97101        """ 
    98102        if I is None: 
    99             R = self.N * s.ones(self.P) / self.P 
     103            R = s.ones(self.P) / self.P 
    100104        elif hasattr(self, 'II'): 
    101105            R = s.array( 
     
    111115        R = s.clip(s.round_(self.N*R), self.rMin, self.rMax).astype(int) 
    112116        # Twiddle the biggest one as needed to keep sum = Number of members 
    113         R[s.argmax(R)] += - sum(R) 
     117        R[s.argmax(R)] += self.N - sum(R) 
    114118        # Replace the old list and subset index 
    115119        self.R = R 
     
    128132        self.pm = projectManager 
    129133        self.mm = projectManager.mgr 
     134        self.resampler = Resampler() 
    130135     
    131136    def _get_queue(self): 
     
    201206            W[j:j+N_this] = wfd.getResult() 
    202207            j += N_this 
    203         return XP, W 
     208        yield XP, W 
    204209 
    205210    @defer.deferredGenerator 
     
    240245            XP, W = resultList 
    241246            # Resample everything together 
    242             I = resample(W, N_members) 
     247            I = self.resampler(W, N_members) 
    243248            X = XP[I] 
    244249            allocator.updateAllocations(I) 
  • projects/AsynCluster/trunk/svpmc/test/test_pmc.py

    r168 r169  
    2525from zope.interface import implements 
    2626from twisted.internet import defer, interfaces 
    27 from asynqueue import ThreadQueue 
    2827 
    2928import model, pmc 
    30 from sample import resample 
    3129import util 
    3230 
     
    3533 
    3634 
    37 class Mock_ThreadQueue(util.Mock): 
    38     def call(self, func, *args, **kw): 
    39         kw['delay'] = 0.01 
    40         return util.deferToLater(func, *args, **kw) 
    41  
    42  
    43 class Mock_ProjectManager(util.Mock): 
    44     def __init__(self, modelObj): 
    45         self.modelObj = modelObj 
    46         self.threadQueue = Mock_ThreadQueue() 
     35class Test_Allocator(util.TestCase): 
     36    def setUp(self): 
     37        self.N = 100 
     38        self.V = [0.1, 0.01, 0.001] 
     39        self.allocator = pmc.Allocator(self.N, self.V) 
     40 
     41    def test_init(self): 
     42        self.failUnlessElementsEqual(self.allocator.R, [34, 33, 33]) 
     43     
     44    def test_subsetIndex(self): 
     45        R = self.allocator.R = [10, 20, 70] 
     46        self.allocator.Is = s.arange(sum(R)) 
     47        I_all = s.array([]) 
     48        for k, r in enumerate(R): 
     49            I = self.allocator.subsetIndex(k) 
     50            self.failUnlessEqual(len(I), r) 
     51            self.failUnless(min(I) >= 0) 
     52            self.failUnless(max(I) < self.N) 
     53            self.failIf(s.setmember1d(I, I_all).any()) 
     54            I_all = s.concatenate([I_all, I]) 
     55        self.failUnlessEqual(len(I_all), self.N) 
     56 
     57    def test_assembler(self): 
     58        def check(null): 
     59            self.failUnlessEqual(len(resultList), 2) 
     60            x, y = resultList 
     61            self.failUnlessEqual(s.diff(x).min(), 1) 
     62            self.failUnlessEqual(s.diff(x).max(), 1) 
     63            y0, y1 = divmod(y, 1) 
     64            self.failUnlessElementsEqual(y0, x) 
     65            self.failUnlessAlmostEqual(y1.max(), self.V[0]) 
     66            self.failUnlessAlmostEqual(y1.min(), self.V[-1]) 
     67         
     68        X = s.arange(self.N) 
     69        dList, resultList = [], [] 
     70        for v, I, d in self.allocator.assembler(resultList): 
     71            d.addCallback(lambda _: (X[I], X[I]+v)) 
     72            dList.append(d) 
     73        return defer.DeferredList(dList).addCallback(check) 
     74 
     75 
     76class Mock_ModelManager(util.Mock): 
     77    def __init__(self): 
    4778        self.resetCalls() 
    4879 
     
    5182        self.callsPending = [] 
    5283        self.stepCounter = 0 
    53          
    54     def likelihood(self, paramContainer, wiggle, remote=False, local=False): 
     84 
     85    def likelihood(self, paramContainer, sigma, remote=False, local=False): 
    5586        def done(results): 
    5687            if self.callsPending: 
     
    6899 
    69100 
    70 class BaseTC(util.TestCase): 
     101class Mock_PriorContainer(util.Mock): 
     102    def __init__(self): 
     103        self.callCounter = 0 
     104     
     105    def new(self): 
     106        self.callCounter += 1 
     107        return util.Mock_ParameterContainer(self.callCounter) 
     108 
     109    def proposal(self, paramContainer, sigma): 
     110        # TODO 
     111        pass 
     112 
     113 
     114class Mock_ProjectManager(util.Mock): 
     115    def __init__(self, m=100): 
     116        self.m = m 
     117        self.mgr = Mock_ModelManager() 
     118        self.priors = Mock_PriorContainer() 
     119         
     120    def writeParams(paramArray): 
     121        pass 
     122 
     123    def done(self): 
     124        pass 
     125 
     126 
     127class Test_PMC(util.TestCase): 
    71128    def setUp(self): 
    72         self.mgr = model.ModelManager(projectManager) 
    73         self.mgr.caller = Mock_ModelCaller(self.mgr.modelMap) 
    74         if self.__class__ == Test_MCMC: 
    75             self.mc = mcmc.MCMC(self.mgr) 
    76         elif self.__class__ == Test_DE_MCMC: 
    77             self.mc = mcmc.DE_MCMC(self.mgr) 
    78         else: 
    79             self.mc = mcmc.PMC(self.mgr) 
    80      
    81     def tearDown(self): 
    82         self.consumer = None 
    83         return util.TestCase.tearDown(self) 
    84  
    85     def _analyzeSamples(self, null, delay=5): 
    86         from study.rinterface import Rinterface 
    87         ri = Rinterface() 
    88         ri.packages.append('coda') 
    89         mo = ri.rc('mcmc', s.concatenate(self.consumer.X)) 
    90         ri.rc('plot', mo) 
    91         return util.deferToLater(delay=delay) 
    92  
    93  
    94 class Test_PMC(BaseTC): 
    95     def test_subsetIndex(self): 
    96         R = self.mc.R = [10,20,15,5] 
    97         self.mc.Is = s.arange(sum(R)) 
    98         I_all = s.array([]) 
    99         for k, r in enumerate(R): 
    100             I = self.mc.subsetIndex(k) 
    101             self.failUnlessEqual(len(I), r) 
    102             self.failUnless(min(I) >= 0) 
    103             self.failUnless(max(I) < 200) 
    104             self.failIf(s.setmember1d(I, I_all).any()) 
    105             I_all = s.concatenate([I_all, I]) 
    106  
     129        self.pm = Mock_ProjectManager() 
     130        self.pmc = pmc.PMC(self.pm) 
     131     
    107132    def test_proposals(self): 
    108133        N = 5