Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixAutoCorrelation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('simple', () => {
test('matrixAutoCorrelation too small', () => {
const matrix = [[0]];

expect(() => matrixAutoCorrelation(matrix)).toThrow(
expect(() => matrixAutoCorrelation(matrix)).toThrowError(
'can not calculate info if matrix contains less than 2 rows',
);
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixBoxPlot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test('matrixBoxPlot odd small', () => {
test('matrixBoxPlot too small', () => {
const matrix = [[0], [1], [2], [4]];

expect(() => matrixBoxPlot(matrix)).toThrow(
expect(() => matrixBoxPlot(matrix)).toThrowError(
'can not calculate info if matrix contains less than 5 rows',
);
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('should throw error', () => {
[3, 2, 3],
];

expect(() => matrixCheck(wrongMatrix)).toThrow(
expect(() => matrixCheck(wrongMatrix)).toThrowError(
'all rows must has the same length',
);
});
4 changes: 2 additions & 2 deletions src/matrix/__tests__/matrixCheckRanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('should not throw error for valid indices', () => {
endColumn: 2,
};

expect(() => matrixCheckRanges(matrix, options)).not.toThrow();
expect(() => matrixCheckRanges(matrix, options)).not.toThrowError();
});

test('should throw RangeError for out-of-range indices', () => {
Expand All @@ -38,5 +38,5 @@ test('should throw RangeError for out-of-range indices', () => {
};

// Call the function and expect test to throw RangeError
expect(() => matrixCheckRanges(matrix, options)).toThrow(RangeError);
expect(() => matrixCheckRanges(matrix, options)).toThrowError(RangeError);
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixGetSubMatrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ test('should throw RangeError for out-of-range indices', () => {
duplicate: true,
};

expect(() => matrixGetSubMatrix(matrix, options)).toThrow(RangeError);
expect(() => matrixGetSubMatrix(matrix, options)).toThrowError(RangeError);
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixMaxAbsoluteZ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ test('large negative', () => {
test('zero', () => {
expect(() => {
matrixMaxAbsoluteZ([[]]);
}).toThrow('matrix must have at least 1 row and 1 column');
}).toThrowError('matrix must have at least 1 row and 1 column');
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixMinMaxAbsoluteZ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ test('zero', () => {

expect(() => {
matrixMinMaxAbsoluteZ(matrix);
}).toThrow('matrixMinMaxAbsoluteZ requires at least 1 row and 1 column');
}).toThrowError('matrixMinMaxAbsoluteZ requires at least 1 row and 1 column');
});
2 changes: 1 addition & 1 deletion src/matrix/__tests__/matrixMinMaxZ.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ test('basic', () => {
test('zero', () => {
expect(() => {
matrixMinMaxZ([[]]);
}).toThrow('matrix must contain data');
}).toThrowError('matrix must contain data');
});
4 changes: 2 additions & 2 deletions src/matrix/__tests__/matrixSetSubMatrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ test('simple case', () => {
[3, 4, 5, 0, 0],
]);

expect(() => matrixSetSubMatrix(matrix, subMatrix, 2, 4)).toThrow(
expect(() => matrixSetSubMatrix(matrix, subMatrix, 2, 4)).toThrowError(
'submatrix indices are out of range',
);

expect(() =>
matrixSetSubMatrix(matrix, subMatrix.concat([0, 0]), 2, 3),
).toThrow('submatrix indices are out of range');
).toThrowError('submatrix indices are out of range');
});
24 changes: 22 additions & 2 deletions src/reim/__tests__/reimZeroFilling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ test('xreimZeroFilling over', () => {
re: [0, 1, 2, 3, 0, 0],
im: [4, 5, 6, 7, 0, 0],
});

expect(result.re).toBeInstanceOf(Array);
expect(result.im).toBeInstanceOf(Array);
});

test('xreimZeroFilling over with Float64', () => {
const re = new Float64Array([0, 1, 2, 3]);
const im = new Float64Array([4, 5, 6, 7]);
const result = reimZeroFilling({ re, im }, 6);

const newRe = Array.from(result.re);
const newIm = Array.from(result.im);

expect({ re: newRe, im: newIm }).toStrictEqual({
re: [0, 1, 2, 3, 0, 0],
im: [4, 5, 6, 7, 0, 0],
});

expect(result.re).toBeInstanceOf(Float64Array);
expect(result.im).toBeInstanceOf(Float64Array);
});

test('xreimZeroFilling equal', () => {
Expand All @@ -30,8 +50,8 @@ test('xreimZeroFilling equal', () => {
});

test('xreimZeroFilling under', () => {
const re = [0, 1, 2, 3];
const im = [4, 5, 6, 7];
const re = new Float64Array([0, 1, 2, 3]);
const im = new Float64Array([4, 5, 6, 7]);
const result = reimZeroFilling({ re, im }, 2);
const newRe = Array.from(result.re);
const newIm = Array.from(result.im);
Expand Down
33 changes: 24 additions & 9 deletions src/reim/reimZeroFilling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { DoubleArray } from 'cheminfo-types';

import type { DataReIm } from '../types/index.ts';
import type { DoubleArrayConstructor } from '../utils/createArray.ts';
import { createDoubleArray } from '../utils/createArray.ts';

/**
* This function make a zero filling to re and im part.
Expand All @@ -8,7 +12,10 @@ import type { DataReIm } from '../types/index.ts';
* truncated arrays if totalLength is smaller current length or
* the same input if totalLength is equal that current length
*/
export function reimZeroFilling(data: DataReIm, totalLength: number): DataReIm {
export function reimZeroFilling<ArrayType extends DoubleArray>(
data: DataReIm<ArrayType>,
totalLength: number,
): DataReIm<ArrayType> {
if (!Number.isInteger(totalLength) || totalLength < 0) {
throw new RangeError('totalLength must be a non-negative integer');
}
Expand All @@ -20,19 +27,27 @@ export function reimZeroFilling(data: DataReIm, totalLength: number): DataReIm {

if (length > totalLength) {
return {
re: re.slice(0, totalLength),
im: im.slice(0, totalLength),
re: re.slice(0, totalLength) as ArrayType,
im: im.slice(0, totalLength) as ArrayType,
};
}

const newRE = new Float64Array(totalLength);
const newIM = new Float64Array(totalLength);
const newRE = createDoubleArray(
re.constructor as DoubleArrayConstructor,
totalLength,
);
const newIM = createDoubleArray(
im.constructor as DoubleArrayConstructor,
totalLength,
);

newRE.set(re);
newIM.set(im);
for (let i = 0; i < re.length; i++) {
newRE[i] = re[i];
newIM[i] = im[i];
}

return {
re: newRE,
im: newIM,
re: newRE as ArrayType,
im: newIM as ArrayType,
};
}
2 changes: 1 addition & 1 deletion src/utils/__tests__/createFromToArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@ test('case when we choose a distribution other than uniform or log', () => {
// @ts-expect-error Testing bad argument.
distribution: 'other',
});
}).toThrow(Error);
}).toThrowError(Error);
});
2 changes: 1 addition & 1 deletion src/utils/__tests__/getRescaler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('getRescale with wrong min / max, no clamp', () => {
clamp: false,
});

expect(() => rescaler(2)).toThrow('Value 2 is out of range [0, 1]');
expect(() => rescaler(2)).toThrowError('Value 2 is out of range [0, 1]');
});

test('getRescale with wrong min / max and logartihmic', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/calculateAdaptiveWeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export interface WeightsAndControlPoints {
weights?: NumberArray;
}

export interface CalculateAdaptiveWeightsOptions
extends WeightsAndControlPoints {
export interface CalculateAdaptiveWeightsOptions extends WeightsAndControlPoints {
/**
* Factor that determines how quickly the weights are updated in each iteration.
* A value between 0 and 1, where higher values mean faster updates.
Expand Down
2 changes: 1 addition & 1 deletion src/x/__tests__/xBoxPlot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('xBoxPlot odd small', () => {
test('xBoxPlot too small', () => {
const array: number[] = [];

expect(() => xBoxPlot(array)).toThrow('input must not be empty');
expect(() => xBoxPlot(array)).toThrowError('input must not be empty');
});

test('xBoxPlot with one element', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/x/__tests__/xCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { expect, test } from 'vitest';
import { xCheck } from '../xCheck.ts';

test('should throw on invalid value', () => {
expect(() => xCheck()).toThrow(/input must be an array/);
expect(() => xCheck([])).toThrow(/input must not be empty/);
expect(() => xCheck([1])).not.toThrow();
expect(() => xCheck([1], { minLength: 2 })).toThrow(
expect(() => xCheck()).toThrowError(/input must be an array/);
expect(() => xCheck([])).toThrowError(/input must not be empty/);
expect(() => xCheck([1])).not.toThrowError();
expect(() => xCheck([1], { minLength: 2 })).toThrowError(
/input must have a length of at least 2/,
);
});
4 changes: 3 additions & 1 deletion src/x/__tests__/xDistributionStats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { xDistributionStats } from '../xDistributionStats.ts';
test('empty array', () => {
const data: number[] = [];

expect(() => xDistributionStats(data)).toThrow('input must not be empty');
expect(() => xDistributionStats(data)).toThrowError(
'input must not be empty',
);
});

test('one element', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/x/__tests__/xMeanAbsoluteError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ test('different length', () => {

expect(() => {
return xMeanAbsoluteError(array1, array2);
}).toThrow(/length of array1 and array2 must be identical/);
}).toThrowError(/length of array1 and array2 must be identical/);
});
2 changes: 1 addition & 1 deletion src/x/__tests__/xMeanSquaredError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ test('different length', () => {

expect(() => {
return xMeanSquaredError(array1, array2);
}).toThrow(/length of array1 and array2 must be identical/);
}).toThrowError(/length of array1 and array2 must be identical/);
});
4 changes: 2 additions & 2 deletions src/x/__tests__/xMeanWeighted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ test('xMeanWeighted', () => {
xMeanWeighted([3, 2, 1], [1, 1, 1], { fromIndex: 1, toIndex: 1 }),
).toBe(2);
// expect to throw an error if the length of the arrays are not the same
expect(() => xMeanWeighted([1, 2, 3], [1, 1])).toThrow(
expect(() => xMeanWeighted([1, 2, 3], [1, 1])).toThrowError(
'array and weights must have the same length',
);
expect(() => xMeanWeighted([1, 2, 3], [0, 0, 0])).toThrow(
expect(() => xMeanWeighted([1, 2, 3], [0, 0, 0])).toThrowError(
'sum of weights must be > 0',
);
});
2 changes: 1 addition & 1 deletion src/x/__tests__/xMedian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ test('should return the median with typed array', () => {
});

test('should throw on invalid value', () => {
expect(() => xMedian([])).toThrow(/input must not be empty/);
expect(() => xMedian([])).toThrowError(/input must not be empty/);
});
2 changes: 1 addition & 1 deletion src/x/__tests__/xMode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ test('should return the mode', () => {
});

test('should throw on invalid value', () => {
expect(() => xMode([])).toThrow(/input must not be empty/);
expect(() => xMode([])).toThrowError(/input must not be empty/);
});
30 changes: 15 additions & 15 deletions src/x/__tests__/xNormed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import { xNormed } from '../xNormed.ts';
test('should return the norm', () => {
expect(() => {
xNormed([0]);
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0]);
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(xNormed([-1, 1])).toStrictEqual(Float64Array.from([-0.5, 0.5]));
expect(xNormed([1, 3])).toStrictEqual(Float64Array.from([0.25, 0.75]));
});

test('should return the norm algorithm=absolute', () => {
expect(() => {
xNormed([0], { algorithm: 'absolute' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0], { algorithm: 'absolute' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(xNormed([-1, 1], { algorithm: 'absolute' })).toStrictEqual(
Float64Array.from([-0.5, 0.5]),
);
Expand All @@ -31,10 +31,10 @@ test('should return the norm algorithm=absolute', () => {
test('should return the norm algorithm=max', () => {
expect(() => {
xNormed([0], { algorithm: 'max' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0], { algorithm: 'max' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(xNormed([-1, 1], { algorithm: 'max' })).toStrictEqual(
Float64Array.from([-1, 1]),
);
Expand All @@ -46,10 +46,10 @@ test('should return the norm algorithm=max', () => {
test('should return the norm algorithm=max and max to 100', () => {
expect(() => {
xNormed([0], { algorithm: 'max', value: 100 });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0], { algorithm: 'max', value: 100 });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');

expect(xNormed([-1, 1], { algorithm: 'max', value: 100 })).toStrictEqual(
Float64Array.from([-100, 100]),
Expand All @@ -62,13 +62,13 @@ test('should return the norm algorithm=max and max to 100', () => {
test('should return the norm algorithm=sum', () => {
expect(() => {
xNormed([0], { algorithm: 'sum' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0], { algorithm: 'sum' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([-1, 1], { algorithm: 'sum' });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(xNormed([-1, 3], { algorithm: 'sum' })).toStrictEqual(
Float64Array.from([-0.5, 1.5]),
);
Expand All @@ -80,13 +80,13 @@ test('should return the norm algorithm=sum', () => {
test('should return the norm algorithm=sum sumValue=5', () => {
expect(() => {
xNormed([0], { algorithm: 'sum', value: 5 });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([0, 0], { algorithm: 'sum', value: 5 });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(() => {
xNormed([-1, 1], { algorithm: 'sum', value: 5 });
}).toThrow('trying to divide by 0');
}).toThrowError('trying to divide by 0');
expect(xNormed([-1, 3], { algorithm: 'sum', value: 5 })).toStrictEqual(
Float64Array.from([-2.5, 7.5]),
);
Expand All @@ -96,5 +96,5 @@ test('should return the norm algorithm=sum sumValue=5', () => {
});

test('should throw on invalid value', () => {
expect(() => xNormed([])).toThrow(/input must not be empty/);
expect(() => xNormed([])).toThrowError(/input must not be empty/);
});
Loading
Loading