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

Load/make thumbnail for any 2D image #56

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/opera_utils/h5explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ def __dir__(self):
return list(self._attr_cache.keys())


def create_explorer_widget(hf: h5py.File, load_less_than: float = 1e3):
def create_explorer_widget(
hf: h5py.File,
load_less_than: float = 1e3,
subsample: tuple[int, int] = (10, 10),
cmap: str = "gray",
interpolation: str = "nearest",
):
"""Make a widget in Jupyter to explore a h5py file.

Requires `ipywidgets` and `matplotlib` to be installed.
Expand All @@ -88,10 +94,17 @@ def create_explorer_widget(hf: h5py.File, load_less_than: float = 1e3):
import ipywidgets as widgets
import matplotlib.pyplot as plt

sub_row, sub_col = subsample

def _make_thumbnail(image):
# Create a thumbnail of the dataset
fig, ax = plt.subplots(figsize=(5, 5))
ax.imshow(image, cmap="gray", vmax=np.nanpercentile(image, 99))
ax.imshow(
image,
cmap=cmap,
interpolation=interpolation,
vmax=np.nanpercentile(image, 99),
)
ax.axis("off")
buf = BytesIO()
plt.savefig(buf, format="png", dpi=150)
Expand Down Expand Up @@ -120,10 +133,12 @@ def _add_widgets(item, level: int = 0):
content += f"<br>Value: {item[()]}"
html_widget = widgets.HTML(content)

if not item.ndim == 2 or not item.dtype == np.complex64:
if not item.ndim == 2:
return html_widget
# If the dataset is a 2D complex array, make a thumbnail
image_widget = _make_thumbnail(np.abs(item[::5, ::10]))
# If the dataset is a 2D array, make a thumbnail
# Handle the real or complex the same
data = np.abs(item[::sub_row, ::sub_col])
image_widget = _make_thumbnail(data)
return widgets.VBox([image_widget, html_widget])

else:
Expand Down