Menu

#382 Catch fit errors

None
closed-accepted
nobody
None
5
2022-01-15
2014-03-23
No

If a fit goes wrong, a running user script just stops and falls back to the console.

It´d be great if "fit" could be instructed to not raise an exception, e.g.

"set fit [no]stoponerror"

That´d make it possible to let the script evaluate GPVAL_ERRNO and GPVAL_ERRMSG to e.g. repeat the fit with less variables or newly determined starting values or just skip that one and do the next dataset, without user interaction.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2015-05-22

    Just noticed this request from last year...

    As it happens, I've been working with a fitting script that does exactly what you want. No changes required from the current cvs version. My script has:

        ...
        stats $PROFILE using 2 name "PROFILE" nooutput
        reset errors
        fit F(x) $PROFILE using 1:(column(2)/PROFILE_max) via Tm,b
        if (GPVAL_ERRNO) {
            Tm = b = NaN;
            print "Fit error for data line ", lineno
        } else {
            [... normal processing of fit results]
        }
    

    Does this not work for you?

     
  • Karl Ratzsch

    Karl Ratzsch - 2015-05-27

    Seems not. Could this depend on the kind of error? With an
    "Undefined value during function evaluation", the script always falls back to the console immediately, for me. Attached is a (stupid) test problem, can you try if you reach the part after the "fit" command, evaluating GPVAL_ERRNO?

     

    Last edit: Karl Ratzsch 2015-05-27
  • Ethan Merritt

    Ethan Merritt - 2015-05-27

    Your test script did not upload correctly (0 bytes).

    Maybe a key difference is that I am driving gnuplot via a pipe from a perl script. Can you try

    cat test.gp | gnuplot
    
     
  • Karl Ratzsch

    Karl Ratzsch - 2015-05-27

    (I replaced the test script above, it now downloads correctly.)

    You were right, it only works when piping the script in. Unfortunately that is not an option for me, because it breaks if the offending "fit" command is inside a command block {fit ...}, see attachment.

     
  • Ethan Merritt

    Ethan Merritt - 2015-05-27

    That is because the entire content of a bracketed clause is treated as one long input line. But you can isolate the contents by placing them in a loaded file.

    wrap.gp:

    a=1.
    
    set fit quiet
    
    if (a == 1) {
        load 'fit.gp'
        # This is never reached
        if (GPVAL_ERRNO) {
            print "Fit error was : ", GPVAL_ERRMSG
        } else {
            print "fits finely"
        }
    
    }    
    

    fit.gp:

      reset errors
      fit a/x $dat noerror via a
    

    cat wrap.gp | gnuplot

    Update: Hmmm. That doesn't seem to work as I remembered. There must have been something else involved when I used this trick before.

     

    Last edit: Ethan Merritt 2015-05-28
  • Karl Ratzsch

    Karl Ratzsch - 2015-05-29

    Solution would of course be to not implement critical programming structures in gnuplot but in an overlying perl/python/etc. script. Gnuplot is no general purpose data evaluation language, something i keep forgetting. ;-)

    Still, the GPVAL_ error variables don't make a lot of sense if i cannot evaluate them in a given script. Not only for fitting. A general "un/set haltonerror" would be useful in many ways.

     
  • Ethan Merritt

    Ethan Merritt - 2022-01-15

    Version 5.5 now traps and returns from fit errors.
    From the documentation:

    Subtopic of fit: error_recovery

    Starting with gnuplot version 5.5, the fit command always returns to the
    next command input line regardless of the success or failure of fitting.
    This allows scripted recovery from fit errors. The variable FIT_ERROR is
    set to 0 on success, non-zero on error.
    Example:

      do for [i=1:5] {
          DATA = sprintf("Data_%05d.dat", i)
          fit f(x) DATA via a,b,c
          if (FIT_ERROR || !FIT_CONVERGED) {
              continue
          }
          set output sprintf("dataset_%05.png", i)
          plot DATA, f(x)
          unset output
      }
    
     
  • Ethan Merritt

    Ethan Merritt - 2022-01-15
    • status: open --> closed-accepted
    • Group: -->
     

Log in to post a comment.