question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable magicgui decorator on class member functions

See original GitHub issue

Hi Talley @tlambert03 ,

thanks again for your support recently on the forum. I think I have a related feature request. To simplify my code, it would be nice if one could use magicgui on class member functions. In this way, the function could have access to a broader context, which is not handled in the magicgui.

To illustrate what I mean, take a look at this code:

import napari

from magicgui import magicgui
from napari.layers import Image


class MyObject:
    def __init__(self):
        self.counter = 0

    @magicgui(auto_call=True)
    def process_image(self, image : Image, sigma: float = 5):
        self.counter = self.counter + sigma
        print(self)

# load data
from skimage.io import imread
image = imread('https://samples.fiji.sc/blobs.png')

# start up napari
with napari.gui_qt():
    viewer = napari.Viewer()
    viewer.add_image(image, name='blobs')

    # generate a Graphical User Interface from the function above magically
    gui = MyObject().process_image.Gui()
    viewer.window.add_dock_widget(gui)

When executing it and increasing the sigma in the magic user interface, this error is thrown:

ERROR:root:Unhandled exception:
Traceback (most recent call last):
  File "C:\Users\rober\miniconda3\lib\site-packages\magicgui\core.py", line 532, in __call__
    value = self.func(**_kwargs)
  File "C:/structure/code/pyclesperanto_prototype/demo/napari_gui/napari_magicgui.py", line 17, in process_image
    self.counter = self.counter + sigma
AttributeError: 'str' object has no attribute 'counter'

Thus, the self variable is not correctly set in such a context. Do you think this could be implemented? I’m also open to other solutions if you see some.

Thanks!

Cheers, Robert

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
tlambert03commented, Dec 14, 2020

omg! it already works on the current master!! 🎉 (one of those super happy moments when something you’ve worked on for a while just magically fixes a different problem)

Try checking out the master branch here and using this code:

import napari
from napari.layers import Image
from skimage.io import imread

from magicgui import magicgui


class MyObject:
    def __init__(self):
        self.counter = 0.0

    def process_image(self, image: Image, sigma: float = 5):
        self.counter += sigma
        print(self.counter, sigma)


image = imread("https://samples.fiji.sc/blobs.png")

# start up napari
with napari.gui_qt():
    viewer = napari.Viewer()
    viewer.add_image(image, name="blobs")

    m = MyObject()
    gui = magicgui(m.process_image, auto_call=True)
    viewer.window.add_dock_widget(gui)

1reaction
tlambert03commented, Dec 14, 2020

I’m actually going to leave this issue open, since the decorator syntax still won’t work, but it could be made to work and I like the idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a decorator of an instance method access the class?
The method decorator marks the method as one that is of interest by adding a "use_class" attribute - functions and methods are also...
Read more >
Problem accessing napari viewer with magic-class
Hi I have been using the magic-class library for generating guis in napari. I have been running into an error when using magic...
Read more >
magicgui - PyPI
Enable decorator to be used on methods #56 (tlambert03) add application_name variable #55 (tlambert03)
Read more >
The `persist` argument is ignored in `@magicgui(persist=True ...
I noticed that when I use @magicgui decorator on a class method, it becomes somewhat buggy and ignores some of its arguments. Noticeably,...
Read more >
magicgui.widgets._bases.container_widget - napari
Wraps a widget that implements :class:`~magicgui.widgets. ... is tightly coupled to a specific function signature (as in the "classic" magicgui decorator), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found