Skip to content

Commit

Permalink
Added functions to ensure minimum/maximum columns exist
Browse files Browse the repository at this point in the history
Functions added it utils/io.py and applied in csv.py. Function safe_parameters_fillna for loading and safe_parameters_replace for saving parameters. Now it will be checked if columns exist before replacing inf/-inf and empty strings.
  • Loading branch information
patrickhaetti authored and s-weigand committed Dec 1, 2021
1 parent a0ed949 commit a44cb82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
10 changes: 6 additions & 4 deletions glotaran/builtin/io/pandas/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from glotaran.io import ProjectIoInterface
from glotaran.io import register_project_io
from glotaran.parameter import ParameterGroup
from glotaran.utils.io import safe_parameters_fillna
from glotaran.utils.io import safe_parameters_replace


@register_project_io(["csv"])
Expand All @@ -27,8 +29,8 @@ def load_parameters(self, file_name: str) -> ParameterGroup:
:class:`ParameterGroup
"""
df = pd.read_csv(file_name, skipinitialspace=True, na_values=["None", "none"])
df["minimum"].fillna(-np.inf, inplace=True)
df["maximum"].fillna(np.inf, inplace=True)
safe_parameters_fillna(df, "minimum", -np.inf)
safe_parameters_fillna(df, "maximum", np.inf)
return ParameterGroup.from_dataframe(df, source=file_name)

def save_parameters(self, parameters: ParameterGroup, file_name: str, *, sep: str = ","):
Expand All @@ -44,6 +46,6 @@ def save_parameters(self, parameters: ParameterGroup, file_name: str, *, sep: st
Other separators can be used optionally.
"""
df = parameters.to_dataframe()
df["minimum"].replace([-np.inf], "", inplace=True)
df["maximum"].replace([np.inf], "", inplace=True)
safe_parameters_replace(df, "minimum", -np.inf, "")
safe_parameters_replace(df, "maximum", np.inf, "")
df.to_csv(file_name, na_rep="None", index=False, sep=sep)
38 changes: 38 additions & 0 deletions glotaran/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,41 @@ def relative_posix_path(source_path: StrOrPath, base_path: StrOrPath | None = No
if base_path is not None and os.path.isabs(source_path):
source_path = os.path.relpath(source_path, Path(base_path).as_posix())
return Path(source_path).as_posix()


def safe_parameters_fillna(df, column_name, fill_value):
"""Ensure that columns exist in order to replace empty strings with +/-np.inf values.
Parameters
----------
df : pd.DataFrame
DataFrame from which specific column values will be replaced
column_name : str
Name of column from DataFrame
fill_value : str
Values to be replaced in column
"""
if column_name in df.columns:
df[column_name].fillna(fill_value, inplace=True)


def safe_parameters_replace(df, column_name, to_be_replaced_values, replace_value):
"""Ensure that columns exist in order to replace +/-np.inf values with empty strings.
If value is not list or tuple format, convert into list with same value as element.
Parameters
----------
df : pd.DataFrame
DataFrame from which specific column values will be replaced
column_name : str
Name of column from DataFrame
to_be_replaced_values : float
Values to be replaced
replace_value : str
Replace values
"""
if not isinstance(to_be_replaced_values, (list, tuple)):
to_be_replaced_values = [to_be_replaced_values]
if column_name in df.columns:
df[column_name].replace(to_be_replaced_values, replace_value, inplace=True)

0 comments on commit a44cb82

Please sign in to comment.