def test_006_pickle_resource_data(): c = pyxnat.Interface(server='https://central.xnat.org', anonymous=True) resources = pk.get_resources(c) r = resources.pop() assert(len(r) == 4)
def test_003_pickle_subject_data(): c = pyxnat.Interface(server='https://central.xnat.org', anonymous=True) subjects = pk.get_subjects(c) s = subjects.pop() keys = sorted(['gender', 'handedness', 'project', 'ID', 'URI', 'age']) assert(sorted(s.keys()) == keys)
def _connect_xnat(self): import pyxnat cfg = self.config.get('xnat') xnat = pyxnat.Interface(server=cfg.get('server'), user=cfg.get('user'), password=cfg.get('password'), cachedir=cfg.get('cachedir')) self.api_imaging = xnat
def main(args): x1 = pyxnat.Interface(config=args.source_config) x2 = pyxnat.Interface(config=args.dest_config) columns = ['subject_label', 'label'] e1 = x1.array.experiments(experiment_id=args.experiment_id, columns=columns).data[0] p = x2.select.project(args.project_id) s = p.subject(e1['subject_label']) if not s.exists(): s.create() e = s.experiment(e1['label']) e.create() src_sess = x1.select.project(e1['project']).subject(e1['subject_ID']).experiment(e1['ID']) dst_sess = e copy_session(src_sess, dst_sess, '/tmp')
def test_004_pickle_experiment_data(): c = pyxnat.Interface(server='https://central.xnat.org', anonymous=True) experiments = pk.get_experiments(c) e = experiments.pop() keys = sorted(['xnat:subjectassessordata/id', 'subject_ID', 'ID', 'project', 'date', 'xsiType', 'URI']) assert(sorted(e.keys()) == keys)
def main(): user = sys.argv[1] if 2 <= len(sys.argv) else raw_input('ConnectomeDB user: '******'https://db.humanconnectome.org', user, password) cdb.packages.download(unrelated_fem_10, structural + diffusion + r_fMRI) cdb.packages.download(unrelated_masc_10, structural + diffusion + r_fMRI)
def connect(self): """ Connect to xnat server via config """ self.conn = pyxnat.Interface(server=self.url, user=self.user, verify=True, password=self.passwd, cachedir='/tmp') # connection object
def test_005_pickle_scan_data(): c = pyxnat.Interface(server='https://central.xnat.org', anonymous=True) scans = pk.get_scans(c) s = scans.pop() keys = sorted(['session_ID', 'xnat:imagesessiondata/subject_id', 'ID', 'project', 'xsiType', 'xnat:imagescandata/id', 'URI', 'xnat:imagescandata/quality', 'xnat:imagescandata/type']) assert(sorted(s.keys()) == keys)
def login(): p = pickle.load(open(cfg.PICKLE_PATH, 'rb')) if request.method == 'GET': kwargs = {} if 'error' in session: kwargs['error'] = session.pop('error') return render_template('authentication/login.html', **kwargs) elif request.method == 'POST': username = request.form['username'] x = pyxnat.Interface(user=username, password=request.form['password'], server=p['server'], verify=p['verify']) has_access = x.head('').ok if has_access: config = json.load(open(cfg.DASHBOARD_CONFIG_PATH)) roles = config['roles'] if username in roles['forbidden']['users']: session['error'] = 'Access denied.' return redirect(url_for('auth.login')) user_roles = [ r for r in roles.keys() if username in roles[r]['users'] ] if len(user_roles) > 1: raise Exception('%s has multiple roles' % str(username)) elif len(user_roles) == 0: role = 'guest' # no role found, guest by default else: role = user_roles[0] # Collect graphs and select them based on access rights graphs = [graph() for graph in __find_all_graphs__(dsh)] # Add data to session session['username'] = username session['server'] = p['server'] session['role'] = role session['graphs'] = [ g.name for g in graphs if role in g.visibility ] session['projects'] = roles[role]['projects'] # Redirect to dashboard return redirect(url_for('dashboard.overview')) else: session['error'] = 'Wrong password/username.' return redirect(url_for('auth.login'))
def main(args=None): """Main program loop Args: args (object): An attributed dict like argparse.Namespace. Returns: None """ # Create interface using stored configuration ifc = pyxnat.Interface(config=args.config) if args.reverse_lookup: search_field = 'xnat:subjectData/SUBJECT_ID' result_idx = 2 else: search_field = 'xnat:subjectData/SUBJECT_LABEL' result_idx = 0 if args.print_project: result_idx = 1 fields_per_subject = [ 'xnat:subjectData/SUBJECT_ID', 'xnat:subjectData/PROJECT', 'xnat:subjectData/SUBJECT_LABEL' ] output = "" for search in args.search: pattern = (search_field, 'LIKE', '%' + search + '%') subjects = ifc.select('xnat:subjectData', fields_per_subject).where([pattern]).items() if args.print_keys: if len(subjects) > 0: fmt = '{0},{1}' res = [ fmt.format(search, record[result_idx]) for record in subjects ] output += '\n'.join(res) else: output += ''.join(search + ',\n') else: res = ['{0}\n'.format(record[result_idx]) for record in subjects] output += ''.join(res) if args.outfile: with open(args.outfile, 'w') as fi: fi.write("case_id, site_id\n") fi.write(output) fi.close() if verbose: print output else: print output
def connect(self): """ Connect to xnat server via config """ try: self.conn = pyxnat.Interface(server=self.url, user=self.user, verify=True, password=self.passwd) # connection object print("Connected to XNAT") except DatabaseError as e: raise e
def test_001_sessionmirror(): from sessionmirror import create_parser, main parser = create_parser() cfg = op.join(op.dirname(op.abspath(__file__)), 'central.cfg') central = pyxnat.Interface(config=cfg) e = 'CENTRAL_E74609' args = ['--h1', cfg, '--h2', cfg, '-e', e, '-p', 'nosetests3'] args = parser.parse_args(args) main(args) e1 = central.array.experiments(experiment_id=e, columns=['subject_label']).data[0]
def test_instance_details(): # Normal flow details = fetch_object_connected.get_instance_details() assert isinstance(details['projects'], list) assert isinstance(details['subjects'], list) assert isinstance(details['experiments'], list) assert isinstance(details['scans'], list) # Wrong password obj = pyxnat.Interface(user="******", password="******", server="https://central.xnat.org", verify=True) obj.save_config('xnat_dashboards/config/wrong_pass.cfg') fetch_object_conn_pwd_incorrect = data_fetcher.Fetcher( 'xnat_dashboards/config/wrong_pass.cfg') assert fetch_object_conn_pwd_incorrect.get_instance_details() == 401 # Wrong URI obj = pyxnat.Interface(user="******", password="******", server="https://central.xnat.org/", verify=True) obj.save_config('xnat_dashboards/config/wrong_uri.cfg') fetch_object_conn_uri_incorrect = data_fetcher.Fetcher( 'xnat_dashboards/config/wrong_uri.cfg') assert fetch_object_conn_uri_incorrect.get_instance_details() == 500 # Wrong URL obj = pyxnat.Interface(user="******", password="******", server="https://central.xna", verify=True) obj.save_config('xnat_dashboards/config/wrong_url.cfg') fetch_object_conn_url_incorrect = data_fetcher.Fetcher( 'xnat_dashboards/config/wrong_url.cfg') assert fetch_object_conn_url_incorrect.get_instance_details() == 1
def __init__(self, user, password, cachedir="/scratch/jjlee/Singularity", prj="CCIR_00754", sbj=None, ses=None, scn=None): """ :param user: :param password: :param cachedir is the preferred cache directory: :param prj: :param sbj: :param ses: :param scn: """ self.host = 'https://cnda.wustl.edu' self.user = user #os.getenv('CNDA_UID') self.password = password #os.getenv('CNDA_PWD') self.cachedir = cachedir os.chdir(self.cachedir) self.xnat = pyxnat.Interface(self.host, user=self.user, password=self.password, cachedir=self.cachedir) assert (isinstance(self.xnat, pyxnat.core.interfaces.Interface)) self.project = self.xnat.select.project(prj) assert (isinstance(self.project, pyxnat.core.resources.Project)) if sbj: self.subject = self.project.subject(sbj) assert (isinstance(self.subject, pyxnat.core.resources.Subject)) else: self.subject = self.project.subjects() assert (isinstance(self.subject, pyxnat.resources.Subjects)) if ses: self.session = self.subject.experiment(ses) assert (isinstance(self.session, pyxnat.core.resources.Experiment)) else: self.session = self.subject.experiments() assert (isinstance(self.session, pyxnat.core.resources.Experiments)) if scn: self.scan = self.session.scan(str(scn)) assert (isinstance(self.scan, pyxnat.core.resources.Scan)) else: self.scan = self.session.scans() assert (isinstance(self.scan, pyxnat.core.resources.Scans)) self.scan_dicom = self.scan.resource('DICOM') self.rawdata = self.session.resource('RawData')
def test_002_pickle_project_data(): c = pyxnat.Interface(server='https://central.xnat.org', anonymous=True) projects = pk.get_projects(c) p = projects.pop() keys = sorted(['insert_date', 'insert_user', 'id', 'name', 'name_csv', 'description', 'description_csv', 'secondary_id', 'keywords', 'pi', 'project_invs', 'project_access', 'project_users', 'project_owners', 'project_members', 'project_collabs', 'project_last_workflow', 'project_last_access', 'project_fav', 'proj_mr_count', 'proj_ct_count', 'proj_pet_count', 'proj_ut_count']) assert(sorted(p.keys()) == keys)
def main(self, server, user, pwd, project, scantypes): cachedir = self.tempdir('XNAT') include = '{}(.*)\.zip$'.format(re.escape(project)) zipfiles = { v: op.join(cachedir, v) for v in os.listdir(cachedir) if re.search(include, v) } if len(zipfiles) == 0: xnat = pyxnat.Interface(server=server, user=user, password=pwd.value, cachedir=cachedir) proj = xnat.select.project(project) if not proj.exists(): raise LookupError( 'Project "{}" does not seem to exist on {} for user {}.'. format(project, server, user)) subjects = proj.subjects() if not subjects: print('Project "{}" is empty!'.format(project, server, user)) if isinstance(scantypes, (list, tuple)): scantypes = ','.join(scantypes) else: scantypes = 'ALL' for subj in subjects: print('{}'.format(subj)) experiments = subj.experiments().get() for m in experiments: expmt = subj.experiment(m) scans = expmt.scans() sub_ses = subj.id() if len(experiments) > 0: sub_ses += '_{}'.format(expmt.id()) print( "Downloading scantypes {} from project {}, subject {}, session {}" .format(scantypes, proj.id(), subj.id(), expmt.id())) zipfiles[sub_ses] = scans.download(cachedir, type=scantypes, extract=False) return FancyDict(zipfiles)
def test_002_deletesubject(): print('DELETING') cfg = op.join(op.dirname(op.abspath(__file__)), 'central.cfg') central = pyxnat.Interface(config=cfg) e = 'CENTRAL_E74609' e0 = central.array.experiments(experiment_id=e, columns=['subject_label', 'label']).data[0] subject_label = e0['subject_label'] experiment_label = e0['label'] e1 = central.array.experiments(project_id='nosetests3', subject_label=subject_label, experiment_label=experiment_label, columns=['subject_id']).data[0] e2 = central.select.project('nosetests3').subject( e1['subject_ID']).experiment(e1['ID']) assert (e2.exists()) e2.delete() assert (not e2.exists())
def test_save(mocker): # Check code for normal flow return_value = { 'server': 'https://central.xnat.org', 'info': 'data', 'projects': [], 'subjects': [], 'experiments': [], 'scans': []} mocker.patch( 'xnat_dashboards.pyxnat_interface.data_fetcher.Fetcher.get_resources', return_value=[]) mocker.patch( 'xnat_dashboards.bbrc.data_fetcher.Fetcher' '.get_resource', return_value=[]) mocker.patch( 'xnat_dashboards.pyxnat_interface.data_fetcher.Fetcher' '.get_instance_details', return_value=return_value) pickle_saver.PickleSaver('xnat_dashboards/config/central.cfg').save() with open(config.PICKLE_PATH, 'rb') as handle: data = pickle.load(handle) assert isinstance(data, dict) assert isinstance(data['info'], dict) assert isinstance(data['resources'], list) assert data['extra_resources'] is None # Code flow if server url mismatches from the already created file # Create a cfg file with different url obj = pyxnat.Interface( user="******", password="******", server="https://central.xna", verify=True) obj.save_config('xnat_dashboards/config/wrong_url.cfg') assert pickle_saver.PickleSaver( 'xnat_dashboards/config/wrong_url.cfg').save() == 1
def main(args=None): if args.verbose: print("Validating input csv.") csv = parse_csv(args.input) # currently just working with one visit event = event_mapping.get(csv.visit_id.get(0)) if args.verbose: print("Creating connection...") dataentry_key_file = open(os.path.join(os.path.expanduser("~"), '.server_config', 'redcap-dataentry-token'), 'r') dataentry_api_key = dataentry_key_file.read().strip() project_entry = redcap.Project('https://ncanda.sri.com/redcap/api/', dataentry_api_key, verify_ssl=False) mri_form = project_entry.export_records(forms=['mr_session_report'], events=[event], fields=['subject_id'], format='df') visit_form = project_entry.export_records(forms=['visit'], events=[event], fields=['visit_ignore', 'visit_ignore_why'], format='df') # These are subject ids from the list that are in REDCap. rc_filter = mri_form.mri_xnat_sid.isin(csv.subject_id) rc_cases = mri_form[rc_filter] # These are the cases that are not in REDCap (i.e., missing sessions) non_rc_filter = ~csv.subject_id.isin(rc_cases.mri_xnat_sid) non_rc_cases = csv[non_rc_filter] # now look up what info is on xnat # Create interface using stored configuration ifc = pyxnat.Interface(config=os.path.join(os.path.expanduser("~"), '.server_config/ncanda.cfg')) print visit_form
def set_xnat_connection(connection_interface=None): """ Setup XNAT to push and pull Parameters ---------- connection_interface : { pxnat.Interface, dict } Set the PyXNAT interface or a dictionary with keys server, user, password, cachedir (optional) """ global xnat_interface if not has_pyxnat: raise Exception('You need to install PyXNAT to use this functionality') if isinstance(connection_interface, dict): xnat_interface = pyxnat.Interface(**connection_interface) elif isinstance(connection_interface, pyxnat.Interface): xnat_interface = connection_interface else: xnat_interface = None if DEBUG_msg: print("Connected to XNAT Server")
import os.path as op from subprocess import Popen import multiprocessing as mp import pyxnat URL = 'https://central.xnat.org' # central URL BET = 'fsl4.1-bet2' # BET executable path central = pyxnat.Interface(URL, anonymous=True) # connection object def bet(in_img, in_hdr): # Python wrapper on FSL BET, essentially a system call in_image = in_img.get() # download .img in_hdr.get() # download .hdr path, name = op.split(in_image) in_image = op.join(path, name.rsplit('.')[0]) out_image = op.join(path, name.rsplit('.')[0] + '_brain') print('==> %s' % in_image[-120:]) Popen('%s %s %s' % (BET, in_image, out_image), shell=True).communicate() return out_image def notify(message): # message to notify the end of a BET process print('<== %s' % message[-120:]) pool = mp.Pool(processes=mp.cpu_count() * 2) # pool of concurrent workers images = {} query = ('/projects/CENTRAL_OASIS_CS/subjects/*'
import pyxnat x = pyxnat.Interface(config='.xnat.cfg') p = x.select.project('nosetests') p.create() for i in range(3, 10): p = x.select.project('nosetests%s'%i) p.create() uri = '/data/projects/nosetests' options = {'alias': 'nosetests2'} data = x.put(uri, params=options).text
import pyxnat import os xnat = pyxnat.Interface('http://xnat.ninds.nih.gov/', 'linc9', 'linc9') project = xnat.select.project('CGRD_test1') subject = project.subject('test1') experiment = subject.experiment('17BQ36167X01') files_resource = experiment.resource('radiology reports') file1 = files_resource.file('README.md') file1.put('../README.md') xnat.disconnect() print('done')
import ConfigParser import pyxnat config = ConfigParser.RawConfigParser() config.read('./xnat.cfg') xnat_config = { kw: config.get('xnat', kw) for kw in ['server', 'user', 'password'] } xnat = pyxnat.Interface(**xnat_config)
#!pip install pyxnat #!pip install xnat # XNATpy import pyxnat import xnat import os import glob from datetime import date # Display Today's Date print('Current Date: %s\npyXNAT version: %s\nXNATpy version: %s' % (date.today(), pyxnat.__version__, xnat.__version__)) ##################### # Server Connection # ##################### # Session Implementation session = pyxnat.Interface( 'http://120.126.47.114', # Connect to Server (website https / IP) user='******', # Username Registered password='******') # Password projectID = input('plz input Project ID: ') #input project id subjects = session.select.project( projectID).subjects().get() #how many subjects print(subjects) allSessions = [] for i, subject in enumerate(subjects): label = session.select.project(projectID).subject( subject).label() # Subject name = e.g. Test2 -> Test print(label), ('%i/%i' % (i + 1, len(subjects))) sessions = session.select.project(projectID).subjects(subject).experiments( ).get() #how many data in this Subject(include accession) allSessions.append(sessions) # append Subject Accession
def _list_outputs(self): """Execute this module. """ # setup XNAT connection cache_dir = self.inputs.cache_dir or tempfile.gettempdir() if self.inputs.config: xnat = pyxnat.Interface(config=self.inputs.config) else: xnat = pyxnat.Interface(self.inputs.server, self.inputs.user, self.inputs.pwd, cache_dir) # if possible share the subject from the original project if self.inputs.share: result = xnat.select( 'xnat:subjectData', ['xnat:subjectData/PROJECT', 'xnat:subjectData/SUBJECT_ID' ]).where('xnat:subjectData/SUBJECT_ID = %s AND' % self.inputs.subject_id) subject_id = self.inputs.subject_id # subject containing raw data exists on the server if isinstance(result.data[0], dict): result = result.data[0] shared = xnat.select( '/project/%s/subject/%s' % (self.inputs.project_id, self.inputs.subject_id)) if not shared.exists(): # subject not in share project share_project = xnat.select('/project/%s' % self.inputs.project_id) if not share_project.exists(): # check project exists share_project.insert() subject = xnat.select('/project/%(project)s' '/subject/%(subject_id)s' % result) subject.share(str(self.inputs.project_id)) else: # subject containing raw data does not exist on the server subject_id = '%s_%s' % (quote_id( self.inputs.project_id), quote_id(self.inputs.subject_id)) # setup XNAT resource uri_template_args = { 'project_id': quote_id(self.inputs.project_id), 'subject_id': subject_id, 'experiment_id': '%s_%s_%s' % (quote_id(self.inputs.project_id), quote_id( self.inputs.subject_id), quote_id(self.inputs.experiment_id)) } if self.inputs.share: uri_template_args['original_project'] = result['project'] if self.inputs.assessor_id: uri_template_args['assessor_id'] = ( '%s_%s' % (uri_template_args['experiment_id'], quote_id(self.inputs.assessor_id))) elif self.inputs.reconstruction_id: uri_template_args['reconstruction_id'] = ( '%s_%s' % (uri_template_args['experiment_id'], quote_id(self.inputs.reconstruction_id))) # gather outputs and upload them for key, files in self.inputs._outputs.items(): for name in filename_to_list(files): if isinstance(name, list): for i, file_name in enumerate(name): push_file(self, xnat, file_name, '%s_' % i + key, uri_template_args) else: push_file(self, xnat, name, key, uri_template_args)
def _list_outputs(self): # infields are mandatory, however I could not figure out # how to set 'mandatory' flag dynamically, hence manual check cache_dir = self.inputs.cache_dir or tempfile.gettempdir() if self.inputs.config: xnat = pyxnat.Interface(config=self.inputs.config) else: xnat = pyxnat.Interface(self.inputs.server, self.inputs.user, self.inputs.pwd, cache_dir) if self._infields: for key in self._infields: value = getattr(self.inputs, key) if not isdefined(value): msg = ("%s requires a value for input '%s' " "because it was listed in 'infields'" % (self.__class__.__name__, key)) raise ValueError(msg) outputs = {} for key, args in self.inputs.query_template_args.items(): outputs[key] = [] template = self.inputs.query_template if hasattr(self.inputs, 'field_template') and \ isdefined(self.inputs.field_template) and \ key in self.inputs.field_template: template = self.inputs.field_template[key] if not args: file_objects = xnat.select(template).get('obj') if file_objects == []: raise IOError('Template %s returned no files' % template) outputs[key] = list_to_filename([ str(file_object.get()) for file_object in file_objects if file_object.exists() ]) for argnum, arglist in enumerate(args): maxlen = 1 for arg in arglist: if isinstance(arg, str) and hasattr(self.inputs, arg): arg = getattr(self.inputs, arg) if isinstance(arg, list): if (maxlen > 1) and (len(arg) != maxlen): raise ValueError('incompatible number ' 'of arguments for %s' % key) if len(arg) > maxlen: maxlen = len(arg) outfiles = [] for i in range(maxlen): argtuple = [] for arg in arglist: if isinstance(arg, str) and \ hasattr(self.inputs, arg): arg = getattr(self.inputs, arg) if isinstance(arg, list): argtuple.append(arg[i]) else: argtuple.append(arg) if argtuple: target = template % tuple(argtuple) file_objects = xnat.select(target).get('obj') if file_objects == []: raise IOError('Template %s ' 'returned no files' % target) outfiles = list_to_filename([ str(file_object.get()) for file_object in file_objects if file_object.exists() ]) else: file_objects = xnat.select(template).get('obj') if file_objects == []: raise IOError('Template %s ' 'returned no files' % template) outfiles = list_to_filename([ str(file_object.get()) for file_object in file_objects if file_object.exists() ]) outputs[key].insert(i, outfiles) if len(outputs[key]) == 0: outputs[key] = None elif len(outputs[key]) == 1: outputs[key] = outputs[key][0] return outputs
xlslist_re = xlslist.rename(columns={'gender':'G-code', 'Unnamed: 4':'Gender', 'his':'HIS', 'dicom_pidn':'PIDN', 'birthday': 'Birthday'}) xlslist2 = pd.read_csv('/home/anpo/Desktop/pyscript/ai-missing-20210524.csv') cnt = 0 for i in xlslist2['Birthday']: xlslist2.at[cnt,'Birthday'] = i.replace('-','/') cnt+=1 xlslist_re.at[17,'Birthday'] = xlslist_re.at[17,'Birthday'].replace('-','/') pd.concat((xlslist_re,xlslist2)) xlist_all = pd.concat((xlslist_re,xlslist2)) project_name = 'BRS-KHS-1' # project的名稱 session = xnat.connect('http://10.30.223.96:8080', user='', password='') # 輸入你的帳號密碼 pyxnatobj = pyxnat.Interface('http://10.30.223.96:8080','','') # 這個for meta data upload root_path = '/media/anpo/Transcend/DICOM_from_KHS_2021' # data 放的位置 datalist = next(os.walk(root_path))[1] complete = [] fails = [] meta = {} for patient_ID in datalist: print(patient_ID) patient_ID_ = patient_ID.replace(' ', '') image_No = patient_ID_.split('-')[1] pidn = xlist_all['PIDN'][xlist_all['HIS'] == int(image_No)].values[0] print(pidn) gender = xlist_all['Gender'][xlist_all['HIS'] == int(image_No)].values info = {}
def __enter__(self): self.connection = xnat.Interface(server=self.server, user=self.user, password=self.password) return self.connection
import os.path as op import dashboards import pyxnat from dashboards import pickle as pk import pickle import logging as log import tempfile fh, fp = tempfile.mkstemp(suffix='.pickle') log.info('Creating %s...' % fp) config.PICKLE_PATH = fp fp = op.join(op.dirname(dashboards.__file__), '..', 'config.json') config.DASHBOARD_CONFIG_PATH = fp fp = op.join(op.dirname(dashboards.__file__), '..', '.xnat.cfg') x = pyxnat.Interface(config=fp) def test_001_pickle_save(): # should be run first pk.save(x, config.PICKLE_PATH) with open(config.PICKLE_PATH, 'rb') as handle: data = pickle.load(handle) assert isinstance(data, dict) assert data['server'] == "https://devxnat.barcelonabeta.org" assert data['verify'] == 1 assert isinstance(data, dict) assert isinstance(data['resources'], list) assert isinstance(data['longitudinal_data'], dict)