Skip to content

Commit 4a90f1d

Browse files
committed
Update tests/test_liffile.py
1 parent 28eece0 commit 4a90f1d

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

tests/test_liffile.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
"""Unittests for the liffile package.
3131
32-
:Version: 2025.11.8
32+
:Version: 2025.12.12
3333
3434
"""
3535

@@ -54,7 +54,7 @@
5454
try:
5555
import fsspec
5656
except ImportError:
57-
fsspec = None # type: ignore[assignment]
57+
fsspec = None
5858

5959
import liffile
6060
from liffile import (
@@ -875,6 +875,7 @@ def validate(
875875
filename: str | None = None,
876876
dirname: str | None = None,
877877
name: str | None = None,
878+
*,
878879
closed: bool = True,
879880
) -> None:
880881
"""Assert BinaryFile attributes."""
@@ -887,6 +888,10 @@ def validate(
887888
if name is None:
888889
name = fh.filename
889890

891+
attrs = fh.attrs
892+
assert attrs['name'] == name
893+
assert attrs['filepath'] == filepath
894+
890895
assert fh.filepath == filepath
891896
assert fh.filename == filename
892897
assert fh.dirname == dirname
@@ -913,9 +918,8 @@ def test_pathlib(self):
913918

914919
def test_open_file(self):
915920
"""Test BinaryFile with open binary file."""
916-
with open(self.fname, 'rb') as fh:
917-
with BinaryFile(fh) as bf:
918-
self.validate(bf, closed=False)
921+
with open(self.fname, 'rb') as fh, BinaryFile(fh) as bf:
922+
self.validate(bf, closed=False)
919923

920924
def test_bytesio(self):
921925
"""Test BinaryFile with BytesIO."""
@@ -946,8 +950,8 @@ def test_fsspec_localfileopener(self):
946950

947951
def test_text_file_fails(self):
948952
"""Test BinaryFile with open text file fails."""
949-
with open(self.fname) as fh:
950-
with pytest.raises(ValueError):
953+
with open(self.fname) as fh: # noqa: SIM117
954+
with pytest.raises(TypeError):
951955
BinaryFile(fh)
952956

953957
def test_file_extension_fails(self):
@@ -978,6 +982,7 @@ class File:
978982
# mock fsspec OpenFile without seek/tell methods
979983
@staticmethod
980984
def open(*args, **kwargs):
985+
del args, kwargs
981986
return File()
982987

983988
with pytest.raises(ValueError):
@@ -1043,9 +1048,8 @@ def test_pathlib(self):
10431048

10441049
def test_open_file(self):
10451050
"""Test LifFile with open binary file."""
1046-
with open(self.fname, 'rb') as fh:
1047-
with LifFile(fh) as lif:
1048-
self.validate(lif)
1051+
with open(self.fname, 'rb') as fh, LifFile(fh) as lif:
1052+
self.validate(lif)
10491053

10501054
def test_bytesio(self):
10511055
"""Test LifFile with BytesIO."""
@@ -1099,7 +1103,9 @@ def test_imread(asxarray):
10991103
def test_lif(filetype):
11001104
"""Test LIF file."""
11011105
filename = DATA / 'ScanModesExamples.lif'
1102-
file = filename if filetype is str else open(filename, 'rb')
1106+
file = (
1107+
filename if filetype is str else open(filename, 'rb') # noqa: SIM115
1108+
)
11031109

11041110
with LifFile(file, mode='r+b', squeeze=True) as lif:
11051111
str(lif)
@@ -1175,6 +1181,10 @@ def test_lif(filetype):
11751181
assert im.ndim == 5
11761182
assert isinstance(im.xml_element, ElementTree.Element)
11771183

1184+
attrs = im.attrs
1185+
assert attrs['filepath'] == im.parent.filepath
1186+
assert attrs['name'] == im.name
1187+
11781188
attrs = im.attrs['HardwareSetting']
11791189
assert attrs['Software'] == 'LAS-AF [ BETA ] 3.3.0.10067'
11801190
assert attrs['ATLConfocalSettingDefinition']['LineTime'] == 0.0025
@@ -1673,7 +1683,6 @@ def test_flim(name):
16731683
assert len(flim.child_images) == 9
16741684
assert lif.uuid == 'd9f87ad9-b958-11ed-bb27-00506220277a'
16751685
assert flim.is_flim
1676-
# assert flim.xml_element_smd == flim.xml_element
16771686
assert flim.dtype == numpy.uint16
16781687
assert flim.sizes == {'Y': 1024, 'X': 1024, 'H': 528}
16791688
assert pytest.approx(flim.coords['X'][-1]) == 0.0005563808000453999
@@ -1691,7 +1700,7 @@ def test_flim(name):
16911700
assert len(flim.timestamps) == 0
16921701
if name == 'XLEF_TIF/FLIM_testdata.xlef':
16931702
# TIFF file is actually a LOF file
1694-
with pytest.raises(Exception):
1703+
with pytest.raises(RuntimeError):
16951704
flim.memory_block.read()
16961705
else:
16971706
assert len(flim.memory_block.read()) == 15502568
@@ -1701,9 +1710,7 @@ def test_flim(name):
17011710
intensity = lif.images['/Intensity']
17021711
assert intensity in flim.child_images
17031712
assert intensity.parent_image is flim
1704-
if 'FLIM_testdata.lif' in name:
1705-
with pytest.warns(DeprecationWarning):
1706-
assert intensity.xml_element_smd is not None
1713+
17071714
data = intensity.asxarray()
17081715
assert data.shape == (1024, 1024)
17091716
assert data.dtype == numpy.float32
@@ -1712,9 +1719,7 @@ def test_flim(name):
17121719
mean = lif.images['Phasor Intensity$']
17131720
assert mean in flim.child_images
17141721
assert mean.parent_image is flim
1715-
if 'FLIM_testdata.lif' in name:
1716-
with pytest.warns(DeprecationWarning):
1717-
assert mean.xml_element_smd is not None
1722+
17181723
data = mean.asxarray()
17191724
assert data.shape == (1024, 1024)
17201725
assert data.dtype == numpy.float16
@@ -1723,9 +1728,7 @@ def test_flim(name):
17231728
real = lif.images['Phasor Real']
17241729
assert real in flim.child_images
17251730
assert real.parent_image is flim
1726-
if 'FLIM_testdata.lif' in name:
1727-
with pytest.warns(DeprecationWarning):
1728-
assert real.xml_element_smd is not None
1731+
17291732
data = real.asxarray()
17301733
assert data.shape == (1024, 1024)
17311734
assert data.dtype == numpy.float16
@@ -1734,9 +1737,7 @@ def test_flim(name):
17341737
lifetime = lif.images['Fast Flim']
17351738
assert lifetime in flim.child_images
17361739
assert lifetime.parent_image is flim
1737-
if 'FLIM_testdata.lif' in name:
1738-
with pytest.warns(DeprecationWarning):
1739-
assert lifetime.xml_element_smd is not None
1740+
17401741
data = lifetime.asxarray()
17411742
assert data.shape == (1024, 1024)
17421743
assert data.dtype == numpy.float16
@@ -1745,9 +1746,7 @@ def test_flim(name):
17451746
mask = lif.images['Phasor Mask']
17461747
assert mask in flim.child_images
17471748
assert mask.parent_image is flim
1748-
if 'FLIM_testdata.lif' in name:
1749-
with pytest.warns(DeprecationWarning):
1750-
assert mask.xml_element_smd is not None
1749+
17511750
data = mask.asxarray()
17521751
assert data.shape == (1024, 1024)
17531752
assert data.dtype == numpy.uint32
@@ -1781,7 +1780,6 @@ def test_flim_nd(name):
17811780
assert len(flim.child_images) == 10
17821781
assert lif.uuid == '4441a913-c99c-11ee-b559-98597a5338f0'
17831782
assert flim.is_flim
1784-
# assert flim.xml_element_smd == flim.xml_element
17851783
assert flim.dtype == numpy.uint16
17861784
assert flim.sizes == {
17871785
'T': 10,
@@ -1842,8 +1840,6 @@ def test_flim_lof():
18421840
assert isinstance(flim, LifFlimImage)
18431841
assert flim.uuid == '4a942888-fa9c-11eb-913c-a4bb6dd5b508'
18441842
assert flim.is_flim
1845-
with pytest.warns(DeprecationWarning):
1846-
assert flim.xml_element_smd == flim.xml_element
18471843
assert flim.dtype == numpy.uint16
18481844
assert flim.sizes == {'C': 2, 'Y': 512, 'X': 512, 'H': 132}
18491845
assert pytest.approx(flim.coords['X'][-1]) == 0.0011624999998359998
@@ -1914,8 +1910,8 @@ def test_rgb_pad():
19141910
assert data.shape == (531, 531, 3)
19151911
assert data.sum(dtype=numpy.uint64) == 676414
19161912

1913+
out = numpy.zeros((531, 531, 3), numpy.uint8)
19171914
with pytest.raises(ValueError):
1918-
out = numpy.zeros((531, 531, 3), numpy.uint8)
19191915
lif.images[1].asarray(out=out)
19201916

19211917

@@ -1928,7 +1924,7 @@ def test_output(output, asxarray):
19281924
if output == 'ndarray':
19291925
out = numpy.zeros((86, 2, 500, 616), numpy.uint16)
19301926
elif output == 'fname':
1931-
out = tempfile.TemporaryFile()
1927+
out = tempfile.TemporaryFile() # noqa: SIM115
19321928
elif output == 'memmap:.':
19331929
out = output
19341930
else:
@@ -1998,7 +1994,7 @@ def test_phasor_from_lif():
19981994
assert 'harmonic' not in attrs
19991995

20001996
# select series
2001-
mean1, real1, imag1, attrs = phasor_from_lif(
1997+
mean1, _real1, _imag1, attrs = phasor_from_lif(
20021998
filename, image='FLIM Compressed'
20031999
)
20042000
assert_array_equal(mean1, mean)
@@ -2100,7 +2096,7 @@ def test_gil_enabled():
21002096
'fname',
21012097
itertools.chain.from_iterable(
21022098
glob.glob(f'**/*{ext}', root_dir=DATA, recursive=True)
2103-
for ext in FILE_EXTENSIONS.keys()
2099+
for ext in FILE_EXTENSIONS
21042100
),
21052101
)
21062102
def test_glob(fname):
@@ -2125,9 +2121,7 @@ def test_glob(fname):
21252121
image.asxarray()
21262122
else:
21272123
image.asxarray()
2128-
image.timestamps
2129-
with pytest.warns(DeprecationWarning):
2130-
image.xml_element_smd
2124+
_ = image.timestamps
21312125

21322126

21332127
if __name__ == '__main__':

0 commit comments

Comments
 (0)