예제 #1
0
def unpad_results(results, pad):
    """
    Unpads the interpretation results.

    Parameters
    ----------
    results: dict
        Dictionary containing the interpretation results from each class and 
        MRI sequence.
    pad: int
        Pad size for unpadding the interpretation results volumes.

    Returns
    -------
    Dict
        Dictionary with the unpadded interpretability results.
    """
    for c in results.keys():
        for s in results[c].keys():
            results[c][s] = unpadding(image=results[c][s], padding_size=pad)
    return results
예제 #2
0
 def decryptPK(self, data, password, sharedKey):
     ''' Decrypt an double encrypted private key'''
     data = data.decode('base64', 'strict')
     key = PBKDF2(sharedKey + password, data[:16], iterations=10).read(32)
     cipher = AES.new(key, AES.MODE_CBC, data[:16])
     return unpadding(cipher.decrypt(data[16:]))
예제 #3
0
 def decrypt(self, key, cipherdata):
     ''' Decrypt an wallet encrypted with a PBKDF2 Key with AES'''
     key = PBKDF2(key, cipherdata[:16], iterations=10).read(32)
     cipher = AES.new(key, AES.MODE_CBC, cipherdata[:16])
     return unpadding(cipher.decrypt(cipherdata[16:]))
예제 #4
0
파일: wallet.py 프로젝트: topiasv/BitPurse
 def decryptPK(self, data, password, sharedKey):
     ''' Decrypt an double encrypted private key'''
     data = data.decode('base64', 'strict')
     key = PBKDF2(sharedKey + password, data[:16], iterations=10).read(32)
     cipher = AES.new(key, AES.MODE_CBC, data[:16])
     return unpadding(cipher.decrypt(data[16:]))
예제 #5
0
파일: wallet.py 프로젝트: topiasv/BitPurse
 def decrypt(self, key, cipherdata):
     ''' Decrypt an wallet encrypted with a PBKDF2 Key with AES'''
     key = PBKDF2(key, cipherdata[:16], iterations=10).read(32)
     cipher = AES.new(key, AES.MODE_CBC, cipherdata[:16])
     return unpadding(cipher.decrypt(cipherdata[16:]))
예제 #6
0
def main():
    start = time.time()

    ###### PARAMS ######
    save_dir = 'results'
    mri_path = 'data/BRATS_subj_0310'
    rf_path = 'data/rf/RF.npy'
    test_subj_path = 'data/test_subj.hdf5'
    training_feat_path = 'data/train_BRATS.hdf5'
    selected_features_file = 'data/selected_feat.npy'
    slices = [104]  # None if we want all the segmentation
    axis = 0
    patch_size = [9, 9, 9]
    stride = [2, 2, 2]  # if we want to decimate the image, otherwise None
    # Gaussian filter to remove the high frequency transitions made by the
    # decimation
    apply_gaussian_filter = True
    class_names = ['Normal', 'Necrosis', 'Edema', 'Non-enhanced', 'Enhanced']
    sequences = ['T1', 'T1c', 'T2', 'Flair']
    lime_features = 10
    lime_neighbor_samples = 2400
    n_jobs = 8
    ####################

    create_dir(save_dir)

    print 'Loading W matrix...'
    start_subtask = time.time()

    W = read_data('data/W.npy')
    W = preprocess_W(W=W, scale=True)
    W = reshape_W(W_matrix=W, patch_size=patch_size, sequences=sequences)
    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    # Load mask
    print '\nGetting mask...'
    start_subtask = time.time()
    mask, _ = load_mri('data/BRATS_subj_0310/VSD.Mask_HG_310.17608.nii.gz')
    mask = padding(image=mask, padding_size=np.sum(patch_size))

    _, affine = \
        load_mri('data/BRATS_subj_0310/VSD.Brain.XX.O.MR_Flair.17608.nii.gz')
    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    # Segment the test subject
    print '\nSegmenting test subject...'
    start_subtask = time.time()

    rf = read_data(rf_path)

    X_test, test_coors = load_test_subject(test_features_path=test_subj_path)

    mi_selected_features = read_data(selected_features_file)
    X_test = X_test[:, mi_selected_features]

    print '\tStarting RF prediction'

    prediction = None
    if slices is not None:
        X_test, test_coors, slices_image = \
            get_slices_samples(test_features=X_test,
                               test_coordinates=test_coors, mask=mask,
                               slices=slices, axis=axis,
                               patch_size=patch_size,
                               return_slices_image=True)

    prediction = get_rf_predictions(X_test=X_test, rf=rf)

    seg = rebuild(prediction=prediction, coordinates=test_coors, mask=mask)

    save_image(volume=seg, affine=affine, save_dir=save_dir,
               save_name='prediction.nii.gz', unpad=np.sum(patch_size))

    X_test = X_test[prediction > 0, :]
    test_coors = test_coors[prediction > 0, :]

    if stride is not None:
        X_test, test_coors, stride_image = \
            get_strided_sample(test_features=X_test,
                               test_coordinates=test_coors,
                               prediction=prediction, mask=mask,
                               only_foreground=True,
                               stride=stride, return_stride_image=True)

    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    # Prepare LIME explainer
    print '\nPreparing LIME...'
    start_subtask = time.time()
    explainer = prepare_lime(training_path=training_feat_path,
                             class_names=class_names,
                             discretize_continuous=True,
                             sel_feat_file=selected_features_file)

    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    # Getting LIME selected features
    print '\nGetting explaining features from LIME...'
    start_subtask = time.time()
    if n_jobs > 1:
        print '\tExecuting parallel algorithm.'
        lime_selected_features = parallel_explain_subset(
            test_features=X_test, classifier=rf, explainer=explainer,
            save_dir=save_dir, num_features=lime_features,
            n_neighbor_samples=lime_neighbor_samples, top_labels=1,
            n_jobs=n_jobs)
    elif n_jobs == 1:
        print '\tExecuting serial algorithm.'
        lime_selected_features = serial_explain_subset(
            X_test, rf, explainer, save_dir,
            num_features=lime_features,
            n_neighbor_samples=lime_neighbor_samples,
            top_labels=1)
        print '\t', lime_selected_features.shape
    else:
        raise ValueError('Invalid number of jobs')

    if selected_features_file is not None:
        lime_selected_features = \
            convert_selected_lime_to_selected_mi(
                lime_selected_features=lime_selected_features,
                mi_selected_features=mi_selected_features)

    del X_test

    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    # Getting interpretation images
    print '\nGetting interpretation over sequences...'
    start_subtask = time.time()

    results = get_interpretation(W=W, patch_size=patch_size,
                                 coordinates=test_coors,
                                 sequences_name=sequences, mask=mask,
                                 features=lime_selected_features, 
                                 segmentation=seg,
                                 only_foreground=True)

    results = apply_mask(results, mask)
    results = unpad_results(results=results, pad=np.sum(patch_size))

    if apply_gaussian_filter:
        results = smooth_interpretation(results=results, sigma=1.0)

    normalized_results = normalize_max_all_volumes(results)

    mri_sequences = get_mri_sequences(path=mri_path, sequences=sequences)
    seg = unpadding(seg, np.sum(patch_size))

    plot_local_interp(mri_sequences=mri_sequences, save_dir=save_dir,
                      interpretation=normalized_results, 
                      prediction=seg, 
                      classes=class_names[1: len(class_names)],
                      slices=slices, axis=axis, dpi=250, exclude=[3], 
                      vmax=None)

    print '\tDone! Took', float(time.time() - start_subtask) / 60.0,
    'minutes.'

    print 'Elapsed time:', float(time.time() - start) / 60.0, 'minutes.'
    print 'Have a nice day!'