def generate(self):
        while True:
            rand_idxs = np.arange(len(self.oc_gt_paths))
            if self.random:
                self.rng.shuffle(rand_idxs)
            for pos in xrange(0, len(rand_idxs), self.batch_size):
                idxs_batch = rand_idxs[pos:pos + self.batch_size]
                nb = len(idxs_batch)

                # allocate batches
                x_batch = np.zeros((nb,) + self.p_transform['patch_size'], dtype='float32')
                y_batch = np.zeros((nb,) + self.p_transform['patch_size'], dtype='float32')
                z_batch = np.zeros((nb,) + self.p_transform['patch_size'], dtype='float32')

                batch_ids = []

                for i, idx in enumerate(idxs_batch):
                    oc_gt_path = self.oc_gt_paths[idx]

                    cid = contour.get_contour_id(oc_gt_path)
                    did = contour.get_dicom_id(oc_gt_path)
                    sid = str(contour.get_slice_id(oc_gt_path))

                    ic_gt_path = contour.get_icontour_path(oc_gt_path)

                    sample_id = cid + '_' + did + '_' + sid

                    batch_ids.append(sample_id)

                    try:
                        x, pixel_spacing = scan.read_dicom_slice(did, sid)
                    except Exception:
                        print 'cannot open dicom, dicom id: ', did, ', slice id:', sid

                    try:
                        ic_contour_lst = contour.parse_contour_file(ic_gt_path)
                    except Exception:
                        print 'cannot open i-contour', ic_gt_path

                    try:
                        oc_contour_lst = contour.parse_contour_file(oc_gt_path)
                    except Exception:
                        print 'cannot open o-contour', oc_gt_path

                    y = contour.poly_to_mask(ic_contour_lst,x.shape[0],x.shape[1])
                    z = contour.poly_to_mask(oc_contour_lst,x.shape[0],x.shape[1])

                    x_batch[i] = self.data_prep_fun(x, pixel_spacing)
                    y_batch[i] = self.mask_prep_fun(y)
                    z_batch[i] = self.mask_prep_fun(z)


                if self.full_batch:
                    if nb == self.batch_size:
                        yield x_batch, y_batch, z_batch, batch_ids
                else:
                    yield x_batch, y_batch, z_batch, batch_ids

            if not self.infinite:
                break
def generate_polygon_mask_plot(icp):
    did = contour.get_dicom_id(icp)
    sid = contour.get_slice_id(icp)
    c_lst = contour.parse_contour_file(icp)
    mask = contour.poly_to_mask(c_lst,256,256)
    outfile = 'plots/pm_'+contour.get_contour_id(icp)+'_' \
                      +did+'_' \
                      +str(sid)+'.jpg'
    print 'plotting polygones and masks to file', outfile
    utils_plots.plot_polygone_on_mask(c_lst, mask, outfile, resolution=100)
def generate_polygon_mask_plots():
    for idx, icp in enumerate(contour.get_list_available_icontours()):
        did = contour.get_dicom_id(icp)
        sid = contour.get_slice_id(icp)
        c_lst = contour.parse_contour_file(icp)
        mask = contour.poly_to_mask(c_lst,256,256)
        outfile = 'plots/bp_oc_ic_'+contour.get_contour_id(icp)+'_' \
                          +did+'_' \
                          +str(sid)+'.jpg'
        print 'plotting polygones and masks to file', outfile
        utils_plots.plot_polygone_on_mask(c_lst, mask, outfile)
def generate_scan_plots_from_contour_lst(lst_contours):
    for idx, cp in enumerate(lst_contours):
        did = contour.get_dicom_id(cp)
        sid = contour.get_slice_id(cp)
        slice, pixel_spacing = scan.read_dicom_slice(did, sid)
        oc_lst = contour.parse_contour_file(cp)
        
        outfile = 'plots/scan_'+contour.get_contour_id(cp)+'_' \
                          +did+'_' \
                          +str(sid)+'.jpg'
        print pixel_spacing, 'plotting slice to file', outfile

        utils_plots.plot_slice(slice, outfile, figsize=(20, 20), x_bs=[75,175], y_bs=[75,175])
def generate_masked_scan_plots(lst_ocontours):
    for idx, ocp in enumerate(lst_ocontours):
        did = contour.get_dicom_id(ocp)
        sid = contour.get_slice_id(ocp)
        slice, pixel_spacing = scan.read_dicom_slice(did, sid)
        oc_lst = contour.parse_contour_file(ocp)
        
        outfile = 'plots/masked_scan_'+contour.get_contour_id(ocp)+'_' \
                          +did+'_' \
                          +str(sid)+'.jpg'
        print pixel_spacing, 'plotting masked slice to file', outfile

        mask = contour.poly_to_mask(oc_lst,slice.shape[0],slice.shape[1])
        masked_slice = mask*slice
        utils_plots.plot_slice(masked_slice, outfile, figsize=(20, 20), x_bs=[75,175], y_bs=[75,175])
def generate_border_plots(lst_contours, pixel_spacing_normalization=False):
    for idx, icp in enumerate(lst_contours):
        did = contour.get_dicom_id(icp)
        sid = contour.get_slice_id(icp)
        slice, pixel_spacing = scan.read_dicom_slice(did, sid)
        c_lst = contour.parse_contour_file(icp)
        
        outfile = 'plots/'+contour.get_contour_id(icp)+'_' \
                          +did+'_' \
                          +str(sid)+'.jpg'
        print pixel_spacing, 'plotting mask to file', outfile
        if pixel_spacing_normalization:
            assert pixel_spacing[0]==pixel_spacing[1]
            slice = scipy.ndimage.zoom(slice, pixel_spacing[0], order=0)

        mask = contour.poly_to_mask(c_lst,slice.shape[0],slice.shape[1])
        utils_plots.plot_border_on_slice(slice, mask, outfile, figsize=(20, 20))#, x_bs=[75,175], y_bs=[75,175])
def generate_histograms_masked_scan(lst_ocontours):
    for idx, ocp in enumerate(lst_ocontours):
        did = contour.get_dicom_id(ocp)
        sid = contour.get_slice_id(ocp)
        slice, pixel_spacing = scan.read_dicom_slice(did, sid)
        oc_lst = contour.parse_contour_file(ocp)
        
        outfile = 'plots/hist_masked_sc_'+contour.get_contour_id(ocp)+'_' \
                          +did+'_' \
                          +str(sid)+'.jpg'
        print pixel_spacing, 'plotting masked slice to file', outfile

        mask = contour.poly_to_mask(oc_lst,slice.shape[0],slice.shape[1])
        masked_slice = mask*slice 
        print np.amin(masked_slice), np.amax(masked_slice)
        bins = np.arange(0,550,10) 
        hist,bins = np.histogram(masked_slice, bins=bins) 
        print hist
        th_mslice = masked_slice > 150
        utils_plots.plot_slice_hist(masked_slice, th_mslice, bins[1:], outfile, figsize=(10, 5), x_bs=[75,175], y_bs=[75,175])