def build_graph(subjects, database, types, verbose=True): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases.database(osp.abspath(database)) dates = {} lines = [], [] steps = [] for i, s in enumerate(subjects): if verbose and False: print s, '(%s/%s)'%(i, len(subjects)) dates[s] = get_subject_dates(s, db, types) stypes = [types.index(e) for e in dates[s].keys()] ordered_dates = [dates[s][types[k]] for k in stypes] if len(stypes) == 0: print 'no items found for subject', s continue lines[0].extend(pd.to_datetime([datetime.fromtimestamp(e) for e in ordered_dates])) lines[1].extend([i]*len(ordered_dates)) steps.extend(stypes) l0, l1 = {}, {} for x, y ,label in zip(lines[0], lines[1], steps): l0.setdefault(label, []).append(x) l1.setdefault(label, []).append(y) legends = [types[i] for i in set(steps)] plot(subjects, l0, l1, steps, legends)
def build_graph(subjects, database, types, verbose=True): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases.database(osp.abspath(database)) dates = {} lines = [], [] steps = [] for i, s in enumerate(subjects): if verbose and False: print s, '(%s/%s)' % (i, len(subjects)) dates[s] = get_subject_dates(s, db, types) stypes = [types.index(e) for e in dates[s].keys()] ordered_dates = [dates[s][types[k]] for k in stypes] if len(stypes) == 0: print 'no items found for subject', s continue lines[0].extend( pd.to_datetime([datetime.fromtimestamp(e) for e in ordered_dates])) lines[1].extend([i] * len(ordered_dates)) steps.extend(stypes) l0, l1 = {}, {} for x, y, label in zip(lines[0], lines[1], steps): l0.setdefault(label, []).append(x) l1.setdefault(label, []).append(y) legends = [types[i] for i in set(steps)] plot(subjects, l0, l1, steps, legends)
def check_subject(subject, database, jsonfile, antecfile, verbose=True): ''' Given a subject, the path to a database and a JSON file describing input/outputs of a pipeline, checks''' from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases.database(osp.abspath(database)) j = json.load(open(jsonfile)) types = json.load(open(antecfile)) #collect_types(jsonfile) dates = get_subject_dates(subject, db, types) issues = [] for k, v in j.items(): outputs = [e.strip('>') for e in v if e.startswith('>')] inputs = [e.strip('@') for e in v if e.startswith('@')] for i in inputs: for o in outputs: if o in dates and i in dates: if dates[o] < dates[i]: if verbose: print 'problem with', o, '(%s)' % datetime.fromtimestamp( dates[o]), i, '(%s)' % datetime.fromtimestamp( dates[i]), subject, k issues.append( (o, datetime.fromtimestamp(dates[o]), i, datetime.fromtimestamp(dates[i]), subject, k)) return issues
def identify_tissues_axon(subject, database='/home/grg/data/ALFA_DWI', doit=False, verbose=True): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases._databases[database] fast = [e for e in db.findDiskItems(_type='ALFA DWI B0 Brain FSL FAST', subject=subject)][0].fullPath() b0 = [e for e in db.findDiskItems(_type='ALFA DWI B0 Map', subject=subject)][0].fullPath() identify_tissues(fast, b0, doit, verbose)
def main(noapp=0): app = None if noapp == 0: print "NO APP" app = QtGui.QApplication(sys.argv) axon.initializeProcesses() ll = LocateLeds(app=app) ll.show() if noapp == 0: sys.exit(app.exec_())
def backup_freesurfer_subject(subject, tarfile, dbdir, doit=False, verbose=True): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases._databases[dbdir] tmpdir = tempfile.mkdtemp() if not doit: print '##############################################' print 'Not doing the job. Just printing the commands.' print '##############################################' fstypes = ['ResampledWhite', 'ResampledPial', 'ResampledFreesurferThicknessType', 'Freesurfer Cortical Parcellation using Destrieux Atlas', 'Freesurfer aseg'] if verbose: print '=== Collecting data for subject', subject, ' ===' for each in fstypes[0:3]: for side in ['left', 'right']: item = [e for e in db.findDiskItems(_type = each, subject=subject, side=side)] if len(item)!=1: raise Exception(' Expected one item of type %s found %s (%s)'%(each, len(item), ' - '.join([e.fullPath() for e in item]))) copy_item(item[0], tmpdir, doit, verbose) if verbose: print each, side, item for each in fstypes[3:]: item = [e for e in db.findDiskItems(_type = each, subject=subject)] if len(item)!=1: raise Exception(' Expected one item of type %s found %s (%s)'%(each, len(item), ' - '.join([e.fullPath() for e in item]))) copy_item(item[0], tmpdir, doit, verbose) print each, item mri = [e for e in db.findDiskItems(_type='T1 FreesurferAnat', _format='NIFTI-1 image', subject=subject)] if len(mri)!=1: raise Exception(' Expected one item of type %s found %s (%s)'%(each, len(item), ' - '.join([e.fullPath() for e in mri]))) copy_item(mri[0], tmpdir, doit, verbose) print 'mri', mri opt = 'cvf' if osp.splitext(tarfile)[1] == '.gz': opt = opt + 'z' elif osp.splitext(tarfile)[1] != '.tar': tarfile = tarfile + '.tar' if verbose: print 'Creating %s'%tarfile p1, p2 = tmpdir, subject cmd = 'tar %s %s -C %s %s'%(opt, tarfile, p1, p2) if verbose: print cmd if doit: os.system(cmd)
def run(self, directory): import os.path as osp directory = osp.abspath(directory) from brainvisa import axon axon.initializeProcesses() import neuroHierarchy for direct, db in neuroHierarchy.databases._databases.items(): if direct == directory: break if direct != directory: raise Exception('%s should be a valid Axon database.\nCurrent'\ 'existing Axon databases : %s'\ %(directory, neuroHierarchy.databases._databases.keys())) items = [each for each in db.findDiskItems(**{'_type': 'Any Type'})] self.identified = {} self.index = [] self.firstcol = 'subject' self.headers = set() for each in items: subject = each.get(self.firstcol) self.index.append(subject) self.identified.setdefault(subject, {}) self.headers.add(each.type.name) fp = each.fullPath() self.identified[subject].setdefault(each.type.name, []).append(fp) self.index = sorted(list(set(self.index))) self.headers = sorted(list(self.headers)) self.repository = directory self.count_table = [] self.table = [] for s in self.index: h = self.headers self.count_table.append([len(self.identified[s].get(e, [])) for e in h]) self.table.append([self.identified[s].get(e, []) for e in h])
def check_subject(subject, database, jsonfile, antecfile, verbose=True): ''' Given a subject, the path to a database and a JSON file describing input/outputs of a pipeline, checks''' from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases.database(osp.abspath(database)) j = json.load(open(jsonfile)) types = json.load(open(antecfile)) #collect_types(jsonfile) dates = get_subject_dates(subject, db ,types) issues = [] for k,v in j.items(): outputs = [e.strip('>') for e in v if e.startswith('>')] inputs = [e.strip('@') for e in v if e.startswith('@')] for i in inputs: for o in outputs: if o in dates and i in dates: if dates[o] < dates[i]: if verbose: print 'problem with', o,'(%s)'%datetime.fromtimestamp(dates[o]), i, '(%s)'%datetime.fromtimestamp(dates[i]), subject,k issues.append((o, datetime.fromtimestamp(dates[o]), i, datetime.fromtimestamp(dates[i]), subject, k)) return issues
def backup_subject_axon(subject, tarfile, database, doit=False, verbose=True, remove=False): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases._databases[database] items = [e for e in db.findDiskItems(_type='Any Type', subject=subject)] items = [e.fullPath() for e in items if e.get('subject')] tmpdir = osp.join(tempfile.mkdtemp(), osp.split(database)[1]) action = 'cp' if not remove else 'mv' if verbose: print 'Found %s items' % len(items) print items if not doit: print '##############################################' print 'Not doing the job. Just printing the commands.' print '##############################################' for i in items: f = i.split(database)[1][1:] newdir = osp.join(tmpdir, osp.split(f)[0]) if not osp.isdir(newdir): cmd = 'super-mkdir %s' % newdir if verbose: print cmd if doit: os.makedirs(newdir) cmd = 'cp %s %s' % (i, osp.join(tmpdir, f)) if verbose: print cmd if doit: os.system(cmd) if remove: db.removeDiskItem(db.createDiskItemFromFilename(i), eraseFiles=True) if action == 'mv': removed = 0 for i in items: if not osp.exists(i): removed += 1 if verbose: print 'Removed %s files out of %s' % (removed, len(items)) emptydirs = list(find_empty_dirs(database)) print len(emptydirs), 'empty directories in %s (%s)' % ( database, ', '.join(emptydirs)) opt = 'cvf' if osp.splitext(tarfile)[1] == '.gz': opt = opt + 'z' elif osp.splitext(tarfile)[1] != '.tar': tarfile = tarfile + '.tar' if verbose: print 'Creating %s' % tarfile p1, p2 = osp.split(tmpdir) cmd = 'tar %s %s -C %s %s' % (opt, tarfile, p1, p2) if verbose: print cmd if doit: os.system(cmd)
#!/usr/bin/env python import argparse if __name__ == '__main__': parser = argparse.ArgumentParser(description='Axon database browser') opts = parser.parse_args() from brainvisa import axon, processes axon.initializeProcesses() from brainvisa.data.qtgui.hierarchyBrowser import HierarchyBrowser from PyQt4 import QtGui, QtCore, Qt import sys app = Qt.QApplication(sys.argv) a = HierarchyBrowser() a.show() app.exec_()
#!/usr/bin/env python import argparse if __name__ == '__main__': parser = argparse.ArgumentParser(description='Axon database browser') opts = parser.parse_args() from brainvisa import axon, processes axon.initializeProcesses() from brainvisa.data.qtgui.hierarchyBrowser import HierarchyBrowser from PyQt4 import QtGui, QtCore, Qt import sys app = Qt.QApplication( sys.argv ) a = HierarchyBrowser() a.show() app.exec_()
def backup_subject_axon(subject, tarfile, database, doit=False, verbose=True, remove=False): from brainvisa import axon axon.initializeProcesses() import neuroHierarchy db = neuroHierarchy.databases._databases[database] items = [e for e in db.findDiskItems(_type='Any Type', subject=subject)] items = [e.fullPath() for e in items if e.get('subject')] tmpdir = osp.join(tempfile.mkdtemp(), osp.split(database)[1]) action = 'cp' if not remove else 'mv' if verbose: print 'Found %s items'%len(items) print items if not doit: print '##############################################' print 'Not doing the job. Just printing the commands.' print '##############################################' for i in items: f = i.split(database)[1][1:] newdir = osp.join(tmpdir, osp.split(f)[0]) if not osp.isdir(newdir): cmd = 'super-mkdir %s'%newdir if verbose: print cmd if doit: os.makedirs(newdir) cmd = 'cp %s %s'%(i, osp.join(tmpdir, f)) if verbose: print cmd if doit: os.system(cmd) if remove: db.removeDiskItem(db.createDiskItemFromFilename(i), eraseFiles=True) if action == 'mv': removed = 0 for i in items: if not osp.exists(i): removed += 1 if verbose: print 'Removed %s files out of %s'%(removed, len(items)) emptydirs = list(find_empty_dirs(database)) print len(emptydirs), 'empty directories in %s (%s)'%(database, ', '.join(emptydirs)) opt = 'cvf' if osp.splitext(tarfile)[1] == '.gz': opt = opt + 'z' elif osp.splitext(tarfile)[1] != '.tar': tarfile = tarfile + '.tar' if verbose: print 'Creating %s'%tarfile p1, p2 = osp.split(tmpdir) cmd = 'tar %s %s -C %s %s'%(opt, tarfile, p1, p2) if verbose: print cmd if doit: os.system(cmd)