NHPatterson / wsireg

multimodal whole slide image registration in a graph structure
MIT License
78 stars 7 forks source link

Tifffile throws delayed import error #8

Closed JLrumberger closed 3 years ago

JLrumberger commented 3 years ago

Description

I want to register an IF WSI (moving) on a HE WSI (fixed). PreProcessing and calculation of offsets worked like a charm, but the transform_images step failed because of a delayed import error. The imagecodecs library is definitively available in the environment, so that shouldn't be the error. I'll try it now with compression=None instead of "jpeg" in the file_writer and see if it solves this problem. Thanks for the work, the library seems great besides this problem with the file writer.

What I Did

from wsireg.wsireg2d import WsiReg2D

# initialize registration graph
reg_graph = WsiReg2D("my_reg_project", "PreProcessed", cache_images=False)

# add registration images (modalities)
reg_graph.add_modality(
    "modality_fluo",
    'PreProcessed\E8388\stitched.ome.tif',
    image_res=1,
    channel_names=["DAPI", "C1", "C2", "C3"],
    channel_colors=["blue", "green", "red", "white"],
    prepro_dict={
        "image_type": "FL",
        "ch_indices": [0],
        "as_uint8": True,
        "contrast_enhance": True,
    },
)

reg_graph.add_modality(
    "modality_brightfield",
    "PreProcessed\E8388\he.ome.tif",
    image_res=1,
    prepro_dict={
        "image_type": "BF", # makes colors inverse
        "as_uint8": True,
        "ch_indices": None,
        #"inv_int_opt": False
    },
)
reg_graph.add_reg_path(
    "modality_fluo", "modality_brightfield", reg_params=["rigid", "nl"],
) # map IF onto brightfield

print("number of modalities : {}".format(reg_graph.n_modalities))
print("number of registrations : {}".format(reg_graph.n_registrations))

reg_graph.register_images()
reg_graph.save_transformations()

reg_graph.transform_images(file_writer="ome.tiff")

transforming modality_fluo to modality_brightfield
saving to PreProcessed\my_reg_project-modality_fluo_to_modality_brightfield_registered.ome.tiff
transforming : 0
transformed : 0
transforming : 1
transformed : 1
transforming : 2
transformed : 2
transforming : 3
transformed : 3
---------------------------------------------------------------------------
DelayedImportError                        Traceback (most recent call last)
<ipython-input-4-25c0ce81c0de> in <module>
----> 1 reg_graph.transform_images(file_writer="ome.tiff")

C:\ProgramData\Anaconda3\lib\site-packages\wsireg\wsireg2d.py in transform_images(self, file_writer, transform_non_reg, to_original_size)
    915 
    916             for key in reg_path_keys:
--> 917                 im_fp = self._transform_image(
    918                     key,
    919                     file_writer=file_writer,

C:\ProgramData\Anaconda3\lib\site-packages\wsireg\wsireg2d.py in _transform_image(self, edge_key, file_writer, attachment, attachment_modality, to_original_size)
    864             channel_colors=im_data.get("channel_colors"),
    865         )
--> 866         im_fp = tfregimage.transform_image(
    867             output_path.stem,
    868             transform_data=transformations,

C:\ProgramData\Anaconda3\lib\site-packages\wsireg\reg_images\reg_image.py in transform_image(self, image_name, transform_data, file_writer, output_dir, **transformation_opts)
    272             )
    273         if file_writer.lower() == "ome.tiff":
--> 274             im_fp = transform_to_ome_tiff(
    275                 self,
    276                 image_name,

C:\ProgramData\Anaconda3\lib\site-packages\wsireg\utils\im_utils.py in transform_to_ome_tiff(tform_reg_im, image_name, output_dir, final_transform, composite_transform, tile_size, write_pyramid)
   1365 
   1366             # write channel data
-> 1367             tif.write(
   1368                 rgb_im_data,
   1369                 subifds=subifds,

C:\ProgramData\Anaconda3\lib\site-packages\tifffile\tifffile.py in write(***failed resolving arguments***)
   2454                                 chunk = pad_tile(chunk, tileshape, datadtype)
   2455                             isbytes = False
-> 2456                             chunk = compress(chunk)
   2457                         fh.write(chunk)
   2458                         databytecounts[tileindex] = len(chunk)

C:\ProgramData\Anaconda3\lib\site-packages\tifffile\tifffile.py in compress(data, compressor, subsampling, kwargs)
   2294                     kwargs=compressionargs,
   2295                 ):
-> 2296                     return compressor(
   2297                         data,
   2298                         subsampling=subsampling,

C:\ProgramData\Anaconda3\lib\site-packages\imagecodecs\imagecodecs.py in jpeg_encode(data, level, colorspace, outcolorspace, subsampling, optimize, smoothing, out)
    946     else:
    947         raise ValueError(f'invalid data type {data.dtype}')
--> 948     return func(
    949         data,
    950         level=level,

C:\ProgramData\Anaconda3\lib\site-packages\imagecodecs\imagecodecs.py in stub_encode(*args, **kwargs)
    512         def stub_encode(*args, **kwargs):
    513             f"""Stub for imagecodecs.{name}."""
--> 514             raise DelayedImportError(name)
    515 
    516         return stub_encode

DelayedImportError: could not import name 'jpeg12_encode' from 'imagecodecs'```
NHPatterson commented 3 years ago

Thanks for giving it a shot and raising this issue, there's definitely still things to improve.. I think this error may arise because you are trying to compress data with JPEG that is not 8-bit RGB. What is the dtype and shape of your fluorescent image? It may be stored interleaved but greater than unsigned 8-bit.

Definitely I will also expose compressed as an argument to the file_writer so you don't have to hack the code yourself!

NHPatterson commented 3 years ago

Closing due to inactivity.