Skip to content

Commit

Permalink
refactor commands for reuse in test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
austinorr committed May 4, 2024
1 parent 9b553f7 commit 566a1a9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/hsp2-pip-install-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 3 additions & 54 deletions HSP2tools/HSP2_CLI.py
Original file line number Diff line number Diff line change
@@ -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()


Expand Down
6 changes: 3 additions & 3 deletions HSP2tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from importlib.metadata import version

from HSP2tools.readHBN import readHBN
from HSP2tools.readUCI import readUCI
from HSP2tools.readWDM import readWDM
Expand All @@ -9,6 +11,4 @@
from HSP2tools.graph import HDF5_isconnected, component_list, color_graph



import HSP2
__version__ = HSP2.__version__
__version__ = version('hsp2')
59 changes: 59 additions & 0 deletions HSP2tools/commands.py
Original file line number Diff line number Diff line change
@@ -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)
59 changes: 1 addition & 58 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 566a1a9

Please sign in to comment.