From 5ee0963555e21ccd489f3b3a54873542adf90739 Mon Sep 17 00:00:00 2001 From: Liza Shchehlik Date: Mon, 14 Apr 2025 17:14:32 -0400 Subject: [PATCH 1/4] Major Change: Added pin_colors functionality to histogram: group_by = True,False, together = True/False --- src/spac/visualization.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/spac/visualization.py b/src/spac/visualization.py index 0ab0ee11..f16a9e94 100644 --- a/src/spac/visualization.py +++ b/src/spac/visualization.py @@ -398,7 +398,8 @@ def tsne_plot(adata, color_column=None, ax=None, **kwargs): def histogram(adata, feature=None, annotation=None, layer=None, group_by=None, together=False, ax=None, - x_log_scale=False, y_log_scale=False, **kwargs): + x_log_scale=False, y_log_scale=False, + defined_color_map=None, **kwargs): """ Plot the histogram of cells based on a specific feature from adata.X or annotation from adata.obs. @@ -439,6 +440,10 @@ def histogram(adata, feature=None, annotation=None, layer=None, y_log_scale : bool, default False If True, the y-axis will be set to log scale. + + defined_color_map : str, optional, default=None + Key in adata.uns used to retrieve a color mapping dictionary to + color code the histogram. **kwargs Additional keyword arguments passed to seaborn histplot function. @@ -488,7 +493,6 @@ def histogram(adata, feature=None, annotation=None, layer=None, DataFrame containing the data used for plotting the histogram. """ - # If no feature or annotation is specified, apply default behavior if feature is None and annotation is None: # Default to the first feature in adata.var_names @@ -523,6 +527,10 @@ def histogram(adata, feature=None, annotation=None, layer=None, df = pd.concat([df, adata.obs], axis=1) + if defined_color_map: + color_dict = get_defined_color_map(adata,defined_color_map) + kwargs.setdefault("palette", color_dict) + if feature and annotation: raise ValueError("Cannot pass both feature and annotation," " choose one.") @@ -651,7 +659,6 @@ def calculate_histogram(data, bins, bin_edges=None): kwargs.setdefault("multiple", "stack") kwargs.setdefault("element", "bars") - sns.histplot(data=hist_data, x='bin_center', weights='count', hue=group_by, ax=ax, **kwargs) # If plotting feature specify which layer @@ -671,12 +678,18 @@ def calculate_histogram(data, bins, bin_edges=None): ax_array = ax_array.flatten() for i, ax_i in enumerate(ax_array): - group_data = plot_data[plot_data[group_by] == - groups[i]][data_column] + group = groups[i] + group_data = plot_data[plot_data[group_by] == group][data_column] hist_data = calculate_histogram(group_data, kwargs['bins']) - + + # Retrieve the specific color for this group if defined_color_map is provided + group_color = None + if defined_color_map: + group_color = color_dict.get(group, None) + sns.histplot(data=hist_data, x="bin_center", ax=ax_i, - weights='count', **kwargs) + weights='count', color=group_color, **kwargs) + # If plotting feature specify which layer if feature: ax_i.set_title(f'{groups[i]} with Layer: {layer}') @@ -713,8 +726,14 @@ def calculate_histogram(data, bins, bin_edges=None): hist_data = calculate_histogram(plot_data[data_column], kwargs['bins']) if pd.api.types.is_numeric_dtype(plot_data[data_column]): ax.set_xlim(hist_data['bin_left'].min(), - hist_data['bin_right'].max()) - + hist_data['bin_right'].max()) + + # Set default color from custom color map if available + if defined_color_map: + color_dict = get_defined_color_map(adata,defined_color_map) + default_color = list(color_dict.values())[0] + kwargs['color'] = default_color + sns.histplot( data=hist_data, x='bin_center', From a962367928796232107e654931969c5769f0e319 Mon Sep 17 00:00:00 2001 From: LizaShch Date: Wed, 16 Apr 2025 00:01:47 +0000 Subject: [PATCH 2/4] Minor Edit: Changed lines to fulfill charecter limits --- oryx-build-commands.txt | 2 ++ src/spac/visualization.py | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 oryx-build-commands.txt diff --git a/oryx-build-commands.txt b/oryx-build-commands.txt new file mode 100644 index 00000000..d647bdf7 --- /dev/null +++ b/oryx-build-commands.txt @@ -0,0 +1,2 @@ +PlatformWithVersion=Python +BuildCommands=conda env create --file environment.yml --prefix ./venv --quiet diff --git a/src/spac/visualization.py b/src/spac/visualization.py index f16a9e94..4471e949 100644 --- a/src/spac/visualization.py +++ b/src/spac/visualization.py @@ -679,10 +679,14 @@ def calculate_histogram(data, bins, bin_edges=None): for i, ax_i in enumerate(ax_array): group = groups[i] - group_data = plot_data[plot_data[group_by] == group][data_column] - hist_data = calculate_histogram(group_data, kwargs['bins']) + group_data = plot_data[plot_data[group_by] == group][ + data_column + ] + hist_data = calculate_histogram( + group_data, kwargs['bins'] + ) - # Retrieve the specific color for this group if defined_color_map is provided + #If defined_color_map provided, retrieves color map group_color = None if defined_color_map: group_color = color_dict.get(group, None) From f0b140e29071c077fd31907c0b51667c2b872e90 Mon Sep 17 00:00:00 2001 From: LizaShch Date: Wed, 16 Apr 2025 00:03:48 +0000 Subject: [PATCH 3/4] Removed accidental adding of build-commands --- oryx-build-commands.txt | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 oryx-build-commands.txt diff --git a/oryx-build-commands.txt b/oryx-build-commands.txt deleted file mode 100644 index d647bdf7..00000000 --- a/oryx-build-commands.txt +++ /dev/null @@ -1,2 +0,0 @@ -PlatformWithVersion=Python -BuildCommands=conda env create --file environment.yml --prefix ./venv --quiet From ecb39407fe4fdd79576ea91702b9df9814f111c3 Mon Sep 17 00:00:00 2001 From: Sam Ying Date: Thu, 29 May 2025 14:31:38 -0400 Subject: [PATCH 4/4] addittional formatting of histogram function --- src/spac/visualization.py | 57 ++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/spac/visualization.py b/src/spac/visualization.py index 4471e949..eb676383 100644 --- a/src/spac/visualization.py +++ b/src/spac/visualization.py @@ -396,6 +396,7 @@ def tsne_plot(adata, color_column=None, ax=None, **kwargs): return fig, ax + def histogram(adata, feature=None, annotation=None, layer=None, group_by=None, together=False, ax=None, x_log_scale=False, y_log_scale=False, @@ -440,7 +441,7 @@ def histogram(adata, feature=None, annotation=None, layer=None, y_log_scale : bool, default False If True, the y-axis will be set to log scale. - + defined_color_map : str, optional, default=None Key in adata.uns used to retrieve a color mapping dictionary to color code the histogram. @@ -488,7 +489,7 @@ def histogram(adata, feature=None, annotation=None, layer=None, axs : matplotlib.axes.Axes or list of Axes The Axes object(s) of the histogram plot(s). Returns a single Axes if only one plot is created, otherwise returns a list of Axes. - + df : pandas.DataFrame DataFrame containing the data used for plotting the histogram. @@ -528,7 +529,7 @@ def histogram(adata, feature=None, annotation=None, layer=None, df = pd.concat([df, adata.obs], axis=1) if defined_color_map: - color_dict = get_defined_color_map(adata,defined_color_map) + color_dict = get_defined_color_map(adata, defined_color_map) kwargs.setdefault("palette", color_dict) if feature and annotation: @@ -537,7 +538,7 @@ def histogram(adata, feature=None, annotation=None, layer=None, data_column = feature if feature else annotation - # Check for negative values and apply log1p transformation if + # Check for negative values and apply log1p transformation if # x_log_scale is True if x_log_scale: if (df[data_column] < 0).any(): @@ -568,7 +569,7 @@ def cal_bin_num( ): bins = max(int(2*(num_rows ** (1/3))), 1) print(f'Automatically calculated number of bins is: {bins}') - return(bins) + return (bins) num_rows = plot_data.shape[0] @@ -584,25 +585,25 @@ def calculate_histogram(data, bins, bin_edges=None): Parameters: - data (pd.Series): The input data to be binned. - - bins (int or sequence): Number of bins (if numeric) or unique categories + - bins (int or sequence): Number of bins (if numeric) or unique categories (if categorical). - - bin_edges (array-like, optional): Predefined bin edges for numeric data. + - bin_edges (array-like, optional): Predefined bin edges for numeric data. If None, automatic binning is used. Returns: - pd.DataFrame: A DataFrame containing the following columns: - - `count`: + - `count`: Frequency of values in each bin. - - `bin_left`: + - `bin_left`: Left edge of each bin (for numeric data). - - `bin_right`: + - `bin_right`: Right edge of each bin (for numeric data). - - `bin_center`: - Center of each bin (for numeric data) or category labels + - `bin_center`: + Center of each bin (for numeric data) or category labels (for categorical data). - + """ - + # Check if the data is numeric or categorical if pd.api.types.is_numeric_dtype(data): if bin_edges is None: @@ -620,7 +621,7 @@ def calculate_histogram(data, bins, bin_edges=None): else: counts = data.value_counts().sort_index() return pd.DataFrame({ - 'bin_center': counts.index, + 'bin_center': counts.index, 'bin_left': counts.index, 'bin_right': counts.index, 'count': counts.values @@ -649,7 +650,7 @@ def calculate_histogram(data, bins, bin_edges=None): group_data = plot_data[ plot_data[group_by] == group ][data_column] - group_hist = calculate_histogram(group_data, kwargs['bins'], + group_hist = calculate_histogram(group_data, kwargs['bins'], bin_edges=global_bin_edges) group_hist[group_by] = group hist_data.append(group_hist) @@ -659,7 +660,7 @@ def calculate_histogram(data, bins, bin_edges=None): kwargs.setdefault("multiple", "stack") kwargs.setdefault("element", "bars") - sns.histplot(data=hist_data, x='bin_center', weights='count', + sns.histplot(data=hist_data, x='bin_center', weights='count', hue=group_by, ax=ax, **kwargs) # If plotting feature specify which layer if feature: @@ -685,15 +686,15 @@ def calculate_histogram(data, bins, bin_edges=None): hist_data = calculate_histogram( group_data, kwargs['bins'] ) - - #If defined_color_map provided, retrieves color map + + # If defined_color_map provided, retrieves color map group_color = None if defined_color_map: group_color = color_dict.get(group, None) - - sns.histplot(data=hist_data, x="bin_center", ax=ax_i, - weights='count', color=group_color, **kwargs) - + + sns.histplot(data=hist_data, x="bin_center", ax=ax_i, + weights='count', color=group_color, **kwargs) + # If plotting feature specify which layer if feature: ax_i.set_title(f'{groups[i]} with Layer: {layer}') @@ -729,7 +730,7 @@ def calculate_histogram(data, bins, bin_edges=None): # Precompute histogram data for single plot hist_data = calculate_histogram(plot_data[data_column], kwargs['bins']) if pd.api.types.is_numeric_dtype(plot_data[data_column]): - ax.set_xlim(hist_data['bin_left'].min(), + ax.set_xlim(hist_data['bin_left'].min(), hist_data['bin_right'].max()) # Set default color from custom color map if available @@ -739,13 +740,13 @@ def calculate_histogram(data, bins, bin_edges=None): kwargs['color'] = default_color sns.histplot( - data=hist_data, + data=hist_data, x='bin_center', - weights="count", - ax=ax, + weights="count", + ax=ax, **kwargs ) - + # If plotting feature specify which layer if feature: ax.set_title(f'Layer: {layer}')