Python API¶
Pipeline¶
manifold_genetics.pipeline.runner.run_pipeline ¶
run_pipeline(fit_plink, project_plink, output_dir, labels=None, colormap=None, fit_labels=None, project_labels=None, fit_colormap=None, project_colormap=None, geographic_coords=None, n_pcs=50, pca_backend='python', max_fit_memory_gb=8.0, max_project_memory_gb=8.0, k_min=2, k_max=10, admix_threads=None, admix_gpus=None, admix_batch_size=400, admixture_backend=None, embedding='phate', embedding_params=None, embedding_input='both', admix_group_column=None, admix_within_group_order='chron', projection_plot_fit_column=None, projection_plot_project_column=None, skip_pca=False, skip_admixture=False, skip_embedding=False, skip_visualization=False, skip_pca_visualization=False, skip_admixture_visualization=False, skip_metrics=False)
Run the complete manifold-genetics pipeline.
This is the canonical entry point for running the full genetic analysis pipeline. It coordinates PCA, Admixture, Embeddings, Visualization, and Metrics computation.
Which fields are populated depends on what ran. admixture and
embedding are None when their stage was skipped, as are
geographic_metrics and admixture_metrics. Figure families are empty
rather than None.
pca is the exception: it is never None. Under skip_pca it is still a
PCAStepResult, with skipped=True and fit_pca/project_pca
filled only from cached output already on disk — possibly both None. Check
.skipped rather than truthiness to tell whether PCA ran.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_plink
|
Union[str, Path]
|
Path to fit subset PLINK files (prefix for .bed/.bim/.fam) |
required |
project_plink
|
Union[str, Path]
|
Path to project subset PLINK files (prefix for .bed/.bim/.fam) |
required |
output_dir
|
Union[str, Path]
|
Directory for all outputs |
required |
labels
|
Optional[Union[str, Path]]
|
Path to labels CSV (used for both fit and project if not overridden) |
None
|
colormap
|
Optional[Union[str, Path]]
|
Path to colormap JSON (used for both fit and project if not overridden) |
None
|
fit_labels
|
Optional[Union[str, Path]]
|
Optional override labels CSV for fit dataset |
None
|
project_labels
|
Optional[Union[str, Path]]
|
Optional override labels CSV for project dataset |
None
|
fit_colormap
|
Optional[Union[str, Path]]
|
Optional override colormap JSON for fit dataset |
None
|
project_colormap
|
Optional[Union[str, Path]]
|
Optional override colormap JSON for project dataset |
None
|
geographic_coords
|
Optional[Union[str, Path]]
|
Optional path to geographic coordinates CSV for metrics |
None
|
n_pcs
|
int
|
Number of principal components (default: 50) |
50
|
pca_backend
|
str
|
'flashpca' (external binary) or 'python' (in process) |
'python'
|
max_fit_memory_gb
|
float
|
GB budget for the dense standardised matrix when fitting the in-process PCA backend. Above it the fit streams, which bounds memory to about 110 MB at roughly nineteen times the wall clock. Raise it to match the node: a 60,000 x 120,849 cohort is 54 GB dense and streams under the default. |
8.0
|
max_project_memory_gb
|
float
|
GB budget for one chunk when projecting. Peak resident memory runs to about three times this. Both are ignored by the flashpca backend. |
8.0
|
k_min
|
int
|
Minimum K for admixture (default: 2) |
2
|
k_max
|
int
|
Maximum K for admixture (default: 10) |
10
|
admix_threads
|
Optional[int]
|
Number of threads for neural admixture (None = auto-detect) |
None
|
admix_gpus
|
Optional[int]
|
Number of GPUs for neural admixture (None = auto-detect) |
None
|
admix_batch_size
|
Optional[int]
|
Batch size for neural admixture training |
400
|
admixture_backend
|
Optional[object]
|
Optional AdmixtureBackend instance for testing (if None, constructs a real NeuralAdmixtureBackend, which runs neural-admixture in its own child process) |
None
|
embedding
|
str
|
Embedding method - 'phate', 'umap', 'tsne', or 'diffusion_map' (default: 'phate') |
'phate'
|
embedding_params
|
Optional[Dict]
|
Optional dictionary of embedding-specific parameters |
None
|
embedding_input
|
str
|
Which dataset to embed - 'fit', 'project', or 'both' (default: 'both') |
'both'
|
admix_group_column
|
Optional[str]
|
Column for grouping in admixture barplots (None = use first colormap key) |
None
|
admix_within_group_order
|
Optional[str]
|
Method for ordering samples within groups ('chron', 'tree', or None) |
'chron'
|
projection_plot_fit_column
|
Optional[str]
|
Column from fit colormap to use for projection plot |
None
|
projection_plot_project_column
|
Optional[str]
|
Column from project colormap to use for projection plot |
None
|
skip_pca
|
bool
|
Skip PCA step |
False
|
skip_admixture
|
bool
|
Skip admixture step |
False
|
skip_embedding
|
bool
|
Skip embedding step |
False
|
skip_visualization
|
bool
|
Skip embedding visualization step |
False
|
skip_pca_visualization
|
bool
|
Skip PCA visualization step |
False
|
skip_admixture_visualization
|
bool
|
Skip admixture visualization step |
False
|
skip_metrics
|
bool
|
Skip metrics computation |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
PipelineResult |
PipelineResult
|
The typed outputs of every stage that ran. |
Examples:
>>> # Basic usage with shared labels/colormap
>>> results = run_pipeline(
... fit_plink="data/fit_subset",
... project_plink="data/project_subset",
... labels="labels.csv",
... colormap="colormap.json",
... output_dir="results/",
... n_pcs=50,
... k_min=2,
... k_max=10,
... embedding="phate",
... embedding_params={"knn": 100, "t": 3}
... )
>>> # Cross-cohort analysis with separate labels/colormaps
>>> results = run_pipeline(
... fit_plink="data/hgdp_fit",
... project_plink="data/ukbb_project",
... fit_labels="hgdp_labels.csv",
... project_labels="ukbb_labels.csv",
... fit_colormap="hgdp_colors.json",
... project_colormap="ukbb_colors.json",
... output_dir="cross_cohort_results/",
... )
>>> # Skip steps selectively
>>> results = run_pipeline(
... fit_plink="data/fit",
... project_plink="data/project",
... labels="labels.csv",
... colormap="colormap.json",
... output_dir="results/",
... skip_pca=True, # Use existing PCA
... skip_metrics=True # Don't compute metrics
... )
Note
You must provide either:
- A shared set of
labelsandcolormap, which will be used for both the fit and project cohorts, or - Separate values for all of
fit_labels,project_labels,fit_colormap, andproject_colormapfor cross-cohort analysis.
Source code in src/manifold_genetics/pipeline/runner.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
manifold_genetics.pipeline.configfile.load_config ¶
Read path and return keyword arguments for run_pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
PathLike
|
The YAML config file. |
required |
base_dir
|
Optional[PathLike]
|
Directory relative paths resolve against. Defaults to the config file's own directory, which is what makes an example runnable from anywhere. |
None
|
Raises:
| Type | Description |
|---|---|
ConfigFileError
|
missing file, malformed YAML, unknown section or key, unknown preset, or a contradictory embedding setting. |
Source code in src/manifold_genetics/pipeline/configfile.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
manifold_genetics.pipeline.orchestrator.Pipeline ¶
Pipeline(fit_plink_prefix, project_plink_prefix, labels=None, colormap=None, output_dir=None, geographic_coords=None, fit_labels=None, project_labels=None, fit_colormap=None, project_colormap=None, admixture_backend=None, projection_plot_fit_column=None, projection_plot_project_column=None)
End-to-end pipeline for genetic analysis.
Examples:
>>> # Full pipeline
>>> pipeline = Pipeline(
... fit_plink_prefix="data/fit_subset",
... project_plink_prefix="data/project_subset",
... labels="labels.csv",
... colormap="colormap.json",
... output_dir="results/"
... )
>>> results = pipeline.run(
... n_pcs=50,
... k_min=2, k_max=10,
... embedding="phate", knn=25
... )
Initialize pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_plink_prefix
|
Union[str, Path]
|
Path to fit subset PLINK files |
required |
project_plink_prefix
|
Union[str, Path]
|
Path to project subset PLINK files |
required |
labels
|
Optional[Union[str, Path]]
|
Path to labels CSV (used for both fit and project if not overridden) |
None
|
colormap
|
Optional[Union[str, Path]]
|
Path to colormap JSON (used for both fit and project if not overridden) |
None
|
output_dir
|
Union[str, Path]
|
Directory for outputs |
None
|
geographic_coords
|
Optional[Union[str, Path]]
|
Optional path to geographic coordinates |
None
|
fit_labels
|
Optional[Union[str, Path]]
|
Optional override labels CSV for fit dataset |
None
|
project_labels
|
Optional[Union[str, Path]]
|
Optional override labels CSV for project dataset |
None
|
fit_colormap
|
Optional[Union[str, Path]]
|
Optional override colormap JSON for fit dataset |
None
|
project_colormap
|
Optional[Union[str, Path]]
|
Optional override colormap JSON for project dataset |
None
|
admixture_backend
|
Optional[object]
|
Optional AdmixtureBackend instance for testing (if None, constructs a real NeuralAdmixtureBackend, which runs neural-admixture in its own child process) |
None
|
projection_plot_fit_column
|
Optional[str]
|
Column from fit colormap to use for projection plot |
None
|
projection_plot_project_column
|
Optional[str]
|
Column from project colormap to use for projection plot |
None
|
Note
Must provide either (labels + colormap) OR (fit_labels + project_labels + fit_colormap + project_colormap)
Source code in src/manifold_genetics/pipeline/orchestrator.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
run ¶
run(n_pcs=50, pca_backend='python', max_fit_memory_gb=8.0, max_project_memory_gb=8.0, k_min=2, k_max=10, embedding='phate', embedding_params=None, embedding_input='both', skip_pca=False, skip_admixture=False, skip_embedding=False, skip_visualization=False, skip_pca_visualization=False, skip_admixture_visualization=False, admix_group_column=None, admix_within_group_order='chron', skip_metrics=False, admix_threads=None, admix_gpus=None, admix_batch_size=400)
Run full pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pca_backend
|
str
|
'flashpca' (external binary) or 'python' (in process) |
'python'
|
max_fit_memory_gb
|
float
|
GB budget for the dense standardised matrix when fitting the in-process PCA backend. Above it the fit streams, which bounds memory to about 110 MB at roughly nineteen times the wall clock. Raise it to match the node: a 60,000 x 120,849 cohort is 54 GB dense and streams under the default. |
8.0
|
max_project_memory_gb
|
float
|
GB budget for one chunk when projecting. Peak resident memory runs to about three times this. Both are ignored by the flashpca backend. |
8.0
|
n_pcs
|
int
|
Number of principal components |
50
|
k_min
|
int
|
Minimum K for admixture |
2
|
k_max
|
int
|
Maximum K for admixture |
10
|
embedding
|
str
|
Embedding method ('phate', 'umap', 'tsne', 'diffusion_map') |
'phate'
|
embedding_params
|
Optional[Dict]
|
Optional parameters for embedding |
None
|
embedding_input
|
str
|
Which dataset to embed - 'fit', 'project', or 'both' (default) |
'both'
|
skip_pca
|
bool
|
Skip PCA step |
False
|
skip_admixture
|
bool
|
Skip admixture step |
False
|
skip_embedding
|
bool
|
Skip embedding step |
False
|
skip_visualization
|
bool
|
Skip embedding visualization step |
False
|
skip_pca_visualization
|
bool
|
Skip PCA visualization step |
False
|
admix_group_column
|
Optional[str]
|
Column for grouping in admixture barplots (None = use first colormap key) |
None
|
admix_within_group_order
|
Optional[str]
|
Method for ordering samples within groups ('chron', 'tree', or None) |
'chron'
|
skip_metrics
|
bool
|
Skip metrics computation |
False
|
admix_threads
|
Optional[int]
|
Threads to use for neural admixture (None = auto-detect) |
None
|
admix_gpus
|
Optional[int]
|
Number of GPUs for neural admixture (None = auto-detect) |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
PipelineResult |
PipelineResult
|
The typed outputs of every stage that ran. Fields
for skipped stages are None, except |
Source code in src/manifold_genetics/pipeline/orchestrator.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
manifold_genetics.pipeline.result.PipelineResult
dataclass
¶
PCA¶
manifold_genetics.pca.flashpca.PCA ¶
PCA(n_components=20, flashpca_path=None, force=False, backend=None, max_fit_memory_gb=8.0, max_project_memory_gb=8.0)
FlashPCA wrapper for principal component analysis.
Examples:
>>> # Fit PCA on reference data
>>> pca = PCA(n_components=50)
>>> pca_coords = pca.fit_transform(plink_prefix="data/hgdp")
>>> pca_coords.to_csv("pca_50.csv", index=False)
>>>
>>> # Project new samples onto reference PCA
>>> pca = PCA(n_components=50)
>>> pca.fit(plink_prefix="data/hgdp_ref")
>>> new_coords = pca.project(plink_prefix="data/ukbb")
Initialize PCA analyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of principal components to compute |
20
|
flashpca_path
|
Optional[str]
|
Path to flashpca executable (None = auto-detect) |
None
|
force
|
bool
|
If True, recompute even if outputs exist |
False
|
backend
|
Optional[str]
|
|
None
|
max_fit_memory_gb
|
float
|
Budget for the dense standardised matrix when fitting. Above it the fit streams, which bounds memory to about 110 MB at roughly nineteen times the wall clock. Raise it on a large node: a 60,000 x 120,849 cohort is 54 GB dense, so it streams under the default and does not under 64. |
8.0
|
max_project_memory_gb
|
float
|
Budget for one chunk when projecting. Lower it on a small machine; peak resident memory runs to about three times this figure. Both are ignored by the flashpca backend, which manages its own memory. |
8.0
|
The binary is resolved only for the flashpca backend. Resolving it unconditionally would make merely constructing this object fail on a machine without it -- i.e. anywhere that is not Linux x86-64.
Source code in src/manifold_genetics/pca/flashpca.py
fit ¶
Fit PCA on reference data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix (without extension) |
required |
output_dir
|
Optional[Union[str, Path]]
|
Directory to save outputs (default: ./pca_outputs) |
None
|
Returns:
| Type | Description |
|---|---|
PCA
|
Self (for method chaining) |
Source code in src/manifold_genetics/pca/flashpca.py
project ¶
Project samples onto fitted PCA space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save CSV output |
None
|
output_dir
|
Optional[Union[str, Path]]
|
Optional directory for raw FlashPCA projection outputs |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and PCA coordinates (dim_1, dim_2, ...) |
Source code in src/manifold_genetics/pca/flashpca.py
fit_transform ¶
Fit PCA and transform the same data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save CSV output |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and PCA coordinates |
Source code in src/manifold_genetics/pca/flashpca.py
Embeddings¶
manifold_genetics.embeddings.phate.PHATE ¶
PHATE(n_components=2, knn=5, t='auto', decay=40, gamma=1.0, n_pca=100, n_landmark=2000, random_landmarking=False, random_state=42, n_jobs=-1, mds_solver='sgd', embed_batch_size=None)
Bases: EmbeddingBase
PHATE embedding for manifold learning.
Examples:
>>> # Basic usage
>>> phate_model = PHATE(n_components=2, knn=25)
>>> embedding = phate_model.fit_transform("pca_50.csv")
>>>
>>> # With custom parameters
>>> phate_model = PHATE(n_components=2, knn=50, t=15, gamma=1)
>>> embedding = phate_model.fit_transform("pca_50.csv", output_path="phate_2d.csv")
Initialize PHATE embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of embedding dimensions |
2
|
knn
|
int
|
Number of nearest neighbors |
5
|
t
|
Union[int, str]
|
Diffusion time (int or 'auto') |
'auto'
|
decay
|
Optional[int]
|
Alpha decay parameter for graph kernel |
40
|
gamma
|
float
|
Informational distance parameter |
1.0
|
n_pca
|
Optional[int]
|
Number of PCs to compute before PHATE (None to skip) |
100
|
n_landmark
|
Optional[int]
|
Number of landmarks for efficiency |
2000
|
random_landmarking
|
bool
|
Use random landmark selection instead of spectral clustering |
False
|
random_state
|
Optional[int]
|
Random seed |
42
|
n_jobs
|
int
|
Number of parallel jobs (-1 for all cores) |
-1
|
mds_solver
|
str
|
MDS solver ('sgd' or 'smacof') |
'sgd'
|
embed_batch_size
|
Optional[int]
|
Number of samples to transform at once (None for all) |
None
|
Source code in src/manifold_genetics/embeddings/phate.py
fit ¶
Fit PHATE on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
PHATE
|
Self (for method chaining) |
Source code in src/manifold_genetics/embeddings/phate.py
transform ¶
Transform data using fitted PHATE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and PHATE coordinates |
Source code in src/manifold_genetics/embeddings/phate.py
fit_transform ¶
Fit PHATE and transform data in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save output CSV |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and PHATE coordinates |
Source code in src/manifold_genetics/embeddings/phate.py
manifold_genetics.embeddings.umap.UMAP ¶
UMAP(n_components=2, n_neighbors=15, min_dist=0.1, metric='euclidean', random_state=42, n_epochs=None, learning_rate=1.0, n_jobs=-1)
Bases: EmbeddingBase
UMAP embedding for manifold learning.
Examples:
>>> # Basic usage
>>> umap_model = UMAP(n_components=2, n_neighbors=15)
>>> embedding = umap_model.fit_transform("pca_50.csv")
>>>
>>> # With custom parameters
>>> umap_model = UMAP(n_components=2, n_neighbors=50, min_dist=0.1)
>>> embedding = umap_model.fit_transform("pca_50.csv", output_path="umap_2d.csv")
Initialize UMAP embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of embedding dimensions |
2
|
n_neighbors
|
int
|
Number of nearest neighbors |
15
|
min_dist
|
float
|
Minimum distance between points in embedding |
0.1
|
metric
|
str
|
Distance metric to use |
'euclidean'
|
random_state
|
Optional[int]
|
Random seed |
42
|
n_epochs
|
Optional[int]
|
Number of training epochs (None = auto) |
None
|
learning_rate
|
float
|
Learning rate for optimization |
1.0
|
n_jobs
|
int
|
Number of parallel jobs (-1 for all cores) |
-1
|
Source code in src/manifold_genetics/embeddings/umap.py
fit ¶
Fit UMAP on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
UMAP
|
Self (for method chaining) |
Source code in src/manifold_genetics/embeddings/umap.py
transform ¶
Transform data using fitted UMAP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and UMAP coordinates |
Source code in src/manifold_genetics/embeddings/umap.py
fit_transform ¶
Fit UMAP and transform data in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save output CSV |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and UMAP coordinates |
Source code in src/manifold_genetics/embeddings/umap.py
manifold_genetics.embeddings.tsne.TSNE ¶
TSNE(n_components=2, perplexity=30.0, learning_rate='auto', n_iter=1000, metric='euclidean', random_state=42, n_jobs=-1)
Bases: EmbeddingBase
t-SNE embedding for manifold learning.
Examples:
>>> # Basic usage
>>> tsne_model = TSNE(n_components=2, perplexity=30)
>>> embedding = tsne_model.fit_transform("pca_50.csv")
>>>
>>> # With custom parameters
>>> tsne_model = TSNE(n_components=2, perplexity=50, learning_rate=200)
>>> embedding = tsne_model.fit_transform("pca_50.csv", output_path="tsne_2d.csv")
Initialize t-SNE embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of embedding dimensions |
2
|
perplexity
|
float
|
Perplexity parameter (balance local vs global structure) |
30.0
|
learning_rate
|
Union[float, str]
|
Learning rate for optimization ('auto' or float) |
'auto'
|
n_iter
|
int
|
Number of optimization iterations |
1000
|
metric
|
str
|
Distance metric to use |
'euclidean'
|
random_state
|
Optional[int]
|
Random seed |
42
|
n_jobs
|
int
|
Number of parallel jobs (-1 for all cores) |
-1
|
Source code in src/manifold_genetics/embeddings/tsne.py
fit ¶
Fit t-SNE on the data.
Note: t-SNE does not support separate fit/transform. This method will store the data for later use in transform().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
TSNE
|
Self (for method chaining) |
Source code in src/manifold_genetics/embeddings/tsne.py
transform ¶
Transform data using t-SNE.
Note: t-SNE does not support out-of-sample extension. This will refit the model on the new data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and t-SNE coordinates |
Source code in src/manifold_genetics/embeddings/tsne.py
fit_transform ¶
Fit t-SNE and transform data in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save output CSV |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and t-SNE coordinates |
Source code in src/manifold_genetics/embeddings/tsne.py
manifold_genetics.embeddings.diffusion_map.DiffusionMap ¶
Bases: EmbeddingBase
Diffusion Maps embedding for manifold learning.
Examples:
>>> # Basic usage
>>> dm_model = DiffusionMap(n_components=2, knn=25)
>>> embedding = dm_model.fit_transform("pca_50.csv")
>>>
>>> # With custom parameters
>>> dm_model = DiffusionMap(n_components=2, knn=50, alpha=1.0)
>>> embedding = dm_model.fit_transform("pca_50.csv", output_path="dm_2d.csv")
Initialize Diffusion Maps embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of embedding dimensions |
2
|
knn
|
int
|
Number of nearest neighbors for kernel |
5
|
alpha
|
float
|
Normalization parameter (0=no normalization, 1=Laplacian normalization) |
1.0
|
t
|
int
|
Diffusion time (powers of diffusion operator) |
1
|
random_state
|
Optional[int]
|
Random seed |
42
|
Source code in src/manifold_genetics/embeddings/diffusion_map.py
fit ¶
Fit Diffusion Maps on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DiffusionMap
|
Self (for method chaining) |
Source code in src/manifold_genetics/embeddings/diffusion_map.py
transform ¶
Transform data using fitted Diffusion Maps.
Note: Diffusion maps does not naturally support out-of-sample extension. This will use Nyström extension method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and diffusion coordinates |
Source code in src/manifold_genetics/embeddings/diffusion_map.py
fit_transform ¶
Fit Diffusion Maps and transform data in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save output CSV |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and diffusion coordinates |
Source code in src/manifold_genetics/embeddings/diffusion_map.py
manifold_genetics.embeddings.base.EmbeddingBase ¶
Bases: ABC
Abstract base class for dimensionality reduction methods.
All embedding methods should inherit from this class and implement the fit(), transform(), and fit_transform() methods.
Initialize embedding method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of dimensions for the embedding |
2
|
random_state
|
Optional[int]
|
Random seed for reproducibility |
42
|
Source code in src/manifold_genetics/embeddings/base.py
fit
abstractmethod
¶
Fit the embedding method on the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
EmbeddingBase
|
Self (for method chaining) |
Source code in src/manifold_genetics/embeddings/base.py
transform
abstractmethod
¶
Transform data using the fitted embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and embedding coordinates |
Source code in src/manifold_genetics/embeddings/base.py
fit_transform
abstractmethod
¶
Fit and transform data in one step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[ndarray, DataFrame, str, Path]
|
Input data (numpy array, DataFrame, or path to CSV) |
required |
output_path
|
Optional[Union[str, Path]]
|
Optional path to save output CSV |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with sample_id and embedding coordinates |
Source code in src/manifold_genetics/embeddings/base.py
Admixture¶
manifold_genetics.admixture.neural.NeuralAdmixture ¶
NeuralAdmixture(k_min=2, k_max=10, neural_admixture_path=None, force=False, threads=None, num_gpus=None, batch_size=None, backend=None)
Neural admixture wrapper for ancestry inference.
This class provides a user-friendly API and delegates the actual computation to a backend. By default, it uses NeuralAdmixtureBackend for real computation, but you can inject different backends for testing.
Examples:
>>> # Default: real neural-admixture computation
>>> admix = NeuralAdmixture(k_min=2, k_max=5)
>>> admix.fit("data/hgdp.plink", output_dir="admixture/")
>>> q_files = admix.transform("data/ukbb.plink", output_prefix="admixture/ukbb")
>>>
>>> # For testing: use precomputed backend
>>> from manifold_genetics.admixture.backends import PrecomputedAdmixtureBackend
>>> backend = PrecomputedAdmixtureBackend(k_min=2, k_max=3)
>>> admix = NeuralAdmixture(backend=backend)
>>> q_files = admix.fit_transform("data/test.plink", output_prefix="test/output")
Initialize Neural Admixture analyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_min
|
int
|
Minimum number of ancestral populations |
2
|
k_max
|
int
|
Maximum number of ancestral populations |
10
|
neural_admixture_path
|
Optional[str]
|
Path to executable (None = auto-detect) |
None
|
force
|
bool
|
If True, retrain even if models exist |
False
|
threads
|
Optional[int]
|
Number of threads to use. If None, attempts to detect available CPUs (respecting SLURM/HPC limits). |
None
|
num_gpus
|
Optional[int]
|
Number of GPUs to use. If None, uses 1 when CUDA is available and 0 otherwise. |
None
|
batch_size
|
Optional[int]
|
Batch size for training and inference. If None, uses neural-admixture defaults. |
None
|
backend
|
Optional[AdmixtureBackend]
|
Optional backend to use for computation. If None, uses NeuralAdmixtureBackend (real computation). For testing, you can inject PrecomputedAdmixtureBackend or FakeAdmixtureBackend. |
None
|
Source code in src/manifold_genetics/admixture/neural.py
fit ¶
Train neural admixture models on reference data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix (without extension) |
required |
output_dir
|
Optional[Union[str, Path]]
|
Directory to save model outputs (default: ./admixture_outputs) |
None
|
model_name
|
str
|
Name for the model (used in output filenames) |
'fit'
|
Returns:
| Type | Description |
|---|---|
NeuralAdmixture
|
Self (for method chaining) |
Source code in src/manifold_genetics/admixture/neural.py
transform ¶
Infer ancestry proportions on new samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix |
required |
output_prefix
|
Union[str, Path]
|
Prefix for output CSV files (will create |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict[int, Path]
|
Dictionary mapping K values to CSV file paths |
|
Example |
Dict[int, Path]
|
{2: Path("output.2.csv"), 3: Path("output.3.csv")} |
Source code in src/manifold_genetics/admixture/neural.py
fit_transform ¶
Train models and infer on the same data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plink_prefix
|
Union[str, Path]
|
Path to PLINK file prefix |
required |
output_prefix
|
Union[str, Path]
|
Prefix for output CSV files |
required |
Returns:
| Type | Description |
|---|---|
Dict[int, Path]
|
Dictionary mapping K values to CSV file paths |
Source code in src/manifold_genetics/admixture/neural.py
Visualisation¶
manifold_genetics.visualization.plotting.visualize ¶
visualize(embedding, labels, colormap, output_dir=None, output_prefix='embedding', dataset_prefix='')
Create all standard visualization plots.
Generates a plot for each label column in the colormap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding
|
Union[DataFrame, str, Path]
|
DataFrame or path to embedding CSV |
required |
labels
|
Union[DataFrame, str, Path]
|
DataFrame or path to labels CSV |
required |
colormap
|
Union[Dict, str, Path]
|
Dict or path to colormap JSON |
required |
output_dir
|
Optional[Union[str, Path]]
|
Directory to save plots (default: current directory) |
None
|
output_prefix
|
str
|
Prefix for output filenames |
'embedding'
|
dataset_prefix
|
str
|
Prefix for dataset type (e.g., "fit_" or "project_") |
''
|
Returns:
| Type | Description |
|---|---|
List[Path]
|
List of paths to saved figures |
Source code in src/manifold_genetics/visualization/plotting.py
manifold_genetics.visualization.plotting.plot_embedding ¶
plot_embedding(embedding, labels, colormap, output_path, title=None, figsize=(6, 4), point_size=4.0, alpha=0.6, show_legend=True)
Plot 2D embedding colored by labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding
|
Union[DataFrame, str, Path]
|
DataFrame or path to embedding CSV (sample_id, dim_1, dim_2) |
required |
labels
|
Union[DataFrame, str, Path]
|
DataFrame or path to labels CSV (sample_id, label_columns) |
required |
colormap
|
Union[Dict, str, Path]
|
Dict or path to colormap JSON {label_col: {value: color}} |
required |
output_path
|
Union[str, Path]
|
Path to save figure |
required |
title
|
Optional[str]
|
Optional plot title |
None
|
figsize
|
tuple
|
Figure size (width, height) |
(6, 4)
|
point_size
|
float
|
Size of scatter plot points |
4.0
|
alpha
|
float
|
Transparency of points |
0.6
|
show_legend
|
bool
|
Whether to show legend |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved figure |
Source code in src/manifold_genetics/visualization/plotting.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |