-
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 matrix functions
- Loading branch information
Showing
94 changed files
with
778 additions
and
874 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,4 +1,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest/presets/js-with-ts', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['/node_modules/', '/fixtures/'], | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'], | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { toMatchCloseTo, toBeDeepCloseTo } from 'jest-matcher-deep-close-to'; | ||
|
||
expect.extend({ toMatchCloseTo, toBeDeepCloseTo }); |
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 |
---|---|---|
@@ -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
71
src/matrix/__tests__/matrixApplyNumericalDecoding.test.ts
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,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); | ||
}); |
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,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', | ||
); | ||
}); |
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,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', | ||
); | ||
}); |
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,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]), | ||
]); | ||
}); |
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,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', | ||
); | ||
}); |
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,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); | ||
}); |
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.