Skip to content

Commit

Permalink
fix!: improve types in matrix 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 26c95e6 commit 1918fc8
Show file tree
Hide file tree
Showing 94 changed files with 778 additions and 874 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/fixtures/'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
};
3 changes: 3 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { toMatchCloseTo, toBeDeepCloseTo } from 'jest-matcher-deep-close-to';

expect.extend({ toMatchCloseTo, toBeDeepCloseTo });
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { DoubleArray } from 'cheminfo-types';

export * from './reim/index';

export * from './x/index';
Expand All @@ -22,8 +20,4 @@ export * from './zones/zonesNormalize';
export * from './matrix/index';
export * from './utils/index';

export type DoubleMatrix = DoubleArray[];

export * from './types/DataReIm';
export * from './types/DataXReIm';
export * from './types/Point';
export * from './types/index';
18 changes: 18 additions & 0 deletions src/matrix/__tests__/fixtures/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const datasetForEncoding = [
[73, 'a', 'o', 152],
[93, 'b', 'u', 185],
[89, 'c', 'p', 180],
[96, 'd', 'v', 196],
[73, 'e', 'j', 142],
[53, 'f', 'w', 101],
[69, 'g', 'x', 149],
[47, 'h', 'y', 115],
[87, 'i', 'p', 175],
[79, 'j', 'b', 164],
[69, 'j', 'm', 141],
[70, 'k', 'g', 141],
[93, 'l', 'c', 184],
[79, 'a', 'm', 152],
[70, 'm', 'z', 148],
[93, 'n', 'aa', 192],
];
71 changes: 25 additions & 46 deletions src/matrix/__tests__/matrixApplyNumericalDecoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,30 @@
import {
matrixApplyNumericalEncoding,
matrixNumericalEncoding,
} from '../../index';
import { matrixApplyNumericalEncoding } from '../matrixApplyNumericalEncoding';
import { matrixNumericalEncoding } from '../matrixNumericalEncoding';

describe('matrixApplyNumericalEncoding', () => {
const datasetForEncoding = [
[73, 'a', 'o', 152],
[93, 'b', 'u', 185],
[89, 'c', 'p', 180],
[96, 'd', 'v', 196],
[73, 'e', 'j', 142],
[53, 'f', 'w', 101],
[69, 'g', 'x', 149],
[47, 'h', 'y', 115],
[87, 'i', 'p', 175],
[79, 'j', 'b', 164],
[69, 'j', 'm', 141],
[70, 'k', 'g', 141],
[93, 'l', 'c', 184],
[79, 'a', 'm', 152],
[70, 'm', 'z', 148],
[93, 'n', 'aa', 192],
];
import { datasetForEncoding } from './fixtures/encoding';

const datasetToEncode = [
[78, 'o', 'ab', 147],
[81, 'p', 'u', 183],
[88, 'q', 's', 177],
[78, 'r', 'x', 159],
[82, 's', 'p', 177],
[86, 't', 'n', 175],
[78, 'r', 'ac', 175],
[76, 'r', 'ad', 149],
[96, 'u', 'l', 192],
];
const datasetToEncode = [
[78, 'o', 'ab', 147],
[81, 'p', 'u', 183],
[88, 'q', 's', 177],
[78, 'r', 'x', 159],
[82, 's', 'p', 177],
[86, 't', 'n', 175],
[78, 'r', 'ac', 175],
[76, 'r', 'ad', 149],
[96, 'u', 'l', 192],
];

it('should return an array of numbers', () => {
const { dictCategoricalToNumerical } =
matrixNumericalEncoding(datasetForEncoding);
const matrix = matrixApplyNumericalEncoding(
datasetToEncode,
dictCategoricalToNumerical,
);
test('should return an array of numbers', () => {
const { dictCategoricalToNumerical } =
matrixNumericalEncoding(datasetForEncoding);
const matrix = matrixApplyNumericalEncoding(
datasetToEncode,
dictCategoricalToNumerical,
);

const nonNumbers = matrix
.flat(2)
.filter((value) => typeof value !== 'number');
expect(nonNumbers).toHaveLength(0);
});
const nonNumbers = matrix
.flat(2)
.filter((value) => typeof value !== 'number');
expect(nonNumbers).toHaveLength(0);
});
34 changes: 16 additions & 18 deletions src/matrix/__tests__/matrixAutoCorrelation.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { matrixAutoCorrelation } from '../../index';
import { matrixAutoCorrelation } from '../matrixAutoCorrelation';

describe('matrixAutoCorrelation', () => {
it('simple', () => {
const matrix = [
[1, 3],
[2, 2],
[3, 1],
];
const result = matrixAutoCorrelation(matrix);
expect(result[0]).toBeCloseTo(1, 10);
expect(result[1]).toBeCloseTo(-1, 10);
});
test('simple', () => {
const matrix = [
[1, 3],
[2, 2],
[3, 1],
];
const result = matrixAutoCorrelation(matrix);
expect(result[0]).toBeCloseTo(1, 10);
expect(result[1]).toBeCloseTo(-1, 10);
});

it('test matrixAutoCorrelation too small', () => {
const matrix = [[0]];
expect(() => matrixAutoCorrelation(matrix)).toThrow(
'matrixAutoCorrelation: can not calculate info if matrix contains less than 2 rows',
);
});
test('test matrixAutoCorrelation too small', () => {
const matrix = [[0]];
expect(() => matrixAutoCorrelation(matrix)).toThrow(
'can not calculate info if matrix contains less than 2 rows',
);
});
112 changes: 55 additions & 57 deletions src/matrix/__tests__/matrixBoxPlot.test.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
import { matrixBoxPlot } from '../../index';
import { matrixBoxPlot } from '../matrixBoxPlot';

describe('matrixBoxPlot', () => {
it('test matrixBoxPlot even', () => {
const matrix = [
[0, 0],
[1, 10],
[2, 20],
[3, 30],
[4, 40],
[5, 50],
[6, 60],
[7, 70],
[8, 80],
[9, 90],
[10, 100],
[11, 110],
];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([2.5, 25]),
median: Float64Array.from([5.5, 55]),
q3: Float64Array.from([8.5, 85]),
min: Float64Array.from([0, 0]),
max: Float64Array.from([11, 110]),
});
test('test matrixBoxPlot even', () => {
const matrix = [
[0, 0],
[1, 10],
[2, 20],
[3, 30],
[4, 40],
[5, 50],
[6, 60],
[7, 70],
[8, 80],
[9, 90],
[10, 100],
[11, 110],
];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([2.5, 25]),
median: Float64Array.from([5.5, 55]),
q3: Float64Array.from([8.5, 85]),
min: Float64Array.from([0, 0]),
max: Float64Array.from([11, 110]),
});
});

it('test matrixBoxPlot even small', () => {
const matrix = [[0], [1], [2], [3], [4], [5]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([1]),
median: Float64Array.from([2.5]),
q3: Float64Array.from([4]),
min: Float64Array.from([0]),
max: Float64Array.from([5]),
});
test('test matrixBoxPlot even small', () => {
const matrix = [[0], [1], [2], [3], [4], [5]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([1]),
median: Float64Array.from([2.5]),
q3: Float64Array.from([4]),
min: Float64Array.from([0]),
max: Float64Array.from([5]),
});
});

it('test matrixBoxPlot odd', () => {
const matrix = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([2]),
median: Float64Array.from([5]),
q3: Float64Array.from([8]),
min: Float64Array.from([0]),
max: Float64Array.from([10]),
});
test('test matrixBoxPlot odd', () => {
const matrix = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([2]),
median: Float64Array.from([5]),
q3: Float64Array.from([8]),
min: Float64Array.from([0]),
max: Float64Array.from([10]),
});
});

it('test matrixBoxPlot odd small', () => {
const matrix = [[0], [1], [2], [3], [4]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([0.5]),
median: Float64Array.from([2]),
q3: Float64Array.from([3.5]),
min: Float64Array.from([0]),
max: Float64Array.from([4]),
});
test('test matrixBoxPlot odd small', () => {
const matrix = [[0], [1], [2], [3], [4]];
expect(matrixBoxPlot(matrix)).toStrictEqual({
q1: Float64Array.from([0.5]),
median: Float64Array.from([2]),
q3: Float64Array.from([3.5]),
min: Float64Array.from([0]),
max: Float64Array.from([4]),
});
});

it('test matrixBoxPlot too small', () => {
const matrix = [[0], [1], [2], [4]];
expect(() => matrixBoxPlot(matrix)).toThrow(
'matrixBoxPlot: can not calculate info if matrix contains less than 5 rows',
);
});
test('test matrixBoxPlot too small', () => {
const matrix = [[0], [1], [2], [4]];
expect(() => matrixBoxPlot(matrix)).toThrow(
'can not calculate info if matrix contains less than 5 rows',
);
});
13 changes: 5 additions & 8 deletions src/matrix/__tests__/matrixCenterZMean.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { matrixCenterZMean } from '../../index';
import { matrixCenterZMean } from '../matrixCenterZMean';

test('matrixCenterZMean', () => {
const data = [
[1, 3, 2, 2],
[2, 2, 1, 3],
[3, 1, 3, 1],
];
let result = matrixCenterZMean(data);

result = result.map((row) => Array.from(row));

const result = matrixCenterZMean(data);
expect(result).toStrictEqual([
[-1, 1, 0, 0],
[0, 0, -1, 1],
[1, -1, 1, -1],
Float64Array.from([-1, 1, 0, 0]),
Float64Array.from([0, 0, -1, 1]),
Float64Array.from([1, -1, 1, -1]),
]);
});
20 changes: 9 additions & 11 deletions src/matrix/__tests__/matrixCheck.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { matrixCheck } from '../../index';
import { matrixCheck } from '../matrixCheck';

describe('matrixCheck', () => {
it('should throw error', () => {
const wrongMatrix = [
[1, 2],
[3, 2, 3],
];
test('should throw error', () => {
const wrongMatrix = [
[1, 2],
[3, 2, 3],
];

expect(() => matrixCheck(wrongMatrix)).toThrow(
'All rows should has the same length',
);
});
expect(() => matrixCheck(wrongMatrix)).toThrow(
'all rows must has the same length',
);
});
30 changes: 6 additions & 24 deletions src/matrix/__tests__/matrixClone.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import { matrixClone } from '../../index';
import { matrixClone } from '../matrixClone';

describe('matrixClone', () => {
const dataset = [
[73, 'a', 'o', 152],
[93, 'b', 'u', 185],
[89, 'c', 'p', 180],
[96, 'd', 'v', 196],
[73, 'e', 'j', 142],
[53, 'f', 'w', 101],
[69, 'g', 'x', 149],
[47, 'h', 'y', 115],
[87, 'i', 'p', 175],
[79, 'j', 'b', 164],
[69, 'j', 'm', 141],
[70, 'k', 'g', 141],
[93, 'l', 'c', 184],
[79, 'a', 'm', 152],
[70, 'm', 'z', 148],
[93, 'n', 'aa', 192],
];
import { datasetForEncoding } from './fixtures/encoding';

it('should return an array of numbers', () => {
const matrix = matrixClone(dataset);
expect(matrix).toStrictEqual(dataset);
});
test('should return an array of numbers', () => {
const matrix = matrixClone(datasetForEncoding);
expect(matrix).toStrictEqual(datasetForEncoding);
expect(matrix).not.toBe(datasetForEncoding);
});
17 changes: 5 additions & 12 deletions src/matrix/__tests__/matrixColumnsCorrelation.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { toMatchCloseTo } from 'jest-matcher-deep-close-to';

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

expect.extend({ toMatchCloseTo });
import { matrixColumnsCorrelation } from '../matrixColumnsCorrelation';

test('matrixColumnsCorrelation', () => {
const data: number[][] = [
Expand All @@ -11,13 +7,10 @@ test('matrixColumnsCorrelation', () => {
[3, 1, 3, 1],
];
const result = matrixColumnsCorrelation(data);
for (let i = 0; i < result.length; i++) {
result[i] = Array.from(result[i]);
}
expect(result).toMatchCloseTo([
[1, -1, 1, -1],
[-1, 1, -1, 1],
[1, -1, 1, -1],
[-1, 1, -1, 1],
Float64Array.from([1, -1, 1, -1]),
Float64Array.from([-1, 1, -1, 1]),
Float64Array.from([1, -1, 1, -1]),
Float64Array.from([-1, 1, -1, 1]),
]);
});
Loading

0 comments on commit 1918fc8

Please sign in to comment.