@@ -52,6 +52,11 @@ def __init__(self, files, args):
5252 self ._recode_nonseg (args .nonseg_cutoff )
5353
5454
55+ @property
56+ def ns (self ):
57+ return np .array ([sum (c .n ) for c in self ._contigs ])
58+
59+
5560 def _init_optimizer (self , outdir , algorithm , xtol , ftol , single ):
5661 self ._optimizer = self ._OPTIMIZER_CLS (self , algorithm , xtol , ftol , single )
5762 if outdir :
@@ -88,7 +93,6 @@ def _load_data(self, files):
8893 assert c .data .shape [1 ] == 1 + 3 * len (c .n )
8994 logger .debug (c )
9095 logger .info ("%d population%s" , self .npop , "" if self .npop == 1 else "s" )
91- ns = self ._ns = np .array ([sum (c .n ) for c in self ._contigs ])
9296
9397 def _validate_data (self ):
9498 for c in self ._contigs :
@@ -147,7 +151,7 @@ def _perform_thinning(self, thinning):
147151 if isinstance (thinning , int ):
148152 thinning = np .array ([thinning ] * len (self ._contigs ))
149153 if thinning is None :
150- thinning = (1000 * np .log (2 + self ._ns )).astype ("int" ) # 500 * ns
154+ thinning = (1000 * np .log (2 + self .ns )).astype ("int" ) # 500 * ns
151155 thinning [npop > 1 ] = 0
152156 if np .any (thinning > 1 ):
153157 logger .info ("Thinning..." )
@@ -237,7 +241,7 @@ def _calculate_t1_tK(self, args):
237241 Ne = self ._watterson / self ._theta
238242 logger .debug ("Ne: %f" , Ne )
239243 Ne *= 2 * self ._N0
240- n = 2 + max (self ._ns )
244+ n = 2 + max (self .ns )
241245 t1 = args .t1 or 200 + np .log (.9 ) / (- n * (n - 1 ) / 2 ) * Ne
242246 if t1 <= 0 :
243247 logger .error ("--t1 should be >0" )
@@ -287,7 +291,7 @@ def _init_inference_manager(self, polarization_error):
287291 im .rho = self ._rho
288292 im .alpha = self ._alpha = .01
289293 self ._ims [pid ] = im
290- self ._max_n = np .max (list (max_n .values ()), axis = 0 )
294+ self ._max_n = np .max (list (map ( sum , max_n .values () )), axis = 0 )
291295 self ._model .randomize ()
292296
293297 @property
@@ -366,9 +370,9 @@ def npop(self):
366370
367371 def dump (self , filename ):
368372 'Dump result of this analysis to :filename:.'
369- d = {'theta' : self ._theta , 'rho' : self ._rho }
373+ d = {'theta' : self ._theta , 'rho' : self ._rho , 'alpha' : self . _alpha }
370374 d ['model' ] = self .model .to_dict ()
371- d ['hidden_states' ] = {k : v . tolist ( ) for k , v in self ._hidden_states .items ()}
375+ d ['hidden_states' ] = {k : list ( v ) for k , v in self ._hidden_states .items ()}
372376 json .dump (d , open (filename + ".json" , "wt" ), sort_keys = True , indent = 4 )
373377
374378
@@ -456,18 +460,11 @@ def _init_model(self, N0, spline_class):
456460 self ._y0 = y0 = self ._het / (2. * self ._theta )
457461 logger .debug ("Long term avg. effective population size: %f" , y0 )
458462 y0 = np .log (y0 )
459- if self .npop == 1 :
460- self ._model = SMCModel (
461- self ._knots , self ._N0 ,
462- spline_class , self ._populations [0 ])
463- self ._model [:] = y0
464- else :
465- split = self .rescale (.5 * tK ) # just pick the midpoint as a starting value.
466- mods = []
467- for pid in self ._populations :
468- mods .append (SMCModel (self ._knots , self ._N0 , spline_class , pid ))
469- mods [- 1 ][:] = y0
470- self ._model = SMCTwoPopulationModel (mods [0 ], mods [1 ], split )
463+ assert self .npop == 1
464+ self ._model = SMCModel (
465+ self ._knots , self ._N0 ,
466+ spline_class , self ._populations [0 ])
467+ self ._model [:] = y0
471468
472469 def _preinitialize (self ):
473470 args = self ._args
@@ -530,7 +527,7 @@ def __init__(self, files, args):
530527 self ._perform_thinning (args .thinning )
531528 # Further initialization
532529 self ._init_inference_manager (args .polarization_error )
533- self ._init_optimizer (args .outdir , args .algorithm , args .xtol , args .ftol )
530+ self ._init_optimizer (args .outdir , args .algorithm , args .xtol , args .ftol , True )
534531 self ._niter = 1
535532
536533 def _validate_data (self ):
@@ -556,6 +553,7 @@ def _init_model(self, pop1, pop2):
556553 m1 = _model_cls_d [d ['model' ]['class' ]].from_dict (d ['model' ])
557554 d = json .load (open (pop2 , "rt" ))
558555 m2 = _model_cls_d [d ['model' ]['class' ]].from_dict (d ['model' ])
556+ self ._hidden_states .update (d ['hidden_states' ])
559557 assert d ['theta' ] == self ._theta
560558 self ._max_split = m2 ._knots [- (len (smcpp .defaults .additional_knots ) + 1 )]
561559 self ._model = SMCTwoPopulationModel (m1 , m2 , self ._max_split * 0.5 )
0 commit comments