8000 difference in promotion between py2 and py3 · Issue #41 · ndarray/ndarray · GitHub 8000
[go: up one dir, main page]

Skip to content

difference in promotion between py2 and py3 #41

@nbecker

Description

@nbecker

Here's a weird one. I have a histogram class that overloads += operator:

  bp::class_<hist_t> ("histogram", bp::init<double,double,double,bool> (
                                          (bp::arg ("min"),
                                           bp::arg ("max"),
                                           bp::arg ("delta"),
                                           bp::arg ("clip")=true
                                           )))
    .def (bp::init<nd::Array<int64_t,1,1>,double,double,double,bool>())
    .def (bp::self += hist_t::value_type())
    .def (bp::self += nd::Array<double,1,0>())
    .def ("__call__", &hist_t::operator())
    .def_readwrite ("results", &hist_t::buckets)
    .add_property ("buckets", &get_buckets<hist_t,nd::Array<double,1,1> >)
    .def ("mean", &hist_t::Mean)
    .def ("rms", &hist_t::RMS)
    .def ("cumulative", &hist_t::Cumulative<nd::Array<double,1,1> >)
    .def ("inv_cumulative", &hist_t::OneMinusCumulative<nd::Array<double,1,1> >)
    .def_pickle(hist_pickle_suite<hist_t>())

    .def_readonly ("min", &hist_t::min)
    .def_readonly ("max", &hist_t::max)
    .def_readonly ("delta", &hist_t::delta)
    .add_property ("rc", &getrc<hist_t>) // just for info
    ;

I call it:
remod_out_hist += np.sum (np.max (remod_out_cnts, axis=1))

Now if we print out the type (np.sum (np.max ...)) on
py2: <type 'numpy.int64'>

but on py3: <class 'numpy.int64'>

When run on py2, the type numpy.int64 seems to be promoted to double, which matches the 1st self += (value_type should be double).

But when run on py3, the promotion fails:
Boost.Python.ArgumentError: Python argument types in
histogram.iadd(histogram, numpy.int64)
did not match C++ signature:
iadd(boost::python::back_reference<Histogram<long, double, ndarray::Array<long, 1, 1> >&>, ndarray::Array<double, 1, 0>)
iadd(boost::python::back_reference<Histogram<long, double, ndarray::Array<long, 1, 1> >&>, double)

If I explicitly write
remod_out_hist += float(np.sum (np.max (remod_out_cnts, axis=1)))

then it runs on py3

I'm wondering if the difference in type() on py2 and py3 might explain the different behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0