Skip to content

Commit

Permalink
Merge pull request #8 from funkelab/v0.0.3
Browse files Browse the repository at this point in the history
Add demo video and interactivity
  • Loading branch information
lmanan authored Apr 1, 2024
2 parents 549705d + 0457b80 commit d4b3906
Show file tree
Hide file tree
Showing 16 changed files with 547 additions and 265 deletions.
6 changes: 2 additions & 4 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include LICENSE
include README.md

include src/napari_cellulus/sample_data/*.npy
include src/napari_cellulus/napari.yaml
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

<h2 align="center">A napari plugin for cellulus</h2>



- **[Introduction](#introduction)**
- **[Installation](#installation)**
- **[Getting Started](#getting-started)**
Expand Down Expand Up @@ -55,8 +58,10 @@ Run the following commands in a terminal window:
conda activate napari-cellulus
napari
```
[demo_cellulus.webm](https://github.com/funkelab/napari-cellulus/assets/34229641/35cb09de-c875-487d-9890-86082dcd95b2)



Next, select `Cellulus` from the `Plugins` drop-down menu.

### Citation

Expand Down
8 changes: 4 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ package_dir =
[options.packages.find]
where = src

[options.package_data]
napari_cellulus = *.npy
* = *.yaml

[options.entry_points]
napari.manifest =
napari-cellulus = napari_cellulus:napari.yaml
Expand All @@ -58,7 +62,3 @@ testing =
pytest-qt # https://pytest-qt.readthedocs.io/en/latest/
napari
pyqt5


[options.package_data]
* = *.yaml
4 changes: 2 additions & 2 deletions src/napari_cellulus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__version__ = "0.1.0"

from .sample_data import tissue_net_sample
from .load_sample_data import load_fluo_c2dl_huh7_sample

__all__ = ("tissue_net_sample",)
__all__ = ("load_fluo_c2dl_huh7_sample",)
21 changes: 17 additions & 4 deletions src/napari_cellulus/datasets/napari_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __setup_pipeline(self):
)
+ gp.Unsqueeze([self.raw], 0)
+ gp.RandomLocation()
+ gp.Normalize(self.raw, factor=self.normalization_factor)
)
else:
self.pipeline = (
Expand All @@ -81,6 +82,7 @@ def __setup_pipeline(self):
spatial_dims=self.spatial_dims,
)
+ gp.RandomLocation()
+ gp.Normalize(self.raw, factor=self.normalization_factor)
)

def __iter__(self):
Expand Down Expand Up @@ -236,9 +238,20 @@ def sample_coordinates(self):
return anchor_samples, reference_samples

def get_num_anchors(self):
return int(
self.density * self.unbiased_shape[0] * self.unbiased_shape[1]
)
if self.num_spatial_dims == 2:
return int(
self.density * self.unbiased_shape[0] * self.unbiased_shape[1]
)
elif self.num_spatial_dims == 3:
return int(
self.density
* self.unbiased_shape[0]
* self.unbiased_shape[1]
* self.unbiased_shape[2]
)

def get_num_references(self):
return int(self.density * self.kappa**2 * np.pi)
if self.num_spatial_dims == 2:
return int(self.density * np.pi * self.kappa**2)
elif self.num_spatial_dims == 3:
return int(self.density * 4 / 3 * np.pi * self.kappa**3)
1 change: 1 addition & 0 deletions src/napari_cellulus/datasets/napari_image_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(
self, image: Image, key: gp.ArrayKey, spec: ArraySpec, spatial_dims
):
self.array_spec = spec

self.image = gp.Array(
data=normalize(
image.data.astype(np.float32), 1, 99.8, axis=spatial_dims
Expand Down
25 changes: 25 additions & 0 deletions src/napari_cellulus/load_sample_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pathlib import Path

import numpy as np

FLUO_C2DL_HUH7_SAMPLE_PATH = (
Path(__file__).parent / "sample_data/Fluo-C2DL-Huh7-sample.npy"
)


def load_fluo_c2dl_huh7_sample():
raw = np.load(FLUO_C2DL_HUH7_SAMPLE_PATH)
num_samples = raw.shape[0]
indices = np.random.choice(np.arange(num_samples), 5, replace=False)
raw = raw[indices]

return [
(
raw,
{
"name": "Raw",
"metadata": {"axes": ["s", "c", "y", "x"]},
},
"image",
)
]
48 changes: 48 additions & 0 deletions src/napari_cellulus/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import torch


class Model(torch.nn.Module):
"""
This class is a wrapper on the model object returned by cellulus.
It updates the `forward` function and handles cases when the input raw
image is not (S, C, (Z), Y, X) type.
"""

def __init__(self, model, selected_axes):
super().__init__()
self.model = model
self.selected_axes = selected_axes

def forward(self, raw):
if "s" in self.selected_axes and "c" in self.selected_axes:
pass
elif "s" in self.selected_axes and "c" not in self.selected_axes:

raw = torch.unsqueeze(raw, 1)
elif "s" not in self.selected_axes and "c" in self.selected_axes:
pass
elif "s" not in self.selected_axes and "c" not in self.selected_axes:
raw = torch.unsqueeze(raw, 1)
return self.model(raw)

@staticmethod
def select_and_add_coordinates(outputs, coordinates):
selections = []
# outputs.shape = (b, c, h, w) or (b, c, d, h, w)
for output, coordinate in zip(outputs, coordinates):
if output.ndim == 3:
selection = output[:, coordinate[:, 1], coordinate[:, 0]]
elif output.ndim == 4:
selection = output[
:, coordinate[:, 2], coordinate[:, 1], coordinate[:, 0]
]
selection = selection.transpose(1, 0)
selection += coordinate
selections.append(selection)

# selection.shape = (b, c, p) where p is the number of selected positions
return torch.stack(selections, dim=0)

def set_infer(self, p_salt_pepper, num_infer_iterations, device):
self.model.eval()
self.model.set_infer(p_salt_pepper, num_infer_iterations, device)
20 changes: 7 additions & 13 deletions src/napari_cellulus/napari.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
name: napari-cellulus
display_name: Cellulus
display_name: napari-cellulus
contributions:
commands:
- id: napari-cellulus.tissue_net_sample
python_name: napari_cellulus.sample_data:tissue_net_sample
title: Load sample data from Cellulus
- id: napari-cellulus.fluo_n2dl_hela_sample
python_name: napari_cellulus.sample_data:fluo_n2dl_hela_sample
title: Load sample data from Cellulus
- id: napari-cellulus.load_fluo_c2dl_huh7_sample
python_name: napari_cellulus.load_sample_data:load_fluo_c2dl_huh7_sample
title: Load sample data
- id: napari-cellulus.Widget
python_name: napari_cellulus.widget:Widget
title: Cellulus
sample_data:
- command: napari-cellulus.tissue_net_sample
display_name: TissueNet
key: tissue_net_sample
- command: napari-cellulus.fluo_n2dl_hela_sample
display_name: Fluo-N2DL-HeLa
key: fluo_n2dl_hela_sample
- command: napari-cellulus.load_fluo_c2dl_huh7_sample
display_name: Fluo-C2DL-Huh7
key: load_fluo_c2dl_huh7_sample
widgets:
- command: napari-cellulus.Widget
display_name: Cellulus
45 changes: 0 additions & 45 deletions src/napari_cellulus/sample_data.py

This file was deleted.

Binary file not shown.
Empty file.
Binary file removed src/napari_cellulus/sample_data/fluo_n2dl_hela.tif
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit d4b3906

Please sign in to comment.