fastmixture2 2.0.3__tar.gz → 2.0.5__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 (72) hide show
  1. {fastmixture2-2.0.3/fastmixture2.egg-info → fastmixture2-2.0.5}/PKG-INFO +64 -54
  2. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/README.md +61 -51
  3. fastmixture2-2.0.5/fastmixture2/__init__.py +1 -0
  4. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/defaults.py +3 -3
  5. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/em.pyx +41 -42
  6. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/fastmixture2.py +32 -22
  7. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/functions.py +102 -18
  8. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/gpu.py +682 -515
  9. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/kernels.py +54 -20
  10. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/main.py +13 -2
  11. fastmixture2-2.0.5/fastmixture2/packed.pxd +277 -0
  12. fastmixture2-2.0.5/fastmixture2/plink.py +119 -0
  13. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/shared.pyx +50 -49
  14. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/sqp.pyx +62 -20
  15. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/svd.pyx +28 -8
  16. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/utils.py +273 -121
  17. {fastmixture2-2.0.3 → fastmixture2-2.0.5/fastmixture2.egg-info}/PKG-INFO +64 -54
  18. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2.egg-info/SOURCES.txt +8 -4
  19. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/pyproject.toml +3 -2
  20. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/check_portable.py +6 -1
  21. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/helpers.py +28 -2
  22. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_adversarial_io.py +1 -1
  23. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_bootstrap.py +2 -2
  24. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_cli.py +41 -19
  25. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_convergence.py +2 -2
  26. fastmixture2-2.0.5/tests/test_cpu_subset.py +333 -0
  27. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_cross_validation.py +9 -16
  28. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_frequency_cache.py +15 -19
  29. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_genotypes.py +44 -1
  30. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_called.py +35 -4
  31. fastmixture2-2.0.5/tests/test_gpu_em.py +256 -0
  32. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_gl_precision.py +30 -7
  33. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_large.py +4 -5
  34. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_modes.py +14 -8
  35. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_precision.py +323 -10
  36. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_safety.py +133 -15
  37. fastmixture2-2.0.5/tests/test_gpu_streamed.py +426 -0
  38. fastmixture2-2.0.5/tests/test_gpu_subset.py +503 -0
  39. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_svd_cache.py +32 -9
  40. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_initialization.py +99 -14
  41. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_likelihoods.py +32 -35
  42. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_numerical_equivalence.py +114 -1
  43. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_optimizers.py +11 -11
  44. fastmixture2-2.0.5/tests/test_plink_files.py +493 -0
  45. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_q_constraints.py +2 -2
  46. fastmixture2-2.0.5/tests/test_restarts.py +362 -0
  47. fastmixture2-2.0.5/tests/test_sqp_solver.py +435 -0
  48. fastmixture2-2.0.5/tests/test_subset_modes.py +360 -0
  49. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/validate_distribution.py +1 -0
  50. fastmixture2-2.0.3/fastmixture2/__init__.py +0 -1
  51. fastmixture2-2.0.3/fastmixture2/packed.pxd +0 -144
  52. fastmixture2-2.0.3/tests/test_gpu_als.py +0 -214
  53. fastmixture2-2.0.3/tests/test_gpu_boot_precision.py +0 -159
  54. fastmixture2-2.0.3/tests/test_gpu_em_reduction.py +0 -114
  55. fastmixture2-2.0.3/tests/test_gpu_native_precision.py +0 -76
  56. fastmixture2-2.0.3/tests/test_sqp_solver.py +0 -177
  57. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/LICENSE +0 -0
  58. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/MANIFEST.in +0 -0
  59. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2/threads.pxd +0 -0
  60. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2.egg-info/dependency_links.txt +0 -0
  61. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2.egg-info/entry_points.txt +0 -0
  62. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2.egg-info/requires.txt +0 -0
  63. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/fastmixture2.egg-info/top_level.txt +0 -0
  64. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/setup.cfg +0 -0
  65. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/setup.py +0 -0
  66. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/run_tests.py +0 -0
  67. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_cpu_scaling.py +0 -0
  68. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_distribution.py +0 -0
  69. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_stat_reuse.py +0 -0
  70. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_gpu_supervision.py +0 -0
  71. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_sqp_scoring.py +0 -0
  72. {fastmixture2-2.0.3 → fastmixture2-2.0.5}/tests/test_threads.py +0 -0
@@ -1,8 +1,8 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastmixture2
3
- Version: 2.0.3
4
- Summary: Fast and scalable memory-aware maximum-likelihood ancestry estimation
5
- Author-email: Jonas Meisner <meisnerucph@gmail.com>
3
+ Version: 2.0.5
4
+ Summary: Biobank-scale ancestry estimation in unrelated individuals
5
+ Author-email: "J. Meisner" <meisnerucph@gmail.com>, "L. Thunegard" <lisathunegard@icloud.com>
6
6
  License-Expression: GPL-3.0-only
7
7
  Project-URL: Homepage, https://github.com/MeisnerLab/fastmixture2
8
8
  Project-URL: Documentation, https://github.com/MeisnerLab/fastmixture2#readme
@@ -32,13 +32,14 @@ Dynamic: license-file
32
32
  # fastmixture2
33
33
 
34
34
  [![Tests](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg)](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml)
35
- [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)
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)
36
37
 
37
38
  `fastmixture2` estimates ancestry proportions (`Q`) and ancestral allele
38
39
  frequencies (`P`) by maximum likelihood. It reads SNP-major PLINK genotypes
39
40
  or ANGSD Beagle genotype likelihoods. Unsupervised initialization uses SVD/ALS
40
- for K > 1. All modes use safeguarded SQP/ZAL updates on CPU or, with `--gpu`,
41
- on NVIDIA CUDA.
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.
42
43
 
43
44
  ## Installation
44
45
 
@@ -58,7 +59,8 @@ Source builds need a C compiler with OpenMP. On macOS, install OpenMP with
58
59
  `brew install libomp`. On HPC, build on the compute-node type used for your
59
60
  analyses.
60
61
 
61
- With Conda, the [CPU](environment-cpu.yml) and [GPU](environment-gpu.yml)
62
+ With Conda, the [CPU](https://github.com/MeisnerLab/fastmixture2/blob/main/environment-cpu.yml)
63
+ and [GPU](https://github.com/MeisnerLab/fastmixture2/blob/main/environment-gpu.yml)
62
64
  environments retain native CPU tuning. Run from the cloned repository:
63
65
 
64
66
  ```bash
@@ -69,7 +71,7 @@ conda activate fastmixture2-cpu
69
71
  For GPU use, select `environment-gpu.yml` and activate `fastmixture2-gpu`.
70
72
 
71
73
  PyPI wheels cover Linux (glibc) and macOS 14 or newer, on x86-64 and ARM64.
72
- They are portable and select SIMD kernels when available:
74
+ They use AVX2 on supported x86-64 CPUs and NEON on ARM64, including Apple Silicon:
73
75
 
74
76
  ```bash
75
77
  python -m pip install fastmixture2
@@ -109,11 +111,25 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --out cohort
109
111
  fastmixture2 --beagle cohort.beagle.gz --K 5 --threads 8 --out cohort.gl
110
112
  ```
111
113
 
112
- - `--bfile` takes the shared prefix of SNP-major PLINK `.bed`, `.bim` and `.fam`
113
- files. Genotypes stay packed on CPU and GPU. Missing genotypes are supported.
114
+ - `--bfile` takes one or more ordered prefixes of SNP-major PLINK `.bed`, `.bim`
115
+ and `.fam` files. Genotypes stay packed on CPU and GPU. Missing genotypes are
116
+ supported.
114
117
  - `--beagle` takes a gzipped ANGSD Beagle file. Likelihood triplets are normalized
115
118
  internally. Exactly uniform triplets contribute no ancestry information.
116
119
 
120
+ Per-chromosome inputs are fitted jointly without creating a merged BED:
121
+
122
+ ```bash
123
+ fastmixture2 --bfile chr{1..22} --K 5 --threads 8 --out cohort
124
+ ```
125
+
126
+ The shell expands `chr{1..22}` in chromosome order. Explicit prefixes also work,
127
+ such as `--bfile chr1 chr2 chr3`. All FAM files must have identical FID/IID pairs
128
+ in the same order. Marker rows, including annotated P output, follow the supplied
129
+ file order. The files remain memory-mapped, with an additional native address
130
+ per marker (8 bytes on 64-bit systems) and bounded transfer tiles. This works
131
+ with CPU, CUDA, supervised estimation, projection, CV, bootstrap and checkpoints.
132
+
117
133
  Perform sample and variant QC first. For each K, try several seeds and keep
118
134
  the converged fit with the highest log-likelihood. Component labels may change
119
135
  between runs, so align them before comparing Q or P.
@@ -123,6 +139,15 @@ frequencies. It uses `--seed` and works with either input format on CPU or CUDA
123
139
  in unsupervised mode. With `--exact-cv`, each fold also starts randomly.
124
140
  Resuming a checkpoint retains its saved estimates.
125
141
 
142
+ Add `--restarts N` to try N additional restart attempts in unsupervised mode (default 0).
143
+ Each starts from the original finished fit, permutes Q columns while retaining P, and
144
+ jointly refines a fresh genome-wide 50,000-SNP subset. Smaller panels use all SNPs.
145
+ Larger panels then project P and refit the full data. Only full-data improvements are
146
+ retained. This works with both input formats on CPU and CUDA, uses `--seed`,
147
+ and keeps the same `--iter` and `--tole` per stage. CV and bootstrap retain their
148
+ usual refit settings.
149
+ At K = 1 it has no effect.
150
+
126
151
  Add `--gpu` to use CUDA with either input format:
127
152
 
128
153
  ```bash
@@ -132,53 +157,36 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --gpu --out cohort.gpu
132
157
  CPU and CUDA support `1 <= K <= 128` for both input formats in all modes.
133
158
  CUDA keeps input data on the GPU when they fit and streams in blocks otherwise.
134
159
  Small CPU/GPU differences can arise from floating-point arithmetic.
135
- At K > 1, CUDA SVD starts at 256 samples. Large ALS factors at K <= 8 use
136
- CUDA ALS with float64 contractions. Smaller or high-K factors retain CPU ALS.
137
- Resident PLINK CUDA fits use float32 SQP statistics while gains remain material,
138
- then switch to float64 refinement. There is no fixed float32 iteration cap.
139
- Parameters, constrained solves and likelihood checks stay in float64.
140
- PLINK cuBLAS uses float32 tile products with float64 accumulation.
141
-
142
- Genotype-likelihood fits use Q-first SQP/ZAL on both CPU and CUDA, with exact
143
- EM/QN rescue and an independent EM check before convergence. Resident CUDA
144
- fits start with float32 probabilities, derivatives and statistics,
145
- then switch to float64 refinement. Near frequency bounds, GL probabilities
146
- and derivatives use float64. cuBLAS tile sums, parameters, solves and scores stay
147
- in float64.
148
- CPU and CUDA start directly with SQP. `--iter` limits refinement.
149
- EM skips missing genotypes and exactly uniform likelihood triplets, with
150
- normalization over informative entries. Nonuniform likelihoods are retained.
151
- Full likelihood scores retain the constant contribution from uniform triplets.
152
-
153
- CPU SQP uses tiled BLAS statistics for Q and P at every supported K.
154
- This applies to PLINK and genotype likelihoods in all modes.
155
- BLAS scratch and P statistics each have a separate 512 MiB cap. Q statistics
156
- use 4096-sample shards with a separate 4 GiB cap. Optional update caches
157
- remain capped at 64 MiB. Input storage, factors and optimizer history are
158
- additional allocations.
159
-
160
- Resident CUDA uses native kernels at K = 1..3 and tiled cuBLAS for Q and P
161
- at K = 4..16 during float32 refinement, regardless of input format or dataset size.
162
- Tiles share at most 64 MiB of scratch and keep float32 copies of P and Q.
163
- Solves and likelihood checks remain in float64. At K <= 16, final refinement
164
- and streamed fits use native float64 kernels, except resident GL P updates at
165
- K = 8..16 with up to 1024 samples. Those share the tiled cuBLAS P routine and
166
- keep combined scratch within 64 MiB.
167
- Unsupervised resident PLINK fits at K = 10..16 keep the first eight iterations
168
- fresh, then alternate Q-Hessian reuse during float32 refinement. The cache is
169
- capped at 64 MiB. Weak cached proposals retry with fresh curvature, and
170
- float64 refinement always uses fresh curvature.
171
-
172
- At K > 16, CUDA uses tiled cuBLAS statistics and a cooperative active-set
173
- solver for both input formats. Resident and bootstrap maps share 64 MiB of
174
- scratch. Streamed maps allow up to 128 MiB of compute scratch and 512 MiB of
175
- Q statistics, with free-VRAM guards. Curvature stays fresh. Larger K still
176
- increases compute cost, especially for the quadratic solves.
160
+
161
+ At K > 1, unsupervised initialization uses 10 SVD power iterations on all SNPs
162
+ up to 50,000, or a genome-wide 50,000-SNP subset otherwise. Larger panels refine
163
+ subset Q/P with SQP/ZAL, then fit full-marker P by least squares at fixed Q and
164
+ retain the refined subset rows. This applies to both input formats, including
165
+ missing genotypes.
166
+ Panels up to 50,000 SNPs proceed directly from SVD/ALS to full-data refinement,
167
+ without a duplicate subset fit or projection. For PLINK, subset refinement is
168
+ skipped if any sample has no observed subset genotypes. Exact CV folds skip
169
+ subset likelihood refinement.
170
+
171
+ Subsets exceeding 512 MiB on CPU or 256 MiB on CUDA are built in bounded blocks
172
+ into a temporary memory-mapped file. CUDA streams the subset when it does not
173
+ fit in VRAM. Temporary files use `TMPDIR`, which can point to disk-backed scratch
174
+ storage.
175
+
176
+ Refinement uses safeguarded SQP/ZAL with EM/QN rescue when needed, for both
177
+ input formats and all modes. CPU refinement uses float64. CUDA uses mixed
178
+ SQP statistics before final float64 refinement. Parameters, constrained solves,
179
+ EM/QN and likelihood checks stay in float64. EM/QN runs on CUDA for both
180
+ resident and streamed input. PLINK rescue falls back to CPU if a CUDA allocation
181
+ fails.
182
+
183
+ Workspaces use bounded blocks. Input storage, Q, P and optimizer history are
184
+ additional allocations that grow with panel size and K.
177
185
 
178
186
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
179
187
  `--threads` caps CPU computation, including BLAS and GPU host copies.
180
188
  The optimizer scales the tolerance by observed genotypes or informative
181
- likelihood triplets.
189
+ likelihood triplets. `--iter` and `--tole` apply to each refinement stage.
182
190
  Check the log for convergence. Run `fastmixture2 --help` for the full CLI.
183
191
 
184
192
  ### Beagle cache
@@ -298,6 +306,8 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --resume run.ckpt --out cohort
298
306
  ```
299
307
 
300
308
  Keep the same input, supervision and projection files when resuming.
309
+ With `--restarts`, checkpoints retain the best full-data fit. Resuming runs the
310
+ requested number of additional restarts after the resumed fit.
301
311
 
302
312
  ## Output
303
313
 
@@ -333,4 +343,4 @@ fastmixture2 does not create filesystem locks or require private lock directorie
333
343
  ## Authors and license
334
344
 
335
345
  Lisa Thunegard and Jonas Meisner. Developed at **MeisnerLab** and distributed
336
- under the [GNU General Public License v3.0](LICENSE).
346
+ under the [GNU General Public License v3.0](https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE).
@@ -1,13 +1,14 @@
1
1
  # fastmixture2
2
2
 
3
3
  [![Tests](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml/badge.svg)](https://github.com/MeisnerLab/fastmixture2/actions/workflows/ci.yml)
4
- [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](LICENSE)
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)
5
6
 
6
7
  `fastmixture2` estimates ancestry proportions (`Q`) and ancestral allele
7
8
  frequencies (`P`) by maximum likelihood. It reads SNP-major PLINK genotypes
8
9
  or ANGSD Beagle genotype likelihoods. Unsupervised initialization uses SVD/ALS
9
- for K > 1. All modes use safeguarded SQP/ZAL updates on CPU or, with `--gpu`,
10
- on NVIDIA CUDA.
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.
11
12
 
12
13
  ## Installation
13
14
 
@@ -27,7 +28,8 @@ Source builds need a C compiler with OpenMP. On macOS, install OpenMP with
27
28
  `brew install libomp`. On HPC, build on the compute-node type used for your
28
29
  analyses.
29
30
 
30
- With Conda, the [CPU](environment-cpu.yml) and [GPU](environment-gpu.yml)
31
+ With Conda, the [CPU](https://github.com/MeisnerLab/fastmixture2/blob/main/environment-cpu.yml)
32
+ and [GPU](https://github.com/MeisnerLab/fastmixture2/blob/main/environment-gpu.yml)
31
33
  environments retain native CPU tuning. Run from the cloned repository:
32
34
 
33
35
  ```bash
@@ -38,7 +40,7 @@ conda activate fastmixture2-cpu
38
40
  For GPU use, select `environment-gpu.yml` and activate `fastmixture2-gpu`.
39
41
 
40
42
  PyPI wheels cover Linux (glibc) and macOS 14 or newer, on x86-64 and ARM64.
41
- They are portable and select SIMD kernels when available:
43
+ They use AVX2 on supported x86-64 CPUs and NEON on ARM64, including Apple Silicon:
42
44
 
43
45
  ```bash
44
46
  python -m pip install fastmixture2
@@ -78,11 +80,25 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --out cohort
78
80
  fastmixture2 --beagle cohort.beagle.gz --K 5 --threads 8 --out cohort.gl
79
81
  ```
80
82
 
81
- - `--bfile` takes the shared prefix of SNP-major PLINK `.bed`, `.bim` and `.fam`
82
- files. Genotypes stay packed on CPU and GPU. Missing genotypes are supported.
83
+ - `--bfile` takes one or more ordered prefixes of SNP-major PLINK `.bed`, `.bim`
84
+ and `.fam` files. Genotypes stay packed on CPU and GPU. Missing genotypes are
85
+ supported.
83
86
  - `--beagle` takes a gzipped ANGSD Beagle file. Likelihood triplets are normalized
84
87
  internally. Exactly uniform triplets contribute no ancestry information.
85
88
 
89
+ Per-chromosome inputs are fitted jointly without creating a merged BED:
90
+
91
+ ```bash
92
+ fastmixture2 --bfile chr{1..22} --K 5 --threads 8 --out cohort
93
+ ```
94
+
95
+ The shell expands `chr{1..22}` in chromosome order. Explicit prefixes also work,
96
+ such as `--bfile chr1 chr2 chr3`. All FAM files must have identical FID/IID pairs
97
+ in the same order. Marker rows, including annotated P output, follow the supplied
98
+ file order. The files remain memory-mapped, with an additional native address
99
+ per marker (8 bytes on 64-bit systems) and bounded transfer tiles. This works
100
+ with CPU, CUDA, supervised estimation, projection, CV, bootstrap and checkpoints.
101
+
86
102
  Perform sample and variant QC first. For each K, try several seeds and keep
87
103
  the converged fit with the highest log-likelihood. Component labels may change
88
104
  between runs, so align them before comparing Q or P.
@@ -92,6 +108,15 @@ frequencies. It uses `--seed` and works with either input format on CPU or CUDA
92
108
  in unsupervised mode. With `--exact-cv`, each fold also starts randomly.
93
109
  Resuming a checkpoint retains its saved estimates.
94
110
 
111
+ Add `--restarts N` to try N additional restart attempts in unsupervised mode (default 0).
112
+ Each starts from the original finished fit, permutes Q columns while retaining P, and
113
+ jointly refines a fresh genome-wide 50,000-SNP subset. Smaller panels use all SNPs.
114
+ Larger panels then project P and refit the full data. Only full-data improvements are
115
+ retained. This works with both input formats on CPU and CUDA, uses `--seed`,
116
+ and keeps the same `--iter` and `--tole` per stage. CV and bootstrap retain their
117
+ usual refit settings.
118
+ At K = 1 it has no effect.
119
+
95
120
  Add `--gpu` to use CUDA with either input format:
96
121
 
97
122
  ```bash
@@ -101,53 +126,36 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --gpu --out cohort.gpu
101
126
  CPU and CUDA support `1 <= K <= 128` for both input formats in all modes.
102
127
  CUDA keeps input data on the GPU when they fit and streams in blocks otherwise.
103
128
  Small CPU/GPU differences can arise from floating-point arithmetic.
104
- At K > 1, CUDA SVD starts at 256 samples. Large ALS factors at K <= 8 use
105
- CUDA ALS with float64 contractions. Smaller or high-K factors retain CPU ALS.
106
- Resident PLINK CUDA fits use float32 SQP statistics while gains remain material,
107
- then switch to float64 refinement. There is no fixed float32 iteration cap.
108
- Parameters, constrained solves and likelihood checks stay in float64.
109
- PLINK cuBLAS uses float32 tile products with float64 accumulation.
110
-
111
- Genotype-likelihood fits use Q-first SQP/ZAL on both CPU and CUDA, with exact
112
- EM/QN rescue and an independent EM check before convergence. Resident CUDA
113
- fits start with float32 probabilities, derivatives and statistics,
114
- then switch to float64 refinement. Near frequency bounds, GL probabilities
115
- and derivatives use float64. cuBLAS tile sums, parameters, solves and scores stay
116
- in float64.
117
- CPU and CUDA start directly with SQP. `--iter` limits refinement.
118
- EM skips missing genotypes and exactly uniform likelihood triplets, with
119
- normalization over informative entries. Nonuniform likelihoods are retained.
120
- Full likelihood scores retain the constant contribution from uniform triplets.
121
-
122
- CPU SQP uses tiled BLAS statistics for Q and P at every supported K.
123
- This applies to PLINK and genotype likelihoods in all modes.
124
- BLAS scratch and P statistics each have a separate 512 MiB cap. Q statistics
125
- use 4096-sample shards with a separate 4 GiB cap. Optional update caches
126
- remain capped at 64 MiB. Input storage, factors and optimizer history are
127
- additional allocations.
128
-
129
- Resident CUDA uses native kernels at K = 1..3 and tiled cuBLAS for Q and P
130
- at K = 4..16 during float32 refinement, regardless of input format or dataset size.
131
- Tiles share at most 64 MiB of scratch and keep float32 copies of P and Q.
132
- Solves and likelihood checks remain in float64. At K <= 16, final refinement
133
- and streamed fits use native float64 kernels, except resident GL P updates at
134
- K = 8..16 with up to 1024 samples. Those share the tiled cuBLAS P routine and
135
- keep combined scratch within 64 MiB.
136
- Unsupervised resident PLINK fits at K = 10..16 keep the first eight iterations
137
- fresh, then alternate Q-Hessian reuse during float32 refinement. The cache is
138
- capped at 64 MiB. Weak cached proposals retry with fresh curvature, and
139
- float64 refinement always uses fresh curvature.
140
-
141
- At K > 16, CUDA uses tiled cuBLAS statistics and a cooperative active-set
142
- solver for both input formats. Resident and bootstrap maps share 64 MiB of
143
- scratch. Streamed maps allow up to 128 MiB of compute scratch and 512 MiB of
144
- Q statistics, with free-VRAM guards. Curvature stays fresh. Larger K still
145
- increases compute cost, especially for the quadratic solves.
129
+
130
+ At K > 1, unsupervised initialization uses 10 SVD power iterations on all SNPs
131
+ up to 50,000, or a genome-wide 50,000-SNP subset otherwise. Larger panels refine
132
+ subset Q/P with SQP/ZAL, then fit full-marker P by least squares at fixed Q and
133
+ retain the refined subset rows. This applies to both input formats, including
134
+ missing genotypes.
135
+ Panels up to 50,000 SNPs proceed directly from SVD/ALS to full-data refinement,
136
+ without a duplicate subset fit or projection. For PLINK, subset refinement is
137
+ skipped if any sample has no observed subset genotypes. Exact CV folds skip
138
+ subset likelihood refinement.
139
+
140
+ Subsets exceeding 512 MiB on CPU or 256 MiB on CUDA are built in bounded blocks
141
+ into a temporary memory-mapped file. CUDA streams the subset when it does not
142
+ fit in VRAM. Temporary files use `TMPDIR`, which can point to disk-backed scratch
143
+ storage.
144
+
145
+ Refinement uses safeguarded SQP/ZAL with EM/QN rescue when needed, for both
146
+ input formats and all modes. CPU refinement uses float64. CUDA uses mixed
147
+ SQP statistics before final float64 refinement. Parameters, constrained solves,
148
+ EM/QN and likelihood checks stay in float64. EM/QN runs on CUDA for both
149
+ resident and streamed input. PLINK rescue falls back to CPU if a CUDA allocation
150
+ fails.
151
+
152
+ Workspaces use bounded blocks. Input storage, Q, P and optimizer history are
153
+ additional allocations that grow with panel size and K.
146
154
 
147
155
  The defaults are `--threads 1`, `--seed 42`, `--iter 1000` and `--tole 1e-10`.
148
156
  `--threads` caps CPU computation, including BLAS and GPU host copies.
149
157
  The optimizer scales the tolerance by observed genotypes or informative
150
- likelihood triplets.
158
+ likelihood triplets. `--iter` and `--tole` apply to each refinement stage.
151
159
  Check the log for convergence. Run `fastmixture2 --help` for the full CLI.
152
160
 
153
161
  ### Beagle cache
@@ -267,6 +275,8 @@ fastmixture2 --bfile cohort --K 5 --threads 8 --resume run.ckpt --out cohort
267
275
  ```
268
276
 
269
277
  Keep the same input, supervision and projection files when resuming.
278
+ With `--restarts`, checkpoints retain the best full-data fit. Resuming runs the
279
+ requested number of additional restarts after the resumed fit.
270
280
 
271
281
  ## Output
272
282
 
@@ -302,4 +312,4 @@ fastmixture2 does not create filesystem locks or require private lock directorie
302
312
  ## Authors and license
303
313
 
304
314
  Lisa Thunegard and Jonas Meisner. Developed at **MeisnerLab** and distributed
305
- under the [GNU General Public License v3.0](LICENSE).
315
+ under the [GNU General Public License v3.0](https://github.com/MeisnerLab/fastmixture2/blob/main/LICENSE).
@@ -0,0 +1 @@
1
+ __version__ = "2.0.5"
@@ -14,9 +14,9 @@ LIKELIHOOD_CACHE_HEADER_BYTES = 256
14
14
  LIKELIHOOD_CACHE_VERSION = 5
15
15
  LIKELIHOOD_FREQUENCY_VERSION = 2
16
16
 
17
- # SVD/ALS initialization
18
- INITIALIZATION_MARKERS = 100_000
19
- SVD_POWER_ITERATIONS = 5
17
+ # Initialization
18
+ INITIALIZATION_MARKERS = 50_000
19
+ SVD_POWER_ITERATIONS = 10
20
20
  SVD_CHUNK_MARKERS = 4_096
21
21
  SVD_WORKSPACE_BYTES = 512 << 20
22
22
  GPU_SVD_WORKSPACE_BYTES = 256 << 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:
@@ -1244,15 +1245,18 @@ cpdef int accelPacked(
1244
1245
  return 0
1245
1246
 
1246
1247
 
1247
- ### Form one weighted packed marker-bootstrap EM map
1248
- cdef int _batchPacked(
1248
+ ### Form one multiplicity-weighted packed marker-bootstrap EM map
1249
+ cpdef int batchPackedW(
1249
1250
  const u8[:, ::1] B, const f64[:, ::1] P,
1250
1251
  f64[:, ::1] P1, const f64[:, ::1] Q,
1251
- f64[:, ::1] T, const u32[::1] s, const u32* w,
1252
- const Py_ssize_t N, const bint masked, const Py_ssize_t fold,
1253
- const Py_ssize_t folds, const uint64_t seed
1252
+ f64[:, ::1] T, const u32[::1] s, const u32[::1] w,
1253
+ const Py_ssize_t N, Py_ssize_t fold=-1,
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:
1258
+ bint masked = fold >= 0
1259
+ const u32* wt = &w[0]
1256
1260
  u8* g_thr
1257
1261
  u8* g_all
1258
1262
  const f64* p
@@ -1290,19 +1294,19 @@ cdef int _batchPacked(
1290
1294
  for j in prange(M, schedule="static"):
1291
1295
  marker = s[j]
1292
1296
  p = &P[marker, 0]
1293
- if w[j] == 0:
1297
+ if wt[j] == 0:
1294
1298
  for k in range(K):
1295
1299
  P1[marker, k] = p[k]
1296
1300
  continue
1297
1301
  if masked:
1298
1302
  _unpackPackedRowFold(
1299
- &B[marker, 0], g_thr, marker, N,
1303
+ packedRow(B, rows, marker), g_thr, marker, N,
1300
1304
  fold, folds, seed
1301
1305
  )
1302
1306
  else:
1303
- _unpackPackedRow(&B[marker, 0], g_thr, N)
1307
+ _unpackPackedRow(packedRow(B, rows, marker), g_thr, N)
1304
1308
  if not _specializedEmRowW(
1305
- g_thr, p, &Q[0, 0], &P1[marker, 0], q_thr, w[j], N, K
1309
+ g_thr, p, &Q[0, 0], &P1[marker, 0], q_thr, wt[j], N, K
1306
1310
  ):
1307
1311
  for i in range(N):
1308
1312
  if g_thr[i] == 9:
@@ -1311,7 +1315,7 @@ cdef int _batchPacked(
1311
1315
  h = _computeH(p, q, K)
1312
1316
  _innerJW(
1313
1317
  p, q, &p_thr[0], &p_thr[K], &q_thr[i * K],
1314
- g_thr[i], w[j], K, h
1318
+ g_thr[i], wt[j], K, h
1315
1319
  )
1316
1320
  _outerAccelP(p, &P1[marker, 0], &p_thr[0], &p_thr[K], K)
1317
1321
  for x in prange(L, schedule="static"):
@@ -1323,23 +1327,13 @@ cdef int _batchPacked(
1323
1327
  return 0
1324
1328
 
1325
1329
 
1326
- ### Form one multiplicity-weighted packed marker-bootstrap EM map
1327
- cpdef int batchPackedW(
1328
- const u8[:, ::1] B, const f64[:, ::1] P, f64[:, ::1] P1,
1329
- const f64[:, ::1] Q, f64[:, ::1] T,
1330
- const u32[::1] s, const u32[::1] w,
1331
- const Py_ssize_t N, Py_ssize_t fold=-1,
1332
- Py_ssize_t folds=1, uint64_t seed=0
1333
- ) except -1 nogil:
1334
- return _batchPacked(B, P, P1, Q, T, s, &w[0], N, fold >= 0, fold, folds, seed)
1335
-
1336
-
1337
1330
  ### Form one packed fixed-P Q map
1338
1331
  cpdef int stepQPacked(
1339
1332
  const u8[:, ::1] B, const f64[:, ::1] P,
1340
1333
  const f64[:, ::1] Q, f64[:, ::1] T,
1341
1334
  const Py_ssize_t N, Py_ssize_t fold=-1,
1342
- 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
1343
1337
  ) except -1 nogil:
1344
1338
  cdef:
1345
1339
  bint masked = fold >= 0
@@ -1348,7 +1342,7 @@ cpdef int stepQPacked(
1348
1342
  f64* q_thr
1349
1343
  f64* q_all
1350
1344
  f64* t = &T[0, 0]
1351
- Py_ssize_t M = B.shape[0]
1345
+ Py_ssize_t M = packedCount(B, rows)
1352
1346
  Py_ssize_t K = Q.shape[1]
1353
1347
  Py_ssize_t L
1354
1348
  Py_ssize_t i, j, x
@@ -1371,9 +1365,9 @@ cpdef int stepQPacked(
1371
1365
  g_thr = &g_all[tid * N]
1372
1366
  for j in prange(M, schedule="static"):
1373
1367
  if masked:
1374
- _unpackPackedRowFold(&B[j, 0], g_thr, j, N, fold, folds, seed)
1368
+ _unpackPackedRowFold(packedRow(B, rows, j), g_thr, j, N, fold, folds, seed)
1375
1369
  else:
1376
- _unpackPackedRow(&B[j, 0], g_thr, N)
1370
+ _unpackPackedRow(packedRow(B, rows, j), g_thr, N)
1377
1371
  if not _specializedQRow(g_thr, &P[j, 0], &Q[0, 0], q_thr, N, K):
1378
1372
  for i in range(N):
1379
1373
  if g_thr[i] == 9:
@@ -1393,7 +1387,8 @@ cpdef int stepBatchQPackedW(
1393
1387
  const u8[:, ::1] B, const f64[:, ::1] P, const f64[:, ::1] Q,
1394
1388
  f64[:, ::1] T, const u32[::1] s, const u32[::1] w,
1395
1389
  const Py_ssize_t N, Py_ssize_t fold=-1,
1396
- 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
1397
1392
  ) except -1 nogil:
1398
1393
  cdef:
1399
1394
  bint masked = fold >= 0
@@ -1430,11 +1425,11 @@ cpdef int stepBatchQPackedW(
1430
1425
  continue
1431
1426
  if masked:
1432
1427
  _unpackPackedRowFold(
1433
- &B[marker, 0], g_thr, marker, N,
1428
+ packedRow(B, rows, marker), g_thr, marker, N,
1434
1429
  fold, folds, seed
1435
1430
  )
1436
1431
  else:
1437
- _unpackPackedRow(&B[marker, 0], g_thr, N)
1432
+ _unpackPackedRow(packedRow(B, rows, marker), g_thr, N)
1438
1433
  if not _specializedQRowW(
1439
1434
  g_thr, &P[marker, 0], &Q[0, 0], q_thr, wt[j], N, K
1440
1435
  ):
@@ -1727,13 +1722,14 @@ cdef int _tiledEm(
1727
1722
  const f64[:, ::1] P,
1728
1723
  f64[:, ::1] P1, const f64[:, ::1] Q, f64[:, ::1] T,
1729
1724
  const Py_ssize_t N, const u32[::1] s=None, const u32[::1] weights=None,
1730
- 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
1731
1727
  ) except -1 nogil:
1732
1728
  cdef:
1733
1729
  bint gl = L is not None
1734
- 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]
1735
1731
  Py_ssize_t K = Q.shape[1]
1736
- 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)))
1737
1733
  Py_ssize_t n_blk = min(4096, max(16, _EM_STATS_TILE_BYTES // (8 * K)))
1738
1734
  Py_ssize_t mb, nb, b, e, beg, end, j, marker, z, i, k, x, r
1739
1735
  int tid, n_thr, n_use
@@ -1828,16 +1824,18 @@ cdef int _tiledEm(
1828
1824
  if not masked:
1829
1825
  i = 0
1830
1826
  while i < r and ((z + i) & 3):
1831
- g_buf[i] = decodePacked(&B[marker, 0], z + i)
1827
+ g_buf[i] = decodePacked(packedRow(B, rows, marker), z + i)
1832
1828
  i = i + 1
1833
1829
  if i < r:
1834
- _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
+ )
1835
1833
  else:
1836
1834
  for i in range(r):
1837
1835
  if heldOut(marker, z + i, N, fold, folds, seed):
1838
1836
  g_buf[i] = 9
1839
1837
  else:
1840
- g_buf[i] = decodePacked(&B[marker, 0], z + i)
1838
+ g_buf[i] = decodePacked(packedRow(B, rows, marker), z + i)
1841
1839
  if fixed:
1842
1840
  if not _tiledQRowW(g, &P[marker, 0], &Q[z, 0], &T[z, 0], w, r, K):
1843
1841
  fm_q_weight_generic(&P[marker, 0], &Q[z, 0], &T[z, 0], g, r, w, K)
@@ -1868,9 +1866,10 @@ cpdef int tiledPacked(
1868
1866
  const u8[:, ::1] B, const f64[:, ::1] P,
1869
1867
  f64[:, ::1] P1, const f64[:, ::1] Q, f64[:, ::1] T,
1870
1868
  const Py_ssize_t N, const u32[::1] s=None, const u32[::1] weights=None,
1871
- 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
1872
1871
  ) except -1 nogil:
1873
- 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)
1874
1873
 
1875
1874
 
1876
1875
  ### Apply one likelihood full, indexed, or weighted tiled EM map