From 87342adcd799f57b26547cdcc2f98dcdf58b5857 Mon Sep 17 00:00:00 2001
From: Romain Fetick <rfetick@OSU.PYTHEAS>
Date: Wed, 5 Mar 2025 15:13:23 +0100
Subject: [PATCH] can set a max nb of function evaluations in psffit

---
 maoppy/psffit.py | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/maoppy/psffit.py b/maoppy/psffit.py
index 2ac8083..3d0aaa2 100644
--- a/maoppy/psffit.py
+++ b/maoppy/psffit.py
@@ -49,8 +49,8 @@ def lsq_flux_bck(model, data, weights, background=True, positive_bck=False):
     return amp, bck
 
 
-def psffit(psf,model,x0,weights=None,dxdy=(0.,0.),flux_bck=(True,True),
-           positive_bck=False,fixed=None,npixfit=None,**kwargs):
+def psffit(psf, model, x0, weights=None, dxdy=(0.,0.), flux_bck=(True,True),
+           positive_bck=False, fixed=None, npixfit=None, max_nfev=None, **kwargs):
     """Fit a PSF with a parametric model solving the least-square problem
        epsilon(x) = SUM_pixel { weights * (amp * Model(x) + bck - psf)² }
     
@@ -77,6 +77,8 @@ def psffit(psf,model,x0,weights=None,dxdy=(0.,0.),flux_bck=(True,True),
         Fix some parameters to their initial value (default: None)
     npixfit : int (default=None)
         Increased pixel size for improved fitting accuracy
+    max_nfev : int or None
+        Maximum number of evaluation of the cost function
     **kwargs :
         All keywords used to call your `model`
     
@@ -172,10 +174,9 @@ def psffit(psf,model,x0,weights=None,dxdy=(0.,0.),flux_bck=(True,True),
         b_up = np.concatenate((b_up,[np.inf,np.inf]))
         return (b_low,b_up)
     
-    result = least_squares(cost, input2mini(x0,dxdy), bounds=get_bound(model))
+    result = least_squares(cost, input2mini(x0,dxdy), bounds=get_bound(model), max_nfev=max_nfev)
     
-    msg = result.message[0:min(len(result.message),15)]
-    print("\rPSFFIT finished %3u iter  %s..."%(cost.iter,msg))
+    print("\rPSFFIT finished in %u iter : %s"%(cost.iter,result.message))
     
     result.x, result.dxdy = mini2input(result.x) #split output between x and dxdy
     
-- 
GitLab