Ensemble Clustering¶
flyvision.analysis.clustering.compute_umap_and_clustering ¶
compute_umap_and_clustering(
ensemble,
cell_type,
embedding_kwargs=None,
gm_kwargs=None,
subdir="umap_and_clustering",
)
Compute UMAP embedding and Gaussian Mixture clustering of responses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ensemble |
EnsembleView
|
EnsembleView object. |
required |
cell_type |
str
|
Type of cell to analyze. |
required |
embedding_kwargs |
Optional[Dict]
|
UMAP embedding parameters. |
None
|
gm_kwargs |
Optional[Dict]
|
Gaussian Mixture clustering parameters. |
None
|
subdir |
str
|
Subdirectory for storing results. |
'umap_and_clustering'
|
Returns:
Type | Description |
---|---|
GaussianMixtureClustering
|
GaussianMixtureClustering object. |
Note
Results are cached to disk for faster subsequent access.
Source code in flyvision/analysis/clustering.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
|
flyvision.analysis.clustering.umap_embedding ¶
umap_embedding(
X,
n_neighbors=5,
min_dist=0.12,
spread=9.0,
random_state=42,
n_components=2,
metric="correlation",
n_epochs=1500,
**kwargs
)
Perform UMAP embedding on input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Input data with shape (n_samples, n_features). |
required |
n_neighbors |
int
|
Number of neighbors to consider for each point. |
5
|
min_dist |
float
|
Minimum distance between points in the embedding space. |
0.12
|
spread |
float
|
Determines how spread out all embedded points are overall. |
9.0
|
random_state |
int
|
Random seed for reproducibility. |
42
|
n_components |
int
|
Number of dimensions in the embedding space. |
2
|
metric |
str
|
Distance metric to use. |
'correlation'
|
n_epochs |
int
|
Number of training epochs for embedding optimization. |
1500
|
**kwargs |
Additional keyword arguments for UMAP. |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
A tuple containing: |
ndarray
|
|
UMAP
|
|
Tuple[ndarray, ndarray, UMAP]
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If n_components is too large relative to sample size. |
Note
This function handles reshaping of input data and removes constant rows.
Source code in flyvision/analysis/clustering.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
|
flyvision.analysis.clustering.GaussianMixtureClustering
dataclass
¶
Gaussian Mixture Clustering of the embeddings.
Attributes:
Name | Type | Description |
---|---|---|
embedding |
Embedding
|
The embedding to cluster. |
range_n_clusters |
Iterable[int]
|
Range of number of clusters to try. |
n_init |
int
|
Number of initializations for GMM. |
max_iter |
int
|
Maximum number of iterations for GMM. |
random_state |
int
|
Random state for reproducibility. |
labels |
NDArray
|
Cluster labels. |
gm |
object
|
Fitted GaussianMixture object. |
scores |
list
|
Scores for each number of clusters. |
n_clusters |
list
|
Number of clusters tried. |
Source code in flyvision/analysis/clustering.py
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 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 |
|
__call__ ¶
__call__(
range_n_clusters=None,
n_init=1,
max_iter=1000,
random_state=0,
**kwargs
)
Perform Gaussian Mixture clustering.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
range_n_clusters |
Iterable[int]
|
Range of number of clusters to try. |
None
|
n_init |
int
|
Number of initializations for GMM. |
1
|
max_iter |
int
|
Maximum number of iterations for GMM. |
1000
|
random_state |
int
|
Random state for reproducibility. |
0
|
**kwargs |
Additional arguments for gaussian_mixture function. |
{}
|
Returns:
Type | Description |
---|---|
GaussianMixtureClustering
|
Self with updated clustering results. |
Source code in flyvision/analysis/clustering.py
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 |
|
task_error_sort_labels ¶
task_error_sort_labels(task_error, mode='mean')
Sort cluster labels based on task error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_error |
NDArray
|
Array of task errors. |
required |
mode |
str
|
Method to compute task error (‘mean’, ‘min’, or ‘median’). |
'mean'
|
Source code in flyvision/analysis/clustering.py
207 208 209 210 211 212 213 214 215 |
|
plot ¶
plot(
task_error=None,
colors=None,
annotate=True,
annotate_scores=False,
fig=None,
ax=None,
figsize=None,
plot_mode="paper",
fontsize=5,
**kwargs
)
Plot the clustering results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_error |
NDArray
|
Array of task errors. |
None
|
colors |
NDArray
|
Colors for data points. |
None
|
annotate |
bool
|
Whether to annotate clusters. |
True
|
annotate_scores |
bool
|
Whether to annotate BIC scores. |
False
|
fig |
Figure
|
Existing figure to plot on. |
None
|
ax |
Axes
|
Existing axes to plot on. |
None
|
figsize |
tuple
|
Size of the figure. |
None
|
plot_mode |
str
|
Mode for plotting (‘paper’, ‘small’, or ‘large’). |
'paper'
|
fontsize |
int
|
Font size for annotations. |
5
|
**kwargs |
Additional arguments for plot_embedding function. |
{}
|
Returns:
Type | Description |
---|---|
EmbeddingPlot
|
An EmbeddingPlot object. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the embedding is not 2-dimensional. |
Source code in flyvision/analysis/clustering.py
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 |
|
flyvision.analysis.clustering.EnsembleEmbedding ¶
Embedding of the ensemble responses.
Args: responses (CentralActivity): CentralActivity object
Source code in flyvision/analysis/clustering.py
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 |
|
from_cell_type ¶
from_cell_type(cell_type, embedding_kwargs=None)
Umap Embedding of the responses of a specific cell type.
Source code in flyvision/analysis/clustering.py
411 412 413 414 415 416 417 418 419 |
|
flyvision.analysis.clustering.Embedding
dataclass
¶
Embedding of the ensemble responses.
Attributes:
Name | Type | Description |
---|---|---|
embedding |
NDArray
|
The embedded data. |
mask |
NDArray
|
Mask for valid data points. |
reducer |
object
|
The reduction object used for embedding. |
Source code in flyvision/analysis/clustering.py
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 |
|
plot ¶
plot(
fig=None,
ax=None,
figsize=None,
plot_mode="paper",
fontsize=5,
colors=None,
**kwargs
)
Plot the embedding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fig |
Figure
|
Existing figure to plot on. |
None
|
ax |
Axes
|
Existing axes to plot on. |
None
|
figsize |
tuple
|
Size of the figure. |
None
|
plot_mode |
str
|
Mode for plotting (‘paper’, ‘small’, or ‘large’). |
'paper'
|
fontsize |
int
|
Font size for annotations. |
5
|
colors |
NDArray
|
Colors for data points. |
None
|
**kwargs |
Additional arguments passed to plot_embedding. |
{}
|
Returns:
Type | Description |
---|---|
tuple[Figure, Axes]
|
A tuple containing the figure and axes objects. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the embedding is not 2-dimensional. |
Source code in flyvision/analysis/clustering.py
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 |
|
flyvision.analysis.clustering.Clustering
dataclass
¶
Clustering of the embedding.
Attributes:
Name | Type | Description |
---|---|---|
embedding |
Embedding
|
The embedding to be clustered. |
Source code in flyvision/analysis/clustering.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
gaussian_mixture
property
¶
gaussian_mixture
Create a GaussianMixtureClustering object for the embedding.
Returns:
Name | Type | Description |
---|---|---|
GaussianMixtureClustering |
GaussianMixtureClustering
|
A clustering object for Gaussian mixture models. |