Skip to content
Snippets Groups Projects
Commit e9fa8141 authored by payno's avatar payno
Browse files

Merge branch 'cherry-pick-b7cfba9c' into 'main'

Merge branch 'fix_jp2k' into '2.1'

See merge request !256
parents e67fb0f1 f2baa7a2
No related branches found
No related tags found
1 merge request!256Merge branch 'fix_jp2k' into '2.1'
Pipeline #230518 passed
......@@ -183,13 +183,16 @@ class JP2KVolume(VolumeSingleFrameBase):
if not has_glymur:
raise RuntimeError(_MISSING_GLYMUR_MSG)
expected_output_dtype = frame.dtype
if self.clip_values is not None:
frame = numpy.clip(frame, self.clip_values[0], self.clip_values[1])
if expected_output_dtype not in (numpy.uint8, numpy.uint16):
_logger.info(
f"{self.get_identifier().to_str()} get {frame.dtype}. Cast it as {numpy.uint16}"
)
expected_output_dtype = numpy.uint16
if self.rescale_data:
if frame.dtype in (numpy.uint8, numpy.uint16):
max_uint = numpy.iinfo(frame.dtype).max
else:
max_uint = numpy.iinfo(numpy.uint16).max
max_uint = numpy.iinfo(expected_output_dtype).max
frame = rescale_data(
data=frame,
new_min=0,
......@@ -197,13 +200,7 @@ class JP2KVolume(VolumeSingleFrameBase):
data_min=self.clip_values[0] if self.clip_values is not None else None,
data_max=self.clip_values[1] if self.clip_values is not None else None,
)
if not isinstance(frame.dtype, (numpy.uint8, numpy.uint16)):
if self._cast_already_logged:
self._cast_already_logged = True
_logger.info(
f"{self.get_identifier().to_str()} get {frame.dtype}. Cast it as {numpy.uint16}"
)
frame = frame.astype(numpy.uint16)
frame = frame.astype(expected_output_dtype)
if scheme == "glymur":
glymur.Jp2k(file_name, data=frame, psnr=self.psnr, cratios=self.cratios)
......
......@@ -2,19 +2,24 @@
import os
import numpy
import pytest
from tomoscan.esrf.volume.jp2kvolume import JP2KVolume
from tomoscan.esrf.volume.mock import create_volume
_data = create_volume(
frame_dims=(100, 100), z_size=11
) # z_size need to be at least 10 to check loading from file name works
for i in range(len(_data)):
_data[i] += 1
_data = _data.astype(numpy.uint16)
@pytest.fixture
def raw_data(dtype: numpy.dtype):
data = create_volume(
frame_dims=(100, 100), z_size=11
) # z_size need to be at least 10 to check loading from file name works
for i in range(len(data)):
data[i] += 1
return data.astype(dtype)
def test_jp2kvolume_rescale(tmp_path):
@pytest.mark.parametrize("dtype", (numpy.uint8, numpy.uint16))
def test_jp2kvolume_rescale(raw_data, dtype, tmp_path):
"""
Test that rescale is correctly applied by default by the JP2KVolume
"""
......@@ -22,10 +27,13 @@ def test_jp2kvolume_rescale(tmp_path):
os.makedirs(acquisition_dir)
volume_dir = str(acquisition_dir / "volume")
os.makedirs(volume_dir)
volume = JP2KVolume(folder=volume_dir, data=_data, metadata={})
assert raw_data.dtype == dtype
volume = JP2KVolume(folder=volume_dir, data=raw_data, metadata={})
assert volume.data.dtype == dtype
volume.save()
volume.clear_cache()
volume.load()
assert volume.data.dtype == dtype
assert volume.data.min() == 0
assert volume.data.max() == numpy.iinfo(numpy.uint16).max
assert volume.data.max() in (numpy.iinfo(dtype).max, numpy.iinfo(dtype).max - 1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment