Skip to content

Commit 4da4fa4

Browse files
committed
ParamArray set_value and set_partial
Parts of the strored array can now be set through slicing and set_value works correctly if no argument provided.
1 parent bfbaa26 commit 4da4fa4

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

puzzlepiece/param.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,22 +401,45 @@ def _click_handler(self, _):
401401
# Flip back the checkbox if the click resulted in an error
402402
self.input.setChecked(not(self.input.isChecked()))
403403
raise e
404-
404+
405+
406+
class _PartialAccessor:
407+
def __init__(self, param):
408+
self.param = param
409+
410+
def __setitem__(self, key, value):
411+
self.param._value.__setitem__(key, value)
412+
self.param.set_value()
405413

406414
class ParamArray(BaseParam):
407415
"""
408416
A param that stores a numpy array. There is no GUI input, the Param simply displays the
409417
dimensions of the array, and indicates when the data has been updated.
410418
411-
The array can be modified programmatically by providing setters or getters, or using
412-
:func:`~puzzlepiece.param.BaseParam.set_value`.
419+
The array can be modified through programmatic interaction with setter or getter functions
420+
(for example the array can be obtained from a hardware spectrometer), or treated as a variable
421+
and set using :func:`~puzzlepiece.param.BaseParam.set_value`.
413422
"""
414423
_type = np.asarray
415424

416425
def __init__(self, name, value, setter=None, getter=None, visible=True, format='{}', _type=None, *args, **kwargs):
417426
self._indicator_state = True
427+
self._partial_accessor = _PartialAccessor(self)
418428
super().__init__(name, value, setter, getter, visible, format, _type, *args, **kwargs)
419429

430+
@property
431+
def set_partial(self):
432+
"""
433+
Use this property to set values to slices of the stored numpy array, using
434+
any slicing methods that a numpy array accepts::
435+
436+
puzzle['piece'].params['image'].set_partial[100:200, :] = 128
437+
438+
This will call the param's setter if there's one, and in general
439+
acts like :func:`~puzzlepiece.param.BaseParam.set_value`.
440+
"""
441+
return self._partial_accessor
442+
420443
def _make_input(self, value=None, connect=None):
421444
"""
422445
:meta private:
@@ -430,7 +453,27 @@ def _input_set_value(self, value):
430453
"""
431454
:meta private:
432455
"""
433-
self.input.setText(self._format_array(value))
456+
self.input.setText(self._format_array(value))
457+
458+
def _input_get_value(self):
459+
"""
460+
:meta private:
461+
"""
462+
return self._value
463+
464+
def set_value(self, value=None):
465+
"""
466+
This method overrides :func:`puzzlepiece.param.BaseParam.set_value`.
467+
See there for documentation.
468+
469+
:meta private:
470+
"""
471+
# Small check to account for the label being set twice
472+
# in super().set_value(value) when the
473+
# value argument is None
474+
if value is not None:
475+
self._indicator_state = not self._indicator_state
476+
return super().set_value(value)
434477

435478
def _format_array(self, value):
436479
self._indicator_state = not self._indicator_state

0 commit comments

Comments
 (0)