From f6b5ac938e93d4b95ca03ccd12955b06e22ea68a Mon Sep 17 00:00:00 2001 From: Julia Date: Tue, 12 Dec 2023 13:30:20 +0100 Subject: [PATCH] add html exports for interactive plots --- docs/cli_config.rst | 2 +- docs/examples/plotting.rst | 6 ------ pandora/pandora.py | 12 ++++++++++++ tests/test_pandora.py | 12 ++++++------ 4 files changed, 19 insertions(+), 13 deletions(-) delete mode 100644 docs/examples/plotting.rst diff --git a/docs/cli_config.rst b/docs/cli_config.rst index 8506f6a..d920630 100644 --- a/docs/cli_config.rst +++ b/docs/cli_config.rst @@ -97,4 +97,4 @@ Running Pandora in the command line will produce a number of (intermediate) outp - ``*.fst``: The results of ``smartpca`` Fst computation (in case of MDS analyses) - In case you specified ``keep_replicates: true`` in your config, there will also be the dataset files for the windows (``*.geno``, ``*.snp``, ``*.ind``). -- ``plots/``: If you set ``plot_results: true`` in your config, this directory will contain all plots Pandora generated during the execution. The names of the files should be self-explanatory. +- ``plots/``: If you set ``plot_results: true`` in your config, this directory will contain all plots Pandora generated during the execution. The names of the files should be self-explanatory. As of version 1.0.8, we provide each plot in two formats: pdf and HTML. You can open the HTML file in any browser to see an interactive version of the plot. diff --git a/docs/examples/plotting.rst b/docs/examples/plotting.rst deleted file mode 100644 index 7fc694a..0000000 --- a/docs/examples/plotting.rst +++ /dev/null @@ -1,6 +0,0 @@ - -Plotting Embeddings -=================== - -The following examples demonstrate how to plot PCA or MDS results such that you can make sense of the Pandora results. -We recommend doing plotting in an interactive jupyter notebook. Pandora's plotting functions use plotly and when used in a jupyter notebook, you can interact with these plots to gain more insights. diff --git a/pandora/pandora.py b/pandora/pandora.py index 43d4a66..8898963 100644 --- a/pandora/pandora.py +++ b/pandora/pandora.py @@ -500,6 +500,9 @@ def _plot_dataset(self, dataset: EigenDataset, plot_prefix: str) -> None: fig.write_image( self.pandora_config.plot_dir / f"{plot_prefix}_with_populations.pdf" ) + fig.write_html( + self.pandora_config.plot_dir / f"{plot_prefix}_with_populations.html" + ) # plot with annotated clusters fig = plot_clusters( @@ -511,6 +514,9 @@ def _plot_dataset(self, dataset: EigenDataset, plot_prefix: str) -> None: fig.write_image( self.pandora_config.plot_dir / f"{plot_prefix}_with_clusters.pdf" ) + fig.write_html( + self.pandora_config.plot_dir / f"{plot_prefix}_with_clusters.html" + ) if len(self.dataset.embedding_populations) > 0: fig = plot_projections( @@ -522,6 +528,9 @@ def _plot_dataset(self, dataset: EigenDataset, plot_prefix: str) -> None: fig.write_image( self.pandora_config.plot_dir / f"{plot_prefix}_projections.pdf" ) + fig.write_html( + self.pandora_config.plot_dir / f"{plot_prefix}_projections.html" + ) def bootstrap_embeddings(self) -> None: """Draws bootstrap replicates of ``self.dataset`` and computes and compares the respective embedding for all @@ -681,10 +690,13 @@ def _plot_sample_support_values( if projected_samples_only: fig_name = "projected_sample_support_values.pdf" + fig_name_html = "projected_sample_support_values.html" else: fig_name = "sample_support_values.pdf" + fig_name_html = "sample_support_values.html" fig.write_image(self.pandora_config.plot_dir / fig_name) + fig.write_html(self.pandora_config.plot_dir / fig_name_html) return fig def _compare_replicates_similarity(self) -> None: diff --git a/tests/test_pandora.py b/tests/test_pandora.py index 81e77a7..bd787d5 100644 --- a/tests/test_pandora.py +++ b/tests/test_pandora.py @@ -226,8 +226,8 @@ def test_do_pca(self, pandora_test_config): # pandora's dataset's PCA should be a PCA object now and not None assert isinstance(pandora.dataset.pca, PCA) - # plot directory should contain two plots - assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 2 + # plot directory should contain four plots (two pdf, two html) + assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 4 def test_do_mds(self, pandora_test_config_mds): pandora = Pandora(pandora_test_config_mds) @@ -238,8 +238,8 @@ def test_do_mds(self, pandora_test_config_mds): # pandora's dataset's MDS should be a MDS object now and not None assert isinstance(pandora.dataset.mds, MDS) - # plot directory should contain two plots - assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 2 + # plot directory should contain four plots (two pdf, two html) + assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 4 def test_do_pca_with_pca_populations( self, pandora_test_config_with_embedding_populations @@ -254,8 +254,8 @@ def test_do_pca_with_pca_populations( # pandora's dataset's PCA should be a PCA object now and not None assert isinstance(pandora.dataset.pca, PCA) - # plot directory should contain three plots - assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 3 + # plot directory should contain six plots (3 pdf, 3 html) + assert len(list(pandora.pandora_config.plot_dir.iterdir())) == 6 def test_plot_dataset_fails_if_pca_is_missing(self, pandora_test_config): pandora = Pandora(pandora_test_config)