Skip to content

Commit

Permalink
fix!: improve types in utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
targos authored and lpatiny committed Feb 26, 2024
1 parent b6257b5 commit 7476f47
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 279 deletions.
181 changes: 90 additions & 91 deletions src/utils/__tests__/createFromToArray.ts
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);
});
109 changes: 54 additions & 55 deletions src/utils/__tests__/createRandomArray.test.ts
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,
);
});
18 changes: 8 additions & 10 deletions src/utils/__tests__/createStepArray.test.ts
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]);
});
2 changes: 1 addition & 1 deletion src/utils/__tests__/isPowerOfTwo.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPowerOfTwo } from '../..';
import { isPowerOfTwo } from '../isPowerOfTwo';

test('check if a power of two is next', () => {
expect(isPowerOfTwo(1024)).toEqual(true);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__tests__/nextPowerOfTwo.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nextPowerOfTwo } from '../..';
import { nextPowerOfTwo } from '../nextPowerOfTwo';

test('check if a power of two is next', () => {
expect(nextPowerOfTwo(1024)).toEqual(1024);
Expand Down
Loading

0 comments on commit 7476f47

Please sign in to comment.