param icon indicating copy to clipboard operation
param copied to clipboard

Parameterized.param.pprint() hangs when param.Array parameter is present

Open peterroelants opened this issue 3 years ago • 1 comments

Description of expected behavior and the observed behavior

Having a param.Array in a Parameterized class results in Parameterized.param.pprint() hanging.

Complete, minimal, self-contained example code that reproduces the issue

Running:

import param
import panel as pn
import numpy as np
pn.extension()


class MatParam(param.Parameterized):
    mat = param.Array(np.zeros((2, 2)))

              
mp = MatParam()
p = pn.Column(mp.param.mat)
display(p)

print("Before pprint.")
print(mp.param.pprint())
print("After pprint (Does not print this!)")

hangs, and does not print the "After pprint (Does not print this!)" line.

Screenshot of the full output:

ALL software version info

  • Panel 0.12.6
  • Param 1.12.0
  • Python 3.9.10
  • jupyterlab : 3.2.8

peterroelants avatar Feb 13 '22 18:02 peterroelants

Thanks! I can reproduce this without panel, where it gives an error message rather than hanging:

import param, numpy as np

class MatParam(param.Parameterized):
    mat = param.Array(np.zeros((2, 2)))

mp = MatParam()

print("Before pprint.")
print(mp.param.pprint())
print("After pprint (Does not print this!)")
Before pprint.
 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-1ab328ac4e59> in <module>
      8 
      9 print("Before pprint.")
---> 10 print(mp.param.pprint())
     11 print("After pprint (Does not print this!)")

~/param/param/parameterized.py in pprint(self_, imports, prefix, unknown_value, qualify, separator)
   2597         """See Parameterized.pprint"""
   2598         self = self_.self
-> 2599         return self._pprint(imports, prefix, unknown_value, qualify, separator)
   2600 
   2601 

~/param/param/parameterized.py in wrapper(self, *args, **kwargs)
   3089                 repr_running.add(key)
   3090                 try:
-> 3091                     result = user_function(self, *args, **kwargs)
   3092                 finally:
   3093                     repr_running.discard(key)

~/param/param/parameterized.py in _pprint(self, imports, prefix, unknown_value, qualify, separator)
   3285         imports.append("import %s"%bits[0])
   3286 
-> 3287         changed_params = self.param.values(onlychanged=script_repr_suppress_defaults)
   3288         values = self.param.values()
   3289         spec = getfullargspec(self.__init__)

~/param/param/parameterized.py in values(self_, onlychanged)
   2172         # issues in python2, but can be inverted if get_param_values
   2173         # is removed when python2 support is dropped
-> 2174         return dict(self_.get_param_values(onlychanged))
   2175 
   2176     def force_new_dynamic_value(self_, name): # pylint: disable-msg=E0213

~/param/param/parameterized.py in get_param_values(self_, onlychanged)
   2154         for name, val in self_or_cls.param.objects('existing').items():
   2155             value = self_or_cls.param.get_value_generator(name)
-> 2156             if not onlychanged or not all_equal(value, val.default):
   2157                 vals.append((name, value))
   2158 

~/param/param/parameterized.py in all_equal(arg1, arg2)
    260         return arg1==arg2
    261     try:
--> 262         return all(a1 == a2 for a1, a2 in zip(arg1, arg2))
    263     except TypeError:
    264         return arg1==arg2

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

There's code in pprint to check if a value is the same as its default, in which case it isn't usually printed, and it looks like that code is failing with an array. Probably easy enough to fix up...

jbednar avatar Feb 14 '22 06:02 jbednar

This issue is still present in param main as of today.

jbednar avatar May 12 '23 20:05 jbednar