def do_sampling(pat_df, out_dir):
     for pat,side,view in pat_df.index.unique():
         full_fn = const_filename(pat, side, view, full_img_dir)
         # import pdb; pdb.set_trace()
         try:
             if target_width is None:
                 full_img = read_resize_img(
                     full_fn, target_height=target_height)
             else:
                 full_img = read_resize_img(
                     full_fn, target_size=(target_height, target_width))
             img_id = '_'.join([pat, side, view])
             print "ID:%s, read image of size=%s" % (img_id, full_img.shape),
             if segment_breast:
                 full_img, bbox = imprep.segment_breast(full_img)
                 print "size after segmentation=%s" % (str(full_img.shape))
             sys.stdout.flush()
             # Read mask image(s).
             abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view]
             if isinstance(abn_path, pd.Series):
                 abn_num = [abn_path['abn_num']]
                 pathology = [abn_path['pathology']]
                 itypes = [abn_path['type']]
             else:
                 abn_num = abn_path['abn_num']
                 pathology = abn_path['pathology']
                 itypes = abn_path['type']
             bkg_sampled = False
             for abn, path, itype in zip(abn_num, pathology, itypes):
                 mask_fn = const_filename(pat, side, view, roi_mask_dir, itype, abn)
                 if target_width is None:
                     mask_img = read_resize_img(
                         mask_fn, target_height=target_height, gs_255=True)
                 else:
                     mask_img = read_resize_img(
                         mask_fn, target_size=(target_height, target_width), 
                         gs_255=True)
                 if segment_breast:
                     mask_img = crop_img(mask_img, bbox)
                 # sample using mask and full image.
                 nb_hns_ = nb_hns if not bkg_sampled else 0
                 if nb_hns_ > 0:
                     hns_sampled = sample_blob_negatives(
                         full_img, mask_img, out_dir, img_id, 
                         abn, blob_detector, patch_size, neg_cutoff, 
                         nb_hns_, 0, bkg_dir, verbose)
                 else:
                     hns_sampled = 0
                 pos = path.startswith('MALIGNANT')
                 nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0
                 sample_patches(full_img, mask_img, out_dir, img_id, abn, pos, 
                                patch_size, pos_cutoff, neg_cutoff, 
                                nb_bkg_, nb_abn, hns_sampled, itype,
                                bkg_dir, calc_pos_dir, calc_neg_dir, 
                                mass_pos_dir, mass_neg_dir, verbose)
                 bkg_sampled = True
         except AttributeError:
             print "Read image error: %s" % (full_fn)
         except ValueError:
             print "Error sampling from ROI mask image: %s" % (mask_fn)
    def do_sampling(pat_df, out_dir):
        for patient_id, side, view in pat_df.index.unique():
            cur_desc = description_df.loc[patient_id].loc[side].loc[view]
            abn_ids = cur_desc["abnormality id"]
            pathologies = cur_desc["pathology"]
            abn_types = cur_desc["abnormality type"]
            if isinstance(cur_desc, pd.Series):
                abn_ids = [abn_ids]
                pathologies = [pathologies]
                abn_types = [abn_types]

            # Read mask image(s).
            bkg_sampled = False
            for abn_id, pathology, abn_type in zip(abn_ids, pathologies, abn_types):
                # NOTE csv not reliable due to formatting error, and there are missing files.
                # image_path = cur_desc["image file path"]
                # mask_path = cur_desc["ROI mask file path"]
                try:
                    full_image, mask_image = get_image_and_mask(
                        patient_id=patient_id,
                        side=side,
                        view=view,
                        image_dir=image_dir,
                        roi_mask_dir=roi_mask_dir,
                        abn_type=abn_type,
                        abn_id=abn_id,
                        target_height=target_height,
                        target_width=target_width
                    )

                    image_id = "_".join([patient_id, side, view])
                    print "ID:%s, read image of size=%s" % (image_id, full_image.shape)
                    if segment_breast:
                        full_image, bbox = imprep.segment_breast(full_image)
                        print "size after segmentation=%s" % (str(full_image.shape))
                        mask_image = crop_img(mask_image, bbox)

                    # sample using mask and full image.
                    nb_hns_ = nb_hns if not bkg_sampled else 0
                    if nb_hns_ > 0:
                        hns_sampled = sample_blob_negatives(
                            full_image, mask_image, out_dir, image_id,
                            abn_id, blob_detector, patch_size, neg_cutoff,
                            nb_hns_, 0, bkg_dir, verbose)
                    else:
                        hns_sampled = 0
                    pos = pathology.startswith("MALIGNANT")
                    nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0
                    sample_patches(full_image, mask_image, out_dir, image_id, abn_id, pos,
                                   patch_size, pos_cutoff, neg_cutoff,
                                   nb_bkg_, nb_abn, hns_sampled, abn_type,
                                   bkg_dir, calc_pos_dir, calc_neg_dir,
                                   mass_pos_dir, mass_neg_dir, verbose)
                    bkg_sampled = True
                except RuntimeError as exception:
                    print exception
 def do_sampling(pat_df, out_dir):
     for pat, side, view in pat_df.index.unique():
         full_fn = const_filename(pat, side, view, full_img_dir)
         # import pdb; pdb.set_trace()
         try:
             full_img = read_resize_img(full_fn,
                                        target_height=target_height)
             img_id = '_'.join([pat, side, view])
             print "ID:%s, read image of size=%s" % (img_id,
                                                     full_img.shape),
             full_img, bbox = imprep.segment_breast(full_img)
             print "size after segmentation=%s" % (str(full_img.shape))
             sys.stdout.flush()
             # Read mask image(s).
             abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view]
             if isinstance(abn_path, pd.Series):
                 abn_num = [abn_path['abn_num']]
                 pathology = [abn_path['pathology']]
                 itypes = [abn_path['type']]
             else:
                 abn_num = abn_path['abn_num']
                 pathology = abn_path['pathology']
                 itypes = abn_path['type']
             bkg_sampled = False
             for abn, path, itype in zip(abn_num, pathology, itypes):
                 mask_fn = const_filename(pat, side, view, roi_mask_dir,
                                          itype, abn)
                 mask_img = read_resize_img(mask_fn,
                                            target_height=target_height,
                                            gs_255=True)
                 mask_img = crop_img(mask_img, bbox)
                 # sample using mask and full image.
                 nb_hns_ = nb_hns if not bkg_sampled else 0
                 if nb_hns_ > 0:
                     hns_sampled = sample_blob_negatives(
                         full_img, mask_img, out_dir, img_id, abn,
                         blob_detector, patch_size, neg_cutoff, nb_hns_, 0,
                         bkg_dir, verbose)
                 else:
                     hns_sampled = 0
                 pos = path.startswith('MALIGNANT')
                 nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0
                 sample_patches(full_img, mask_img, out_dir, img_id, abn,
                                pos, patch_size, pos_cutoff, neg_cutoff,
                                nb_bkg_, nb_abn, hns_sampled, itype,
                                bkg_dir, calc_pos_dir, calc_neg_dir,
                                mass_pos_dir, mass_neg_dir, verbose)
                 bkg_sampled = True
         except AttributeError:
             print "Read image error: %s" % (full_fn)
         except ValueError:
             print "Error sampling from ROI mask image: %s" % (mask_fn)
 def do_sampling(pat_df, out_dir):
     #遍历病人信息
     for pat, side, view in pat_df.index.unique():
         #文件名命名
         full_fn = const_filename(pat, side, view, full_img_dir)
         # import pdb; pdb.set_trace()
         try:
             #如果未指定宽度则根据高度调整图片大小
             if target_width is None:
                 full_img = read_resize_img(full_fn,
                                            target_height=target_height)
             else:
                 #根据宽高调整大小
                 full_img = read_resize_img(full_fn,
                                            target_size=(target_height,
                                                         target_width))
             #命名图片ID
             img_id = '_'.join([pat, side, view])
             print(
                 "ID:%s, read image of size=%s" %
                 (img_id, full_img.shape), )
             #图像中分割出乳腺为true
             if segment_breast:
                 #获取乳腺图像和坐标宽高
                 full_img, bbox = imprep.segment_breast(full_img)
                 #输出图像宽高通道数
                 print("size after segmentation=%s" % (str(full_img.shape)))
             sys.stdout.flush()
             # Read mask image(s).读取掩码图
             #获取异常路径
             abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view]
             #无论此路径是否为pd.Series类型,都要进行异常数,病理(良恶性),类型(肿块或钙化点)的数据获取
             if isinstance(abn_path, pd.Series):
                 abn_num = [abn_path['abn_num']]
                 pathology = [abn_path['pathology']]
                 itypes = [abn_path['type']]
             else:
                 abn_num = abn_path['abn_num']
                 pathology = abn_path['pathology']
                 itypes = abn_path['type']
             #
             bkg_sampled = False
             #遍历刚才获取的数据,转为掩码文件
             for abn, path, itype in zip(abn_num, pathology, itypes):
                 mask_fn = const_filename(pat, side, view, roi_mask_dir,
                                          itype, abn)
                 if target_width is None:
                     mask_img = read_resize_img(mask_fn,
                                                target_height=target_height,
                                                gs_255=True)
                 else:
                     mask_img = read_resize_img(mask_fn,
                                                target_size=(target_height,
                                                             target_width),
                                                gs_255=True)
                 if segment_breast:
                     mask_img = crop_img(mask_img, bbox)
                 # sample using mask and full image.样本使用掩码和全图
                 #获取难类数
                 nb_hns_ = nb_hns if not bkg_sampled else 0
                 if nb_hns_ > 0:
                     #返回阴性斑点采样得到的背景样本数
                     hns_sampled = sample_blob_negatives(
                         full_img, mask_img, out_dir, img_id, abn,
                         blob_detector, patch_size, neg_cutoff, nb_hns_, 0,
                         bkg_dir, verbose)
                 else:
                     hns_sampled = 0
                 pos = path.startswith('MALIGNANT')
                 #背景数为总背景数-阴性斑点采样数
                 nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0
                 #对块进行采样
                 sample_patches(full_img, mask_img, out_dir, img_id, abn,
                                pos, patch_size, pos_cutoff, neg_cutoff,
                                nb_bkg_, nb_abn, hns_sampled, itype,
                                bkg_dir, calc_pos_dir, calc_neg_dir,
                                mass_pos_dir, mass_neg_dir, verbose)
                 bkg_sampled = True
         except AttributeError:
             print("Read image error: %s" % (full_fn))
         except ValueError:
             print("Error sampling from ROI mask image: %s" % (mask_fn))