class ContextTests(DependencyInjectionTestBase): def setUp(self): super(ContextTests, self).setUp() patcher = patch('niprov.context.niprov') self.niprov = patcher.start() self.addCleanup(patcher.stop) from niprov import ProvenanceContext self.context = ProvenanceContext() self.context.deps = self.dependencies def test_get(self): self.assertEqual(self.context.get(), self.query) def test_add(self): self.context.add('file.p', True, {'c': 3}) self.niprov.adding.add.assert_called_with('file.p', True, {'c': 3}, self.dependencies) def test_log(self): self.context.log('new', 'trf', 'parents', 'code', 'logtext', False, 'script', 'user', {'prov': 1}, 'opts') self.niprov.plogging.log.assert_called_with('new', 'trf', 'parents', 'code', 'logtext', False, 'script', 'user', {'prov': 1}, 'opts', self.dependencies) def test_backup(self): self.context.backup() self.niprov.exporting.backup.assert_called_with(self.dependencies) def test_export(self): self.context.export('prov', 'medium', 'form') self.niprov.exporting.export.assert_called_with( 'prov', 'medium', 'form', False, self.dependencies) def test_import(self): self.context.importp('fpath') self.niprov.importing.importp.assert_called_with( 'fpath', self.dependencies) def test_print(self): self.context.print_('images') self.niprov.exporting.print_.assert_called_with( 'images', False, self.dependencies) def test_record(self): self.context.record('cmd', 'new', 'parents', True, 'args', 'kwargs', 'user', 'opts') self.niprov.recording.record.assert_called_with( 'cmd', 'new', 'parents', True, 'args', 'kwargs', 'user', 'opts', self.dependencies) def test_view(self): self.context.view('images', pipeline=True) self.niprov.exporting.view.assert_called_with('images', True, self.dependencies)
class ContextTests(DependencyInjectionTestBase): def setUp(self): super(ContextTests, self).setUp() patcher = patch('niprov.context.niprov') self.niprov = patcher.start() self.addCleanup(patcher.stop) from niprov import ProvenanceContext self.context = ProvenanceContext() self.context.deps = self.dependencies def test_get(self): self.assertEqual(self.context.get(), self.query) def test_add(self): self.context.add('file.p', True, {'c':3}) self.niprov.adding.add.assert_called_with('file.p', True, {'c':3}, self.dependencies) def test_log(self): self.context.log('new', 'trf', 'parents', 'code', 'logtext', False, 'script', 'user', {'prov':1}, 'opts') self.niprov.plogging.log.assert_called_with('new', 'trf', 'parents', 'code', 'logtext', False, 'script', 'user', {'prov':1}, 'opts', self.dependencies) def test_backup(self): self.context.backup() self.niprov.exporting.backup.assert_called_with(self.dependencies) def test_export(self): self.context.export('prov', 'medium', 'form') self.niprov.exporting.export.assert_called_with('prov', 'medium', 'form', False, self.dependencies) def test_import(self): self.context.importp('fpath') self.niprov.importing.importp.assert_called_with('fpath', self.dependencies) def test_print(self): self.context.print_('images') self.niprov.exporting.print_.assert_called_with('images', False, self.dependencies) def test_record(self): self.context.record('cmd', 'new', 'parents', True, 'args', 'kwargs', 'user', 'opts') self.niprov.recording.record.assert_called_with('cmd', 'new', 'parents', True, 'args', 'kwargs', 'user', 'opts', self.dependencies) def test_view(self): self.context.view('images', pipeline=True) self.niprov.exporting.view.assert_called_with('images', True, self.dependencies)
def handler(text, func, out, params, dependencies=Dependencies()): """mnefun on_process handler Responds to the following mnefun steps: fetch_raw_files: Runs discover on the subjects' raw data dirs. fetch_sss_files, apply_preprocessing_combined, save_epochs: For these steps, runs log for the new files. """ listener = dependencies.getListener() libs = dependencies.getLibraries() provenance = ProvenanceContext() funcname = func.func_name listener.mnefunEventReceived(funcname) paramdict = {} for paramname in dir(params): objectvalue = getattr(params, paramname) blacklist = ["get_projs_from", "inv_runs"] if hasattr(objectvalue, "__call__"): continue if paramname[0] == "_": continue if "array" in str(type(objectvalue)): continue if paramname in blacklist: continue paramdict[paramname] = objectvalue subjects = [params.subjects[i] for i in params.subject_indices] for subj in subjects: customprov = {"mnefun": paramdict} if funcname in ["fetch_raw_files", "score_fun", "fetch_sss_files"]: rawfiles = libs.mnefun.get_raw_fnames( params, subj, "raw", add_splits=True, run_indices=params.subject_run_indices ) if funcname == "fetch_raw_files": for f in rawfiles: provenance.add(f, provenance=customprov) elif funcname == "score_fun": eventfiles = libs.mnefun._paths.get_event_fnames(params, subj, params.subject_run_indices) for rawfile, eventfile in zip(rawfiles, eventfiles): provenance.log(eventfile, "scoring", rawfile, provenance=customprov) elif funcname == "fetch_sss_files": trans = "Signal Space Separation" sssfiles = libs.mnefun.get_raw_fnames(params, subj, "sss") for rawfile, sssfile in zip(rawfiles, sssfiles): provenance.log(sssfile, trans, rawfile, provenance=customprov) elif funcname == "apply_preprocessing_combined": trans = "Signal Space Projection" sssfiles = libs.mnefun.get_raw_fnames(params, subj, "sss") pcafiles = libs.mnefun.get_raw_fnames(params, subj, "pca") for sssfile, pcafile in zip(sssfiles, pcafiles): provenance.log(pcafile, trans, sssfile, provenance=customprov) elif funcname == "save_epochs": pcafiles = libs.mnefun.get_raw_fnames(params, subj, "pca") evtfiles = libs.mnefun._paths.get_epochs_evokeds_fnames(params, subj, params.analyses) for evtfile in evtfiles: if os.path.isfile(evtfile[0]): provenance.log(evtfile[0], "Epoching", pcafiles, provenance=customprov)
class TerminalApiTests(unittest.TestCase): def setUp(self): self.dbpath = os.path.expanduser( os.path.join('~', 'provenance_test.json')) if os.path.exists(self.dbpath): os.remove(self.dbpath) os.mkdir('temp') from niprov import ProvenanceContext self.provenance = ProvenanceContext() self.provenance.config.database_type = 'file' self.provenance.config.database_url = self.dbpath self.provenance.config.verbose = False self.oldstdout = sys.stdout sys.stdout = self.stdout = StringIO() def tearDown(self): sys.stdout = self.oldstdout self.stdout.close() shutil.rmtree('temp') @unittest.skip("Doesn't work on travis") def test_Report_pipeline(self): self.provenance.discover('testdata') raw = os.path.abspath('testdata/eeg/stub.cnt') self.provenance.log(absp('p1a.f'), 'test', raw, transient=True) self.provenance.log(absp('p1b.f'), 'test', raw, transient=True) self.provenance.log(absp('p2.f'), 'test', absp('p1a.f'), transient=True) self.provenance.export(None, 'stdout', 'simple', pipeline=True) exp = '' exp += '+---stub.cnt\n' exp += '| +---p1a.f\n' exp += '| | +---p2.f\n' #exp += '| +---p1b.f\n' # p1b is not related to p2f import time time.sleep(0.3) self.assertIn(exp, self.stdout.getvalue())
class TerminalApiTests(unittest.TestCase): def setUp(self): self.dbpath = os.path.expanduser(os.path.join('~','provenance_test.json')) if os.path.exists(self.dbpath): os.remove(self.dbpath) os.mkdir('temp') from niprov import ProvenanceContext self.provenance = ProvenanceContext() self.provenance.config.database_type = 'file' self.provenance.config.database_url = self.dbpath self.provenance.config.verbose = False self.oldstdout = sys.stdout sys.stdout = self.stdout = StringIO() def tearDown(self): sys.stdout = self.oldstdout self.stdout.close() shutil.rmtree('temp') @unittest.skip("Doesn't work on travis") def test_Report_pipeline(self): self.provenance.discover('testdata') raw = os.path.abspath('testdata/eeg/stub.cnt') self.provenance.log(absp('p1a.f'), 'test', raw, transient=True) self.provenance.log(absp('p1b.f'), 'test', raw, transient=True) self.provenance.log(absp('p2.f'), 'test', absp('p1a.f'), transient=True) self.provenance.export(None, 'stdout', 'simple', pipeline=True) exp = '' exp += '+---stub.cnt\n' exp += '| +---p1a.f\n' exp += '| | +---p2.f\n' #exp += '| +---p1b.f\n' # p1b is not related to p2f import time time.sleep(0.3) self.assertIn(exp, self.stdout.getvalue())
from niprov import ProvenanceContext provenance = ProvenanceContext() rawimg = provenance.add('testdata/eeg/stub.cnt') g1c1 = provenance.log('g1c1','action', str(rawimg.location), transient=True) #new, transformation, parents g1c2 = provenance.log('g1c2','action', str(rawimg.location), transient=True) #new, transformation, parents g2c1 = provenance.log('g2c1','action', str(g1c1.location), transient=True) #new, transformation, parents g2c2 = provenance.log('g2c2','action', str(g1c1.location), transient=True) #new, transformation, parents g2c3 = provenance.log('g2c3','action', [str(g1c2.location), str(rawimg.location)], transient=True) g3c1 = provenance.log('g3c1','action', str(g2c3.location), transient=True) #new, transformation, parents g3c2 = provenance.log('g3c2','action', str(g2c3.location), transient=True) #new, transformation, parents
class ProvenanceContextApiTests(unittest.TestCase): def setUp(self): self.dbpath = os.path.expanduser(os.path.join('~','provenance_test.json')) if os.path.exists(self.dbpath): os.remove(self.dbpath) os.mkdir('temp') from niprov import ProvenanceContext self.provenance = ProvenanceContext() self.provenance.config.database_type = 'file' self.provenance.config.database_url = self.dbpath def tearDown(self): shutil.rmtree('temp') def touch(self, path): with open(path,'w') as tempfile: tempfile.write('0') def test_Relative_paths(self): self.provenance.discover('testdata') newfile = 'temp/smoothed.test' self.touch(newfile) self.provenance.log(newfile, 'test', 'testdata/eeg/stub.cnt') img = self.provenance.get().byLocation(newfile) def test_Log(self): self.provenance.discover('testdata') newfile = 'temp/smoothed.test' self.touch(newfile) parent = os.path.abspath('testdata/eeg/stub.cnt') self.provenance.log(newfile, 'test', parent) testfpath = os.path.abspath(newfile) img = self.provenance.get().byLocation(testfpath) self.assertEqual(img.provenance['subject'], 'Jane Doe') self.assertEqual(img.provenance['size'], os.path.getsize(newfile)) def test_Record_with_user(self): self.provenance.discover('testdata') newfile = 'temp/recorded.test' self.touch(newfile) parent = os.path.abspath('testdata/eeg/stub.cnt') self.provenance.record('echo hallo', newfile, parent, user='******') testfpath = os.path.abspath(newfile) img = self.provenance.get().byLocation(testfpath) self.assertEqual(img.provenance['user'], '007') def test_Export_Import(self): from niprov.exceptions import UnknownFileError self.provenance.discover('testdata') discoveredFile = os.path.abspath('testdata/eeg/stub.cnt') self.assertIsNotNone(self.provenance.get().byLocation(discoveredFile)) backupFilepath = self.provenance.backup() os.remove(self.dbpath) # get rid of existing data. self.assertIsNone(self.provenance.get().byLocation(discoveredFile)) self.provenance.importp(backupFilepath) self.assertIsNotNone(self.provenance.get().byLocation(discoveredFile)) @unittest.skip("Doesn't work on Travis right now.") def test_Attach_provenance_string_in_file_based_on_config(self): import nibabel self.provenance.config.attach = True newfile = os.path.abspath('temp/fileX.nii.gz') shutil.copy('testdata/nifti/fieldmap.nii.gz', newfile) self.provenance.add(newfile) img = nibabel.load(newfile) self.assertEqual(img.get_header().extensions.count('comment'), 1) self.assertEqual(img.get_header().extensions[0].get_code(), 6) content = img.get_header().extensions[0].get_content() self.assertIn('"path": "{0}"'.format(newfile), content) def test_Approval(self): self.provenance.discover('testdata') self.provenance.markForApproval(['testdata/parrec/T1.PAR', 'testdata/parrec/T2.PAR']) imgs = self.provenance.markedForApproval() self.provenance.approve('testdata/parrec/T1.PAR') imgs = self.provenance.markedForApproval() def test_Comparison(self): # Given two PARREC images' provenance records par1 = self.provenance.add(abspath('testdata/parrec/T1.PAR')) par2 = self.provenance.add(abspath('testdata/parrec/T2.PAR')) # Comparing them returns a Diff object with methods testing equality self.assertFalse(self.provenance.compare(par1, par2).areEqual()) # Compare() can also be called as a method on the objects themselves, # and the Diff object has assert..() methods that raise AssertionErrors msgRegExp = ".*echo-time.*2\.08.*80\.0.*" with self.assertRaisesRegexp(AssertionError, msgRegExp): par1.compare(par2).assertEqualProtocol() def test_Search(self): x1 = self.provenance.add('x1', transient=True, provenance={'transformation':'needle and thread'}) x2 = self.provenance.add('x2/needle.y', transient=True, provenance={'transformation':'needle and thread'}) x3 = self.provenance.add('x3', transient=True, provenance={'transformation':'hammer and tongs'}) results = self.provenance.search('needle') self.assertEqual(len(results), 2) self.assertEqual(x2.provenance['id'], results[0].provenance['id']) self.assertEqual(x1.provenance['id'], results[1].provenance['id']) def test_GetModalities(self): self.provenance.discover('testdata') modalities = self.provenance.get().allModalities() self.assertIn('MRI', modalities) self.assertIn('DWI', modalities) self.assertIn('EEG', modalities) def test_Version_history(self): self.provenance.add('f', transient=True, provenance={'a':1}) img = self.provenance.get().byLocation('f') self.assertEqual(1, img.provenance['a']) self.provenance.add('f', transient=True, provenance={'a':2}) img = self.provenance.get().byLocation('f') self.assertEqual(2, img.provenance['a']) self.assertEqual(1, img.versions[-1]['a']) self.provenance.add('f', transient=True, provenance={'a':3}) img = self.provenance.get().byLocation('f') self.assertEqual(3, img.provenance['a']) self.assertEqual(2, img.versions[-1]['a']) self.assertEqual(1, img.versions[-2]['a']) def test_If_no_parent_provided_found_copy_considered_parent(self): self.provenance.add('testdata/eeg/stub.cnt') self.touch('temp/orig.f') self.provenance.log('temp/orig.f', 'op1', 'testdata/eeg/stub.cnt') shutil.copy('temp/orig.f', 'temp/copy.f') self.touch('temp/child.f') child = self.provenance.log('temp/child.f', 'op2', 'temp/copy.f') self.assertEqual(child.provenance['subject'], 'Jane Doe') copy = self.provenance.get().byLocation('temp/copy.f') self.assertIn('temp/orig.f', copy.provenance['parents'][0]) def test_Differentiates_Fifs(self): """ The fields tested here are just a subset. """ PROJDESC = 'ECG-planar-999--0.080-0.080-PCA-01' fiftypes = {'ave': {'fif-type':'ave', 'dimensions':[2,401,701]}, 'cov': {'fif-type':'cov', 'dimensions':[366, 366]}, 'epo': {'fif-type':'epo', 'highpass':0.10000000149011612}, 'fwd': {'fif-type':'fwd'}, 'trans': {'fif-type':'trans'}, 'proj' : {'fif-type':'proj', 'projection-description':PROJDESC}} for ftype, fields in fiftypes.items(): pth = os.path.abspath('testdata/fif/test-{}.fif'.format(ftype)) img = self.provenance.add(pth) for field, expectedValue in fields.items(): self.assertIn(field, img.provenance) self.assertEqual(img.provenance[field], expectedValue)
def handler(text, func, out, params, dependencies=Dependencies()): """mnefun on_process handler Responds to the following mnefun steps: fetch_raw_files: Runs discover on the subjects' raw data dirs. fetch_sss_files, apply_preprocessing_combined, save_epochs: For these steps, runs log for the new files. """ listener = dependencies.getListener() libs = dependencies.getLibraries() provenance = ProvenanceContext() funcname = func.func_name listener.mnefunEventReceived(funcname) paramdict = {} for paramname in dir(params): objectvalue = getattr(params, paramname) blacklist = ['get_projs_from', 'inv_runs'] if hasattr(objectvalue, '__call__'): continue if paramname[0] == '_': continue if 'array' in str(type(objectvalue)): continue if paramname in blacklist: continue paramdict[paramname] = objectvalue subjects = [params.subjects[i] for i in params.subject_indices] for subj in subjects: customprov = {'mnefun': paramdict} if funcname in ['fetch_raw_files', 'score_fun', 'fetch_sss_files']: rawfiles = libs.mnefun.get_raw_fnames( params, subj, 'raw', add_splits=True, run_indices=params.subject_run_indices) if funcname == 'fetch_raw_files': for f in rawfiles: provenance.add(f, provenance=customprov) elif funcname == 'score_fun': eventfiles = libs.mnefun._paths.get_event_fnames( params, subj, params.subject_run_indices) for rawfile, eventfile in zip(rawfiles, eventfiles): provenance.log(eventfile, 'scoring', rawfile, provenance=customprov) elif funcname == 'fetch_sss_files': trans = 'Signal Space Separation' sssfiles = libs.mnefun.get_raw_fnames(params, subj, 'sss') for rawfile, sssfile in zip(rawfiles, sssfiles): provenance.log(sssfile, trans, rawfile, provenance=customprov) elif funcname == 'apply_preprocessing_combined': trans = 'Signal Space Projection' sssfiles = libs.mnefun.get_raw_fnames(params, subj, 'sss') pcafiles = libs.mnefun.get_raw_fnames(params, subj, 'pca') for sssfile, pcafile in zip(sssfiles, pcafiles): provenance.log(pcafile, trans, sssfile, provenance=customprov) elif funcname == 'save_epochs': pcafiles = libs.mnefun.get_raw_fnames(params, subj, 'pca') evtfiles = libs.mnefun._paths.get_epochs_evokeds_fnames( params, subj, params.analyses) for evtfile in evtfiles: if os.path.isfile(evtfile[0]): provenance.log(evtfile[0], 'Epoching', pcafiles, provenance=customprov)
from niprov import ProvenanceContext provenance = ProvenanceContext() rawimg = provenance.add('testdata/eeg/stub.cnt') g1c1 = provenance.log('g1c1', 'action', str(rawimg.location), transient=True) #new, transformation, parents g1c2 = provenance.log('g1c2', 'action', str(rawimg.location), transient=True) #new, transformation, parents g2c1 = provenance.log('g2c1', 'action', str(g1c1.location), transient=True) #new, transformation, parents g2c2 = provenance.log('g2c2', 'action', str(g1c1.location), transient=True) #new, transformation, parents g2c3 = provenance.log( 'g2c3', 'action', [str(g1c2.location), str(rawimg.location)], transient=True) g3c1 = provenance.log('g3c1', 'action', str(g2c3.location), transient=True) #new, transformation, parents g3c2 = provenance.log('g3c2', 'action', str(g2c3.location), transient=True) #new, transformation, parents
from niprov import ProvenanceContext provenance = ProvenanceContext() rawstru = provenance.add('testdata/dicom/T1.dcm') rawbold = provenance.add('testdata/parrec/T2_.PAR') rawevnt = provenance.add('events.log', transient=True) stru = provenance.log('struct.nii','reconstruction', str(rawstru.location), transient=True) run1 = provenance.log('run1.nii','reconstruction', str(rawbold.location), transient=True) dsgn = provenance.log('design.mat','parse events', str(rawevnt.location), transient=True) moco = provenance.log('run1_moco.nii','motion correction', str(run1.location), transient=True) corg = provenance.log('run1_coreg.nii','coregistration', [str(moco.location), str(stru.location)], transient=True) stat = provenance.log('run1_tstat.nii','statistics', [str(corg.location), str(dsgn.location)], transient=True)
from niprov import ProvenanceContext provenance = ProvenanceContext() rawstru = provenance.add('testdata/dicom/T1.dcm') rawbold = provenance.add('testdata/parrec/T2_.PAR') rawevnt = provenance.add('events.log', transient=True) stru = provenance.log('struct.nii', 'reconstruction', str(rawstru.location), transient=True) run1 = provenance.log('run1.nii', 'reconstruction', str(rawbold.location), transient=True) dsgn = provenance.log('design.mat', 'parse events', str(rawevnt.location), transient=True) moco = provenance.log('run1_moco.nii', 'motion correction', str(run1.location), transient=True) corg = provenance.log( 'run1_coreg.nii', 'coregistration', [str(moco.location), str(stru.location)], transient=True) stat = provenance.log(