Exemplo n.º 1
0
def write2nifti(file_name, voxel_coord, labels):
    """Write label info into nifti files.

    """
    labels = labels + 1
    
    data = np.zeros((91, 109, 91), dtype=np.uint8)
    for i in range(len(voxel_coord)):
        data[voxel_coord[i][0],
             voxel_coord[i][1],
             voxel_coord[i][2]] = labels[i]

    fsl_dir = os.getenv('FSL_DIR')
    std_brain = os.path.join(fsl_dir, 'data', 'standard',
                             'MNI152_T1_2mm_brain.nii.gz')
    header = nib.load(std_brain).get_header()
    header['datatype'] = 4
    mybase.save2nifti(data, header, file_name)
Exemplo n.º 2
0
def make_mask(label_file_list, class_label, output_dir):
    """
    Create a label mask derived from a group of label files.

    """
    print 'Create a whole-fROI mask ...'
    num = len(label_file_list)
    for i in range(num):
        label_data = nib.load(label_file_list[i]).get_data()
        if not i:
            mask_data = np.zeros(label_data.shape)
            header = nib.load(label_file_list[i]).get_header()
        uniq_val = np.unique(label_data)
        for val in uniq_val:
            if not val in class_label:
                label_data[label_data == val] = 0
        label_data[label_data > 0] = 1
        mask_data += label_data
    mask_data[mask_data > 0] = 1
    # save to file
    mask_file = os.path.join(output_dir, 'mask.nii.gz')
    ntbase.save2nifti(mask_data, header, mask_file)
    # return data
    return mask_data
Exemplo n.º 3
0
def make_mask(label_file_list, class_label, output_dir):
    """
    Create a label mask derived from a group of label files.

    """
    print 'Create a whole-fROI mask ...'
    num = len(label_file_list)
    for i in range(num):
        label_data = nib.load(label_file_list[i]).get_data()
        if not i:
            mask_data = np.zeros(label_data.shape)
            header = nib.load(label_file_list[i]).get_header()
        uniq_val = np.unique(label_data)
        for val in uniq_val:
            if not val in class_label:
                label_data[label_data==val] = 0
        label_data[label_data>0] = 1
        mask_data += label_data
    mask_data[mask_data>0] = 1
    # save to file
    mask_file = os.path.join(output_dir, 'mask.nii.gz')
    ntbase.save2nifti(mask_data, header, mask_file)
    # return data
    return mask_data
Exemplo n.º 4
0
        # compute Dice
        for label_idx in cls_list[1:]:
            P = pred_label == label_idx
            T = test_label == label_idx
            dice_val = mymath.dice_coef(T, P)
            print 'Dice for label %s: %f'%(label_idx, dice_val)
            if label_idx == 3:
                temp_ffa_dice.append(dice_val)
            else:
                temp_ofa_dice.append(dice_val)
        
        # save to nifti file
        output_dir = os.path.join(base_dir, 'ma_202', 'gss_pred_file', 'r_fc')
        file_name = os.path.join(output_dir, sessid[i]+'_'+str(num)+'.nii.gz')
        mybase.save2nifti(pred_label, header, file_name)

    ffa_dice.append(temp_ffa_dice)
    ofa_dice.append(temp_ofa_dice)

out_file = 'r_ffa_output.txt'
f = open(out_file, 'w')
str_line = [str(item) for item in selected_num]
str_line = ','.join(str_line)
f.write(str_line + '\n')
for line in ffa_dice:
    tmp_line = [str(item) for item in line]
    tmp_line = ','.join(tmp_line)
    f.write(tmp_line + '\n')

out_file = 'r_ofa_output.txt'
Exemplo n.º 5
0
def get_posterior_map(sid_list,
                      data_dir,
                      class_label,
                      forest_list,
                      classes_list,
                      spatial_ptn,
                      save_nifti=True,
                      probabilistic=True):
    """
    Get posterior map of each AF.

    """
    mask_data = np.array(arlib.load_mask_coord(data_dir))
    for i in range(len(sid_list)):
        print 'AF from subject %s' % (sid_list[i])
        # label the voxels within the mask using atlas forests (AFs)
        clf = forest_list[i]
        prob = clf.predict_proba(mask_data)
        std_prob = np.zeros((prob.shape[0], len(class_label) + 1))
        # TODO: construct a std prob table
        for cls_idx in range(prob.shape[1]):
            if classes_list[i][cls_idx] == 0:
                std_prob[..., 0] = prob[..., cls_idx]
            else:
                tmp_idx = class_label.index(classes_list[i][cls_idx])
                std_prob[..., tmp_idx + 1] = prob[..., cls_idx]
        # generate final labeling
        new_prob = np.zeros(std_prob.shape)
        new_prob[range(new_prob.shape[0]), np.argmax(std_prob, axis=1)] = 1
        tmp_pred_y = np.argmax(new_prob, axis=1)
        pred_y = np.zeros(tmp_pred_y.shape)
        for k in range(1, new_prob.shape[1]):
            pred_y[tmp_pred_y == k] = class_label[k - 1]

        # save posterior map as a nifti file
        if save_nifti:
            # predicted nifti directory
            pred_dir = os.path.join(data_dir, 'posterior_maps')
            if not os.path.exists(pred_dir):
                os.system('mkdir ' + pred_dir)
            fsl_dir = os.getenv('FSL_DIR')
            img = nib.load(
                os.path.join(fsl_dir, 'data', 'standard',
                             'MNI152_T1_2mm_brain.nii.gz'))
            # save predicted label
            header = img.get_header()
            coords = mask_data
            #pred_data = arlib.write2array(coords, pred_y)
            #pred_data = np.around(pred_data)
            if probabilistic:
                pred_data = np.zeros((91, 109, 91, len(class_label) + 1))
                for j in range(len(class_label) + 1):
                    pred_data[..., j] = np.copy(
                        arlib.write2array(coords, std_prob[..., j]))
                out_file = os.path.join(pred_dir,
                                        sid_list[i] + '_posterior.nii.gz')
            else:
                pred_data = np.copy(arlib.write2array(coords, pred_y))
                out_file = os.path.join(pred_dir,
                                        sid_list[i] + '_label.nii.gz')
            mybase.save2nifti(pred_data, header, out_file)

    if not probabilistic:
        merged_file = os.path.join(pred_dir, 'merged_label.nii.gz')
        str_cmd = ['fslmerge', '-a', merged_file]
        for subj in sid_list:
            temp = os.path.join(pred_dir, subj + '_label.nii.gz')
            str_cmd.append(temp)
        os.system(' '.join(str_cmd))
Exemplo n.º 6
0
def predict(x_mtx,
            atlas_num,
            out_dir,
            out_name,
            class_label,
            forest_list,
            classes_list,
            spatial_ptn,
            save_nifti=True,
            sorted=True,
            single_atlas=False):
    """
    Predict ROI label for unseen query image.

    """
    # mask out voxels which are not activated significantly
    x_mtx = np.array(x_mtx)
    z_vtr = x_mtx[..., 0].copy()
    z_vtr[z_vtr < 2.3] = 0
    z_vtr[z_vtr > 0] = 1
    smp_mask = z_vtr > 0
    #test_x = x_mtx[smp_mask, 0:4]
    test_x = x_mtx[smp_mask, 1:4]

    # define similarity index
    similarity = []
    for i in range(len(forest_list)):
        #r = normalized_mutual_info_score(spatial_ptn[..., i], z_vtr)
        z_vtr = x_mtx[..., 0].copy()
        z_vtr[z_vtr < 0] = 0
        r = np.corrcoef(spatial_ptn[..., i], z_vtr)[0, 1]
        similarity.append(r)

    # sort the similarity
    sorted_sim_idx = np.argsort(similarity)[::-1]

    # label the activation voxels with atlas forests (AFs)
    for num in atlas_num:
        #print 'atlas number %s'%(num)
        if sorted:
            if not single_atlas:
                selected_atlas = sorted_sim_idx[0:num]
            else:
                selected_atlas = sorted_sim_idx[(num - 1):num]
        else:
            selected_atlas = np.random.choice(len(sorted_sim_idx),
                                              num,
                                              replace=False)
        pred_prob = None
        for idx in selected_atlas:
            clf = forest_list[idx]
            prob = clf.predict_proba(test_x)
            std_prob = np.zeros((prob.shape[0], len(class_label) + 1))
            # TODO: construct a std prob table
            for cls_idx in range(prob.shape[1]):
                if classes_list[idx][cls_idx] == 0:
                    std_prob[..., 0] = prob[..., cls_idx]
                else:
                    tmp_idx = class_label.index(classes_list[idx][cls_idx])
                    std_prob[..., tmp_idx + 1] = prob[..., cls_idx]
            new_prob = np.zeros(std_prob.shape)
            new_prob[range(new_prob.shape[0]), np.argmax(std_prob, axis=1)] = 1
            # optional: modulate prediction probability with similarity
            #new_prob = new_prob * similarity[clf_idx]
            if not isinstance(pred_prob, np.ndarray):
                pred_prob = new_prob
            else:
                pred_prob += new_prob
        #print pred_prob.shape
        tmp_pred_y = np.argmax(pred_prob, axis=1)
        pred_y = np.zeros(tmp_pred_y.shape)
        for k in range(1, pred_prob.shape[1]):
            pred_y[tmp_pred_y == k] = class_label[k - 1]

        # save predicted label as a nifti file
        if save_nifti:
            # predicted nifti directory
            if not os.path.exists(out_dir):
                os.system('mkdir ' + out_dir)
            fsl_dir = os.getenv('FSL_DIR')
            img = nib.load(
                os.path.join(fsl_dir, 'data', 'standard',
                             'MNI152_T1_2mm_brain.nii.gz'))
            # save predicted label
            header = img.get_header()
            #coords = test_x[..., 1:4]
            coords = test_x
            pred_data = arlib.write2array(coords, pred_y)
            pred_data = np.around(pred_data)
            out_file = os.path.join(out_dir, out_name)
            mybase.save2nifti(np.around(pred_data), header, out_file)
Exemplo n.º 7
0
def leave_one_out_test(sid_list,
                       atlas_num,
                       data_dir,
                       class_label,
                       forest_list,
                       classes_list,
                       spatial_ptn,
                       save_nifti=False,
                       sorted=True,
                       single_atlas=False):
    """
    Evaluate classifier performance with leave-one-out scheme.

    """
    # initial output dice value
    dice = {}
    for idx in class_label:
        dice[idx] = []

    for i in range(len(sid_list)):
        print 'Test subject %s' % (sid_list[i])
        test_data = get_subj_sample(sid_list[i], data_dir)
        # mask out voxels which are not activated significantly
        smp_mask = test_data[..., 0] >= 2.3
        #test_x = test_data[smp_mask, 0:4]
        test_x = test_data[smp_mask, 1:4]
        test_y = test_data[smp_mask, -1]

        # define similarity index
        similarity = []
        atlas_idx = []
        for j in range(len(sid_list)):
            if i == j:
                continue
            atlas_idx.append(j)
            #r = normalized_mutual_info_score(spatial_ptn[..., i],
            #                                 spatial_ptn[..., j])
            #r = mymath.mutual_information(spatial_ptn[..., i],
            #                              spatial_ptn[..., j])
            r = np.corrcoef(spatial_ptn[..., i], spatial_ptn[..., j])[0, 1]
            similarity.append(r)

        # sort the similarity
        sorted_sim_idx = np.argsort(similarity)[::-1]
        #print similarity
        #print sorted_sim_idx

        # label the activation voxels with atlas forests (AFs)
        tmp_dice = {}
        for idx in class_label:
            tmp_dice[idx] = []

        for num in atlas_num:
            print 'atlas number %s' % (num)
            if sorted:
                if not single_atlas:
                    selected_atlas = sorted_sim_idx[0:num]
                else:
                    selected_atlas = sorted_sim_idx[(num - 1):num]
            else:
                selected_atlas = np.random.choice(len(sorted_sim_idx),
                                                  num,
                                                  replace=False)
            pred_prob = None
            for idx in selected_atlas:
                clf = forest_list[atlas_idx[idx]]
                prob = clf.predict_proba(test_x)
                std_prob = np.zeros((prob.shape[0], len(class_label) + 1))
                # TODO: construct a std prob table
                for cls_idx in range(prob.shape[1]):
                    if classes_list[atlas_idx[idx]][cls_idx] == 0:
                        std_prob[..., 0] = prob[..., cls_idx]
                    else:
                        tmp_idx = class_label.index(
                            classes_list[atlas_idx[idx]][cls_idx])
                        std_prob[..., tmp_idx + 1] = prob[..., cls_idx]
                new_prob = np.zeros(std_prob.shape)
                new_prob[range(new_prob.shape[0]),
                         np.argmax(std_prob, axis=1)] = 1
                # optional: modulate prediction probability with similarity
                #new_prob = new_prob * similarity[clf_idx]
                if not isinstance(pred_prob, np.ndarray):
                    pred_prob = new_prob
                else:
                    pred_prob += new_prob
            #print pred_prob.shape
            tmp_pred_y = np.argmax(pred_prob, axis=1)
            pred_y = np.zeros(tmp_pred_y.shape)
            for k in range(1, pred_prob.shape[1]):
                pred_y[tmp_pred_y == k] = class_label[k - 1]

            for label_idx in class_label:
                P = pred_y == label_idx
                T = test_y == label_idx
                dice_val = mymath.dice_coef(T, P)
                print 'Dice for label %s: %f' % (label_idx, dice_val)
                tmp_dice[label_idx].append(dice_val)

            # save predicted label as a nifti file
            if save_nifti:
                # predicted nifti directory
                pred_dir = os.path.join(data_dir, str(num) + '_atlas_pred')
                if not os.path.exists(pred_dir):
                    os.system('mkdir ' + pred_dir)
                fsl_dir = os.getenv('FSL_DIR')
                img = nib.load(
                    os.path.join(fsl_dir, 'data', 'standard',
                                 'MNI152_T1_2mm_brain.nii.gz'))
                # save predicted label
                header = img.get_header()
                #coords = test_x[..., 1:4]
                coords = test_x
                pred_data = arlib.write2array(coords, pred_y)
                pred_data = np.around(pred_data)
                out_file = os.path.join(pred_dir, sid_list[i] + '_pred.nii.gz')
                mybase.save2nifti(pred_data, header, out_file)

        for idx in class_label:
            dice[idx].append(tmp_dice[idx])

    for idx in class_label:
        print 'Mean Dice for label %s: %s' % (idx, np.mean(dice[idx],
                                                           axis=0)[0])
    return dice