Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about get_video convention to ImagingExtractor #244

Merged
merged 9 commits into from
Oct 19, 2023

Conversation

h-mayorquin
Copy link
Collaborator

Related to #156

The ideal will be to add this in a section of the read the docs but we are far from it. The standard should be written somewhere and this is a place.

@h-mayorquin h-mayorquin added the documentation Improvements or additions to documentation label Sep 15, 2023
Copy link
Member

@pauladkisson pauladkisson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, get_video only returns 3D numpy ndarrays (frames, height, width). Depth is handled only for some extractors, so they should override their get_video docstring to specify how it is treated.

@h-mayorquin
Copy link
Collaborator Author

Every method needs to override the whole function and therefore also the docstring as this is an abstract method. In other words, this is not user facing documentation misguiding the users but documentation for developers with instructions of a common pitfall when they overide the method. In that sense, I think it is better served if it has the more general case, more information is better than less in this case.

@pauladkisson
Copy link
Member

Every method needs to override the whole function and therefore also the docstring as this is an abstract method.

Most of the current ImagingExtractors return video exactly as it's described in the base class, so they can inherit the docstring without needing to rewrite it. I believe that is one of the pros of defining an abstract get_video function.

In that sense, I think it is better served if it has the more general case, more information is better than less in this case.

More information can be helpful for devs, but let's at least mention that the return video can be 3D or 4D depending on whether the extractor defines depth.

@h-mayorquin
Copy link
Collaborator Author

Every method needs to override the whole function and therefore also the docstring as this is an abstract method.

Most of the current ImagingExtractors return video exactly as it's described in the base class, so they can inherit the docstring without needing to rewrite it. I believe that is one of the pros of defining an abstract get_video function.

Maybe you have a plan that I don't understand. How are you going to inherit the docstring of the abstract method?

This seems like a non-standard trick, is that something that you guys have discussed, @CodyCBakerPhD ?

@pauladkisson
Copy link
Member

How are you going to inherit the docstring of the abstract method?

The child class inherits the docstrings of the parent methods automatically even if it overrides the body of the method -- as long as it doesn't define its own docstring.

@h-mayorquin
Copy link
Collaborator Author

h-mayorquin commented Sep 19, 2023

It does not seem that this is true to me, can you reference this or show me this behavior? If it is, what's missing in the code below?

from abc import ABC, abstractmethod

class AbstractClass(ABC):
    """
    This is the AbstractClass's docstring.
    """
    
    @abstractmethod
    def abstract_method(self):
        """
        This is the abstract_method's docstring.
        """
        pass
    
    @abstract_method
    def another_abstract_method(self):
        """
        This is the another_abstract_method's docstring.
        """
        pass
    
    def a_normal_method(self):
        """Parent docstring"""

class ChildClass(AbstractClass):
    
    def abstract_method(self):
        return "Implemented in ChildClass"

    def another_abstract_method(self):
        """This one has a docstring"""
        return "Implemented in ChildClass"
    
    def a_normal_method(self):
        return "Do I override the parent's docstring?"
# Test
child = ChildClass()
print(child.abstract_method())
print(child.abstract_method.__doc__)
print(child.another_abstract_method.__doc__)
print(child.a_normal_method())
print(child.a_normal_method.__doc__)```

Output:

Implemented in ChildClass
None
This one has a docstring
Do I override the parent's docstring?
None

@h-mayorquin
Copy link
Collaborator Author

It would be nice to get a response for this, @pauladkisson. I think that you are wrong in your claim above.

@CodyCBakerPhD
Copy link
Member

CodyCBakerPhD commented Sep 20, 2023

@h-mayorquin Note that directly accessing the __doc__ is not how we generally instruct users to view docstrings on instances of objects

Check out our recommended ways

child.abstract_method?
help(child.abstract_method)

This discrepancy is still good for all of us to take note of

@h-mayorquin
Copy link
Collaborator Author

That seems convicing to me:

image

I will implement the requested changes.

@pauladkisson
Copy link
Member

@h-mayorquin, sorry for the delayed response. Basically, as Cody pointed out, dynamic docstring look-up via help finds inherited docstrings, even though __doc__ doesn't. We are taking advantage of that functionality to only write docstrings for the base class methods (get_num_channels, get_image_size, etc.) in the base class implementation UNLESS a child class specifically changes its inputs/outputs, in which case it would need to write its own docstring for the altered method.

@h-mayorquin
Copy link
Collaborator Author

That's fine, this was useful to me. I was not aware that python performs attribute lookup even for attributes of the class' methods when using the help builtin. At the beginning I thought that this was ipython / jupyter behavior but I also tried on the python REPL and is the same behavior.

@h-mayorquin
Copy link
Collaborator Author

Changed this.

Copy link
Member

@pauladkisson pauladkisson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One formatting change otherwise LGTM

src/roiextractors/imagingextractor.py Outdated Show resolved Hide resolved
@pauladkisson pauladkisson merged commit 8aa0c11 into main Oct 19, 2023
14 checks passed
@pauladkisson pauladkisson deleted the add_some_docstrings branch October 19, 2023 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants