Skip to content

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

@lauramurgatroyd

Description

@lauramurgatroyd

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions