diff --git a/src/dimet/config/analysis/method/pca_plot.yaml b/src/dimet/config/analysis/method/pca_plot.yaml index c138b3d..4cff1d9 100644 --- a/src/dimet/config/analysis/method/pca_plot.yaml +++ b/src/dimet/config/analysis/method/pca_plot.yaml @@ -20,3 +20,6 @@ impute_values: # how to impute zeroes mean_enrichment: "min" isotopologues: "min" isotopologue_proportions: "min" + +width: !!float 6.2 +height: !!float 4.0 \ No newline at end of file diff --git a/src/dimet/processing/differential_analysis.py b/src/dimet/processing/differential_analysis.py index 77fc14b..a2a94be 100644 --- a/src/dimet/processing/differential_analysis.py +++ b/src/dimet/processing/differential_analysis.py @@ -40,9 +40,13 @@ def compute_span_incomparison(df: pd.DataFrame, groups: List) -> pd.DataFrame: the computed differences. """ for i in df.index.values: - all_values = list(df.loc[i, groups[0]]) + list(df.loc[i, groups[1]]) + group1_values = df.loc[i, groups[0]].to_numpy() + group2_values = df.loc[i, groups[1]].to_numpy() + + all_values = np.concatenate([group1_values, group2_values]) + + interval = np.nanmax(all_values) - np.nanmin(all_values) - interval = max(all_values) - min(all_values) df.loc[i, "span_allsamples"] = interval return df diff --git a/src/dimet/visualization/abundance_bars.py b/src/dimet/visualization/abundance_bars.py index 7e3bdbb..fc8662e 100644 --- a/src/dimet/visualization/abundance_bars.py +++ b/src/dimet/visualization/abundance_bars.py @@ -21,6 +21,7 @@ cs = ConfigStore.instance() +matplotlib.rcParams['svg.fonttype'] = 'none' def pile_up_abundance(df: pd.DataFrame, metada_sel: pd.DataFrame) -> pd.DataFrame: @@ -69,6 +70,9 @@ def plot_one_metabolite(df: pd.DataFrame, ) if do_stripplot: np.random.seed(123) # to force the jitter not to randomly change + dodge_bool = True + if axisx_var == hue_var: + dodge_bool = False try: sns.stripplot( ax=curr_ax, @@ -77,7 +81,7 @@ def plot_one_metabolite(df: pd.DataFrame, hue=str(hue_var), data=df, palette=palette_choice, - dodge=True, + dodge=dodge_bool, edgecolor="black", linewidth=1.5, alpha=1, @@ -132,7 +136,10 @@ def plot_abundance_bars_no_grid( axs_k = apply_advanced_param_custom_x_labs(cfg, axs_k) if k == 0: # collect the legend, which is same for any plot thehandles, thelabels = axs_k.get_legend_handles_labels() - axs_k.legend_.remove() + try: + axs_k.legend_.remove() + except AttributeError: + pass # no legend is generated when xaxis and barcolor are equal plt.tight_layout(pad=0.01, w_pad=-2, h_pad=0.1) plt.savefig(output_path_k, bbox_inches="tight", @@ -141,6 +148,8 @@ def plot_abundance_bars_no_grid( plt.legend(handles=thehandles, labels=thelabels, loc="upper right") plt.axis("off") + if hue_var == axisx_var: + plt.figtext(0.1, 0.7, "No legend:\n xaxis equal to barcolor") plt.savefig(os.path.join( output_directory, f"legend.{cfg.analysis.method.figure_format}"), @@ -198,7 +207,10 @@ def plot_as_grid_of_bars( thehandles, thelabels = axs[-1].get_legend_handles_labels() for i in range(len(selected_metabolites)): - axs[i].legend_.remove() + try: + axs[i].legend_.remove() + except AttributeError: + pass # no legend is generated when xaxis and barcolor are equal plt.subplots_adjust(left=0.2, top=0.76, bottom=0.2, hspace=corrector_factor) @@ -211,7 +223,10 @@ def plot_as_grid_of_bars( plt.close() plt.legend(handles=thehandles, labels=thelabels, loc="upper right") + if hue_var == axisx_var: + plt.figtext(0.1, 0.7, "No legend:\n xaxis equal to barcolor") plt.axis("off") + plt.savefig(os.path.join( output_directory, f"legend.{cfg.analysis.method.figure_format}"), diff --git a/src/dimet/visualization/isotopologue_proportions.py b/src/dimet/visualization/isotopologue_proportions.py index 0372434..c6c1227 100644 --- a/src/dimet/visualization/isotopologue_proportions.py +++ b/src/dimet/visualization/isotopologue_proportions.py @@ -22,6 +22,8 @@ cs = ConfigStore.instance() +matplotlib.rcParams['svg.fonttype'] = 'none' + def isotopologue_proportions_2piled_df( compartment_df: pd.DataFrame, diff --git a/src/dimet/visualization/mean_enrichment_line_plot.py b/src/dimet/visualization/mean_enrichment_line_plot.py index b9035a7..048d101 100644 --- a/src/dimet/visualization/mean_enrichment_line_plot.py +++ b/src/dimet/visualization/mean_enrichment_line_plot.py @@ -16,6 +16,8 @@ cs = ConfigStore.instance() +matplotlib.rcParams['svg.fonttype'] = 'none' + def melt_data_metadata_2df(compartment_df: pd.DataFrame, metadata_co_df: pd.DataFrame) -> pd.DataFrame: diff --git a/src/dimet/visualization/metabologram.py b/src/dimet/visualization/metabologram.py index 40b863b..8e9b5c2 100644 --- a/src/dimet/visualization/metabologram.py +++ b/src/dimet/visualization/metabologram.py @@ -28,6 +28,8 @@ logger = logging.getLogger(__name__) +matplotlib.rcParams['svg.fonttype'] = 'none' + def get_differential_results_dict(file_name: str, data_integration: DataIntegration, diff --git a/src/dimet/visualization/pca_plot.py b/src/dimet/visualization/pca_plot.py index 6747d12..e6e03ee 100644 --- a/src/dimet/visualization/pca_plot.py +++ b/src/dimet/visualization/pca_plot.py @@ -5,6 +5,7 @@ import os from typing import List, Union +import matplotlib import matplotlib.figure as figure import matplotlib.pyplot as plt import numpy as np @@ -18,6 +19,8 @@ cs = ConfigStore.instance() +matplotlib.rcParams['svg.fonttype'] = 'none' + def variance_expl_plot(var_explained_df: pd.DataFrame) -> figure.Figure: """ @@ -47,11 +50,12 @@ def pca_scatter_plot(pc_df: pd.DataFrame, var_explained_df: pd.DataFrame, col1: str, col2: str, labels_column: str, - ellipses_column: Union[str, None]) -> figure.Figure: + ellipses_column: Union[str, None], + width: float=6.0, height: float=4.0) -> figure.Figure: """ returns the scatter plot (a seaborn matplotlib figure) """ - fig, ax = plt.subplots() + fig, ax = plt.subplots(figsize=(width, height)) sns.scatterplot(x="PC1", y="PC2", ax=ax, data=pc_df, @@ -59,6 +63,15 @@ def pca_scatter_plot(pc_df: pd.DataFrame, style=col2, legend=True, s=80, zorder=3) + handles, labels = ax.get_legend_handles_labels() + ax.legend( + handles, + labels, + bbox_to_anchor=(1.05, 1), # Places legend outside the top-right corner + loc='upper left', + borderaxespad=0.0 + ) + plt.tight_layout() # Ensures the legend is not clipped ax.axhline(0, ls="--", color="gray", zorder=1) ax.axvline(0, ls="--", color="gray", zorder=1) if labels_column != "": @@ -149,6 +162,7 @@ def run_pca_plot(pca_results_dict: dict, cfg: DictConfig, color_dot = cfg.analysis.method.color style_dot = cfg.analysis.method.style + options_labels = {'label-y': "name_to_plot", 'label-n': ""} # when empty string, no dot labels @@ -159,7 +173,10 @@ def run_pca_plot(pca_results_dict: dict, cfg: DictConfig, scatter_fig: figure.Figure = pca_scatter_plot( pc_df, var_explained_df, color_dot, style_dot, labels_column, - ellipses_column=cfg.analysis.method.draw_ellipses) + ellipses_column=cfg.analysis.method.draw_ellipses, + width=cfg.analysis.method.width, + height=cfg.analysis.method.height + ) pca_scatter_2_file(scatter_fig, name_elements, out_plot_dir, cfg.analysis.method.figure_format) plt.close()