[SciPy-User] Passing a dictionary to fsolve as initial parameter

josef.pktd at gmail.com josef.pktd at gmail.com
Fri Oct 22 22:04:58 EDT 2010


On Fri, Oct 22, 2010 at 9:44 PM, Gökhan Sever <gokhansever at gmail.com> wrote:
> Hello,
>
> I am getting NameError: global name 'kohler_data' is not defined when
> I try to execute the following code piece in script1. Here
> petters_critical_condition function is imported from an external
> library. In reality script2.py contains many similar functions that I
> use as an external library which then I import those functions into my
> main analysis script. Right now the execution can't go further due to
> the error. When I define the function within the script1.py than its
> ok.
>
> Is there any workaround for this or a bug?
>
> Thanks.
>
> This is:
>
> I[8]: scipy.__version__
> O[8]: '0.8.0.dev6156'
>
> Sample code is below
>
> # script1.py
> from script2 import *
> kohler_data = {'rd': rd_accumu}
> rwcrit_accumu = fsolve(petters_critical_condition, kohler_data['rd']*2)
>
>
> # script2.py
> def petters_critical_condition(x):
>    term1 = -kohler_const['kelvin']/x**2 *
> np.exp(kohler_const['kelvin']/x) * (x**3
>            - kohler_data['rd']**3) / (x**3 - kohler_data['rd']**3 *
> (1.0 - kohler_const['kappa']))
>    term2 = np.exp(kohler_const['kelvin']/x) * 3*x**2 / (x**3 -
> kohler_data['rd']**3 * (1.0 - kohler_const['kappa']))
>    term3 = -np.exp(kohler_const['kelvin']/x) * (x**3 -
> kohler_data['rd']**3) * \
>            3*x**2 / (x**3 - kohler_data['rd']**3 * (1.0
> -kohler_const['kappa']))**2
>    return term1+term2+term3

If this is the entire module, then none of the kohler dictionary
should be defined. If you want to use them from the outer scope, then
you have to define values in the same module.
I'm not quite sure because I never try to do this.

What I (would) do instead is  to wrap petters_critical_condition in a
class and add the kohler_data and kohler_const as attributes, that can
then be accessed by the petters_critical_condition method

Josef


> #sample data.
> I[4]: kohler_data
> O[4]:
> {'rd': array([  6.80000000e-08,   8.05000000e-08,   9.85000000e-08,
>         1.16000000e-07,   1.42500000e-07,   1.90500000e-07,
>         3.29000000e-07,   4.66500000e-07,   6.83500000e-07,
>         1.09700000e-06,   1.34000000e-06,   1.44650000e-06,
>         1.74050000e-06,   2.25050000e-06,   2.70650000e-06])}
>
> following is the full error output:
>
> I[3]: run airplot.py
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /home/gsever/Desktop/ccnworks/thesis/part2_saudi/airplot.py in <module>()
>    220 dn_accumu = pcasp_conc / cubic_meter_by_cubic_centimeter
>    221 kohler_data = {'rd': rd_accumu}
> --> 222 rwcrit_accumu = fsolve(petters_critical_condition, kohler_data['rd']*2)
>    223 scrit_accumu = petters_s(rwcrit_accumu)
>    224 kohler_data['rh'] = rh.mean()
>
> /usr/lib/python2.6/site-packages/scipy/optimize/minpack.py in
> fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev,
> band, epsfcn, factor, diag, warning)
>    126     n = len(x0)
>    127     if type(args) != type(()): args = (args,)
> --> 128     check_func(func,x0,args,n,(n,))
>    129     Dfun = fprime
>    130     if Dfun is None:
>
> /usr/lib/python2.6/site-packages/scipy/optimize/minpack.py in
> check_func(thefunc, x0, args, numinputs, output_shape)
>     11
>     12 def check_func(thefunc, x0, args, numinputs, output_shape=None):
> ---> 13     res = atleast_1d(thefunc(*((x0[:numinputs],)+args)))
>     14     if (output_shape is not None) and (shape(res) != output_shape):
>     15         if (output_shape[0] != 1):
>
> /home/gsever/Desktop/ccnworks/thesis/part2_saudi/modellib.py in
> petters_critical_condition(x)
>    313     term2 = np.exp(kohler_const['kelvin']/x) * 3*x**2 / (x**3
> - kohler_data['rd']**3 * (1.0 - kohler_const['kappa']))
>    314     term3 = -np.exp(kohler_const['kelvin']/x) * (x**3 -
> kohler_data['rd']**3) * \
> --> 315             3*x**2 / (x**3 - kohler_data['rd']**3 * (1.0
> -kohler_const['kappa']))**2
>    316     return term1+term2+term3
>    317
>
> NameError: global name 'kohler_data' is not defined
> WARNING: Failure executing file: <airplot.py>
>
>
>
>
> --
> Gökhan
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list