Skip to content

Commit

Permalink
Sample contribution merge to main (#434)
Browse files Browse the repository at this point in the history
* first-pass live overlays

* correct colormap

* first-pass scale passing

* match scale shapes

* bug fixes

* warn about mismatched scales

* add slider

* working prototype slider

* refine gui elements

* contributions from local

* download from zenodo and unzip into temp

* change to platformdirs

* versioned sample data folder

* fix type annotation

* pass on windows

* squeeze scale with image

* Revert "squeeze scale with image"

This reverts commit d373a1e.
  • Loading branch information
talonchandler authored Aug 31, 2023
1 parent 2f5ae16 commit f2d5bb8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions recOrder/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ contributions:
- id: recOrder-napari.get_reader
title: Read ome-zarr and ome-tif files
python_name: recOrder.io._reader:napari_get_reader
- id: recOrder-napari.polarization_target_data
title: Polarization Target Data
python_name: recOrder.scripts.samples:read_polarization_target_data
- id: recOrder-napari.polarization_target_reconstruction
title: Polarization Target Data
python_name: recOrder.scripts.samples:read_polarization_target_reconstruction
readers:
- command: recOrder-napari.get_reader
accepts_directories: true
filename_patterns: ['*.zarr', '*.tif']
widgets:
- command: recOrder-napari.MainWidget
display_name: Main Menu
sample_data:
- command: recOrder-napari.polarization_target_data
key: polarization-target-data
display_name: Polarization Target Data
- command: recOrder-napari.polarization_target_reconstruction
key: polarization-target-reconstruction
display_name: Polarization Target Reconstruction

67 changes: 67 additions & 0 deletions recOrder/scripts/samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import shutil
from pathlib import Path

from iohub import open_ome_zarr
from napari.utils.notifications import show_warning
from platformdirs import user_data_dir
from wget import download


def download_and_unzip() -> tuple[Path]:
"""Downloads sample data .zip from zenodo, unzips, and returns Paths to the .zarr datasets.
Skips the download if the files already exist.
Uses platformdirs.user_data_dir to store data.
"""
temp_dirpath = Path(user_data_dir("recOrder-sample-v1.4"))
temp_dirpath.mkdir(exist_ok=True, parents=True)
data_dirpath = temp_dirpath / "sample_contribution"

if not data_dirpath.with_suffix(".zip").exists():
show_warning(
"Downloading 10 MB sample contribution. This might take a moment..."
)
data_url = "https://zenodo.org/record/8280720/files/sample_contribution.zip?download=1"
download(data_url, out=str(temp_dirpath))
if not data_dirpath.exists():
shutil.unpack_archive(
data_dirpath.with_suffix(".zip"), extract_dir=temp_dirpath
)

data_path = data_dirpath / "raw_data.zarr"
recon_path = data_dirpath / "reconstruction.zarr"
return data_path, recon_path


def read_polarization_target_data():
"""Returns the polarization data sample contribution"""
data_path, _ = download_and_unzip()

dataset = open_ome_zarr(data_path)
layer_list = []
for channel_index, channel_name in enumerate(dataset.channel_names):
position = dataset["0/0/0"]
data = (position["0"][0, channel_index],)
layer_dict = {"name": channel_name, "scale": position.scale[3:]}
layer_list.append((data, layer_dict))

return layer_list


def read_polarization_target_reconstruction():
"""Returns the polarization target reconstruction sample contribution"""

_, recon_path = download_and_unzip()
dataset = open_ome_zarr(recon_path)

layer_list = []
for channel_index, channel_name in enumerate(
["Retardance", "Orientation"]
):
position = dataset["0/0/0"]
data = (position["0"][0, channel_index],)
layer_dict = {"name": channel_name, "scale": position.scale[3:]}
layer_list.append((data, layer_dict))

return layer_list
8 changes: 8 additions & 0 deletions recOrder/tests/widget_tests/test_sample_contributions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from recOrder.scripts.samples import download_and_unzip


def test_download_and_unzip():
p1, p2 = download_and_unzip()

assert p1.exists()
assert p2.exists()

0 comments on commit f2d5bb8

Please sign in to comment.