-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add facet_col and animation_frame argument to imshow #2746
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
afb5c4d
use init_figure from main px core
emmanuelle 8be8ca0
WIP: add facet_col arg to imshow
emmanuelle d236bc2
animations work for grayscale images, with or without binary string
emmanuelle c8e852e
animations now work + tests
emmanuelle 12cec34
docs on facets and animations + add subplots titles
emmanuelle ab427ae
Merge branch 'master' into imshow-animation
emmanuelle 7a3a9f4
solved old unnoticed conflict
emmanuelle b689a2f
attempt to use imshow with binary strings and xarrays
emmanuelle fbb3f65
added test
emmanuelle 882810f
animation work for xarrays, still need to fix slider label
emmanuelle ba65990
added test with xarray and animations
emmanuelle cf644e5
added doc
emmanuelle 72674b7
added pooch to doc requirements
emmanuelle bd42385
Update packages/python/plotly/plotly/express/_imshow.py
emmanuelle fc2375b
Update doc/python/imshow.md
emmanuelle a431fad
remove commented-out code
emmanuelle b652039
animation + facet kinda working now, but it broke labels
emmanuelle 59c6622
added test
emmanuelle c7285a3
simplified code
emmanuelle 91c066e
simplified code
emmanuelle ac5aa1f
polished code and added doc example
emmanuelle 36b9f98
Merge branch 'imshow-animation' of https://github.com/plotly/plotly.p…
emmanuelle 8cdc6af
updated doc
emmanuelle cf1c2b9
Merge branch 'master' into imshow-animation
emmanuelle 5d1d8d8
add facet_col_spacing and facet_row_spacing
emmanuelle c27f88a
modify error message + animation_frame label
emmanuelle 502fdfd
improve code readibility
emmanuelle 135b01b
added example with sequence of images
emmanuelle 6ac3e36
typoe
emmanuelle a5a2252
label names
emmanuelle 77cb5cd
label name
emmanuelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added doc
- Loading branch information
commit cf644e55f6824e29faf821b38c09dabe81ce6d23
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -403,7 +403,7 @@ fig.show() | |||||
|
|
||||||
| *Introduced in plotly 4.11* | ||||||
|
|
||||||
| For three-dimensional image datasets, obtained for example by MRI or CT in medical imaging, one can explore the dataset by representing its different planes as facets. The `facet_col` argument specifies along which axes the image is sliced through to make the facets. With `facet_col_wrap` , one can set the maximum number of columns. | ||||||
| For three-dimensional image datasets, obtained for example by MRI or CT in medical imaging, one can explore the dataset by representing its different planes as facets. The `facet_col` argument specifies along which axes the image is sliced through to make the facets. With `facet_col_wrap` , one can set the maximum number of columns. For image datasets passed as xarrays, it is also possible to give an axis name as a string for `facet_col`. | ||||||
|
|
||||||
| It is recommended to use `binary_string=True` for facetted plots of images in order to keep a small figure size and a short rendering time. | ||||||
|
|
||||||
|
|
@@ -455,12 +455,14 @@ fig.show() | |||||
|
|
||||||
| *Introduced in plotly 4.11* | ||||||
|
||||||
| *Introduced in plotly 4.11* | |
| *Introduced in plotly 4.13* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,23 +288,23 @@ def imshow( | |
| args = locals() | ||
| apply_default_cascade(args) | ||
| labels = labels.copy() | ||
| col_labels = [] | ||
| nslices = 1 | ||
| if facet_col is not None: | ||
| if isinstance(facet_col, str): | ||
| facet_col = img.dims.index(facet_col) | ||
| nslices = img.shape[facet_col] | ||
| ncols = int(facet_col_wrap) if facet_col_wrap is not None else nslices | ||
| nrows = nslices // ncols + 1 if nslices % ncols else nslices // ncols | ||
| col_labels = ["plane = %d" % i for i in range(nslices)] | ||
| else: | ||
| nrows = 1 | ||
| ncols = 1 | ||
| if animation_frame is not None: | ||
| if isinstance(animation_frame, str): | ||
| animation_frame = img.dims.index(animation_frame) | ||
| nslices = img.shape[animation_frame] | ||
| slice_through = (facet_col is not None) or (animation_frame is not None) | ||
| plane_label = None | ||
| fig = init_figure(args, "xy", [], nrows, ncols, col_labels, []) | ||
| slice_label = None | ||
| slices = range(nslices) | ||
| # ----- Define x and y, set labels if img is an xarray ------------------- | ||
| if xarray_imported and isinstance(img, xarray.DataArray): | ||
| # if binary_string: | ||
|
|
@@ -314,13 +314,12 @@ def imshow( | |
| # "`img.values`" | ||
| # ) | ||
| dims = list(img.dims) | ||
| print(dims) | ||
| if slice_through: | ||
| slice_index = facet_col if facet_col is not None else animation_frame | ||
| slices = img.coords[img.dims[slice_index]].values | ||
| _ = dims.pop(slice_index) | ||
| plane_label = img.dims[slice_index] | ||
| slice_label = img.dims[slice_index] | ||
| y_label, x_label = dims[0], dims[1] | ||
| print(y_label, x_label) | ||
| # np.datetime64 is not handled correctly by go.Heatmap | ||
| for ax in [x_label, y_label]: | ||
| if np.issubdtype(img.coords[ax].dtype, np.datetime64): | ||
|
|
@@ -335,8 +334,8 @@ def imshow( | |
| labels["x"] = x_label | ||
| if labels.get("y", None) is None: | ||
| labels["y"] = y_label | ||
| if labels.get("plane", None) is None: | ||
| labels["plane"] = plane_label | ||
| if labels.get("slice", None) is None: | ||
| labels["slice"] = slice_label | ||
| if labels.get("color", None) is None: | ||
| labels["color"] = xarray.plot.utils.label_from_attrs(img) | ||
| labels["color"] = labels["color"].replace("\n", "<br>") | ||
|
|
@@ -378,7 +377,7 @@ def imshow( | |
| img = np.moveaxis(img, animation_frame, 0) | ||
| animation_frame = True | ||
| args["animation_frame"] = ( | ||
| "plane" if labels.get("plane") is None else labels["plane"] | ||
| "slice" if labels.get("slice") is None else labels["slice"] | ||
| ) | ||
|
|
||
| # Default behaviour of binary_string: True for RGB images, False for 2D | ||
|
|
@@ -531,6 +530,14 @@ def imshow( | |
| % str(img.shape) | ||
| ) | ||
|
|
||
| # Now build figure | ||
| col_labels = [] | ||
| if facet_col is not None: | ||
| slice_label = "slice" if labels.get("slice") is None else labels["slice"] | ||
| if slices is None: | ||
| slices = range(nslices) | ||
| col_labels = ["%s = %d" % (slice_label, i) for i in slices] | ||
| fig = init_figure(args, "xy", [], nrows, ncols, col_labels, []) | ||
| layout_patch = dict() | ||
| for attr_name in ["height", "width"]: | ||
| if args[attr_name]: | ||
|
|
@@ -541,11 +548,11 @@ def imshow( | |
| layout_patch["margin"] = {"t": 60} | ||
|
|
||
| frame_list = [] | ||
| for index, trace in enumerate(traces): | ||
| for index, (slice_index, trace) in enumerate(zip(slices, traces)): | ||
| if facet_col or index == 0: | ||
| fig.add_trace(trace, row=nrows - index // ncols, col=index % ncols + 1) | ||
| if animation_frame: | ||
| frame_list.append(dict(data=trace, layout=layout, name=str(index))) | ||
| frame_list.append(dict(data=trace, layout=layout, name=str(slice_index))) | ||
| if animation_frame: | ||
| fig.frames = frame_list | ||
| fig.update_layout(layout) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a bit odd to have |
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.