Skip to content

Commit

Permalink
fix(contours): use xSanplot instead of absMedian (#1790)
Browse files Browse the repository at this point in the history
* fix(contours): use xSanplot instead of absMedian

* chore: move calculateSanPlot to data folder
  • Loading branch information
jobo322 committed Oct 2, 2022
1 parent a90d0e9 commit e8b16c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
@@ -1,19 +1,16 @@
import { xNoiseSanPlot, xyReduce } from 'ml-spectra-processing';
import { xyReduce } from 'ml-spectra-processing';

import { Data1D } from '../../../../../data/types/data1d';
import { Data2D } from '../../../../../data/types/data2d';

import { calculateSanPlot } from '../../../../../data/utilities/calculateSanPlot';

export function processSnapPlot<T extends '1D' | '2D'>(
dimension: T,
data: T extends '1D' ? Data1D : Data2D,
yLogBase: number,
) {
const input =
dimension === '1D'
? prepare1DData(data as Data1D)
: prepare2DData(data as Data2D);

const sanResult = xNoiseSanPlot(input);
const sanResult = calculateSanPlot(dimension, data);
const sanPlot: any = {};
const lines: any = {};
for (let plotKey in sanResult.sanplot) {
Expand All @@ -28,29 +25,6 @@ export function processSnapPlot<T extends '1D' | '2D'>(
return { sanPlot, lines };
}

function prepare1DData(data: Data1D) {
const length = data.re.length;
const jump = Math.floor(length / 307200) || 1;
const array = new Float64Array((length / jump) >> 0);
let index = 0;
for (let i = 0; i < array.length; i += jump) {
array[index++] = data.re[i];
}
return array;
}

function prepare2DData(data: Data2D) {
let cols = data.z[0].length;
let rows = data.z.length;
let jump = Math.floor((cols * rows) / 204800) || 1;
const array = new Float64Array(((cols * rows) / jump) >> 0);
let index = 0;
for (let i = 0; i < array.length; i += jump) {
array[index++] = data.z[(i / rows) >> 0][i % rows];
}
return array;
}

function getLine(value, data, options) {
const { log10, abs } = Math;
const { yLogBase } = options;
Expand Down
7 changes: 5 additions & 2 deletions src/data/data2d/Processing2D.ts
@@ -1,5 +1,7 @@
import { Conrec } from 'ml-conrec';
import { matrixAbsoluteMedian } from 'ml-spectra-processing';

import { Data2D } from '../types/data2d';
import { calculateSanPlot } from '../utilities/calculateSanPlot';

export const defaultContourOptions = {
positive: {
Expand Down Expand Up @@ -41,7 +43,8 @@ export default class Processing2D {

this.conrec = new Conrec(minMax.z, { xs, ys, swapAxes: false });

this.median = matrixAbsoluteMedian(minMax.z);
const sanResult = calculateSanPlot('2D', minMax as Data2D);
this.median = sanResult.positive;

this.minMax = minMax;
}
Expand Down
43 changes: 43 additions & 0 deletions src/data/utilities/calculateSanPlot.ts
@@ -0,0 +1,43 @@
import { xNoiseSanPlot } from 'ml-spectra-processing';

import { Data1D } from '../types/data1d';
import { Data2D } from '../types/data2d';

export function calculateSanPlot<T extends '1D' | '2D'>(
dimension: T,
data: T extends '1D' ? Data1D : Data2D,
) {
const input =
dimension === '1D'
? prepare1DData(data as Data1D)
: prepare2DData(data as Data2D);

return xNoiseSanPlot(input);
}

function prepare1DData(data: Data1D) {
const length = data.re.length;
const jump = Math.floor(length / 307200) || 1;
const array = new Float64Array((length / jump) >> 0);
let index = 0;
for (let i = 0; i < array.length; i += jump) {
array[index++] = data.re[i];
}
return array;
}

function prepare2DData(data: Data2D) {
let cols = data.z[0].length;
let rows = data.z.length;
let jump = Math.floor((cols * rows) / 204800) || 1;
const array = new Float64Array(((cols * rows) / jump) >> 0);
let index = 0;
// console.log('jump', jump, cols * rows);
for (let r = 0; r < rows; r += 1) {
for (let c = 0; c < cols; c += jump) {
array[index++] = data.z[r][c];
}
}

return array;
}

0 comments on commit e8b16c7

Please sign in to comment.