Skip to content

Updata on plotting categories clusters. #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
christophechu opened this issue Mar 27, 2025 · 1 comment
Closed

Updata on plotting categories clusters. #437

christophechu opened this issue Mar 27, 2025 · 1 comment

Comments

@christophechu
Copy link

As those issues mentioned.

#422 (comment)
#391
#414

#423

I think you have already resolved part of the issue, especially for the matplotlib backend — which now correctly handles aspects like coordinate alignment and axis visibility.

However, the current fix appears to be limited to method='matplotlib'. When working with large datasets and switching to method='datashader', the same issues still persist. For example:

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/spatialdata_plot/pl/basic.py:936, in PlotAccessor.show(self, coordinate_systems, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, na_in_legend, colorbar, wspace, hspace, ncols, frameon, figsize, dpi, fig, title, share_extent, pad_extent, ax, return_ax, save)
931 wanted_elements, wanted_shapes_on_this_cs, wants_shapes = _get_wanted_render_elements(
932 sdata, wanted_elements, params_copy, cs, "shapes"
933 )
935 if wanted_shapes_on_this_cs:
--> 936 _render_shapes(
937 sdata=sdata,
938 render_params=params_copy,
939 coordinate_system=cs,
940 ax=ax,
941 fig_params=fig_params,
942 scalebar_params=scalebar_params,
943 legend_params=legend_params,
944 )
946 elif cmd == "render_points" and has_points:
947 wanted_elements, wanted_points_on_this_cs, wants_points = _get_wanted_render_elements(
948 sdata, wanted_elements, params_copy, cs, "points"
949 )

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/spatialdata_plot/pl/render.py:262, in _render_shapes(sdata, render_params, coordinate_system, ax, fig_params, scalebar_params, legend_params)
259 if isinstance(ds_cmap, str) and ds_cmap[0] == "#":
260 ds_cmap = ds_cmap[:-2]
--> 262 ds_result = _datashader_map_aggregate_to_color(
263 agg,
264 cmap=ds_cmap,
265 color_key=color_key,
266 min_alpha=np.min([254, render_params.fill_alpha * 255]),
267 ) # prevent min_alpha == 255, bc that led to fully colored test plots instead of just colored points/shapes
268 elif aggregate_with_reduction is not None: # to shut up mypy
269 ds_cmap = render_params.cmap_params.cmap

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/spatialdata_plot/pl/utils.py:2332, in _datashader_map_aggregate_to_color(agg, cmap, color_key, min_alpha, span, clip)
2329 stack[stack[:, :, 3] == 0] = img_over[stack[:, :, 3] == 0]
2330 return stack
-> 2332 return ds.tf.shade(agg, cmap=cmap, color_key=color_key, min_alpha=min_alpha, span=span, how="linear")

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/datashader/transfer_functions/init.py:720, in shade(agg, cmap, color_key, how, alpha, min_alpha, span, name, color_baseline, rescale_discrete_levels)
717 return _interpolate(agg, cmap, how, alpha, span, min_alpha, name,
718 rescale_discrete_levels)
719 elif agg.ndim == 3:
--> 720 return _colorize(agg, color_key, how, alpha, span, min_alpha, name, color_baseline,
721 rescale_discrete_levels)
722 else:
723 raise ValueError("agg must use 2D or 3D coordinates")

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/datashader/transfer_functions/init.py:378, in _colorize(agg, color_key, how, alpha, span, min_alpha, name, color_baseline, rescale_discrete_levels)
374 if len(color_key) < len(cats):
375 raise ValueError(f"Insufficient colors provided ({len(color_key)}) for the categorical "
376 f"fields available ({len(cats)})")
--> 378 colors = [rgb(color_key[c]) for c in cats]
379 rs, gs, bs = map(array, zip(*colors))
381 # Reorient array (transposing the category dimension first)

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/datashader/transfer_functions/init.py:378, in (.0)
374 if len(color_key) < len(cats):
375 raise ValueError(f"Insufficient colors provided ({len(color_key)}) for the categorical "
376 f"fields available ({len(cats)})")
--> 378 colors = [rgb(color_key[c]) for c in cats]
379 rs, gs, bs = map(array, zip(*colors))
381 # Reorient array (transposing the category dimension first)

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/datashader/colors.py:116, in rgb(x)
114 if isinstance(x, str):
115 if x.startswith('#'):
--> 116 return hex_to_rgb(x)
117 elif x in color_lookup:
118 return hex_to_rgb(color_lookup[x])

File /rsrch8/scratch/genomic_med/tchu1/conda/envs/st-python/lib/python3.10/site-packages/datashader/colors.py:91, in hex_to_rgb(x)
83 """Convert a color hexcode to an rgb tuple.
84
85 Example
(...)
88 (255, 255, 255)
89 """
90 if not (x.startswith('#') and len(x) == 7):
---> 91 raise ValueError("Invalid hex color")
92 x = x.strip('#')
93 try:

ValueError: Invalid hex color

@timtreis
Copy link
Member

Hey @christophechu, please add further info to existing issues if it's the same problem instead of opening new issues, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants