Skip to content

Commit

Permalink
docs: improve jsDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Nov 23, 2022
1 parent 99285a8 commit b47d149
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { it, expect, test } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

test('We check that array of points are not converted and are normalized', () => {
const comparator = new Comparator();
Expand Down Expand Up @@ -101,7 +101,7 @@ test('We check that we can change the peaks', () => {
expect(comparator.getExtract2()).toStrictEqual([[2], [1]]);
});

test.only('We check similarity of identical spectra', () => {
test('We check similarity of identical spectra', () => {
const comparator = new Comparator();
comparator.setTrapezoid(0.2, 0.2);
comparator.setPeaks1([
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/common.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

describe('We check common array similarity with common first', () => {
const comparator = new Comparator({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/emptyArray.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

describe('We check that array of points are not converted and are not normalized', () => {
const comparator = new Comparator({});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/fastSimilarity.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

test('We check to test fast similarity common second', () => {
const comparator = new Comparator({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/partialCommon.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

describe('We check common array similarity 1', () => {
const comparator = new Comparator({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/self-similar.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

test('similarity with itself should be 1', () => {
const comparator = new Comparator();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/trapezoid.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';

import Comparator from '..';
import { Comparator } from '..';

describe('We check similarity of identical spectra trapezoid', () => {
const comparator6 = new Comparator({ trapezoid: true });
Expand Down
22 changes: 0 additions & 22 deletions src/checkArray.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/checkPeaks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* This code requires the use of an array like [[x1,y1],[x2,y2], ...]
* If it is not the right format, we will just convert it
* Otherwise we return the correct format
* @param {Peaks} peaks
* @returns [number[], number[]]
*/
export function checkPeaks(peaks) {
// if it is already a 2D array of points, we just return them
if (Array.isArray(peaks) && Array.isArray(peaks[0]) && peaks.length === 2) {
return peaks;
}
const x = new Array(peaks.length);
const y = new Array(peaks.length);
for (let i = 0; i < peaks.length; i++) {
x[i] = peaks[i][0];
y[i] = peaks[i][1];
}
return [x, y];
}
93 changes: 73 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// should be a binary operation !

import { calculateOverlapFromDiff } from './calculateOverlapFromDiff';
import { checkArray } from './checkArray';
import { checkPeaks } from './checkPeaks';
import { commonExtractAndNormalize } from './commonExtractAndNormalize';
import { extract } from './extract';
import { extractAndNormalize } from './extractAndNormalize';
import { getCommonArray } from './getCommonArray.js';
import { getIntersection } from './getIntersection';
import { normalize } from './normalize';

const COMMON_NO = 0;
export const COMMON_NO = 0;
export const COMMON_FIRST = 1;
export const COMMON_SECOND = 2;
const COMMON_BOTH = 3;
export const COMMON_BOTH = 3;

/**
* A number, or a string containing a number.
* @typedef {([number[],number[]]|[number,number][])} Peaks
*/

/**
* Create a comparator class
Expand All @@ -23,7 +26,6 @@ const COMMON_BOTH = 3;
* {number} [options.from] from region used for similarity calculation
* {number} [options.to] to region used for similarity calculation
*/

export class Comparator {
constructor(options = {}) {
this.array1 = [];
Expand Down Expand Up @@ -66,8 +68,12 @@ export class Comparator {
this.setFromTo(from, to);
}

setPeaks1(anArray) {
this.array1 = checkArray(anArray);
/**
*
* @param {Peaks} peaks
*/
setPeaks1(peaks) {
this.array1 = checkPeaks(peaks);

if (this.common) {
const extracts = commonExtractAndNormalize(
Expand All @@ -89,8 +95,12 @@ export class Comparator {
}
}

setPeaks2(anArray) {
this.array2 = checkArray(anArray);
/**
*
* @param {Peaks} peaks
*/
setPeaks2(peaks) {
this.array2 = checkPeaks(peaks);
if (this.common) {
const extracts = commonExtractAndNormalize(
this.array1,
Expand Down Expand Up @@ -127,6 +137,11 @@ export class Comparator {
return this.array2ExtractInfo;
}

/**
* Set the new bottom and top width of the trapezoid
* @param {number} newWidthBottom
* @param {number} newWidthTop
*/
setTrapezoid(newWidthBottom, newWidthTop) {
this.widthTop = newWidthTop;
this.widthBottom = newWidthBottom;
Expand All @@ -136,6 +151,12 @@ export class Comparator {
}
}

/**
* Set the from / to for comparison
* @param {number} newFrom - set the new from value
* @param {number} newTo - set the new to value
* @returns
*/
setFromTo(newFrom, newTo) {
if (newFrom === this.from && newTo === this.to) return;
this.from = newFrom;
Expand Down Expand Up @@ -164,6 +185,14 @@ export class Comparator {
}
}

/**
*
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
* @returns
*/
getOverlap(x1, y1, x2, y2) {
if (y1 === 0 || y2 === 0) return 0;

Expand All @@ -181,8 +210,19 @@ export class Comparator {
return Math.min(y1, y2, maxValue);
}

// This is the old trapezoid similarity
/**
* This is the old trapezoid similarity
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
* @param {number} widthTop
* @param {number} widthBottom
* @returns
*/
getOverlapTrapezoid(x1, y1, x2, y2, widthTop, widthBottom) {
// eslint-disable-next-line no-console
console.error('getOverlapTrapezoid should not be used anymore');
const factor = 2 / (widthTop + widthBottom); // correction for surface=1
if (y1 === 0 || y2 === 0) return 0;
if (x1 === x2) {
Expand Down Expand Up @@ -264,17 +304,20 @@ export class Comparator {
return NaN;
}

// this method calculates the total diff. The sum of positive value will yield to overlap
/**
* This method calculates the total diff. The sum of positive value will yield to overlap
* @returns
*/
calculateDiff() {
// we need to take 2 pointers
// and travel progressively between them ...
const newFirst = [
[].concat(this.array1Extract[0]),
[].concat(this.array1Extract[1]),
this.array1Extract[0].slice(),
this.array1Extract[1].slice(),
];
const newSecond = [
[].concat(this.array2Extract[0]),
[].concat(this.array2Extract[1]),
this.array2Extract[0].slice(),
this.array2Extract[1].slice(),
];
const array1Length = this.array1Extract[0]
? this.array1Extract[0].length
Expand Down Expand Up @@ -330,6 +373,12 @@ export class Comparator {
return newSecond;
}

/**
* Set the new peaks and return info
* @param {Peaks} newPeaks1
* @param {Peaks} newPeaks2
* @returns
*/
getSimilarity(newPeaks1, newPeaks2) {
if (newPeaks1) this.setPeaks1(newPeaks1);
if (newPeaks2) this.setPeaks2(newPeaks2);
Expand All @@ -346,10 +395,14 @@ export class Comparator {
};
}

/*
This works mainly when you have a array1 that is fixed
newPeaks2 have to be normalized ! (sum to 1)
*/
/**
* This works mainly when you have a array1 that is fixed
* newPeaks2 have to be normalized ! (sum to 1)
* @param {Peaks} newPeaks2
* @param {number} from
* @param {number} to
* @returns
*/
fastSimilarity(newPeaks2, from, to) {
this.array1Extract = extract(this.array1, from, to);
this.array2Extract = newPeaks2;
Expand Down

0 comments on commit b47d149

Please sign in to comment.