TheJacksonLaboratory / ezomero

A module with convenience functions for writing Python code that interacts with OMERO.
GNU General Public License v2.0
38 stars 13 forks source link

[BUG] TypeError for Shapes with no FillColor #63

Closed JensWendt closed 1 year ago

JensWendt commented 1 year ago

Describe the bug While calling get_shape(conn, id) , if the Shape has FillColor = None it leads to a TypeError in _int_to_rgba(omero_val).

To Reproduce Upload generated ROIs via https://github.com/GReD-Clermont/omero_batch-plugin from Fiji. As far as I tried you cannot generate FillColor None in OMERO itself, only 0. Therefore it might be a fringe exception, but nevertheless interesting that it can happen.

Expected behavior Maybe catch the None type and treat it as a 0 because that is how it is displayed (as rgba(0, 0, 0, 0) ) in OMERO.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [78], in <cell line: 9>()
      2 print(type(roilist[0]))
      3 # for roi in roilist:
      4 #     shape_id = ezom.get_shape_ids(conn, roi)[0]
      5 #     shape = ezom.get_shape(conn, shape_id)
      6 #     comment = unwrap(shape.textValue)
      7 #     print(roi," ---- ", comment)
----> 9 shape = ezom.get_shape(conn, 10832)

File ~\AppData\Roaming\Python\Python39\site-packages\ezomero\_ezomero.py:62, in do_across_groups.<locals>.wrapper(*args, **kwargs)
     60     current_group = args[0].getGroupFromContext().getId()
     61     args[0].SERVICE_OPTS.setOmeroGroup('-1')
---> 62     res = f(*args, **kwargs)
     63     set_group(args[0], current_group)
     64 else:

File ~\AppData\Roaming\Python\Python39\site-packages\ezomero\_gets.py:1112, in get_shape(conn, shape_id, across_groups)
   1110     raise TypeError('Shape ID must be an integer')
   1111 omero_shape = conn.getObject('Shape', shape_id)
-> 1112 return _omero_shape_to_shape(omero_shape)

File ~\AppData\Roaming\Python\Python39\site-packages\ezomero\_gets.py:1189, in _omero_shape_to_shape(omero_shape)
   1186     err = 'The shape passed for the roi is not a valid shape type'
   1187     raise TypeError(err)
-> 1189 fill_color = _int_to_rgba(omero_shape.getFillColor())
   1190 stroke_color = _int_to_rgba(omero_shape.getStrokeColor())
   1191 stroke_width = omero_shape.getStrokeWidth().getValue()

File ~\AppData\Roaming\Python\Python39\site-packages\ezomero\_gets.py:1198, in _int_to_rgba(omero_val)
   1196 def _int_to_rgba(omero_val):
   1197     """ Helper function returning the color as an Integer in RGBA encoding """
-> 1198     if omero_val < 0:
   1199         omero_val = omero_val + (2**32)
   1200     r = omero_val >> 24

TypeError: '<' not supported between instances of 'NoneType' and 'int'
erickmartins commented 1 year ago

Hi Jens, I don't have an easy way to test this, but the version on the latest PR (#64) should solve this - can you test it and see if it works for you?

JensWendt commented 1 year ago

Hi @erickmartins , it seems to do the job. I uploaded the ROIs in the same way as before. Now

roi_service = conn.getRoiService()
roiList = roi_service.findByImage(2874,None).rois
for roi in roiList:
    FillColor = roi._shapesSeq[0].getFillColor()
    StrokeColor = roi._shapesSeq[0].getStrokeColor()
    StrokeWidth = roi._shapesSeq[0].getStrokeWidth()
    print("FillColor ",FillColor,"  StrokeColor ", StrokeColor,"  StrokeWidth ", StrokeWidth)

gives me: FillColor None StrokeColor None StrokeWidth 0.0 POINT

I assume the "POINT" is the optional length unit that the official documentation of omero model is reffering to

StrokeWidth - the width of the stroke in pixels (optional). This also has an optional length unit, StrokeWidthUnit.

and with ezomero:

roilist = ezom.get_roi_ids(conn, 2874)
for roiId in roilist:
    shapeId = ezom.get_shape_ids(conn, roiId)[0]
    shape = ezom.get_shape(conn, shapeId)
    print("FillColor ",shape[1],"  StrokeColor ", shape[2],"  StrokeWidth ", shape[3])

gives me: FillColor (0, 0, 0, 0) StrokeColor (0, 0, 0, 0) StrokeWidth 0.0

I cannot really judge the StrokeWidth fix as 0.0 is not None.

JensWendt commented 1 year ago

Looks good to me, overall

erickmartins commented 1 year ago

Alright, thanks a lot! We'll merge it soon and release a new version with the fix.