forked from PEtab-dev/libpetab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping.py
More file actions
119 lines (91 loc) · 3.07 KB
/
mapping.py
File metadata and controls
119 lines (91 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Functionality related to the PEtab entity mapping table"""
# TODO: Move to petab.v2.mapping
from pathlib import Path
import pandas as pd
from . import lint
from .C import * # noqa: F403
from .models import Model
__all__ = [
"get_mapping_df",
"write_mapping_df",
"check_mapping_df",
"resolve_mapping",
]
def get_mapping_df(
mapping_file: None | str | Path | pd.DataFrame,
) -> pd.DataFrame:
"""
Read the provided mapping file into a ``pandas.Dataframe``.
Arguments:
mapping_file: Name of file to read from or pandas.Dataframe
Returns:
Mapping DataFrame
"""
if mapping_file is None:
return mapping_file
if isinstance(mapping_file, str | Path):
mapping_file = pd.read_csv(
mapping_file, sep="\t", float_precision="round_trip"
)
if not isinstance(mapping_file.index, pd.RangeIndex):
mapping_file.reset_index(
drop=mapping_file.index.name != PETAB_ENTITY_ID,
inplace=True,
)
for col in MAPPING_DF_REQUIRED_COLS:
if col not in mapping_file.columns:
raise KeyError(f"Mapping table missing mandatory field {col}.")
lint.assert_no_leading_trailing_whitespace(
mapping_file.reset_index()[col].values, col
)
mapping_file.set_index([PETAB_ENTITY_ID], inplace=True)
return mapping_file
def write_mapping_df(df: pd.DataFrame, filename: str | Path) -> None:
"""Write PEtab mapping table
Arguments:
df: PEtab mapping table
filename: Destination file name. The parent directory will be created
if necessary.
"""
df = get_mapping_df(df)
Path(filename).parent.mkdir(parents=True, exist_ok=True)
df.to_csv(filename, sep="\t", index=True)
def check_mapping_df(
df: pd.DataFrame,
model: Model | None = None,
) -> None:
"""Run sanity checks on PEtab mapping table
Arguments:
df: PEtab mapping DataFrame
model: Model for additional checking of parameter IDs
Raises:
AssertionError: in case of problems
"""
lint._check_df(df, MAPPING_DF_REQUIRED_COLS[1:], "mapping")
if df.index.name != PETAB_ENTITY_ID:
raise AssertionError(
f"Mapping table has wrong index {df.index.name}. "
f"Expected {PETAB_ENTITY_ID}."
)
lint.check_ids(df.index.values, kind=PETAB_ENTITY_ID)
if model:
for model_entity_id in df[MODEL_ENTITY_ID]:
if not model.has_entity_with_id(model_entity_id):
raise AssertionError(
"Mapping table maps to unknown "
f"model entity ID {model_entity_id}."
)
def resolve_mapping(mapping_df: pd.DataFrame | None, element: str) -> str:
"""Resolve mapping for a given element.
:param element:
Element to resolve.
:param mapping_df:
Mapping table.
:return:
Resolved element.
"""
if mapping_df is None:
return element
if element in mapping_df.index:
return mapping_df.loc[element, MODEL_ENTITY_ID]
return element