diff --git a/wonkyconn/tests/test_workflow.py b/wonkyconn/tests/test_workflow.py new file mode 100644 index 00000000..06a646bf --- /dev/null +++ b/wonkyconn/tests/test_workflow.py @@ -0,0 +1,35 @@ +from argparse import Namespace +from pathlib import Path + +import pandas as pd +import pytest + +from wonkyconn.workflow import load_data_frame + + +def test_load_data_frame(tmp_path: Path) -> None: + # Create a sample phenotypes file + phenotypes_path = tmp_path / "phenotypes.tsv" + phenotypes_path.write_text("participant_id\tage\tgender\nsub-01\t25\tM\nsub-02\t30\tF\nsub-03\t35\tM\n") + + # Load the data frame + data_frame = load_data_frame(Namespace(phenotypes=str(phenotypes_path))) + + # Check that the data frame has the expected shape and columns + row_count, _ = data_frame.shape + assert row_count == 3 + assert list(data_frame.reset_index().columns) == ["participant_id", "age", "gender"] + + data_frame = data_frame.reset_index() + + # Check that we throw an error for missing columns + for missing_column in data_frame.columns: + data_frame.drop(columns=missing_column).to_csv(phenotypes_path, sep="\t", index=False) + with pytest.raises(ValueError, match=missing_column): + load_data_frame(Namespace(phenotypes=str(phenotypes_path))) + + # Check that we throw an error for duplicate participant_id entries + data_frame = pd.concat([data_frame, data_frame.iloc[0:1]]) # Add a duplicate row + data_frame.to_csv(phenotypes_path, sep="\t", index=False) + with pytest.raises(ValueError, match="duplicate"): + load_data_frame(Namespace(phenotypes=str(phenotypes_path))) diff --git a/wonkyconn/workflow.py b/wonkyconn/workflow.py index ef556225..f58ddfa1 100644 --- a/wonkyconn/workflow.py +++ b/wonkyconn/workflow.py @@ -239,8 +239,8 @@ def make_record( # age / sex predictability metrics try: - ages = seg_data_frame["age"].to_numpy() - genders = seg_data_frame["gender"].to_numpy() + ages: npt.NDArray[np.float64] = seg_data_frame["age"].to_numpy() + genders: npt.NDArray[np.str_] = seg_data_frame["gender"].to_numpy() scores = age_sex_scores( connectivity_matrices, @@ -292,6 +292,8 @@ def load_data_frame(args: argparse.Namespace) -> pd.DataFrame: index_col="participant_id", dtype={"participant_id": str}, ) + if data_frame.index.has_duplicates: + raise ValueError("Phenotypes file has duplicate participant_id entries") if "gender" not in data_frame.columns: raise ValueError('Phenotypes file is missing the "gender" column') if "age" not in data_frame.columns: