From e2f7af15f8a74ed4018b9261a59d3b565fc2439f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Wed, 17 Jun 2020 13:04:14 -0400 Subject: [PATCH 01/10] adding test for row appending --- phys2bids/tests/test_utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/phys2bids/tests/test_utils.py b/phys2bids/tests/test_utils.py index 0163009bf..06be4939e 100644 --- a/phys2bids/tests/test_utils.py +++ b/phys2bids/tests/test_utils.py @@ -4,7 +4,7 @@ import json import os - +from csv import reader from phys2bids import utils @@ -95,3 +95,14 @@ def test_load_heuristics(): test_heuristic = 'heur_test_acq' heuristic_output_filename = utils.load_heuristic(test_heuristic).filename assert test_heuristic in heuristic_output_filename + + +# Test writing rows util +def append_list_as_row(): + file_name = 'test_row.tsv' + list_of_elem = ["01", 32, 'some_info', 182.98, 'M'] + utils.append_list_as_row(file_name, list_of_elem) + with open(file_name, mode='r') as tsv: + tsv_read = reader(tsv, delimiter="\t") + for row in tsv_read: + assert len(row) == len(list_of_elem) From 2583ad818ce02eb657a28daaf44ddf1abf9c8152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Thu, 18 Jun 2020 10:28:01 -0400 Subject: [PATCH 02/10] tests --- phys2bids/tests/test_bids.py | 4 ++++ phys2bids/tests/test_utils.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 8ef8f3a17..d9741bc35 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -58,3 +58,7 @@ 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)) + + +def test_participants_file(): + test_tsv = 'participants.tsv' diff --git a/phys2bids/tests/test_utils.py b/phys2bids/tests/test_utils.py index 06be4939e..d2936e596 100644 --- a/phys2bids/tests/test_utils.py +++ b/phys2bids/tests/test_utils.py @@ -98,7 +98,7 @@ def test_load_heuristics(): # Test writing rows util -def append_list_as_row(): +def test_append_list_as_row(): file_name = 'test_row.tsv' list_of_elem = ["01", 32, 'some_info', 182.98, 'M'] utils.append_list_as_row(file_name, list_of_elem) From 7d4733bb4fac800ea8e1f2070ee57213bb5563f7 Mon Sep 17 00:00:00 2001 From: Eneko Date: Thu, 18 Jun 2020 17:51:18 +0200 Subject: [PATCH 03/10] Adds first participants_file test --- phys2bids/tests/test_bids.py | 47 +++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 7756546e2..10cc1cabb 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -3,9 +3,12 @@ from pkg_resources import resource_filename import pytest +import yaml +from csv import reader -from phys2bids.bids import bidsify_units, use_heuristic, readme_file, dataset_description_file +from phys2bids import bids from phys2bids.bids import UNIT_ALIASES +from phys2bids.utils import append_list_as_row def test_bidsify_units(): @@ -20,10 +23,10 @@ def test_bidsify_units(): } # Actually test for unit_key in unit_tests.keys(): - assert bidsify_units(unit_key) == unit_tests[unit_key] + assert bids.bidsify_units(unit_key) == unit_tests[unit_key] # test there is not problem with every unit in the dict for unit_key in UNIT_ALIASES.keys(): - assert bidsify_units(unit_key) == UNIT_ALIASES[unit_key] + assert bids.bidsify_units(unit_key) == bids.UNIT_ALIASES[unit_key] @pytest.mark.parametrize('test_sub', ['SBJ01', 'sub-006', '006']) @@ -38,7 +41,7 @@ def test_use_heuristic(tmpdir, test_sub, test_ses): test_outdir = tmpdir test_record_label = 'test' - heur_path = use_heuristic(test_full_heur_path, test_sub, test_ses, + heur_path = bids.use_heuristic(test_full_heur_path, test_sub, test_ses, test_full_input_path, test_outdir, test_record_label) if test_sub[:4] == 'sub-': @@ -62,13 +65,45 @@ def test_use_heuristic(tmpdir, test_sub, test_ses): @pytest.mark.parametrize('outdir', '.') def test_README_file(outdir): - readme_file(outdir) + bids.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) + bids.dataset_description_file(outdir) assert os.path.join(outdir, "dataset_description.json") os.remove(os.path.join(outdir, "dataset_description.json")) + + +@pytest.mark.parametrize('outdir', '.') +def test_participants_file(outdir): + test_sub = '001' + test_yaml_path = os.path.join(outdir, 'test.yml') + + # Populate yaml file + data = dict( + participant = dict( + participant_id = f'sub-{test_sub}', + age = '25', + sex = 'm', + handedness = 'r', + ) + ) + + test_header = ['participant_id', 'age', 'sex', 'handedness'] + test_data = [f'sub-{test_sub}', '25', 'm', 'r'] + test_list = [test_header, test_data] + + with open(test_yaml_path, 'w') as outfile: + yaml.dump(data, outfile, default_flow_style=False) + + bids.participants_file(outdir, test_yaml_path, test_sub) + + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_list[counter] + counter += 1 From 0134277dc8b2e0f43d8b20bc7d3615c65348bbf3 Mon Sep 17 00:00:00 2001 From: Eneko Date: Thu, 18 Jun 2020 17:52:59 +0200 Subject: [PATCH 04/10] Add comments to test_participants_file --- phys2bids/tests/test_bids.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 10cc1cabb..1b43e9992 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -79,6 +79,8 @@ def test_dataset_description_file(outdir): @pytest.mark.parametrize('outdir', '.') def test_participants_file(outdir): + + # Checks first condition in line 198 test_sub = '001' test_yaml_path = os.path.join(outdir, 'test.yml') @@ -107,3 +109,5 @@ def test_participants_file(outdir): for line in tsvreader: assert line == test_list[counter] counter += 1 + + # Checks second condition in line 206 \ No newline at end of file From 144be8e8645089009d0a3f07479f431b415ae91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Thu, 18 Jun 2020 14:03:33 -0400 Subject: [PATCH 05/10] adding tests for no yml --- phys2bids/tests/test_bids.py | 38 ++++++++++++++++++++++++++--------- phys2bids/tests/test_utils.py | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 1b43e9992..8d9f88203 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -42,7 +42,7 @@ def test_use_heuristic(tmpdir, test_sub, test_ses): test_record_label = 'test' heur_path = bids.use_heuristic(test_full_heur_path, test_sub, test_ses, - test_full_input_path, test_outdir, test_record_label) + test_full_input_path, test_outdir, test_record_label) if test_sub[:4] == 'sub-': test_sub = test_sub[4:] @@ -82,22 +82,25 @@ def test_participants_file(outdir): # Checks first condition in line 198 test_sub = '001' + test_sub_no_yml = '002' + test_missing_sub = '003' test_yaml_path = os.path.join(outdir, 'test.yml') # Populate yaml file - data = dict( - participant = dict( - participant_id = f'sub-{test_sub}', - age = '25', - sex = 'm', - handedness = 'r', - ) - ) + data = dict(participant=dict( + participant_id=f'sub-{test_sub}', + age='25', + sex='m', + handedness='r')) test_header = ['participant_id', 'age', 'sex', 'handedness'] test_data = [f'sub-{test_sub}', '25', 'm', 'r'] + test_na = [f'sub-{test_sub_no_yml}', 'n/a', 'n/a', 'n/a'] + test_missing_list = [f'sub-{test_missing_sub}', 'n/a', 'n/a', 'n/a'] test_list = [test_header, test_data] + test_no_yml = [test_header, test_data, test_na] + # Checks validity of tsv lines when yml file is given with open(test_yaml_path, 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False) @@ -110,4 +113,19 @@ def test_participants_file(outdir): assert line == test_list[counter] counter += 1 - # Checks second condition in line 206 \ No newline at end of file + # Checks when no yml file is given + bids.participants_file(outdir, yml=None, sub=test_sub_no_yml) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_no_yml[counter] + counter += 1 + + # Checks validity of tsv lines when file exists but no line for Subject + bids.participants_file(outdir, test_missing_sub) + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_missing_list[counter] + counter += 1 + breakpoint() diff --git a/phys2bids/tests/test_utils.py b/phys2bids/tests/test_utils.py index d2936e596..77456fa8a 100644 --- a/phys2bids/tests/test_utils.py +++ b/phys2bids/tests/test_utils.py @@ -105,4 +105,4 @@ def test_append_list_as_row(): with open(file_name, mode='r') as tsv: tsv_read = reader(tsv, delimiter="\t") for row in tsv_read: - assert len(row) == len(list_of_elem) + assert row == list_of_elem From 2118686977cee70ba3aa2a678d9ddd85e10be806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Thu, 18 Jun 2020 15:07:56 -0400 Subject: [PATCH 06/10] tests for missing sub --- phys2bids/tests/test_bids.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 8d9f88203..1dd6a0b23 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -96,10 +96,10 @@ def test_participants_file(outdir): test_header = ['participant_id', 'age', 'sex', 'handedness'] test_data = [f'sub-{test_sub}', '25', 'm', 'r'] test_na = [f'sub-{test_sub_no_yml}', 'n/a', 'n/a', 'n/a'] - test_missing_list = [f'sub-{test_missing_sub}', 'n/a', 'n/a', 'n/a'] + test_missing = [f'sub-{test_missing_sub}', 'n/a', 'n/a', 'n/a'] test_list = [test_header, test_data] test_no_yml = [test_header, test_data, test_na] - + test_missing_list = [test_header, test_data, test_na, test_missing] # Checks validity of tsv lines when yml file is given with open(test_yaml_path, 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False) @@ -114,7 +114,7 @@ def test_participants_file(outdir): counter += 1 # Checks when no yml file is given - bids.participants_file(outdir, yml=None, sub=test_sub_no_yml) + bids.participants_file(outdir=outdir, yml='', sub=test_sub_no_yml) counter = 0 with open(os.path.join(outdir, 'participants.tsv')) as pf: tsvreader = reader(pf, delimiter="\t") @@ -123,9 +123,12 @@ def test_participants_file(outdir): counter += 1 # Checks validity of tsv lines when file exists but no line for Subject - bids.participants_file(outdir, test_missing_sub) - tsvreader = reader(pf, delimiter="\t") - for line in tsvreader: - assert line == test_missing_list[counter] - counter += 1 - breakpoint() + bids.participants_file(outdir=outdir, yml='', sub=test_missing_sub) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_missing_list[counter] + counter += 1 + os.remove(os.path.join(outdir, "participants.tsv")) + os.remove(os.path.join(outdir, "test.yml")) From 48e9aa86e99aee64b9c92627f94d0e14aee57080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Thu, 18 Jun 2020 15:22:03 -0400 Subject: [PATCH 07/10] fixing test in utils --- phys2bids/tests/test_bids.py | 2 +- phys2bids/tests/test_utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 1dd6a0b23..895ab31d3 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -130,5 +130,5 @@ def test_participants_file(outdir): for line in tsvreader: assert line == test_missing_list[counter] counter += 1 - os.remove(os.path.join(outdir, "participants.tsv")) + #os.remove(os.path.join(outdir, "participants.tsv")) os.remove(os.path.join(outdir, "test.yml")) diff --git a/phys2bids/tests/test_utils.py b/phys2bids/tests/test_utils.py index 77456fa8a..f5caa3486 100644 --- a/phys2bids/tests/test_utils.py +++ b/phys2bids/tests/test_utils.py @@ -100,7 +100,7 @@ def test_load_heuristics(): # Test writing rows util def test_append_list_as_row(): file_name = 'test_row.tsv' - list_of_elem = ["01", 32, 'some_info', 182.98, 'M'] + list_of_elem = ["01", "32", 'some_info', "132.98", 'M'] utils.append_list_as_row(file_name, list_of_elem) with open(file_name, mode='r') as tsv: tsv_read = reader(tsv, delimiter="\t") From 3f2639674d4796bd0f8122dd2fd404a1ac2be837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Lespinasse?= Date: Thu, 18 Jun 2020 15:22:35 -0400 Subject: [PATCH 08/10] fixing test in bids --- phys2bids/tests/test_bids.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 895ab31d3..1dd6a0b23 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -130,5 +130,5 @@ def test_participants_file(outdir): for line in tsvreader: assert line == test_missing_list[counter] counter += 1 - #os.remove(os.path.join(outdir, "participants.tsv")) + os.remove(os.path.join(outdir, "participants.tsv")) os.remove(os.path.join(outdir, "test.yml")) From 4c06398dde594799122d66789bdd1f97d26e9bfb Mon Sep 17 00:00:00 2001 From: Eneko Date: Fri, 19 Jun 2020 09:10:23 +0200 Subject: [PATCH 09/10] Add missing test --- phys2bids/tests/test_bids.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/phys2bids/tests/test_bids.py b/phys2bids/tests/test_bids.py index 1dd6a0b23..8bb6598fe 100644 --- a/phys2bids/tests/test_bids.py +++ b/phys2bids/tests/test_bids.py @@ -100,6 +100,7 @@ def test_participants_file(outdir): test_list = [test_header, test_data] test_no_yml = [test_header, test_data, test_na] test_missing_list = [test_header, test_data, test_na, test_missing] + # Checks validity of tsv lines when yml file is given with open(test_yaml_path, 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False) @@ -130,5 +131,15 @@ def test_participants_file(outdir): for line in tsvreader: assert line == test_missing_list[counter] counter += 1 + + # Test that subject from previous check is already there + bids.participants_file(outdir=outdir, yml='', sub=test_missing_sub) + counter = 0 + with open(os.path.join(outdir, 'participants.tsv')) as pf: + tsvreader = reader(pf, delimiter="\t") + for line in tsvreader: + assert line == test_missing_list[counter] + counter += 1 + os.remove(os.path.join(outdir, "participants.tsv")) os.remove(os.path.join(outdir, "test.yml")) From fc745ca2f2ae5f6231f448fdf495a71f702826ff Mon Sep 17 00:00:00 2001 From: Eneko Date: Fri, 19 Jun 2020 09:21:44 +0200 Subject: [PATCH 10/10] Run tests again