Example #1
0
def main():
    """
    this script extracts patches using images and lesion masks
    images with the same name will be matched between the two input directories

    CLI Args:
        1: a directory of segmented lung images
        2: a directory of lesion masks
        3: the output directory for the patches
    """
    image_dir = sys.argv[1]
    mask_dir = sys.argv[2]
    out_dir = sys.argv[3]

    image_files = get_all_files(image_dir)
    mask_files = get_all_files(mask_dir)

    check_files = list(
        set([ops(f)[0]
             for f in image_files]) & set([ops(f)[0] for f in mask_files]))

    for cf in check_files:
        mask_file = [f for f in mask_files if cf == ops(f)[0]][0]
        image_file = [f for f in image_files if cf == ops(f)[0]][0]
        mask = Image.open(opj(mask_dir, mask_file))
        #mask = PIL.ImageOps.invert(mask)
        patch_coords = util.find_lesion_coordiates(np.array(mask))
        image = Image.open(opj(image_dir, image_file))
        #image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
        #image = preprocess.preprocess(image)
        #print(cf, patch_coords, image.shape)
        patch = util.extract_patch(np.array(image), patch_coords,
                                   preprocess.PATCH_SIZE)
        patch_image = Image.fromarray(np.uint8(patch)).convert("L")
        patch_image.save(opj(out_dir, cf + ".png"), "PNG")
Example #2
0
 def __call__(self, data):
     count = 0
     for fpath in _find_files(self.regex, dirs=self.dirs, topdir=self.topdir):
         lgr.log(5, "Found file %s" % fpath)
         count += 1
         path, filename = ops(fpath)
         yield updated(data, {'path': path, 'filename': filename})
     self._total_count += count
     if not self._total_count and self.fail_if_none:
         raise RuntimeError("We did not match any file using regex %r" % self.regex)
Example #3
0
 def __call__(self, data):
     count = 0
     for fpath in _find_files(self.regex,
                              dirs=self.dirs,
                              topdir=self.topdir):
         lgr.log(5, "Found file %s" % fpath)
         count += 1
         path, filename = ops(fpath)
         yield updated(data, {'path': path, 'filename': filename})
     self._total_count += count
     if not self._total_count and self.fail_if_none:
         raise RuntimeError("We did not match any file using regex %r" %
                            self.regex)
Example #4
0
    def commit_collection(self, collection, msg):

        if self.is_read_only:
            raise RuntimeWarning("Can't commit remote collection.")

        if not isinstance(collection, Collection):
            raise TypeError("Can't save non-collection type: %s" %
                            type(collection))

        # save current branch ...
        current_branch = self.repo.git_get_active_branch()

        if self.branch != current_branch:
            # ... and switch to the one to be changed:
            self.repo.git_checkout(self.branch)

        # handle files we no longer have:
        files_to_remove = [f for f in self.repo.get_indexed_files()
                           if self.repo._filename2key(ops(f)[0]) not in
                           collection.keys()]

        self.repo.git_remove(files_to_remove)

        # update everything else to be safe
        files_to_add = []

        # collection level:
        collection.meta.serialize(opj(self.repo.path, REPO_STD_META_FILE),
                                  format="turtle")
        files_to_add.append(REPO_STD_META_FILE)

        # handles:
        for k, v in collection.iteritems():

            v.commit()
            # files_to_add.append(self.repo._key2filename(k))
            # Actually, this shouldn't be necessary, since it was
            # committed above. On the other hand, that's a lot of commits.
            # May be don't commit the handles but just write_to_file and commit
            # herein.

        self.repo.git_add(files_to_add)
        self.repo.git_commit(msg)

        if self.branch != current_branch:
            # switch back to repo's active branch on disk
            self.repo.git_checkout(current_branch)