def visualize_prediction( algorithms, pred_results, M, labels, output_prefix=""):
    visualize_prediction.counter += 1

    for m in M:
        metadata = m

        for algorithm in algorithms:
            alg_name = algorithm[2]

            mean_preds = np.sum(np.array(pred_results[alg_name])-np.array(labels), axis=0)
            sd_preds = np.std(np.array(pred_results[alg_name])-np.array(labels), axis=0)

            scalars = np.abs(mean_preds * 10. / np.max(mean_preds))

            petacc_root = "/datagrid/Medical/microscopy/petacc3"
            image_name = os.path.basename(metadata[0])[:8]
            patch_size = int(os.path.basename(metadata[0]).split('_')[3][1:])
            image_path = petacc_root + "/batch_2/" + image_name + ".ndpi"
            if not os.path.exists(image_path):
                image_path = petacc_root + "/batch_1/" + image_name + ".ndpi"

            si = NDPISlideImage(image_path, None)
            patchvis = si.get_patch_visualization(5, metadata[2], patch_size,
                                                  scalars=scalars[m[3]:m[3]+len(m[2])],
                                                  line_thickness=2,
                                                  show=False )

            cv2.imwrite("/tmp/{}_l0_vis_".format(output_prefix)+image_name+"_"+alg_name+"features_{:d}.png".format(visualize_prediction.counter), patchvis)
def visualize_prediction( feature_name, pred_results, M, labels, train_len=0, output_prefix=""):

    data_counter = 0
    for m in M:
        metadata = m

        # mean_preds = np.sum(np.array(pred_results[alg_name])-np.array(labels), axis=0)
        scalars = np.abs(pred_results * 100. / np.max(pred_results))

        petacc_root = "/datagrid/Medical/microscopy/petacc3"
        image_name = os.path.basename(metadata[0])[:8]
        patch_size = 1024
        image_path = petacc_root + "/batch_2/" + image_name + ".ndpi"
        if not os.path.exists(image_path):
            image_path = petacc_root + "/batch_1/" + image_name + ".ndpi"

        si = NDPISlideImage(image_path, None)
        patchvis = si.get_patch_visualization(6, metadata[2], patch_size,
                                              scalars=scalars[m[3]:m[3]+len(m[2])],
                                              line_thickness=1,
                                              show=False, filled=True)

        # if data_counter < train_len:
        #     cv2.putText(patchvis, 'test', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 2, cv2.LINE_AA)
        # else:
        #     cv2.putText(patchvis, 'vali', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, cv2.LINE_AA)
        cv2.imwrite("{}_{}_{}".format(output_prefix, image_name, feature_name) + ".png", patchvis)

        data_counter += 1
def visualize_prediction( algorithms, pred_results, M, labels, train_len=0, output_prefix=""):
    visualize_prediction.counter += 1

    data_counter = 0
    validation_offset = 0
    imtype = ''
    for m in M:
        metadata = m

        for algorithm in algorithms:
            alg_name = algorithm[2]

            # mean_preds = np.sum(np.array(pred_results[alg_name])-np.array(labels), axis=0)
            mean_preds = np.sum(np.array(pred_results[alg_name]), axis=0)
            max_preds = np.max(np.array(pred_results[alg_name]), axis=0)
            min_preds = np.min(np.array(pred_results[alg_name]), axis=0)
            sd_preds = np.sum(np.array(pred_results[alg_name]), axis=0)
            # sd_preds = np.std(np.array(pred_results[alg_name])-np.array(labels), axis=0)

            scalars = np.abs(mean_preds * 100. / np.max(mean_preds))

            border_scalars = np.zeros_like(scalars)
            for li, label in enumerate(labels):
                if label > 0:
                    border_scalars[li] = 100 * min_preds[li]
                else:
                    border_scalars[li] = 100 * max_preds[li]

            petacc_root = "/datagrid/Medical/microscopy/petacc3"
            image_name = os.path.basename(metadata[0])[:8]
            patch_size = int(os.path.basename(metadata[0]).split('_')[3][1:])
            image_path = petacc_root + "/batch_2/" + image_name + ".ndpi"
            if not os.path.exists(image_path):
                image_path = petacc_root + "/batch_1/" + image_name + ".ndpi"

            si = NDPISlideImage(image_path, None)
            patchvis = si.get_patch_visualization(6, metadata[2], patch_size,
                                                  scalars=scalars[validation_offset+m[3]:validation_offset+m[3]+len(m[2])],
                                                  border_scalars=border_scalars[validation_offset+m[3]:validation_offset+m[3]+len(m[2])],
                                                  line_thickness=1,
                                                  show=False, filled=True)

            if data_counter < train_len:
                cv2.putText(patchvis, 'test', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 2, cv2.LINE_AA)
                imtype='test'
            else:
                cv2.putText(patchvis, 'vali', (40, 40), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 2, cv2.LINE_AA)
                imtype='vali'

            data_counter += 1

            if data_counter == train_len:
                validation_offset = m[3]+len(m[2])

            cv2.imwrite("/local/temporary/clf_vis/{}".format(output_prefix)+image_name+"_{}_".format(imtype)+alg_name+"features_{:d}.png".format(visualize_prediction.counter), patchvis)