From 1fec7e23545ac067c34ff4c6d42f45e3a695704c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= <38459088+jo-mueller@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:43:06 +0100 Subject: [PATCH 1/5] added `repr_html` method for `_ImageWrapper` --- src/omero/gateway/__init__.py | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index 229af9d59..dba2de6be 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -10426,6 +10426,107 @@ def getROICount(self, shapeType=None, filterByCurrentUser=False): return count[0][0].getValue() return len(self._get_rois(shapeType, filterByCurrentUser)) + def _repr_html_(self): + import base64 + + html_style_header = """ + + + + + + Image Details + + + """ + + # create a sub-table for image information + table_imageinfo = f""" + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n +
Image name: {self.getName()}
Image ID: {self.getId()}
Project ID: {self.getProject().getId()}
+ """ + + # get entries for thumbnail and dimensions + encoded_image = base64.b64encode(self.getThumbnail()).decode('utf-8') + dimensions = f"""( + {self.getSizeT()}, + {self.getSizeC()}, + {self.getSizeZ()}, + {self.getSizeY()}, + {self.getSizeX()})""" + physical_dims = f"""( + {self.getPixelSizeZ()}, + {self.getPixelSizeY()}, + {self.getPixelSizeX()})""" + physical_units = f"""( + {self.getPixelSizeZ(units=True).getUnit()}, + {self.getPixelSizeY(units=True).getUnit()}, + {self.getPixelSizeX(units=True).getUnit()})""" + + table_dimensions = f""" + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n +
Dimensions (TCZYX): {dimensions}
Voxel/Pixel dimensions (ZYX): {physical_dims}
Physical units: {physical_units}
Channel Names: {self.getChannelLabels()}
+ """ + + table_assembly = f""" + + + + + +
+ Thumbnail +

Image information

+ {table_imageinfo} +
+ + + + +
{table_dimensions}
+ """ + + return '\n'.join([ + html_style_header, + '', + table_assembly, + '', + '' + ]) + + ImageWrapper = _ImageWrapper # INSTRUMENT AND ACQUISITION # From 1e40b2acb4827a014af3cb1bb7311a742482963e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= <38459088+jo-mueller@users.noreply.github.com> Date: Wed, 31 Jan 2024 18:49:22 +0100 Subject: [PATCH 2/5] limited floating point precision in voxel dimensions --- src/omero/gateway/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index dba2de6be..42b6d956c 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -10474,10 +10474,10 @@ def _repr_html_(self): {self.getSizeZ()}, {self.getSizeY()}, {self.getSizeX()})""" - physical_dims = f"""( - {self.getPixelSizeZ()}, - {self.getPixelSizeY()}, - {self.getPixelSizeX()})""" + physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format( + self.getPixelSizeZ(), + self.getPixelSizeY(), + self.getPixelSizeX()) physical_units = f"""( {self.getPixelSizeZ(units=True).getUnit()}, {self.getPixelSizeY(units=True).getUnit()}, From 48d4de448500282c9753cc86f052f818731ec09d Mon Sep 17 00:00:00 2001 From: William Moore Date: Thu, 1 Feb 2024 12:27:05 +0000 Subject: [PATCH 3/5] Show Image, Dataset and Project in a table --- src/omero/gateway/__init__.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index dba2de6be..0e3057b9a 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -10451,18 +10451,21 @@ def _repr_html_(self): """ + def obj_html(obj, otype): + return f""" + {otype} + {obj.id if obj else ""} + {obj.name if obj else ""} + + """ + # create a sub-table for image information table_imageinfo = f""" - \n - \n - \n - \n - \n - \n - \n - \n - \n - \n +
Image name: {self.getName()}
Image ID: {self.getId()}
Project ID: {self.getProject().getId()}
+ + {obj_html(self, 'Image')} + {obj_html(self.getParent(), 'Dataset')} + {obj_html(self.getProject(), 'Project')}
IDName
""" From da9d6443549e8353ba3cd8c5aa4c952dca95620e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= <38459088+jo-mueller@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:11:54 +0200 Subject: [PATCH 4/5] moved `repr_html` code to utils --- src/omero/gateway/__init__.py | 113 +++------------------------------- src/omero/gateway/utils.py | 103 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 105 deletions(-) diff --git a/src/omero/gateway/__init__.py b/src/omero/gateway/__init__.py index cfd495c2d..c9dd4e581 100644 --- a/src/omero/gateway/__init__.py +++ b/src/omero/gateway/__init__.py @@ -59,7 +59,7 @@ from omero.cmd import Chgrp2, Delete2, DoAll, SkipHead, Chown2 from omero.cmd.graphs import ChildOption from omero.api import Save -from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean +from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean, image_to_html from omero.model.enums import PixelsTypeint8, PixelsTypeuint8, PixelsTypeint16 from omero.model.enums import PixelsTypeuint16, PixelsTypeint32 from omero.model.enums import PixelsTypeuint32, PixelsTypefloat @@ -291,6 +291,13 @@ def __init__(self, conn=None, obj=None, cache=None, **kwargs): self._conn.SERVICE_OPTS) self.__prepare__(**kwargs) + def _repr_html_(self): + """ + Returns an HTML representation of the object. This is used by the + IPython notebook to display the object in a cell. + """ + return image_to_html(self) + def __eq__(self, a): """ Returns true if the object is of the same type and has same id and name @@ -10426,110 +10433,6 @@ def getROICount(self, shapeType=None, filterByCurrentUser=False): return count[0][0].getValue() return len(self._get_rois(shapeType, filterByCurrentUser)) - def _repr_html_(self): - import base64 - - html_style_header = """ - - - - - - Image Details - - - """ - - def obj_html(obj, otype): - return f""" - {otype} - {obj.id if obj else ""} - {obj.name if obj else ""} - - """ - - # create a sub-table for image information - table_imageinfo = f""" - - - {obj_html(self, 'Image')} - {obj_html(self.getParent(), 'Dataset')} - {obj_html(self.getProject(), 'Project')} -
IDName
- """ - - # get entries for thumbnail and dimensions - encoded_image = base64.b64encode(self.getThumbnail()).decode('utf-8') - dimensions = f"""( - {self.getSizeT()}, - {self.getSizeC()}, - {self.getSizeZ()}, - {self.getSizeY()}, - {self.getSizeX()})""" - physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format( - self.getPixelSizeZ(), - self.getPixelSizeY(), - self.getPixelSizeX()) - physical_units = f"""( - {self.getPixelSizeZ(units=True).getUnit()}, - {self.getPixelSizeY(units=True).getUnit()}, - {self.getPixelSizeX(units=True).getUnit()})""" - - table_dimensions = f""" - \n - \n - \n - \n - \n - \n - \n - \n - \n - \n - \n - \n - \n -
Dimensions (TCZYX): {dimensions}
Voxel/Pixel dimensions (ZYX): {physical_dims}
Physical units: {physical_units}
Channel Names: {self.getChannelLabels()}
- """ - - table_assembly = f""" - - - - - -
- Thumbnail -

Image information

- {table_imageinfo} -
- - - - -
{table_dimensions}
- """ - - return '\n'.join([ - html_style_header, - '', - table_assembly, - '', - '' - ]) - - ImageWrapper = _ImageWrapper # INSTRUMENT AND ACQUISITION # diff --git a/src/omero/gateway/utils.py b/src/omero/gateway/utils.py index 481ff2063..72ab03020 100644 --- a/src/omero/gateway/utils.py +++ b/src/omero/gateway/utils.py @@ -221,3 +221,106 @@ def propertiesToDict(m, prefix=None): except: d[items[-1]] = value return nested_dict + +def image_to_html(image): + import base64 + + html_style_header = """ + + + + + + Image Details + + + """ + + def obj_html(obj, otype): + return f""" + {otype} + {obj.id if obj else ""} + {obj.name if obj else ""} + + """ + + # create a sub-table for image information + table_imageinfo = f""" + + + {obj_html(image, 'Image')} + {obj_html(image.getParent(), 'Dataset')} + {obj_html(image.getProject(), 'Project')} +
IDName
+ """ + + # get entries for thumbnail and dimensions + encoded_image = base64.b64encode(image.getThumbnail()).decode('utf-8') + dimensions = f"""( + {image.getSizeT()}, + {image.getSizeC()}, + {image.getSizeZ()}, + {image.getSizeY()}, + {image.getSizeX()})""" + physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format( + image.getPixelSizeZ(), + image.getPixelSizeY(), + image.getPixelSizeX()) + physical_units = f"""( + {image.getPixelSizeZ(units=True).getUnit()}, + {image.getPixelSizeY(units=True).getUnit()}, + {image.getPixelSizeX(units=True).getUnit()})""" + + table_dimensions = f""" + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n + \n +
Dimensions (TCZYX): {dimensions}
Voxel/Pixel dimensions (ZYX): {physical_dims}
Physical units: {physical_units}
Channel Names: {image.getChannelLabels()}
+ """ + + table_assembly = f""" + + + + + +
+ Thumbnail +

Image information

+ {table_imageinfo} +
+ + + + +
{table_dimensions}
+ """ + + return '\n'.join([ + html_style_header, + '', + table_assembly, + '', + '' + ]) \ No newline at end of file From b0a7fada46d8af0764eeb94882474a610fbadde4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= <38459088+jo-mueller@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:38:11 +0200 Subject: [PATCH 5/5] escaped non-existing pixsizes/units --- src/omero/gateway/utils.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/omero/gateway/utils.py b/src/omero/gateway/utils.py index 72ab03020..2b2d3c3a4 100644 --- a/src/omero/gateway/utils.py +++ b/src/omero/gateway/utils.py @@ -225,6 +225,17 @@ def propertiesToDict(m, prefix=None): def image_to_html(image): import base64 + try: + pixsizeX = '{:.3f}'.format(image.getPixelSizeX()) + pixsizeY = '{:.3f}'.format(image.getPixelSizeY()) + pixsizeZ = '{:.3f}'.format(image.getPixelSizeZ()) + UnitX = image.getPixelSizeX().getUnit() + UnitY = image.getPixelSizeY().getUnit() + UnitZ = image.getPixelSizeZ().getUnit() + except: + pixsizeX, pixsizeY, pixsizeZ = 'na', 'na', 'na' + UnitX, UnitY, UnitZ = 'na', 'na', 'na' + html_style_header = """ @@ -273,14 +284,8 @@ def obj_html(obj, otype): {image.getSizeZ()}, {image.getSizeY()}, {image.getSizeX()})""" - physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format( - image.getPixelSizeZ(), - image.getPixelSizeY(), - image.getPixelSizeX()) - physical_units = f"""( - {image.getPixelSizeZ(units=True).getUnit()}, - {image.getPixelSizeY(units=True).getUnit()}, - {image.getPixelSizeX(units=True).getUnit()})""" + physical_dims = f"""({pixsizeZ}, {pixsizeY}, {pixsizeX})""".format() + physical_units = f"""({UnitZ}, {UnitY}, {UnitX})""" table_dimensions = f""" \n