Visualization tools¶
hierarchical_clustering(df, vmin=None, vmax=None, figsize=(8, 8), top_height=2, left_width=2, xmaxticks=None, ymaxticks=None, metric='cosine', cmap=None)
¶
Perform and plot hierarchical clustering on a dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Input data in DataFrame format. |
required |
vmin |
Optional[float]
|
Minimum value to anchor the colormap. If None, inferred from data. |
None
|
vmax |
Optional[float]
|
Maximum value to anchor the colormap. If None, inferred from data. |
None
|
figsize |
Tuple[int, int]
|
Size of the main figure in inches. |
(8, 8)
|
top_height |
int
|
Height of the top dendrogram. |
2
|
left_width |
int
|
Width of the left dendrogram. |
2
|
xmaxticks |
Optional[int]
|
Maximum number of x-ticks to display. |
None
|
ymaxticks |
Optional[int]
|
Maximum number of y-ticks to display. |
None
|
metric |
Union[str, Tuple[str, str]]
|
Distance metric to use. Either a string to use the same metric for both axes, or a tuple of two strings for different metrics for each axis. |
'cosine'
|
cmap |
Optional[str]
|
Matplotlib colormap name. If None, uses "coolwarm". |
None
|
Returns:
Type | Description |
---|---|
Tuple[DataFrame, Figure, List[int], List[int]]
|
A tuple containing: - The clustered DataFrame (reordered according to clustering) - The matplotlib Figure object - The indices of rows in their clustered order - The indices of columns in their clustered order |
Source code in src/ms_mint/matplotlib_tools.py
14 15 16 17 18 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 |
|
plot_metabolomics_hist2d(df, figsize=(4, 2.5), dpi=300, set_dim=True, cmap='jet', rt_range=None, mz_range=None, mz_bins=100, **kwargs)
¶
Create a 2D histogram of metabolomics data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
DataFrame containing metabolomics data with scan_time, mz, and intensity columns. |
required |
figsize |
Tuple[float, float]
|
Size of the figure in inches (width, height). |
(4, 2.5)
|
dpi |
int
|
Resolution of the figure in dots per inch. |
300
|
set_dim |
bool
|
Whether to set figure dimensions. |
True
|
cmap |
str
|
Colormap name to use for the plot. |
'jet'
|
rt_range |
Optional[Tuple[float, float]]
|
Retention time range (min, max) to display. If None, uses data range. |
None
|
mz_range |
Optional[Tuple[float, float]]
|
M/Z range (min, max) to display. If None, uses data range. |
None
|
mz_bins |
int
|
Number of bins to use for the m/z axis. |
100
|
**kwargs |
Additional keyword arguments passed to plt.hist2d. |
{}
|
Returns:
Type | Description |
---|---|
Tuple[ndarray, ndarray, ndarray, Any]
|
The result of plt.hist2d, which is a tuple containing: - The histogram array - The edges of the bins along the x-axis - The edges of the bins along the y-axis - The Axes object |
Source code in src/ms_mint/matplotlib_tools.py
plot_peak_shapes(mint_results, mint_metadata=None, fns=None, peak_labels=None, height=3, aspect=1.5, legend=False, col_wrap=4, hue='ms_file_label', title=None, dpi=None, sharex=False, sharey=False, kind='line', **kwargs)
¶
Plot peak shapes from MS-MINT results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mint_results |
DataFrame
|
DataFrame in Mint results format. |
required |
mint_metadata |
Optional[DataFrame]
|
DataFrame in Mint metadata format for additional sample information. |
None
|
fns |
Optional[List[str]]
|
Filenames to include. If None, includes all files. |
None
|
peak_labels |
Optional[Union[str, List[str]]]
|
Peak label(s) to include. If None, includes all peak labels. |
None
|
height |
int
|
Height of each figure facet in inches. |
3
|
aspect |
float
|
Aspect ratio (width/height) of each figure facet. |
1.5
|
legend |
bool
|
Whether to display a legend. |
False
|
col_wrap |
int
|
Number of columns for subplots. |
4
|
hue |
str
|
Column name to use for color grouping. |
'ms_file_label'
|
title |
Optional[str]
|
Title to add to the figure. |
None
|
dpi |
Optional[int]
|
Resolution of generated image. |
None
|
sharex |
bool
|
Whether to share x-axis range between subplots. |
False
|
sharey |
bool
|
Whether to share y-axis range between subplots. |
False
|
kind |
str
|
Type of seaborn relplot ('line', 'scatter', etc.). |
'line'
|
**kwargs |
Additional keyword arguments passed to seaborn's relplot. |
{}
|
Returns:
Type | Description |
---|---|
FacetGrid
|
A seaborn FacetGrid object containing the plot. |
Source code in src/ms_mint/matplotlib_tools.py
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
plot_peaks(series, peaks=None, highlight=None, expected_rt=None, weights=None, legend=True, label=None, **kwargs)
¶
Plot time series data with peak annotations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
series |
Series
|
Time series data with time as index and intensity as values. |
required |
peaks |
Optional[DataFrame]
|
DataFrame containing peak information. |
None
|
highlight |
Optional[List[int]]
|
List of peak indices to highlight. |
None
|
expected_rt |
Optional[float]
|
Expected retention time to mark on the plot. |
None
|
weights |
Optional[ndarray]
|
Array of weight values (e.g., for Gaussian weighting). |
None
|
legend |
bool
|
Whether to display the legend. |
True
|
label |
Optional[str]
|
Label for the time series data. |
None
|
**kwargs |
Additional keyword arguments passed to the plot function. |
{}
|
Returns:
Type | Description |
---|---|
Figure
|
Matplotlib Figure containing the plot. |
Source code in src/ms_mint/matplotlib_tools.py
options: show_root_heading: true show_root_full_path: true show_submodules: true members_order: source
get_palette_colors(palette_name, num_colors)
¶
Get a list of colors from a specific colorlover palette.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
palette_name |
str
|
Name of the color palette. |
required |
num_colors |
int
|
Number of colors to extract. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of color strings in the requested palette. |
Source code in src/ms_mint/plotly_tools.py
plotly_heatmap(df, normed_by_cols=False, transposed=False, clustered=False, add_dendrogram=False, name='', x_tick_colors=None, height=None, width=None, correlation=False, call_show=False, verbose=False)
¶
Create an interactive heatmap from a dense-formatted dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Input data in DataFrame format. |
required |
normed_by_cols |
bool
|
Whether to normalize column vectors. |
False
|
transposed |
bool
|
Whether to transpose the generated image. |
False
|
clustered |
bool
|
Whether to apply hierarchical clustering on rows. |
False
|
add_dendrogram |
bool
|
Whether to show a dendrogram (only when clustered=True). |
False
|
name |
str
|
Name to use in figure title. |
''
|
x_tick_colors |
Optional[str]
|
Color of x-ticks. |
None
|
height |
Optional[int]
|
Image height in pixels. |
None
|
width |
Optional[int]
|
Image width in pixels. |
None
|
correlation |
bool
|
Whether to convert the table to a correlation matrix. |
False
|
call_show |
bool
|
Whether to display the figure immediately. |
False
|
verbose |
bool
|
Whether to print additional information. |
False
|
Returns:
Type | Description |
---|---|
Optional[Figure]
|
A Plotly Figure object, or None if call_show is True. |
Source code in src/ms_mint/plotly_tools.py
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 |
|
plotly_peak_shapes(mint_results, mint_metadata=None, color='ms_file_label', fns=None, col_wrap=1, peak_labels=None, legend=True, verbose=False, legend_orientation='v', call_show=False, palette='Plasma')
¶
Plot peak shapes from mint results as interactive Plotly figure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mint_results |
DataFrame
|
DataFrame in Mint results format. |
required |
mint_metadata |
Optional[DataFrame]
|
DataFrame in Mint metadata format. |
None
|
color |
str
|
Column name determining color-coding of plots. |
'ms_file_label'
|
fns |
Optional[List[str]]
|
Filenames to include. If None, all files are used. |
None
|
col_wrap |
int
|
Maximum number of subplot columns. |
1
|
peak_labels |
Optional[Union[str, List[str]]]
|
Peak-labels to include. If None, all peaks are used. |
None
|
legend |
bool
|
Whether to display legend. |
True
|
verbose |
bool
|
If True, prints additional details. |
False
|
legend_orientation |
str
|
Legend orientation ('v' for vertical, 'h' for horizontal). |
'v'
|
call_show |
bool
|
If True, displays the plot immediately. |
False
|
palette |
str
|
Color palette to use. |
'Plasma'
|
Returns:
Type | Description |
---|---|
Optional[Figure]
|
A Plotly Figure object, or None if call_show is True. |
Source code in src/ms_mint/plotly_tools.py
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 |
|
set_template()
¶
Set a default template for plotly figures.
Creates a "draft" template with smaller font size and sets it as the default template for all plotly figures.
Source code in src/ms_mint/plotly_tools.py
options: show_root_heading: true show_root_full_path: true show_submodules: true members_order: source
PCA_Plotter
¶
Class for visualizing PCA results from MS-MINT analysis.
This class provides methods to create various plots of PCA results, including cumulative variance plots, pairplots, and loading plots.
Attributes:
Name | Type | Description |
---|---|---|
pca |
The PrincipalComponentsAnalyser instance containing results to visualize. |
Source code in src/ms_mint/pca.py
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 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 |
|
__init__(pca)
¶
Initialize a PCA_Plotter instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pca |
PrincipalComponentsAnalyser
|
PrincipalComponentsAnalyser instance with results to visualize. |
required |
cumulative_variance(interactive=False, **kwargs)
¶
Plot the cumulative explained variance of principal components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interactive |
bool
|
If True, returns a Plotly interactive figure. If False, returns a static Matplotlib figure. |
False
|
**kwargs |
Additional keyword arguments passed to the underlying plotting functions. |
{}
|
Returns:
Type | Description |
---|---|
Union[Figure, Figure]
|
Either a Matplotlib figure or a Plotly figure depending on the interactive parameter. |
Source code in src/ms_mint/pca.py
cumulative_variance_px(**kwargs)
¶
Create an interactive Plotly plot of cumulative explained variance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Additional keyword arguments passed to px.bar. |
{}
|
Returns:
Type | Description |
---|---|
Figure
|
Plotly figure showing cumulative explained variance. |
Source code in src/ms_mint/pca.py
cumulative_variance_sns(**kwargs)
¶
Create a static Matplotlib plot of cumulative explained variance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Additional keyword arguments for figure customization. 'aspect': Width-to-height ratio of the figure (default: 1). 'height': Height of the figure in inches (default: 5). |
{}
|
Returns:
Type | Description |
---|---|
Figure
|
Matplotlib figure showing cumulative explained variance. |
Source code in src/ms_mint/pca.py
loadings(interactive=False, **kwargs)
¶
Plot PCA loadings (feature contributions to principal components).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interactive |
bool
|
If True, returns a Plotly interactive figure. If False, returns a static Seaborn FacetGrid. |
False
|
**kwargs |
Additional keyword arguments passed to the underlying plotting functions. |
{}
|
Returns:
Type | Description |
---|---|
Union[FacetGrid, Figure]
|
Either a Seaborn FacetGrid or a Plotly figure depending on the interactive parameter. |
Source code in src/ms_mint/pca.py
loadings_plotly(**kwargs)
¶
Create an interactive Plotly plot of PCA loadings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Additional keyword arguments passed to px.bar. If 'facet_row' is not specified, it defaults to 'PC'. |
{}
|
Returns:
Type | Description |
---|---|
Figure
|
Plotly figure object. |
Source code in src/ms_mint/pca.py
loadings_sns(**kwargs)
¶
Create a static Seaborn plot of PCA loadings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs |
Additional keyword arguments passed to sns.catplot. If 'row' is not specified, it defaults to 'PC'. |
{}
|
Returns:
Type | Description |
---|---|
FacetGrid
|
Seaborn FacetGrid object. |
Source code in src/ms_mint/pca.py
pairplot(n_components=3, hue=None, fig_kws=None, interactive=False, **kwargs)
¶
Create a pairplot of principal components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_components |
int
|
Number of principal components to include in the plot. |
3
|
hue |
Optional[Union[str, List[str]]]
|
Labels used for coloring points. If a string, data is taken from the mint.meta DataFrame. If a list, values are used directly. |
None
|
fig_kws |
Optional[Dict[str, Any]]
|
Keyword arguments passed to plt.figure if using seaborn. |
None
|
interactive |
bool
|
If True, returns a Plotly interactive figure. If False, returns a static Seaborn PairGrid. |
False
|
**kwargs |
Additional keyword arguments passed to the underlying plotting functions. |
{}
|
Returns:
Type | Description |
---|---|
Union[PairGrid, Figure]
|
Either a Seaborn PairGrid or a Plotly figure depending on the interactive parameter. |
Source code in src/ms_mint/pca.py
pairplot_plotly(df, color_col=None, **kwargs)
¶
Create an interactive Plotly pairplot of principal components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
DataFrame containing the data to visualize. |
required |
color_col |
Optional[str]
|
Column name to use for coloring points. |
None
|
**kwargs |
Additional keyword arguments passed to ff.create_scatterplotmatrix. |
{}
|
Returns:
Type | Description |
---|---|
Figure
|
Plotly figure object. |
Source code in src/ms_mint/pca.py
pairplot_sns(df, fig_kws=None, **kwargs)
¶
Create a static Seaborn pairplot of principal components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
DataFrame containing the data to visualize. |
required |
fig_kws |
Optional[Dict[str, Any]]
|
Keyword arguments passed to plt.figure. |
None
|
**kwargs |
Additional keyword arguments passed to sns.pairplot. |
{}
|
Returns:
Type | Description |
---|---|
PairGrid
|
Seaborn PairGrid object. |
Source code in src/ms_mint/pca.py
PrincipalComponentsAnalyser
¶
Class for applying PCA to MS-MINT analysis results.
This class provides functionality to perform Principal Component Analysis on MS-MINT metabolomics data and store the results for visualization.
Attributes:
Name | Type | Description |
---|---|---|
mint |
The Mint instance containing the data to analyze. |
|
results |
Optional[Dict[str, Any]]
|
Dictionary containing PCA results after running the analysis. |
plot |
PCA_Plotter instance for visualizing the PCA results. |
Source code in src/ms_mint/pca.py
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 |
|
__init__(mint=None)
¶
Initialize a PrincipalComponentsAnalyser instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mint |
Optional['ms_mint.Mint.Mint']
|
Mint instance containing the data to analyze. |
None
|
Source code in src/ms_mint/pca.py
run(n_components=3, on=None, var_name='peak_max', fillna='median', apply=None, groupby=None, scaler='standard')
¶
Run Principal Component Analysis on the current results.
Performs PCA on the data and stores results in self.results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_components |
int
|
Number of PCA components to calculate. |
3
|
on |
Optional[str]
|
Deprecated, use var_name instead. |
None
|
var_name |
str
|
Column name from results to use for PCA. |
'peak_max'
|
fillna |
Union[str, float]
|
Method to fill missing values. One of "median", "mean", "zero", or a numeric value. |
'median'
|
apply |
Optional[str]
|
Transformation to apply to the data before PCA. |
None
|
groupby |
Optional[Union[str, List[str]]]
|
Column(s) to group by before analysis. |
None
|
scaler |
str
|
Method to scale the data. One of "standard", "robust", "minmax". |
'standard'
|
Raises:
Type | Description |
---|---|
DeprecationWarning
|
If the deprecated 'on' parameter is used. |
Source code in src/ms_mint/pca.py
options: show_root_heading: true show_root_full_path: true show_submodules: true members_order: source