Skip to content

feature: add jitterX and jitterY transforms #65

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

Merged
merged 24 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format
  • Loading branch information
gka committed May 26, 2025
commit 60930d1a2adf86daa25eb14748a0fd7af6c04935
20 changes: 5 additions & 15 deletions src/lib/transforms/jitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ describe('jitterX', () => {

const data = [{ y: 5 }, { y: 10 }];
// @ts-ignore - Bypassing type checking for tests
const result = jitterX(
{ data, y: 'y' },
{ source: mockRandom }
);
const result = jitterX({ data, y: 'y' }, { source: mockRandom });

// The result should be the same as the input
expect(result.data).toEqual(data);
Expand All @@ -85,17 +82,13 @@ describe('jitterX', () => {

const data = [{ x: new Date(Date.UTC(2020, 0, 1)) }, { x: new Date(Date.UTC(2021, 0, 1)) }];
// @ts-ignore - Bypassing type checking for tests
const result = jitterX(
{ data, x: 'x' },
{ source: mockRandom, width: '1 month' }
);
const result = jitterX({ data, x: 'x' }, { source: mockRandom, width: '1 month' });

const { x } = result;
expect(result.data[0][x]).toBeTypeOf("object");
expect(result.data[0][x].getTime).toBeTypeOf("function");
expect(result.data[0][x]).toBeTypeOf('object');
expect(result.data[0][x].getTime).toBeTypeOf('function');
expect(result.data[0][x]).toStrictEqual(new Date(Date.UTC(2020, 0, 16)));
});

});

describe('jitterY', () => {
Expand Down Expand Up @@ -163,10 +156,7 @@ describe('jitterY', () => {

const data = [{ x: 5 }, { x: 10 }];
// @ts-ignore - Bypassing type checking for tests
const result = jitterY(
{ data, x: 'x' },
{ source: mockRandom }
);
const result = jitterY({ data, x: 'x' }, { source: mockRandom });

// The result should be the same as the input
expect(result.data).toEqual(data);
Expand Down
13 changes: 7 additions & 6 deletions src/lib/transforms/jitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type JitterOptions = {
width: number;
/** standard deviation for normal jittering */
std: number;
/**
/**
* optional random number source that produces values in range [0,1)
* useful for testing with a deterministic source
*/
Expand Down Expand Up @@ -48,9 +48,10 @@ export function jitter(

// Use the provided source or default to Math.random
const rng = options?.source ?? Math.random;
const random = type === 'uniform'
? randomUniform.source(rng)(-width, width)
: randomNormal.source(rng)(0, std);
const random =
type === 'uniform'
? randomUniform.source(rng)(-width, width)
: randomNormal.source(rng)(0, std);

const accKey = channel === 'x' ? JITTER_X : JITTER_Y;
return {
Expand All @@ -62,8 +63,8 @@ export function jitter(
typeof value === 'number'
? value + random()
: isDate(value)
? new Date(value.getTime() + random())
: value
? new Date(value.getTime() + random())
: value
};
}),
...channels,
Expand Down
35 changes: 26 additions & 9 deletions src/routes/transforms/jitter/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The jitter transform accepts the following options:
- **type**: Distribution type, either `'uniform'` (default) or `'normal'`
- **width**: Width of the uniform distribution (default: `0.35`); used when `type` is `'uniform'`
- **std**: Standard deviation for the normal distribution (default: `0.15`); used when `type` is `'normal'`
- **source**: Optional random number source that produces values in range [0,1).
- **source**: Optional random number source that produces values in range [0,1).

## jitterX

Expand Down Expand Up @@ -102,23 +102,39 @@ Jittering also works for temporal data. When jittering Date objects, random time
let { bmi } = $derived(page.data.data);

// Use a subset of the data for this example
const data = $derived(bmi.filter(d => d.year > 2018).map(d => ({ ...d, year: new Date(d.year,0,1)})));
const data = $derived(
bmi
.filter((d) => d.year > 2018)
.map((d) => ({
...d,
year: new Date(d.year, 0, 1)
}))
);

let type = $state('uniform');
let width = $state('1 month');

const timeIntervals = ['1 day', '3 days', '1 week', '2 weeks', '3 weeks', '1 month', '2 months', '1 quarter', '1 year']
const timeIntervals = [
'1 day',
'3 days',
'1 week',
'2 weeks',
'3 weeks',
'1 month',
'2 months',
'1 quarter',
'1 year'
];
</script>

<Select
bind:value={type}
options={['uniform', 'normal']}
label="Distribution type" />
<Select
options={timeIntervals}
bind:value={width}
label={type === 'uniform' ? 'Width' : 'Std'}
/>
<Select
options={timeIntervals}
bind:value={width}
label={type === 'uniform' ? 'Width' : 'Std'} />

<Plot inset={20} x={{ type: 'time' }} y={{ grid: true }}>
<Dot
Expand All @@ -130,7 +146,8 @@ Jittering also works for temporal data. When jittering Date objects, random time
},
{
type,
[type === 'uniform' ? 'width' : 'std']: width,
[type === 'uniform' ? 'width' : 'std']:
width
}
)} />
</Plot>
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy