Beispiel #1
0
def main(cropped_exam_list_path, data_prefix, output_exam_list_path, num_processes=1):
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(
        data_list=data_list,
        data_prefix=data_prefix,
        num_processes=num_processes
    )
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)
Beispiel #2
0
def crop_mammogram():
    id_patient = request.args.get('id')
    input_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/' + id_patient
    # exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/exam_list_before_cropping.pkl'
    exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/filename.pkl'
    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    output_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    num_processes = 10
    num_iterations = 100
    buffer_size = 50

    exam_list = pickling.unpickle_from_file(exam_list_path)

    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        sh.rmtree(
            'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/cropped_images'
        )
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = partial(
        crop_mammogram_one_image,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)

    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)
    return jsonify("Done")
            def crop_mammogram(input_data_folder, exam_list_path,
                               cropped_exam_list_path, output_data_folder,
                               num_processes, num_iterations, buffer_size):
                """
                In parallel, crops mammograms in DICOM format found in input_data_folder and save as png format in
                output_data_folder and saves new image list in cropped_image_list_path
                """
                exam_list = pickling.unpickle_from_file(exam_list_path)

                image_list = data_handling.unpack_exam_into_images(exam_list)

                if os.path.exists(output_data_folder):
                    # Prevent overwriting to an existing directory
                    print(
                        "Error: the directory to save cropped images already exists."
                    )
                    return
                else:
                    os.makedirs(output_data_folder)
                #global crop_mammogram_one_image_func
                crop_mammogram_one_image_func = partial(
                    crop_mammogram_one_image_short_path,
                    input_data_folder=input_data_folder,
                    output_data_folder=output_data_folder,
                    num_iterations=num_iterations,
                    buffer_size=buffer_size,
                )
                with Pool(num_processes) as pool:
                    cropped_image_info = pool.map(
                        crop_mammogram_one_image_func, image_list)

                window_location_dict = dict([x[0] for x in cropped_image_info])
                rightmost_points_dict = dict(
                    [x[1] for x in cropped_image_info])
                bottommost_points_dict = dict(
                    [x[2] for x in cropped_image_info])
                distance_from_starting_side_dict = dict(
                    [x[3] for x in cropped_image_info])

                data_handling.add_metadata(exam_list, "window_location",
                                           window_location_dict)
                data_handling.add_metadata(exam_list, "rightmost_points",
                                           rightmost_points_dict)
                data_handling.add_metadata(exam_list, "bottommost_points",
                                           bottommost_points_dict)
                data_handling.add_metadata(exam_list,
                                           "distance_from_starting_side",
                                           distance_from_starting_side_dict)

                pickling.pickle_to_file(cropped_exam_list_path, exam_list)
Beispiel #4
0
def crop_mammogram(input_data_folder, exam_list_path, cropped_exam_list_path,
                   output_data_folder, num_processes, num_iterations,
                   buffer_size):
    """
    In parallel, crops mammograms in DICOM format found in input_data_folder and save as png format in
    output_data_folder and saves new image list in cropped_image_list_path
    """

    # list of exams (one dictionary per exam)
    exam_list = pickling.unpickle_from_file(exam_list_path)

    # list per image (one dictionary per image). It contains same information than in list of exams + cropped information if present.
    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        return
    else:
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = partial(
        crop_mammogram_one_image_short_path,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)
    # F: cropped image info returns a list. Each entry is the return of a single execution
    # F: of crop_mammogram_one_image_func.

    # F: each dict defined contains the information of all images
    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)
Beispiel #5
0
def main():
    id_patient = request.args.get('id');
    cropped_exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'+id_patient+'/'+id_patient+'.pkl'
    data_prefix='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'+id_patient
    output_exam_list_path='sample_output/'+id_patient+'.pkl'
    num_processes = 10
    id_patient = request.args.get('id');
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(
        data_list=data_list,
        data_prefix=data_prefix,
        num_processes=num_processes
    )
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)
    return jsonify("Done")
Beispiel #6
0
def crop_mammogram():
    id_patient = request.args.get('id')
    input_data_folder = 'C:/Users/ultra/PycharmProjects/BIRADS_classifier-master/images/' + id_patient
    # exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/exam_list_before_cropping.pkl'
    exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/filename.pkl'
    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    output_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    num_processes = 10
    num_iterations = 100
    buffer_size = 50

    exam_list = pickling.unpickle_from_file(exam_list_path)

    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        sh.rmtree(
            'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'
            + id_patient)
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = ps(
        crop_mammogram_one_image,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)

    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)

    ###NEXT####

    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    data_prefix = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    output_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.pkl'
    num_processes = 10
    id_patient = request.args.get('id')
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(data_list=data_list,
                                          data_prefix=data_prefix,
                                          num_processes=num_processes)
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)

    ###NEXT #####
    parser = argparse.ArgumentParser(
        description='Run image-only model or image+heatmap model')
    # parser.add_argument('--model-path', required=True)
    # parser.add_argument('--data-path', required=True)
    # parser.add_argument('--image-path', required=True)
    # parser.add_argument('--output-path', required=True)
    parser.add_argument('--batch-size', default=1, type=int)
    parser.add_argument('--seed', default=0, type=int)
    parser.add_argument('--use-heatmaps', action="store_true")
    parser.add_argument('--heatmaps-path')
    parser.add_argument('--use-augmentation', action="store_true")
    parser.add_argument('--use-hdf5', action="store_true")
    # parser.add_argument('--num-epochs', default=1, type=int)
    # parser.add_argument('--device-type', default="cpu", choices=['gpu', 'cpu'])
    # parser.add_argument("--gpu-number", type=int, default=0)
    args = parser.parse_args()

    parameters = {
        "device_type":
        'gpu',
        "gpu_number":
        0,
        "max_crop_noise": (100, 100),
        "max_crop_size_noise":
        100,
        "image_path":
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'
        + id_patient,
        "batch_size":
        args.batch_size,
        "seed":
        args.seed,
        "augmentation":
        args.use_augmentation,
        "num_epochs":
        10,
        "use_heatmaps":
        args.use_heatmaps,
        "heatmaps_path":
        args.heatmaps_path,
        #   "heatmaps_path": 'C:/Users/ultra/jPycharmProjects/breast_cancer_classifier/src/cropping/sample_output/heatmaps_'+id_patient,
        "use_hdf5":
        args.use_hdf5
    }
    # load_run_save(
    #    model_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/models/sample_image_model.p',
    #    data_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.pkl',
    #   # output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/image_predictions.csv',
    #   output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.csv',
    #   parameters=parameters,
    # )
    return load_run_save(
        model_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/models/sample_image_model.p',
        data_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/'
        + id_patient + '.pkl',
        # output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/image_predictions.csv',
        output_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/'
        + id_patient + '.csv',
        parameters=parameters,
    )
Beispiel #7
0
import os, sys
# print(sys.path)
current_dir = os.path.dirname(os.path.abspath(__file__))
# appending parent dir of current_dir to sys.path
sys.path.append(os.path.dirname(current_dir))

from multiprocessing import Pool
import argparse
from functools import partial
import scipy.ndimage
import numpy as np
import pandas as pd

import src.utilities.pickling as pickling
import src.utilities.reading_images as reading_images
import src.utilities.saving_images as saving_images
import src.utilities.data_handling as data_handling

# list of exams (one dictionary per exam)
exam_list = pickling.unpickle_from_file('exam_list_before_cropping.pkl')
cropped_exam_list_path = '../sample_output/cropped_images/cropped_exam_list_test.pkl'
print(exam_list)
exam_list[0]['L-CC'] = []
# list per image (one dictionary per image). It contains same information than in list of exams + cropped information if present.
image_list = data_handling.unpack_exam_into_images(exam_list)

pickling.pickle_to_file(cropped_exam_list_path, exam_list)