minerva.analysis.model_analysis =============================== .. py:module:: minerva.analysis.model_analysis Attributes ---------- .. autoapisummary:: minerva.analysis.model_analysis._plot_tsne_written_dirs Classes ------- .. autoapisummary:: minerva.analysis.model_analysis.TSNEAnalysis minerva.analysis.model_analysis._ModelAnalysis Module Contents --------------- .. py:class:: 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) Bases: :py:obj:`_ModelAnalysis` Perform 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_names : Optional[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 height : int, optional Height of the figure, by default 1000 width : int, optional Width of the figure, by default 1000 text_size : int, optional Size of font used in plot, by default 30 title : str, optional Title of graph, by default None x_axis_title : str, optional Name of x-axis, by default "x" y_axis_title : str, optional Name of y-axis, by default "y" legend_title : str, optional Name for legend title, by default "Label" output_filename : PathLike, 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" seed : int, optional Random seed for t-SNE, by default 42 n_components : int, optional Number of components to use in t-SNE, by default 2 marker_symbols : Optional[Dict[str, str]], optional Dictionary mapping labels to marker symbols. If None, will use default symbols. colors : Optional[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', ) .. py:attribute:: colors :value: None .. py:method:: compute(model, data) 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 ---------- model : lightning.LightningModule A trained PyTorch Lightning model with a `.backbone` attribute that produces embeddings from the input data. data : lightning.LightningDataModule A Lightning DataModule providing the dataset to project. Data is extracted using the `"predict"` split. .. py:attribute:: height :value: 1000 .. py:attribute:: label_names :value: None .. py:attribute:: legend_title :value: 'Label' .. py:attribute:: marker_symbols :value: None .. py:attribute:: n_components :value: 2 .. py:attribute:: output_filename .. py:attribute:: seed :value: 42 .. py:method:: set_path(path) Set the output path for saving the plots. .. py:attribute:: text_size :value: 30 .. py:attribute:: title :value: None .. py:attribute:: width :value: 1000 .. py:attribute:: write_html :value: True .. py:attribute:: x_axis_title :value: 'x' .. py:attribute:: y_axis_title :value: 'y' .. py:class:: _ModelAnalysis(path = None) 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. .. py:attribute:: _path :value: None .. py:method:: compute(model, data) :abstractmethod: .. py:property:: path .. py:data:: _plot_tsne_written_dirs