@@ -262,7 +262,6 @@ return /******/ (function(modules) { // webpackBootstrap
262262 if ( count > 2 ) {
263263 var fitted = Opt . optimizeSingleLorentzian ( candidates ,
264264 { x : broadLines [ maxI ] . x , y :max , width : Math . abs ( candidates [ 0 ] [ 0 ] - candidates [ candidates . length - 1 ] [ 0 ] ) } ) ;
265- //console.log(fitted)
266265 peakList . push ( { x :fitted [ 0 ] [ 0 ] , y :fitted [ 1 ] [ 0 ] , width :fitted [ 2 ] [ 0 ] , soft :false } ) ;
267266
268267 }
@@ -430,15 +429,21 @@ return /******/ (function(modules) { // webpackBootstrap
430429 * @returns {*[] }
431430 */
432431 function optimizeSingleLorentzian ( xy , peak , opts ) {
433- var xy2 = parseData ( xy ) ;
432+ opts = opts || { } ;
433+ var xy2 = parseData ( xy , opts . percentage || 0 ) ;
434+
435+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
436+ return null ; //Cannot run an optimization with less than 3 points
437+ }
438+
434439 var t = xy2 [ 0 ] ;
435440 var y_data = xy2 [ 1 ] ;
436441 var maxY = xy2 [ 2 ] ;
437- var nbPoints = t . columns , i ;
442+ var nbPoints = t . rows , i ;
438443
439444 var weight = [ nbPoints / Math . sqrt ( y_data . dot ( y_data ) ) ] ;
440445
441- var opts = Object . create ( opts || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
446+ var opts = Object . create ( opts . LMOptions || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
442447 //var opts = [ 3, 100, 1e-3, 1e-3, 1e-3, 1e-2, 1e-2, 11, 9, 1 ];
443448 var consts = [ ] ;
444449 var dt = Math . abs ( t [ 0 ] [ 0 ] - t [ 1 ] [ 0 ] ) ; // optional vector of constants
@@ -461,16 +466,24 @@ return /******/ (function(modules) { // webpackBootstrap
461466 * @returns {*[] }
462467 */
463468 function optimizeSingleGaussian ( xy , peak , opts ) {
464- var xy2 = parseData ( xy ) ;
469+ opts = opts || { } ;
470+ var xy2 = parseData ( xy , opts . percentage || 0 ) ;
471+
472+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
473+ return null ; //Cannot run an optimization with less than 3 points
474+ }
475+
465476 var t = xy2 [ 0 ] ;
466477 var y_data = xy2 [ 1 ] ;
467478 var maxY = xy2 [ 2 ] ;
468479
469- var nbPoints = t . columns , i ;
480+ var nbPoints = t . rows , i ;
481+
482+
470483
471484 var weight = [ nbPoints / Math . sqrt ( y_data . dot ( y_data ) ) ] ;
472485
473- var opts = Object . create ( opts || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
486+ var opts = Object . create ( opts . LMOptions || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
474487 //var opts = [ 3, 100, 1e-3, 1e-3, 1e-3, 1e-2, 1e-2, 11, 9, 1 ];
475488 var consts = [ ] ; // optional vector of constants
476489 var dt = Math . abs ( t [ 0 ] [ 0 ] - t [ 1 ] [ 0 ] ) ;
@@ -488,6 +501,91 @@ return /******/ (function(modules) { // webpackBootstrap
488501 return [ p_fit [ 0 ] , [ p_fit [ 1 ] [ 0 ] * maxY ] , p_fit [ 2 ] ] ;
489502 }
490503
504+ /*
505+ peaks on group should sorted
506+ */
507+ function optimizeLorentzianTrain ( xy , group , opts ) {
508+ var xy2 = parseData ( xy ) ;
509+ //console.log(xy2[0].rows);
510+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
511+ return null ; //Cannot run an optimization with less than 3 points
512+ }
513+
514+ var t = xy2 [ 0 ] ;
515+ var y_data = xy2 [ 1 ] ;
516+ var maxY = xy2 [ 2 ] ;
517+ var currentIndex = 0 ;
518+ var nbPoints = t . length ;
519+ var nextX ;
520+ var tI , yI , maxY ;
521+ var result = [ ] , current ;
522+ for ( var i = 0 ; i < group . length ; i ++ ) {
523+ nextX = group [ i ] . x - group [ i ] . width * 4 ;
524+ //console.log(group[i]);
525+ while ( t [ currentIndex ++ ] < nextX && currentIndex < nbPoints ) ;
526+ nextX = group [ i ] . x + group [ i ] . width * 4 ;
527+ tI = [ ] ;
528+ yI = [ ] ;
529+ while ( t [ currentIndex ] <= nextX && currentIndex < nbPoints ) {
530+ tI . push ( t [ currentIndex ] [ 0 ] ) ;
531+ yI . push ( y_data [ currentIndex ] [ 0 ] * maxY ) ;
532+ currentIndex ++ ;
533+ }
534+
535+ current = optimizeSingleLorentzian ( [ tI , yI ] , group [ i ] , opts ) ;
536+ if ( current ) {
537+ result . push ( { "x" :current [ 0 ] [ 0 ] , "y" :current [ 1 ] [ 0 ] , "width" :current [ 2 ] [ 0 ] , "opt" :true } ) ;
538+ }
539+ else {
540+ result . push ( { "x" :group [ i ] . x , "y" :group [ i ] . y , "width" :group [ i ] . width , "opt" :false } ) ;
541+ }
542+ }
543+
544+ return result ;
545+
546+ }
547+
548+ function optimizeGaussianTrain ( xy , group , opts ) {
549+ var xy2 = parseData ( xy ) ;
550+ //console.log(xy2[0].rows);
551+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
552+ return null ; //Cannot run an optimization with less than 3 points
553+ }
554+
555+ var t = xy2 [ 0 ] ;
556+ var y_data = xy2 [ 1 ] ;
557+ var maxY = xy2 [ 2 ] ;
558+ var currentIndex = 0 ;
559+ var nbPoints = t . length ;
560+ var nextX ;
561+ var tI , yI , maxY ;
562+ var result = [ ] , current ;
563+ for ( var i = 0 ; i < group . length ; i ++ ) {
564+ nextX = group [ i ] . x - group [ i ] . width * 4 ;
565+ //console.log(group[i]);
566+ while ( t [ currentIndex ++ ] < nextX && currentIndex < nbPoints ) ;
567+ nextX = group [ i ] . x + group [ i ] . width * 4 ;
568+ tI = [ ] ;
569+ yI = [ ] ;
570+ while ( t [ currentIndex ] <= nextX && currentIndex < nbPoints ) {
571+ tI . push ( t [ currentIndex ] [ 0 ] ) ;
572+ yI . push ( y_data [ currentIndex ] [ 0 ] * maxY ) ;
573+ currentIndex ++ ;
574+ }
575+
576+ current = optimizeSingleGaussian ( [ tI , yI ] , group [ i ] , opts ) ;
577+ if ( current ) {
578+ result . push ( { "x" :current [ 0 ] [ 0 ] , "y" :current [ 1 ] [ 0 ] , "width" :current [ 2 ] [ 0 ] , "opt" :true } ) ;
579+ }
580+ else {
581+ result . push ( { "x" :group [ i ] . x , "y" :group [ i ] . y , "width" :group [ i ] . width , "opt" :false } ) ;
582+ }
583+ }
584+
585+ return result ;
586+ }
587+
588+
491589
492590 /**
493591 *
@@ -497,13 +595,18 @@ return /******/ (function(modules) { // webpackBootstrap
497595 */
498596 function optimizeLorentzianSum ( xy , group , opts ) {
499597 var xy2 = parseData ( xy ) ;
598+
599+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
600+ return null ; //Cannot run an optimization with less than 3 points
601+ }
602+
500603 var t = xy2 [ 0 ] ;
501604 var y_data = xy2 [ 1 ] ;
502605 var maxY = xy2 [ 2 ] ;
503- var nbPoints = t . columns , i ;
606+ var nbPoints = t . rows , i ;
504607
505608 var weight = [ nbPoints / math . sqrt ( y_data . dot ( y_data ) ) ] ;
506- var opts = Object . create ( opts || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
609+ var opts = Object . create ( opts || [ 3 , 100 , 1e-3 , 1e-3 , 1e-3 , 1e-2 , 1e-2 , 11 , 9 , 1 ] ) ;
507610 var consts = [ ] ; // optional vector of constants
508611
509612 var nL = group . length ;
@@ -551,6 +654,11 @@ return /******/ (function(modules) { // webpackBootstrap
551654 */
552655 function optimizeGaussianSum ( xy , group , opts ) {
553656 var xy2 = parseData ( xy ) ;
657+
658+ if ( xy2 === null || xy2 [ 0 ] . rows < 3 ) {
659+ return null ; //Cannot run an optimization with less than 3 points
660+ }
661+
554662 var t = xy2 [ 0 ] ;
555663 var y_data = xy2 [ 1 ] ;
556664 var maxY = xy2 [ 2 ] ;
@@ -608,7 +716,7 @@ return /******/ (function(modules) { // webpackBootstrap
608716 * @param xy
609717 * @returns {*[] }
610718 */
611- function parseData ( xy ) {
719+ function parseData ( xy , threshold ) {
612720 var nbSeries = xy . length ;
613721 var t = null ;
614722 var y_data = null , x , y ;
@@ -617,56 +725,66 @@ return /******/ (function(modules) { // webpackBootstrap
617725 if ( nbSeries == 2 ) {
618726 //Looks like row wise matrix [x,y]
619727 var nbPoints = xy [ 0 ] . length ;
620- if ( nbPoints < 3 )
621- throw new SizeException ( nbPoints ) ;
728+ //if(nbPoints<3)
729+ // throw new Exception(nbPoints);
730+ //else{
731+ t = new Array ( nbPoints ) ; //new Matrix(nbPoints,1);
732+ y_data = new Array ( nbPoints ) ; //new Matrix(nbPoints,1);
733+ x = xy [ 0 ] ;
734+ y = xy [ 1 ] ;
735+ if ( typeof x [ 0 ] === "number" ) {
736+ for ( i = 0 ; i < nbPoints ; i ++ ) {
737+ t [ i ] = x [ i ] ;
738+ y_data [ i ] = y [ i ] ;
739+ if ( y [ i ] > maxY )
740+ maxY = y [ i ] ;
741+ }
742+ }
622743 else {
623- t = new Matrix ( nbPoints , 1 ) ;
624- y_data = new Matrix ( nbPoints , 1 ) ;
625- x = xy [ 0 ] ;
626- y = xy [ 1 ] ;
627- if ( typeof x [ 0 ] === "number" ) {
744+ //It is a colum matrix
745+ if ( typeof x [ 0 ] === "object" ) {
628746 for ( i = 0 ; i < nbPoints ; i ++ ) {
629- t [ i ] [ 0 ] = x [ i ] ;
630- y_data [ i ] [ 0 ] = y [ i ] ;
631- if ( y [ i ] > maxY )
632- maxY = y [ i ] ;
633- }
634- }
635- else {
636- //It is a colum matrix
637- if ( typeof x [ 0 ] === "object" ) {
638- for ( i = 0 ; i < nbPoints ; i ++ ) {
639- t [ i ] [ 0 ] = x [ i ] [ 0 ] ;
640- y_data [ i ] [ 0 ] = y [ i ] [ 0 ] ;
641- if ( y [ i ] [ 0 ] > maxY )
642- maxY = y [ i ] [ 0 ] ;
643- }
747+ t [ i ] = x [ i ] [ 0 ] ;
748+ y_data [ i ] = y [ i ] [ 0 ] ;
749+ if ( y [ i ] [ 0 ] > maxY )
750+ maxY = y [ i ] [ 0 ] ;
644751 }
645-
646752 }
647753
648754 }
755+
756+ //}
649757 }
650758 else {
651759 //Looks like a column wise matrix [[x],[y]]
652760 var nbPoints = nbSeries ;
653- if ( nbPoints < 3 )
654- throw new SizeException ( nbPoints ) ;
655- else {
656- t = new Matrix ( nbPoints , 1 ) ;
657- y_data = new Matrix ( nbPoints , 1 ) ;
658- for ( i = 0 ; i < nbPoints ; i ++ ) {
659- t [ i ] [ 0 ] = xy [ i ] [ 0 ] ;
660- y_data [ i ] [ 0 ] = xy [ i ] [ 1 ] ;
661- if ( y_data [ i ] [ 0 ] > maxY )
662- maxY = y_data [ i ] [ 0 ] ;
663- }
664- }
761+ // if(nbPoints<3)
762+ // throw new SizeException(nbPoints);
763+ // else {
764+ t = new Array ( nbPoints ) ; // new Matrix(nbPoints, 1);
765+ y_data = new Array ( nbPoints ) ; // new Matrix(nbPoints, 1);
766+ for ( i = 0 ; i < nbPoints ; i ++ ) {
767+ t [ i ] = xy [ i ] [ 0 ] ;
768+ y_data [ i ] = xy [ i ] [ 1 ] ;
769+ if ( y_data [ i ] > maxY )
770+ maxY = y_data [ i ] ;
771+ }
772+ // }
665773 }
666774 for ( i = 0 ; i < nbPoints ; i ++ ) {
667- y_data [ i ] [ 0 ] /= maxY ;
775+ y_data [ i ] /= maxY ;
776+ }
777+ if ( threshold ) {
778+ for ( i = nbPoints - 1 ; i >= 0 ; i -- ) {
779+ if ( y_data [ i ] < threshold ) {
780+ y_data . splice ( i , 1 ) ;
781+ t . splice ( i , 1 ) ;
782+ }
783+ }
668784 }
669- return [ t , y_data , maxY ] ;
785+ if ( t . length > 0 )
786+ return [ ( new Matrix ( [ t ] ) ) . transpose ( ) , ( new Matrix ( [ y_data ] ) ) . transpose ( ) , maxY ] ;
787+ return null ;
670788 }
671789
672790 function sizeException ( nbPoints ) {
@@ -679,6 +797,8 @@ return /******/ (function(modules) { // webpackBootstrap
679797 module . exports . optimizeGaussianSum = optimizeGaussianSum ;
680798 module . exports . singleGaussian = singleGaussian ;
681799 module . exports . singleLorentzian = singleLorentzian ;
800+ module . exports . optimizeGaussianTrain = optimizeGaussianTrain ;
801+ module . exports . optimizeLorentzianTrain = optimizeLorentzianTrain ;
682802
683803/***/ } ,
684804/* 3 */
0 commit comments