-
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 xyArray functions
- Loading branch information
Showing
13 changed files
with
249 additions
and
253 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
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,4 +1,4 @@ | ||
import { xy2ToXY } from '../../index'; | ||
import { xy2ToXY } from '../xy2ToXY'; | ||
|
||
test('xy2ToXY', () => { | ||
expect( | ||
|
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,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]], | ||
}); | ||
}); |
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,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]), | ||
], | ||
}); | ||
}); |
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,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]), | ||
}); | ||
}); |
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,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); | ||
}); |
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,4 +1,4 @@ | ||
export * from './xyArrayAlign'; | ||
export * from './xyArrayAlignToFirst'; | ||
export * from './xyArrayMerge'; | ||
export * from './xyArrayWeightedMerge'; | ||
export * from './xyArrayAlignToFirst'; |
Oops, something went wrong.