Repository navigation
Heatmaps plus #3427
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
Open
fakemonster
wants to merge
7
commits into
main
Choose a base branch
from
heatmaps-plus
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
Heatmaps plus #3427
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b8f4a5
Add logarithmic and ranked coloring strategies
fakemonster 45dee80
Add interface for picking color scales
fakemonster b2866ee
Use an actual logarithmic scale
fakemonster 32cca5a
Hide heatmap legend when there's no data, simplify when simple
fakemonster 6226609
Allow settings row to fully collapse, plus small refactors
fakemonster d05b1cb
Add cheap visual tests for point-dropping and color scaling
fakemonster f827549
'irrelevant' -> undefined
fakemonster 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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import { | ||
| getSampleCountMetadata, | ||
| getScalingFunction, | ||
| type ColorScale, | ||
| } from './heatmap-color-scale' | ||
|
|
||
| const scale = (counts: number[], colorScale: ColorScale) => | ||
| getScalingFunction(getSampleCountMetadata(counts), colorScale) | ||
|
|
||
| describe('getSampleCountMetadata', () => { | ||
| it('ignores zeros and duplicates', () => { | ||
| const { minSampleCount, maxSampleCount, ranks } = getSampleCountMetadata([ | ||
| 50, 0, 5, 50, 0, 100, | ||
| ]) | ||
| expect(minSampleCount).toBe(5) | ||
| expect(maxSampleCount).toBe(100) | ||
| expect([...ranks]).toEqual([ | ||
| [5, 0], | ||
| [50, 1], | ||
| [100, 2], | ||
| ]) | ||
| }) | ||
|
|
||
| it('handles lack of samples', () => { | ||
| const { minSampleCount, maxSampleCount, ranks } = getSampleCountMetadata([0, 0]) | ||
| expect(minSampleCount).toBe(0) | ||
| expect(maxSampleCount).toBe(0) | ||
| expect(ranks.size).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('linear', () => { | ||
| it('is the fraction of the max', () => { | ||
| const f = scale([10, 50, 200], 'linear') | ||
| expect(f(10)).toBe(0.05) | ||
| expect(f(50)).toBe(0.25) | ||
| expect(f(200)).toBe(1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('logarithmic', () => { | ||
| it("has 1 as 0 and the collection's greatest value as 1", () => { | ||
| const f = scale([10, 200], 'logarithmic') | ||
| expect(f(1)).toBe(0) | ||
| expect(f(200)).toBe(1) | ||
| }) | ||
|
|
||
| it('gives equal ratios equal steps', () => { | ||
| const expectEqualSpacing = (values: number[]) => { | ||
| const f = scale(values, 'logarithmic') | ||
| values.forEach((value, i) => expect(f(value)).toBeCloseTo(i / (values.length - 1))) | ||
| } | ||
|
|
||
| expectEqualSpacing([1, 10, 100, 1000, 10000]) | ||
| expectEqualSpacing([1, 7, 49, 343]) | ||
| }) | ||
|
|
||
| it("makes an exception when the collection's greatest value is 1", () => { | ||
| expect(scale([1, 0], 'logarithmic')(1)).toBe(1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('ranked', () => { | ||
| it('spaces distinct counts evenly', () => { | ||
| const f = scale([1, 2, 1000], 'ranked') | ||
| expect(f(1)).toBe(0) | ||
| expect(f(2)).toBe(0.5) | ||
| expect(f(1000)).toBe(1) | ||
| }) | ||
|
|
||
| it('ignores duplicates', () => { | ||
| const f = scale([3, 3, 7, 9], 'ranked') | ||
| expect(f(3)).toBe(0) | ||
| expect(f(7)).toBe(0.5) | ||
| }) | ||
|
|
||
| it('puts a single count at the top', () => { | ||
| expect(scale([4, 4, 0], 'ranked')(4)).toBe(1) | ||
| }) | ||
| }) |
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,61 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import * as R from 'remeda' | ||
| import { match } from 'ts-pattern' | ||
|
|
||
| export type ColorScale = 'linear' | 'logarithmic' | 'ranked' | ||
|
|
||
| const scaleLinear = | ||
| (maxCount: number) => | ||
| (count: number): number => | ||
| count / maxCount | ||
| const scaleLog = (maxCount: number) => | ||
| maxCount <= 1 | ||
| ? // if the max is 1, a logarithmic scaling function is undefined. but | ||
| // since 1 is then the only valid value, in a charting context it makes | ||
| // sense to treat that as a scale of 100% | ||
| R.constant(1) | ||
| : (count: number): number => Math.log(count) / Math.log(maxCount) | ||
| const scaleRank = (ranks: Map<number, number>) => | ||
| ranks.size <= 1 | ||
| ? R.constant(1) | ||
| : (count: number): number => (ranks.get(count) ?? 0) / (ranks.size - 1) | ||
|
|
||
| export type SampleCountMetadata = { | ||
| minSampleCount: number | ||
| maxSampleCount: number | ||
| ranks: Map<number, number> | ||
| } | ||
|
|
||
| export const getSampleCountMetadata = (counts: number[]): SampleCountMetadata => { | ||
| const allCounts = R.sort( | ||
| R.unique(counts).filter((x) => x !== 0), | ||
| (a, b) => a - b | ||
| ) | ||
|
|
||
| return { | ||
| minSampleCount: R.first(allCounts) ?? 0, | ||
| maxSampleCount: R.last(allCounts) ?? 0, | ||
| ranks: new Map(allCounts.map((n, i) => [n, i])), | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Maps a sample count to a position for a color ramp, from 0 to 1. | ||
| * Expects that the count given to the final scaling function is a nonzero | ||
| * natural number. | ||
| */ | ||
| export const getScalingFunction = ( | ||
| metadata: SampleCountMetadata, | ||
| colorScale: ColorScale | ||
| ): ((n: number) => number) => | ||
| match(colorScale) | ||
| .with('linear', () => scaleLinear(metadata.maxSampleCount)) | ||
| .with('logarithmic', () => scaleLog(metadata.maxSampleCount)) | ||
| .with('ranked', () => scaleRank(metadata.ranks)) | ||
| .exhaustive() | ||
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.
functional programmer in the house
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.
💅