Skip to content

Move color_wheel to core visualization_utils and extend it - #322

Open
maclariz wants to merge 7 commits into
electronmicroscopy:devfrom
maclariz:color-wheel-to-core
Open

maclariz wants to merge 7 commits into
electronmicroscopy:devfrom
maclariz:color-wheel-to-core

Conversation

@maclariz

Copy link
Copy Markdown
Collaborator

color_wheel is useful beyond digital dark field, for example to label DPC or phase gradient directions, so it now lives in quantem.core.visualization.visualization_utils; digital_dark_field re-exports it, so existing imports keep working.

It also gains:

  • colorslist: colors placed evenly around the wheel (matplotlib names, hex strings or RGB tuples in 0-1 or 0-255), joined back to the first color so the wheel has no seam;
  • repeat: the number of times the hues go round the wheel (default 1);
  • gamma: brightness at radius r is r**gamma, to match an image shown with the same gamma.

With the defaults the output is unchanged.

Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732

What problem this PR addreseses

color_wheel is of likely wider applicability than just Digital Dark Field imaging, so is worth making into a core visualization function that digital_dark_field calls when needed

What should the reviewer(s) do

@cophus cophus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Ian. The move and the re-export look good. With the default arguments the output is identical to the old DDF color_wheel, and from quantem.diffraction.digital_dark_field import color_wheel still works. A few requests before we merge:

  1. Perceptual mode. Now that this is the core color wheel for DPC and phase directions, it should be able to match how quantEM already colors angles. show_2d for complex arrays, add_arg_cbar_to_ax and the ACOM-FFT angle maps (_angle_rgb in systematic_row.py) all use array_to_rgba, which works in CIECAM02 JCh at constant lightness across hue. Around the rim of the HSV wheel the lightness J runs from 20 (blue) to 89 (yellow), while a JCh wheel stays at 58 to 59. An option that calls array_to_rgba(np.clip(r, 0, 1) ** gamma, np.arctan2(y, x)) would make the wheel an exact legend for show_2d complex images. Please keep HSV as the default so composite_ddf still matches its legend.
  2. Rename colorslist to color_list, since quantEM arguments are snake_case and this is public API.
  3. The 0-255 check runs per color, so (1, 1, 1) in a list of 0-255 colors is read as white rather than near-black. Please decide the scale once for the whole list.
  4. A list with fewer than two colors fails with an IndexError (empty) or a matplotlib ValueError (one color). A clear ValueError would help.
  5. Minor: export color_wheel from quantem.core.visualization next to turbo_black. In the test, repeat=1 and the gamma=1.0 case repeat the defaults and can be dropped.

maclariz and others added 7 commits December 6, 2026 17:47
color_wheel is useful beyond digital dark field, for example to label
DPC or phase gradient directions, so it now lives in
quantem.core.visualization.visualization_utils; digital_dark_field
re-exports it, so existing imports keep working.

It can also take a list of colors to place evenly around the wheel,
joined back to the first color so the wheel has no seam, a repeat count
for the hues, and a gamma for how the brightness grows with radius.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
mode="perceptual" colors the wheel with array_to_rgba, the CIECAM02 JCh
coloring that show_2d uses for complex arrays, add_arg_cbar_to_ax and
the ACOM angle maps, so the wheel is an exact legend for those images.
Its rim keeps a lightness J of about 58 all the way round, where the HSV
rim runs from 19 (blue) to 91 (yellow). saturation sets chroma_boost,
and repeat and gamma work as in the HSV mode. HSV stays the default, so
composite_ddf still matches its legend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
The 0-255 check ran per color, so (1, 1, 1) in a list of 0-255 colors
was read as white rather than near black. The scale is now decided once
for the whole list: if any tuple has a value above 1, all of them are
read as 0-255.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
color_wheel and composite_ddf now color with array_to_rgba (CIECAM02 hue
at constant lightness) by default, as show_2d does for complex arrays,
add_arg_cbar_to_ax and the ACOM angle maps. mode="hsv" gives the HSV
hues instead. Both take mode=None by default, which means perceptual
unless the caller passes its own colors (color_list, or colors for
composite_ddf); giving both a mode and colors is an error.

In both modes composite_ddf colors image k with the wheel's hue at angle
2 pi k / K and its scaled intensity as the radius, so
color_wheel(gamma=gamma, mode=mode) is an exact legend of the composite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
An empty list failed with an IndexError and a single color with a
matplotlib ValueError; both now raise a ValueError that says the list
needs at least two colors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
Also drop test arguments that repeated the defaults (repeat=1 and the
gamma=1.0 case).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKw4bfi2sFksBeLfpxq732
@maclariz
maclariz force-pushed the color-wheel-to-core branch from 661c49c to 97cea6b Compare December 6, 2026 17:48
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

Successfully merging this pull request may close these issues.

2 participants