Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathlib to models module and Windows and MacOS added to CI #25

Merged
merged 2 commits into from
Apr 23, 2024
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
43 changes: 42 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- workflow_dispatch

jobs:
test:
test-linux:

runs-on: ubuntu-latest
strategy:
Expand All @@ -26,3 +26,44 @@ jobs:
run: pip install tox tox-gh-actions
- name: Test with tox
run: tox -r

test-windows:

runs-on: windows-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@master
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version}}
architecture: x64
- name: Install pandoc
run: choco install pandoc
- name: Install tox
run: pip install tox tox-gh-actions
- name: Test with tox
run: tox -r

test-macos:

runs-on: macOS-latest
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@master
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version}}
- name: Install pandoc
run: brew install pandoc
- name: Install tox
run: pip install tox tox-gh-actions
- name: Test with tox
run: tox -r
92 changes: 48 additions & 44 deletions tests/writers/test_to_clapeyron.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
from ugropy.writers.clapeyron_writers import write_molar_mass


here = Path(__file__).parent.resolve()
path_db = f"{here}/test_expected_result"
here = Path(__file__).parent
path_db = here / "test_expected_result"


def test_to_clapeyron():
with open(f"{path_db}/molarmass.csv", mode="r") as f:
with open(path_db / "molarmass.csv", mode="r") as f:
df_molarmass = pd.read_csv(f, sep="|", index_col=None)

with open(f"{path_db}/ogUNIFAC_groups.csv", mode="r") as f:
with open(path_db / "ogUNIFAC_groups.csv", mode="r") as f:
df_unifac = pd.read_csv(f, sep="|", index_col=None)

with open(f"{path_db}/PSRK_groups.csv", mode="r") as f:
with open(path_db / "PSRK_groups.csv", mode="r") as f:
df_psrk = pd.read_csv(f, sep="|", index_col=None)

limonene = Groups("CC1=CCC(CC1)C(=C)C", "smiles")
Expand All @@ -32,19 +32,21 @@ def test_to_clapeyron():
[limonene.unifac.subgroups, ethanol.unifac.subgroups],
[limonene.psrk.subgroups, ethanol.psrk.subgroups],
[limonene.joback, ethanol.joback],
f"{here}/database",
(here / "database").resolve(),
)

with open(f"{here}/database/critical.csv", mode="r") as f:
with open(here / "database" / "critical.csv", mode="r") as f:
df_critical_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(f"{here}/database/molarmass.csv", mode="r") as f:
with open(here / "database" / "molarmass.csv", mode="r") as f:
df_molarmass_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(f"{here}/database/ogUNIFAC/ogUNIFAC_groups.csv", mode="r") as f:
with open(
here / "database" / "ogUNIFAC" / "ogUNIFAC_groups.csv", mode="r"
) as f:
df_unifac_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(f"{here}/database/PSRK/PSRK_groups.csv", mode="r") as f:
with open(here / "database" / "PSRK/PSRK_groups.csv", mode="r") as f:
df_psrk_ugropy = pd.read_csv(f, sep="|", index_col=None)

assert df_unifac.equals(df_unifac_ugropy)
Expand Down Expand Up @@ -72,23 +74,23 @@ def test_to_clapeyron():
== "ethanol,,519.5440517502864,5756641.437226128,0.00016649999999999998,0.5610678012451401" # noqa
)

os.remove(f"{here}/database/critical.csv")
os.remove(f"{here}/database/molarmass.csv")
os.remove(f"{here}/database/ogUNIFAC/ogUNIFAC_groups.csv")
os.remove(f"{here}/database/PSRK/PSRK_groups.csv")
os.rmdir(f"{here}/database/ogUNIFAC")
os.rmdir(f"{here}/database/PSRK")
os.rmdir(f"{here}/database")
os.remove(here / "database" / "critical.csv")
os.remove(here / "database" / "molarmass.csv")
os.remove(here / "database" / "ogUNIFAC" / "ogUNIFAC_groups.csv")
os.remove(here / "database" / "PSRK" / "PSRK_groups.csv")
os.rmdir(here / "database" / "ogUNIFAC")
os.rmdir(here / "database/PSRK")
os.rmdir(here / "database")


def test_to_clapeyron_batch_name():
with open(f"{path_db}/molarmass.csv", mode="r") as f:
with open(path_db / "molarmass.csv", mode="r") as f:
df_molarmass = pd.read_csv(f, sep="|", index_col=None)

with open(f"{path_db}/ogUNIFAC_groups.csv", mode="r") as f:
with open(path_db / "ogUNIFAC_groups.csv", mode="r") as f:
df_unifac = pd.read_csv(f, sep="|", index_col=None)

with open(f"{path_db}/PSRK_groups.csv", mode="r") as f:
with open(path_db / "PSRK_groups.csv", mode="r") as f:
df_psrk = pd.read_csv(f, sep="|", index_col=None)

limonene = Groups("CC1=CCC(CC1)C(=C)C", "smiles")
Expand All @@ -99,22 +101,24 @@ def test_to_clapeyron_batch_name():
[limonene.unifac.subgroups, ethanol.unifac.subgroups],
[limonene.psrk.subgroups, ethanol.psrk.subgroups],
[limonene.joback, ethanol.joback],
f"{here}/database",
(here / "database").resolve(),
"otacon",
)

with open(f"{here}/database/otacon_critical.csv", mode="r") as f:
with open(here / "database" / "otacon_critical.csv", mode="r") as f:
df_critical_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(f"{here}/database/otacon_molarmass.csv", mode="r") as f:
with open(here / "database" / "otacon_molarmass.csv", mode="r") as f:
df_molarmass_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(
f"{here}/database/ogUNIFAC/otacon_ogUNIFAC_groups.csv", mode="r"
here / "database" / "ogUNIFAC" / "otacon_ogUNIFAC_groups.csv", mode="r"
) as f:
df_unifac_ugropy = pd.read_csv(f, sep="|", index_col=None)

with open(f"{here}/database/PSRK/otacon_PSRK_groups.csv", mode="r") as f:
with open(
here / "database" / "PSRK" / "otacon_PSRK_groups.csv", mode="r"
) as f:
df_psrk_ugropy = pd.read_csv(f, sep="|", index_col=None)

assert df_unifac.equals(df_unifac_ugropy)
Expand Down Expand Up @@ -142,13 +146,13 @@ def test_to_clapeyron_batch_name():
== "ethanol,,519.5440517502864,5756641.437226128,0.00016649999999999998,0.5610678012451401" # noqa
)

os.remove(f"{here}/database/otacon_critical.csv")
os.remove(f"{here}/database/otacon_molarmass.csv")
os.remove(f"{here}/database/ogUNIFAC/otacon_ogUNIFAC_groups.csv")
os.remove(f"{here}/database/PSRK/otacon_PSRK_groups.csv")
os.rmdir(f"{here}/database/ogUNIFAC")
os.rmdir(f"{here}/database/PSRK")
os.rmdir(f"{here}/database")
os.remove(here / "database" / "otacon_critical.csv")
os.remove(here / "database" / "otacon_molarmass.csv")
os.remove(here / "database" / "ogUNIFAC" / "otacon_ogUNIFAC_groups.csv")
os.remove(here / "database" / "PSRK" / "otacon_PSRK_groups.csv")
os.rmdir(here / "database" / "ogUNIFAC")
os.rmdir(here / "database" / "PSRK")
os.rmdir(here / "database")


def test_molar_mass_csv():
Expand All @@ -161,22 +165,22 @@ def test_molar_mass_csv():
["limonene", "ethanol"],
unifac_groups=[limonene.unifac.subgroups, ethanol.unifac.subgroups],
psrk_groups=[limonene.psrk.subgroups, ethanol.psrk.subgroups],
path=f"{here}/database",
path=(here / "database").resolve(),
)

with open(f"{here}/database/molarmass.csv", mode="r") as f:
with open(here / "database" / "molarmass.csv", mode="r") as f:
df_molarmass_unifac = pd.read_csv(f, sep="|", index_col=None)

os.remove(f"{here}/database/molarmass.csv")
os.remove(here / "database" / "molarmass.csv")

# PSRK molar mass
to_clapeyron(
["limonene", "ethanol"],
psrk_groups=[limonene.psrk.subgroups, ethanol.psrk.subgroups],
path=f"{here}/database",
path=(here / "database").resolve(),
)

with open(f"{here}/database/molarmass.csv", mode="r") as f:
with open(here / "database" / "molarmass.csv", mode="r") as f:
df_molarmass_psrk = pd.read_csv(f, sep="|", index_col=None)

assert (
Expand All @@ -192,17 +196,17 @@ def test_molar_mass_csv():
assert df_molarmass_unifac.iloc[3].to_numpy()[0] == "ethanol,,46.069"
assert df_molarmass_unifac.equals(df_molarmass_psrk)

os.remove(f"{here}/database/molarmass.csv")
os.remove(f"{here}/database/ogUNIFAC/ogUNIFAC_groups.csv")
os.remove(f"{here}/database/PSRK/PSRK_groups.csv")
os.rmdir(f"{here}/database/ogUNIFAC")
os.rmdir(f"{here}/database/PSRK")
os.rmdir(f"{here}/database")
os.remove(here / "database" / "molarmass.csv")
os.remove(here / "database" / "ogUNIFAC" / "ogUNIFAC_groups.csv")
os.remove(here / "database" / "PSRK" / "PSRK_groups.csv")
os.rmdir(here / "database" / "ogUNIFAC")
os.rmdir(here / "database" / "PSRK")
os.rmdir(here / "database")

# Making it fail
with pytest.raises(ValueError):
write_molar_mass(
path=f"{here}/database",
path=(here / "database").resolve(),
batch_name=None,
molecules_names=["limonene", "ethanol"],
)
Expand Down
4 changes: 2 additions & 2 deletions ugropy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@


# constants.py path
_here = Path(__file__).parent.resolve()
_here = Path(__file__).parent

# CSVs path
_csvs = f"{_here}/groupscsv"
_csvs = _here / "groupscsv"

# Gas constant [J/mol/K]
R = 8.31446261815324
44 changes: 22 additions & 22 deletions ugropy/fragmentation_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _rd(file_path: str, index_col: str = None) -> pd.DataFrame:

Parameters
----------
file_path : str
file_path : str or pathlib
Path to csv file.
index_col : str, optional
Name of the index column, by default None.
Expand All @@ -39,13 +39,13 @@ def _rd(file_path: str, index_col: str = None) -> pd.DataFrame:
# =============================================================================
# LV-UNIFAC
# =============================================================================
_uni = f"{_csvs}/unifac"
_uni = _csvs / "unifac"

_uni_sg = _rd(f"{_uni}/unifac_subgroups.csv", "group")
_uni_mg = _rd(f"{_uni}/unifac_maingroups.csv", "no.")
_uni_info = _rd(f"{_uni}/unifac_info.csv", "group")
_uni_problems = _rd(f"{_csvs}/problematic_structures.csv", "smarts")
_uni_hide = _rd(f"{_uni}/hideouts.csv", "group")
_uni_sg = _rd(_uni / "unifac_subgroups.csv", "group")
_uni_mg = _rd(_uni / "unifac_maingroups.csv", "no.")
_uni_info = _rd(_uni / "unifac_info.csv", "group")
_uni_problems = _rd(_csvs / "problematic_structures.csv", "smarts")
_uni_hide = _rd(_uni / "hideouts.csv", "group")

unifac = GibbsModel(
subgroups=_uni_sg,
Expand All @@ -60,13 +60,13 @@ def _rd(file_path: str, index_col: str = None) -> pd.DataFrame:
# =============================================================================
# PSRK
# =============================================================================
_psrk = f"{_csvs}/psrk"
_psrk = _csvs / "psrk"

_psrk_sg = _rd(f"{_psrk}/psrk_subgroups.csv", "group")
_psrk_mg = _rd(f"{_psrk}/psrk_maingroups.csv", "no.")
_psrk_info = _rd(f"{_psrk}/psrk_info.csv", "group")
_psrk_problems = _rd(f"{_csvs}/problematic_structures.csv", "smarts")
_psrk_hide = _rd(f"{_psrk}/hideouts.csv", "group")
_psrk_sg = _rd(_psrk / "psrk_subgroups.csv", "group")
_psrk_mg = _rd(_psrk / "psrk_maingroups.csv", "no.")
_psrk_info = _rd(_psrk / "psrk_info.csv", "group")
_psrk_problems = _rd(_csvs / "problematic_structures.csv", "smarts")
_psrk_hide = _rd(_psrk / "hideouts.csv", "group")

psrk = GibbsModel(
subgroups=_psrk_sg,
Expand Down Expand Up @@ -97,11 +97,11 @@ def _rd(file_path: str, index_col: str = None) -> pd.DataFrame:
# =============================================================================
# Joback
# =============================================================================
_jo = f"{_csvs}/joback"
_jo = _csvs / "joback"

_jo_sg = _rd(f"{_jo}/joback_subgroups.csv", "group")
_jo_problems = _rd(f"{_jo}/joback_problematics.csv", "smarts")
_jo_props = _rd(f"{_jo}/properties_contrib.csv", "group")
_jo_sg = _rd(_jo / "joback_subgroups.csv", "group")
_jo_problems = _rd(_jo / "joback_problematics.csv", "smarts")
_jo_props = _rd(_jo / "properties_contrib.csv", "group")

joback = PropertiesEstimator(
subgroups=_jo_sg,
Expand All @@ -114,12 +114,12 @@ def _rd(file_path: str, index_col: str = None) -> pd.DataFrame:
# Constantinou and Gani
# =============================================================================
# Primary structures
_cg = f"{_csvs}/constantinou_gani"
_cg_p = _csvs / "constantinou_gani" / "primary"

_cg_sg = _rd(f"{_cg}/primary/c_g_prymary_subgroups.csv", "group")
_cg_problems = _rd(f"{_cg}/primary/cg_problematics.csv", "smarts")
_cg_hide = _rd(f"{_cg}/primary/hideouts.csv", "group")
_cg_props = _rd(f"{_cg}/primary/properties_prymary_contrib.csv", "group")
_cg_sg = _rd(_cg_p / "c_g_prymary_subgroups.csv", "group")
_cg_problems = _rd(_cg_p / "cg_problematics.csv", "smarts")
_cg_hide = _rd(_cg_p / "hideouts.csv", "group")
_cg_props = _rd(_cg_p / "properties_prymary_contrib.csv", "group")

constantinou_gani_primary = PropertiesEstimator(
subgroups=_cg_sg,
Expand Down
9 changes: 5 additions & 4 deletions ugropy/writers/clapeyron.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def to_clapeyron(
unifac_groups: List[dict] = [],
psrk_groups: List[dict] = [],
joback_objects: List[JobackProperties] = [],
path: str = "./database",
path: str = "database",
batch_name: str = "",
) -> None:
"""Write the .csv input files for Clapeyron.jl.
Expand Down Expand Up @@ -51,9 +51,6 @@ def to_clapeyron(
if len(molecules_names) == 0:
raise ValueError("No names provided for the molecules.")

if not path_pathlib.is_dir():
path_pathlib.mkdir(parents=True)

if unifac_groups and len(unifac_groups) != len(molecules_names):
raise ValueError(
"UNIFAC groups list must have the same amount of elements than"
Expand All @@ -72,6 +69,10 @@ def to_clapeyron(
"the molecules name list."
)

# Create dir if not created
if not path_pathlib.is_dir():
path_pathlib.mkdir(parents=True)

# Molar mass
write_molar_mass(
path_pathlib,
Expand Down
Loading