Skip to content

Commit f1af2c9

Browse files
committed
[core] minor bug fixes
1 parent 93d6c5a commit f1af2c9

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

smcpp/analysis.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

smcpp/commands/posterior.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ def main(self, args):
8989
all_obs = estimation_tools.thin_dataset(all_obs, [args.thinning] * len(all_obs))
9090
if npop == 1:
9191
im = _smcpp.PyOnePopInferenceManager(
92-
n[0], all_obs, hidden_states, contig.key, args.polarization_error)
92+
n[0], all_obs, hidden_states, contig.key[0], args.polarization_error)
9393
else:
9494
assert npop == 2
9595
im = _smcpp.PyTwoPopInferenceManager(
96-
*n, *a, all_obs, hidden_states, contig.key, args.polarization_error)
96+
*n, *a, all_obs, hidden_states, contig.key[0], args.polarization_error)
9797
im.theta = j['theta']
9898
im.rho = j['rho']
99+
im.alpha = j['alpha']
99100
im.save_gamma = True
100101
im.model = m
101102
im.E_step()

src/transition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ HJTransition<T>::HJTransition(const PiecewiseConstantRateFunction<T> &eta, const
248248
T small = eta.zero() + 1e-20;
249249
this->Phi = this->Phi.unaryExpr([small] (const T &x) { if (x < 1e-20) return small; return x; });
250250
CHECK_NAN(this->Phi);
251+
return;
251252
const double beta = 1e-5;
252253
T p2 = eta.zero() + beta / this->M;
253254
Matrix<T> Phi2(this->M, this->M);

test/integration/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ set -e
44
$SMC vcf2smc -v example/example.vcf.gz /tmp/example.1.smc.gz 1 msp1:msp_0,msp_1,msp_2
55
$SMC vcf2smc -d msp_0 msp_0 example/example.vcf.gz /tmp/example.2.smc.gz 1 msp2:msp_0,msp_3,msp_4
66
$SMC vcf2smc -d msp_1 msp_1 example/example.vcf.gz /tmp/example.12.smc.gz 1 msp1:msp_1,msp_2 msp2:msp_3,msp_4,msp_0
7-
$SMC estimate -o /tmp/out/1 --unfold --em-iterations 1 1.25e-8 /tmp/example.1.smc.gz
8-
$SMC estimate -p 0.01 -r 1e-8 -o /tmp/out/2 --em-iterations 1 1.25e-8 /tmp/example.2.smc.gz
7+
$SMC estimate -o /tmp/out/1 --unfold --knots 5 --em-iterations 1 1.25e-8 /tmp/example.1.smc.gz
8+
$SMC estimate -p 0.01 -r 1e-8 -o /tmp/out/2 --knots 5 --em-iterations 1 1.25e-8 /tmp/example.2.smc.gz
99
$SMC split -o /tmp/out/split --em-iterations 1 \
1010
/tmp/out/1/model.final.json \
1111
/tmp/out/2/model.final.json \

0 commit comments

Comments
 (0)