Skip to content

Commit

Permalink
fix!: improve types in xyArray 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 5c4a68f commit 018b825
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 253 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ exports[`test existence of exported functions 1`] = `
"xreimSortX",
"xreimZeroFilling",
"xyArrayAlign",
"xyArrayAlignToFirst",
"xyArrayMerge",
"xyArrayWeightedMerge",
"xyArrayAlignToFirst",
"xyObjectBestPoints",
"xyObjectJoinX",
"xyObjectMaxXPoint",
Expand Down
2 changes: 1 addition & 1 deletion src/xy2/__tests__/xy2ToXY.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { xy2ToXY } from '../../index';
import { xy2ToXY } from '../xy2ToXY';

test('xy2ToXY', () => {
expect(
Expand Down
60 changes: 26 additions & 34 deletions src/xyArray/__tests__/xyArrayAlign.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
import { xyArrayAlign } from '../../index';
import { xyArrayAlign } from '../xyArrayAlign';

describe('xyArrayAlign', () => {
it('same length spectra, simple integers', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayAlign(data, { delta: 0.15 });
test('same length spectra, simple integers', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayAlign(data, { delta: 0.15 });

result.x = Array.from(result.x);
result.ys = result.ys.map((array) => Array.from(array));

expect(result).toStrictEqual({
x: [0.1, 1.05, 2.05, 3.025, 3.9, 4.1, 4.9],
ys: [
[0, 1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 2, 1, 0, 1],
],
});
expect(result).toStrictEqual({
x: Float64Array.from([0.1, 1.05, 2.05, 3.025, 3.9, 4.1, 4.9]),
ys: [
Float64Array.from([0, 1, 1, 1, 0, 0, 0]),
Float64Array.from([1, 1, 1, 1, 0, 1, 0]),
Float64Array.from([0, 0, 0, 2, 1, 0, 1]),
],
});
});

it('The y values must be present everywhere', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayAlign(data, { delta: 0.15, requiredY: true });

result.x = Array.from(result.x);
result.ys = result.ys.map((array) => Array.from(array));
test('The y values must be present everywhere', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayAlign(data, { delta: 0.15, requiredY: true });

expect(result).toStrictEqual({
x: [3.025],
ys: [[1], [1], [2]],
});
expect(result).toStrictEqual({
x: [3.025],
ys: [[1], [1], [2]],
});
});
66 changes: 30 additions & 36 deletions src/xyArray/__tests__/xyArrayAlignToFirst.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import { xyArrayAlignToFirst } from '../../index';
import { xyArrayAlignToFirst } from '../xyArrayAlignToFirst';

describe('xyArrayAlignToFirst', () => {
it('mixed example with delta: 0.25', () => {
const data = [
{ x: [1, 1.1, 2, 4], y: [1, 1, 1, 1] },
{ x: [0.1, 0.95, 1.05, 3], y: [1, 1, 1, 1] },
{ x: [2.05, 3.9, 4.1, 4.9, 5.1], y: [1, 1, 1, 1, 3] },
];
const result = xyArrayAlignToFirst(data, { delta: 0.25 });
result.x = Array.from(result.x);
result.ys = result.ys.map((array) => Array.from(array));
expect(result).toStrictEqual({
x: [0.1, 1, 1.1, 2, 3, 4, 5.05],
ys: [
[0, 1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 2, 4],
],
});
test('mixed example with delta: 0.25', () => {
const data = [
{ x: [1, 1.1, 2, 4], y: [1, 1, 1, 1] },
{ x: [0.1, 0.95, 1.05, 3], y: [1, 1, 1, 1] },
{ x: [2.05, 3.9, 4.1, 4.9, 5.1], y: [1, 1, 1, 1, 3] },
];
const result = xyArrayAlignToFirst(data, { delta: 0.25 });
expect(result).toStrictEqual({
x: Float64Array.from([0.1, 1, 1.1, 2, 3, 4, 5.05]),
ys: [
Float64Array.from([0, 1, 1, 1, 0, 1, 0]),
Float64Array.from([1, 1, 1, 0, 1, 0, 0]),
Float64Array.from([0, 0, 0, 1, 0, 2, 4]),
],
});
});

it('mixed example with delta a callback', () => {
const data = [
{ x: [1, 1.1, 2, 4], y: [1, 1, 1, 1] },
{ x: [0.1, 0.95, 1.05, 3], y: [1, 1, 1, 1] },
{ x: [2.05, 3.9, 4.1, 4.9, 5.1], y: [1, 1, 1, 1, 3] },
];
const result = xyArrayAlignToFirst(data, { delta: () => 0.25 });
result.x = Array.from(result.x);
result.ys = result.ys.map((array) => Array.from(array));
expect(result).toStrictEqual({
x: [0.1, 1, 1.1, 2, 3, 4, 5.05],
ys: [
[0, 1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 2, 4],
],
});
test('mixed example with delta a callback', () => {
const data = [
{ x: [1, 1.1, 2, 4], y: [1, 1, 1, 1] },
{ x: [0.1, 0.95, 1.05, 3], y: [1, 1, 1, 1] },
{ x: [2.05, 3.9, 4.1, 4.9, 5.1], y: [1, 1, 1, 1, 3] },
];
const result = xyArrayAlignToFirst(data, { delta: () => 0.25 });
expect(result).toStrictEqual({
x: Float64Array.from([0.1, 1, 1.1, 2, 3, 4, 5.05]),
ys: [
Float64Array.from([0, 1, 1, 1, 0, 1, 0]),
Float64Array.from([1, 1, 1, 0, 1, 0, 0]),
Float64Array.from([0, 0, 0, 1, 0, 2, 4]),
],
});
});
27 changes: 11 additions & 16 deletions src/xyArray/__tests__/xyArrayMerge.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { xyArrayMerge } from '../../index';
import { xyArrayMerge } from '../xyArrayMerge';

describe('xyArrayMerge', () => {
it('same length spectra, simple integers', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayMerge(data, { delta: 0.15 });
test('same length spectra, simple integers', () => {
const data = [
{ x: [1, 2, 3], y: [1, 1, 1] },
{ x: [0.1, 1.1, 2.1, 3.1, 4.1], y: [1, 1, 1, 1, 1] },
{ x: [2.9, 3.1, 3.9, 4.9], y: [1, 1, 1, 1] },
];
const result = xyArrayMerge(data, { delta: 0.15 });

result.x = Array.from(result.x);
result.y = Array.from(result.y);

expect(result).toStrictEqual({
x: [0.1, 1.05, 2.05, 3.025, 3.9, 4.1, 4.9],
y: [1, 2, 2, 4, 1, 1, 1],
});
expect(result).toStrictEqual({
x: Float64Array.from([0.1, 1.05, 2.05, 3.025, 3.9, 4.1, 4.9]),
y: Float64Array.from([1, 2, 2, 4, 1, 1, 1]),
});
});
176 changes: 87 additions & 89 deletions src/xyArray/__tests__/xyArrayWeightedMerge.test.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,105 @@
import { DataXY } from 'cheminfo-types';
import XSAdd from 'ml-xsadd';

import { xyArrayWeightedMerge } from '../../index';
import { xyArrayWeightedMerge } from '../xyArrayWeightedMerge';

describe('xyArrayWeightedMerge', () => {
it('2 slots', () => {
const data = [
{ x: [100, 202, 300], y: [10, 30, 20] },
{ x: [101, 201, 400], y: [30, 10, 40] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });
test('2 slots', () => {
const data = [
{ x: [100, 202, 300], y: [10, 30, 20] },
{ x: [101, 201, 400], y: [30, 10, 40] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });

expect(result).toStrictEqual({
x: [100.75, 201.75, 300, 400],
y: [40, 40, 20, 40],
});
expect(result).toStrictEqual({
x: [100.75, 201.75, 300, 400],
y: [40, 40, 20, 40],
});
});

it('simple no merge', () => {
const data = [
{ x: [10, 20], y: [1, 2] },
{ x: [60, 70], y: [6, 7] },
{ x: [30, 40], y: [3, 4] },
{ x: [50, 80], y: [5, 8] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result).toStrictEqual({
x: [10, 20, 30, 40, 50, 60, 70, 80],
y: [1, 2, 3, 4, 5, 6, 7, 8],
});
test('simple no merge', () => {
const data = [
{ x: [10, 20], y: [1, 2] },
{ x: [60, 70], y: [6, 7] },
{ x: [30, 40], y: [3, 4] },
{ x: [50, 80], y: [5, 8] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result).toStrictEqual({
x: [10, 20, 30, 40, 50, 60, 70, 80],
y: [1, 2, 3, 4, 5, 6, 7, 8],
});
});

it('simple full merge', () => {
const data = [
{ x: [1, 2], y: [1, 2] },
{ x: [6, 7], y: [6, 7] },
{ x: [3, 4], y: [3, 4] },
{ x: [5, 8], y: [5, 8] },
];
const result = xyArrayWeightedMerge(data);
expect(result).toMatchCloseTo({ x: [5.666666666666667], y: [36] });
});
test('simple full merge', () => {
const data = [
{ x: [1, 2], y: [1, 2] },
{ x: [6, 7], y: [6, 7] },
{ x: [3, 4], y: [3, 4] },
{ x: [5, 8], y: [5, 8] },
];
const result = xyArrayWeightedMerge(data);
expect(result).toMatchCloseTo({ x: [5.666666666666667], y: [36] });
});

it('check dense weighted average for x', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });
test('check dense weighted average for x', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: 2 });

expect(result).toMatchCloseTo({
x: [101.78571428571429, 201.13333333333333, 300],
y: [140, 150, 50],
});
expect(result).toMatchCloseTo({
x: [101.78571428571429, 201.13333333333333, 300],
y: [140, 150, 50],
});
});

it('large slot', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: 100 });
expect(result).toMatchCloseTo({ x: [174.76470588235293], y: [340] });
});
test('large slot', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: 100 });
expect(result).toMatchCloseTo({ x: [174.76470588235293], y: [340] });
});

it('function merge', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: (x) => x * x });
expect(result).toMatchCloseTo({ x: [174.76470588235293], y: [340] });
});
test('function merge', () => {
const data = [
{ x: [100, 101, 102, 200, 201, 202], y: [10, 20, 30, 40, 50, 60] },
{ x: [101, 102, 103, 300], y: [30, 10, 40, 50] },
];
const result = xyArrayWeightedMerge(data, { delta: (x) => x * x });
expect(result).toMatchCloseTo({ x: [174.76470588235293], y: [340] });
});

it('empty data', () => {
const data: DataXY[] = [];
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result).toMatchCloseTo({ x: [], y: [] });
});
test('empty data', () => {
const data: DataXY[] = [];
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result).toMatchCloseTo({ x: [], y: [] });
});

it('large Data Set', () => {
const data = [];
const arraySize = 1e5;
const generator = new XSAdd(0);
for (let dataset = 0; dataset < 20; dataset++) {
const datum = {
x: new Float64Array(arraySize),
y: new Float64Array(arraySize),
};
data.push(datum);
for (let i = 0; i < arraySize; i++) {
datum.x[i] =
Math.round(generator.random() * 100) * 10 + generator.random();
datum.y[i] = generator.random();
}
datum.x.sort();
datum.y.sort();
test('large Data Set', () => {
const data = [];
const arraySize = 1e5;
const generator = new XSAdd(0);
for (let dataset = 0; dataset < 20; dataset++) {
const datum = {
x: new Float64Array(arraySize),
y: new Float64Array(arraySize),
};
data.push(datum);
for (let i = 0; i < arraySize; i++) {
datum.x[i] =
Math.round(generator.random() * 100) * 10 + generator.random();
datum.y[i] = generator.random();
}
const start = Date.now();
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result.x).toHaveLength(101);
expect(result.y).toHaveLength(101);
const elapsed = Date.now() - start;
expect(elapsed).toBeLessThanOrEqual(5000);
});
datum.x.sort();
datum.y.sort();
}
const start = Date.now();
const result = xyArrayWeightedMerge(data, { delta: 2 });
expect(result.x).toHaveLength(101);
expect(result.y).toHaveLength(101);
const elapsed = Date.now() - start;
expect(elapsed).toBeLessThanOrEqual(5000);
});
2 changes: 1 addition & 1 deletion src/xyArray/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './xyArrayAlign';
export * from './xyArrayAlignToFirst';
export * from './xyArrayMerge';
export * from './xyArrayWeightedMerge';
export * from './xyArrayAlignToFirst';
Loading

0 comments on commit 018b825

Please sign in to comment.