Skip to content

Commit

Permalink
ParamArray set_value and set_partial
Browse files Browse the repository at this point in the history
Parts of the strored array can now be set through slicing and set_value works correctly if no argument provided.
  • Loading branch information
jdranczewski committed Dec 8, 2023
1 parent bfbaa26 commit 4da4fa4
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions puzzlepiece/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,45 @@ def _click_handler(self, _):
# Flip back the checkbox if the click resulted in an error
self.input.setChecked(not(self.input.isChecked()))
raise e



class _PartialAccessor:
def __init__(self, param):
self.param = param

def __setitem__(self, key, value):
self.param._value.__setitem__(key, value)
self.param.set_value()

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

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

@property
def set_partial(self):
"""
Use this property to set values to slices of the stored numpy array, using
any slicing methods that a numpy array accepts::
puzzle['piece'].params['image'].set_partial[100:200, :] = 128
This will call the param's setter if there's one, and in general
acts like :func:`~puzzlepiece.param.BaseParam.set_value`.
"""
return self._partial_accessor

def _make_input(self, value=None, connect=None):
"""
:meta private:
Expand All @@ -430,7 +453,27 @@ def _input_set_value(self, value):
"""
:meta private:
"""
self.input.setText(self._format_array(value))
self.input.setText(self._format_array(value))

def _input_get_value(self):
"""
:meta private:
"""
return self._value

def set_value(self, value=None):
"""
This method overrides :func:`puzzlepiece.param.BaseParam.set_value`.
See there for documentation.
:meta private:
"""
# Small check to account for the label being set twice
# in super().set_value(value) when the
# value argument is None
if value is not None:
self._indicator_state = not self._indicator_state
return super().set_value(value)

def _format_array(self, value):
self._indicator_state = not self._indicator_state
Expand Down

0 comments on commit 4da4fa4

Please sign in to comment.