Repository: mehta-lab/recOrder (language: python)

Issue:
Sample contribution
Fixes #361, #370.

This .gif demonstrates the sample contributions (from local files for now) and demonstrates the birefringence overlay improvements in #426.
![output2](https://github.com/mehta-lab/recOrder/assets/9554101/8bd57e60-60e1-47a4-8775-d6f35895c0f7)


TODO:
- [x] upload files to Zenodo
- [x] make sample contribution buttons download the files (10 MB), unzip, and open 



Discussion / hints:

Fix patch:
diff --git a/recOrder/napari.yaml b/recOrder/napari.yaml
index 6a50dd08..1253a46f 100644
--- a/recOrder/napari.yaml
+++ b/recOrder/napari.yaml
@@ -8,6 +8,12 @@ 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
@@ -15,4 +21,11 @@ contributions:
   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
 
diff --git a/recOrder/scripts/samples.py b/recOrder/scripts/samples.py
new file mode 100644
index 00000000..1b09cb1a
--- /dev/null
+++ b/recOrder/scripts/samples.py
@@ -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


Test patch:
diff --git a/recOrder/tests/widget_tests/test_sample_contributions.py b/recOrder/tests/widget_tests/test_sample_contributions.py
new file mode 100644
index 00000000..2c583050
--- /dev/null
+++ b/recOrder/tests/widget_tests/test_sample_contributions.py
@@ -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()