def prepare_dicom_images(input_data_folder, output_data_folder, exam_list_path): dcm_files = glob.glob(os.path.join(input_data_folder, "**", "*.dcm"), recursive=True) os.makedirs(output_data_folder, exist_ok=True) saving_images.save_dicom_image_as_png( find_view(dcm_files, "L", "CC"), os.path.join(output_data_folder, "L_CC.png")) saving_images.save_dicom_image_as_png( find_view(dcm_files, "R", "CC"), os.path.join(output_data_folder, "R_CC.png")) saving_images.save_dicom_image_as_png( find_view(dcm_files, "L", "MLO"), os.path.join(output_data_folder, "L_MLO.png")) saving_images.save_dicom_image_as_png( find_view(dcm_files, "R", "MLO"), os.path.join(output_data_folder, "R_MLO.png")) exam_list = [{ 'horizontal_flip': 'NO', 'L-CC': ['L_CC'], 'R-CC': ['R_CC'], 'L-MLO': ['L_MLO'], 'R-MLO': ['R_MLO'] }] pickling.pickle_to_file(exam_list_path, exam_list)
def crop_single_mammogram(mammogram_path, horizontal_flip, view, cropped_mammogram_path, metadata_path, num_iterations, buffer_size): """ Crop a single mammogram image """ metadata_dict = dict( short_file_path=None, horizontal_flip=horizontal_flip, full_view=view, side=view[0], view=view[2:], ) cropped_image_info = crop_mammogram.crop_mammogram_one_image( scan=metadata_dict, input_file_path=mammogram_path, output_file_path=cropped_mammogram_path, num_iterations=num_iterations, buffer_size=buffer_size, ) metadata_dict["window_location"] = cropped_image_info[0] metadata_dict["rightmost_points"] = cropped_image_info[1] metadata_dict["bottommost_points"] = cropped_image_info[2] metadata_dict["distance_from_starting_side"] = cropped_image_info[3] pickling.pickle_to_file(metadata_path, metadata_dict)
def get_optimal_center_single(cropped_mammogram_path, metadata_path): """ Get optimal center for single example """ metadata = pickling.unpickle_from_file(metadata_path) image = reading_images.read_image_png(cropped_mammogram_path) optimal_center = get_optimal_centers.extract_center(metadata, image) metadata["best_center"] = optimal_center pickling.pickle_to_file(metadata_path, metadata)
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)
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)
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)
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")
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, )
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)