Skip to content

Commit

Permalink
ref: restructure file_format submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Mar 23, 2022
1 parent 8aac6c0 commit a356df0
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 85 deletions.
5 changes: 3 additions & 2 deletions qpformat/file_formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from os.path import commonprefix
import pathlib

from .dataset import SeriesData, hash_obj
from .dataset import SingleData # noqa:F401 (user convenience)
from .series_base import SeriesData
from .single_base import SingleData # noqa:F401 (user convenience)
from .util import hash_obj
from . import fmts_ready, fmts_raw_oah


Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_raw_oah/series_hdf5_hyperspy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import h5py
import qpimage

from ..dataset import SeriesData
from ..series_base import SeriesData


class HyperSpyNoDataFoundError(BaseException):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_raw_oah/series_hdf5_raw_oah.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import qpimage

from ..dataset import SeriesData
from ..series_base import SeriesData


class SeriesHDF5RawOAH(SeriesData):
Expand Down
3 changes: 2 additions & 1 deletion qpformat/file_formats/fmts_raw_oah/series_zip_tif_holo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import numpy as np

from ..dataset import SeriesData
from ..series_base import SeriesData

from .single_tif_holo import SingleTifHolo


Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_raw_oah/single_hdf5_raw_oah.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import qpimage

from ..dataset import SingleData
from ..single_base import SingleData


class SingleHDF5RawOAH(SingleData):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_raw_oah/single_tif_holo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import qpimage
import tifffile

from ..dataset import SingleData
from ..single_base import SingleData


class SingleTifHolo(SingleData):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/series_hdf5_meep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import qpimage

from ..dataset import SeriesData
from ..series_base import SeriesData


class NoSinogramDataFoundError(BaseException):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/series_hdf5_qpimage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import h5py
import qpimage

from ..dataset import SeriesData
from ..series_base import SeriesData


class SeriesHdf5Qpimage(SeriesData):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/series_zip_tif_phasics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import functools
import zipfile

from ..dataset import SeriesData
from ..series_base import SeriesData
from .single_tif_phasics import SingleTifPhasics


Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/single_hdf5_qpimage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import h5py
import qpimage

from ..dataset import SingleData
from ..single_base import SingleData


class SingleHdf5Qpimage(SingleData):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/single_npy_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import qpimage

from ..dataset import SingleData
from ..single_base import SingleData


class SingleNpyNumpy(SingleData):
Expand Down
2 changes: 1 addition & 1 deletion qpformat/file_formats/fmts_ready/single_tif_phasics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import qpimage
import tifffile

from ..dataset import SingleData
from ..single_base import SingleData


# baseline clamp intensity normalization for phasics tif files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import abc
import copy
import functools
import hashlib
import io
import pathlib

import numpy as np
import qpimage

from .util import hash_obj


class SeriesData(object):
"""Series data file format base class
Expand Down Expand Up @@ -353,74 +354,3 @@ def verify(path):
memory efficient, because e.g. the "GroupFolder" file
format depends on it.
"""


class SingleData(SeriesData):
"""Single data file format base class
Parameters
----------
path: str or pathlib.Path
Path to the experimental data file.
meta_data: dict
Dictionary containing meta data.
see :py:class:`qpimage.META_KEYS`.
as_type: str
Defines the data type that the input data is casted to.
The default is "float32" which saves memory. If high
numerical accuracy is required (does not apply for a
simple 2D phase analysis), set this to double precision
("float64").
"""
__meta__ = abc.ABCMeta
is_series = False

def __len__(self):
return 1

def get_identifier(self, idx=0):
return self.identifier

def get_name(self, idx=0):
return super(SingleData, self).get_name(idx=0)

def get_qpimage(self, idx=0):
return super(SingleData, self).get_qpimage(idx=0)

@abc.abstractmethod
def get_qpimage_raw(self, idx=0):
"""QPImage without background correction"""

def get_time(self, idx=0):
"""Time of the data
Returns nan if the time is not defined
"""
thetime = super(SingleData, self).get_time(idx=0)
return thetime


def hash_obj(data, maxlen=5):
hasher = hashlib.md5()
tohash = obj2bytes(data)
hasher.update(tohash)
return hasher.hexdigest()[:maxlen]


def obj2bytes(data):
tohash = []
if isinstance(data, (tuple, list)):
for item in data:
tohash.append(obj2bytes(item))
elif isinstance(data, str):
tohash.append(data.encode("utf-8"))
elif isinstance(data, bytes):
tohash.append(data)
elif isinstance(data, np.ndarray):
tohash.append(data.tobytes())
elif isinstance(data, int):
tohash.append(bytes(data))
else:
msg = "No rule to convert to bytes: {}".format(data)
raise NotImplementedError(msg)
return b"".join(tohash)
48 changes: 48 additions & 0 deletions qpformat/file_formats/single_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import abc

from .series_base import SeriesData


class SingleData(SeriesData):
"""Single data file format base class
Parameters
----------
path: str or pathlib.Path
Path to the experimental data file.
meta_data: dict
Dictionary containing meta data.
see :py:class:`qpimage.META_KEYS`.
as_type: str
Defines the data type that the input data is casted to.
The default is "float32" which saves memory. If high
numerical accuracy is required (does not apply for a
simple 2D phase analysis), set this to double precision
("float64").
"""
__meta__ = abc.ABCMeta
is_series = False

def __len__(self):
return 1

def get_identifier(self, idx=0):
return self.identifier

def get_name(self, idx=0):
return super(SingleData, self).get_name(idx=0)

def get_qpimage(self, idx=0):
return super(SingleData, self).get_qpimage(idx=0)

@abc.abstractmethod
def get_qpimage_raw(self, idx=0):
"""QPImage without background correction"""

def get_time(self, idx=0):
"""Time of the data
Returns nan if the time is not defined
"""
thetime = super(SingleData, self).get_time(idx=0)
return thetime
29 changes: 29 additions & 0 deletions qpformat/file_formats/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import hashlib

import numpy as np


def hash_obj(data, maxlen=5):
hasher = hashlib.md5()
tohash = obj2bytes(data)
hasher.update(tohash)
return hasher.hexdigest()[:maxlen]


def obj2bytes(data):
tohash = []
if isinstance(data, (tuple, list)):
for item in data:
tohash.append(obj2bytes(item))
elif isinstance(data, str):
tohash.append(data.encode("utf-8"))
elif isinstance(data, bytes):
tohash.append(data)
elif isinstance(data, np.ndarray):
tohash.append(data.tobytes())
elif isinstance(data, int):
tohash.append(bytes(data))
else:
msg = "No rule to convert to bytes: {}".format(data)
raise NotImplementedError(msg)
return b"".join(tohash)
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest

0 comments on commit a356df0

Please sign in to comment.