-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor metadata module to be more pythonic
The axis submodule has also been streamlined to drop the CalibratedAxis dict and instead uses a list of Strings. This avoids Java import errors if a user imports the axis submodule before initializing ImageJ.
- Loading branch information
Showing
5 changed files
with
87 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import Sequence | ||
|
||
import imagej.dims as dims | ||
import imagej.metadata.axis as axis | ||
from imagej._java import jc | ||
|
||
|
||
def create_imagej_metadata( | ||
axes: Sequence["jc.CalibratedAxis"], dim_seq: Sequence[str] | ||
) -> dict: | ||
""" | ||
Create the ImageJ metadata attribute dictionary for xarray's global attributes. | ||
:param axes: A list or tuple of ImageJ2 axis objects | ||
(e.g. net.imagej.axis.DefaultLinearAxis). | ||
:param dim_seq: A list or tuple of the dimension order (e.g. ['X', 'Y', 'C']). | ||
:return: Dict of image metadata. | ||
""" | ||
ij_metadata = {} | ||
if len(axes) != len(dim_seq): | ||
raise ValueError( | ||
f"Axes length ({len(axes)}) does not match \ | ||
dimension length ({len(dim_seq)})." | ||
) | ||
|
||
for i in range(len(axes)): | ||
# get CalibratedAxis type as string (e.g. "EnumeratedAxis") | ||
ij_metadata[ | ||
dims._to_ijdim(dim_seq[i]) + "_cal_axis_type" | ||
] = axis.calibrated_axis_to_str(axes[i]) | ||
# get scale and origin for DefaultLinearAxis | ||
if isinstance(axes[i], jc.DefaultLinearAxis): | ||
ij_metadata[dims._to_ijdim(dim_seq[i]) + "_scale"] = float(axes[i].scale()) | ||
ij_metadata[dims._to_ijdim(dim_seq[i]) + "_origin"] = float( | ||
axes[i].origin() | ||
) | ||
|
||
return ij_metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from _jpype import JClass | ||
|
||
from imagej._java import jc | ||
|
||
_calibrated_axes = [ | ||
"net.imagej.axis.ChapmanRichardsAxis", | ||
"net.imagej.axis.DefaultLinearAxis", | ||
"net.imagej.axis.EnumeratedAxis", | ||
"net.imagej.axis.ExponentialAxis", | ||
"net.imagej.axis.ExponentialRecoveryAxis", | ||
"net.imagej.axis.GammaVariateAxis", | ||
"net.imagej.axis.GaussianAxis", | ||
"net.imagej.axis.IdentityAxis", | ||
"net.imagej.axis.InverseRodbardAxis", | ||
"net.imagej.axis.LogLinearAxis", | ||
"net.imagej.axis.PolynomialAxis", | ||
"net.imagej.axis.PowerAxis", | ||
"net.imagej.axis.RodbardAxis", | ||
] | ||
|
||
|
||
def calibrated_axis_to_str(axis: "jc.CalibratedAxis") -> str: | ||
""" | ||
Convert a CalibratedAxis class to a String. | ||
:param axis: CalibratedAxis type (e.g. net.imagej.axis.DefaultLinearAxis). | ||
:return: String of CalibratedAxis typeb(e.g. "DefaultLinearAxis"). | ||
""" | ||
if not isinstance(axis, JClass): | ||
axis = axis.__class__ | ||
|
||
return str(axis).split("'")[1] | ||
|
||
|
||
def str_to_calibrated_axis(axis: str) -> "jc.CalibratedAxis": | ||
""" | ||
Convert a String to CalibratedAxis class. | ||
:param axis: String of calibratedAxis type (e.g. "DefaultLinearAxis"). | ||
:return: Java class of CalibratedAxis type | ||
(e.g. net.imagej.axis.DefaultLinearAxis). | ||
""" | ||
if not isinstance(axis, str): | ||
raise TypeError(f"Axis {type(axis)} is not a String.") | ||
|
||
if axis in _calibrated_axes: | ||
return getattr(jc, axis.split(".")[3]) | ||
else: | ||
return None |