Skip to content

Commit

Permalink
h5py supports pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jun 21, 2018
1 parent fe639ff commit b69aaff
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 19 deletions.
9 changes: 5 additions & 4 deletions qpformat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
def guess_format(path):
"""Determine the file format of a folder or a file"""
for fmt in formats:
if fmt.verify(str(path)):
if fmt.verify(path):
return fmt.__name__
else:
raise UnknownFileFormatError(str(path))
msg = "Undefined file format: '{}'".format(path)
raise UnknownFileFormatError(msg)


def load_data(path, fmt=None, bg_data=None, bg_fmt=None,
Expand Down Expand Up @@ -56,7 +57,7 @@ def load_data(path, fmt=None, bg_data=None, bg_fmt=None,
if fmt is None:
fmt = guess_format(path)

dataobj = formats_dict[fmt](path=str(path),
dataobj = formats_dict[fmt](path=path,
meta_data=meta_data,
holo_kw=holo_kw,
as_type=as_type)
Expand All @@ -70,7 +71,7 @@ def load_data(path, fmt=None, bg_data=None, bg_fmt=None,
bg_path = pathlib.Path(bg_data).resolve()
if bg_fmt is None:
bg_fmt = guess_format(bg_path)
bgobj = formats_dict[bg_fmt](path=str(bg_path),
bgobj = formats_dict[bg_fmt](path=bg_path,
meta_data=meta_data,
holo_kw=holo_kw,
as_type=as_type)
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/series_hdf5_hyperspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def verify(path):
"""
valid = False
try:
h5 = h5py.File(str(path), mode="r")
h5 = h5py.File(path, mode="r")
except (OSError,):
pass
else:
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/series_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def verify(path):
"""
valid = False
try:
h5 = h5py.File(str(path), mode="r")
h5 = h5py.File(path, mode="r")
qpi0 = h5["qpi_0"]
except (OSError, KeyError):
pass
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/single_hdf5_qpimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def verify(path):
"""
valid = False
try:
h5 = h5py.File(str(path), mode="r")
h5 = h5py.File(path, mode="r")
except (OSError,):
pass
else:
Expand Down
8 changes: 4 additions & 4 deletions qpformat/file_formats/single_tif_holo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import copy
import pathlib
from os import fspath

import qpimage
from skimage.external import tifffile
Expand All @@ -13,12 +13,12 @@ class SingleTifHolo(SingleData):

@staticmethod
def _get_tif(path):
if isinstance(path, pathlib.Path):
path = str(path)
elif not isinstance(path, str):
if hasattr(path, "seek"): # opened file
# Seek open file zero to avoid error in tifffile:
# "ValueError: invalid TIFF file"
path.seek(0)
else: # path
path = fspath(path)
return tifffile.TiffFile(path)

def get_qpimage_raw(self, idx=0):
Expand Down
10 changes: 5 additions & 5 deletions qpformat/file_formats/single_tif_phasics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import calendar
import copy
import pathlib
from os import fspath
import time
import xml.etree.ElementTree as ET

Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self, path, meta_data={}, *args, **kwargs):
@staticmethod
def _get_meta_data(path, section, name):
with SingleTifPhasics._get_tif(path) as tf:
meta = str(tf.pages[0].tags["61238"].value)
meta = tf.pages[0].tags["61238"].as_str()

meta = meta.strip("'b")
meta = meta.replace("\\n", "\n")
Expand All @@ -81,12 +81,12 @@ def _get_meta_data(path, section, name):

@staticmethod
def _get_tif(path):
if isinstance(path, pathlib.Path):
path = str(path)
elif not isinstance(path, str):
if hasattr(path, "seek"): # opened file
# Seek open file zero to avoid error in tifffile:
# "ValueError: invalid TIFF file"
path.seek(0)
else: # path
path = fspath(path)
return tifffile.TiffFile(path)

def get_qpimage_raw(self, idx=0):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
license="MIT",
description=description,
long_description=open('README.rst').read() if exists('README.rst') else '',
install_requires=["nrefocus>=0.1.5",
install_requires=["h5py>=2.7.0",
"nrefocus>=0.1.5",
"numpy>=1.12.0",
"qpimage",
"scikit-image>=0.11.0",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_single_tif_holo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_basic():
# basic tests
assert ds.storage_type == "hologram"
assert not ds.is_series
assert ds.path == path
assert ds.path == path.resolve()
assert "SingleTifHolo" in ds.__repr__()


Expand Down
2 changes: 1 addition & 1 deletion tests/test_single_tif_phasics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
def test_load_data():
path = datapath / "single_phasics.tif"
ds = qpformat.load_data(path)
assert pathlib.Path(ds.path) == path
assert pathlib.Path(ds.path) == path.resolve()
assert "SingleTifPhasics" in ds.__repr__()


Expand Down

0 comments on commit b69aaff

Please sign in to comment.