-
Notifications
You must be signed in to change notification settings - Fork 11
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
compare_view widget and colab support #41
Conversation
Hi @amorgun, |
I will have time for testing and reviewing this pr next weekend. |
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've now had some time to test this your implementation and gave some comments.
Thanks for your great work, I think this can be merged soon! :)
Another thing:
Maybe we can add this into the example notebook:
from jupyter_compare_view import compare
from skimage import data
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
img = data.chelsea()
grayscale_img = rgb2gray(img)
compare([img,grayscale_img], add_controls=False)
jupyter_compare_view/compare.py
Outdated
ImageLike = typing.TypeVar('ImageLike') | ||
|
||
|
||
def img2bytes(img: ImageLike, format: str = 'jpeg') -> bytes: |
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.
Is there a reason to choose 'jpeg' over 'png' here ?
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.
No particular reason. But now I added it as a compare
argument so it can be altered.
jupyter_compare_view/compare.py
Outdated
# anything other that can be displayed with plt.imshow | ||
import matplotlib.pyplot as plt | ||
|
||
plt.imsave(im_file, img, format=format) |
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.
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 think it's better to respect rcParams['image.cmap']
so I added cmap
argument to compare
.
* Update __init__.py * Update compare.py * Update sw_cellmagic.py * Update compare.py * Update compare.py * add widget exaple * Update compare.py * Add cmap arg
The widget is really great now! Thanks for these quick changes. %%compare --config '{"circle_size": 30}'
plt.imshow(img)
plt.axis("off")
plt.show()
plt.imshow(grayscale_img, cmap="gray")
plt.axis("off")
plt.show()
|
Thanks for these changes. |
* Update sw_cellmagic.py * Update sw_cellmagic.py * Update compare.py
I think cell magic is fixed now. I tested it in colab and JupyterLab, but I don't have VSCode configured to test it. |
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.
Hey!
This looks great and provides basically all of the functionality I was after in #16 and I definitely recommend merging and releasing :)
That said, I think it's important to note that this is not actually making an ipywidget
. The simple way to see this is that the return object does not inherit from ipywidgets.Widget
. The practical consequence of this is two fold:
First, It may be difficult to embed the compare view into a larger widget layout.
Second, there is no way to interact programmatically with the display, and you cannot re-use the same object. The advantage of going to make a widget would be that the following code would all be possible with the compare_view updating in sync with the code:
import ipywidgets as widgets
from jupyter_compare_view import CompareView
# create object
comparer = CompareView(img1, img2)
# comparer is a subclass of Widget which lets you do things
# like embed it in a widget layout, and interact with parameters programmatically
assert isinstance(widgets.Widget, comparer)
display(widgets.HBox([widgets.Button(...), comparer]))
# query the current slider position:
print(comparer.slider_position)
# set slider position
comparer.slider_position = .75
# change control type
comparer.mode = "circle"
# swap in a different image
comparer.img2 = some_new_image
Those are all nice to be able to do, but I don't think that the lack of them significantly impedes utility here. So given that it would be a decent lift to turn this into a widget I'd suggest not doing it unless you wanted to learn how to make a widget (which is a valuable skill).
jupyter_compare_view/compare.py
Outdated
Args: | ||
add_controls: pass False to not create controls | ||
start_mode: either "circle", "horizonal" or "vertical" | ||
circumference_fraction: size of circle outline as fraction of image width or height (whatever is bigger) | ||
circle_size: the radius in pixel | ||
circle_fraction: a fraction of the image width or height (whichever is bigger—called max_size in this document) | ||
show_circle: draw line around circle | ||
slider_time: time slider takes to reach clicked location | ||
start_slider_pos: 0.0 -> left; 1.0 -> right | ||
show_slider: draw line at slider |
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.
Note that several parameters are not included in the docstring. And the return value is missing. Also it may be worth considering following the numpydoc style for consistentcy with the larger ecosystem of scientific and datascience packages.
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 added some missing parameters in the docstring. There is also a type hint for the return value. I think the current Google style docstring is fine, it's also widely used.
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.
Great work!
As soon as the conversations are resolved, this PR can be merged
Co-authored-by: Jan-Hendrik Müller <[email protected]>
* Update compare.py * Update example_notebook * Created using Colaboratory * Update README.md
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.
This is a very clean implementation in my opinion! I especially like that you're using types so frequently. Now we finally have a proper config-marshalling layer from Python to TypeScript. I really don't have anything to complain about, merge.
Thank you for your efforts!
Thank you all for making this possible! |
I just made the next release, @amorgun : Also, can you test this in google coolab? |
Sure, my twitter is https://twitter.com/amorgun2. |
Cool! |
I tried using this library in colab and encountered several issues:
import jupyter_compare_view
fails on version check.get_current_notebook
script does not work there. Changing selectors didn't help so I removed them altogether and embedded a copy ofcompare_view
in every result.It's the same issue as in the Images and image widget not appearing. #40, so this PR should also fix it.
I also added
compare
function to create a widget without the cell magic.