@@ -7,8 +7,10 @@ import { GSDPeakOptimized } from '../GSDPeakOptimized';
77import { addMissingIDs } from '../utils/addMissingIDs' ;
88import { addMissingShape } from '../utils/addMissingShape' ;
99
10- import { optimizePeaks } from './optimizePeaks' ;
11- import { GSDPeakOptimizedID } from './optimizePeaksWithLogs' ;
10+ import {
11+ GSDPeakOptimizedID ,
12+ optimizePeaksWithLogs ,
13+ } from './optimizePeaksWithLogs' ;
1214
1315export interface JoinBroadPeaksOptions {
1416 /**
@@ -75,7 +77,6 @@ export function joinBroadPeaks<T extends GSDPeakOptionalShape>(
7577
7678 //@ts -expect-error Push a feke peak
7779 broadLines . push ( { x : Number . MAX_VALUE , y : 0 } ) ;
78-
7980 let candidates : { x : number [ ] ; y : number [ ] } = {
8081 x : [ broadLines [ 0 ] . x ] ,
8182 y : [ broadLines [ 0 ] . y ] ,
@@ -93,26 +94,38 @@ export function joinBroadPeaks<T extends GSDPeakOptionalShape>(
9394 count ++ ;
9495 } else {
9596 if ( count > 2 ) {
96- const fitted = optimizePeaks (
97+ const initialWidth = Math . abs (
98+ candidates . x [ candidates . x . length - 1 ] - candidates . x [ 0 ] ,
99+ ) ;
100+ const { logs, optimizedPeaks } = optimizePeaksWithLogs (
97101 candidates ,
98102 [
99103 {
100104 id : generateID ( ) ,
101105 x : broadLines [ maxI ] . x ,
102106 y : max ,
103- width : Math . abs (
104- candidates . x [ candidates . x . length - 1 ] - candidates . x [ 0 ] ,
105- ) ,
107+ width : initialWidth ,
108+ parameters : {
109+ width : { max : initialWidth * 4 , min : initialWidth * 0.8 } ,
110+ } ,
106111 } ,
107112 ] ,
108- { shape, optimization } ,
113+ { shape : { kind : 'pseudoVoigt' } , optimization } ,
109114 ) ;
110- newPeaks . push ( fitted [ 0 ] ) ;
111- } else {
112- // Put back the candidates to the peak list
113- for ( const index of indexes ) {
114- newPeaks . push ( getGSDPeakOptimizedStructure ( broadLines [ index ] ) ) ;
115+ [ max , maxI ] = [ 0 , 0 ] ;
116+ const log = logs . find ( ( l ) => l . message === 'optimization successful' ) ;
117+ if ( log ) {
118+ const { error } = log ;
119+ if ( error < 0.2 ) {
120+ newPeaks . push ( optimizedPeaks [ 0 ] ) ;
121+ } else {
122+ pushBackPeaks ( broadLines , indexes , newPeaks ) ;
123+ }
124+ } else {
125+ pushBackPeaks ( broadLines , indexes , newPeaks ) ;
115126 }
127+ } else {
128+ pushBackPeaks ( broadLines , indexes , newPeaks ) ;
116129 }
117130
118131 candidates = { x : [ broadLines [ i ] . x ] , y : [ broadLines [ i ] . y ] } ;
@@ -129,6 +142,11 @@ export function joinBroadPeaks<T extends GSDPeakOptionalShape>(
129142 return addMissingIDs ( newPeaks , { output : newPeaks } ) ;
130143}
131144
145+ function pushBackPeaks ( broadLines , indexes , peaks ) {
146+ for ( const index of indexes ) {
147+ peaks . push ( getGSDPeakOptimizedStructure ( broadLines [ index ] ) ) ;
148+ }
149+ }
132150function getGSDPeakOptimizedStructure < T extends GSDPeakOptionalShape > ( peak : T ) {
133151 const { id, shape, x, y, width } = peak ;
134152
0 commit comments