<s> import os , sys , re , cv2 , dicom , fnmatch <EOL> import numpy as np <EOL> from PIL import Image <EOL> import dsb_utils as du <EOL> import utils as u <EOL> import config as c <EOL> np . random . seed ( <NUM_LIT> ) <EOL> print '<STR_LIT>' <EOL> img_size = <NUM_LIT> <EOL> sax_series_dict = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> tr_contour_path = os . path . join ( c . data_sunnybrook , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> onl_contour_path = os . path . join ( c . data_sunnybrook , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> val_contour_path = os . path . join ( c . data_sunnybrook , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> tr_img_path = os . path . join ( c . data_sunnybrook , '<STR_LIT>' ) <EOL> val_img_path = os . path . join ( c . data_sunnybrook , '<STR_LIT>' ) <EOL> onl_img_path = os . path . join ( c . data_sunnybrook , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def shrink_case ( case ) : <EOL> toks = case . split ( '<STR_LIT:->' ) <EOL> def shrink_if_number ( x ) : <EOL> try : <EOL> cvt = int ( x ) <EOL> return str ( cvt ) <EOL> except ValueError : <EOL> return x <EOL> return '<STR_LIT:->' . join ( [ shrink_if_number ( t ) for t in toks ] ) <EOL> class Contour ( object ) : <EOL> def __init__ ( self , ctr_path ) : <EOL> self . ctr_path = ctr_path <EOL> match = re . search ( r'<STR_LIT>' , ctr_path ) <EOL> self . case = shrink_case ( match . group ( <NUM_LIT:1> ) ) <EOL> self . img_no = int ( match . group ( <NUM_LIT:2> ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . case , self . img_no ) <EOL> __repr__ = __str__ <EOL> def load_contour ( contour , img_path ) : <EOL> filename = '<STR_LIT>' % ( sax_series_dict [ contour . case ] , contour . img_no ) <EOL> full_path = os . path . join ( img_path , contour . case , filename ) <EOL> f = dicom . read_file ( full_path ) <EOL> img = f . pixel_array . astype ( np . uint8 ) <EOL> ctrs = np . loadtxt ( contour . ctr_path , delimiter = '<STR_LIT:U+0020>' ) . astype ( np . int ) <EOL> label = np . zeros_like ( img , dtype = '<STR_LIT>' ) <EOL> cv2 . fillPoly ( label , [ ctrs ] , <NUM_LIT:1> ) <EOL> return cv2 . resize ( img , ( img_size , img_size ) ) , cv2 . resize ( label , ( img_size , img_size ) ) <EOL> def get_all_contours ( contour_path ) : <EOL> contours = [ os . path . join ( dirpath , f ) <EOL> for dirpath , dirnames , files in os . walk ( contour_path ) <EOL> for f in fnmatch . filter ( files , '<STR_LIT>' ) ] <EOL> np . random . shuffle ( contours ) <EOL> print ( '<STR_LIT>' . format ( len ( contours ) ) ) <EOL> extracted = map ( Contour , contours ) <EOL> return extracted <EOL> def process_contours ( contours , img_path ) : <EOL> n = len ( contours ) <EOL> imgs_arr = np . empty ( ( n , <NUM_LIT:1> , img_size , img_size ) , dtype = np . uint8 ) <EOL> labels_arr = np . empty ( ( n , <NUM_LIT:1> , img_size , img_size ) , dtype = np . uint8 ) <EOL> for idx , ctr in enumerate ( contours ) : <EOL> img , label = load_contour ( ctr , img_path ) <EOL> imgs_arr [ idx , <NUM_LIT:0> ] = img <EOL> labels_arr [ idx , <NUM_LIT:0> ] = label <EOL> return imgs_arr , labels_arr <EOL> def process_data_hdf5 ( ) : <EOL> contour_paths = [ tr_contour_path , val_contour_path , onl_contour_path ] <EOL> image_paths = [ tr_img_path , val_img_path , onl_img_path ] <EOL> all_data = [ ] <EOL> for contour_path , img_path in zip ( contour_paths , image_paths ) : <EOL> train_ctrs = get_all_contours ( contour_path ) <EOL> imgs , labels = process_contours ( train_ctrs , img_path ) <EOL> all_data . append ( [ imgs , labels ] ) <EOL> aug_contour_path = os . path . join ( c . data_manual , '<STR_LIT>' , '<STR_LIT>' ) <EOL> aug_image_path = os . path . join ( c . data_manual , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for cfn in [ fn_ for fn_ in os . listdir ( aug_contour_path ) if '<STR_LIT>' in fn_ ] : <EOL> if '<STR_LIT>' in cfn : <EOL> continue <EOL> dcm_img = cv2 . imread ( os . path . join ( aug_image_path , cfn ) , <NUM_LIT:0> ) <EOL> dcm_img = cv2 . resize ( dcm_img , ( img_size , img_size ) ) . reshape ( <NUM_LIT:1> , <NUM_LIT:1> , img_size , img_size ) <EOL> contour_img = cv2 . imread ( os . path . join ( aug_contour_path , cfn ) , <NUM_LIT:0> ) <EOL> contour_img = cv2 . resize ( contour_img , ( img_size , img_size ) ) <EOL> _ , contour_img = cv2 . threshold ( contour_img , <NUM_LIT> , <NUM_LIT:255> , cv2 . THRESH_BINARY_INV ) <EOL> contour_img = contour_img . reshape ( <NUM_LIT:1> , <NUM_LIT:1> , img_size , img_size ) / <NUM_LIT:255> <EOL> all_data . append ( [ dcm_img , contour_img ] ) <EOL> with open ( os . path . join ( c . data_manual , '<STR_LIT>' ) ) as f : <EOL> label = np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:1> , img_size , img_size ) , dtype = np . uint8 ) ; <EOL> for l in f : <EOL> row = [ int ( x ) for x in l . split ( '<STR_LIT:U+002C>' ) ] ; <EOL> case = row [ <NUM_LIT:0> ] ; <EOL> s = row [ <NUM_LIT:1> : : <NUM_LIT:2> ] ; <EOL> t = row [ <NUM_LIT:2> : : <NUM_LIT:2> ] ; <EOL> assert ( len ( s ) == len ( t ) ) ; <EOL> dset = du . CNN_Dataset ( case , img_size = img_size ) <EOL> n = len ( s ) ; <EOL> print ( "<STR_LIT>" . format ( case ) ) <EOL> for i in range ( n ) : <EOL> img = dset . images [ s [ i ] , t [ i ] , <NUM_LIT:0> ] . reshape ( <NUM_LIT:1> , <NUM_LIT:1> , img_size , img_size ) <EOL> all_data . append ( [ img , label ] ) <EOL> np . random . shuffle ( all_data ) <EOL> all_imgs = np . concatenate ( [ a [ <NUM_LIT:0> ] for a in all_data ] , axis = <NUM_LIT:0> ) <EOL> all_labels = np . concatenate ( [ a [ <NUM_LIT:1> ] for a in all_data ] , axis = <NUM_LIT:0> ) <EOL> n = all_imgs . shape [ <NUM_LIT:0> ] <EOL> fn = os . path . join ( c . data_intermediate , '<STR_LIT>' . format ( img_size ) ) <EOL> if os . path . exists ( fn ) : <EOL> os . remove ( fn ) <EOL> u . save_hd5py ( { '<STR_LIT>' : all_imgs , '<STR_LIT>' : all_labels } , fn , <NUM_LIT:5> ) </s>
