Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/submodules/bids-examples-d039879
Browse files Browse the repository at this point in the history
  • Loading branch information
surchs authored Sep 26, 2023
2 parents f6993ec + 818e0aa commit d89e772
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
-
name: Build and push
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ jobs:
coverage run -m pytest
coverage lcov -o ./coverage/lcov.info
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v2.2.1
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
52 changes: 32 additions & 20 deletions bagel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import bagel.bids_utils as butil
import bagel.pheno_utils as putil
from bagel import mappings, models
from bagel.utility import load_json
from bagel.utility import check_overwrite, load_json

bagel = typer.Typer()

Expand All @@ -32,14 +32,6 @@ def pheno(
dir_okay=False,
resolve_path=True,
),
output: Path = typer.Option(
...,
help="The directory where outputs should be created.",
exists=True,
file_okay=False,
dir_okay=True,
resolve_path=True,
),
name: str = typer.Option(
...,
help="A descriptive name for the dataset the input belongs to. "
Expand All @@ -51,6 +43,18 @@ def pheno(
callback=putil.validate_portal_uri,
help="URL (HTTP/HTTPS) to a website or page that describes the dataset and access instructions (if available).",
),
output: Path = typer.Option(
default="pheno.jsonld",
help="The path for the output .jsonld file.",
file_okay=True,
dir_okay=False,
resolve_path=True,
),
overwrite: bool = typer.Option(
False,
"--overwrite",
help="Overwrite output file if it already exists.",
),
):
"""
Process a tabular phenotypic file (.tsv) that has been successfully annotated
Expand All @@ -61,6 +65,9 @@ def pheno(
graph datamodel for the provided phenotypic file in the .jsonld format.
You can upload this .jsonld file to the Neurobagel graph.
"""
# Check if output file already exists
check_overwrite(output, overwrite)

data_dictionary = load_json(dictionary)
pheno_df = pd.read_csv(pheno, sep="\t", keep_default_na=False, dtype=str)
putil.validate_inputs(data_dictionary, pheno_df)
Expand Down Expand Up @@ -140,11 +147,10 @@ def pheno(
# TODO: we should revisit this because there may be reasons to have None be meaningful in the future
context.update(**dataset.dict(exclude_none=True))

output_filename = output / "pheno.jsonld"
with open(output_filename, "w") as f:
with open(output, "w") as f:
f.write(json.dumps(context, indent=2))

print(f"Saved output to: {output_filename}")
print(f"Saved output to: {output}")


@bagel.command()
Expand All @@ -166,13 +172,17 @@ def bids(
resolve_path=True,
),
output: Path = typer.Option(
...,
help="The directory where outputs should be created",
exists=True,
file_okay=False,
dir_okay=True,
help="The path for the output .jsonld file.",
default="pheno_bids.jsonld",
file_okay=True,
dir_okay=False,
resolve_path=True,
),
overwrite: bool = typer.Option(
False,
"--overwrite",
help="Overwrite output file if it already exists.",
),
):
"""
Extract imaging metadata from a valid BIDS dataset and combine them
Expand All @@ -183,6 +193,9 @@ def bids(
graph datamodel for the combined metadata in the .jsonld format.
You can upload this .jsonld file to the Neurobagel graph.
"""
# Check if output file already exists
check_overwrite(output, overwrite)

jsonld = load_json(jsonld_path)
layout = BIDSLayout(bids_dir, validate=True)

Expand Down Expand Up @@ -264,8 +277,7 @@ def bids(

merged_dataset = {**context, **pheno_dataset.dict(exclude_none=True)}

output_filename = output / "pheno_bids.jsonld"
with open(output_filename, "w") as f:
with open(output, "w") as f:
f.write(json.dumps(merged_dataset, indent=2))

print(f"Saved output to: {output_filename}")
print(f"Saved output to: {output}")
29 changes: 20 additions & 9 deletions bagel/tests/test_cli_bids.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from pathlib import Path

import pytest

from bagel.cli import bagel


@pytest.fixture(scope="function")
def default_pheno_bids_output_path(tmp_path):
"Return temporary bids command output filepath that uses the default filename."
return tmp_path / "pheno_bids.jsonld"


def test_bids_valid_inputs_run_successfully(
runner, test_data_upload_path, bids_synthetic, tmp_path
runner,
test_data_upload_path,
bids_synthetic,
default_pheno_bids_output_path,
):
"""Basic smoke test for the "add-bids" subcommand"""
result = runner.invoke(
Expand All @@ -16,20 +27,20 @@ def test_bids_valid_inputs_run_successfully(
"--bids-dir",
bids_synthetic,
"--output",
tmp_path,
default_pheno_bids_output_path,
],
)
assert result.exit_code == 0, f"Errored out. STDOUT: {result.output}"
assert (
tmp_path / "pheno_bids.jsonld"
default_pheno_bids_output_path
).exists(), "The pheno_bids.jsonld output was not created."


def test_bids_sessions_have_correct_labels(
runner,
test_data_upload_path,
bids_synthetic,
tmp_path,
default_pheno_bids_output_path,
load_test_json,
):
"""Check that sessions added to pheno_bids.jsonld have the expected labels."""
Expand All @@ -42,11 +53,11 @@ def test_bids_sessions_have_correct_labels(
"--bids-dir",
bids_synthetic,
"--output",
tmp_path,
default_pheno_bids_output_path,
],
)

pheno_bids = load_test_json(tmp_path / "pheno_bids.jsonld")
pheno_bids = load_test_json(default_pheno_bids_output_path)
for sub in pheno_bids["hasSamples"]:
assert ["ses-01", "ses-02"] == [
ses["hasLabel"] for ses in sub["hasSession"]
Expand All @@ -56,7 +67,7 @@ def test_bids_sessions_have_correct_labels(
def test_bids_data_with_sessions_have_correct_paths(
runner,
test_data_upload_path,
tmp_path,
default_pheno_bids_output_path,
load_test_json,
):
"""
Expand All @@ -72,11 +83,11 @@ def test_bids_data_with_sessions_have_correct_paths(
"--bids-dir",
Path(__file__).parent / "../../bids-examples/synthetic",
"--output",
tmp_path,
default_pheno_bids_output_path,
],
)

pheno_bids = load_test_json(tmp_path / "pheno_bids.jsonld")
pheno_bids = load_test_json(default_pheno_bids_output_path)
for sub in pheno_bids["hasSamples"]:
for ses in sub["hasSession"]:
assert sub["hasLabel"] in ses["hasFilePath"]
Expand Down
Loading

0 comments on commit d89e772

Please sign in to comment.