def rename_prefixe_lib(root):
    """
    Rename automatically the generated .so files to be consistent.

    :param root:
    :return:
    """
    files = listfiles(root, patterns=[".so"])
    files = [(file, "{}/{}.so".format(parent(file), name(name(file))))
             for file in files]
    for (file_before, file_after) in files:
        command = "mv {} {}".format(file_before, file_after)
        print(command)
        os.system(command)
Beispiel #2
0
 def __init__(self, lib_root, version, logo_img=""):
     os.environ["ASPHINX_LIBROOT"] = lib_root
     os.environ["ASPHINX_NAMELIB"] = name(lib_root)
     os.environ["ASPHINX_VERSION"] = version
     os.environ["ASPHINX_LOGO"] = logo_img
     os.environ[
         "PATH"] = f"{parent(os.sys.executable)}:{os.environ['PATH']}"
     self._name_lib = name(lib_root)
     self._lib_root = lib_root
     self._parent_lib_root = f"{parent(self._lib_root)}/"
     self._version = version
     self._current_dir = parent(os.path.abspath(__file__))
     self._module = None
     self._name_class = None
     self._name_function = None
def ext_modules(root, remove_c=True, filter_key=""):
    """
    Clean up the modules by removing undesired extensions default is ["c", "o", "pyc", "pyx"]
    Return a list if extension to use in setup.py

    :param root:
    :param remove_c: default True
    :return list:
    """
    from distutils.extension import Extension

    py_files = listfiles(root,
                         patterns=[".py"],
                         excludes=["/__init__.py", ".pyc"])
    modules_dict = {}
    proot = parent(root)
    for file in py_files:
        file = file.replace(proot, "")[1:]
        parent_modules = ".".join(parent(file).replace(
            "/", ".").split(".")).replace("-", "_")
        key = "{parent_modules}.{leaf_module}".format(
            parent_modules=parent_modules, leaf_module=name(file))
        if key.__contains__(filter_key):
            try:
                modules_dict[key].append(file)
            except:
                modules_dict[key] = [file]

    print(modules_dict)
    return [Extension(key, value) for key, value in list(modules_dict.items())]
Beispiel #4
0
    def generate_rst(self, pkg):
        """
        Scan a package and generate automatically a rst string to save as a rst file for documentation.

        :param root: package root
        :param package: package or module to generate the rst file
        :return string:
        """

        #Set the variables
        package = f"{self._lib_root}/{pkg.replace('.', '/')}"
        files = os.listdir(package)
        pyfiles = [f"{package}/{f}" for f in files if f[-3:] == ".py"]
        pyscripts = [PyscriptParser(file) for file in pyfiles]
        [
            s.update({
                "currentmodule": self.get_current_module(s._file),
                "currentfile": name(s._file)
            }) for s in pyscripts
        ]

        self.__output = f"{pyscripts[0]['currentmodule']}\n==============================================================\n\n"

        # Process each module sequentially
        [self.read_member_class(pyscript) for pyscript in pyscripts]
        [self.read_functions(pyscript) for pyscript in pyscripts]

        # Write the result
        print(
            f"{self.__output}\n============================================================================\n"
        )
        with open(f"{self._current_dir}/{self._name_lib}.{pkg}.rst", "w") as f:
            f.write(self.__output)
Beispiel #5
0
 def __init__(self, root, setup, version="1.0a28"):
     self.root = root
     self.setup = setup
     self.lib_name = name(root)
     self.curdir = parent(os.path.realpath(__file__))
     self.version = version
     self.exe = os.path.sys.executable
     os.environ["VERSION"] = self.version
     self.clean()
Beispiel #6
0
 def export(self, img, plyfile):
     # Load the files
     setmesh = NMesh(self.files['model'])
     setmesh.set_color([0, 200, 200])
     # Set the color
     deg = int(name(img).split("_")[0])
     theta = (deg/360)*2*pi
     setmesh.rotate(axis_rotation=2, theta=theta)
     setmesh.export(plyfile)
 def reload_annotations(dir, imgs, ann_file='annotation.txt'):
    try:
       assert os.path.exists(ann_file)
       my_case = name(dir)
       lines = open(ann_file, 'r').readlines()[::-1]
       lines = [line.split('\n')[0] for line in lines]
       for line in lines:
          case, img = line.split(",")
          if case==my_case:
             return int(np.argwhere(imgs==img).reshape(-1, )[0]), True
       return 0, False
Beispiel #8
0
    def __init__(self, root):
        super(ImageGenerator, self).__init__()
        def add_text(string, img):
            font = cv2.FONT_HERSHEY_SIMPLEX
            # org
            org = (50, 50)
            # fontScale
            fontScale = 1
            # Blue color in BGR
            color = (255, 255, 255)
            # Line thickness of 2 px
            thickness = 2
            # Using cv2.putText() method
            return cv2.putText(img, string, org, font, fontScale, color, thickness, cv2.LINE_AA)
        # Load the files
        self.files = dict([(name(file), file) for file in listfiles(root)])
        setmesh = NMesh(self.files['model'])
        # Set the color
        setmesh.set_color([0, 200, 200])

        _img0 = setmesh.shot()
        setmesh.rotate(axis_rotation=0, theta=pi)
        _img1 = setmesh.shot()
        _img1 = cv2.flip(_img1, 0)
        img_master0 = np.zeros((800, 800, 3), np.uint8)
        img_master0[200:-200, 200:-200] = 255 - _img0
        img_master1 = np.zeros((800, 800, 3), np.uint8)
        img_master1[200:-200, 200:-200] = 255 - _img1

        for theta in np.arange(0, 2*pi, 2*pi / 360):
            deg = int((theta / pi) * 180)
            img0 = self.rotateImage(img_master0, deg)
            img0 = add_text(name(root), img0)
            img1 = self.rotateImage(img_master1, deg)
            img = np.zeros((800, 1600, 3), np.uint8)
            img[:, :800] = img0
            img[:, 800:] = img1
            self.setdefault('{root}/{deg}_None.None'.format(root=root, deg=str(deg).zfill(3)), img)
def up_down_selection():
   while True:
      dir = os.path.join(args.root, cases[k])
      result, img = process_dir(dir,  ImageGenerator(dir))
      if result == __PREVIOUS_CASE__:
         k=k-1
         if k<0:
            k = len(cases)-1
      elif result==__NEXT_CASE__:
         k = k + 1
         # if k == len(cases):
         #    print('finished')
         #    break
      else:
         with open('annotation.txt', 'a') as f:
            f.write(",".join([name(parent(img)), img]) + '\n')
         print(img,' is validated')
         k = k + 1
         if k == len(cases):
            k = 0
Beispiel #10
0
    def click_and_crop(event, x, y, flags, param):
        global refPt, croping

        # grab references to the global variables
        # if the left mouse button was clicked, record the starting
        # (x, y) coordinates and indicate that cropping is being
        # performed
        if event == cv2.EVENT_LBUTTONDOWN:
            refPt = [(x, y)]
            cropping = True

        # check to see if the left mouse button was released
        elif event == cv2.EVENT_LBUTTONUP:
            # record the ending (x, y) coordinates and indicate that
            # the cropping operation is finished
            refPt.append((x, y))
            cropping = False
            with open('annotations.txt', 'a') as f:
                f.write('{name_file}\t{pts}\n'.format(name_file=name(
                    parent(file)),
                                                      pts=refPt))
            # draw a rectangle around the region of interest
            cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
            cv2.imshow("image", image)
         k = k + 1
         # if k == len(cases):
         #    print('finished')
         #    break
      else:
         with open('annotation.txt', 'a') as f:
            f.write(",".join([name(parent(img)), img]) + '\n')
         print(img,' is validated')
         k = k + 1
         if k == len(cases):
            k = 0

if __name__ == '__main__':
   parser = argparse.ArgumentParser(description='Annotate 3d files from 2d projected volumes')
   parser.add_argument('--root', default='samples')
   args = parser.parse_args()
   cases = set(os.listdir(args.root))
   cases_processed = set([line.split(",")[0] for line in open("annotation.txt", "r").readlines()])
   cases = cases.difference(cases_processed)
   cases = list(cases)
   k=0

   for k in tqdm(range(len(cases)), total=len(cases)):
      dir = os.path.join(args.root, cases[k])
      image_generator = ImageGenerator(dir)
      result, img = process_dir(dir,  image_generator)
      assert result==__VALIDATE_IMG__
      with open('annotation.txt', 'a') as f:
         image_generator.export(img, "samples/{name}.ply".format(name=name(parent(img))))
         f.write(",".join([name(parent(img)), img]) + '\n')
Beispiel #12
0
import sys
import subprocess
from gnutools.utils import listfiles, name
import random
argslist = list(sys.argv)[1:]
root_manifest = argslist[argslist.index('--root-manifest') + 1]
manifests = list(
    set([
        name(manifest).replace('train_', '').replace('val_', '')
        for manifest in listfiles(root_manifest, ['.json'])
    ]))
random.shuffle(manifests)
for manifest in manifests:
    argslist_k = argslist + ['--manifest', name(manifest), '--epochs', '1']
    print(argslist_k)
    p = subprocess.Popen([str(sys.executable)] + ['train.py'] + argslist_k)
    p.wait()
    if p.returncode != 0:
        raise subprocess.CalledProcessError(returncode=p.returncode,
                                            cmd=p.args)
def generate_rst(root, package):
    """
    Scan a package and generate automatically a rst string to save as a rst file for documentation.

    :param root: package root
    :param package: package or module to generate the rst file
    :return string:
    """
    root = os.path.realpath(root)
    package = os.path.realpath(package)
    proot = parent(root) + "/"

    modules = np.unique([
        file.replace(proot, "").replace("/", ".").replace("py", "")[:-1]
        for file in listfiles(package, level=1, patterns=[".py"])
    ])
    module_name = package.replace(proot, "").replace("/",
                                                     ".").replace(".py", "")
    output = "{}\n==============================================================\n\n".format(
        module_name)
    for module in modules:
        splits = module.split("__init__")
        if len(splits) == 2:
            path = "{}{}__init__.py".format(proot, splits[0].replace(".", "/"))
        else:
            path = "{}{}.py".format(proot, module.replace(".", "/"))
        with open(path, "r") as f:
            modules_dict = {}
            modules_dict[name(path)] = []
            members_class = {}
            functions = []
            lines = f.readlines()
            last = ""
            for line in lines:
                if ((line[:8] == "    def ") | (line[:6] == "class ")):
                    if line.__contains__("class "):
                        name_class = line.split("class ")[1].replace(
                            ":\n", "").split("(")[0].split("\n")[0]
                        modules_dict[name_class] = module
                        members_class[name_class] = []
                        last = "class"
                    else:
                        name_member_class = line.split("    def ")[1].split(
                            "(")[0]
                        if not name_member_class.__contains__("__"):
                            if last == "class":
                                members_class[name_class].append(
                                    name_member_class)
                                last = "class"
                elif line[:4] == "def ":
                    name_function = line.split("def ")[1].split("(")[0]
                    modules_dict[name_function] = module
                    functions.append(name_function)
                    last = "function"

            for name_class, class_value in members_class.items():
                output += ".. currentmodule:: {}\n\n".format(module_name)
                output += \
                    "{}\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" \
                    ".. autoclass:: {}\n" \
                    "    :members: {}\n" \
                    "    :special-members:\n\n".format(name_class, name_class, ", ".join(class_value))

            if len(functions) > 0:
                if not ext(module) == "__init__":
                    output += ".. currentmodule:: {}\n\n".format(module_name)
                    output += "{}\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n".format(
                        ext(module))
                else:
                    output += ".. currentmodule:: {}\n\n".format(module_name)
                for function in functions:
                    output += \
                        ".. autofunction:: {}\n".format(function)
            output += "\n"
    print(
        "{}\n============================================================================\n"
        .format(output))
    return output