minerva.analysis.model_analysis¶
Attributes¶
Classes¶
Perform t-SNE analysis on the embeddings generated by a model. |
|
Main interface for model analysis. A model analysis is a post-training |
Module Contents¶
- class minerva.analysis.model_analysis.TSNEAnalysis(label_names=None, height=1000, width=1000, text_size=30, title=None, x_axis_title='x', y_axis_title='y', legend_title='Label', output_filename='tsne.png', seed=42, n_components=2, marker_symbols=None, colors=None, write_html=True)[source]¶
Bases:
_ModelAnalysisPerform t-SNE analysis on the embeddings generated by a model. A t-SNE plot is generated using the embeddings and saved in the path directory. The plot is saved as both PNG and HTML files.
Plot a t-SNE plot of the embeddings generated by a model.
Parameters¶
- label_namesOptional[Dict[Union[int, str], str]], optional
Labels to use for the plot, instead of the original labels in the data (y). The keys are the original labels and the values are the new labels to use in the plot. If None, the original labels are used as they are. By default None
- heightint, optional
Height of the figure, by default 1000
- widthint, optional
Width of the figure, by default 1000
- text_sizeint, optional
Size of font used in plot, by default 30
- titlestr, optional
Title of graph, by default None
- x_axis_titlestr, optional
Name of x-axis, by default “x”
- y_axis_titlestr, optional
Name of y-axis, by default “y”
- legend_titlestr, optional
Name for legend title, by default “Label”
- output_filenamePathLike, optional
Name of the output file to save the plot as a PNG image file. The file will be saved in the path directory with this name. By default “tsne.png”
- seedint, optional
Random seed for t-SNE, by default 42
- n_componentsint, optional
Number of components to use in t-SNE, by default 2
- marker_symbolsOptional[Dict[str, str]], optional
Dictionary mapping labels to marker symbols. If None, will use default symbols.
- colorsOptional[Dict[str, str]], optional
Dictionary mapping labels to color values (hex codes or names). If None, will use plotly’s default color sequence.
- write_html: bool, optional
If True, saves the plot as an HTML file with interactive controls. Default is True.
Examples¶
Create and run a t-SNE analysis for a fine-tuned model on the MotionSense dataset:
>>> analysis = TSNEAnalysis( height=1000, width=1000, legend_title="Activity", title=" ", output_filename="tsne_analysis.png", label_names={ 0: "sit", 1: "stand", 2: "walk", 3: "stair up", 4: "stair down", 5: "run", 6: "stair up and down", }, marker_symbols={ "sit": "x-open", "stand": "cross-open", "stair up": "triangle-up-open", "stair down": "triangle-down-open", "walk": "circle-open", "run": "star-open" }, text_size=30, x_axis_title='1st Component', y_axis_title='2nd Component', )
- colors = None¶
- compute(model, data)[source]¶
Run the t-SNE analysis on the provided model and dataset.
This method extracts embeddings from the given model using the provided LightningDataModule, applies t-SNE to reduce the embeddings to the specified number of components, and generates a scatter plot with points colored and marked according to their labels.
The resulting plot is saved as: - A static PNG file (output_filename) in the directory self.path. - An interactive HTML file (if write_html=True).
Parameters¶
- modellightning.LightningModule
A trained PyTorch Lightning model with a .backbone attribute that produces embeddings from the input data.
- datalightning.LightningDataModule
A Lightning DataModule providing the dataset to project. Data is extracted using the “predict” split.
- Parameters:
model (lightning.LightningModule)
data (lightning.LightningDataModule)
- height = 1000¶
- label_names = None¶
- legend_title = 'Label'¶
- marker_symbols = None¶
- n_components = 2¶
- output_filename¶
- seed = 42¶
- set_path(path)[source]¶
Set the output path for saving the plots.
- Parameters:
path (minerva.utils.typing.PathLike)
- text_size = 30¶
- title = None¶
- width = 1000¶
- write_html = True¶
- x_axis_title = 'x'¶
- y_axis_title = 'y'¶
- Parameters:
label_names (Optional[Dict[Union[int, str], str]])
height (int)
width (int)
text_size (int)
title (Optional[str])
x_axis_title (str)
y_axis_title (str)
legend_title (str)
output_filename (minerva.utils.typing.PathLike)
seed (int)
n_components (int)
marker_symbols (Optional[Dict[str, str]])
colors (Optional[Dict[str, str]])
write_html (bool)
- class minerva.analysis.model_analysis._ModelAnalysis(path=None)[source]¶
Main interface for model analysis. A model analysis is a post-training analysis that can be run on a trained model to generate insights about the model’s performance. It has a path attribute that specifies the directory where the analysis results will be saved. The compute method should be implemented by subclasses to perform the actual analysis. All insights generated by the analysis should be saved in the path directory. Note that, differently from Metric, _ModelAnalysis does not return any value. Instead, the results of the analysis should be saved in the path directory. All subclasses of _ModelAnalysis should implement the compute method. Inside a pipeline the path will be automatically set to the pipeline.log_dir attribute.
- Parameters:
path (Optional[minerva.utils.typing.PathLike])
- _path = None¶
- abstract compute(model, data)[source]¶
- Parameters:
model (lightning.LightningModule)
data (lightning.LightningDataModule)
- property path¶
- minerva.analysis.model_analysis._plot_tsne_written_dirs¶