Exemple #1
0
 def test_splitext(self):
     self.assertEqual(libmag.splitext("foo/bar/item.py"),
                      ("foo/bar/item", ".py"))
     self.assertEqual(libmag.splitext("item.py"), ("item", ".py"))
     self.assertEqual(libmag.splitext("foo/bar/item"), ("foo/bar/item", ""))
     self.assertEqual(libmag.splitext("foo/bar/item.file.ext"),
                      ("foo/bar/item.file", ".ext"))
Exemple #2
0
def read_sitk(path, dryrun=False):
    """Read an image file into :class:``sitk.Image`` format, checking for 
    alternative supported extensions if necessary.
    
    Args:
        path (str): Path, including prioritized extension to check first.
        dryrun (bool): True to load the image; defaults to False. Use False
            to test whether an path to load is found.
    
    Returns:
        :obj:`sitk.Image`, str: Image object located at ``path`` with
        the found extension, or None if unable to load; and the loaded path,
        or None if no matching, existing path is found. If a file at
        ``path`` cannot be found, its extension is replaced successively
        with remaining extensions in :const:``EXTS_3D`` until a file is found.
    
    """
    # prioritize given extension
    path_split = libmag.splitext(path)
    exts = list(EXTS_3D)
    if path_split[1] in exts: exts.remove(path_split[1])
    exts.insert(0, path_split[1])

    # attempt to load using each extension until found
    img_sitk = None
    path_loaded = None
    for ext in exts:
        img_path = path_split[0] + ext
        if os.path.exists(img_path):
            if not dryrun:
                print("Loading image with SimpleITK:", img_path)
                img_sitk = sitk.ReadImage(img_path)
            path_loaded = img_path
            break
    if not dryrun and img_sitk is None:
        print("could not find image from {} and extensions {}".format(
            path_split[0], exts))
    return img_sitk, path_loaded