Example #1
0
def test_request_token():

    response = requests.post(uri('auth', '/tokens'), headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    assert response.json()['token']
    response = requests.post(uri('auth', '/tokens'), headers=login_header('x', 'xxx'))
    assert response.status_code == 403
Example #2
0
def test_check_token():

    response = requests.post(uri('auth', '/tokens'), headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token1 = response.json()['token']
    response = requests.post(uri('auth', '/tokens'), headers=login_header('quentin', 'secret'))
    assert response.status_code == 201
    token2 = response.json()['token']
    assert token1 != token2
    response = requests.post(uri('auth', '/token-checks'), json={'token': token2}, headers=token_header(token1))
    assert response.status_code == 201
    response = requests.post(uri('auth', '/token-checks'), headers=token_header('1234'))
    assert response.status_code == 403
Example #3
0
def test_create_update_and_delete_user():

    response = requests.post(uri('auth', '/tokens'), headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token = response.json()['token']
    data = {
        'username': generate_string(),
        'password': '******',
        'email': '{}@yoda.com'.format(generate_string()),
    }

    # Create user
    response = requests.post(uri('auth', '/users'), json=data, headers=token_header(token))
    assert response.status_code == 201
    user_id = response.json()['id']

    # Update user and check update was successful
    data['first_name'] = 'John'
    response = requests.put(uri('auth', '/users/{}'.format(user_id)), json=data, headers=token_header(token))
    assert response.status_code == 200
    response = requests.get(uri('auth', '/users/{}'.format(user_id)), headers=token_header(token))
    assert response.status_code == 200
    assert response.json()['first_name'] == 'John'

    # Delete user and check it no longer exists
    response = requests.delete(uri('auth', '/users/{}'.format(user_id)), headers=token_header(token))
    assert response.status_code == 204
    response = requests.get(uri('auth', '/users/{}'.format(user_id)), headers=token_header(token))
    assert response.status_code == 404
Example #4
0
def test_users():

    response = requests.post(uri('auth', '/tokens'), headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token = response.json()['token']
    response = requests.get(uri('auth', '/users'), headers=token_header(token))
    assert response.status_code == 200
    assert len(response.json()) >= 4
Example #5
0
def test_upload_and_download():

    if os.getenv('DATA_DIR', None) is None:
        return

    response = requests.post(uri('auth', '/tokens'),
                             headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token = response.json()['token']

    response = requests.get(uri('storage', '/file-types?names=txt'),
                            headers=token_header(token))
    assert response.status_code == 200
    file_type_id = response.json()[0]['id']

    response = requests.get(uri('storage', '/scan-types?name=none'),
                            headers=token_header(token))
    assert response.status_code == 200
    scan_type_id = response.json()[0]['id']

    name = 'repository-{}'.format(generate_string(8))
    response = requests.post(uri('storage', '/repositories'),
                             json={'name': name},
                             headers=token_header(token))
    assert response.status_code == 201
    repository_id = response.json()['id']

    file_name = os.path.join(os.getenv('DATA_DIR'), 'data.nii.gz')
    file_id, _ = upload_file(file_name, file_type_id, scan_type_id,
                             repository_id, token)

    response = requests.get(uri(
        'storage', '/repositories/{}/files/{}'.format(repository_id, file_id)),
                            headers=token_header(token))
    assert response.status_code == 200
    storage_id = response.json()['storage_id']

    response = requests.get(uri('storage', '/downloads/{}'.format(storage_id)),
                            headers=token_header(token))
    assert response.status_code == 200
    assert response.content

    with open('tmp.nii.gz', 'wb') as f:
        for chunk in response.iter_content(1024 * 1024):
            f.write(chunk)

    n = os.path.getsize('tmp.nii.gz')
    m = os.path.getsize(file_name)
    assert n == m

    os.system('rm -f tmp.nii.gz')
Example #6
0
def test_create_update_and_delete_group():

    response = requests.post(uri('auth', '/tokens'), headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token = response.json()['token']
    data = {'name': generate_string()}

    # Create group
    response = requests.post(uri('auth', '/user-groups'), json=data, headers=token_header(token))
    assert response.status_code == 201
    user_group_id = response.json()['id']

    # Update group and check update successful
    data['name'] = generate_string()
    response = requests.put(uri('auth', '/user-groups/{}'.format(user_group_id)), json=data, headers=token_header(token))
    assert response.status_code == 200
    response = requests.get(uri('auth', '/user-groups/{}'.format(user_group_id)), headers=token_header(token))
    assert response.status_code == 200
    assert response.json()['name'] == data['name']

    # Add user to group and check it was added
    response = requests.get(uri('auth', '/users?username=ralph'), headers=token_header(token))
    assert response.status_code == 200
    user_id = response.json()[0]['id']
    response = requests.put(uri('auth', '/user-groups/{}/users/{}'.format(user_group_id, user_id)), headers=token_header(token))
    assert response.status_code == 200
    response = requests.get(uri('auth', '/user-groups/{}/users'.format(user_group_id)), headers=token_header(token))
    assert response.status_code == 200
    assert len(response.json()) == 1
    assert response.json()[0]['username'] == 'ralph'

    # Remove user from group and check it was successfully removed
    response = requests.delete(uri('auth', '/user-groups/{}/users/{}'.format(user_group_id, user_id)), headers=token_header(token))
    assert response.status_code == 200
    response = requests.get(uri('auth', '/user-groups/{}/users'.format(user_group_id)), headers=token_header(token))
    assert response.status_code == 200
    assert len(response.json()) == 0

    # Delete group and check it no longer exists
    response = requests.delete(uri('auth', '/user-groups/{}'.format(user_group_id)), headers=token_header(token))
    assert response.status_code == 204
    response = requests.get(uri('auth', '/user-groups/{}'.format(user_group_id)), headers=token_header(token))
    assert response.status_code == 404
Example #7
0
File: util.py Project: lamboy/yoda
def get_access_token():
    username, password = get_worker_username_and_password()
    response = requests.post(uri('auth', '/tokens'), headers=login_header(username, password))
    return response.json()['token']
Example #8
0
def test_train_classifier():

    if os.getenv('DATA_DIR', None) is None:
        return

    # Get access token
    response = requests.post(uri('auth', '/tokens'),
                             headers=login_header('ralph', 'secret'))
    assert response.status_code == 201
    token = response.json()['token']

    # Create storage repository
    name = 'repository-{}'.format(generate_string(8))
    response = requests.post(uri('storage', '/repositories'),
                             headers=token_header(token),
                             json={'name': name})
    assert response.status_code == 201
    repository_id = response.json()['id']

    # Get CSV file type ID
    response = requests.get(uri('storage', '/file-types?name=csv'),
                            headers=token_header(token))
    assert response.status_code == 200
    file_type_id = response.json()[0]['id']

    # Get scan type ID
    response = requests.get(uri('storage', '/scan-types?name=none'),
                            headers=token_header(token))
    assert response.status_code == 200
    scan_type_id = response.json()[0]['id']

    # Load features, extract HC and SZ subjects, remove categorical columns and
    # save the feature file back to disk
    file_path = os.path.join(os.getenv('DATA_DIR'), 'data.csv')
    features = pd.read_csv(file_path, index_col='MRid')
    subject_labels = list(features['Diagnosis'])

    # Upload CSV file with brain features
    file_id, _ = upload_file(file_path, file_type_id, scan_type_id,
                             repository_id, token)
    assert file_id

    # Train classifier using the uploaded CSV file. As parameters we specify the
    # pipeline ID (which in this case is a classifier training pipeline). The 'file_id'
    # refers to the CSV file. The parameter 'subject_labels' contains a list of diagnostic
    # labels. This list is used to pre-calculate training and testing indices which can be
    # passed to the different workers handling the cross-validation folds in parallel.
    response = requests.post(uri('compute', '/tasks'),
                             headers=token_header(token),
                             json={
                                 'pipeline_name': 'svm_train',
                                 'params': {
                                     'repository_id': repository_id,
                                     'file_id': file_id,
                                     'subject_labels': subject_labels,
                                     'nr_folds': 2,
                                     'index_column': 'MRid',
                                     'target_column': 'Diagnosis',
                                     'kernel': 'rbf',
                                 }
                             })

    assert response.status_code == 201
    task_id = response.json()['id']

    # Retrieve task status periodically until it finishes successfully. In practice,
    # this means the task status == SUCCESS and result != None
    classifier_id = 0
    while True:
        response = requests.get(uri('compute', '/tasks/{}'.format(task_id)),
                                headers=token_header(token))
        assert response.status_code == 200
        status = response.json()['status']
        assert status == 'PENDING' or status == 'SUCCESS'
        result = response.json()['result']
        sys.stdout.write('.')
        sys.stdout.flush()
        if status == 'SUCCESS' and result is not None:
            classifier_id = result['classifier_id']
            break
        time.sleep(2)

    # Remove diagnosis column from the feature data. Then select the first subject
    # so we can send it to the classifier for prediction.
    features.drop('Diagnosis', axis=1, inplace=True)
    subject_data = list(features.iloc[0])
    subject_label = subject_labels[0]

    # Send some data to the trained classifier for prediction
    response = requests.post(uri('compute', '/tasks'),
                             headers=token_header(token),
                             json={
                                 'pipeline_name': 'svm_predict',
                                 'params': {
                                     'classifier_id': classifier_id,
                                     'subjects': [
                                         subject_data,
                                     ],
                                 }
                             })

    assert response.status_code == 201
    task_id = response.json()['id']

    while True:
        response = requests.get(uri('compute', '/tasks/{}'.format(task_id)),
                                headers=token_header(token))
        assert response.status_code == 200
        status = response.json()['status']
        assert status == 'PENDING' or status == 'SUCCESS'
        result = response.json()['result']
        sys.stdout.write('.')
        sys.stdout.flush()
        if status == 'SUCCESS' and result is not None:
            assert subject_label == result['predicted_labels'][0]
            break
        time.sleep(2)