fastmixture2 2.0.4__tar.gz → 2.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. {fastmixture2-2.0.4/fastmixture2.egg-info → fastmixture2-2.0.6}/PKG-INFO +60 -82
  2. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/README.md +59 -81
  3. fastmixture2-2.0.6/fastmixture2/__init__.py +1 -0
  4. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/defaults.py +1 -3
  5. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/em.pyx +32 -24
  6. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/fastmixture2.py +32 -15
  7. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/functions.py +218 -52
  8. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/gpu.py +655 -444
  9. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/kernels.py +76 -22
  10. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/main.py +12 -1
  11. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/packed.pxd +17 -1
  12. fastmixture2-2.0.6/fastmixture2/plink.py +148 -0
  13. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/shared.pyx +70 -35
  14. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/sqp.pyx +56 -20
  15. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/svd.pyx +13 -9
  16. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/utils.py +222 -102
  17. {fastmixture2-2.0.4 → fastmixture2-2.0.6/fastmixture2.egg-info}/PKG-INFO +60 -82
  18. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2.egg-info/SOURCES.txt +4 -0
  19. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/check_portable.py +5 -1
  20. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/helpers.py +11 -0
  21. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_adversarial_io.py +1 -1
  22. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_bootstrap.py +11 -7
  23. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_cli.py +22 -3
  24. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_convergence.py +2 -2
  25. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_cpu_scaling.py +204 -1
  26. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_cpu_subset.py +148 -12
  27. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_cross_validation.py +6 -1
  28. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_frequency_cache.py +76 -1
  29. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_called.py +264 -230
  30. fastmixture2-2.0.6/tests/test_gpu_em.py +256 -0
  31. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_gl_precision.py +49 -6
  32. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_large.py +8 -10
  33. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_modes.py +6 -2
  34. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_precision.py +5 -5
  35. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_safety.py +83 -11
  36. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_streamed.py +30 -5
  37. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_subset.py +119 -7
  38. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_svd_cache.py +26 -1
  39. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_initialization.py +64 -1
  40. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_likelihoods.py +36 -38
  41. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_numerical_equivalence.py +92 -0
  42. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_optimizers.py +11 -11
  43. fastmixture2-2.0.6/tests/test_plink_files.py +668 -0
  44. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_q_constraints.py +2 -2
  45. fastmixture2-2.0.6/tests/test_restarts.py +853 -0
  46. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_subset_modes.py +7 -5
  47. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/validate_distribution.py +1 -0
  48. fastmixture2-2.0.4/fastmixture2/__init__.py +0 -1
  49. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/LICENSE +0 -0
  50. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/MANIFEST.in +0 -0
  51. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2/threads.pxd +0 -0
  52. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2.egg-info/dependency_links.txt +0 -0
  53. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2.egg-info/entry_points.txt +0 -0
  54. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2.egg-info/requires.txt +0 -0
  55. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/fastmixture2.egg-info/top_level.txt +0 -0
  56. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/pyproject.toml +0 -0
  57. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/setup.cfg +0 -0
  58. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/setup.py +0 -0
  59. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/run_tests.py +0 -0
  60. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_distribution.py +0 -0
  61. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_genotypes.py +0 -0
  62. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_stat_reuse.py +0 -0
  63. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_gpu_supervision.py +0 -0
  64. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_sqp_scoring.py +0 -0
  65. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_sqp_solver.py +0 -0
  66. {fastmixture2-2.0.4 → fastmixture2-2.0.6}/tests/test_threads.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmixture2
3
- Version: 2.0.4
3
+ Version: 2.0.6
4
4
  Summary: Biobank-scale ancestry estimation in unrelated individuals
5
5
  Author-email: "J. Meisner" <meisnerucph@gmail.com>, "L. Thunegard" <lisathunegard@icloud.com>
6
6
  License-Expression: GPL-3.0-only
@@ -31,15 +31,20 @@ Dynamic: license-file
31
31
 
32
32
  # fastmixture2
33
33
 
34
- [![Tests](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg)](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml)
35
- [![PyPI](https://img.shields.io/pypi/v/fastmixture2.svg)](https://pypi.org/project/fastmixture2/)
36
- [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE)
34
+ <p align="center">
35
+ <img src="assets/fastmixture2-logo-small-wordmark.png" alt="fastmixture2 logo: a snake-shaped 2 intertwined with a colorful DNA helix" width="300">
36
+ </p>
37
+
38
+ <p align="center">
39
+ <a href="https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml"><img src="https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg" alt="Tests"></a>
40
+ <a href="https://pypi.org/project/fastmixture2/"><img src="https://img.shields.io/pypi/v/fastmixture2.svg" alt="PyPI"></a>
41
+ <a href="https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-GPLv3-blue.svg" alt="License: GPL v3"></a>
42
+ </p>
37
43
 
38
44
  `fastmixture2` estimates ancestry proportions (`Q`) and ancestral allele
39
45
  frequencies (`P`) by maximum likelihood. It reads SNP-major PLINK genotypes
40
- or ANGSD Beagle genotype likelihoods. Unsupervised initialization uses SVD/ALS
41
- for K > 1, with subset likelihood refinement for large panels.
42
- All modes use safeguarded SQP/ZAL updates on CPU or, with `--gpu`, on NVIDIA CUDA.
46
+ or ANGSD Beagle genotype likelihoods. Unsupervised, supervised and projection
47
+ modes run on CPU or, with `--gpu`, on NVIDIA CUDA.
43
48
 
44
49
  ## Installation
45
50
 
@@ -70,7 +75,7 @@ conda activate fastmixture2-cpu
70
75
 
71
76
  For GPU use, select `environment-gpu.yml` and activate `fastmixture2-gpu`.
72
77
 
73
- PyPI wheels cover Linux (glibc) and macOS 14 or newer, on x86-64 and ARM64.
78
+ PyPI wheels cover Linux (glibc) and macOS 15 or newer, on x86-64 and ARM64.
74
79
  They use AVX2 on supported x86-64 CPUs and NEON on ARM64, including Apple Silicon:
75
80
 
76
81
  ```bash
@@ -111,11 +116,23 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --out cohort
111
116
  fastmixture2 --beagle cohort.beagle.gz --K 5 --threads 8 --out cohort.gl
112
117
  ```
113
118
 
114
- - `--bfile` takes the shared prefix of SNP-major PLINK `.bed`, `.bim` and `.fam`
115
- files. Genotypes stay packed on CPU and GPU. Missing genotypes are supported.
119
+ - `--bfile` takes one or more ordered prefixes of SNP-major PLINK `.bed`, `.bim`
120
+ and `.fam` files. Genotypes stay packed on CPU and GPU. Missing genotypes are
121
+ supported.
116
122
  - `--beagle` takes a gzipped ANGSD Beagle file. Likelihood triplets are normalized
117
123
  internally. Exactly uniform triplets contribute no ancestry information.
118
124
 
125
+ Per-chromosome inputs are memory-mapped and fitted jointly without a merged BED:
126
+
127
+ ```bash
128
+ fastmixture2 --bfile chr{1..22} --K 5 --threads 8 --out cohort
129
+ ```
130
+
131
+ The shell expands `chr{1..22}` in chromosome order. Explicit prefixes also work,
132
+ such as `--bfile chr1 chr2 chr3`. All FAM files must have identical FID/IID pairs
133
+ in the same order. Marker rows, including annotated P output, follow the supplied
134
+ file order. Per-chromosome input works on CPU and CUDA in all modes.
135
+
119
136
  Perform sample and variant QC first. For each K, try several seeds and keep
120
137
  the converged fit with the highest log-likelihood. Component labels may change
121
138
  between runs, so align them before comparing Q or P.
@@ -135,75 +152,20 @@ CPU and CUDA support `1 <= K <= 128` for both input formats in all modes.
135
152
  CUDA keeps input data on the GPU when they fit and streams in blocks otherwise.
136
153
  Small CPU/GPU differences can arise from floating-point arithmetic.
137
154
 
138
- At K > 1, unsupervised initialization uses 10 SVD power iterations on all SNPs
139
- up to 50,000, or a genome-wide 50,000-SNP subset otherwise. Larger panels refine
140
- subset Q/P with SQP/ZAL, then fit full-marker P by least squares at fixed Q and
141
- retain the refined subset rows. This applies to both input formats, including
142
- missing genotypes.
143
- Panels up to 50,000 SNPs proceed directly from SVD/ALS to full-data refinement,
144
- without a duplicate subset fit or projection. For PLINK, subset refinement is
145
- skipped if any sample has no observed subset genotypes. Exact CV folds and
146
- `--iter 0` retain SVD/ALS without subset likelihood refinement.
147
-
148
- Subsets exceeding 512 MiB on CPU or 256 MiB on CUDA are built in bounded blocks
149
- into a temporary memory-mapped file. CUDA streams the subset when it does not
150
- fit in VRAM. Temporary files use `TMPDIR`, which can point to disk-backed scratch
151
- storage.
152
-
153
- CUDA SVD starts at 256 samples. Both devices use CPU ALS with float64
154
- contractions. Fixed-Q P projection uses float64 products on CPU and float32
155
- products on CUDA.
156
-
157
- Resident and streamed PLINK CUDA fits use float32 SQP statistics while gains
158
- remain material, then switch to float64 refinement. There is no fixed float32
159
- iteration cap. Tile products use float32, with float64 sums across tiles.
160
- Parameters, constrained solves and likelihood checks stay in float64.
161
-
162
- Genotype-likelihood fits use Q-first SQP/ZAL on both CPU and CUDA, with exact
163
- EM/QN rescue and an independent EM check before convergence. CUDA fits start
164
- with float32 probabilities, derivatives and statistics in either storage mode,
165
- then switch to float64 refinement. Near frequency bounds, GL probabilities
166
- and derivatives use float64. cuBLAS tile sums, parameters, solves and scores stay
167
- in float64.
168
- CPU and CUDA start directly with SQP and use ZAL history depth 3.
169
- `--iter` and `--tole` apply to each SQP/ZAL stage.
170
- EM skips missing genotypes and exactly uniform likelihood triplets, with
171
- normalization over informative entries. Nonuniform likelihoods are retained.
172
- Full likelihood scores retain the constant contribution from uniform triplets.
173
-
174
- CPU SQP uses tiled BLAS statistics for Q and P at every supported K.
175
- This applies to PLINK and genotype likelihoods in all modes.
176
- BLAS scratch and P statistics each have a separate 512 MiB cap. Q statistics
177
- use 4096-sample shards with a separate 4 GiB cap. Optional update caches
178
- remain capped at 64 MiB. Input storage, factors and optimizer history are
179
- additional allocations.
180
-
181
- At K <= 16, CUDA fits use native kernels at K = 1..3
182
- and tiled cuBLAS at K = 4..16 during float32 refinement. Resident tiles share
183
- 64 MiB of scratch and keep float32 copies of P and Q. At K = 8..16,
184
- resident mixed fits reuse frequency scratch for cooperative float64 solves.
185
- Tiles that cannot hold the solve tables use the native solve. Streamed fits keep
186
- float32 copies within tiles and reuse their float64 Q-statistics buffers, with
187
- up to 128 MiB of compute scratch.
188
- Final refinement uses native float64 kernels, except
189
- resident GL P updates at K = 8..16 with up to 1024 samples. Those share the tiled
190
- cuBLAS P routine and keep combined scratch within 64 MiB.
191
- Unsupervised resident PLINK fits at K = 10..16 keep the first eight iterations
192
- fresh, then alternate Q-Hessian reuse during float32 refinement. The cache is
193
- capped at 64 MiB. Weak cached proposals retry with fresh curvature, and
194
- float64 refinement always uses fresh curvature.
195
-
196
- At K > 16, CUDA uses tiled cuBLAS statistics and a cooperative active-set
197
- solver for both input formats. Resident and bootstrap maps share 64 MiB of
198
- scratch. Streamed maps allow up to 128 MiB of compute scratch and 512 MiB of
199
- Q statistics, with free-VRAM guards. Streamed tiles adapt to the panel width,
200
- with at most 8192 rows within the same memory caps. Curvature stays fresh.
155
+ At K > 1, unsupervised fits use SVD/ALS on up to 50,000 genome-wide SNPs, with
156
+ subset refinement for larger panels. All modes use safeguarded SQP/ZAL with EM/QN
157
+ rescue and float64 likelihood checks. CPU fitting uses float64. CUDA uses
158
+ mixed precision followed by float64 refinement.
159
+
160
+ Processing uses bounded tiles. Total memory still grows with panel size and K.
161
+ Temporary files use `TMPDIR`. For large panels, point it to disk-backed scratch
162
+ with sufficient space.
201
163
 
202
164
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
203
- `--threads` caps CPU computation, including BLAS and GPU host copies.
165
+ `--threads` also controls CPU work during GPU fits.
204
166
  The optimizer scales the tolerance by observed genotypes or informative
205
- likelihood triplets.
206
- Check the log for convergence. Run `fastmixture2 --help` for the full CLI.
167
+ likelihood triplets. `--iter` and `--tole` apply to each refinement stage.
168
+ Run `fastmixture2 --help` for the full CLI.
207
169
 
208
170
  ### Beagle cache
209
171
 
@@ -298,7 +260,7 @@ fastmixture2 --bfile cohort --K 5 --threads 8 \
298
260
  ```
299
261
 
300
262
  `--bootstrap` defaults to 200 replicates. Each replicate starts from the
301
- full-data fit and takes up to three weighted SQP/ZAL iterations with EM rescue.
263
+ full-data fit and takes up to three refinement iterations.
302
264
  Use `--bootstrap-iter` for longer refits. Add `--bootstrap-tole` to stop at a mean
303
265
  likelihood gain per informative entry, within that iteration limit. The log
304
266
  reports refit termination. Short refits give a local estimate of uncertainty.
@@ -312,6 +274,23 @@ Add `--gpu` for CUDA refits. Projection errors are conditional on the supplied
312
274
  P matrix. Supervised reference samples have zero Q uncertainty. Bootstrap and
313
275
  CV must be run separately.
314
276
 
277
+ ### Restarts
278
+
279
+ Try additional unsupervised fits with `--restarts N` (default 0):
280
+
281
+ ```bash
282
+ fastmixture2 --bfile cohort --K 5 --threads 8 --restarts 6 --out cohort
283
+ ```
284
+
285
+ Each round tries a Q-column permutation and a component merge/split mutation,
286
+ followed by full-data refits. Panels above 50,000 SNPs use fresh subsets.
287
+ For K >= 3, N rounds add up to 2N full-data fits. Outputs and checkpoints
288
+ retain the best full-data fit.
289
+
290
+ Restarts support both input formats on CPU and CUDA, use `--seed`, and keep
291
+ the same `--iter` and `--tole` per stage. K = 2 uses only Q permutations.
292
+ At K = 1 they have no effect.
293
+
315
294
  ### Checkpointing
316
295
 
317
296
  Save accepted estimates and resume an interrupted CPU or GPU fit:
@@ -322,6 +301,8 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --resume run.ckpt --out cohort
322
301
  ```
323
302
 
324
303
  Keep the same input, supervision and projection files when resuming.
304
+ With `--restarts`, checkpoints retain the best full-data fit. Resuming runs the
305
+ requested number of additional restart rounds after the resumed fit.
325
306
 
326
307
  ## Output
327
308
 
@@ -339,10 +320,8 @@ Rows follow input order and columns correspond to components. Use
339
320
  P. Projection does not write P. For PLINK, P is the frequency of BIM A1.
340
321
  For Beagle, it is the frequency of allele2.
341
322
 
342
- Q and Q_se contain N x K values, and P contains M x K values. Matrices use
343
- ten decimal places. The log records export counts and full-precision
344
- likelihoods before output rounding. Console likelihoods use six decimal
345
- places. Numbers have no thousands separators.
323
+ Matrices use ten decimal places. Logs retain full-precision likelihoods and
324
+ iteration counts, including ALS when used.
346
325
 
347
326
  Outputs, checkpoints and caches use standard file permissions controlled by
348
327
  your umask.
@@ -352,7 +331,6 @@ numerical failure. Unsuccessful fits retain the best accepted parameters.
352
331
 
353
332
  Parallel jobs should use distinct output prefixes. Build each likelihood cache
354
333
  with one job, then reuse it across fits, or use a separate cache path per job.
355
- fastmixture2 does not create filesystem locks or require private lock directories.
356
334
 
357
335
  ## Authors and license
358
336
 
@@ -1,14 +1,19 @@
1
1
  # fastmixture2
2
2
 
3
- [![Tests](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg)](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml)
4
- [![PyPI](https://img.shields.io/pypi/v/fastmixture2.svg)](https://pypi.org/project/fastmixture2/)
5
- [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE)
3
+ <p align="center">
4
+ <img src="assets/fastmixture2-logo-small-wordmark.png" alt="fastmixture2 logo: a snake-shaped 2 intertwined with a colorful DNA helix" width="300">
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml"><img src="https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg" alt="Tests"></a>
9
+ <a href="https://pypi.org/project/fastmixture2/"><img src="https://img.shields.io/pypi/v/fastmixture2.svg" alt="PyPI"></a>
10
+ <a href="https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-GPLv3-blue.svg" alt="License: GPL v3"></a>
11
+ </p>
6
12
 
7
13
  `fastmixture2` estimates ancestry proportions (`Q`) and ancestral allele
8
14
  frequencies (`P`) by maximum likelihood. It reads SNP-major PLINK genotypes
9
- or ANGSD Beagle genotype likelihoods. Unsupervised initialization uses SVD/ALS
10
- for K > 1, with subset likelihood refinement for large panels.
11
- All modes use safeguarded SQP/ZAL updates on CPU or, with `--gpu`, on NVIDIA CUDA.
15
+ or ANGSD Beagle genotype likelihoods. Unsupervised, supervised and projection
16
+ modes run on CPU or, with `--gpu`, on NVIDIA CUDA.
12
17
 
13
18
  ## Installation
14
19
 
@@ -39,7 +44,7 @@ conda activate fastmixture2-cpu
39
44
 
40
45
  For GPU use, select `environment-gpu.yml` and activate `fastmixture2-gpu`.
41
46
 
42
- PyPI wheels cover Linux (glibc) and macOS 14 or newer, on x86-64 and ARM64.
47
+ PyPI wheels cover Linux (glibc) and macOS 15 or newer, on x86-64 and ARM64.
43
48
  They use AVX2 on supported x86-64 CPUs and NEON on ARM64, including Apple Silicon:
44
49
 
45
50
  ```bash
@@ -80,11 +85,23 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --out cohort
80
85
  fastmixture2 --beagle cohort.beagle.gz --K 5 --threads 8 --out cohort.gl
81
86
  ```
82
87
 
83
- - `--bfile` takes the shared prefix of SNP-major PLINK `.bed`, `.bim` and `.fam`
84
- files. Genotypes stay packed on CPU and GPU. Missing genotypes are supported.
88
+ - `--bfile` takes one or more ordered prefixes of SNP-major PLINK `.bed`, `.bim`
89
+ and `.fam` files. Genotypes stay packed on CPU and GPU. Missing genotypes are
90
+ supported.
85
91
  - `--beagle` takes a gzipped ANGSD Beagle file. Likelihood triplets are normalized
86
92
  internally. Exactly uniform triplets contribute no ancestry information.
87
93
 
94
+ Per-chromosome inputs are memory-mapped and fitted jointly without a merged BED:
95
+
96
+ ```bash
97
+ fastmixture2 --bfile chr{1..22} --K 5 --threads 8 --out cohort
98
+ ```
99
+
100
+ The shell expands `chr{1..22}` in chromosome order. Explicit prefixes also work,
101
+ such as `--bfile chr1 chr2 chr3`. All FAM files must have identical FID/IID pairs
102
+ in the same order. Marker rows, including annotated P output, follow the supplied
103
+ file order. Per-chromosome input works on CPU and CUDA in all modes.
104
+
88
105
  Perform sample and variant QC first. For each K, try several seeds and keep
89
106
  the converged fit with the highest log-likelihood. Component labels may change
90
107
  between runs, so align them before comparing Q or P.
@@ -104,75 +121,20 @@ CPU and CUDA support `1 <= K <= 128` for both input formats in all modes.
104
121
  CUDA keeps input data on the GPU when they fit and streams in blocks otherwise.
105
122
  Small CPU/GPU differences can arise from floating-point arithmetic.
106
123
 
107
- At K > 1, unsupervised initialization uses 10 SVD power iterations on all SNPs
108
- up to 50,000, or a genome-wide 50,000-SNP subset otherwise. Larger panels refine
109
- subset Q/P with SQP/ZAL, then fit full-marker P by least squares at fixed Q and
110
- retain the refined subset rows. This applies to both input formats, including
111
- missing genotypes.
112
- Panels up to 50,000 SNPs proceed directly from SVD/ALS to full-data refinement,
113
- without a duplicate subset fit or projection. For PLINK, subset refinement is
114
- skipped if any sample has no observed subset genotypes. Exact CV folds and
115
- `--iter 0` retain SVD/ALS without subset likelihood refinement.
116
-
117
- Subsets exceeding 512 MiB on CPU or 256 MiB on CUDA are built in bounded blocks
118
- into a temporary memory-mapped file. CUDA streams the subset when it does not
119
- fit in VRAM. Temporary files use `TMPDIR`, which can point to disk-backed scratch
120
- storage.
121
-
122
- CUDA SVD starts at 256 samples. Both devices use CPU ALS with float64
123
- contractions. Fixed-Q P projection uses float64 products on CPU and float32
124
- products on CUDA.
125
-
126
- Resident and streamed PLINK CUDA fits use float32 SQP statistics while gains
127
- remain material, then switch to float64 refinement. There is no fixed float32
128
- iteration cap. Tile products use float32, with float64 sums across tiles.
129
- Parameters, constrained solves and likelihood checks stay in float64.
130
-
131
- Genotype-likelihood fits use Q-first SQP/ZAL on both CPU and CUDA, with exact
132
- EM/QN rescue and an independent EM check before convergence. CUDA fits start
133
- with float32 probabilities, derivatives and statistics in either storage mode,
134
- then switch to float64 refinement. Near frequency bounds, GL probabilities
135
- and derivatives use float64. cuBLAS tile sums, parameters, solves and scores stay
136
- in float64.
137
- CPU and CUDA start directly with SQP and use ZAL history depth 3.
138
- `--iter` and `--tole` apply to each SQP/ZAL stage.
139
- EM skips missing genotypes and exactly uniform likelihood triplets, with
140
- normalization over informative entries. Nonuniform likelihoods are retained.
141
- Full likelihood scores retain the constant contribution from uniform triplets.
142
-
143
- CPU SQP uses tiled BLAS statistics for Q and P at every supported K.
144
- This applies to PLINK and genotype likelihoods in all modes.
145
- BLAS scratch and P statistics each have a separate 512 MiB cap. Q statistics
146
- use 4096-sample shards with a separate 4 GiB cap. Optional update caches
147
- remain capped at 64 MiB. Input storage, factors and optimizer history are
148
- additional allocations.
149
-
150
- At K <= 16, CUDA fits use native kernels at K = 1..3
151
- and tiled cuBLAS at K = 4..16 during float32 refinement. Resident tiles share
152
- 64 MiB of scratch and keep float32 copies of P and Q. At K = 8..16,
153
- resident mixed fits reuse frequency scratch for cooperative float64 solves.
154
- Tiles that cannot hold the solve tables use the native solve. Streamed fits keep
155
- float32 copies within tiles and reuse their float64 Q-statistics buffers, with
156
- up to 128 MiB of compute scratch.
157
- Final refinement uses native float64 kernels, except
158
- resident GL P updates at K = 8..16 with up to 1024 samples. Those share the tiled
159
- cuBLAS P routine and keep combined scratch within 64 MiB.
160
- Unsupervised resident PLINK fits at K = 10..16 keep the first eight iterations
161
- fresh, then alternate Q-Hessian reuse during float32 refinement. The cache is
162
- capped at 64 MiB. Weak cached proposals retry with fresh curvature, and
163
- float64 refinement always uses fresh curvature.
164
-
165
- At K > 16, CUDA uses tiled cuBLAS statistics and a cooperative active-set
166
- solver for both input formats. Resident and bootstrap maps share 64 MiB of
167
- scratch. Streamed maps allow up to 128 MiB of compute scratch and 512 MiB of
168
- Q statistics, with free-VRAM guards. Streamed tiles adapt to the panel width,
169
- with at most 8192 rows within the same memory caps. Curvature stays fresh.
124
+ At K > 1, unsupervised fits use SVD/ALS on up to 50,000 genome-wide SNPs, with
125
+ subset refinement for larger panels. All modes use safeguarded SQP/ZAL with EM/QN
126
+ rescue and float64 likelihood checks. CPU fitting uses float64. CUDA uses
127
+ mixed precision followed by float64 refinement.
128
+
129
+ Processing uses bounded tiles. Total memory still grows with panel size and K.
130
+ Temporary files use `TMPDIR`. For large panels, point it to disk-backed scratch
131
+ with sufficient space.
170
132
 
171
133
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
172
- `--threads` caps CPU computation, including BLAS and GPU host copies.
134
+ `--threads` also controls CPU work during GPU fits.
173
135
  The optimizer scales the tolerance by observed genotypes or informative
174
- likelihood triplets.
175
- Check the log for convergence. Run `fastmixture2 --help` for the full CLI.
136
+ likelihood triplets. `--iter` and `--tole` apply to each refinement stage.
137
+ Run `fastmixture2 --help` for the full CLI.
176
138
 
177
139
  ### Beagle cache
178
140
 
@@ -267,7 +229,7 @@ fastmixture2 --bfile cohort --K 5 --threads 8 \
267
229
  ```
268
230
 
269
231
  `--bootstrap` defaults to 200 replicates. Each replicate starts from the
270
- full-data fit and takes up to three weighted SQP/ZAL iterations with EM rescue.
232
+ full-data fit and takes up to three refinement iterations.
271
233
  Use `--bootstrap-iter` for longer refits. Add `--bootstrap-tole` to stop at a mean
272
234
  likelihood gain per informative entry, within that iteration limit. The log
273
235
  reports refit termination. Short refits give a local estimate of uncertainty.
@@ -281,6 +243,23 @@ Add `--gpu` for CUDA refits. Projection errors are conditional on the supplied
281
243
  P matrix. Supervised reference samples have zero Q uncertainty. Bootstrap and
282
244
  CV must be run separately.
283
245
 
246
+ ### Restarts
247
+
248
+ Try additional unsupervised fits with `--restarts N` (default 0):
249
+
250
+ ```bash
251
+ fastmixture2 --bfile cohort --K 5 --threads 8 --restarts 6 --out cohort
252
+ ```
253
+
254
+ Each round tries a Q-column permutation and a component merge/split mutation,
255
+ followed by full-data refits. Panels above 50,000 SNPs use fresh subsets.
256
+ For K >= 3, N rounds add up to 2N full-data fits. Outputs and checkpoints
257
+ retain the best full-data fit.
258
+
259
+ Restarts support both input formats on CPU and CUDA, use `--seed`, and keep
260
+ the same `--iter` and `--tole` per stage. K = 2 uses only Q permutations.
261
+ At K = 1 they have no effect.
262
+
284
263
  ### Checkpointing
285
264
 
286
265
  Save accepted estimates and resume an interrupted CPU or GPU fit:
@@ -291,6 +270,8 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --resume run.ckpt --out cohort
291
270
  ```
292
271
 
293
272
  Keep the same input, supervision and projection files when resuming.
273
+ With `--restarts`, checkpoints retain the best full-data fit. Resuming runs the
274
+ requested number of additional restart rounds after the resumed fit.
294
275
 
295
276
  ## Output
296
277
 
@@ -308,10 +289,8 @@ Rows follow input order and columns correspond to components. Use
308
289
  P. Projection does not write P. For PLINK, P is the frequency of BIM A1.
309
290
  For Beagle, it is the frequency of allele2.
310
291
 
311
- Q and Q_se contain N x K values, and P contains M x K values. Matrices use
312
- ten decimal places. The log records export counts and full-precision
313
- likelihoods before output rounding. Console likelihoods use six decimal
314
- places. Numbers have no thousands separators.
292
+ Matrices use ten decimal places. Logs retain full-precision likelihoods and
293
+ iteration counts, including ALS when used.
315
294
 
316
295
  Outputs, checkpoints and caches use standard file permissions controlled by
317
296
  your umask.
@@ -321,7 +300,6 @@ numerical failure. Unsuccessful fits retain the best accepted parameters.
321
300
 
322
301
  Parallel jobs should use distinct output prefixes. Build each likelihood cache
323
302
  with one job, then reuse it across fits, or use a separate cache path per job.
324
- fastmixture2 does not create filesystem locks or require private lock directories.
325
303
 
326
304
  ## Authors and license
327
305
 
@@ -0,0 +1 @@
1
+ __version__ = "2.0.6"
@@ -30,9 +30,6 @@ ALS_TOLERANCE = 1e-4
30
30
 
31
31
  # Refinement
32
32
  GPU_SQP_EXACT_POLISH_ITERATIONS = 2
33
- GPU_SQP_Q_REUSE_MIN_K = 10
34
- GPU_SQP_Q_REUSE_DELAY = 8
35
- GPU_SQP_Q_CACHE_BYTES = 64 << 20
36
33
  LIKELIHOOD_TRUST_RADIUS = 0.02
37
34
  # Halve the step before falling back to EM
38
35
  LIKELIHOOD_BACKTRACK_STEPS = 3
@@ -42,6 +39,7 @@ LIKELIHOOD_CONVERGENCE_WINDOW = 10
42
39
  GPU_TILE_BYTES = 512 << 20
43
40
  GPU_SQP_STAT_BYTES = 512 << 20
44
41
  GPU_SQP_WORKSPACE_BYTES = 128 << 20
42
+ GPU_BOOT_STAT_BYTES = 64 << 20
45
43
  GPU_MIN_TILE_BYTES = 128 << 20
46
44
  GPU_PREFETCH_MIN_TILES = 3
47
45
  GPU_MEMORY_RESERVE_BYTES = 512 << 20
@@ -8,9 +8,9 @@ __author__ = "Jonas Meisner"
8
8
  cimport openmp as omp
9
9
  from cython.parallel import parallel, prange
10
10
  from libc.math cimport fmax, fmin
11
- from libc.stdint cimport uint8_t, uint32_t, uint64_t
11
+ from libc.stdint cimport uint8_t, uint32_t, uint64_t, uintptr_t
12
12
  from libc.stdlib cimport calloc, free
13
- from fastmixture2.packed cimport decodePacked, flatLikelihood, heldOut
13
+ from fastmixture2.packed cimport packedRow, packedCount, decodePacked, flatLikelihood, heldOut
14
14
 
15
15
  ctypedef uint8_t u8
16
16
  ctypedef uint32_t u32
@@ -1180,7 +1180,8 @@ cpdef int accelPacked(
1180
1180
  const u8[:, ::1] B, const f64[:, ::1] P,
1181
1181
  f64[:, ::1] P1, const f64[:, ::1] Q,
1182
1182
  f64[:, ::1] T, const Py_ssize_t N,
1183
- Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0
1183
+ Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0,
1184
+ const uintptr_t[::1] rows=None
1184
1185
  ) except -1 nogil:
1185
1186
  cdef:
1186
1187
  bint masked = fold >= 0
@@ -1193,7 +1194,7 @@ cpdef int accelPacked(
1193
1194
  f64* p_all
1194
1195
  f64* q_all
1195
1196
  f64* t = &T[0, 0]
1196
- Py_ssize_t M = B.shape[0]
1197
+ Py_ssize_t M = packedCount(B, rows)
1197
1198
  Py_ssize_t K = Q.shape[1]
1198
1199
  Py_ssize_t L
1199
1200
  Py_ssize_t i, j, x
@@ -1221,9 +1222,9 @@ cpdef int accelPacked(
1221
1222
  for j in prange(M, schedule="static"):
1222
1223
  p = &P[j, 0]
1223
1224
  if masked:
1224
- _unpackPackedRowFold(&B[j, 0], g_thr, j, N, fold, folds, seed)
1225
+ _unpackPackedRowFold(packedRow(B, rows, j), g_thr, j, N, fold, folds, seed)
1225
1226
  else:
1226
- _unpackPackedRow(&B[j, 0], g_thr, N)
1227
+ _unpackPackedRow(packedRow(B, rows, j), g_thr, N)
1227
1228
  if not _specializedEmRow(g_thr, p, &Q[0, 0], &P1[j, 0], q_thr, N, K):
1228
1229
  for i in range(N):
1229
1230
  if g_thr[i] == 9:
@@ -1250,7 +1251,8 @@ cpdef int batchPackedW(
1250
1251
  f64[:, ::1] P1, const f64[:, ::1] Q,
1251
1252
  f64[:, ::1] T, const u32[::1] s, const u32[::1] w,
1252
1253
  const Py_ssize_t N, Py_ssize_t fold=-1,
1253
- Py_ssize_t folds=1, uint64_t seed=0
1254
+ Py_ssize_t folds=1, uint64_t seed=0,
1255
+ const uintptr_t[::1] rows=None
1254
1256
  ) except -1 nogil:
1255
1257
  cdef:
1256
1258
  bint masked = fold >= 0
@@ -1298,11 +1300,11 @@ cpdef int batchPackedW(
1298
1300
  continue
1299
1301
  if masked:
1300
1302
  _unpackPackedRowFold(
1301
- &B[marker, 0], g_thr, marker, N,
1303
+ packedRow(B, rows, marker), g_thr, marker, N,
1302
1304
  fold, folds, seed
1303
1305
  )
1304
1306
  else:
1305
- _unpackPackedRow(&B[marker, 0], g_thr, N)
1307
+ _unpackPackedRow(packedRow(B, rows, marker), g_thr, N)
1306
1308
  if not _specializedEmRowW(
1307
1309
  g_thr, p, &Q[0, 0], &P1[marker, 0], q_thr, wt[j], N, K
1308
1310
  ):
@@ -1330,7 +1332,8 @@ cpdef int stepQPacked(
1330
1332
  const u8[:, ::1] B, const f64[:, ::1] P,
1331
1333
  const f64[:, ::1] Q, f64[:, ::1] T,
1332
1334
  const Py_ssize_t N, Py_ssize_t fold=-1,
1333
- Py_ssize_t folds=1, uint64_t seed=0
1335
+ Py_ssize_t folds=1, uint64_t seed=0,
1336
+ const uintptr_t[::1] rows=None
1334
1337
  ) except -1 nogil:
1335
1338
  cdef:
1336
1339
  bint masked = fold >= 0
@@ -1339,7 +1342,7 @@ cpdef int stepQPacked(
1339
1342
  f64* q_thr
1340
1343
  f64* q_all
1341
1344
  f64* t = &T[0, 0]
1342
- Py_ssize_t M = B.shape[0]
1345
+ Py_ssize_t M = packedCount(B, rows)
1343
1346
  Py_ssize_t K = Q.shape[1]
1344
1347
  Py_ssize_t L
1345
1348
  Py_ssize_t i, j, x
@@ -1362,9 +1365,9 @@ cpdef int stepQPacked(
1362
1365
  g_thr = &g_all[tid * N]
1363
1366
  for j in prange(M, schedule="static"):
1364
1367
  if masked:
1365
- _unpackPackedRowFold(&B[j, 0], g_thr, j, N, fold, folds, seed)
1368
+ _unpackPackedRowFold(packedRow(B, rows, j), g_thr, j, N, fold, folds, seed)
1366
1369
  else:
1367
- _unpackPackedRow(&B[j, 0], g_thr, N)
1370
+ _unpackPackedRow(packedRow(B, rows, j), g_thr, N)
1368
1371
  if not _specializedQRow(g_thr, &P[j, 0], &Q[0, 0], q_thr, N, K):
1369
1372
  for i in range(N):
1370
1373
  if g_thr[i] == 9:
@@ -1384,7 +1387,8 @@ cpdef int stepBatchQPackedW(
1384
1387
  const u8[:, ::1] B, const f64[:, ::1] P, const f64[:, ::1] Q,
1385
1388
  f64[:, ::1] T, const u32[::1] s, const u32[::1] w,
1386
1389
  const Py_ssize_t N, Py_ssize_t fold=-1,
1387
- Py_ssize_t folds=1, uint64_t seed=0
1390
+ Py_ssize_t folds=1, uint64_t seed=0,
1391
+ const uintptr_t[::1] rows=None
1388
1392
  ) except -1 nogil:
1389
1393
  cdef:
1390
1394
  bint masked = fold >= 0
@@ -1421,11 +1425,11 @@ cpdef int stepBatchQPackedW(
1421
1425
  continue
1422
1426
  if masked:
1423
1427
  _unpackPackedRowFold(
1424
- &B[marker, 0], g_thr, marker, N,
1428
+ packedRow(B, rows, marker), g_thr, marker, N,
1425
1429
  fold, folds, seed
1426
1430
  )
1427
1431
  else:
1428
- _unpackPackedRow(&B[marker, 0], g_thr, N)
1432
+ _unpackPackedRow(packedRow(B, rows, marker), g_thr, N)
1429
1433
  if not _specializedQRowW(
1430
1434
  g_thr, &P[marker, 0], &Q[0, 0], q_thr, wt[j], N, K
1431
1435
  ):
@@ -1718,13 +1722,14 @@ cdef int _tiledEm(
1718
1722
  const f64[:, ::1] P,
1719
1723
  f64[:, ::1] P1, const f64[:, ::1] Q, f64[:, ::1] T,
1720
1724
  const Py_ssize_t N, const u32[::1] s=None, const u32[::1] weights=None,
1721
- Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0
1725
+ Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0,
1726
+ const uintptr_t[::1] rows=None
1722
1727
  ) except -1 nogil:
1723
1728
  cdef:
1724
1729
  bint gl = L is not None
1725
- Py_ssize_t M = (L.shape[0] if gl else B.shape[0]) if s is None else s.shape[0]
1730
+ Py_ssize_t M = (L.shape[0] if gl else packedCount(B, rows)) if s is None else s.shape[0]
1726
1731
  Py_ssize_t K = Q.shape[1]
1727
- Py_ssize_t c = min(128, max(1, _EM_DATA_TILE_BYTES // (12 * N if gl else B.shape[1])))
1732
+ Py_ssize_t c = min(128, max(1, _EM_DATA_TILE_BYTES // (12 * N if gl else (N + 3) // 4)))
1728
1733
  Py_ssize_t n_blk = min(4096, max(16, _EM_STATS_TILE_BYTES // (8 * K)))
1729
1734
  Py_ssize_t mb, nb, b, e, beg, end, j, marker, z, i, k, x, r
1730
1735
  int tid, n_thr, n_use
@@ -1819,16 +1824,18 @@ cdef int _tiledEm(
1819
1824
  if not masked:
1820
1825
  i = 0
1821
1826
  while i < r and ((z + i) & 3):
1822
- g_buf[i] = decodePacked(&B[marker, 0], z + i)
1827
+ g_buf[i] = decodePacked(packedRow(B, rows, marker), z + i)
1823
1828
  i = i + 1
1824
1829
  if i < r:
1825
- _unpackPackedRow(&B[marker, (z + i) >> 2], g_buf + i, r - i)
1830
+ _unpackPackedRow(
1831
+ packedRow(B, rows, marker) + ((z + i) >> 2), g_buf + i, r - i
1832
+ )
1826
1833
  else:
1827
1834
  for i in range(r):
1828
1835
  if heldOut(marker, z + i, N, fold, folds, seed):
1829
1836
  g_buf[i] = 9
1830
1837
  else:
1831
- g_buf[i] = decodePacked(&B[marker, 0], z + i)
1838
+ g_buf[i] = decodePacked(packedRow(B, rows, marker), z + i)
1832
1839
  if fixed:
1833
1840
  if not _tiledQRowW(g, &P[marker, 0], &Q[z, 0], &T[z, 0], w, r, K):
1834
1841
  fm_q_weight_generic(&P[marker, 0], &Q[z, 0], &T[z, 0], g, r, w, K)
@@ -1859,9 +1866,10 @@ cpdef int tiledPacked(
1859
1866
  const u8[:, ::1] B, const f64[:, ::1] P,
1860
1867
  f64[:, ::1] P1, const f64[:, ::1] Q, f64[:, ::1] T,
1861
1868
  const Py_ssize_t N, const u32[::1] s=None, const u32[::1] weights=None,
1862
- Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0
1869
+ Py_ssize_t fold=-1, Py_ssize_t folds=1, uint64_t seed=0,
1870
+ const uintptr_t[::1] rows=None
1863
1871
  ) except -1 nogil:
1864
- return _tiledEm(B, None, P, P1, Q, T, N, s, weights, fold, folds, seed)
1872
+ return _tiledEm(B, None, P, P1, Q, T, N, s, weights, fold, folds, seed, rows)
1865
1873
 
1866
1874
 
1867
1875
  ### Apply one likelihood full, indexed, or weighted tiled EM map