Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate dataset_description.json and README.md when they do not exist and update the json file when the subject is missing #255

Merged
merged 18 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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
41 changes: 41 additions & 0 deletions phys2bids/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,44 @@ def participants_file(outdir, yml, sub):
participants_data = ['n/a'] * header_length
participants_data[p_id_idx] = f'sub-{sub}'
utils.append_list_as_row(file_path, participants_data)


def dataset_description_file(outdir):
"""
Create dataset_description.json file if it does not exist.
If it exists, do nothing.

Parameters
----------
outdir: path
Full path to the output directory.

"""
# dictionary that will be written for the basic dataset description version
data_dict = {"Name": os.path.splitext(os.path.basename(outdir))[0],
smoia marked this conversation as resolved.
Show resolved Hide resolved
"BIDSVersion": "1.4.0", "DatasetType": "raw"}
file_path = os.path.join(outdir, 'dataset_description.json')
# check if dataset_description.json exists, if it doesn't create it
if not os.path.exists(file_path):
LGR.warning('phys2bids could not find dataset_description.json,'
'generating it with provided info')
utils.writejson(file_path, data_dict)


def readme_file(outdir):
"""
Create README file if it does not exist.
If it exists, do nothing.

Parameters
----------
outdir: path
Full path to the output directory.

"""
file_path = os.path.join(outdir, 'README.md')
text = 'Empty README, please fill in describing the dataset in more detail.'
if not os.path.exists(file_path):
LGR.warning('phys2bids could not find README,'
'generating it EMPTY, please fill in the necessary info')
utils.writefile(file_path, '', text)
5 changes: 5 additions & 0 deletions phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from phys2bids import utils, viz, _version
from phys2bids.bids import bidsify_units, use_heuristic, participants_file
from phys2bids.bids import dataset_description_file, readme_file
smoia marked this conversation as resolved.
Show resolved Hide resolved
from phys2bids.cli.run import _get_parser
from phys2bids.physio_obj import BlueprintOutput

Expand Down Expand Up @@ -269,6 +270,10 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
# Update the file if the subject is not in the file.
# Do not update if the subject is already in the file.
participants_file(outdir, yml, sub)
# Generate dataset_description.json file if it doesn't exist already.
dataset_description_file(outdir)
# Generate README file if it doesn't exist already.
readme_file(outdir)
elif heur_file and not sub:
LGR.warning('While "-heur" was specified, option "-sub" was not.\n'
'Skipping BIDS formatting.')
Expand Down
16 changes: 15 additions & 1 deletion phys2bids/tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from phys2bids.bids import bidsify_units, use_heuristic
from phys2bids.bids import bidsify_units, use_heuristic, readme_file, dataset_description_file
from phys2bids.bids import UNIT_ALIASES


Expand Down Expand Up @@ -58,3 +58,17 @@ def test_use_heuristic(tmpdir, test_sub, test_ses):
f'_task-test_rec-biopac_run-01_recording-test_physio')

assert os.path.normpath(test_result) == os.path.normpath(str(heur_path))


@pytest.mark.parametrize('outdir', '.')
def test_README_file(outdir):
readme_file(outdir)
assert os.path.join(outdir, "README.md")
os.remove(os.path.join(outdir, "README.md"))


@pytest.mark.parametrize('outdir', '.')
def test_dataset_description_file(outdir):
dataset_description_file(outdir)
assert os.path.join(outdir, "dataset_description.json")
os.remove(os.path.join(outdir, "dataset_description.json"))