Skip to content

Commit

Permalink
Merge pull request #580 from kif/578_edf_fast
Browse files Browse the repository at this point in the history
Fix `fast_read_data` when reading the last line
  • Loading branch information
kif authored Aug 21, 2024
2 parents 00f76a9 + 75436ac commit 8a3a5eb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/fabio/edfimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "ESRF"
__date__ = "01/06/2022"
__date__ = "20/08/2024"

import os
import re
Expand Down Expand Up @@ -1339,6 +1339,8 @@ def fast_read_roi(self, filename, coords=None):
Method reading Region of Interest of another file based on metadata available in current EdfImage.
The aim is performances, ... but only supports uncompressed files.
:param filename: name of another file with the same structure.
:paran coords: 2 tuple of slices (RECOMMANDED) or a 4 tuple (NOT RECOMMANDED, cryptic fabian convention)
:return: ROI-data from another file using positions from current EdfImage
:rtype: numpy 2darray
"""
Expand All @@ -1365,6 +1367,9 @@ def fast_read_roi(self, filename, coords=None):
with open(filename, "rb")as f:
f.seek(start)
raw = f.read(size)
if len(raw)<size:
# Pad with zero until the right size
raw+=b"\x00"*(size-len(raw))
try:
data = numpy.frombuffer(raw, dtype=self.bytecode).copy()
data.shape = -1, d1
Expand Down
3 changes: 2 additions & 1 deletion src/fabio/fabioimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "ESRF"
__date__ = "04/05/2021"
__date__ = "20/08/2024"

import os
import logging
Expand Down Expand Up @@ -169,6 +169,7 @@ def make_slice(self, coords):
the latter are immutable, meaning the roi can be cached
"""
assert len(coords) == 4
coords = list(coords)
if len(coords) == 4:
# fabian edfimage preference
if coords[0] > coords[2]:
Expand Down
23 changes: 23 additions & 0 deletions src/fabio/test/codecs/test_edfimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ def test_fastread(self):
obt = ref.fast_read_data(self.fastFilename)
self.assertEqual(abs(obt - refdata).max(), 0, "testedffastread: Same data")

def test_578(self):
"Non regression for issue 578: EDFImage.fast_read_data: cannot read a line"

file_name = os.path.join(UtilsTest.tempdir, "test_578.edf")
# create some dummy data
edf_writer = fabio.edfimage.EdfImage(
data=numpy.arange(0, 99).reshape(9, 11),
)
edf_writer.write(file_name)

edf_reader = fabio.open(file_name)

# if I want to read it line by line
line_index_to_read = 4
line_data = edf_reader.fast_read_roi(
file_name,coords=(
slice(3, 9),
slice(line_index_to_read, line_index_to_read+1),
),
)
self.assertTrue(numpy.allclose(edf_writer.data[3:9,line_index_to_read:line_index_to_read+1],
line_data))


class TestEdfWrite(unittest.TestCase):
"""
Expand Down
1 change: 0 additions & 1 deletion src/fabio/test/codecs/test_edfimage_expg.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,6 @@ def test_special(self):

# test files 10 and 11 for testing with to be copied and added later


def suite():
loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
testsuite = unittest.TestSuite()
Expand Down

0 comments on commit 8a3a5eb

Please sign in to comment.