1
|
|
|
import unittest |
2
|
|
|
import os, shutil |
3
|
|
|
from os.path import abspath |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class ContextApiTests(unittest.TestCase): |
7
|
|
|
|
8
|
|
|
def setUp(self): |
9
|
|
|
self.dbpath = os.path.expanduser(os.path.join('~','provenance_test.json')) |
10
|
|
|
if os.path.exists(self.dbpath): |
11
|
|
|
os.remove(self.dbpath) |
12
|
|
|
os.mkdir('temp') |
13
|
|
|
from niprov import Context |
14
|
|
|
self.provenance = Context() |
15
|
|
|
self.provenance.config.database_type = 'file' |
16
|
|
|
self.provenance.config.database_url = self.dbpath |
17
|
|
|
|
18
|
|
|
def tearDown(self): |
19
|
|
|
shutil.rmtree('temp') |
20
|
|
|
|
21
|
|
|
def touch(self, path): |
22
|
|
|
with open(path,'w') as tempfile: |
23
|
|
|
tempfile.write('0') |
24
|
|
|
|
25
|
|
|
def test_Relative_paths(self): |
26
|
|
|
self.provenance.discover('testdata') |
27
|
|
|
newfile = 'temp/smoothed.test' |
28
|
|
|
self.touch(newfile) |
29
|
|
|
self.provenance.log(newfile, 'test', 'testdata/eeg/stub.cnt') |
30
|
|
|
img = self.provenance.get().byLocation(newfile) |
31
|
|
|
|
32
|
|
View Code Duplication |
def test_Log(self): |
|
|
|
|
33
|
|
|
self.provenance.discover('testdata') |
34
|
|
|
newfile = 'temp/smoothed.test' |
35
|
|
|
self.touch(newfile) |
36
|
|
|
parent = os.path.abspath('testdata/eeg/stub.cnt') |
37
|
|
|
self.provenance.log(newfile, 'test', parent) |
38
|
|
|
testfpath = os.path.abspath(newfile) |
39
|
|
|
img = self.provenance.get().byLocation(testfpath) |
40
|
|
|
self.assertEqual(img.provenance['subject'], 'Jane Doe') |
41
|
|
|
self.assertEqual(img.provenance['size'], os.path.getsize(newfile)) |
42
|
|
|
|
43
|
|
View Code Duplication |
def test_Record_with_user(self): |
|
|
|
|
44
|
|
|
self.provenance.discover('testdata') |
45
|
|
|
newfile = 'temp/recorded.test' |
46
|
|
|
self.touch(newfile) |
47
|
|
|
parent = os.path.abspath('testdata/eeg/stub.cnt') |
48
|
|
|
self.provenance.record('echo hallo', newfile, parent, user='007') |
49
|
|
|
testfpath = os.path.abspath(newfile) |
50
|
|
|
img = self.provenance.get().byLocation(testfpath) |
51
|
|
|
self.assertEqual(img.provenance['user'], '007') |
52
|
|
|
|
53
|
|
|
def test_Export_Import(self): |
54
|
|
|
from niprov.exceptions import UnknownFileError |
55
|
|
|
self.provenance.discover('testdata') |
56
|
|
|
discoveredFile = os.path.abspath('testdata/eeg/stub.cnt') |
57
|
|
|
self.assertIsNotNone(self.provenance.get().byLocation(discoveredFile)) |
58
|
|
|
backupFilepath = self.provenance.backup() |
59
|
|
|
os.remove(self.dbpath) # get rid of existing data. |
60
|
|
|
with self.assertRaises(IndexError): |
61
|
|
|
self.provenance.get().byLocation(discoveredFile) |
62
|
|
|
self.provenance.importp(backupFilepath) |
63
|
|
|
self.assertIsNotNone(self.provenance.get().byLocation(discoveredFile)) |
64
|
|
|
|
65
|
|
|
@unittest.skip("Doesn't work on Travis right now.") |
66
|
|
|
def test_Attach_provenance_string_in_file_based_on_config(self): |
67
|
|
|
import nibabel |
68
|
|
|
self.provenance.config.attach = True |
69
|
|
|
newfile = os.path.abspath('temp/fileX.nii.gz') |
70
|
|
|
shutil.copy('testdata/nifti/fieldmap.nii.gz', newfile) |
71
|
|
|
self.provenance.add(newfile) |
72
|
|
|
img = nibabel.load(newfile) |
73
|
|
|
self.assertEqual(img.get_header().extensions.count('comment'), 1) |
74
|
|
|
self.assertEqual(img.get_header().extensions[0].get_code(), 6) |
75
|
|
|
content = img.get_header().extensions[0].get_content() |
76
|
|
|
self.assertIn('"path": "{0}"'.format(newfile), content) |
77
|
|
|
|
78
|
|
|
def test_Approval(self): |
79
|
|
|
self.provenance.discover('testdata') |
80
|
|
|
self.provenance.markForApproval([os.path.abspath('testdata/dicom/T1.dcm'), |
81
|
|
|
os.path.abspath('testdata/dicom/T2.dcm')]) |
82
|
|
|
imgs = self.provenance.markedForApproval() |
83
|
|
|
self.provenance.approve(os.path.abspath('testdata/dicom/T1.dcm')) |
84
|
|
|
imgs = self.provenance.markedForApproval() |
85
|
|
|
|
86
|
|
|
def test_Comparison(self): |
87
|
|
|
# Given two PARREC images' provenance records |
88
|
|
|
par1 = self.provenance.add(abspath('testdata/parrec/T1.PAR'))[0] |
89
|
|
|
par2 = self.provenance.add(abspath('testdata/parrec/T2.PAR'))[0] |
90
|
|
|
# Comparing them returns a Diff object with methods testing equality |
91
|
|
|
self.assertFalse(self.provenance.compare(par1, par2).areEqual()) |
92
|
|
|
# Compare() can also be called as a method on the objects themselves, |
93
|
|
|
# and the Diff object has assert..() methods that raise AssertionErrors |
94
|
|
|
msgRegExp = ".*echo-time.*2\.08.*80\.0.*" |
95
|
|
|
with self.assertRaisesRegexp(AssertionError, msgRegExp): |
96
|
|
|
par1.compare(par2).assertEqualProtocol() |
97
|
|
|
|
98
|
|
|
def test_Search(self): |
99
|
|
|
x1, s = self.provenance.add('x1', transient=True, |
100
|
|
|
provenance={'transformation':'needle and thread'}) |
101
|
|
|
x2, s = self.provenance.add('x2/needle.y', transient=True, |
102
|
|
|
provenance={'transformation':'needle and thread'}) |
103
|
|
|
x3, s = self.provenance.add('x3', transient=True, |
104
|
|
|
provenance={'transformation':'hammer and tongs'}) |
105
|
|
|
results = self.provenance.search('needle') |
106
|
|
|
self.assertEqual(len(results), 2) |
107
|
|
|
self.assertEqual(x2.provenance['id'], results[0].provenance['id']) |
108
|
|
|
self.assertEqual(x1.provenance['id'], results[1].provenance['id']) |
109
|
|
|
|
110
|
|
|
def test_GetModalities(self): |
111
|
|
|
self.provenance.discover('testdata') |
112
|
|
|
modalities = self.provenance.get().allModalities() |
113
|
|
|
self.assertIn('MRI', modalities) |
114
|
|
|
self.assertIn('DWI', modalities) |
115
|
|
|
self.assertIn('EEG', modalities) |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
if __name__ == '__main__': |
119
|
|
|
unittest.main() |
120
|
|
|
|
121
|
|
|
|