Skip to content

Commit

Permalink
ENH: using python's warnings instead of prints in fitpack.
Browse files Browse the repository at this point in the history
When fitpack detects a recoverable problem, it will signal it with
a RuntimeWarning instead of printing to stdout. This way the output
can be programatically silenced or made exceptions.
  • Loading branch information
David Menéndez Hurtado committed Sep 18, 2014
1 parent fdddfc3 commit ad056f9
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions scipy/interpolate/fitpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
For univariate splines: cocosp, concon, fourco, insert
For bivariate splines: profil, regrid, parsur, surev
"""
from __future__ import division, print_function, absolute_import
from __future__ import division, absolute_import


__all__ = ['splrep', 'splprep', 'splev', 'splint', 'sproot', 'spalde',
'bisplrep', 'bisplev', 'insert', 'splder', 'splantider']

import warnings
import numpy as np
from . import _fitpack
from numpy import atleast_1d, array, ones, zeros, sqrt, ravel, transpose, \
Expand Down Expand Up @@ -229,7 +230,7 @@ def splprep(x,w=None,u=None,ub=None,ue=None,k=3,task=0,s=None,t=None,
for i in range(idim):
if x[i][0] != x[i][-1]:
if quiet < 2:
print('Warning: Setting x[%d][%d]=x[%d][0]' % (i,m,i))
warnings.warn(RuntimeWarning('Setting x[%d][%d]=x[%d][0]' % (i,m,i)))
x[i][-1] = x[i][0]
if not 0 < idim < 11:
raise TypeError('0 < idim < 11 must hold')
Expand Down Expand Up @@ -295,11 +296,12 @@ def splprep(x,w=None,u=None,ub=None,ue=None,k=3,task=0,s=None,t=None,
c.shape = idim,n-k-1
tcku = [t,list(c),k],u
if ier <= 0 and not quiet:
print(_iermess[ier][0])
print("\tk=%d n=%d m=%d fp=%f s=%f" % (k,len(t),m,fp,s))
warnings.warn(RuntimeWarning(_iermess[ier][0] +
"\tk=%d n=%d m=%d fp=%f s=%f" %
(k, len(t), m, fp, s)))
if ier > 0 and not full_output:
if ier in [1,2,3]:
print("Warning: "+_iermess[ier][0])
warnings.warn(RuntimeWarning(_iermess[ier][0]))
else:
try:
raise _iermess[ier][1](_iermess[ier][0])
Expand Down Expand Up @@ -498,11 +500,12 @@ def splrep(x,y,w=None,xb=None,xe=None,k=3,task=0,s=None,t=None,
n,c,fp,ier = dfitpack.percur(task, x, y, w, t, wrk, iwrk, k, s)
tck = (t[:n],c[:n],k)
if ier <= 0 and not quiet:
print(_iermess[ier][0])
print("\tk=%d n=%d m=%d fp=%f s=%f" % (k,len(t),m,fp,s))
warnings.warn(RuntimeWarning(_iermess[ier][0]) +
"\tk=%d n=%d m=%d fp=%f s=%f" %
(k, len(t), m, fp, s))
if ier > 0 and not full_output:
if ier in [1,2,3]:
print("Warning: "+_iermess[ier][0])
warnings.warn(RuntimeWarning(+_iermess[ier][0]))
else:
try:
raise _iermess[ier][1](_iermess[ier][0])
Expand Down Expand Up @@ -723,7 +726,7 @@ def sproot(tck,mest=10):
if ier == 0:
return z
if ier == 1:
print("Warning: the number of zeros exceeds mest")
warnings.warn(RuntimeWarning("The number of zeros exceeds mest"))
return z
raise TypeError("Unknown error")

Expand Down Expand Up @@ -951,14 +954,14 @@ def bisplrep(x,y,z,w=None,xb=None,xe=None,yb=None,ye=None,kx=3,ky=3,task=0,

ierm = min(11,max(-3,ier))
if ierm <= 0 and not quiet:
print(_iermess2[ierm][0])
print("\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" % (kx,ky,len(tx),
len(ty),m,fp,s))
warnings.warn(RuntimeWarning(_iermess2[ierm][0]) +
"\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" %
(kx, ky, len(tx), len(ty), m, fp, s))
if ierm > 0 and not full_output:
if ier in [1,2,3,4,5]:
print("Warning: "+_iermess2[ierm][0])
print("\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" % (kx,ky,len(tx),
len(ty),m,fp,s))
_mess = "\n\tkx,ky=%d,%d nx,ny=%d,%d m=%d fp=%f s=%f" % (kx,ky,len(tx),
len(ty),m,fp,s)
warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess))
else:
try:
raise _iermess2[ierm][1](_iermess2[ierm][0])
Expand Down

0 comments on commit ad056f9

Please sign in to comment.