@@ -2,70 +2,77 @@ import type { DataXY } from 'cheminfo-types';
22import type { Shape1D } from 'ml-peak-shape-generator' ;
33import SG from 'ml-savitzky-golay-generalized' ;
44import { optimize } from 'ml-spectra-fitting' ;
5+ import type { IOptimizationOptions } from 'ml-spectra-fitting' ;
56import { xFindClosestIndex } from 'ml-spectra-processing' ;
67
78import type { Peak1D } from '../gsd' ;
89
910/**
1011 * This function try to join the peaks that seems to belong to a broad signal in a single broad peak.
11- * @param {Array } peaks - A list of initial parameters to be optimized. e.g. coming from a peak picking [{x, y, width}].
12- * @param {object } [options = {}] - options
13- * @param {number } [options.width=0.25] - width limit to join peaks.
14- * @param {object } [options.shape={}] - it's specify the kind of shape used to fitting.
15- * @param {string } [options.shape.kind = 'gaussian'] - kind of shape; lorentzian, gaussian and pseudovoigt are supported.
16- * @param {object } [options.optimization = {}] - it's specify the kind and options of the algorithm use to optimize parameters.
17- * @param {string } [options.optimization.kind = 'lm'] - kind of algorithm. By default it's levenberg-marquardt.
18- * @param {number } [options.optimization.options.timeout = 10] - maximum time running before break in seconds.
19- * @param {object } [options.optimization.options = {}] - options for the specific kind of algorithm.
12+ * @param peaks - A list of initial parameters to be optimized. e.g. coming from a peak picking [{x, y, width}].
2013 */
2114
2215interface GetSoftMaskOptions {
2316 sgOptions : {
2417 windowSize : number ;
2518 polynomial : number ;
2619 } ;
20+ /**
21+ * broadRatio
22+ * @default 0.0025
23+ */
2724 broadRatio : number ;
2825}
2926
30- interface OptionsType extends Partial < GetSoftMaskOptions > {
31- width ?: number ;
27+ export interface IJoinBroadPeaksOptions extends Partial < GetSoftMaskOptions > {
28+ /**
29+ * width limit to join peaks.
30+ * @default 0.25
31+ */
32+ broadWidth ?: number ;
33+ /**
34+ * it's specify the kind of shape used to fitting.
35+ */
3236 shape ?: Shape1D ;
33- optimization ?: { kind : string ; timeout : number } ;
34- mask ?: boolean [ ] ;
37+ /**
38+ * it's specify the kind and options of the algorithm use to optimize parameters.
39+ */
40+ optimization ?: IOptimizationOptions ;
41+ broadMask ?: boolean [ ] ;
3542}
3643
3744export function joinBroadPeaks (
3845 data : DataXY ,
3946 peakList : Peak1D [ ] ,
40- options : OptionsType = { } ,
47+ options : IJoinBroadPeaksOptions = { } ,
4148) : Peak1D [ ] {
4249 let {
43- mask ,
50+ broadMask ,
4451 shape = { kind : 'gaussian' } ,
45- optimization = { kind : 'lm' , timeout : 10 } ,
52+ optimization = { kind : 'lm' , options : { timeout : 10 } } ,
4653 sgOptions = {
4754 windowSize : 9 ,
4855 polynomial : 3 ,
4956 } ,
5057 broadRatio = 0.0025 ,
58+ broadWidth = 0.25 ,
5159 } = options ;
52- let { width = 0.25 } = options ;
5360
5461 let max = 0 ;
5562 let maxI = 0 ;
5663 let count = 1 ;
5764 const broadLines : Peak1D [ ] = [ ] ;
5865 const peaks : Peak1D [ ] = JSON . parse ( JSON . stringify ( peakList ) ) ;
59- const broadMask = ! mask
66+ const mask = ! broadMask
6067 ? getSoftMask ( data , peaks , { sgOptions, broadRatio } )
61- : mask ;
68+ : broadMask ;
6269
63- if ( broadMask . length !== peaks . length ) {
70+ if ( mask . length !== peaks . length ) {
6471 throw new Error ( 'mask length does not match the length of peaksList' ) ;
6572 }
6673
6774 for ( let i : number = peaks . length - 1 ; i >= 0 ; i -- ) {
68- if ( broadMask [ i ] ) {
75+ if ( mask [ i ] ) {
6976 broadLines . push ( peaks . splice ( i , 1 ) [ 0 ] ) ;
7077 }
7178 }
@@ -79,7 +86,7 @@ export function joinBroadPeaks(
7986 } ;
8087 let indexes : number [ ] = [ 0 ] ;
8188 for ( let i = 1 ; i < broadLines . length ; i ++ ) {
82- if ( Math . abs ( broadLines [ i - 1 ] . x - broadLines [ i ] . x ) < width ) {
89+ if ( Math . abs ( broadLines [ i - 1 ] . x - broadLines [ i ] . x ) < broadWidth ) {
8390 candidates . x . push ( broadLines [ i ] . x ) ;
8491 candidates . y . push ( broadLines [ i ] . y ) ;
8592 if ( broadLines [ i ] . y > max ) {
0 commit comments