Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401, F405
max-line-length = 120
max-complexity = 18
select = C,E,F,W,B,B950
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ repos:
rev: 6.0.0
hooks:
- id: flake8
args: [--config=.flake8]
29 changes: 14 additions & 15 deletions src/modacor/io/hdf/hdf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
__status__ = "Alpha"


from ..io_source import IoSource
from ..io_sources import IoSources
from modacor.dataclasses.messagehandler import *

from os.path import abspath
from logging import WARNING
import numpy as np
from os.path import abspath

import h5py
import numpy as np

from modacor.dataclasses.messagehandler import *

from ..io_source import IoSource
from ..io_sources import IoSources


class HDFLoader(IoSource):
Expand All @@ -46,22 +48,20 @@ def __init__(self, source_reference: str, logging_level = WARNING):
self._file_datasets = []
self._file_datasets_shapes = {}


def _open_file(self, file_path = None):
def _open_file(self, file_path=None):
if file_path is None:
error = 'No filepath given'
error = "No filepath given"
self.hdf_logger.log.error(error)
raise OSError(error)

try:
self._file_reference = h5py.File(file_path, 'r')
self._file_reference = h5py.File(file_path, "r")
self._file_path = abspath(file_path)
self._file_reference.visititems(self._find_datasets)
except OSError as error:
self.hdf_logger.logger.error(error)
raise OSError(error)


def _close_file(self):
try:
self._file_reference.close()
Expand All @@ -73,9 +73,8 @@ def _close_file(self):
self.hdf_logger.log.error(error)
raise OSError(error)


def _find_datasets(self, path_name, path_object):
"""An internal function to be used to walk the tree of an HDF5 file and return a list of the datasets within"""
if(isinstance(self._file_reference[path_name], h5py._hl.dataset.Dataset)):
if isinstance(self._file_reference[path_name], h5py._hl.dataset.Dataset):
self._file_datasets.append(path_name)
self._file_datasets_shapes[path_name] = self._file_reference[path_name].shape
self._file_datasets_shapes[path_name] = self._file_reference[path_name].shape
11 changes: 4 additions & 7 deletions src/modacor/tests/io/hdf/test_hdf_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
__copyright__ = "Copyright 2025 MoDaCor Authors"
__status__ = "Alpha"

from modacor.io.hdf.hdf_loader import *

from os.path import abspath
import unittest
from logging import WARNING
from os.path import abspath
from os import unlink
import numpy as np
import tempfile
import unittest
import h5py
import numpy as np

from ....io.hdf.hdf_loader import *


class TestHDFLoader(unittest.TestCase):
Expand All @@ -52,23 +53,19 @@ def setUp(self):
self.temp_hdf_file[self.temp_dataset_name] = np.zeros(self.temp_dataset_shape)
self.temp_file_handle.close()


def tearDown(self):
self.test_h5_loader = None
self.test_file_path = None
self.test_dataset_name = None
self.test_dataset_shape = None
unlink(self.temp_file_path)


def test_open_file(self):
self.test_hdf_loader._open_file(self.temp_file_path)

self.assertEqual(self.temp_file_path, self.test_hdf_loader._file_path)
self.assertEqual(self.temp_dataset_name, self.test_hdf_loader._file_datasets[0])
self.assertEqual(self.temp_dataset_shape, self.test_hdf_loader._file_datasets_shapes[self.temp_dataset_name])


def test_close_file(self):
self.test_open_file()
self.test_hdf_loader._close_file()
Expand Down
29 changes: 29 additions & 0 deletions src/modacor/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""try to import all sub-modules from the project"""

import importlib
import os

dirname = os.path.dirname


def test_import_all():
project_dir = dirname(dirname(os.path.abspath(__file__)))
start = len(project_dir) - len("modacor")
modules = []
for path, dirs, files in os.walk(project_dir):
for f in files:
if f.endswith(".py") and not f.startswith("__"):
modules.append(os.path.join(path[start:], f[:-3]))
cnt = 0
for i in modules:
j = i.replace(os.sep, ".")
try:
_ = importlib.import_module(j)
except Exception as err:
print(f"{type(err)} in {j}: {err}.")
cnt += 1
assert cnt == 0


if __name__ == "__main__":
test_import_all()
2 changes: 1 addition & 1 deletion src/modacor/tests/test_variance_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from uncertainties.unumpy import nominal_values, std_devs, uarray

import modacor.math.variance_calculations as varc
from ..math import variance_calculations as varc

samples = 1000

Expand Down
Loading