Skip to content
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

VALUE is not a is not a valid RESUNIT #329

Closed
esgomezm opened this issue Feb 27, 2023 · 12 comments
Closed

VALUE is not a is not a valid RESUNIT #329

esgomezm opened this issue Feb 27, 2023 · 12 comments

Comments

@esgomezm
Copy link
Contributor

Hi! I'm trying to export a model specifying the pixel size but whenever it's different from 1 or 0.5, it gives the following error. The variable pixel size enters as {'x': 0.8, 'y': 0.8, 'z': 0.005} in the kwargs of the build_model.

I'm trying it with the 3D UNet notebook of ZeroCostDL4Mic using TF 2.11 (I will update the notebook now in their repo)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/tifffile/tifffile.py in enumarg(enum, arg)
  22897     try:
> 22898         return enum(arg)  # type: ignore
  22899     except Exception:

9 frames
[/usr/lib/python3.8/enum.py](https://localhost:8080/#) in __call__(cls, value, names, module, qualname, type, start)
    338         if names is None:  # simple value lookup
--> 339             return cls.__new__(cls, value)
    340         # otherwise, functional API: we're creating a new Enum type

[/usr/lib/python3.8/enum.py](https://localhost:8080/#) in __new__(cls, value)
    662                 if result is None and exc is None:
--> 663                     raise ve_exc
    664                 elif exc is None:

ValueError: 20.0 is not a valid RESUNIT

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/tifffile/tifffile.py in enumarg(enum, arg)
  22900         try:
> 22901             return enum[arg.upper()]  # type: ignore
  22902         except Exception:

AttributeError: 'float' object has no attribute 'upper'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
[<ipython-input-51-bcabd10cff2c>](https://localhost:8080/#) in <module>
    203 
    204 # export the model with keras weihgts
--> 205 build_model(
    206     weight_uri=weight_path,
    207     test_inputs=[test_in_path],

[/usr/local/lib/python3.8/dist-packages/bioimageio/core/build_spec/build_model.py](https://localhost:8080/#) in build_model(weight_uri, test_inputs, test_outputs, input_axes, output_axes, name, description, authors, tags, documentation, cite, output_path, architecture, model_kwargs, weight_type, sample_inputs, sample_outputs, input_names, input_step, input_min_shape, input_data_range, output_names, output_reference, output_scale, output_offset, output_data_range, halo, preprocessing, postprocessing, pixel_sizes, maintainers, license, covers, git_repo, attachments, packaged_by, run_mode, parent, config, dependencies, links, training_data, root, add_deepimagej_config, tensorflow_version, opset_version, pytorch_version, weight_attachments)
    830     if add_deepimagej_config:
    831         if sample_inputs is None:
--> 832             sample_inputs, sample_outputs = _write_sample_data(
    833                 test_inputs, test_outputs, input_axes, output_axes, pixel_sizes, root
    834             )

[/usr/local/lib/python3.8/dist-packages/bioimageio/core/build_spec/build_model.py](https://localhost:8080/#) in _write_sample_data(input_paths, output_paths, input_axes, output_axes, pixel_sizes, export_folder)
    452         sample_in_path = export_folder / f"sample_input_{i}.tif"
    453         pixel_size = None if pixel_sizes is None else pixel_sizes[i]
--> 454         write_im(sample_in_path, inp, axes, pixel_size)
    455         sample_in_paths.append(sample_in_path)
    456 

[/usr/local/lib/python3.8/dist-packages/bioimageio/core/build_spec/build_model.py](https://localhost:8080/#) in write_im(path, im, axes, pixel_size)
    445         if np.dtype(im.dtype) == np.dtype("float64"):
    446             im = im.astype("float32")
--> 447         tifffile.imwrite(path, im, imagej=True, resolution=resolution)
    448 
    449     sample_in_paths = []

[/usr/local/lib/python3.8/dist-packages/tifffile/tifffile.py](https://localhost:8080/#) in imwrite(file, data, bigtiff, byteorder, imagej, ome, shaped, append, shape, dtype, photometric, planarconfig, extrasamples, volumetric, tile, rowsperstrip, bitspersample, compression, compressionargs, predictor, subsampling, jpegtables, colormap, description, datetime, resolution, resolutionunit, subfiletype, software, metadata, extratags, contiguous, truncate, align, maxworkers, returnoffset)
   1209         shaped=shaped,
   1210     ) as tif:
-> 1211         result = tif.write(
   1212             data,
   1213             shape=shape,

[/usr/local/lib/python3.8/dist-packages/tifffile/tifffile.py](https://localhost:8080/#) in write(***failed resolving arguments***)
   2800                 unit = resolution[2]  # type: ignore
   2801                 if unit is not None:
-> 2802                     resolutionunit = enumarg(RESUNIT, unit)
   2803             addtag(tags, 296, 3, 1, resolutionunit)  # ResolutionUnit
   2804         else:

/usr/local/lib/python3.8/dist-packages/tifffile/tifffile.py in enumarg(enum, arg)
  22901             return enum[arg.upper()]  # type: ignore
  22902         except Exception:
> 22903             raise ValueError(f'invalid argument {arg!r}')
  22904 
  22905 

ValueError: invalid argument 20.0

tifffile has changed some parameters of imwrite in their last version so it may be that they also have some requirements for the resolution variable.

tifffile.imwrite(path, im, imagej=True, resolution=resolution)

@FynnBe
Copy link
Member

FynnBe commented Feb 27, 2023

I can't reproduce this in my dev environment with tifffile 2022.10.10 . In colab the notebook installs version 2023.2.3 via pip., but there I can run this without error, too:

import numpy as np
import tifffile

im = np.zeros((2, 3)).astype("float32")
tifffile.imwrite("lala.tif", im, imagej=True, resolution=(1, 2))
tifffile.imwrite("lala.tif", im, imagej=True, resolution=(.8, 0.005))

Do you have a minimal example that fails?
Maybe you could even open a PR with a (failing) test?

@esgomezm
Copy link
Contributor Author

Hi @FynnBe

The problem here is that the image is a volume and for some reason, when specifying the resolution for the three dimensions, it does not work. For example, these work

import numpy as np
import tifffile

im = np.zeros((8, 512, 512)).astype("float32")
tifffile.imwrite("/content/lala.tif", im, imagej=True, resolution=(1, 2))
tifffile.imwrite("/content/lala.tif", im, imagej=True, resolution=(.8, 0.005))

and these not:

tifffile.imwrite("/content/lala.tif", im, imagej=True, resolution=(.8, 0.005, .8))
tifffile.imwrite("/content/lala.tif", im, imagej=True, resolution=(.8, .8, 0.005))
tifffile.imwrite("/content/lala.tif", im, imagej=True, resolution=(0.005, .8, .8)) 

@FynnBe
Copy link
Member

FynnBe commented Mar 1, 2023

ah, we might have to use tifffile.volwrite... I'll have a look (edit: volwrite does not exist...)

@FynnBe
Copy link
Member

FynnBe commented Mar 1, 2023

there are solutions out there:

https://github.com/cgohlke/tifffile/blob/eb79f64e65fdd17fa5722dac9ce32ec6dd224f5d/tifffile/tifffile.py#L453-L468
(posted in https://forum.image.sc/t/saving-tif-with-voxel-size-in-python/72299 )

I started working on this in #331, but I do not have time to finish that atm.
I suggest that you use small custom scripts with the snipped linked above in the mean time.

@esgomezm
Copy link
Contributor Author

esgomezm commented Mar 1, 2023

I suggest that you use small custom scripts with the snipped linked above in the mean time.

But the function is inside the bioimageio library. Do you suggest building the model with res = (1,1,1) and then change it manually? This is not a huge problem but note that we will need a workaround as QuPath requires the pixel size. We could also store that value in the deepImageJ config and stored a tiff file without metadata regarding the resolution :)

@FynnBe
Copy link
Member

FynnBe commented Mar 1, 2023

yes, probably best with res = None. (The resolution argument is only intended for 2d images).
I would still store it in the tiff file if that works for DeepImageJ, but for now you'd have to add it manually either way.

@esgomezm
Copy link
Contributor Author

esgomezm commented Mar 1, 2023

We don't need that information in the tiff but rather in the config field of the yaml

@esgomezm
Copy link
Contributor Author

esgomezm commented Mar 2, 2023

Hi again!
Have you seen this library? https://allencellmodeling.github.io/aicsimageio/
Seems to work very nicely with the metadata of tiff files both for the import and export

@FynnBe
Copy link
Member

FynnBe commented Mar 2, 2023

no, I wonder if @constantinpape is aware of that library? Before changing anything image io related I would want to consult him!
It definitely looks promising to me! xarray and xarray with dask data 👍

@constantinpape
Copy link
Contributor

constantinpape commented Mar 2, 2023

no, I wonder if @constantinpape is aware of that library? Before changing anything image io related I would want to consult him!

Yeah, this is the image data format developed by Allen. I haven't used it much myself, but from what I know it's pretty good. If we can added without too much dependency hassle I am all for adding it. (I am saying this because I think it uses bioformats internally, which in turn uses a javabridge, which makes it a bit of a mess; but I might be wrong / or this might only be an optional dependency that we don't need).

@FynnBe
Copy link
Member

FynnBe commented Mar 2, 2023

I won't have time for this in the next few weeks though

@FynnBe
Copy link
Member

FynnBe commented Oct 25, 2023

closing in favor of #353

@FynnBe FynnBe closed this as completed Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants