Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(description='Radiomics results')
    parser.add_argument('-feat', '--feat', metavar='feat',
                        nargs='+', dest='feat', type=str, required=True,
                        help='List of patient feature files (HDF)')
    parser.add_argument('-class', '--class', metavar='class',
                        nargs='+', dest='classs', type=str, required=True,
                        help='Classification of patients (text)')
    parser.add_argument('-out', '--out', metavar='out',
                        nargs='+', dest='out', type=str, required=True,
                        help='Output png file')
    args = parser.parse_args()

    if type(args.classs) is list:
        args.classs = ''.join(args.classs)

    if type(args.out) is list:
        args.out = ''.join(args.out)

    if type(args.feat) is list and len(args.feat) == 1:
        args.feat = ''.join(args.feat)

    if os.path.isdir(args.feat):
        args.feat = glob.glob(args.feat + '/features_*.hdf5')
        args.feat = natsorted(args.feat)

    # Read and stack the features
    print("Reading features.")
    image_features_temp = list()
    for i_feat in range(len(args.feat)):
        feat = dict()
        print args.feat[i_feat]
        feat_temp = pd.read_hdf(args.feat[i_feat])
        feat_temp = feat_temp.image_features

        for feattype in feat_temp.keys():
            feat_type = feat_temp[feattype]
            for subtype in feat_type.keys():
                subfeat = feat_type[subtype]
                for k in subfeat.keys():
                    feat[k] = subfeat[k]

        image_features_temp.append(feat)

    # Get the mutation labels and patient IDs
    print("Reading class labels.")
    mutation_type = [['WT']]
    mutation_data, image_features = gp.findmutationdata(args.classs,
                                                        mutation_type,
                                                        args.feat,
                                                        image_features_temp)

    labels = image_features[0].keys()
    featvect = dict()
    flab = dict()
    for l in labels:
        featvect[l] = {"all": [], "1": [], "0": []}
        flab[l] = {"all": [], "1": [], "0": []}

    # Stack per feature type and class
    print("Stacking features.")
    mutation_label = mutation_data['mutation_label'].tolist()[0]
    patient_IDs = mutation_data['patient_IDs'].tolist()
    for imfeat, label, pid in zip(image_features, mutation_label, patient_IDs):
        for fl in labels:
            featvect[fl]['all'].append(imfeat[fl])
            flab[fl]['all'].append(pid)
            if label[0] == 0:
                featvect[fl]['0'].append(imfeat[fl])
                flab[fl]['0'].append(pid)
            else:
                featvect[fl]['1'].append(imfeat[fl])
                flab[fl]['1'].append(pid)

    # Create the boxplots
    print("Generating boxplots.")

    # Split in 5x5 figures.
    nfig = np.ceil(len(labels) / 25.0)

    labels = sorted(labels)
    for fi in range(0, int(nfig)):
        f = plt.figure()
        fignum = 1
        for i in range(fi*25, min((fi+1)*25, len(labels))):
            ax = plt.subplot(5, 5, fignum)
            lab = labels[i]
            plt.subplots_adjust(hspace=0.3, wspace=0.2)
            ax.scatter(np.ones(len(featvect[lab]['all'])),
                       featvect[lab]['all'],
                       color='blue')
            ax.scatter(np.ones(len(featvect[lab]['1']))*2.0,
                       featvect[lab]['1'],
                       color='red')
            ax.scatter(np.ones(len(featvect[lab]['0']))*3.0,
                       featvect[lab]['0'],
                       color='green')

            plt.boxplot([featvect[lab]['all'], featvect[lab]['1'], featvect[lab]['0']])

            fz = 5  # Works best after saving
            ax.set_title(lab, fontsize=fz)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(fz)

            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(fz)

            fignum += 1

        # Maximize figure to get correct spacings
        mng = plt.get_current_fig_manager()
        mng.resize(*mng.window.maxsize())
        # plt.show()

        # High DTI to  make sure we save the maximized image
        f.savefig(args.out + ('_{}.png').format(str(fi)), dpi=600)
        print(("Boxplot saved as {} !").format(args.out + ('_{}.png').format(str(fi))))
def main():
    parser = argparse.ArgumentParser(description='Radiomics results')
    parser.add_argument('-feat',
                        '--feat',
                        metavar='feat',
                        nargs='+',
                        dest='feat',
                        type=str,
                        required=True,
                        help='List of patient feature files (HDF)')
    parser.add_argument('-class',
                        '--class',
                        metavar='class',
                        nargs='+',
                        dest='classs',
                        type=str,
                        required=True,
                        help='Classification of patients (text)')
    parser.add_argument('-lab',
                        '--lab',
                        metavar='lab',
                        nargs='+',
                        dest='lab',
                        type=str,
                        required=True,
                        help='Label of two features to plot')
    parser.add_argument('-out',
                        '--out',
                        metavar='out',
                        nargs='+',
                        dest='out',
                        type=str,
                        required=True,
                        help='Output png file')
    args = parser.parse_args()

    if type(args.classs) is list:
        args.classs = ''.join(args.classs)

    if type(args.out) is list:
        args.out = ''.join(args.out)

    if type(args.feat) is list and len(args.feat) == 1:
        args.feat = ''.join(args.feat)

    if os.path.isdir(args.feat):
        args.feat = glob.glob(args.feat + '/features_*.hdf5')
        args.feat = natsorted(args.feat)

    # Read and stack the features
    image_features_temp = list()
    for i_feat in range(len(args.feat)):
        feat = dict()
        feat_temp = pd.read_hdf(args.feat[i_feat])
        feat_temp = feat_temp.image_features

        for feattype in feat_temp.keys():
            feat_type = feat_temp[feattype]
            for subtype in feat_type.keys():
                subfeat = feat_type[subtype]
                for k in subfeat.keys():
                    feat[k] = subfeat[k]

        image_features_temp.append(feat)

    # Get the mutation labels and patient IDs
    mutation_type = [['GP']]
    mutation_data, image_features = gp.findmutationdata(
        args.classs, mutation_type, args.feat, image_features_temp)

    image_features = image_features.tolist()
    # Select the two relevant features
    feat1_c0 = list()
    feat2_c0 = list()
    feat1_c1 = list()
    feat2_c1 = list()
    mutation_label = mutation_data['mutation_label'].tolist()[0]
    patient_IDs = mutation_data['patient_IDs'].tolist()

    for imfeat, label, pid in zip(image_features, mutation_label, patient_IDs):
        print imfeat[args.lab[0]], pid
        if label[0] == 0:
            feat1_c0.append(imfeat[args.lab[0]])
            feat2_c0.append(imfeat[args.lab[1]])
        else:
            feat1_c1.append(imfeat[args.lab[0]])
            feat2_c1.append(imfeat[args.lab[1]])

    # Make a scatter plot
    f = plt.figure()
    subplot = f.add_subplot(111)
    subplot.plot(feat1_c0,
                 feat2_c0,
                 linestyle='',
                 ms=12,
                 marker='o',
                 color='navy')
    subplot.plot(feat1_c1,
                 feat2_c1,
                 linestyle='',
                 ms=12,
                 marker='x',
                 color='red')
    # NOTE: arbitrary limits!
    plt.xlim([0, 10])
    plt.ylim([0, 10])
    plt.xlabel(args.lab[0])
    plt.ylabel(args.lab[1])
    plt.title('Feature scatter plot')
    plt.legend()
    plt.show()

    f.savefig(args.out)
    print(("Snapshot saved as {} !").format(args.out))