Skip to content

Commit 575fd45

Browse files
feat: expose CosemLabel as option in simulation (#18)
* cosem views * wip * wip * wip * wip * style(pre-commit.ci): auto fixes [...] * add download * cleanup * style(pre-commit.ci): auto fixes [...] * better bin * faster download * working rebin * style(pre-commit.ci): auto fixes [...] * misc * lots of stuff to run a cosem generation * wip * misc changes * minor change * update example * update * misc changes * fix: remove caching on bins * remove self type * linting * minor changes * fallback to tight_layout * rename cosem sample * style(pre-commit.ci): auto fixes [...] * changes * pin griffe --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 652fc1d commit 575fd45

28 files changed

+436
-48
lines changed

examples/basic_confocal.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
from microsim import schema as ms
44
from microsim.util import ndview, ortho_plot
55

6-
GreenMats = ms.FluorophoreDistribution(
7-
distribution=ms.MatsLines(density=0.5, length=30, azimuth=5, max_r=1),
8-
fluorophore="EGFP",
9-
)
10-
116
sim = ms.Simulation(
12-
truth_space=ms.ShapeScaleSpace(shape=(128, 512, 512), scale=(0.02, 0.01, 0.01)),
13-
output_space={"downscale": 8},
14-
sample=ms.Sample(labels=[GreenMats]),
7+
truth_space=ms.ShapeScaleSpace(shape=(64, 256, 256), scale=(0.04, 0.02, 0.02)),
8+
output_space={"downscale": 4},
9+
sample=ms.Sample(
10+
labels=[
11+
ms.FluorophoreDistribution(
12+
distribution=ms.MatsLines(density=0.5, length=30, azimuth=5, max_r=1),
13+
fluorophore="EGFP",
14+
)
15+
]
16+
),
1517
modality=ms.Confocal(pinhole_au=0.5),
1618
settings=ms.Settings(random_seed=100, max_psf_radius_aus=8),
1719
detector=ms.CameraCCD(qe=0.82, read_noise=2, bit_depth=12),

examples/cosem.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from microsim import schema as ms
2+
from microsim.schema.optical_config import lib
3+
from microsim.util import ndview
4+
5+
sim = ms.Simulation(
6+
# note: this is a rather coarse simulation, but it's fast
7+
# scale should be a one of .004 * 2^n, where n is an integer from 0 to 4
8+
# space basically determines the field of view.
9+
truth_space=ms.ShapeScaleSpace(shape=(52, 512, 512), scale=(0.064, 0.064, 0.064)),
10+
output_space={"downscale": 2},
11+
sample=ms.Sample(
12+
labels=[
13+
# pick dataset and layer name from https://openorganelle.janelia.org/datasets
14+
ms.FluorophoreDistribution(
15+
distribution=ms.CosemLabel(dataset="jrc_hela-3", label="er-mem_pred"),
16+
fluorophore="EGFP",
17+
),
18+
ms.FluorophoreDistribution(
19+
distribution=ms.CosemLabel(dataset="jrc_hela-3", label="mito-mem_pred"),
20+
fluorophore="mCherry",
21+
),
22+
]
23+
),
24+
channels=[lib.FITC, lib.DSRED],
25+
modality=ms.Widefield(),
26+
detector=ms.CameraCCD(qe=0.82, read_noise=6),
27+
settings=ms.Settings(max_psf_radius_aus=2),
28+
)
29+
30+
ndview(sim.digital_image())

examples/notebooks/cosem.ipynb

Lines changed: 138 additions & 0 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ docs = [
7272
"mkdocs-material==9.5.17",
7373
"mkdocstrings ==0.24.3",
7474
"mkdocstrings-python ==1.9.2",
75+
"griffe==0.45.3",
7576
"griffe-fieldz",
7677
]
7778

src/microsim/cosem/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _supabase(url: str | None = None, key: str | None = None) -> supabase.Client
7171
text = response.read().decode("utf-8")
7272
key = text.split("SUPABASE_KEY:")[1].split(",")[0].strip("\"'")
7373
url = text.split("SUPABASE_URL:")[1].split(",")[0].strip("\"'")
74-
return Client(url, key) # type: ignore
74+
return Client(url, key)
7575

7676

7777
@cache

src/microsim/cosem/_tstore.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ def _sum_bin_chunk(
262262
dest_slice = tuple(slice(*x) for x in dest)
263263
block = in_arr[src_slice]
264264
# TODO: see if tensorstore has a better way to check if a block is empty
265-
if np.any(block): # type: ignore
265+
if np.any(block):
266266
binned = block_bin(block.translate_to[0, 0, 0])
267267
if np.any(binned):
268268
# note, we could also use .write(), which is async...
269269
# but we're already in a thread, so it's not necessary?
270-
out_arr[dest_slice] = binned.astype(out_arr.dtype.numpy_dtype) # type: ignore
270+
out_arr[dest_slice] = binned.astype(out_arr.dtype.numpy_dtype)
271271

272272

273273
def block_bin(
@@ -294,6 +294,6 @@ def block_bin(
294294
sliced = block[slc]
295295

296296
# reshape the block and sum the values
297-
reshaped_block = np.reshape(sliced, new_shape) # type: ignore
297+
reshaped_block = np.reshape(sliced, new_shape)
298298
axes = tuple(range(1, reshaped_block.ndim, 2))
299299
return ufunc(reshaped_block, axis=axes)

src/microsim/cosem/_xarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def create_dataarray(
137137
if attrs is None:
138138
attrs = dict(getattr(element, "attrs", {}))
139139
if use_dask:
140-
element = da.from_array(element, chunks=chunks, inline_array=True) # type: ignore
140+
element = da.from_array(element, chunks=chunks, inline_array=True)
141141
result = xr.DataArray(element, coords=coords, attrs=attrs, name=name)
142142
return result
143143

src/microsim/cosem/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CosemImage(BaseModel):
9797
def bucket_path(self) -> str:
9898
return urllib.parse.urlparse(self.url).path
9999

100-
@computed_field
100+
@computed_field # type: ignore
101101
@property
102102
def scales(self) -> list[str]:
103103
"""Fetch all available scales for the image from s3."""
@@ -155,6 +155,8 @@ def read(
155155
from microsim.cosem._tstore import read_tensorstore
156156

157157
if bin_mode == "auto":
158+
# we convert segmentation images to sum mode, because they are encoded
159+
# for instance segmentation, which is not what we want.
158160
bin_mode = "sum" if self.content_type == "segmentation" else "standard"
159161
return read_tensorstore(
160162
self, level=level, transpose=transpose, bin_mode=bin_mode
@@ -293,7 +295,7 @@ def thumbnail(self) -> "np.ndarray":
293295
"To use the thumbnail property, install the imageio package."
294296
) from e
295297

296-
return imread(self.thumbnail_url)
298+
return imread(self.thumbnail_url) # type: ignore [no-any-return]
297299

298300
def read(
299301
self, image_keys: str | Sequence[str], **read_kwargs: Any

0 commit comments

Comments
 (0)