Skip to content

Commit ac6b49e

Browse files
committed
removed comments
1 parent db5e564 commit ac6b49e

File tree

1 file changed

+3
-33
lines changed

1 file changed

+3
-33
lines changed

src/spatialdata_plot/pl/utils.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ def _set_color_source_vec(
744744
color = np.full(len(element), value_to_plot)
745745
return None, color, False
746746

747-
# Figure out where to get the color from
748747
origins = _locate_value(
749748
value_key=value_to_plot,
750749
sdata=sdata,
@@ -766,11 +765,9 @@ def _set_color_source_vec(
766765
table_layer=table_layer,
767766
)[value_to_plot]
768767

769-
# Check what type of data we're dealing with
770768
is_categorical = isinstance(color_source_vector.dtype, pd.CategoricalDtype)
771769
is_numeric = pd.api.types.is_numeric_dtype(color_source_vector)
772770

773-
# If it's numeric data, handle it appropriately
774771
if is_numeric and not is_categorical:
775772
if (
776773
not isinstance(element, GeoDataFrame)
@@ -785,7 +782,6 @@ def _set_color_source_vec(
785782
)
786783
return None, color_source_vector, False
787784

788-
# For non-numeric, non-categorical data (like strings), convert to categorical
789785
if not is_categorical:
790786
try:
791787
color_source_vector = pd.Categorical(color_source_vector)
@@ -795,8 +791,6 @@ def _set_color_source_vec(
795791
return None, color_source_vector, False
796792

797793
# At this point color_source_vector should be categorical
798-
799-
# Look for predefined colors in the AnnData object
800794
adata_with_colors = None
801795
cluster_key = value_to_plot
802796

@@ -813,12 +807,12 @@ def _set_color_source_vec(
813807
first_table = next(iter(annotator_tables))
814808
adata_with_colors = sdata.tables[first_table]
815809
adata_with_colors.uns["spatialdata_key"] = first_table
810+
816811
# If no specific table is found, try using the default table
817812
elif sdata.table is not None:
818813
adata_with_colors = sdata.table
819814
adata_with_colors.uns["spatialdata_key"] = "default_table"
820815

821-
# Now generate the color mapping using the appropriate AnnData object and cluster_key
822816
color_mapping = _get_categorical_color_mapping(
823817
adata=adata_with_colors,
824818
cluster_key=cluster_key,
@@ -869,7 +863,6 @@ def _map_color_seg(
869863
) -> ArrayLike:
870864
cell_id = np.array(cell_id)
871865

872-
# Safely handle different types of color_vector
873866
is_categorical = pd.api.types.is_categorical_dtype(getattr(color_vector, "dtype", None))
874867
is_numeric = pd.api.types.is_numeric_dtype(getattr(color_vector, "dtype", None))
875868
is_pandas_series = isinstance(color_vector, pd.Series)
@@ -963,31 +956,26 @@ def _generate_base_categorial_color_mapping(
963956
na_color: ColorLike,
964957
cmap_params: CmapParams | None = None,
965958
) -> Mapping[str, str]:
966-
color_key = f"{cluster_key}_colors"
967959

968-
# Break long string template into multiple lines to fix E501 error
960+
color_key = f"{cluster_key}_colors"
969961
color_found_in_uns_msg_template = (
970962
"Using colors from '{cluster}_colors' in .uns slot of table '{table}' for plotting. "
971963
"If this is unexpected, please delete the column from your AnnData object."
972964
)
973965

974-
# Check if we have a valid AnnData and if the color key exists in uns
975966
if adata is not None and cluster_key is not None:
976-
# Check for direct color dictionary in uns (e.g., {'A': '#FF5733', 'B': '#3498DB'})
977967
if cluster_key in adata.uns and isinstance(adata.uns[cluster_key], dict):
978968
# We have a direct color mapping dictionary
979969
color_dict = adata.uns[cluster_key]
980970
table_name = getattr(adata, "uns", {}).get("spatialdata_key", "")
981971
if table_name:
982-
# Format the template with the actual values
983972
logger.info(color_found_in_uns_msg_template.format(cluster=cluster_key, table=table_name))
984973

985974
# Ensure all values are hex colors
986975
for k, v in color_dict.items():
987976
if isinstance(v, str) and not v.startswith("#"):
988977
color_dict[k] = to_hex(to_rgba(v))
989978

990-
# Add NA color if missing
991979
categories = color_source_vector.categories.tolist()
992980
na_color_hex = to_hex(to_rgba(na_color)[:3])
993981

@@ -997,24 +985,16 @@ def _generate_base_categorial_color_mapping(
997985
colors = adata.uns[color_key]
998986
table_name = getattr(adata, "uns", {}).get("spatialdata_key", "")
999987
if table_name:
1000-
if isinstance(colors, dict):
1001-
# Format the template with the actual values
1002-
logger.info(color_found_in_uns_msg_template.format(cluster=cluster_key, table=table_name))
1003-
else:
1004-
# Format the template with the actual values
1005-
logger.info(color_found_in_uns_msg_template.format(cluster=cluster_key, table=table_name))
988+
logger.info(color_found_in_uns_msg_template.format(cluster=cluster_key, table=table_name))
1006989

1007-
# Ensure colors are in hex format
1008990
if isinstance(colors, list):
1009991
colors = [to_hex(to_rgba(color)[:3]) for color in colors]
1010992
categories = color_source_vector.categories.tolist()
1011993

1012-
# Handle NaN values
1013994
na_color_hex = to_hex(to_rgba(na_color)[:3])
1014995
if "NaN" not in categories:
1015996
categories.append("NaN")
1016997

1017-
# Make sure we have enough colors
1018998
if len(colors) < len(categories) - 1: # -1 for NaN
1019999
logger.warning(
10201000
f"Not enough colors in {color_key} ({len(colors)}) for all categories ({len(categories) - 1}). "
@@ -1023,39 +1003,31 @@ def _generate_base_categorial_color_mapping(
10231003
# Extend with default colors or duplicate the last color
10241004
colors.extend([na_color_hex] * (len(categories) - 1 - len(colors)))
10251005

1026-
# Create mapping with NaN color
10271006
return dict(zip(categories, colors + [na_color_hex], strict=False))
10281007

10291008
if isinstance(colors, np.ndarray):
1030-
# Convert numpy array to list of hex colors
10311009
colors = [to_hex(to_rgba(color)[:3]) for color in colors]
10321010
categories = color_source_vector.categories.tolist()
10331011

1034-
# Handle NaN values
10351012
na_color_hex = to_hex(to_rgba(na_color)[:3])
10361013
if "NaN" not in categories:
10371014
categories.append("NaN")
10381015

1039-
# Make sure we have enough colors
10401016
if len(colors) < len(categories) - 1: # -1 for NaN
10411017
logger.warning(
10421018
f"Not enough colors in {color_key} ({len(colors)}) for all categories ({len(categories) - 1}). "
10431019
"Some categories will use default colors."
10441020
)
1045-
# Extend with default colors
10461021
colors.extend([na_color_hex] * (len(categories) - 1 - len(colors)))
10471022

1048-
# Create mapping with NaN color
10491023
return dict(zip(categories, colors + [na_color_hex], strict=False))
10501024

1051-
# Dictionary format - direct color mapping
10521025
if isinstance(colors, dict):
10531026
# Ensure all values are hex colors
10541027
for k, v in colors.items():
10551028
if isinstance(v, str) and not v.startswith("#"):
10561029
colors[k] = to_hex(to_rgba(v))
10571030

1058-
# Get categories and handle NaN
10591031
categories = color_source_vector.categories.tolist()
10601032
na_color_hex = to_hex(to_rgba(na_color)[:3])
10611033

@@ -1073,8 +1045,6 @@ def _generate_base_categorial_color_mapping(
10731045

10741046
return result
10751047

1076-
# If we reach here, we didn't find usable colors in uns, use default color mapping
1077-
logger.info(f"No colors found for '{cluster_key}' in AnnData.uns, using default colors")
10781048
return _get_default_categorial_color_mapping(color_source_vector=color_source_vector, cmap_params=cmap_params)
10791049

10801050

0 commit comments

Comments
 (0)