-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: improve types in utils functions
- Loading branch information
Showing
11 changed files
with
290 additions
and
279 deletions.
There are no files selected for viewing
This file contains 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,109 +1,108 @@ | ||
import { createFromToArray } from '../../index'; | ||
import { createFromToArray } from '../createFromToArray'; | ||
|
||
describe('createFromToArray', () => { | ||
it('case when we sample within a specific range with log distribution and include start and end points', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: true, | ||
includeTo: true, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([2, 4, 8, 16, 32]); | ||
test('case when we sample within a specific range with log distribution and include start and end points', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: true, | ||
includeTo: true, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([2, 4, 8, 16, 32]); | ||
}); | ||
|
||
it('case when we sample within a specific range with log distribution and include start point but not end point', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: true, | ||
includeTo: false, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
2, 3.4822022531844965, 6.062866266041592, 10.556063286183154, | ||
18.379173679952558, | ||
]); | ||
test('case when we sample within a specific range with log distribution and include start point but not end point', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: true, | ||
includeTo: false, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
2, 3.4822022531844965, 6.062866266041592, 10.556063286183154, | ||
18.379173679952558, | ||
]); | ||
}); | ||
|
||
it('case when we sample within a specific range with log distribution and include end point but not start point', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: false, | ||
includeTo: true, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
3.4822022531844965, 6.062866266041592, 10.556063286183154, | ||
18.379173679952558, 32, | ||
]); | ||
test('case when we sample within a specific range with log distribution and include end point but not start point', () => { | ||
const result = createFromToArray({ | ||
from: 2, | ||
to: 32, | ||
length: 5, | ||
includeFrom: false, | ||
includeTo: true, | ||
distribution: 'log', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
3.4822022531844965, 6.062866266041592, 10.556063286183154, | ||
18.379173679952558, 32, | ||
]); | ||
}); | ||
|
||
it('case when we sample within a speci]fic range with uniform distribution and include start and end points', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: true, | ||
includeTo: true, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([1, 12, 23, 34, 45, 56, 67, 78, 89, 100]); | ||
test('case when we sample within a speci]fic range with uniform distribution and include start and end points', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: true, | ||
includeTo: true, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([1, 12, 23, 34, 45, 56, 67, 78, 89, 100]); | ||
}); | ||
|
||
it('case when we sample within a specific range with uniform distribution and include start point but do not include end point', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: true, | ||
includeTo: false, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
1, 10.9, 20.8, 30.7, 40.6, 50.5, 60.4, 70.3, 80.2, 90.1, | ||
]); | ||
test('case when we sample within a specific range with uniform distribution and include start point but do not include end point', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: true, | ||
includeTo: false, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
1, 10.9, 20.8, 30.7, 40.6, 50.5, 60.4, 70.3, 80.2, 90.1, | ||
]); | ||
}); | ||
|
||
it('case when we sample within a specific range with uniform distribution and include end point but do not include start point', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: false, | ||
includeTo: true, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
10.9, 20.8, 30.7, 40.6, 50.5, 60.4, 70.3, 80.2, 90.1, 100, | ||
]); | ||
test('case when we sample within a specific range with uniform distribution and include end point but do not include start point', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: false, | ||
includeTo: true, | ||
distribution: 'uniform', | ||
}); | ||
expect(result).toBeDeepCloseTo([ | ||
10.9, 20.8, 30.7, 40.6, 50.5, 60.4, 70.3, 80.2, 90.1, 100, | ||
]); | ||
}); | ||
|
||
test('case when we sample within a specific range with uniform distribution and do not include start point or end point', () => { | ||
const result = createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: false, | ||
includeTo: false, | ||
distribution: 'uniform', | ||
}); | ||
|
||
it('case when we sample within a specific range with uniform distribution and do not include start point or end point', () => { | ||
const result = createFromToArray({ | ||
expect(result).toBeDeepCloseTo([10, 19, 28, 37, 46, 55, 64, 73, 82, 91]); | ||
}); | ||
|
||
test('case when we choose a distribution other than uniform or log', () => { | ||
expect(() => { | ||
createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
includeFrom: false, | ||
includeTo: false, | ||
distribution: 'uniform', | ||
// @ts-expect-error Testing bad argument. | ||
distribution: 'other', | ||
}); | ||
|
||
expect(result).toBeDeepCloseTo([10, 19, 28, 37, 46, 55, 64, 73, 82, 91]); | ||
}); | ||
|
||
it('case when we choose a distribution other than uniform or log', () => { | ||
expect(() => { | ||
createFromToArray({ | ||
from: 1, | ||
to: 100, | ||
length: 10, | ||
distribution: 'other', | ||
}); | ||
}).toThrow(Error); | ||
}); | ||
}).toThrow(Error); | ||
}); |
This file contains 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,66 +1,65 @@ | ||
import { optimize } from 'ml-spectra-fitting'; | ||
|
||
import { createRandomArray, xHistogram } from '../../index'; | ||
import { xHistogram } from '../../x'; | ||
import { createRandomArray } from '../createRandomArray'; | ||
|
||
describe('createRandomXArray', () => { | ||
it('normal distribution', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
standardDeviation: 0.001, | ||
length: 5, | ||
seed: 0, | ||
}); | ||
expect(array).toBeDeepCloseTo([10, 10, 10, 10, 10], 1); | ||
test('normal distribution', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
standardDeviation: 0.001, | ||
length: 5, | ||
seed: 0, | ||
}); | ||
expect(array).toBeDeepCloseTo([10, 10, 10, 10, 10], 1); | ||
}); | ||
|
||
it('normal distribution default mean (0)', () => { | ||
const array = createRandomArray({ | ||
standardDeviation: 1, | ||
length: 10000, | ||
seed: 0, | ||
}); | ||
expect(Math.min(...array)).toBeCloseTo(-3.7159385968751244); | ||
expect(Math.max(...array)).toBeCloseTo(3.606967549040919); | ||
expect( | ||
array.reduce((previous, current) => previous + current, 0) / 10000, | ||
).toBeCloseTo(0); | ||
test('normal distribution default mean (0)', () => { | ||
const array = createRandomArray({ | ||
standardDeviation: 1, | ||
length: 10000, | ||
seed: 0, | ||
}); | ||
expect(Math.min(...array)).toBeCloseTo(-3.7159385968751244); | ||
expect(Math.max(...array)).toBeCloseTo(3.606967549040919); | ||
expect( | ||
array.reduce((previous, current) => previous + current, 0) / 10000, | ||
).toBeCloseTo(0); | ||
}); | ||
|
||
it('uniform distribution', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
range: 2, | ||
length: 100000, | ||
distribution: 'uniform', | ||
seed: 0, | ||
}); | ||
const histogram = xHistogram(array, { nbSlots: 10 }); | ||
for (let i = 0; i < histogram.x.length; i++) { | ||
const slot = 9 + 0.1 + i * 0.2; | ||
expect(histogram.x[i]).toBeCloseTo(slot); | ||
} | ||
for (const y of histogram.y) { | ||
expect(y).toBeGreaterThan(9500); | ||
expect(y).toBeLessThan(10500); | ||
} | ||
test('uniform distribution', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
range: 2, | ||
length: 100000, | ||
distribution: 'uniform', | ||
seed: 0, | ||
}); | ||
const histogram = xHistogram(array, { nbSlots: 10 }); | ||
for (let i = 0; i < histogram.x.length; i++) { | ||
const slot = 9 + 0.1 + i * 0.2; | ||
expect(histogram.x[i]).toBeCloseTo(slot); | ||
} | ||
for (const y of histogram.y) { | ||
expect(y).toBeGreaterThan(9500); | ||
expect(y).toBeLessThan(10500); | ||
} | ||
}); | ||
|
||
it('Testing in conjunction with spectra-fitting', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
standardDeviation: 1, | ||
length: 100000, | ||
distribution: 'normal', | ||
seed: 0, | ||
}); | ||
const histogram = xHistogram(array, { centerX: false }); | ||
const fittedPeaks = optimize(histogram, [{ x: 10, y: 0.1 }], { | ||
shape: { kind: 'gaussian', fwhm: 2 }, | ||
}); | ||
expect(fittedPeaks.peaks[0].x).toBeDeepCloseTo(10, 2); | ||
expect(fittedPeaks.peaks[0].shape.fwhm).toBeDeepCloseTo( | ||
2 * Math.sqrt(2 * Math.log(2)), | ||
1, | ||
); | ||
test('Testing in conjunction with spectra-fitting', () => { | ||
const array = createRandomArray({ | ||
mean: 10, | ||
standardDeviation: 1, | ||
length: 100000, | ||
distribution: 'normal', | ||
seed: 0, | ||
}); | ||
const histogram = xHistogram(array, { centerX: false }); | ||
const fittedPeaks = optimize(histogram, [{ x: 10, y: 0.1 }], { | ||
shape: { kind: 'gaussian', fwhm: 2 }, | ||
}); | ||
expect(fittedPeaks.peaks[0].x).toBeDeepCloseTo(10, 2); | ||
expect(fittedPeaks.peaks[0].shape.fwhm).toBeDeepCloseTo( | ||
2 * Math.sqrt(2 * Math.log(2)), | ||
1, | ||
); | ||
}); |
This file contains 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,13 +1,11 @@ | ||
import { createStepArray } from '../../index'; | ||
import { createStepArray } from '../createStepArray'; | ||
|
||
describe('createStepArray', () => { | ||
it('case when we specify the step', () => { | ||
const result = createStepArray({ | ||
from: 1, | ||
step: 10, | ||
length: 10, | ||
}); | ||
|
||
expect(result).toBeDeepCloseTo([1, 11, 21, 31, 41, 51, 61, 71, 81, 91]); | ||
test('case when we specify the step', () => { | ||
const result = createStepArray({ | ||
from: 1, | ||
step: 10, | ||
length: 10, | ||
}); | ||
|
||
expect(result).toBeDeepCloseTo([1, 11, 21, 31, 41, 51, 61, 71, 81, 91]); | ||
}); |
This file contains 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 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
Oops, something went wrong.