-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add writers #9
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a0f58b
Move OME utilities to separate file
SeanLeRoy 99b54b3
Add writers
SeanLeRoy 8552a3c
Add writer tests
SeanLeRoy 00edfb2
Fix link
SeanLeRoy 7ac3d9e
Use tmp_path in tests; Use ome-types 0.4.0
SeanLeRoy 1dcd7ae
Remove references to downloading test resources
SeanLeRoy a686637
Merge branch 'main' into feature/add-writers
SeanLeRoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import logging | ||
import typing | ||
|
||
import numpy as np | ||
from ome_types.model.simple_types import PixelType | ||
|
||
############################################################################### | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
############################################################################### | ||
|
||
|
||
def generate_ome_channel_id(image_id: str, channel_id: typing.Union[str, int]) -> str: | ||
""" | ||
Naively generates the standard OME channel ID using a provided ID. | ||
|
||
Parameters | ||
---------- | ||
image_id: str | ||
An image id to pull the image specific index from. | ||
See: `generate_ome_image_id` for more details. | ||
channel_id: Union[str, int] | ||
A string or int representing the ID for a channel. | ||
In the context of the usage of this function, this is usually used with the | ||
index of the channel. | ||
|
||
Returns | ||
------- | ||
ome_channel_id: str | ||
The OME standard for channel IDs. | ||
|
||
Notes | ||
----- | ||
ImageIds are usually: "Image:0", "Image:1", or "Image:N", | ||
ChannelIds are usually the combination of image index + channel index -- | ||
"Channel:0:0" for the first channel of the first image for example. | ||
""" | ||
# Remove the prefix 'Image:' to get just the index | ||
image_index = image_id.replace("Image:", "") | ||
return f"Channel:{image_index}:{channel_id}" | ||
|
||
|
||
def generate_ome_image_id(image_id: typing.Union[str, int]) -> str: | ||
""" | ||
Naively generates the standard OME image ID using a provided ID. | ||
|
||
Parameters | ||
---------- | ||
image_id: Union[str, int] | ||
A string or int representing the ID for an image. | ||
In the context of the usage of this function, this is usually used with the | ||
index of the scene / image. | ||
|
||
Returns | ||
------- | ||
ome_image_id: str | ||
The OME standard for image IDs. | ||
""" | ||
return f"Image:{image_id}" | ||
|
||
|
||
def dtype_to_ome_type(npdtype: np.dtype) -> PixelType: | ||
""" | ||
Convert numpy dtype to OME PixelType | ||
|
||
Parameters | ||
---------- | ||
npdtype: numpy.dtype | ||
A numpy datatype. | ||
|
||
Returns | ||
------- | ||
ome_type: PixelType | ||
One of the supported OME Pixels types | ||
|
||
Raises | ||
------ | ||
ValueError | ||
No matching pixel type for provided numpy type. | ||
""" | ||
ometypedict = { | ||
np.dtype(np.int8): PixelType.INT8, | ||
np.dtype(np.int16): PixelType.INT16, | ||
np.dtype(np.int32): PixelType.INT32, | ||
np.dtype(np.uint8): PixelType.UINT8, | ||
np.dtype(np.uint16): PixelType.UINT16, | ||
np.dtype(np.uint32): PixelType.UINT32, | ||
np.dtype(np.float32): PixelType.FLOAT, | ||
np.dtype(np.float64): PixelType.DOUBLE, | ||
np.dtype(np.complex64): PixelType.COMPLEXFLOAT, | ||
np.dtype(np.complex128): PixelType.COMPLEXDOUBLE, | ||
} | ||
ptype = ometypedict.get(npdtype) | ||
if ptype is None: | ||
raise ValueError(f"Ome utils can't resolve pixel type: {npdtype.name}") | ||
return ptype | ||
|
||
|
||
def ome_to_numpy_dtype(ome_type: PixelType) -> np.dtype: | ||
""" | ||
Convert OME PixelType to numpy dtype | ||
|
||
Parameters | ||
---------- | ||
ome_type: PixelType | ||
One of the supported OME Pixels types | ||
|
||
Returns | ||
------- | ||
npdtype: numpy.dtype | ||
A numpy datatype. | ||
|
||
Raises | ||
------ | ||
ValueError | ||
No matching numpy type for the provided pixel type. | ||
""" | ||
ometypedict: typing.Dict[PixelType, np.dtype] = { | ||
PixelType.INT8: np.dtype(np.int8), | ||
PixelType.INT16: np.dtype(np.int16), | ||
PixelType.INT32: np.dtype(np.int32), | ||
PixelType.UINT8: np.dtype(np.uint8), | ||
PixelType.UINT16: np.dtype(np.uint16), | ||
PixelType.UINT32: np.dtype(np.uint32), | ||
PixelType.FLOAT: np.dtype(np.float32), | ||
PixelType.DOUBLE: np.dtype(np.float64), | ||
PixelType.COMPLEXFLOAT: np.dtype(np.complex64), | ||
PixelType.COMPLEXDOUBLE: np.dtype(np.complex128), | ||
} | ||
nptype = ometypedict.get(ome_type) | ||
if nptype is None: | ||
raise ValueError(f"Ome utils can't resolve pixel type: {ome_type.value}") | ||
return nptype |
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 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,2 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually rethinking this I think I'll change this to use tempdir. Maybe we can write out an
example.txt
to tempdir too and avoid having any files download for this package thus far.