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

TIFF compression unit tests fail with numpy v2 as numpy float32 can't be dumped to json #2030

Open
lauramurgatroyd opened this issue Jan 9, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@lauramurgatroyd
Copy link
Member

Description

This is because we try to write a dictionary to a json file and the dictionary contains something of type numpy.float32. This is because here:

scale = save_range / data_range

scale becomes the same type as the data because data_range is the same type as the data

data_range = data.max() - data_min

We need to convert the scale to a normal float type and then it can be 'dumped' to json

ERROR: test_TIFF_compression3D_1 (test_io.TestTIFF.test_TIFF_compression3D_1)

Traceback (most recent call last):
File "/home/lhe97136/CIL/Wrappers/Python/test/test_io.py", line 283, in test_TIFF_compression3D_1
self.TIFF_compression_test('uint8')
File "/home/lhe97136/CIL/Wrappers/Python/test/test_io.py", line 324, in TIFF_compression_test
writer.write()
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/site-packages/cil/io/TIFF.py", line 179, in write
save_scale_offset(fname, self.scale, self.offset)
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/site-packages/cil/io/TIFF.py", line 51, in save_scale_offset
utilities.save_dict_to_file(txt, d)
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/site-packages/cil/io/utilities.py", line 134, in save_dict_to_file
json.dump(dictionary, configfile)
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/json/init.py", line 179, in dump
for chunk in iterable:
^^^^^^^^
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/json/encoder.py", line 432, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/json/encoder.py", line 406, in _iterencode_dict
yield from chunks
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/json/encoder.py", line 439, in _iterencode
o = _default(o)
^^^^^^^^^^^
File "/home/lhe97136/miniconda3/envs/cil_mat/lib/python3.12/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type float32 is not JSON serializable

Environment

python 3.12, numpy 2

import cil, sys
print(cil.version.version, cil.version.commit_hash, sys.version, sys.platform)
@lauramurgatroyd lauramurgatroyd added the bug Something isn't working label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant