Skip to content

Commit

Permalink
fix: xy data with discontinuous
Browse files Browse the repository at this point in the history
 x

Related to cheminfo#2820
  • Loading branch information
andcastillo committed Jan 12, 2024
1 parent 12d03b8 commit 9005e29
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/component/hooks/useXYReduce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataXY } from 'cheminfo-types';
import { DataXY, FromTo } from 'cheminfo-types';
import { xyReduce } from 'ml-spectra-processing';
import { useCallback } from 'react';

Expand All @@ -23,9 +23,32 @@ export default function useXYReduce(
domainAxis === XYReducerDomainAxis.XAxis ? xDomain : yDomain;
return xyReduce(
{ x, y },
{ from, to, nbPoints: width * 4, optimize: true },
{ from, to, nbPoints: width * 4, optimize: true, zones: getZones(x)},
);
},
[domainAxis, width, xDomain, yDomain],
);
}

function getZones(x: Float64Array): FromTo[] {
const zones: FromTo[] = [];
let from = x[0];
const deltaX = x[1] - x[0];
const deltaTol = deltaX * 0.005; // Accept deltas having this discrepancy as equal
let i = 1;
for(; i < x.length; i++) {
if(Math.abs(x[i + 1] - x[i] - deltaX) > deltaTol) {
zones.push({from, to: x[i]})
from = x[i + 1];
i++;
}
}
zones.push({from, to: x[i - 1]});

if(zones.length >= x.length/10) {
// There are too many zones compared with the original x
return [];
}

return zones;
}

0 comments on commit 9005e29

Please sign in to comment.