Skip to content
Open
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
35 changes: 35 additions & 0 deletions wonkyconn/tests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -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)))
6 changes: 4 additions & 2 deletions wonkyconn/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down