diff --git a/.github/workflows/hsp2-pip-install-test.yml b/.github/workflows/hsp2-pip-install-test.yml index 843e66c0..874ebe11 100644 --- a/.github/workflows/hsp2-pip-install-test.yml +++ b/.github/workflows/hsp2-pip-install-test.yml @@ -27,7 +27,7 @@ jobs: - name: Install hsp2 run: | # install the hsp2 executable - pip install . + pip install .[dev] - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names diff --git a/HSP2tools/HSP2_CLI.py b/HSP2tools/HSP2_CLI.py index 4d1d94e6..5d520a29 100644 --- a/HSP2tools/HSP2_CLI.py +++ b/HSP2tools/HSP2_CLI.py @@ -1,62 +1,11 @@ import cltoolbox -from HSP2.main import main as hsp2main -from HSP2tools.readUCI import readUCI -from HSP2tools.readWDM import readWDM -from HSP2IO.hdf import HDF5 -from HSP2IO.io import IOManager - - -@cltoolbox.command() -def run(hdfname, saveall=True, jupyterlab=False): - """Run a HSPsquared model. - - Parameters - ---------- - hdfname: str - HDF5 (path) filename used for both input and output. - saveall: bool - [optional] Default is False. - Saves all calculated data ignoring SAVE tables. - jupyterlab: bool - Jupyterlab - """ - hdf5_instance = HDF5(hdfname) - io_manager = IOManager(hdf5_instance) - hsp2main(io_manager, saveall=saveall, jupyterlab=jupyterlab) - - -@cltoolbox.command() -def import_uci(ucifile, h5file): - """Import UCI and WDM files into HDF5 file. - - Parameters - ---------- - ucifile: str - The UCI file to import into HDF file. - h5file: str - The destination HDF5 file. - """ - readUCI(ucifile, h5file) - - with open(ucifile, "r") as fp: - uci = [] - for line in fp.readlines(): - if '***' in line[:81]: - continue - if not line[:81].strip(): - continue - uci.append(line[:81].rstrip()) - - files_start = uci.index("FILES") - files_end = uci.index("END FILES") - - for nline in uci[files_start: files_end+1]: - if (nline[:10].strip())[:3] == "WDM": - readWDM(nline[16:].strip(), h5file) +from HSP2tools.commands import import_uci, run def main(): + cltoolbox.command(run) + cltoolbox.command(import_uci) cltoolbox.main() diff --git a/HSP2tools/__init__.py b/HSP2tools/__init__.py index 641b1b7d..6287f9cd 100644 --- a/HSP2tools/__init__.py +++ b/HSP2tools/__init__.py @@ -1,3 +1,5 @@ +from importlib.metadata import version + from HSP2tools.readHBN import readHBN from HSP2tools.readUCI import readUCI from HSP2tools.readWDM import readWDM @@ -9,6 +11,4 @@ from HSP2tools.graph import HDF5_isconnected, component_list, color_graph - -import HSP2 -__version__ = HSP2.__version__ +__version__ = version('hsp2') diff --git a/HSP2tools/commands.py b/HSP2tools/commands.py new file mode 100644 index 00000000..98ac8e1f --- /dev/null +++ b/HSP2tools/commands.py @@ -0,0 +1,59 @@ +from pathlib import Path + +from HSP2.main import main +from HSP2tools.readUCI import readUCI +from HSP2tools.readWDM import readWDM +from HSP2IO.hdf import HDF5 +from HSP2IO.io import IOManager + + +def run(h5file, saveall=True, compress=True): + """Run a HSPsquared model. + + Parameters + ---------- + h5file: str + HDF5 (path) filename used for both input and output. + saveall: bool + [optional] Default is True. + Saves all calculated data ignoring SAVE tables. + compression: bool + [optional] Default is True. + use compression on the save h5 file. + """ + hdf5_instance = HDF5(h5file) + io_manager = IOManager(hdf5_instance) + main(io_manager, saveall=saveall, jupyterlab=compress) + + +def import_uci(ucifile, h5file): + """Import UCI and WDM files into HDF5 file. + + Parameters + ---------- + ucifile: str + The UCI file to import into HDF file. + h5file: str + The destination HDF5 file. + """ + + readUCI(ucifile, h5file) + + with open(ucifile, "r") as fp: + uci = [] + for line in fp.readlines(): + if "***" in line[:81]: + continue + if not line[:81].strip(): + continue + uci.append(line[:81].rstrip()) + + files_start = uci.index("FILES") + files_end = uci.index("END FILES") + + uci_dir = Path(ucifile).parent + for nline in uci[files_start : files_end + 1]: + if (nline[:10].strip())[:3] == "WDM": + wdmfile = (uci_dir / nline[16:].strip()).resolve() + if wdmfile.exists(): + readWDM(wdmfile, h5file) diff --git a/tests/test_regression.py b/tests/test_regression.py index edd610bf..2573b6b3 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -2,69 +2,12 @@ import pytest -from HSP2.main import main -from HSP2tools.readUCI import readUCI -from HSP2tools.readWDM import readWDM -from HSP2IO import hdf +from HSP2tools.commands import import_uci, run from HSP2tools.HDF5 import HDF5 -from HSP2IO.io import IOManager - from .convert.regression_base import RegressTest as RegressTestBase -def import_uci(ucifile, h5file): - """Import UCI and WDM files into HDF5 file. - - Parameters - ---------- - ucifile: str - The UCI file to import into HDF file. - h5file: str - The destination HDF5 file. - """ - - readUCI(ucifile, h5file) - - with open(ucifile, "r") as fp: - uci = [] - for line in fp.readlines(): - if "***" in line[:81]: - continue - if not line[:81].strip(): - continue - uci.append(line[:81].rstrip()) - - files_start = uci.index("FILES") - files_end = uci.index("END FILES") - - uci_dir = Path(ucifile).parent - for nline in uci[files_start : files_end + 1]: - if (nline[:10].strip())[:3] == "WDM": - wdmfile = (uci_dir / nline[16:].strip()).resolve() - if wdmfile.exists(): - readWDM(wdmfile, h5file) - - -def run(h5file, saveall=True, compress=True): - """Run a HSPsquared model. - - Parameters - ---------- - h5file: str - HDF5 (path) filename used for both input and output. - saveall: bool - [optional] Default is True. - Saves all calculated data ignoring SAVE tables. - compression: bool - [optional] Default is True. - use compression on the save h5 file. - """ - hdf5_instance = hdf.HDF5(h5file) - io_manager = IOManager(hdf5_instance) - main(io_manager, saveall=saveall, jupyterlab=compress) - - class RegressTest(RegressTestBase): def _get_hsp2_data(self, test_root) -> None: test_root_hspf = Path(test_root) / "HSPFresults"