Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinchai committed Nov 7, 2024
1 parent f01bddf commit 709d437
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 231 deletions.
1 change: 1 addition & 0 deletions linc_convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Data conversion tools for the LINC project."""

__all__ = ["modalities", "utils"]
from . import modalities, utils
1 change: 1 addition & 0 deletions linc_convert/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Root command line entry point."""

from cyclopts import App

help = "Collection of conversion scripts for LINC datasets"
Expand Down
1 change: 1 addition & 0 deletions linc_convert/modalities/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Converters for all imaging modalities."""

__all__ = ["df", "lsm"]
from . import df, lsm
1 change: 1 addition & 0 deletions linc_convert/modalities/df/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Dark Field microscopy converters."""

__all__ = ["cli", "multi_slice", "single_slice"]

from . import cli, multi_slice, single_slice
1 change: 1 addition & 0 deletions linc_convert/modalities/df/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Entry-points for Dark Field microscopy converter."""

from cyclopts import App

from linc_convert.cli import main
Expand Down
88 changes: 53 additions & 35 deletions linc_convert/modalities/df/multi_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
We do not recompute the image pyramid but instead reuse the JPEG2000
levels (obtained by wavelet transform).
"""

# stdlib
import ast
import json
Expand Down Expand Up @@ -130,7 +131,7 @@ def convert(
new_width = jp2.shape[1]
new_size = (new_height, new_width)
if has_channel:
new_size += (3.,)
new_size += (3.0,)
print(len(inp), new_size, nblevel, has_channel)

# Prepare chunking options
Expand Down Expand Up @@ -158,15 +159,25 @@ def convert(
vxw, vxh = get_pixelsize(j2k)
subdat = WrappedJ2K(j2k, level=level)
subdat_size = subdat.shape
print("Convert level", level, "with shape", shape,
"for slice", idx, "with size", subdat_size)
print(
"Convert level",
level,
"with shape",
shape,
"for slice",
idx,
"with size",
subdat_size,
)

# offset while attaching
x = floordiv(shape[-2] - subdat_size[-2], 2)
y = floordiv(shape[-1] - subdat_size[-1], 2)

if max_load is None or (shape[-2] < max_load and shape[-1] < max_load):
array[..., idx, x:x + subdat_size[1], y:y + subdat_size[2]] = subdat[...]
array[..., idx, x : x + subdat_size[1], y : y + subdat_size[2]] = (
subdat[...]
)

else:
ni = ceildiv(shape[-2], max_load)
Expand All @@ -175,8 +186,14 @@ def convert(
for i in range(ni):
for j in range(nj):
print(f"\r{i+1}/{ni}, {j+1}/{nj}", end=" ")
start_x, end_x = i*max_load, min((i+1)*max_load, shape[-2])
start_y, end_y = j*max_load, min((j+1)*max_load, shape[-1])
start_x, end_x = (
i * max_load,
min((i + 1) * max_load, shape[-2]),
)
start_y, end_y = (
j * max_load,
min((j + 1) * max_load, shape[-1]),
)
if end_x <= x or end_y <= y:
continue

Expand All @@ -186,28 +203,30 @@ def convert(
array[
...,
idx,
x + start_x:x + min(end_x, subdat_size[-2]),
y + start_y:y + min(end_y, subdat_size[-1]),
x + start_x : x + min(end_x, subdat_size[-2]),
y + start_y : y + min(end_y, subdat_size[-1]),
] = subdat[
...,
start_x:min((i+1)*max_load, subdat_size[-2]),
start_y:min((j+1)*max_load, subdat_size[-1]),
start_x : min((i + 1) * max_load, subdat_size[-2]),
start_y : min((j + 1) * max_load, subdat_size[-1]),
]
print("")

# Write OME-Zarr multiscale metadata
print("Write metadata")
multiscales = [{
"version": "0.4",
"axes": [
{"name": "z", "type": "space", "unit": "micrometer"},
{"name": "y", "type": "distance", "unit": "micrometer"},
{"name": "x", "type": "space", "unit": "micrometer"}
],
"datasets": [],
"type": "jpeg2000",
"name": "",
}]
multiscales = [
{
"version": "0.4",
"axes": [
{"name": "z", "type": "space", "unit": "micrometer"},
{"name": "y", "type": "distance", "unit": "micrometer"},
{"name": "x", "type": "space", "unit": "micrometer"},
],
"datasets": [],
"type": "jpeg2000",
"name": "",
}
]
if has_channel:
multiscales[0]["axes"].insert(0, {"name": "c", "type": "channel"})

Expand All @@ -225,26 +244,25 @@ def convert(
level["coordinateTransformations"] = [
{
"type": "scale",
"scale": [1.0] * has_channel + [
"scale": [1.0] * has_channel
+ [
1.0,
(shape0[0]/shape[0])*vxh,
(shape0[1]/shape[1])*vxw,
]
(shape0[0] / shape[0]) * vxh,
(shape0[1] / shape[1]) * vxw,
],
},
{
"type": "translation",
"translation": [0.0] * has_channel + [
"translation": [0.0] * has_channel
+ [
0.0,
(shape0[0]/shape[0] - 1)*vxh*0.5,
(shape0[1]/shape[1] - 1)*vxw*0.5,
]
}
(shape0[0] / shape[0] - 1) * vxh * 0.5,
(shape0[1] / shape[1] - 1) * vxw * 0.5,
],
},
]
multiscales[0]["coordinateTransformations"] = [
{
"scale": [1.0] * (3 + has_channel),
"type": "scale"
}
{"scale": [1.0] * (3 + has_channel), "type": "scale"}
]
omz.attrs["multiscales"] = multiscales

Expand Down Expand Up @@ -278,7 +296,7 @@ def convert(

# Write sidecar .json file
json_name = os.path.splitext(out)[0]
json_name += '.json'
json_name += ".json"
dic = {}
dic["PixelSize"] = json.dumps([vxw, vxh])
dic["PixelSizeUnits"] = "um"
Expand Down
56 changes: 29 additions & 27 deletions linc_convert/modalities/df/single_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
It does not recompute the image pyramid but instead reuse the JPEG2000 levels
(obtained by wavelet transform).
"""

# stdlib
import ast
import os
Expand Down Expand Up @@ -134,27 +135,29 @@ def convert(
print(f"\r{i+1}/{ni}, {j+1}/{nj}", end="")
array[
...,
i*max_load:min((i+1)*max_load, shape[-2]),
j*max_load:min((j+1)*max_load, shape[-1]),
i * max_load : min((i + 1) * max_load, shape[-2]),
j * max_load : min((j + 1) * max_load, shape[-1]),
] = subdat[
...,
i*max_load:min((i+1)*max_load, shape[-2]),
j*max_load:min((j+1)*max_load, shape[-1]),
i * max_load : min((i + 1) * max_load, shape[-2]),
j * max_load : min((j + 1) * max_load, shape[-1]),
]
print("")

# Write OME-Zarr multiscale metadata
print("Write metadata")
multiscales = [{
"version": "0.4",
"axes": [
{"name": "y", "type": "space", "unit": "micrometer"},
{"name": "x", "type": "space", "unit": "micrometer"}
],
"datasets": [],
"type": "jpeg2000",
"name": "",
}]
multiscales = [
{
"version": "0.4",
"axes": [
{"name": "y", "type": "space", "unit": "micrometer"},
{"name": "x", "type": "space", "unit": "micrometer"},
],
"datasets": [],
"type": "jpeg2000",
"name": "",
}
]
if has_channel:
multiscales[0]["axes"].insert(0, {"name": "c", "type": "channel"})

Expand All @@ -172,24 +175,23 @@ def convert(
level["coordinateTransformations"] = [
{
"type": "scale",
"scale": [1.0] * has_channel + [
(shape0[0]/shape[0])*vxh,
(shape0[1]/shape[1])*vxw,
]
"scale": [1.0] * has_channel
+ [
(shape0[0] / shape[0]) * vxh,
(shape0[1] / shape[1]) * vxw,
],
},
{
"type": "translation",
"translation": [0.0] * has_channel + [
(shape0[0]/shape[0] - 1)*vxh*0.5,
(shape0[1]/shape[1] - 1)*vxw*0.5,
]
}
"translation": [0.0] * has_channel
+ [
(shape0[0] / shape[0] - 1) * vxh * 0.5,
(shape0[1] / shape[1] - 1) * vxw * 0.5,
],
},
]
multiscales[0]["coordinateTransformations"] = [
{
"scale": [1.0] * (2 + has_channel),
"type": "scale"
}
{"scale": [1.0] * (2 + has_channel), "type": "scale"}
]
omz.attrs["multiscales"] = multiscales

Expand Down
1 change: 1 addition & 0 deletions linc_convert/modalities/lsm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Light Sheet Microscopy converters."""

__all__ = ["cli", "mosaic"]
from . import cli, mosaic
1 change: 1 addition & 0 deletions linc_convert/modalities/lsm/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Entry-points for Dark Field microscopy converter."""

from cyclopts import App

from linc_convert.cli import main
Expand Down
Loading

0 comments on commit 709d437

Please sign in to comment.