Skip to content

Commit

Permalink
Error out on single column input
Browse files Browse the repository at this point in the history
  • Loading branch information
surchs committed Sep 26, 2023
1 parent 480f323 commit 480a0a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
32 changes: 15 additions & 17 deletions bagel/pheno_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,21 @@ def load_pheno(input_p: Path) -> pd.DataFrame | None:

if pheno_df.shape[1] > 1:
return pheno_df
elif len(pheno_df.columns[0].split(",")) > 1:
# We only extracted one column, this might mean that we have a sneaky .csv
raise ValueError(
f"Your phenotypic input file {input_p} has only one column "
f"and is therefore not valid as a Neurobagel phenotypic file. "
"Note that your phenotypic input file also looks like a .csv file "
"as it contains several ',' commas. It is possible that "
"you have accidentally renamed a .csv file as a .tsv."
" Please provide a valid .tsv pheno file!"
)
else:
warnings.warn(
f"Your phenotypic input file {input_p} has only one column."
" Such a file is not a valid Neurobagel phenotypic data file."
" Please check your input again!"
)
return pheno_df

# If we have only one column, but splitting by ',' gives us several elements
# then there is a good chance the user accidentally renamed a .csv into .tsv
# and we should give them some extra info with our error message to fix this.
note_misnamed_csv = (
"Note that your phenotypic input file also looks like a .csv file "
"as it contains several ',' commas. It is possible that "
"you have accidentally renamed a .csv file as a .tsv."
)
raise ValueError(
f"Your phenotypic input file {input_p} has only one column "
f"and is therefore not valid as a Neurobagel phenotypic file. "
" Please provide a valid .tsv pheno file!"
f"\n\n{note_misnamed_csv if len(pheno_df.columns[0].split(',')) > 1 else ''}"
)

raise ValueError(
f"Your ({input_p}) is not a .tsv file."
Expand Down
3 changes: 2 additions & 1 deletion bagel/tests/test_cli_pheno.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ def test_providing_csv_file_raises_error(
test_data,
tmp_path,
):
"""Providing a .csv file or a file with .tsv extension but incorrect encoding should be handled with an informative error."""
"""Providing a .csv file or a file with .tsv extension but incorrect encoding should be handled with an
informative error."""
with pytest.raises(ValueError) as e:
runner.invoke(
bagel,
Expand Down

0 comments on commit 480a0a4

Please sign in to comment.