Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: VUIIS/dax
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: DecisionNeurosciencePsychopathology/dax
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 6 commits
  • 2 files changed
  • 3 contributors

Commits on Jul 22, 2021

  1. Xnatdownload accept password from XNAT_PASS

    tsb31 committed Jul 22, 2021
    Copy the full SHA
    bf24c76 View commit details

Commits on Oct 7, 2021

  1. Copy the full SHA
    ed336c8 View commit details
  2. Copy the full SHA
    5fbacc2 View commit details
  3. Copy the full SHA
    c807de3 View commit details

Commits on Nov 5, 2021

  1. Copy the full SHA
    f5c8991 View commit details
  2. Copy the full SHA
    4ae68d0 View commit details
Showing with 75 additions and 35 deletions.
  1. +22 −32 bin/Xnat_tools/Xnatdownload
  2. +53 −3 dax/XnatUtils.py
54 changes: 22 additions & 32 deletions bin/Xnat_tools/Xnatdownload
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/ihome/crc/install/python/miniconda3-3.7/bin/python
# -*- coding: utf-8 -*-

'''
@@ -641,13 +641,6 @@ def download_data_xnat():
assessor_dict['qcstatus']])
else:
row = None

if OPTIONS.rename:
assessor_obj = XNAT.select_assessor(assessor_dict['project_label'], \
assessor_dict['subject_label'], \
assessor_dict['session_label'], \
assessor_dict['assessor_label'])
proc_path = XnatUtils.rename_directory_with_assessor_inputs(assessor_obj,proc_path)
download_assessor(proc_path, assessor_dict, asses_res_list, row,
LAST_D)

@@ -741,17 +734,17 @@ def download_resource(directory, res_obj, res_label, row, one_dir, label=None,
LOGGER.info(' >Resource %s: WARNING -- no resource %s '
% (res_label, res_label))
else:
if not res_obj.files().get():
LOGGER.info(' >Resource %s: ERROR -- No files in the resources.'
% (res_label))
else:
if one_dir:
one_dir_download(directory, res_obj, res_label, label,
row, last_dl_date)
# TSB: try to download the resource, skip otherwise
try:
if not res_obj.files().get():
LOGGER.info(' >Resource %s: ERROR -- No files in the resources.' % (res_label))
else:
default_download(directory, res_obj, res_label,
row, last_dl_date)

if one_dir:
one_dir_download(directory, res_obj, res_label, label, row, last_dl_date)
else:
default_download(directory, res_obj, res_label, row, last_dl_date)
except:
LOGGER.info(' >Resource %s: WARNING -- Unable to download the resource %s ' % (res_label, res_label))

def one_dir_download(directory, res_obj, res_label, label,
row, last_dl_date):
@@ -963,9 +956,6 @@ def download_specific_assessor(xnat):
if not OPTIONS.oneDir:
assessor_path = os.path.join(
DIRECTORY, assessor_dict['label'])
if OPTIONS.rename:
assessor_path = XnatUtils.rename_directory_with_assessor_inputs\
(assessor_obj,assessor_path)
out_reslist = get_resources_list(
assessor_dict, assessors_resources)
for rname in out_reslist:
@@ -1006,11 +996,6 @@ the csvfile.')
print('OPTION ERROR: You need to set the OPTIONS -p with the \
projects ID from Xnat.')
return False
# Checking for rename option-> only with assessors
if OPTIONS.rename and \
not (OPTIONS.selectionAssessor or OPTIONS.assessortype):
print('OPTIONS ERROR: You used the option --rename but have not filtered \
for assessors with --selectionP or --a')
if OPTIONS.update:
if not os.path.exists(os.path.abspath(OPTIONS.directory)):
print('OPTION ERROR: You used the option --continue but the \
@@ -1232,12 +1217,12 @@ def parse_args():
help='Host for XNAT. Default: using $XNAT_HOST.')
argp.add_argument('-u', '--username', dest='username', default=None,
help='Username for XNAT. Default: using $XNAT_USER.')
argp.add_argument('-x', '--password', dest='password', default=None,
help='Password for XNAT. Default: using $XNAT_PASS.')
argp.add_argument("-p", "--project", dest="project", default=None,
help="Project(s) ID on Xnat")
argp.add_argument("-d", "--directory", dest="directory", default=None,
help="Directory where the data will be download")
argp.add_argument("--rename",dest="rename", action="store_true" , \
help="Rename assessor directories with scan ID in directory name")
argp.add_argument("-D", "--oneDirectory", dest="oneDir",
action="store_true",
help="Data will be downloaded in the same directory. \
@@ -1354,17 +1339,22 @@ scans.')
HOST = OPTIONS.host
else:
HOST = os.environ['XNAT_HOST']
if OPTIONS.username:
if OPTIONS.username and (OPTIONS.password != None):
MSG = "Please provide the password for user <%s> on xnat(%s):"
PWD = getpass.getpass(prompt=MSG % (OPTIONS.username, HOST))
else:
PWD = None
PWD = OPTIONS.password
if PWD == None:
try:
PWD = os.environ['XNAT_PASS']
except:
pass

MSG = 'INFO: connection to xnat <%s>:' % (HOST)
LOGGER.info(MSG)
with XnatUtils.get_interface(host=OPTIONS.host,
user=OPTIONS.username,
pwd=PWD) as XNAT:
MSG = 'INFO: connection to xnat <%s>:' % (HOST)
LOGGER.info(MSG)
if OPTIONS.selectionScan or OPTIONS.selectionAssessor:
CSVWRITER = None
if OPTIONS.selectionScan:
56 changes: 53 additions & 3 deletions dax/XnatUtils.py
Original file line number Diff line number Diff line change
@@ -10,11 +10,14 @@
import traceback
import time
import logging

import pathlib
import json
from urllib.parse import urlparse
import xml.etree.cElementTree as ET
from lxml import etree
from pyxnat import Interface
from pyxnat.core.errors import DatabaseError

import requests

from . import utilities
@@ -142,7 +145,8 @@ def __init__(self, xnat_host=None, xnat_user=None, xnat_pass=None,
timeout_emails=None,
xnat_timeout=300,
xnat_retries=4,
xnat_wait=600):
xnat_wait=600,
xnat_verify=None):

"""Entry point for the InterfaceTemp class.
@@ -157,6 +161,7 @@ def __init__(self, xnat_host=None, xnat_user=None, xnat_pass=None,
self.user = xnat_user
self.pwd = xnat_pass
self.smtp_host = smtp_host
self.verify = xnat_verify
if not xnat_host:
self.host = os.environ['XNAT_HOST']
# User:
@@ -173,6 +178,50 @@ def __init__(self, xnat_host=None, xnat_user=None, xnat_pass=None,
self.xnat_wait = xnat_wait
self.timeout_emails = timeout_emails

# get the home directory
HOME = str(pathlib.Path.home())

# Verification:
if xnat_verify == None:
# look for the default verify settings (~/.dax/to_verify.json)
if os.path.isfile(HOME + '/.dax/to_verify.json'):
# try to find the verify setting for the given host
try:
# load the file
with open(HOME + '/.dax/to_verify.json', 'r') as f:
json_dict = json.load(f)
# set this to what was found for the current host
self.verify = json_dict[urlparse(self.host).hostname]
# otherwise
except:
# set verify to false
self.verify = False
# for setting based on Boolean
elif type(xnat_verify) == bool:
self.verify = xnat_verify
# otherwise, set the verify parameter for this host
else:
# if ~/.dax does not exist
if not os.path.isdir(HOME + '/.dax'):
# create the directory
os.mkdir(HOME + '/.dax')
# if the to_verify.json file does not exist
if not os.path.isfile(HOME + '/.dax/to_verify.json'):
# create the file
with open(HOME + '/.dax/to_verify.json', 'w') as f:
initial_json = dict()
json.dump(initial_json, f, indent=4)
# load the json as a python object
with open(HOME + '/.dax/to_verify.json', 'r') as f:
json_data = json.load(f)
# add/update the verification item to the json file
json_data[self.host] = xnat_verify
# save the json as a python object
with open(HOME + '/.dax/to_verify.json', 'w') as f:
json.dump(json_data, f, indent=4)
# set self.verify
self.verify = xnat_verify

self.authenticate()

def __enter__(self, xnat_host=None, xnat_user=None, xnat_pass=None):
@@ -187,7 +236,8 @@ def connect(self):
"""Connect to XNAT."""
super(InterfaceTemp, self).__init__(server=self.host,
user=self.user,
password=self.pwd)
password=self.pwd,
verify=self.verify)

def disconnect(self):
super(InterfaceTemp, self).disconnect()