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())]
def get_packages(root): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) proot = parent(root) + "/" py_files = [file.rsplit(proot)[1] for file in listfiles(root)] packages = list( np.unique([parent(file).replace("/", ".") for file in py_files])) # return list(np.unique([parent(file).replace("/", ".").split(".{name_root}.".format(name_root=name(root)))[1] # for file in py_files])) return packages
def copy_inits(py_package, in_pattern, out_pattern): """ :param py_package: :param so_package: :return: """ [ os.remove(_f) for _f in listfiles(os.path.realpath(py_package), patterns=[".pyc"]) ] init_files_src = listfiles(py_package, patterns=["__init__.py"]) init_files_dst = [ parent(file).replace(in_pattern, out_pattern).replace( "/{PACKAGE}/".format(PACKAGE=os.environ["PACKAGE"]), "/{PACKAGE}/".format( PACKAGE=os.environ["PACKAGE"].replace("-", "_"))) for file in init_files_src ] commands = [ "cp {} {}".format(src, dst) for src, dst in zip(init_files_src, init_files_dst) ] [print(command) for command in commands] [os.system(command) for command in commands]
def inference(model, input, transform, device="cuda"): input_image = Image.open(input) data = {'image': input_image} # data pre-processing data = transform(data) with torch.no_grad(): input_image_list = ImageList([data['image'].to(device)], image_sizes=[input_image.size[::-1]]) panoptic_result, _ = model.forward(input_image_list) semseg_logics = [o.to('cpu') for o in panoptic_result["semantic_segmentation_result"]] # Export the result output = input.replace("/data/", "/output/") os.makedirs(parent(output), exist_ok=True) assert os.path.exists(parent(output)) semseg_prob = [torch.argmax(semantic_logit, dim=0) for semantic_logit in semseg_logics] seg_vis = visualize_segmentation_image(semseg_prob[0], input_image, cityscapes_colormap_sky) Image.fromarray(seg_vis.astype('uint8')).save(output)
def get_packages(root): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) proot = parent(root) + "/" py_files = [ file.rsplit(proot)[1] for file in listfiles(root, patterns=[".py"]) if ((os.stat(file).st_size > 0) & (not file.__contains__(".pyc"))) ] packages = list( np.unique([parent(file).replace("/", ".") for file in py_files])) # return list(np.unique([parent(file).replace("/", ".").split(".{name_root}.".format(name_root=name(root)))[1] # for file in py_files])) return packages
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()
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)
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 copy_data(py_package, in_pattern, out_pattern): """ :param py_package: :param so_package: :return: """ data_files = listfiles(os.path.realpath(py_package), patterns=["__data__"]) output_dirs = [ parent(_f.replace(in_pattern, out_pattern)) for _f in data_files ] [os.makedirs(_output_dir, exist_ok=True) for _output_dir in output_dirs] commands = [ "cp {} {}".format(_f, _output_dir) for _f, _output_dir in zip(data_files, output_dirs) ] [print(command) for command in commands] [os.system(command) for command in commands]
def get_packages(self, root, namelib): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) files = listfiles(root, patterns=[".py"], excludes=[".pyc", "__pycache__"]) files = [f for f in files if os.stat(f).st_size > 0] py_files = [file.rsplit(self._parent_lib_root)[1] for file in files] modules = [ parent(file).replace("/", ".").split(f"{namelib}.") for file in py_files ] modules = [m for m in modules if len(m) == 2] return list(np.unique([m[1] for m in modules]))
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
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)
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
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')
def bin(binary): os.system(os.path.join(parent(os.path.abspath(__file__)), "bin/{}".format(binary)))
def get_current_module(self, f): return parent(f).split(self._parent_lib_root)[1].replace( "/", ".").split(".py")[0]
import sphinx_rtd_theme from sphinx.util.docfields import TypedField import os from gnutools.utils import listfiles, replace_dir from autosphinx import AutoSphinx from gnutools.utils import parent import time CURRENT_DIR = parent(os.path.abspath(__file__)) os.system("rm *.rst; rm -r doctrees; rm -r html") ASPHINX_NAMELIB = os.environ["ASPHINX_NAMELIB"] ASPHINX_VERSION = os.environ["ASPHINX_VERSION"] ASPHINX_LIBROOT = os.environ["ASPHINX_LIBROOT"] ASPHINX_LOGO = os.environ["ASPHINX_LOGO"] if not os.environ["ASPHINX_LOGO"]=="": ASPHINX_LOGO = os.environ["ASPHINX_LOGO"].split("/")[-1] replace_dir(f"{CURRENT_DIR}/img") os.system(f"cp {os.environ['ASPHINX_LOGO']} {CURRENT_DIR}/img") # print(os.environ["ASPHINX_LOGO"], "{current_dir}/img".format(current_dir=CURRENT_DIR)) # time.sleep(10) # sphinx = AutoSphinx(lib_root = os.environ["ASPHINX_LIBROOT"], version = os.environ["ASPHINX_VERSION"]) TypedField.make_field = sphinx.patched_make_field extensions = [ 'sphinx.ext.autodoc',