From 566bafdb2764ac8f63bc97907bc54a23fe286be0 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Sat, 15 Jun 2024 07:26:35 +0200 Subject: [PATCH 1/3] test: add non x linear tests for xyReduce (#247) --- src/xy/__tests__/xyReduce.test.ts | 23 +++++++++++++++++++++++ src/xy/xyReduce.ts | 6 ++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/xy/__tests__/xyReduce.test.ts b/src/xy/__tests__/xyReduce.test.ts index e75be2e7..78d29fbd 100644 --- a/src/xy/__tests__/xyReduce.test.ts +++ b/src/xy/__tests__/xyReduce.test.ts @@ -115,6 +115,29 @@ test('xyCheck optimization', () => { expect(result.y).toStrictEqual([0, 5, 10]); }); +test('xyCheck non-linear x', () => { + const xs = []; + const ys = []; + for (let i = 0; i < 11; i++) { + xs.push(i * 1.2 ** i); + ys.push(i); + } + const result = xyReduce({ x: xs, y: ys }, { nbPoints: 5 }); + expect(result.y).toStrictEqual([0, 1, 8, 9, 10]); +}); + +test('xyCheck extreme non-linear x', () => { + const xs = []; + const ys = []; + for (let i = 0; i < 11; i++) { + xs.push(i * 2 ** i); + ys.push(i); + } + //console.log(xs, ys); + const result = xyReduce({ x: xs, y: ys }, { nbPoints: 5 }); + expect(result.y).toStrictEqual([0, 1, 10]); +}); + test('Part rounded far 2 with optimization', () => { const result = xyReduce({ x, y }, { nbPoints: 5, optimize: true }); diff --git a/src/xy/xyReduce.ts b/src/xy/xyReduce.ts index 11012430..f58ceb46 100644 --- a/src/xy/xyReduce.ts +++ b/src/xy/xyReduce.ts @@ -29,7 +29,7 @@ export interface XYReduceOptions { * */ nbPoints?: number; /** - * If optimize we may have less than nbPoints at the end + * If optimize we may have less than nbPoints at the end. It should not have visible effects * @default false * */ optimize?: boolean; @@ -42,7 +42,9 @@ export interface XYReduceOptions { /** * Reduce the number of points while keeping visually the same noise. Practical to - * display many spectra as SVG. If you want a similar looking spectrum you should still however generate 4x the nbPoints that is being displayed. + * display many spectra as SVG. If you want a similar looking spectrum you should still however + * generate at least 4x the nbPoints that is being displayed. + * * SHOULD NOT BE USED FOR DATA PROCESSING !!! * You should rather use ml-xy-equally-spaced to make further processing * From af7ca47921ef34c936cc970db625079a25abf6a7 Mon Sep 17 00:00:00 2001 From: Jose Alejandro Bolanos Arroyave Date: Thu, 20 Jun 2024 00:02:17 -0500 Subject: [PATCH 2/3] fix(xySortX): avoid NaN results when X is a typedArray (#249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: add failing test case * fix: avoid NaN results when X is a typedArray * use callback in Array.from Co-authored-by: Michaël Zasso * chore: move test case to __tests__ folder --------- Co-authored-by: Michaël Zasso --- src/xy/__tests__/xySortX.test.ts | 16 ++++++++++++++++ src/xy/utils/{ => __tests__}/integral.test.ts | 2 +- src/xy/xySortX.ts | 10 ++++------ 3 files changed, 21 insertions(+), 7 deletions(-) rename src/xy/utils/{ => __tests__}/integral.test.ts (92%) diff --git a/src/xy/__tests__/xySortX.test.ts b/src/xy/__tests__/xySortX.test.ts index 9ca42280..5652d9a4 100644 --- a/src/xy/__tests__/xySortX.test.ts +++ b/src/xy/__tests__/xySortX.test.ts @@ -43,3 +43,19 @@ test('sorted reverse', () => { y: Float64Array.from([3, 2, 1]), }); }); + +test('typed XY arrays', () => { + const data = { + x: Float64Array.from([ + 1.557, 1.265, 1.535, 1.622, 2, 1.426, 1.094, 1.201, 1.825, + ]), + y: Float64Array.from([0, 1, 2, 5, 6, 7, 8, 9, 10]), + }; + const result = xySortX(data); + expect(result).toStrictEqual({ + x: Float64Array.from([ + 1.094, 1.201, 1.265, 1.426, 1.535, 1.557, 1.622, 1.825, 2, + ]), + y: Float64Array.from([8, 9, 1, 7, 2, 0, 5, 10, 6]), + }); +}); diff --git a/src/xy/utils/integral.test.ts b/src/xy/utils/__tests__/integral.test.ts similarity index 92% rename from src/xy/utils/integral.test.ts rename to src/xy/utils/__tests__/integral.test.ts index 0834a0d4..4bac56da 100644 --- a/src/xy/utils/integral.test.ts +++ b/src/xy/utils/__tests__/integral.test.ts @@ -1,6 +1,6 @@ import { expect, test } from 'vitest'; -import integral from './integral'; +import integral from '../integral'; test('should compute expect(integral with a,b, x0 and x1', () => { expect(integral(0, 1, 1, 0)).toBe(0.5); diff --git a/src/xy/xySortX.ts b/src/xy/xySortX.ts index e240dfd0..1766a425 100644 --- a/src/xy/xySortX.ts +++ b/src/xy/xySortX.ts @@ -24,12 +24,10 @@ export function xySortX(data: DataXY): DataXY { } } - const xyObject = (x as number[]) - .map((val, index) => ({ - x: val, - y: y[index], - })) - .sort((a, b) => a.x - b.x); + const xyObject = Array.from(x, (val, index) => ({ + x: val, + y: y[index], + })).sort((a, b) => a.x - b.x); const response = { x: new Float64Array(x.length), From 39336cdd2db457c2aa41bd5407f594fba94be849 Mon Sep 17 00:00:00 2001 From: mljs-bot <72700744+mljs-bot@users.noreply.github.com> Date: Thu, 20 Jun 2024 07:04:27 +0200 Subject: [PATCH 3/3] chore(main): release 14.5.1 (#250) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af72cbb9..cfc01b07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [14.5.1](https://github.com/mljs/spectra-processing/compare/v14.5.0...v14.5.1) (2024-06-20) + + +### Bug Fixes + +* **xySortX:** avoid NaN results when X is a typedArray ([#249](https://github.com/mljs/spectra-processing/issues/249)) ([af7ca47](https://github.com/mljs/spectra-processing/commit/af7ca47921ef34c936cc970db625079a25abf6a7)) + ## [14.5.0](https://github.com/mljs/spectra-processing/compare/v14.4.0...v14.5.0) (2024-05-02) diff --git a/package.json b/package.json index 88a08775..cc362791 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ml-spectra-processing", - "version": "14.5.0", + "version": "14.5.1", "description": "Various method to process spectra", "main": "./lib/index.js", "module": "./lib-esm/index.js",