|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
|
3
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 et: |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
import os.path as op |
|
8
|
|
|
|
|
9
|
|
|
from hansel import mktree |
|
10
|
|
|
from hansel.utils import ParameterGrid |
|
11
|
|
|
|
|
12
|
|
|
from test_crumb import tmp_crumb |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def test_mktree1(tmp_crumb): |
|
16
|
|
|
|
|
17
|
|
|
assert not op.exists(tmp_crumb.split()[0]) |
|
18
|
|
|
|
|
19
|
|
|
nupaths = mktree(tmp_crumb, None) |
|
20
|
|
|
|
|
21
|
|
|
assert all([op.exists(npath) for npath in nupaths]) |
|
22
|
|
|
|
|
23
|
|
|
pytest.raises(TypeError, mktree, tmp_crumb, 'hansel') |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def test_mktree_dicts(tmp_crumb): |
|
27
|
|
|
|
|
28
|
|
|
assert not op.exists(tmp_crumb.split()[0]) |
|
29
|
|
|
|
|
30
|
|
|
values_map = {'session_id': ['session_' + str(i) for i in range(2)], |
|
31
|
|
|
'subject_id': ['subj_' + str(i) for i in range(3)]} |
|
32
|
|
|
|
|
33
|
|
|
nupaths = mktree(tmp_crumb, list(ParameterGrid(values_map))) |
|
34
|
|
|
|
|
35
|
|
|
assert all([op.exists(npath) for npath in nupaths]) |
|
36
|
|
|
|
|
37
|
|
|
values_map['grimm'] = ['Jacob', 'Wilhelm'] |
|
38
|
|
|
pytest.raises(ValueError, mktree, tmp_crumb, list(ParameterGrid(values_map))) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def test_mktree_tuples(tmp_crumb): |
|
42
|
|
|
|
|
43
|
|
|
assert not op.exists(tmp_crumb.split()[0]) |
|
44
|
|
|
|
|
45
|
|
|
session_ids = ['session_' + str(i) for i in range(2)] |
|
46
|
|
|
subj_ids = ['subj_' + str(i) for i in range(3)] |
|
47
|
|
|
values_dict = {'session_id': session_ids, |
|
48
|
|
|
'subject_id': subj_ids} |
|
49
|
|
|
|
|
50
|
|
|
values_map = list(ParameterGrid(values_dict)) |
|
51
|
|
|
values_tups = [tuple(d.items()) for d in values_map] |
|
52
|
|
|
|
|
53
|
|
|
nupaths = mktree(tmp_crumb, values_tups) |
|
54
|
|
|
|
|
55
|
|
|
assert all([op.exists(npath) for npath in nupaths]) |
|
56
|
|
|
|
|
57
|
|
|
ls_session_ids = tmp_crumb.ls('session_id', |
|
58
|
|
|
fullpath = False, |
|
59
|
|
|
make_crumbs = False, |
|
60
|
|
|
duplicates = False, |
|
61
|
|
|
check_exists = False) |
|
62
|
|
|
assert ls_session_ids == session_ids |
|
63
|
|
|
|
|
64
|
|
|
ls_subj_ids = tmp_crumb.ls('subject_id', |
|
65
|
|
|
fullpath = False, |
|
66
|
|
|
make_crumbs = False, |
|
67
|
|
|
duplicates = False, |
|
68
|
|
|
check_exists = False) |
|
69
|
|
|
assert ls_subj_ids == subj_ids |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_mktree_raises(tmp_crumb): |
|
73
|
|
|
assert not op.exists(tmp_crumb.split()[0]) |
|
74
|
|
|
|
|
75
|
|
|
values_dict = {'session_id': ['session_' + str(i) for i in range(2)], |
|
76
|
|
|
'subject_id': ['subj_' + str(i) for i in range(3)], |
|
77
|
|
|
'modality': ['anat', 'rest', 'pet'], |
|
78
|
|
|
'image': ['mprage.nii', 'rest.nii', 'pet.nii'], |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
del values_dict['session_id'] |
|
82
|
|
|
del values_dict['modality'] |
|
83
|
|
|
pytest.raises(KeyError, mktree, tmp_crumb, list(ParameterGrid(values_dict))) |
|
84
|
|
|
|
|
85
|
|
|
|