-
Notifications
You must be signed in to change notification settings - Fork 615
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
Make HistogramVisual updatable after instantiation #2555
base: main
Are you sure you want to change the base?
Conversation
Oh and does this need tests? |
Co-authored-by: David Hoese <[email protected]>
…vispy into updateable-histogram
thanks for the comments @djhoese! Updated the typing and docs. haven't added a test yet. Technically, it should have the same coverage as before, since it's mostly just refactoring existing functionality into separate methods. but I'll have a look at the existing tests and see if I can extend them easily |
ok, updated the test here to instantiate without data, and use the set_raw_data method to update the plot. |
vispy/visuals/histogram.py
Outdated
data : array-like, optional | ||
Data to histogram. May be `None` on initialization, use `set_raw_data` | ||
to set data after initialization. | ||
bins : int | array-like | str | ||
If `bins` is an int, it defines the number of equal-width | ||
bins in the given range (10, by default). If `bins` is a | ||
sequence, it defines a monotonically increasing array of bin edges, | ||
including the rightmost edge, allowing for non-uniform bin widths. | ||
May also be a string if the calc_hist function supports it. | ||
color : str | Color | ||
Color of the faces in the histogram mesh. | ||
orientation : {'h', 'v'} | ||
Orientation of the histogram. | ||
calc_hist : Callable | ||
Function that computes the histogram. Must accept two positional arguments | ||
(data, bins) and return (hist_data, bin_edges). Default is numpy.histogram. | ||
**kwargs : dict | ||
Keyword arguments to pass to `MeshVisual`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on removing the type definitions in the docstring? We'd have to check the generated docs, but I'm pretty sure sphinx should handle this fine and include the types in the API docs. Not sure it is needed in the docstring otherwise, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't have any strong thoughts on that. I'd say it's a project-specific decision, and I can see arguments for and against. but, I'm happy to do whatever you want! if vispy is moving in a particular direction with docstrings and you want to use this PR to update this particular module, that's fine, just let me know how you want it to look :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brisvag Thoughts? @tlambert03 what have you seen other projects do? It seems redundant to have you put this much work into defining the type annotations with TypeAliases and then have to type them again in the docstrings. Could you try removing them and generating the docs locally? Or if you want, push here and we can download the website artifact from CI and view it.
cd doc
make html_dev-pattern PATTERN=abcd
^ this should generate the docs but skip all the examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's redundant. but I do still think it's slightly a matter of opinion. I think you'll find some holdouts who feel like docstrings are for humans and type annotations are for machines, and that docstrings are "sacred" in a sense... people in that camp would probably say "go ahead and use type annotations if you want, but leave my docstrings out of it". They might additionally argue that strictly accurate type annotations can become verbose at the expense of human readability. then there's strict DRY camp that says it should only ever be in one place, even if it possibly requires someone to look in a different place than they might be accustomed to in interactive usage. Or those who simply feel that type annotations are more standardized than what people tend to put in their docstrings...
(both might argue that they don't care how your rendered HTML docs look, or whether you're using sphinx type hints to achieve the same final result on your webpage)
i kinda like the modern type-hints only approach... but more than anything this feels like a repo-wide discussion, and not something I would try to change incrementally in each PR that happens to touch a docstring. I added the "regular" docs to match the rest of vispy, I added the type hints after your comment suggested to add them ... I think this general discussion belongs somewhere outside of this PR :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think my thing is that I typically either am looking at API docs on the website or I'm telling my IDE to pull them up for me. I rarely open the docstring version of the docs in my IDE and only pull up the "show me the parameters and their types" view inline as I'm typing. I think PyCharm is smart enough to put type annotations where it needs to in these cases. Anyway...I don't think vispy is maintained well enough to do this repo-wide so I personally would be OK with it being done here as a test...assuming it renders properly in sphinx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tlambert03 Maybe that leaves it up to you then since we're asking you to do the work. I think I'd prefer type annotations only, @brisvag seems to have a slight preference for docstrings (human-readable). I don't think NOT having type annotations is a reasonable option so the choice is both or annotations only. Again, I vote for annotations only and we see how it goes with user complaints (if any).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, so just so I'm clear. you want me to add type annotations to parts of the code didn't previously have them? (like that one method in plotwidget)? You're ok with just that single method having type annotations? or you want me to add it to the whole module? (this is why i find this all a bit confusing... i imagined this would be a quick PR that just moves a little existing code around to allow it to be called as a method 😄 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it looks like what we have here already will do just fine (better than anything we had before anyways :P)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought is since you added type annotations for every argument in the __init__
that it made sense to remove them from the docstring for __init__
/the class. This is inline with my opinion that docstring types aren't necessary if type annotations are there.
BUT I don't think other future PRs where someone adds a single new kwarg and puts a type annotation on it means that they should remove types from the docstrings. I think it is an all-or-nothing kind of thing on a per-method basis (maybe a per class).
So I think I'd prefer removing the docstring types for any documented method in this file that you already modified and added type annotations for. If it doesn't have type annotations or you didn't modify it then feel free to leave it as-is. I see this as more of an experiment than a complete cleanup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, will do. I believe I added type annotations in the __init__
in response to a previous comment ... it wasn't in the original PR, which largely aimed to match the existing code patterns.
I'll go ahead and remove docstring types in HistogramVisual
Co-authored-by: David Hoese <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
vispy/visuals/histogram.py
Outdated
data : array-like, optional | ||
Data to histogram. May be `None` on initialization, use `set_raw_data` | ||
to set data after initialization. | ||
bins : int | array-like | str | ||
If `bins` is an int, it defines the number of equal-width | ||
bins in the given range (10, by default). If `bins` is a | ||
sequence, it defines a monotonically increasing array of bin edges, | ||
including the rightmost edge, allowing for non-uniform bin widths. | ||
May also be a string if the calc_hist function supports it. | ||
color : str | Color | ||
Color of the faces in the histogram mesh. | ||
orientation : {'h', 'v'} | ||
Orientation of the histogram. | ||
calc_hist : Callable | ||
Function that computes the histogram. Must accept two positional arguments | ||
(data, bins) and return (hist_data, bin_edges). Default is numpy.histogram. | ||
**kwargs : dict | ||
Keyword arguments to pass to `MeshVisual`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it looks like what we have here already will do just fine (better than anything we had before anyways :P)
Wait...those docstrings don't match the current state of your PR. WTF Edit: Side note, you need two backticks around literals to go from italics to literal code block. Like:
|
I'm going to rerun the documentation building job to see if the docstrings make more sense. Otherwise, @tlambert03 if you have time you could generate the docs locally and see if the API docs docstrings match for you. |
@tlambert03 I seem to not have push permissions on your fork. Any idea why? |
hmm, I'm not sure. I do have the box checked to allow edits by maintainers, and it is from a branch in my personal fork... but I have had this happen before in PRs to vispy... so I wonder if there's something wrong about my fork. sorry about that. will try to build docs locally |
I did end up making a PR in your fork. If you merge that there should be some better handling of type annotations by sphinx. |
Add sphinx type hint extension
Did you change something? I was able to make a commit now. This time I did it in github's basic editor, before I was doing it in a "github codespace". |
Ok so issues:
My understanding is that the |
Lines 146 to 226 in 6667599
|
Can we go back to my original PR that simply moves the init logo into methods, using the existing patterns, and consider possible changes to how vispy build the docs and how type hints play a role in another issue? |
Sure. Could you please copy this existing branch to a new name and make a new PR and describe it as adding the ability to use type annotations in sphinx docs or something like that? I'd like to not lose what's here already. I suppose the best solution here would be to add the typing back into the docstring and then in |
adds
set_raw_data
method toHistogramVisual
to allow data to be updated after the initial instantiation. Also allows the user to customize the method used to calculate the histogram (defaults tonumpy.histogram
)