Repository navigation
Extend heatmap into 3D surface with framing and height scale #2081
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NAThompson
wants to merge
1
commit into
silx-kit:main
Choose a base branch
from
NAThompson:feat/surface-vis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,95 @@ | ||
| import { getDomain, mockValues, SurfaceVis } from '@h5web/lib'; | ||
| import { assertDefined } from '@h5web/shared/guards'; | ||
| import { COLOR_SCALE_TYPES, ScaleType } from '@h5web/shared/vis-models'; | ||
| import { createArrayFromView } from '@h5web/shared/vis-utils'; | ||
| import { extend, type Object3DNode, useThree } from '@react-three/fiber'; | ||
| import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
| import ndarray from 'ndarray'; | ||
|
|
||
| import preview from '../.storybook/preview'; | ||
| import FillHeight from './decorators/FillHeight'; | ||
|
|
||
| extend({ OrbitControls }); | ||
| declare module '@react-three/fiber' { | ||
| interface ThreeElements { | ||
| orbitControls: Object3DNode<OrbitControls, typeof OrbitControls>; | ||
| } | ||
| } | ||
|
|
||
| const dataArray = createArrayFromView(mockValues.fourD().pick(0, 0)); | ||
| const domain = getDomain(dataArray.data); | ||
| assertDefined(domain); | ||
|
|
||
| const [rows, cols] = dataArray.shape; | ||
| const abscissas = Float32Array.from({ length: cols }, (_, i) => -2 + i * 0.1); | ||
| const ordinates = Float32Array.from({ length: rows }, (_, i) => i ** 1.5); | ||
|
|
||
| const withHoles = ndarray( | ||
| Float32Array.from(dataArray.data, (val, i) => | ||
| i % cols > cols / 3 && i % cols < cols / 2 ? Number.NaN : val, | ||
| ), | ||
| dataArray.shape, | ||
| ); | ||
|
|
||
| const meta = preview.meta({ | ||
| title: 'Experimental/SurfaceVis', | ||
| title: 'Visualizations/SurfaceVis', | ||
| component: SurfaceVis, | ||
| decorators: [FillHeight], | ||
| args: { | ||
| dataArray, | ||
| domain, | ||
| }, | ||
| argTypes: { | ||
| dataArray: { control: false }, | ||
| scaleType: { | ||
| control: { type: 'inline-radio' }, | ||
| options: COLOR_SCALE_TYPES, | ||
| }, | ||
| heightScale: { | ||
| control: { type: 'range', min: 0.05, max: 2, step: 0.05 }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export const Default = meta.story({ | ||
| render: (args) => ( | ||
| <SurfaceVis {...args}> | ||
| <Controls /> | ||
| </SurfaceVis> | ||
| ), | ||
| export const Default = meta.story(); | ||
|
|
||
| export const AxisValues = meta.story({ | ||
| args: { | ||
| dataArray, | ||
| domain, | ||
| title: 'With axis values', | ||
| abscissaParams: { value: abscissas }, | ||
| ordinateParams: { value: ordinates }, | ||
| }, | ||
| }); | ||
|
|
||
| function Controls() { | ||
| const camera = useThree((state) => state.camera); | ||
| const domElement = useThree((state) => state.gl.domElement); | ||
| return <orbitControls args={[camera, domElement]} />; | ||
| // Positive data spanning five orders of magnitude: three Gaussian peaks over a low background | ||
| function peak(x: number, y: number, params: [number, number, number, number]) { | ||
| const [cx, cy, amp, width] = params; | ||
| return amp * Math.exp(-((x - cx) ** 2 + (y - cy) ** 2) / (2 * width ** 2)); | ||
| } | ||
|
|
||
| const peaksArray = ndarray( | ||
| Float32Array.from({ length: 80 * 120 }, (_, i) => { | ||
| const x = (i % 120) / 120; | ||
| const y = Math.floor(i / 120) / 80; | ||
| return ( | ||
| 1e-4 + | ||
| peak(x, y, [0.3, 0.4, 1, 0.08]) + | ||
| peak(x, y, [0.7, 0.6, 0.05, 0.06]) + | ||
| peak(x, y, [0.55, 0.2, 0.003, 0.05]) | ||
| ); | ||
| }), | ||
| [80, 120], | ||
| ); | ||
|
|
||
| export const LogScale = meta.story({ | ||
| args: { | ||
| title: 'Peaks (log scale)', | ||
| dataArray: peaksArray, | ||
| domain: getDomain(peaksArray.data, { scaleType: ScaleType.Log }), | ||
| scaleType: ScaleType.Log, | ||
| }, | ||
| }); | ||
|
|
||
| export const NaNHoles = meta.story({ | ||
| name: 'With NaN', | ||
| args: { | ||
| dataArray: withHoles, | ||
| }, | ||
| }); | ||
|
|
||
| export const Downsampled = meta.story({ | ||
| args: { | ||
| maxVertices: 1000, | ||
| }, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.23 KB
(100%)
cypress/snapshots/app.cy.ts/heatmap_2D_inverted_cmap.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.45 KB
(99%)
cypress/snapshots/app.cy.ts/nxheatmap_complex_2d_float_aux.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 186 additions & 0 deletions
186
packages/app/src/__tests__/HeatmapSurface.browser.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import { afterEach, beforeEach, expect, test } from 'vitest'; | ||
| import { page } from 'vitest/browser'; | ||
|
|
||
| import { getSelectedVisTab, getVisTabs, renderApp } from '../test-utils'; | ||
| import { Vis } from '../vis-packs/core/visualizations'; | ||
| import { NxDataVis } from '../vis-packs/nexus/visualizations'; | ||
|
|
||
| function surfaceBtn() { | ||
| return page.getByRole('button', { name: 'Show as 3D surface' }); | ||
| } | ||
|
|
||
| function heightSlider() { | ||
| return page.getByRole('slider', { name: 'Height scale' }); | ||
| } | ||
|
|
||
| function resetViewBtn() { | ||
| return page.getByRole('button', { name: 'Reset view' }); | ||
| } | ||
|
|
||
| /* Each switch between heatmap and surface creates a new WebGL context, which takes ~0.7 s with | ||
| * SwiftShader on an idle machine (profiled: `getContext` dominates; geometry, labels and React work | ||
| * take a few ms) and more when many test files create contexts in parallel, as in CI. | ||
| * ("THREE.WebGLRenderer: Context Lost." is R3F disposing the previous canvas on unmount.) */ | ||
| const SURFACE_TEST_TIMEOUT = 20_000; | ||
|
|
||
| /* In 3D, the toolbar gets wider (height slider), so at the default 1920px viewport | ||
| * it re-measures after the switch and moves its last controls (Flip Y, Keep ratio, Grid) into the | ||
| * "More controls" menu. Querying those controls during that re-layout made tests flaky (the button | ||
| * would detach and the locator would wait until timeout). Use a viewport wide enough for the whole | ||
| * 3D toolbar to fit, so no control moves. */ | ||
| beforeEach(async () => { | ||
| await page.viewport(2560, 1080); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await page.viewport(1920, 1080); | ||
| }); | ||
|
|
||
| test( | ||
| 'extend heatmap into 3D surface and back', | ||
| async () => { | ||
| const { user } = await renderApp('/arrays/twoD'); | ||
|
|
||
| expect(getVisTabs()).toEqual([Vis.Matrix, Vis.Line, Vis.Heatmap]); | ||
| expect(getSelectedVisTab()).toBe(Vis.Heatmap); | ||
| expect(surfaceBtn()).toHaveAttribute('aria-pressed', 'false'); | ||
| expect(heightSlider()).not.toBeInTheDocument(); | ||
|
|
||
| await user.click(surfaceBtn()); | ||
|
|
||
| // Same vis tab, same dataset, now rendered as a surface | ||
| expect(getSelectedVisTab()).toBe(Vis.Heatmap); | ||
| expect(surfaceBtn()).toHaveAttribute('aria-pressed', 'true'); | ||
| await expect | ||
| .element(page.getByRole('figure', { name: 'twoD' })) | ||
| .toBeVisible(); | ||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
| expect(heightSlider()).toHaveValue('0.5'); | ||
|
|
||
| await user.click(surfaceBtn()); | ||
|
|
||
| expect(surfaceBtn()).toHaveAttribute('aria-pressed', 'false'); | ||
| expect(resetViewBtn()).not.toBeInTheDocument(); | ||
| expect(heightSlider()).not.toBeInTheDocument(); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test( | ||
| 'carry heatmap settings over to 3D surface', | ||
| async () => { | ||
| const { user } = await renderApp('/arrays/twoD'); | ||
|
|
||
| await user.click(page.getByRole('button', { name: 'Invert' })); | ||
| await user.click(page.getByRole('button', { name: 'Keep ratio' })); | ||
|
|
||
| await user.click(surfaceBtn()); | ||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
|
|
||
| expect(page.getByRole('button', { name: 'Invert' })).toHaveAttribute( | ||
| 'aria-pressed', | ||
| 'true', | ||
| ); | ||
| expect(page.getByRole('button', { name: 'Keep ratio' })).toHaveAttribute( | ||
| 'aria-pressed', | ||
| 'false', | ||
| ); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test( | ||
| 'keep settings changed in 3D when going back to heatmap', | ||
| async () => { | ||
| const { user } = await renderApp('/arrays/twoD'); | ||
|
|
||
| await user.click(surfaceBtn()); | ||
| await user.click(page.getByRole('button', { name: 'Flip X' })); | ||
| await user.click(surfaceBtn()); | ||
|
|
||
| expect(resetViewBtn()).not.toBeInTheDocument(); | ||
| expect(page.getByRole('button', { name: 'Flip X' })).toHaveAttribute( | ||
| 'aria-pressed', | ||
| 'true', | ||
| ); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test( | ||
| 'keep current slice and 3D mode when switching', | ||
| async () => { | ||
| const { user, selectExplorerNode } = await renderApp('/arrays/threeD'); | ||
|
|
||
| const d0Slider = page.getByRole('slider', { name: 'D0' }); | ||
| await user.type(d0Slider, '{ArrowUp}'); | ||
| expect(d0Slider).toHaveValue(1); | ||
|
|
||
| await user.click(surfaceBtn()); | ||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
| expect(d0Slider).toHaveValue(1); | ||
|
|
||
| // 3D mode is remembered when selecting another dataset | ||
| await selectExplorerNode('twoD'); | ||
| expect(surfaceBtn()).toHaveAttribute('aria-pressed', 'true'); | ||
| await expect | ||
| .element(page.getByRole('figure', { name: 'twoD' })) | ||
| .toBeVisible(); | ||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test( | ||
| 'show interactions help for 3D surface', | ||
| async () => { | ||
| const { user } = await renderApp('/arrays/twoD'); | ||
| await user.click(surfaceBtn()); | ||
|
|
||
| await user.click(page.getByRole('button', { name: 'Show help' })); | ||
| expect(page.getByText('Rotate')).toBeVisible(); | ||
| expect(page.getByText('Select to zoom')).not.toBeInTheDocument(); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test('offer 3D surface for NeXus images but not for complex heatmaps', async () => { | ||
| const { selectNexusExplorerNode } = await renderApp('/nexus/image'); | ||
|
|
||
| expect(getSelectedVisTab()).toBe(NxDataVis.NxHeatmap); | ||
| expect(surfaceBtn()).toBeVisible(); | ||
|
|
||
| await selectNexusExplorerNode('image_complex'); | ||
| expect(getSelectedVisTab()).toBe(NxDataVis.NxHeatmap); | ||
| expect(surfaceBtn()).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test( | ||
| 'fall back to heatmap for slices with a single row or column', | ||
| async () => { | ||
| const { user, selectExplorerNode } = await renderApp('/arrays/twoD'); | ||
| await user.click(surfaceBtn()); | ||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
|
|
||
| // 1 x 10 dataset: no surface can be built | ||
| await selectExplorerNode('twoD_negative'); | ||
| expect(surfaceBtn()).not.toBeInTheDocument(); | ||
| expect(resetViewBtn()).not.toBeInTheDocument(); | ||
| await expect | ||
| .element(page.getByRole('figure', { name: 'twoD_negative' })) | ||
| .toBeVisible(); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); | ||
|
|
||
| test( | ||
| 'show 3D surface of NeXus image with descending axes', | ||
| async () => { | ||
| const { user } = await renderApp('/nexus/descending-axes'); | ||
| await user.click(surfaceBtn()); | ||
|
|
||
| await expect.element(resetViewBtn()).toBeVisible(); | ||
| expect(surfaceBtn()).toHaveAttribute('aria-pressed', 'true'); | ||
| }, | ||
| SURFACE_TEST_TIMEOUT, | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The title displayed weirdly on my end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks! The story now uses
name: 'With NaN'.