Skip to content

Commit f7ff91a

Browse files
committed
Move check functions to imas validation module
1 parent c48efb8 commit f7ff91a

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

torax/_src/imas_tools/input/core_profiles.py

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ def plasma_composition_from_IMAS(
169169
main_ions_symbols = constants.HYDROGENIC_IONS
170170
# main_ions_symbols explicitly provided: validate ions presence in IDS.
171171
else:
172-
_validate_main_ions_presence(parsed_ions, main_ions_symbols)
173-
_validate_ids_ions(parsed_ions)
172+
validation.validate_main_ions_presence(parsed_ions, main_ions_symbols)
173+
validation.validate_core_profiles_ions(parsed_ions)
174174

175175
Z_eff = (
176176
time_array,
@@ -220,41 +220,3 @@ def plasma_composition_from_IMAS(
220220
"species": impurity_species,
221221
},
222222
}
223-
224-
225-
def _validate_ids_ions(
226-
parsed_ions: list[str],
227-
) -> None:
228-
"""Checks if all parsed ions are recognized."""
229-
for ion in parsed_ions:
230-
# ion is casted to str to avoid issues with imas string types.
231-
if str(ion) not in constants.ION_PROPERTIES_DICT.keys():
232-
raise (
233-
KeyError(
234-
f"{ion} is present in the IDS but not a valid TORAX ion. Check"
235-
"typing or add the ion to the excluded_impurities."
236-
)
237-
)
238-
239-
240-
def _validate_main_ions_presence(
241-
parsed_ions: list[str],
242-
main_ion_symbols: Collection[str],
243-
) -> None:
244-
"""Checks that items in main_ion_symbols are present in the IDS."""
245-
for ion in main_ion_symbols:
246-
if ion not in constants.ION_PROPERTIES_DICT.keys():
247-
raise (
248-
KeyError(
249-
f"{ion} is not a valid symbol of a TORAX valid ion. Please"
250-
" check typing of main_ion_symbols."
251-
)
252-
)
253-
if ion not in parsed_ions:
254-
raise (
255-
ValueError(
256-
f"The expected main ion {ion} cannot be found in the input"
257-
" IDS or has no valid data. \n Please check that the IDS is"
258-
" properly filled"
259-
)
260-
)

torax/_src/imas_tools/input/validation.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
import functools
1717
import logging
18-
from typing import Any
18+
from typing import Any, Collection
1919

2020
from imas import ids_toplevel
21+
from torax._src import constants
2122

2223

2324
def _get_nested_attr(obj: Any, path: str) -> Any:
@@ -65,3 +66,42 @@ def validate_core_profiles_ids(ids: ids_toplevel.IDSToplevel) -> None:
6566
"global_quantities.ip",
6667
),
6768
)
69+
70+
71+
def validate_core_profiles_ions(
72+
parsed_ions: list[str],
73+
) -> None:
74+
"""Checks if all parsed ions are recognized."""
75+
for ion in parsed_ions:
76+
# ion is casted to str to avoid issues with imas string types.
77+
if str(ion) not in constants.ION_PROPERTIES_DICT.keys():
78+
raise (
79+
KeyError(
80+
f"{ion} is present in the IDS but not a valid TORAX ion. Check"
81+
"typing or add the ion to the excluded_impurities."
82+
)
83+
)
84+
85+
86+
def validate_main_ions_presence(
87+
parsed_ions: list[str],
88+
main_ion_symbols: Collection[str],
89+
) -> None:
90+
"""Checks that items in main_ion_symbols are present in a list of ions parsed
91+
from a given core_profiles IDS."""
92+
for ion in main_ion_symbols:
93+
if ion not in constants.ION_PROPERTIES_DICT.keys():
94+
raise (
95+
KeyError(
96+
f"{ion} is not a valid symbol of a TORAX valid ion. Please"
97+
" check typing of main_ion_symbols."
98+
)
99+
)
100+
if ion not in parsed_ions:
101+
raise (
102+
ValueError(
103+
f"The expected main ion {ion} cannot be found in the input"
104+
" IDS or has no valid data. \n Please check that the IDS is"
105+
" properly filled"
106+
)
107+
)

0 commit comments

Comments
 (0)