diff --git a/fsl/data/image.py b/fsl/data/image.py index 17a4d5ab6c96714241dc420792ea4272e6235f9e..ba2ae2d78ee572ca17b38fa6a4ee7fb3ec9214d0 100644 --- a/fsl/data/image.py +++ b/fsl/data/image.py @@ -598,7 +598,7 @@ class Image(Nifti, notifier.Notifier): :class:`.Notifier` interface) on the ``'dataRange'`` topic. """ if not self.__suppressDataRange: - self.notify(notifier_topic='dataRange') + self.notify(topic='dataRange') def calcRange(self, sizethres=None): @@ -693,7 +693,7 @@ class Image(Nifti, notifier.Notifier): self.__dataSource = filename self.__saveState = True - self.notify(notifier_topic='saveState') + self.notify(topic='saveState') def __getitem__(self, sliceobj): @@ -739,14 +739,14 @@ class Image(Nifti, notifier.Notifier): if values.size > 0: - self.notify(notifier_topic='data') + self.notify(topic='data') if self.__saveState: self.__saveState = False - self.notify(notifier_topic='saveState') + self.notify(topic='saveState') if not np.all(np.isclose(oldRange, newRange)): - self.notify(notifier_topic='dataRange') + self.notify(topic='dataRange') ALLOWED_EXTENSIONS = ['.nii.gz', '.nii', '.img', '.hdr', '.img.gz', '.hdr.gz'] diff --git a/fsl/data/melodicimage.py b/fsl/data/melodicimage.py index 5229d8bc2b3be72de1a6615fb73bc01865eafd4a..0dada8dc9c37b49eafbefd4dd9884c16cf81aaa2 100644 --- a/fsl/data/melodicimage.py +++ b/fsl/data/melodicimage.py @@ -106,7 +106,7 @@ class MelodicImage(fslimage.Image): self.__tr = val if oldval != val: - self.notify(notifier_topic='tr') + self.notify(topic='tr') def getComponentTimeSeries(self, component): diff --git a/fsl/utils/notifier.py b/fsl/utils/notifier.py index b05aebc3de676b13a741ed4ed61f64cd6dc8d505..106161cdb1751716604a8e056b509ba3672dc42a 100644 --- a/fsl/utils/notifier.py +++ b/fsl/utils/notifier.py @@ -39,6 +39,7 @@ class Notifier(object): class, provided by the :mod:`props` package. """ + def __new__(cls, *args, **kwargs): """Initialises a dictionary of listeners on a new ``Notifier`` instance. @@ -57,13 +58,21 @@ class Notifier(object): """Register a listener with this ``Notifier``. :arg name: A unique name for the listener. - :arg callback: The function to call - must accept this ``Notifier`` - instance as its sole argument. + + :arg callback: The function to call - must accept two positional + arguments: + + - this ``Notifier`` instance. + + - A value, which may be ``None`` - see + :meth:`notify`. + :arg topic: Optional topic on which to listen for notifications. + :arg runOnIdle: If ``True``, this listener will be called on the main thread, via the :func:`.async.idle` function. Otherwise this function will be called directly by the - :meth:`notify` method. + :meth:`notify` method. Defaults to ``False``. """ if topic is None: @@ -132,9 +141,13 @@ class Notifier(object): The documented arguments must be passed as keyword arguments. - :args notifier_topic: The topic on which to notify. Default - listeners are always notified, regardless - of the specified topic. + :arg topic: The topic on which to notify. Default + listeners are always notified, regardless + of the specified topic. + + :arg value: A value passed through to the registered listener + functions. If not provided, listeners will be passed + a value of ``None``. All other arguments passed to this method are ignored. @@ -143,7 +156,8 @@ class Notifier(object): See :meth:`register`. """ - topic = kwargs.get('notifier_topic', DEFAULT_TOPIC) + topic = kwargs.get('topic', DEFAULT_TOPIC) + value = kwargs.get('value', None) listeners = [self.__listeners[topic]] if topic != DEFAULT_TOPIC: @@ -179,5 +193,5 @@ class Notifier(object): 'removing from list'.format(name)) ldict.pop(name) - elif runOnIdle: async.idle(callback, self) - else: callback(self) + elif runOnIdle: async.idle(callback, self, value) + else: callback( self, value)