Beispiel #1
0
 def test_infilled_keys(self):
     dd = DataDirectory(TEST_DATA_DIR)
     for lid, tf in TEST_FILES.items():
         if 'roi_numbers_stitched' in tf:
             b = dd[lid]
             rns = tf['roi_numbers_stitched']
             ii = InfilledImages(b)
             assert set(rns) == set(ii.keys())
Beispiel #2
0
 def images(self, bin=None, infilled=False):
     if bin is None:
         b = self._get_bin()
     else:
         b = bin
     if infilled or b.schema == SCHEMA_VERSION_1:
         return InfilledImages(b)
     else:
         return b.images
Beispiel #3
0
 def shapes(self):
     if self._shapes is not None:
         return self._shapes
     hs, ws, ix = [], [], []
     with self.bin:
         if self.bin.schema == SCHEMA_VERSION_1:
             ii = InfilledImages(self.bin)
         else:
             ii = self.bin.images
         for target_number in ii:
             h, w = ii.shape(target_number)
             hs.append(math.floor(h * self.scale))
             ws.append(math.floor(w * self.scale))
             ix.append(target_number)
     self._shapes = (np.array(hs,
                              dtype=np.int32), np.array(ws, dtype=np.int32),
                     np.array(ix, dtype=np.int32))
     return self._shapes
Beispiel #4
0
    def __init__(self, bin, resize):
        self.bin = bin
        self.images = []
        self.pids = []

        # use 299x299 for inception_v3, all other models use 244x244
        if isinstance(resize, int):
            resize = (resize, resize)
        self.resize = resize

        # old-style bins need to be stitched and infilled
        if bin.schema == SCHEMA_VERSION_1:
            bin_images = InfilledImages(bin)
        else:
            bin_images = bin.images

        for target_number, img in bin_images.items():
            target_pid = bin.pid.with_target(target_number)
            self.images.append(img)
            self.pids.append(target_pid)
Beispiel #5
0
 def add_bin(self, bin, b): # IFCB bin, Bin instance
     # qaqc checks
     qc_bad = check_bad(bin)
     if qc_bad:
         b.qc_bad = True
         return b, 'malformed raw data'
     no_rois = check_no_rois(bin)
     if no_rois:
         b.qc_bad = True
         return b, 'zero ROIs'
     # more error checking for setting attributes
     try:
         ml_analyzed = bin.ml_analyzed
         if ml_analyzed <= 0:
             b.qc_bad = True
             return b, 'ml_analyzed <= 0'
     except Exception as e:
         b.qc_bad = True
         return b, 'ml_analyzed: {}'.format(str(e))
     # metadata
     try:
         b.metadata_json = json.dumps(bin.hdr_attributes)
     except Exception as e:
         b.qc_bad = True
         return b, 'header: {}'.format(str(e))
     #
     b.qc_no_rois = check_no_rois(bin)
     # metrics
     try:
         b.temperature = bin.temperature
     except KeyError: # older data
         b.temperature = 0
     try:
         b.humidity = bin.humidity
     except KeyError: # older data
         b.humidity = 0
     b.size = bin.fileset.getsize() # assumes FilesetBin
     b.ml_analyzed = ml_analyzed
     b.look_time = bin.look_time
     b.run_time = bin.run_time
     b.n_triggers = len(bin)
     if bin.pid.schema_version == SCHEMA_VERSION_1:
         ii = InfilledImages(bin)
         b.n_images = len(ii)
     else:
         b.n_images = len(bin.images)
     b.concentration = b.n_images / ml_analyzed
     if b.concentration < 0: # metadata is bogus!
         return b, 'rois/ml is < 0'
     return b, None # defer save
Beispiel #6
0
 def test_infill_values(self):
     dd = DataDirectory(TEST_DATA_DIR)
     for lid, tf in TEST_FILES.items():
         if 'roi_numbers_stitched' in tf:
             b = dd[lid]
             rns = tf['roi_numbers_stitched']
             roi_corners = tf['stitched_corners']
             iis = InfilledImages(b)
             for rn in rns:
                 ii = iis[rn]
                 c1 = ii[0, 0]
                 c2 = ii[0, -1]
                 c3 = ii[-1, 0]
                 c4 = ii[-1, -1]
                 corners = [c1, c2, c3, c4]
                 assert roi_corners[rn] == corners
Beispiel #7
0
 def page(self, page=0):
     df = self.pack()
     page_h, page_w = self.shape
     page_image = np.zeros((page_h, page_w), dtype=np.uint8) + self.bg_color
     sdf = df[df.page == page]
     with self.bin:
         if self.bin.schema == SCHEMA_VERSION_1:
             ii = InfilledImages(self.bin)
         else:
             ii = self.bin.images
         for index, row in sdf.iterrows():
             y, x = row.y, row.x
             h, w = row.h, row.w
             unscaled_image = ii[row.roi_number]
             scaled_image = resize(unscaled_image, (h, w),
                                   mode='reflect',
                                   preserve_range=True)
             page_image[y:y + h, x:x + w] = scaled_image
     return page_image
Beispiel #8
0
class Mosaic(object):
    def __init__(self, the_bin, shape=(720, 1280), bg_color=200):
        self.bin = the_bin
        self.shape = shape
        self.bg_color = bg_color
        self.ii = InfilledImages(self.bin)

    @lru_cache()
    def _shapes(self):
        hs, ws, ix = [], [], []
        for target_number in self.ii:
            h, w = self.ii.shape(target_number)
            hs.append(h)
            ws.append(w)
            ix.append(target_number)
        return zip(hs, ws, ix)

    @lru_cache()
    def pack(self):
        page_h, page_w = self.shape
        pages = [(page_h - 1, page_w - 1) for _ in range(20)]
        packer = newPacker(sort_algo=SORT_AREA,
                           rotation=False,
                           pack_algo=GuillotineBafSlas)
        for r in self._shapes():
            packer.add_rect(*r)
        for p in pages:
            packer.add_bin(*p)
        packer.pack()
        COLS = ['page', 'y', 'x', 'h', 'w', 'roi_number']
        return pd.DataFrame(packer.rect_list(), columns=COLS)

    def page(self, page=0):
        df = self.pack()
        page_h, page_w = self.shape
        page_image = np.zeros((page_h, page_w), dtype=np.uint8) + self.bg_color
        sdf = df[df.page == page]
        for index, row in sdf.iterrows():
            y, x = row.y, row.x
            h, w = row.h, row.w
            page_image[y:y + h, x:x + w] = self.ii[row.roi_number]
        return page_image
Beispiel #9
0
 def __init__(self, the_bin, shape=(720, 1280), bg_color=200):
     self.bin = the_bin
     self.shape = shape
     self.bg_color = bg_color
     self.ii = InfilledImages(self.bin)