Skip to content

Commit

Permalink
TIFF format
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaehne committed Nov 5, 2022
1 parent af57281 commit f7f6abe
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
8 changes: 7 additions & 1 deletion docs/supported_formats/tiff.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.. _tiff-format:

Tagges image file format (TIFF)
Tagged image file format (TIFF)
-------------------------------

RosettaSciIO can read and write 2D and 3D ``.tiff`` files using using
Expand All @@ -23,6 +23,12 @@ RosettaSciIO can also import the scale and the units from ``.tiff`` files saved
FEI, Zeiss SEM, Olympus SIS, Jeol SightX and Hamamatsu HPD-TA (streak camera)
software.

API functions
^^^^^^^^^^^^^

.. automodule:: rsciio.blockfile
:members:

Extra loading arguments
^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion rsciio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
with open(_specsf, "r") as stream:
_specs = yaml.safe_load(stream)
# for testing purposes
if _specs["name"] in ["Blockfile", "BrukerComposite", "Semper"]:
if _specs["name"] in ["Blockfile", "BrukerComposite", "Semper", "TIFF"]:
_specs["api"] = "rsciio.%s" % os.path.split(sub)[1]
else:
_specs["api"] = "rsciio.%s.api" % os.path.split(sub)[1]
Expand Down
12 changes: 11 additions & 1 deletion rsciio/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,18 @@ def test_dir_plugins():

assert dir(bruker) == ["file_reader"]

# skimage is optional dependencies
# skimage is an optional dependency
pytest.importorskip("skimage")
from rsciio import blockfile

assert dir(blockfile) == ["file_reader", "file_writer"]

from rsciio import semper_unf

assert dir(semper_unf) == ["file_reader", "file_writer"]

# tifffile is an optional dependency
pytest.importorskip("tifffile")
from rsciio import tiff

assert dir(tiff) == ["file_reader", "file_writer"]
24 changes: 12 additions & 12 deletions rsciio/tests/test_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import pytest

tifffile = pytest.importorskip("tifffile", reason="tifffile not installed")
hs = pytest.importorskip("hyperspy.api", reason="hyperspy not installed")
hs = pytest.importorskip("hyperspy._api", reason="hyperspy not installed")

import traits.api as t

Expand Down Expand Up @@ -414,7 +414,7 @@ def test_saving_loading_stack_no_scale():
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.tiff.api",
"io_plugin": "rsciio.tiff._api",
}
},
},
Expand Down Expand Up @@ -444,7 +444,7 @@ def test_saving_loading_stack_no_scale():
"date": "2022-05-17",
"time": "09:07:08",
"authors": "user",
"FileIO": {"0": {"operation": "load", "io_plugin": "rsciio.tiff.api"}},
"FileIO": {"0": {"operation": "load", "io_plugin": "rsciio.tiff._api"}},
},
"Signal": {"signal_type": ""},
"Acquisition_instrument": {
Expand Down Expand Up @@ -589,7 +589,7 @@ def test_read_Zeiss_SEM_scale_metadata_1k_image(self):
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.tiff.api",
"io_plugin": "rsciio.tiff._api",
}
},
},
Expand Down Expand Up @@ -643,7 +643,7 @@ def test_read_Zeiss_SEM_scale_metadata_512_image(self):
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.tiff.api",
"io_plugin": "rsciio.tiff._api",
}
},
},
Expand Down Expand Up @@ -685,7 +685,7 @@ def test_read_RGB_Zeiss_optical_scale_metadata():
assert s.metadata.General.date == "2016-06-13"
assert s.metadata.General.time == "15:59:52"
assert s.metadata.General.FileIO.Number_0.hyperspy_version == hs.__version__
assert s.metadata.General.FileIO.Number_0.io_plugin == "rsciio.tiff.api"
assert s.metadata.General.FileIO.Number_0.io_plugin == "rsciio.tiff._api"


def test_read_BW_Zeiss_optical_scale_metadata():
Expand Down Expand Up @@ -762,7 +762,7 @@ def test_read_TVIPS_metadata():
"0": {
"operation": "load",
"hyperspy_version": hs.__version__,
"io_plugin": "rsciio.tiff.api",
"io_plugin": "rsciio.tiff._api",
}
},
},
Expand Down Expand Up @@ -982,22 +982,22 @@ def test_is_hamamatsu_streak(self):

omd["Artist"] = "TAPTAP"

assert not rsciio.tiff.api._is_streak_hamamatsu(omd)
assert not rsciio.tiff._api._is_streak_hamamatsu(omd)

_ = omd.pop("Artist")

assert not rsciio.tiff.api._is_streak_hamamatsu(omd)
assert not rsciio.tiff._api._is_streak_hamamatsu(omd)

omd.update({"Artist": "Copyright Hamamatsu GmbH, 2018"})

omd["Software"] = "TAPTAPTAP"

assert not rsciio.tiff.api._is_streak_hamamatsu(omd)
assert not rsciio.tiff._api._is_streak_hamamatsu(omd)

_ = omd.pop("Software")

assert not rsciio.tiff.api._is_streak_hamamatsu(omd)
assert not rsciio.tiff._api._is_streak_hamamatsu(omd)

omd.update({"Software": "HPD-TA 9.5 pf4"})

assert rsciio.tiff.api._is_streak_hamamatsu(omd)
assert rsciio.tiff._api._is_streak_hamamatsu(omd)
11 changes: 11 additions & 0 deletions rsciio/tiff/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ._api import file_reader, file_writer


__all__ = [
"file_reader",
"file_writer",
]


def __dir__():
return sorted(__all__)
28 changes: 17 additions & 11 deletions rsciio/tiff/api.py → rsciio/tiff/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@


def file_writer(filename, signal, export_scale=True, extratags=[], **kwds):
"""Writes data to tif using Christoph Gohlke's tifffile library
"""Writes data to tif using Christoph Gohlke's tifffile library.
Parameters
----------
filename: str
signal: a BaseSignal instance
export_scale: bool
default: True
Filename of the file to write to.
signal: dict
Dictionary containing the signal object as defined in the `API guide
<https://hyperspy.org/rosettasciio/api.html>`_.
export_scale: bool, default=True
Export the scale and the units (compatible with DM and ImageJ) to
appropriate tags.
**kwds, optional
Additional arguments to be passed to the `tifffile library
<https://github.com/cgohlke/tifffile/#examples>`_
"""

data = signal["data"]
Expand Down Expand Up @@ -103,7 +108,7 @@ def file_writer(filename, signal, export_scale=True, extratags=[], **kwds):
imwrite(filename, data, software="hyperspy", photometric=photometric, **kwds)


def file_reader(filename, force_read_resolution=False, lazy=False, **kwds):
def file_reader(filename, lazy=False, force_read_resolution=False, **kwds):
"""
Read data from tif files using Christoph Gohlke's tifffile library.
The units and the scale of images saved with ImageJ or Digital
Expand All @@ -113,15 +118,16 @@ def file_reader(filename, force_read_resolution=False, lazy=False, **kwds):
Parameters
----------
filename: str
Name of the file to read
force_read_resolution: bool
Name of the file to read.
lazy : bool, Default=False
Load the data lazily.
force_read_resolution: bool, Default=False
Force reading the x_resolution, y_resolution and the resolution_unit
of the tiff tags.
See https://www.awaresystems.be/imaging/tiff/tifftags/resolutionunit.html
Default is False.
lazy : bool
Load the data lazily. Default is False
See `<https://www.awaresystems.be/imaging/tiff/tifftags/resolutionunit.html>`_
**kwds, optional
Additional arguments to be passed to the `tifffile library
<https://github.com/cgohlke/tifffile/#examples>`_
"""
tmp = kwds.pop("hamamatsu_streak_axis_type", None)

Expand Down

0 comments on commit f7f6abe

Please sign in to comment.