def main(args):
    mitosis_input_dir = '../../../deep-histopath/data/mitoses/mitoses_train_image_data/'
    mitosis_reorganized_dir = '../../../deep-histopath/data/mitoses/mitoses_train_image_data_new/'
    if args.reorganize_folder_structure:
        print("1. Reorganize the data structure for Mask_RCNN")
        reorganize_mitosis_images(mitosis_input_dir, mitosis_reorganized_dir)

    inference_input_dir = 'datasets/stage1_test'
    if args.split_big_images_to_small_ones:
        print("2. Crop big images into small ones")
        mitosis_files = [
            str(f) for f in Path(mitosis_reorganized_dir).glob('**/**/*.tif')
        ]
        crop_image(mitosis_files, inference_input_dir, size=128, overlap=16)

    command = 'detect'
    dataset = 'datasets/'
    weights = 'models/mask_rcnn_nucleus_0380.h5'
    subset = 'stage1_test'
    if args.run_nucleus_detection:
        print("3. Run the nucleus segmentation inference")
        inference_result_dir = nucleus_mitosis.run(command, dataset, weights,
                                                   subset)

    inference_result_dir = '../../results/nucleus/submit_20181016T152921'
    inference_combine_result = 'datasets/stage1_combine_test/'
    if args.combine_small_inference_results:
        print("4. Combine small inference images and csvs to big ones")
        combine_images(inference_result_dir, inference_combine_result)
        combine_csvs(inference_result_dir,
                     inference_combine_result,
                     clean_output_dir=False)

    ground_truth_dir = '../../../deep-histopath/data/mitoses/mitoses_train_ground_truth'
    if args.visualize_the_ground_truth:
        print("5. Visualize the ground truth masks")
        add_groundtruth_mark(inference_combine_result,
                             ground_truth_dir,
                             hasHeader=False,
                             shape=Shape.CIRCLE)
        # add_mark('/Users/fei/Documents/Github/Mask_RCNN/samples/nucleus/datasets/stage1_combine_test/01-01.png',
        #          '/Users/fei/Documents/Github/Mask_RCNN/samples/nucleus/datasets/stage1_combine_test/01-01.csv',
        #          hasHeader=True, shape=Shape.CIRCLE, mark_color=(255,0,0,50))

    inference_dir = "datasets/stage1_combine_test/"
    ground_truth_dir = "../../../deep-histopath/data/mitoses/mitoses_train_ground_truth"
    if args.evaluate_nucleus_inference:
        print("6. Evaluate the nucleus inference result")
        check_nucleius_inference(inference_dir, ground_truth_dir)

    img_dir = '../../../deep-histopath/data/mitoses/mitoses_train_image_data/'
    location_csv_dir = 'datasets/stage1_combine_test/'
    output_patch_basedir = 'datasets/sample_patches'
    if args.extract_patch:
        print("7. Extract patches according to the inference result")
        extract_patches(img_dir, location_csv_dir, output_patch_basedir)

    input_dir_basepath = 'datasets/sample_patches/'
    output_dir_basepath = 'datasets/inference_results/'
    model_file = '../../../deep-histopath/experiments/models/deep_histopath_model.hdf5'
    if args.run_mitosis_classification:
        print("8. Run mitosis classification inference")
        batch_size = 128 * 10
        num_parallel_calls = 1
        prob_thres = 0.5,
        eps = 64
        min_samples = 1
        isWeightedAvg = False
        run_reference_in_batch(batch_size=batch_size,
                               input_dir_basepath=input_dir_basepath,
                               output_dir_basepath=output_dir_basepath,
                               model_file=model_file,
                               num_parallel_calls=num_parallel_calls,
                               prob_thres=prob_thres,
                               eps=eps,
                               min_samples=min_samples,
                               isWeightedAvg=isWeightedAvg)

    output_dir_basepath = 'datasets/val_results/'
    ground_truth_dir = "../../../deep-histopath/data/mitoses/val_ground_truth"
    if args.compute_f1:
        print("9. Compute F1 score")
        f1, precision, recall, over_detected, non_detected, FP, TP, FN = \
            evaluate_global_f1(output_dir_basepath, ground_truth_dir, threshold=30,
                               prob_threshold=None)
        print("F1: {} \n"
              "Precision: {} \n"
              "Recall: {} \n"
              "Over_detected: {} \n"
              "Non_detected: {} \n"
              "FP: {} \n"
              "TP: {} \n"
              "FN: {} \n".format(f1, precision, recall, over_detected,
                                 non_detected, FP, TP, FN))
def main(args):
    config = MitosisInferenceConfig()
    if args.reorganize_folder_structure:
        print("1. Reorganize the data structure for Mask_RCNN")
        reorganize_mitosis_images(config.mitosis_input_dir,
                                  config.mitosis_reorganized_dir)

    if args.split_big_images_to_small_ones:
        print("2. Crop big images into small ones")
        mitosis_files = \
            [str(f) for f in Path(config.mitosis_reorganized_dir)
                                 .glob('**/**/*.tif')]
        crop_image(mitosis_files,
                   config.inference_input_dir,
                   size=config.crop_image_size,
                   overlap=config.crop_image_overlap)

    if args.run_nucleus_detection:
        print("3. Run the nucleus segmentation inference")
        nucleus_mitosis.inference(config.inference_input_dir, config.weights,
                                  config.maskrcnn_inference_result_dir)
        print("Inference result dir: ", config.maskrcnn_inference_result_dir)

    if args.combine_small_inference_results:
        print("4. Combine small inference images and csvs to big ones")
        combine_images(config.maskrcnn_inference_result_dir,
                       config.maskrcnn_inference_combined_result,
                       size=config.crop_image_size)
        combine_csvs(config.maskrcnn_inference_result_dir,
                     config.maskrcnn_inference_combined_result,
                     hasProb=config.hasProb,
                     clean_output_dir=False)

    if args.cluster_nucleus_detection_results:
        print("4.1 Cluster the nucleus detection results")
        cluster_prediction_result(config.maskrcnn_inference_combined_result,
                                  eps=32,
                                  min_samples=1,
                                  hasHeader=True,
                                  isWeightedAvg=False,
                                  prob_threshold=0.8)
        for file_name in [
                "11-01", "11-02", "11-03", "12-01", "12-02", "12-03"
        ]:
            add_mark(os.path.join(config.maskrcnn_inference_combined_result,
                                  "{}.png".format(file_name)),
                     os.path.join(
                         config.maskrcnn_inference_combined_clusterd_result,
                         "{}.csv".format(file_name)),
                     hasHeader=True,
                     shape=Shape.SQUARE,
                     mark_color=(255, 100, 100, 200))

    if args.visualize_the_ground_truth:
        print("5. Visualize the ground truth masks")
        add_groundtruth_mark(config.maskrcnn_inference_combined_result,
                             config.ground_truth_dir,
                             hasHeader=False,
                             shape=Shape.CIRCLE)
        # add_mark('/Users/fei/Documents/Github/Mask_RCNN/samples/nucleus/datasets/stage1_combine_test/01-01.png',
        #          '/Users/fei/Documents/Github/Mask_RCNN/samples/nucleus/datasets/stage1_combine_test/01-01.csv',
        #          hasHeader=True, shape=Shape.CIRCLE, mark_color=(255,0,0,50))

    if args.evaluate_nucleus_inference:
        print("6. Evaluate the nucleus inference result")
        check_nucleus_inference(config.maskrcnn_inference_combined_result,
                                config.ground_truth_dir)

    if args.extract_nucleus_patch:
        print("7. Extract patches according to the inference result")
        extract_patches(config.mitosis_input_dir,
                        config.maskrcnn_inference_combined_result,
                        config.extracted_nucleus_dir,
                        patch_size=config.mitosis_patch_size)

    if args.run_mitosis_classification:
        print("8. Run mitosis classification inference")
        batch_size = 128
        num_parallel_calls = 8
        prob_thres = 0.5,
        eps = 64
        min_samples = 1
        is_weighted_avg = False
        run_mitosis_classification_in_batch(
            batch_size=batch_size,
            input_dir_basepath=config.extracted_nucleus_dir,
            output_dir_basepath=config.mitosis_classification_result_dir,
            model_file=config.mitosis_classification_model_file,
            augmentation_number=config.augmentation_number,
            mitosis_tile_size=config.mitosis_tile_size,
            num_parallel_calls=config.
            mitosis_classification_num_parallel_calls,
            prefectch=config.mitosis_classification_prefetch,
            prob_thres=prob_thres,
            eps=eps,
            min_samples=min_samples,
            isWeightedAvg=is_weighted_avg)

    if args.compute_f1:
        print("9. Compute F1 score")
        f1, precision, recall, over_detected, non_detected, FP, TP, FN = \
            evaluate_global_f1(config.mitosis_classification_result_dir,
                               config.ground_truth_dir,
                               threshold=30,
                               prob_threshold=config.f1_prob_threshhold)
        print("F1: {} \n"
              "Precision: {} \n"
              "Recall: {} \n"
              "Over_detected: {} \n"
              "Non_detected: {} \n"
              "FP: {} \n"
              "TP: {} \n"
              "FN: {} \n".format(f1, precision, recall, len(over_detected),
                                 len(non_detected), len(FP), len(TP), len(FN)))
from deephistopath.evaluation import evaluate_global_f1
import deephistopath.evaluation as ev
import deephistopath.detection as det
import shutil
img_dir = "data/mitoses/mitoses_train_image_data"
#pred_dir = "pred/result/2018/new_orig_v3_fp_v3_8_icpr/scratch/resnet_custom/resnet_custom_stride_32_nm_model_180307_175241"
#pred_dir = "pred/result/2018/new_orig_v3_fp_v3_8_icpr/scratch/resnet_custom/resnet_custom_stride_16_nm_model_180307_175241"
#pred_dir = "pred/result/2018/new_orig_v3_fp_v3_8_icpr/scratch/resnet_custom/resnet_custom_stride_1_nm_model_180307_175241"
#pred_dir = "pred/result/2018/new_orig_v3_fp_v3_8_icpr/scratch/resnet_custom/resnet_custom_stride_16_marg_model_180307_175241"
#ground_true_dir = "pred/data/val_ground_truth"
pred_dir = "/8tb/deep_histopath/result/mitoses/mitoses_test_image_result"

print("raw predictions:")
threshold = 30
prob_thresh = 0.5
f1, ppv, sens, over_detected, non_detected, fp_list, tp_list, fn_list = evaluate_global_f1(
    pred_dir, ground_true_dir, threshold, prob_thresh)
print(f"F1: {f1}; Precision: {ppv}; Recall: {sens}; FP: {len(fp_list)}; TP: {len(tp_list)}; FN: "\
      f"{len(fn_list)}")

# smooth prob maps
print("smoothing predictions:")
radius = 15
det.smooth_prediction_results(pred_dir, img_dir, radius, True)

# NOTE: this is probably not what we want to evaluate, because the smoothing algorithm needs the
# ensuing detection step to be useful
print("smoothed predictions:")
smooth_pred_dir = pred_dir + "_smoothed"
#threshold = 30  # 27
#prob_thresh = 0.35 #65
#f1, ppv, sens, over_detected, non_detected, fp_list, tp_list, fn_list = evaluate_global_f1(