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 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
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')
if not os.path.exists(file_path):
text = 'Empty README, please fill in describing the dataset in more detail.'
LGR.warning('phys2bids could not find README,'
'generating it EMPTY, please fill in the necessary info')
utils.writefile(file_path, '', text)
17 changes: 10 additions & 7 deletions phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@

from numpy import savetxt

from phys2bids import utils, viz, _version
from phys2bids.bids import bidsify_units, use_heuristic, participants_file
from phys2bids import utils, viz, _version, bids
from phys2bids.cli.run import _get_parser
from phys2bids.physio_obj import BlueprintOutput

Expand Down Expand Up @@ -190,7 +189,7 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
LGR.info(f'Reading the file {infile}')
phys_in = populate_phys_input(infile, chtrig)
for index, unit in enumerate(phys_in.units):
phys_in.units[index] = bidsify_units(unit)
phys_in.units[index] = bids.bidsify_units(unit)
LGR.info('Reading infos')
phys_in.print_info(filename)
# #!# Here the function viz.plot_channel should be called
Expand Down Expand Up @@ -268,7 +267,11 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
# Generate participants.tsv file if it doesn't exist already.
# 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)
bids.participants_file(outdir, yml, sub)
# Generate dataset_description.json file if it doesn't exist already.
bids.dataset_description_file(outdir)
# Generate README file if it doesn't exist already.
bids.readme_file(outdir)
elif heur_file and not sub:
LGR.warning('While "-heur" was specified, option "-sub" was not.\n'
'Skipping BIDS formatting.')
Expand All @@ -279,10 +282,10 @@ def phys2bids(filename, info=False, indir='.', outdir='.', heur_file=None,
if heur_file and sub:
if output_amount > 1:
# Add "recording-freq" to filename if more than one freq
outfile = use_heuristic(heur_file, sub, ses, filename,
outdir, uniq_freq)
outfile = bids.use_heuristic(heur_file, sub, ses, filename,
outdir, uniq_freq)
else:
outfile = use_heuristic(heur_file, sub, ses, filename, outdir)
outfile = bids.use_heuristic(heur_file, sub, ses, filename, outdir)

else:
outfile = os.path.join(outdir,
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"))