-2

I'm having trouble using functools.partial with np.multiply and I'm don't understand why I'm getting this error. I'm just learning how partial works so it's possible I'm just making a silly error.

The error is Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 194, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/shilo/PycharmProjects/Video/testing/test_functools.py", line 7, in <module>
    f(x1=image,x2=2.0)
ValueError: invalid number of arguments

The code that I'm attempting to use is below:

  from functools import partial
    import cv2
    import numpy as np
    path = '/Users/shilo/Desktop/Goliath_preview.jpg'
    image = cv2.imread(path)
    f = partial(np.multiply,x1=image,x2=2.0)
    f()

The np.multiply function definition looks like this: def multiply(x1, x2, *args, **kwargs) I assumed that I only needed to supply x1 and x2 but it doesn't seem to work even when I include args/kwargs

I've also tried including all of the kwargs listed in the docstring but still got the same invalid number of arguments error. multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Shilo
  • 313
  • 3
  • 16
  • "The np.multiply function definition looks like this: `def multiply(x1, x2, *args, **kwargs)`" - no it doesn't. If you're looking at PyCharm's fake source code, this is why you should stop doing that. – user2357112 Sep 03 '18 at 00:38

1 Answers1

1

By naming x1 and x2 in your partial call you're using them as kwargs. I think it should be:

f = partial(np.multiply, image, 2.0)

Else, try

f = partial(np.multiply, image)
f(2.0)
ehacinom
  • 8,070
  • 7
  • 43
  • 65
  • Does this work for you? It might be the version of numpy I have installed(15.1) Both of your suggestions result in the following error: `TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'` If I use `cv2.imwrite('img.jpg',image)` The original image is saved so I don't understand the where `None` is coming from. – Shilo Sep 03 '18 at 00:32
  • that sounds like cv2.imread is returning a NoneType instead of an array of your image – ehacinom Sep 03 '18 at 00:35
  • Oh weird, if I use it on the image I saved your solution works but it doesn't seem to work with the original. – Shilo Sep 03 '18 at 00:38