Python os.path.splitext() Examples

The following are code examples for showing how to use os.path.splitext(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: GBVision   Author: GreenBlitz   File: recording_opencv_window.py    Apache License 2.0 15 votes vote down vote up
def __init__(self, window_name: str, file_name: str, fps=20.0, exit_button='qQ',
                 drawing_pipeline=EMPTY_PIPELINE, recording_pipeline=EMPTY_PIPELINE, width=0, height=0,
                 flags=cv2.WINDOW_FREERATIO):
        """
        initializes the stream window
        
        """
        OpenCVWindow.__init__(self, window_name, exit_button=exit_button, flags=flags)
        RecordingWindow.__init__(self, window_name=window_name, drawing_pipeline=drawing_pipeline,
                                 recording_pipeline=recording_pipeline)
        self.file_name = file_name

        _, file_ext = splitext(file_name)

        self.fourcc = cv2.VideoWriter_fourcc(*VIDEO_FILE_TYPE[file_ext.upper()])

        self.fps = fps

        self.video_writer = None
        self.width = width
        self.height = height 
Example 2
Project: PEAKachu   Author: tbischler   File: window.py    ISC License 6 votes vote down vote up
def init_libraries(self, paired_end, max_insert_size, ctr_libs,
                       exp_libs):
        self._paired_end = paired_end
        self._max_insert_size = max_insert_size
        self._ctr_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in ctr_libs]
        self._exp_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in exp_libs]
        # add libs to lib_dict
        for lib_file in exp_libs + ctr_libs:
            if not isfile(lib_file):
                sys.stderr.write("ERROR: The library file {} does not exist.\n"
                                 .format(lib_file))
                sys.exit(1)
            self._lib_dict[splitext(basename(lib_file))[0]] = Library(
                paired_end, max_insert_size, lib_file,
                deepcopy(self._replicon_dict))
        self._lib_names_list = list(self._lib_dict.keys())
        print("The following libraries were initialized:\n"
              "# Experiment libraries\n{0}\n"
              "# Control libraries\n{1}".format(
                  '\n'.join(self._exp_lib_list),
                  '\n'.join(self._ctr_lib_list))) 
Example 3
Project: PEAKachu   Author: tbischler   File: adaptive.py    ISC License 6 votes vote down vote up
def init_libraries(self, paired_end, max_insert_size, ctr_libs,
                       exp_libs):
        self._paired_end = paired_end
        self._max_insert_size = max_insert_size
        self._ctr_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in ctr_libs]
        self._exp_lib_list = [splitext(basename(lib_file))[0]
                              for lib_file in exp_libs]
        # add libs to lib_dict
        for lib_file in exp_libs + ctr_libs:
            if not isfile(lib_file):
                sys.stderr.write(
                    "ERROR: The library file {} does not exist.\n".format(
                        lib_file))
                sys.exit(1)
            self._lib_dict[splitext(basename(lib_file))[0]] = Library(
                paired_end, max_insert_size, lib_file,
                deepcopy(self._replicon_dict))
        self._lib_names_list = list(self._lib_dict.keys())
        print("The following libraries were initialized:\n"
              "# Experiment libraries\n{0}\n"
              "# Control libraries\n{1}".format(
                  '\n'.join(self._exp_lib_list),
                  '\n'.join(self._ctr_lib_list))) 
Example 4
Project: flasky   Author: RoseOu   File: _compat.py    MIT License 6 votes vote down vote up
def _check_if_pyc(fname):
    """Return True if the extension is .pyc, False if .py
    and None if otherwise"""
    from imp import find_module
    from os.path import realpath, dirname, basename, splitext

    # Normalize the file-path for the find_module()
    filepath = realpath(fname)
    dirpath = dirname(filepath)
    module_name = splitext(basename(filepath))[0]

    # Validate and fetch
    try:
        fileobj, fullpath, (_, _, pytype) = find_module(module_name, [dirpath])
    except ImportError:
        raise IOError("Cannot find config file. "
                      "Path maybe incorrect! : {0}".format(filepath))
    return pytype, fileobj, fullpath 
Example 5
Project: python-sii   Author: voipir   File: SectionEmitter.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _logo_template(self):
        if self._logo_path:
            _, ext = path.splitext(self._logo_path)

            height    = '15mm'
            width     = '0.7\\textwidth'
            keepratio = 'true'

            return '\\includegraphics[height={h}, width={w}, keepaspectratio={r}]{{logo{ext}}}'.format(
                h   = height,
                w   = width,
                r   = keepratio,
                ext = ext
            )
        else:
            return '' 
Example 6
Project: rowgenerators   Author: Metatab   File: util.py    MIT License 6 votes vote down vote up
def file_ext(v):
    """Split of the extension of a filename, without throwing an exception of there is no extension. Does not
    return the leading '.'
    :param v: """

    from os.path import splitext

    if not v:
        return None

    try:
        try:
            v = splitext(v)[1][1:]
        except TypeError:
            v = splitext(v.fspath)[1][1:]

        if v == '*':  # Not a file name, probably a fragment regex
            return None

        return v.lower() if v else None
    except IndexError:
        return None 
Example 7
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 6 votes vote down vote up
def importee_dir(self, importer_dir, suite_import, check_alternate=True):
        suitename = suite_import.name
        if suitename in self.suitenamemap:
            suitename = self.suitenamemap[suitename]

        # Try use the URL first so that a big repo is cloned to a local
        # directory whose named is based on the repo instead of a suite
        # nested in the big repo.
        base = None
        for urlinfo in suite_import.urlinfos:
            if urlinfo.abs_kind() == 'source':
                # 'https://github.com/graalvm/graal.git' -> 'graal'
                base, _ = os.path.splitext(basename(_urllib_parse.urlparse(urlinfo.url).path))
                if base: break
        if base:
            path = join(SiblingSuiteModel.siblings_dir(importer_dir), base)
        else:
            path = join(SiblingSuiteModel.siblings_dir(importer_dir), suitename)
        checked = self._check_exists(suite_import, path, check_alternate)
        return SuiteModel._checked_to_importee_tuple(checked, suite_import) 
Example 8
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 6 votes vote down vote up
def get_file_extension(path):
    root, ext = os.path.splitext(path)
    if len(ext) > 0:
        ext = ext[1:]  # remove leading .
    if ext in _tar_compressed_extensions and os.path.splitext(root)[1] == ".tar":
        return "tar." + ext
    if ext == 'zip':
        _, pre_ext = os.path.splitext(root)
        if len(pre_ext) > 0:
            pre_ext = pre_ext[1:]  # remove leading .
        if pre_ext in _known_zip_pre_extensions:
            return pre_ext + ".zip"
    if ext == 'map':
        _, pre_ext = os.path.splitext(root)
        if len(pre_ext) > 0:
            pre_ext = pre_ext[1:]  # remove leading .
            return pre_ext + ".map"
    return ext 
Example 9
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 6 votes vote down vote up
def suite_ci_files(suite, ci_path=None, extension=(".hocon", ".jsonnet", '.libsonnet')):
    """
    Get the list of ci files for the given suite

    :param suite: SourceSuite
    :param ci_path: str or None
    :param extension: str | tuple[str] | list[str] | set[str]
    :return:
    """
    assert isinstance(suite, SourceSuite), "suite must be a SourceSuite"
    assert extension is not None, "extension cannot be None, must be a string or iterable over strings like '.ext'."
    if isinstance(extension, str):
        extension = [extension]
    extension = set(extension)

    ci_files = os.listdir(join(suite.dir, ci_path)) if ci_path else os.listdir(suite.dir)
    return [join(ci_path, name) if ci_path else name
            for name in ci_files
            if os.path.splitext(name)[-1] in extension] 
Example 10
Project: tldr.py   Author: lord63   File: cli.py    MIT License 6 votes vote down vote up
def build_index():
    repo_directory = get_config()['repo_directory']
    index_path = path.join(repo_directory, 'pages', 'index.json')
    page_path = path.join(repo_directory, 'pages')

    tree_generator = os.walk(page_path)
    folders = next(tree_generator)[1]
    commands, new_index = {}, {}
    for folder in folders:
        pages = next(tree_generator)[2]
        for page in pages:
            command_name = path.splitext(page)[0]
            if command_name not in commands:
                commands[command_name] = {'name': command_name,
                                          'platform': [folder]}
            else:
                commands[command_name]['platform'].append(folder)
    command_list = [item[1] for item in
                    sorted(commands.items(), key=itemgetter(0))]
    new_index['commands'] = command_list

    with open(index_path, mode='w') as f:
        json.dump(new_index, f) 
Example 11
Project: Sigmedia-AVSR   Author: georgesterpu   File: extract_faces.py    GNU General Public License v3.0 6 votes vote down vote up
def main(argv):
    r'''
    Multithreaded face-alignment using OpenFace
    '''

    num_threads = int(argv[1]) if len(argv) > 1 else 1

    openface_bin = '/run/media/john_tukey/download/software/OpenFace/build/bin/FeatureExtraction'
    out_dir = '/run/media/john_tukey/download/datasets/tcdtimit/aligned_openface/'
    makedirs(out_dir, exist_ok=True)

    dataset_dir = '/run/media/john_tukey/download/datasets/adapt/work/tcdtimit/'
    train_list = './datasets/tcdtimit/splits/speaker-dependent/train.scp'
    test_list = './datasets/tcdtimit/splits/speaker-dependent/test.scp'

    train = get_files(train_list, dataset_dir)
    test = get_files(test_list, dataset_dir)

    label_map = dict()
    for file in train + test:
        label_map[file] = path.splitext(file.split('tcdtimit/')[-1])[0]

    with Pool(num_threads) as p:
        p.starmap(process_one_batch,
              zip(chunks(train+test, 1000), repeat(openface_bin), repeat(label_map), repeat(out_dir))) 
Example 12
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: datasets.py    MIT License 6 votes vote down vote up
def __getitem__(self, index):
        datafiles = self.files[index]
        image = cv2.imread(datafiles["img"], cv2.IMREAD_COLOR)
        size = image.shape
        name = osp.splitext(osp.basename(datafiles["img"]))[0]
        image = np.asarray(image, np.float32)
        image -= self.mean
        
        img_h, img_w, _ = image.shape
        pad_h = max(self.crop_h - img_h, 0)
        pad_w = max(self.crop_w - img_w, 0)
        if pad_h > 0 or pad_w > 0:
            image = cv2.copyMakeBorder(image, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT, 
                value=(0.0, 0.0, 0.0))
        image = image.transpose((2, 0, 1))
        return image, name, size 
Example 13
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: datasets.py    MIT License 6 votes vote down vote up
def __init__(self, root, list_path, crop_size=(505, 505), mean=(128, 128, 128)):
        self.root = root
        self.list_path = list_path
        self.crop_h, self.crop_w = crop_size
        self.mean = mean
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.img_ids = [i_id.strip().split() for i_id in open(list_path)]
        self.files = [] 
        # for split in ["train", "trainval", "val"]:
        for item in self.img_ids:
            image_path, label_path = item
            name = osp.splitext(osp.basename(label_path))[0]
            img_file = osp.join(self.root, image_path)
            self.files.append({
                "img": img_file
            }) 
Example 14
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: datasets.py    MIT License 6 votes vote down vote up
def __getitem__(self, index):
        datafiles = self.files[index]
        image = cv2.imread(datafiles["img"], cv2.IMREAD_COLOR)
        size = image.shape
        name = osp.splitext(osp.basename(datafiles["img"]))[0]
        image = np.asarray(image, np.float32)
        image -= self.mean
        
        img_h, img_w, _ = image.shape
        pad_h = max(self.crop_h - img_h, 0)
        pad_w = max(self.crop_w - img_w, 0)
        if pad_h > 0 or pad_w > 0:
            image = cv2.copyMakeBorder(image, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT, 
                value=(0.0, 0.0, 0.0))
        image = image.transpose((2, 0, 1))
        return image, name, size 
Example 15
Project: pyqt5downloader   Author: mostafaabobakr7   File: index.py    GNU General Public License v3.0 6 votes vote down vote up
def search(self):
        try:
            # trying to get url info
            # first get pasted url from lineEdit_4
            mainurl = ur.urlopen(self.lineEdit_4.text())
            # use humanize lib to get size in MB from url.info()
            # note that (mainurl.info()['Content-Length']) because it is a dictionary
            file_size_mb = humanize.naturalsize(mainurl.info()['Content-Length'])
            self.textBrowser.setText(file_size_mb)
            # display file Name and type from a url
            disassembled = urlparse(self.lineEdit_4.text())
            filename, file_ext = splitext(basename(disassembled.path))
            name = urllib.parse.unquote(filename)
            self.textBrowser_5.setText(name + file_ext)
            self.textBrowser_2.setText(file_ext)
        except Exception:
            pass
        QApplication.processEvents() 
Example 16
Project: exoduscli   Author: cthlo   File: main.py    MIT License 6 votes vote down vote up
def _run_exodus(*args):
    '''Run a Exodus using runpy
    '''
    fullpath = path.join(config.addonsdir, config.exodus['id'], config.exodus['entryfile'])
    entrypath, entryfile = path.split(fullpath)
    if entrypath not in sys.path:
        sys.path.insert(0, entrypath)
    module, _ = path.splitext(entryfile)

    # Exodus assumes thread names start at 1
    # and gets provider names from the thread name
    c = threading._counter = count().next
    c() # consume 0

    sys.argv = list(args)
    # note: cannot use __import__ because Exodus spawns threads that do import
    # so that would cause deadlock
    old_modules = set(sys.modules.keys()) # unload the newly added modules after runpy
    runpy.run_module(module)
    for k in set(sys.modules.keys()) - old_modules:
        del sys.modules[k] 
Example 17
Project: smaslennikov.github.io   Author: smaslennikov   File: mpdnp.py    MIT License 6 votes vote down vote up
def np(data, buffer, args):
  """
    Send information about the currently
    played song to the channel.
  """
  host = wc.config_get_plugin("host")
  port = int(wc.config_get_plugin("port"))
  cont = mpd.MpdController(host=host, port=port)
  song = cont.getCurrentSong()
  pos, length, pct = cont.getSongPosition()
  
  # insert artist, title, album, track, path
  d = song.__dict__
  d.update({
      "title_or_file": song.title or splitext(basename(song.path))[0],
      "pos_sec": "%02d" % (pos / 60),
      "pos_min": str(pos / 60),
      "length_sec": "%02d" % (length % 60),
      "length_min": str(length / 60),
      "pct": "%2.0f" % pct,
  })
  
  wc.command(wc.current_buffer(), (subst(wc.config_get_plugin("format"), d)))
  return 0 
Example 18
Project: smaslennikov.github.io   Author: smaslennikov   File: mpdnp.py    MIT License 6 votes vote down vote up
def np(data, buffer, args):
  """
    Send information about the currently
    played song to the channel.
  """
  host = wc.config_get_plugin("host")
  port = int(wc.config_get_plugin("port"))
  cont = mpd.MpdController(host=host, port=port)
  song = cont.getCurrentSong()
  pos, length, pct = cont.getSongPosition()
  
  # insert artist, title, album, track, path
  d = song.__dict__
  d.update({
      "title_or_file": song.title or splitext(basename(song.path))[0],
      "pos_sec": "%02d" % (pos / 60),
      "pos_min": str(pos / 60),
      "length_sec": "%02d" % (length % 60),
      "length_min": str(length / 60),
      "pct": "%2.0f" % pct,
  })
  
  wc.command(wc.current_buffer(), (subst(wc.config_get_plugin("format"), d)))
  return 0 
Example 19
Project: seamseg   Author: mapillary   File: dataset.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, in_dir, transform):
        super(ISSTestDataset, self).__init__()
        self.in_dir = in_dir
        self.transform = transform

        # Find all images
        self._images = []
        for img_path in chain(
                *(glob.iglob(path.join(self.in_dir, '**', ext), recursive=True) for ext in ISSTestDataset._EXTENSIONS)):
            _, name_with_ext = path.split(img_path)
            idx, _ = path.splitext(name_with_ext)

            with Image.open(img_path) as img_raw:
                size = (img_raw.size[1], img_raw.size[0])

            self._images.append({
                "idx": idx,
                "path": img_path,
                "size": size,
            }) 
Example 20
Project: seamseg   Author: mapillary   File: test_panoptic.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_prediction_raw(raw_pred, _, img_info, out_dir):
    # Prepare folders and paths
    folder, img_name = path.split(img_info["rel_path"])
    img_name, _ = path.splitext(img_name)
    out_dir = path.join(out_dir, folder)
    ensure_dir(out_dir)
    out_path = path.join(out_dir, img_name + ".pth.tar")

    out_data = {
        "sem_pred": raw_pred[0],
        "bbx_pred": raw_pred[1],
        "cls_pred": raw_pred[2],
        "obj_pred": raw_pred[3],
        "msk_pred": raw_pred[4]
    }
    torch.save(out_data, out_path) 
Example 21
Project: straditize   Author: Chilipp   File: menu_actions.py    GNU General Public License v3.0 6 votes vote down vote up
def _export(self):
        fname = self.txt_fname.text()
        ending = osp.splitext(fname)[1]
        self.stradi.set_attr('exported', str(dt.datetime.now()))
        meta = self.stradi.valid_attrs
        if ending in ['.xls', '.xlsx']:
            with pd.ExcelWriter(fname) as writer:
                self.df.to_excel(writer, 'Data')
                if self.cb_include_meta.isChecked() and len(meta):
                    meta.to_excel(writer, 'Metadata', header=False)
        else:
            with open(fname, 'w') as f:
                if self.cb_include_meta.isChecked():
                    for t in meta.iloc[:, 0].items():
                        f.write('# %s: %s\n' % t)
            self.df.to_csv(fname, mode='a')
        self.accept() 
Example 22
Project: straditize   Author: Chilipp   File: menu_actions.py    GNU General Public License v3.0 6 votes vote down vote up
def _start_directory(self):
        def check_current():
            dirname = osp.dirname(current)
            if osp.exists(dirname) and osp.isdir(dirname):
                return dirname
        if self.straditizer is not None:
            current = None
            for attr in 'project_file', 'image_file':
                try:
                    current = self.straditizer.get_attr(attr)
                except KeyError:
                    pass
                else:
                    start = check_current()
                    if start is not None:
                        break
            if current:
                return osp.splitext(current)[0]
        return os.getcwd() 
Example 23
Project: YAPyPy   Author: Xython   File: py_compile.py    MIT License 6 votes vote down vote up
def py_compile(node, filename='<unknown>', is_entrypoint=False):
    if isinstance(node, Tag):
        ctx = _non_ctx.enter_new(node.tag)
        ctx.bc.filename = filename
        ctx.bc.name = '__main__' if is_entrypoint else splitext(
            Path(filename).relative())[0]
        try:
            py_emit(node.it, ctx)
        except SyntaxError as exc:
            exc.filename = filename
            raise exc
        return ctx.bc.to_code()
        # try:
        #     return ctx.bc.to_code()
        # except Exception as e:
        #     dump_bytecode(ctx.bc)
        #     raise e
    else:
        tag = to_tagged_ast(node)
        return py_compile(tag, filename, is_entrypoint=is_entrypoint) 
Example 24
Project: platzi-hello-gae   Author: xertica-cloud   File: fileadmin.py    GNU General Public License v2.0 6 votes vote down vote up
def is_file_allowed(self, filename):
        """
            Verify if file can be uploaded.

            Override to customize behavior.

            :param filename:
                Source file name
        """
        ext = op.splitext(filename)[1].lower()

        if ext.startswith('.'):
            ext = ext[1:]

        if self.allowed_extensions and ext not in self.allowed_extensions:
            return False

        return True 
Example 25
Project: PEAKachu   Author: tbischler   File: library.py    ISC License 5 votes vote down vote up
def __init__(self, paired_end, max_insert_size, bam_file, replicon_dict):
        self.paired_end = paired_end
        self.bam_file = bam_file
        self.max_insert_size = max_insert_size
        self.lib_name = splitext(basename(bam_file))[0]
        self.replicon_dict = replicon_dict 
Example 26
Project: python-sii   Author: voipir   File: SectionEmitter.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def resources(self):
        ress = []

        if self._logo_path:
            _, ext = path.splitext(self._logo_path)
            ress.append(Resource('logo' + ext, self._logo_data))

        return ress 
Example 27
Project: BDCN   Author: wandoucao   File: ablation_test.py    MIT License 5 votes vote down vote up
def test(model, args):
    test_root = cfg.config_test[args.dataset]['data_root']
    test_lst = cfg.config_test[args.dataset]['data_lst']
    test_name_lst = os.path.join(test_root, test_lst)

    if 'Multicue' in args.dataset:
        test_lst = test_lst % args.k

    mean_bgr = np.array(cfg.config_test[args.dataset]['mean_bgr'])

    test_img = Data(test_root, test_lst, 0.5, mean_bgr=mean_bgr)
    testloader = torch.utils.data.DataLoader(
        test_img, batch_size=1, shuffle=False, num_workers=8)
    lst = np.loadtxt(test_name_lst, dtype=str)[:, 0]
    nm = [osp.splitext(osp.split(x)[-1])[0] for x in lst]
    save_dir = args.res_dir
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    if args.cuda:
        model.cuda()
    model.eval()
    data_iter = iter(testloader)
    iter_per_epoch = len(testloader)
    start_time = time.time()
    all_t = 0
    for i, (data, _) in enumerate(testloader):
        if args.cuda:
            data = data.cuda()
        data = Variable(data, volatile=True)
        t1 = time.time()
        out = model(data)
        t = F.sigmoid(out[-1]).cpu().data.numpy()[0, 0, :, :]
        if not os.path.exists(os.path.join(save_dir, 'fuse')):
            os.mkdir(os.path.join(save_dir, 'fuse'))
        cv2.imwrite(os.path.join(save_dir, 'fuse', '%s.jpg'%nm[i]), 255-t*255)
        all_t += time.time() - t1

    print(all_t)
    print('Overall Time use: ', time.time() - start_time) 
Example 28
Project: MetrixReloaded   Author: Scrounger   File: MetrixReloadedCover.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def findCover(self, path):
		fpath = p1 = p2 = p3 = ""
		name, ext = os_path.splitext(path)
		ext = ext.lower()

		if os_path.isfile(path):
			dir = os_path.dirname(path)
			p1 = name
			p2 = os_path.join(dir, os_path.basename(dir))

		elif os_path.isdir(path):
			if path.lower().endswith("/bdmv"):
				dir = path[:-5]
				if dir.lower().endswith("/brd"): dir = dir[:-4]
			elif path.lower().endswith("video_ts"):
				dir = path[:-9]
				if dir.lower().endswith("/dvd"): dir = dir[:-4]
			else:
				dir = path
				p2 = os_path.join(dir, "folder")

			prtdir, dirname = os_path.split(dir)
			p1 = os_path.join(dir, dirname)
			p3 = os_path.join(prtdir, dirname)

		pathes = (p1, p2, p3)
		for p in pathes:
			for ext in self.exts:
				path = p + ext
				if os_path.exists(path): break
			if os_path.exists(path):
				fpath = path
				break
		return (p1, fpath) 
Example 29
Project: mx   Author: graalvm   File: mx.py    GNU General Public License v2.0 5 votes vote down vote up
def _find_packages(project, onlyPublic=True, included=None, excluded=None):
    """
    Finds the set of packages defined by a project.

    :param JavaProject project: the Java project to process
    :param bool onlyPublic: specifies if only packages containing a ``package-info.java`` file are to be considered
    :param set included: if not None or empty, only consider packages in this set
    :param set excluded: if not None or empty, do not consider packages in this set
    """
    sourceDirs = project.source_dirs()
    def is_visible(folder, names):
        for name in names:
            if onlyPublic:
                if name == 'package-info.java':
                    return True
            elif name.endswith('.java'):
                pubClassPattern = re.compile(r"^public\s+((abstract|final)\s+)?(class|(@)?interface|enum)\s*" + splitext(name)[0] + r"\W.*", re.MULTILINE)
                with open(join(folder, name)) as f:
                    for l in f.readlines():
                        if pubClassPattern.match(l):
                            return True
        return False
    packages = set()
    for sourceDir in sourceDirs:
        for root, _, files in os.walk(sourceDir):
            if is_visible(root, files):
                package = root[len(sourceDir) + 1:].replace(os.sep, '.')
                if not included or package in included:
                    if not excluded or package not in excluded:
                        packages.add(package)
    return packages 
Example 30
Project: PyTeCK   Author: pr-omethe-us   File: eval_model.py    MIT License 5 votes vote down vote up
def create_simulations(dataset, properties):
    """Set up individual simulations for each ignition delay value.

    Parameters
    ----------
    dataset :

    properties : pyked.chemked.ChemKED
        ChemKED object with full set of experimental properties

    Returns
    -------
    simulations : list
        List of :class:`Simulation` objects for each simulation

    """

    simulations = []
    for idx, case in enumerate(properties.datapoints):
        sim_meta = {}
        # Common metadata
        sim_meta['data-file'] = dataset
        sim_meta['id'] = splitext(basename(dataset))[0] + '_' + str(idx)

        simulations.append(Simulation(properties.experiment_type,
                                      properties.apparatus.kind,
                                      sim_meta,
                                      case
                                      )
                           )
    return simulations 
Example 31
Project: openhatch   Author: campbe13   File: genspider.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _list_templates(self):
        print "Available templates:"
        for filename in sorted(os.listdir(self.templates_dir)):
            if filename.endswith('.tmpl'):
                print "  %s" % splitext(filename)[0] 
Example 32
Project: openhatch   Author: campbe13   File: eggstorage.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def list(self, project):
        eggdir = path.join(self.basedir, project)
        versions = [path.splitext(path.basename(x))[0] \
            for x in glob("%s/*.egg" % eggdir)]
        return sorted(versions, key=LooseVersion) 
Example 33
Project: gitautopush   Author: choldgraf   File: gitautopush.py    MIT License 5 votes vote down vote up
def _is_ipynb(path):
    return op.splitext(path)[-1] == '.ipynb' 
Example 34
Project: ICDAR-2019-SROIE   Author: zzzDavid   File: my_data.py    MIT License 5 votes vote down vote up
def get_files(data_path="data/"):
    json_files = sorted(
        (f for f in os.scandir(data_path) if f.name.endswith(".json")), key=lambda f: f.path
    )
    txt_files = sorted(
        (f for f in os.scandir(data_path) if f.name.endswith(".txt")), key=lambda f: f.path
    )

    assert len(json_files) == len(txt_files)
    for f1, f2 in zip(json_files, txt_files):
        assert path.splitext(f1)[0] == path.splitext(f2)[0]

    return json_files, txt_files 
Example 35
Project: ICDAR-2019-SROIE   Author: zzzDavid   File: my_data.py    MIT License 5 votes vote down vote up
def create_data(data_path="tmp/data/"):
    raise DeprecationWarning

    json_files, txt_files = get_files(data_path)
    keys = [path.splitext(f.name)[0] for f in json_files]

    data_dict = {}

    for key, json_file, txt_file in zip(keys, json_files, txt_files):
        with open(json_file, "r", encoding="utf-8") as json_opend:
            key_info = json.load(json_opend)

        text = sort_text(txt_file)
        text_space = regex.sub(r"[\t\n]", " ", text)

        text_class = numpy.zeros(len(text), dtype=int)

        for i, k in enumerate(iter(key_info)):
            if k == "total":
                continue

            v = key_info[k]
            if not v in text_space:
                s = None
                e = 0
                while s is None and e < 3:
                    e += 1
                    s = regex.search("(" + v + "){e<=" + str(e) + "}", text_space)
                v = s[0]

            pos = text_space.find(v)
            text_class[pos : pos + len(v)] = i + 1

        data_dict[key] = (text, text_class)

        print(txt_file.path)
        color_print(text, text_class)

    return keys, data_dict 
Example 36
Project: neural-style-docker   Author: albarji   File: utils.py    MIT License 5 votes vote down vote up
def filename(path):
    """Given a full path to a file, returns just its name, without path or extension"""
    return splitext(basename(path))[0] 
Example 37
Project: neural-style-docker   Author: albarji   File: utils.py    MIT License 5 votes vote down vote up
def fileext(path):
    """Given a full path to a file, returns just its extension"""
    return splitext(basename(path))[1] 
Example 38
Project: Sigmedia-AVSR   Author: georgesterpu   File: files.py    GNU General Public License v3.0 5 votes vote down vote up
def read_sentence_labels(filename, unit='viseme'):
    r"""Finds the labels associated with a sentence
    in a .mlf label file
    Parameters
    ----------
    filename : `str`
    unit : `viseme` or `phoneme`
        The modeled speech unit

    Returns
    -------
    label_seq : `list`
        A list of label symbols
    """
    file = path.splitext(path.split(filename)[1])[0]

    if unit == 'viseme':
        transcript = viseme_file
    elif unit == 'phoneme':
        transcript = phoneme_file
    elif unit == 'character':
        transcript = character_file
    else:
        raise Exception('only `viseme`, `phoneme` and `character` unit transcriptions are supported')

    with open(transcript, 'r') as f:
        contents = f.read()

    # start = contents.find(file)
    # end = contents.find('.\n', start)
    # sentence_transcript = contents[start:end].splitlines()[1:]
    #
    # label_seq = [item.split()[-1] for item in sentence_transcript]
    # return label_seq
    return _get_transcript_from_buffer(contents, file) 
Example 39
Project: Sigmedia-AVSR   Author: georgesterpu   File: files.py    GNU General Public License v3.0 5 votes vote down vote up
def read_all_sentences_labels(filenames, unit='viseme'):
    r"""
    Multi-file version of `read_sentence_labels`
    which prevents reading the ground truth file multiple times
    Parameters
    ----------
    filenames
    unit

    Returns
    -------

    """

    if unit == 'viseme':
        transcript = viseme_file
    elif unit == 'phoneme':
        transcript = phoneme_file
    elif unit == 'character':
        transcript = character_file
    else:
        raise Exception('only `viseme`, `phoneme` and `character` unit transcriptions are supported')

    with open(transcript, 'r') as f:
        contents = f.read()

    labels = {}
    for filename in filenames:
        file = path.splitext(path.split(filename)[1])[0]
        labels[filename] = _get_transcript_from_buffer(contents, file)

    return labels 
Example 40
Project: Sigmedia-AVSR   Author: georgesterpu   File: dataset_writer.py    GNU General Public License v3.0 5 votes vote down vote up
def _remove_extensions(filenames):
    return [path.splitext(file)[0] for file in filenames] 
Example 41
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: datasets.py    MIT License 5 votes vote down vote up
def __init__(self, root, list_path, max_iters=None, crop_size=(321, 321), mean=(128, 128, 128), scale=True, mirror=True, ignore_label=255):
        self.root = root
        self.list_path = list_path
        self.crop_h, self.crop_w = crop_size
        self.scale = scale
        self.ignore_label = ignore_label
        self.mean = mean
        self.is_mirror = mirror
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.img_ids = [i_id.strip().split() for i_id in open(list_path)]
        if not max_iters==None:
                self.img_ids = self.img_ids * int(np.ceil(float(max_iters) / len(self.img_ids)))
        self.files = []
        # for split in ["train", "trainval", "val"]:
        for item in self.img_ids:
            image_path, label_path = item
            name = osp.splitext(osp.basename(label_path))[0]
            img_file = osp.join(self.root, image_path)
            label_file = osp.join(self.root, label_path)
            self.files.append({
                "img": img_file,
                "label": label_file,
                "name": name
            })
        self.id_to_trainid = {-1: ignore_label, 0: ignore_label, 1: ignore_label, 2: ignore_label,
                              3: ignore_label, 4: ignore_label, 5: ignore_label, 6: ignore_label,
                              7: 0, 8: 1, 9: ignore_label, 10: ignore_label, 11: 2, 12: 3, 13: 4,
                              14: ignore_label, 15: ignore_label, 16: ignore_label, 17: 5,
                              18: ignore_label, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14,
                              28: 15, 29: ignore_label, 30: ignore_label, 31: 16, 32: 17, 33: 18}
        print('{} images are loaded!'.format(len(self.img_ids))) 
Example 42
Project: pytorch-segmentation-toolbox   Author: speedinghzl   File: datasets.py    MIT License 5 votes vote down vote up
def __init__(self, root, list_path, crop_size=(505, 505)):
        self.root = root
        self.list_path = list_path
        self.crop_h, self.crop_w = crop_size
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.img_ids = [i_id.strip().split()[0] for i_id in open(list_path)]
        self.files = [] 
        # for split in ["train", "trainval", "val"]:
        for image_path in self.img_ids:
            name = osp.splitext(osp.basename(image_path))[0]
            img_file = osp.join(self.root, image_path)
            self.files.append({
                "img": img_file
            }) 
Example 43
Project: selfmailbot   Author: f213   File: helpers.py    MIT License 5 votes vote down vote up
def get_file(file: telegram.File) -> BytesIO:
    attachment = BytesIO()
    attachment.name = str(uuid.uuid4()) + '.' + path.splitext(file.file_path)[1]

    file.download(out=attachment)
    attachment.seek(0, 0)

    return attachment 
Example 44
Project: gpfsapi-examples   Author: arcapix   File: type_sizes_piechart.py    MIT License 5 votes vote down vote up
def type_sizes(file_list):
    c = Counter()

    for f in file_list:
        c.update({splitext(f.name): f.filesize})

    return c 
Example 45
Project: openstack-ops-middleware   Author: sapcc   File: statsd.py    Apache License 2.0 5 votes vote down vote up
def process_response(self, start, environ, response_wrapper,
                         exception=None):
        self.client.increment('responses_total')

        status = response_wrapper.get('status')
        if status:
            status_code = status.split()[0]
        else:
            status_code = 'none'

        method = environ['REQUEST_METHOD']

        # cleanse request path
        path = urlparse.urlparse(environ['SCRIPT_NAME'] +
                                 environ['PATH_INFO']).path
        # strip extensions
        path = splitext(path)[0]

        # replace parts of the path with constants based on strategy
        path = self.replace_strategy.apply(path)

        parts = path.rstrip('\/').split('/')
        if exception:
            parts.append(exception.__class__.__name__)
        api = '/'.join(parts)

        self.client.timing('latency_by_api',
                           time.time() - start,
                           tags=[
                               'method:%s' % method,
                               'api:%s' % api
                           ])

        self.client.increment('responses_by_api',
                              tags=[
                                  'method:%s' % method,
                                  'api:%s' % api,
                                  'status:%s' % status_code
                              ]) 
Example 46
Project: arriz   Author: MycroftAI   File: arriz.py    Apache License 2.0 5 votes vote down vote up
def write_image(cls, filename, arr):
        """Write png or jpg image"""
        ext = splitext(filename)[1]
        if ext not in ['.png', '.jpg', '.jpeg']:
            filename += '.png'
        try:
            from PIL import Image
        except ImportError:
            raise ValueError('Install pillow to use write_png! (pip install pillow)')
        arr = np.rot90(cls._normalize_arr(arr), 1)
        px_arr = np.array([[list(_calc_color(val)) for val in row] for row in arr], 'uint8')
        im = Image.fromarray(px_arr, 'RGB')
        im.save(filename) 
Example 47
Project: code-base-investigator   Author: intel   File: util.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def ensure_ext(fname, extensions):
    """Return true if the path passed in has specified extension"""
    if not isinstance(extensions, Iterable):
        extensions = [extensions]

    split = splitext(fname)
    return len(split) == 2 and split[1].lower() in extensions 
Example 48
Project: code-base-investigator   Author: intel   File: file_parser.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, _filename):
        self._filename = _filename
        self.full_line = ''

        split = splitext(_filename)
        if len(split) == 2:
            self._file_extension = split[1].lower()
        else:
            self._file_extension = None 
Example 49
Project: me-ica   Author: ME-ICA   File: mghformat.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def filespec_to_file_map(klass, filespec):
        """ Check for compressed .mgz format, then .mgh format """
        if splitext(filespec)[1] == '.mgz':
            return dict(image=FileHolder(filename=filespec))
        return super(MGHImage, klass).filespec_to_file_map(filespec) 
Example 50
Project: ncm2   Author: ncm2   File: ncm2_core.py    MIT License 5 votes vote down vote up
def load_subscope_detectors(self, paths):
        new_scope = False

        # auto find scopers
        for py in paths:
            if py in self._loaded_plugins:
                continue
            self._loaded_plugins[py] = True

            try:
                mod = path.splitext(path.basename(py))[0]
                mod = "ncm2_subscope_detector.%s" % mod
                m = import_module(mod)
            except Exception as ex:
                logger.exception('importing scoper <%s> failed', py)
                continue

            sd = m.SubscopeDetector(self.nvim)

            for scope in sd.scope:
                if scope not in self._subscope_detectors:
                    self._subscope_detectors[scope] = []
                    new_scope = True

                self._subscope_detectors[scope].append(sd)

            logger.info('subscope detector <%s> for %s', py, sd.scope)

        if not new_scope:
            return

        detectors_sync = {}
        for scope in self._subscope_detectors.keys():
            detectors_sync[scope] = 1

        self.notify('ncm2#_s', 'subscope_detectors', detectors_sync)
        self.notify('ncm2#_hook_for_subscope_detectors') 
Example 51
Project: project_generator_definitions   Author: project-generator   File: definitions.py    Apache License 2.0 5 votes vote down vote up
def __init__(self):
        mcu_files = glob.glob(join(dirname(__file__), 'mcu', '*', '*.yaml'))
        self.mcus = {}
        for m in mcu_files:
            self.mcus[splitext(basename(m))[0]] = m 
Example 52
Project: pydataloader   Author: salsita   File: dataloader.py    MIT License 5 votes vote down vote up
def load(self, f, markup=None, name=None, **options):
        path = f if isinstance(f, basestring) else f.name
        if markup is None:
            markup = self.markups.get(splitext(path)[-1][1:], self.default_markup)
        if self.log:
            self.log.info('get %s: %s [%s]', name or self.name, path, markup)
        return self.postprocess(getattr(self, 'load_{0}'.format(markup))(f), name, **options) 
Example 53
Project: JukeBox   Author: gauravsarkar97   File: sysfont.py    MIT License 5 votes vote down vote up
def initsysfonts_unix(path="fc-list"):
    """use the fc-list from fontconfig to get a list of fonts"""
    fonts = {}

    try:
        # note, we capture stderr so if fc-list isn't there to stop stderr
        # printing.
        flout, flerr = subprocess.Popen('%s : file family style' % path, shell=True,
                                        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                        close_fds=True).communicate()
    except Exception:
        return fonts

    entries = toascii(flout)
    try:
        for line in entries.split('\n'):

            try:
                filename, family, style = line.split(':', 2)
                if splitext(filename)[1].lower() in OpenType_extensions:
                    bold = 'Bold' in style
                    italic = 'Italic' in style
                    oblique = 'Oblique' in style
                    for name in family.strip().split(','):
                        if name:
                            break
                    else:
                        name = splitext(basename(filename))[0]

                    _addfont(
                        _simplename(name), bold, italic or oblique, filename, fonts)

            except Exception:
                # try the next one.
                pass

    except Exception:
        pass

    return fonts 
Example 54
Project: Semi-Reid   Author: Flowerfan524   File: dataset.py    MIT License 5 votes vote down vote up
def _pluck(identities, indices, relabel=False):
    ret = []
    for index, pid in enumerate(indices):
        pid_images = identities[pid]
        for camid, cam_images in enumerate(pid_images):
            for fname in cam_images:
                name = osp.splitext(fname)[0]
                x, y, _ = map(int, name.split('_'))
                assert pid == x and camid == y
                if relabel:
                    ret.append((fname, index, camid))
                else:
                    ret.append((fname, pid, camid))
    return ret 
Example 55
Project: Semi-Reid   Author: Flowerfan524   File: Market1501_STD.py    MIT License 5 votes vote down vote up
def _pluck(identities, indices, relabel=False):
    ret = []
    for index, pid in enumerate(indices):
        pid_images = identities[pid]
        for camid, cam_images in enumerate(pid_images):
            for fname in cam_images:
                name = osp.splitext(fname)[0]
                x, y, _ = map(int, name.split('_'))
                assert pid == x and camid == y
                if relabel:
                    ret.append((fname, index, camid, 1.0))
                else:
                    ret.append((fname, pid, camid, 1.0))
    return ret 
Example 56
Project: turing_bot   Author: gabolaev   File: problemBuilding.py    MIT License 5 votes vote down vote up
def getFileWithoutExtension(path):
    from os.path import basename, splitext
    return splitext(basename(path))[0] 
Example 57
Project: pyplis   Author: jgliss   File: image.py    GNU General Public License v3.0 5 votes vote down vote up
def load_file(self, file_path):
        """Try to import file specified by input path."""
        ext = splitext(file_path)[-1]
        try:
            self.load_fits(file_path)
        except:
            img = imread(file_path, -1)
            if self.dtype is not None:
                img = img.astype(self.dtype)
            self.img = img
        self.meta["path"] = abspath(file_path)
        self.meta["file_name"] = basename(file_path)
        self.meta["file_type"] = ext 
Example 58
Project: flask-s3-save-example   Author: Jaza   File: prefix_file_utcnow.py    Apache License 2.0 5 votes vote down vote up
def prefix_file_utcnow(model, file_data):
    parts = path.splitext(file_data.filename)
    return secure_filename('%s%s' % (datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S'), parts[1])) 
Example 59
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: evalrank.py    MIT License 5 votes vote down vote up
def baseroot(name):
    return splitext(basename(name))[0] 
Example 60
Project: Markov-Comic-Generator   Author: TheOpenSourceNinja   File: idchecker.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def checkFile( self, theFile, theFileName, commentMark ):
		'''Check whether a file has a valid ID number in its name and first non-blank-after-comment-removal line.
			Args:
				theFile: The file to check. Must already be open for text-mode reading. Will not be closed by this function.
				theFileName: The name of the file.
				commentMark: The separator used to mark the start of a comment in the file. Comments are ignored.
			Returns:
				A Boolean indicating whether the file is valid.'''
		keepLookingForID = True
		IDFound = False
		
		while keepLookingForID:
			line = theFile.readline()
			if len( line ) == 0: #End of file reached. Lines, even blank lines, will have a length of at least one (the newline character)
				keepLookingForID = False
				break
			else:
				line = line.partition( commentMark )[ 0 ].strip() #Filter out comments
				if len( line ) > 0:
					keepLookingForID = False
					if self.checkString( line ) and line == path.splitext( path.basename( theFileName ) )[ 0 ]: #Why enforce the file name requirement? It's about people, not code: it's to make sure transcribers have read all available documentation (README.md) about the transcription format.
						IDFound = True
					break
				else: #If line is blank after removal of comments, ignore it.
					keepLookingForID = True
		
		return IDFound 
Example 61
Project: seamseg   Author: mapillary   File: test_instance_seg.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_prediction_image(raw_pred, img_info, out_dir, colors, num_stuff, threshold):
    bbx_pred, cls_pred, obj_pred, msk_pred = raw_pred
    img = Image.open(img_info["abs_path"])
    draw = ImageDraw.Draw(img)

    # Prepare folders and paths
    folder, img_name = path.split(img_info["rel_path"])
    img_name, _ = path.splitext(img_name)
    out_dir = path.join(out_dir, folder)
    ensure_dir(out_dir)
    out_path = path.join(out_dir, img_name + ".jpg")

    # Rescale bounding boxes
    scale_factor = [os / bs for os, bs in zip(img_info["original_size"], img_info["batch_size"])]
    bbx_pred[:, [0, 2]] = bbx_pred[:, [0, 2]] * scale_factor[0]
    bbx_pred[:, [1, 3]] = bbx_pred[:, [1, 3]] * scale_factor[1]

    # Expand masks
    bbx_inv = invert_roi_bbx(bbx_pred, list(msk_pred.shape[-2:]), list(img_info["original_size"]))
    bbx_idx = torch.arange(0, msk_pred.size(0), dtype=torch.long)
    msk_pred = roi_sampling(
        msk_pred.cpu().unsqueeze(1).sigmoid(), bbx_inv.cpu(), bbx_idx, list(img_info["original_size"]), padding="zero")
    msk_pred = msk_pred.squeeze(1) > 0.5

    for bbx_pred_i, cls_pred_i, obj_pred_i, msk_pred_i in zip(bbx_pred, cls_pred, obj_pred, msk_pred):
        color = colors[cls_pred_i.item() + num_stuff]
        if obj_pred_i.item() > threshold:
            msk = Image.fromarray(msk_pred_i.numpy() * 192)
            draw.bitmap((0, 0), msk, tuple(color))

            draw.rectangle((
                bbx_pred_i[1].item(),
                bbx_pred_i[0].item(),
                bbx_pred_i[3].item(),
                bbx_pred_i[2].item(),
            ), outline=tuple(color), width=3)

    img.convert(mode="RGB").save(out_path) 
Example 62
Project: seamseg   Author: mapillary   File: test_instance_seg.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_prediction_raw(raw_pred, img_info, out_dir):
    # Prepare folders and paths
    folder, img_name = path.split(img_info["rel_path"])
    img_name, _ = path.splitext(img_name)
    out_dir = path.join(out_dir, folder)
    ensure_dir(out_dir)
    out_path = path.join(out_dir, img_name + ".pth.tar")

    out_data = {
        "bbx_pred": raw_pred[0],
        "cls_pred": raw_pred[1],
        "obj_pred": raw_pred[2],
        "msk_pred": raw_pred[3]
    }
    torch.save(out_data, out_path) 
Example 63
Project: seamseg   Author: mapillary   File: test_panoptic.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_prediction_image(_, panoptic_pred, img_info, out_dir, colors, num_stuff):
    msk, cat, obj, iscrowd = panoptic_pred

    img = Image.open(img_info["abs_path"])

    # Prepare folders and paths
    folder, img_name = path.split(img_info["rel_path"])
    img_name, _ = path.splitext(img_name)
    out_dir = path.join(out_dir, folder)
    ensure_dir(out_dir)
    out_path = path.join(out_dir, img_name + ".jpg")

    # Render semantic
    sem = cat[msk].numpy()
    crowd = iscrowd[msk].numpy()
    sem[crowd == 1] = 255

    sem_img = Image.fromarray(colors[sem])
    sem_img = sem_img.resize(img_info["original_size"][::-1])

    # Render contours
    is_background = (sem < num_stuff) | (sem == 255)
    msk = msk.numpy()
    msk[is_background] = 0

    contours = find_boundaries(msk, mode="outer", background=0).astype(np.uint8) * 255
    contours = dilation(contours)

    contours = np.expand_dims(contours, -1).repeat(4, -1)
    contours_img = Image.fromarray(contours, mode="RGBA")
    contours_img = contours_img.resize(img_info["original_size"][::-1])

    # Compose final image and save
    out = Image.blend(img, sem_img, 0.5).convert(mode="RGBA")
    out = Image.alpha_composite(out, contours_img)
    out.convert(mode="RGB").save(out_path) 
Example 64
Project: oct-lasagne   Author: maet3608   File: octapp.py    Apache License 2.0 5 votes vote down vote up
def prepare_annotation(self):
        """Create annotation file if it does not exist"""
        if osp.exists(self.annopath):
            return
        octpaths = glob(osp.join(self.datadir, '*' + OCTEXT))
        octids = [osp.splitext(osp.basename(p))[0] for p in octpaths]
        df = pd.DataFrame(octids, columns=[OCTID])
        df[DURATION] = None  # add empty duration column
        for name in LAYERS:  # add empty layer columns
            df[name] = None
        save_as_pickle(df, self.annopath) 
Example 65
Project: em   Author: nhynes   File: __main__.py    MIT License 5 votes vote down vote up
def _has_src_changes(repo, config):
    has_src_changes = has_changes = False
    for filepath, status in repo.status().items():
        ext = osp.splitext(osp.basename(filepath))[1][1:]
        changed = status not in GIT_UNCH
        has_changes = has_changes or changed
        if ext in _get_tracked_exts(config):
            has_src_changes = has_src_changes or changed
    return has_src_changes 
Example 66
Project: straditize   Author: Chilipp   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def show_docs(self):
        """Show the docs

        Shows the docs in the
        in the :attr:`~psyplot_gui.main.MainWindow.help_explorer`"""
        from psyplot_gui.main import mainwindow
        if self.fname is not None:
            rst = read_doc_file(self.fname)
            name = osp.splitext(osp.basename(self.fname))[0]
        else:
            rst = self.rst
            name = self.name
        mainwindow.help_explorer.show_rst(
            rst, name, files=list(set(self.files) - {self.fname})) 
Example 67
Project: straditize   Author: Chilipp   File: beginner.py    GNU General Public License v3.0 5 votes vote down vote up
def show(self):
        """Show the documentation of the tutorial"""
        intro, files = self.get_doc_files()
        self.filename = osp.splitext(osp.basename(intro))[0]
        with open(intro) as f:
            rst = f.read()
        name = osp.splitext(osp.basename(intro))[0]
        self.lock_viewer(False)
        self.tutorial_docs.show_rst(rst, name, files=files) 
Example 68
Project: straditize   Author: Chilipp   File: hoya_del_castillo.py    GNU General Public License v3.0 5 votes vote down vote up
def show(self):
        """Show the documentation of the tutorial"""
        from straditize.colnames import tesserocr
        intro, files = self.get_doc_files()
        self.filename = osp.splitext(osp.basename(intro))[0]
        with open(intro) as f:
            rst = f.read()
        if tesserocr is not None:
            rst = rst.replace('straditize-tutorial-column-names-ocr',
                              'straditize-tutorial-column-names')
        name = osp.splitext(osp.basename(intro))[0]
        self.lock_viewer(False)
        self.tutorial_docs.show_rst(rst, name, files=files) 
Example 69
Project: straditize   Author: Chilipp   File: menu_actions.py    GNU General Public License v3.0 5 votes vote down vote up
def _save_image(self, image, fname=None):
        """Save an image to a file

        Parameters
        ----------
        image: PIL.Image.Image
            The image to save
        fname: str or None
            The path of the target filename where to save the `image`. If None,
            A QFileDialog is opened and we ask the user for a filename"""
        if fname is None or not isinstance(fname, six.string_types):
            fname = QFileDialog.getSaveFileName(
                self.straditizer_widgets, 'Straditizer file destination',
                self._start_directory,
                'All images '
                '(*.png *.jpeg *.jpg *.pdf *.tif *.tiff);;'
                'Joint Photographic Experts Group (*.jpeg *.jpg);;'
                'Portable Document Format (*.pdf);;'
                'Portable Network Graphics (*.png);;'
                'Tagged Image File Format(*.tif *.tiff);;'
                'All files (*)'
                )
            if with_qt5:  # the filter is passed as well
                fname = fname[0]
        if not fname:
            return
        ext = osp.splitext(fname)[1]
        if ext.lower() in ['.jpg', '.jpeg', '.pdf'] and image.mode == 'RGBA':
            image = rgba2rgb(image)
        image.save(fname) 
Example 70
Project: platzi-hello-gae   Author: xertica-cloud   File: upload.py    GNU General Public License v2.0 5 votes vote down vote up
def _get_save_format(self, filename, image):
        if not image.format in self.keep_image_formats:
            name, ext = op.splitext(filename)
            filename = '%s.jpg' % name
            return filename, 'JPEG'

        return filename, image.format


# Helpers 
Example 71
Project: platzi-hello-gae   Author: xertica-cloud   File: upload.py    GNU General Public License v2.0 5 votes vote down vote up
def thumbgen_filename(filename):
    """
        Generate thumbnail name from filename.
    """
    name, ext = op.splitext(filename)
    return '%s_thumb.jpg' % name 
Example 72
Project: spleeter   Author: deezer   File: separator.py    MIT License 4 votes vote down vote up
def separate_to_file(
            self, audio_descriptor, destination,
            audio_adapter=get_default_audio_adapter(),
            offset=0, duration=600., codec='wav', bitrate='128k',
            filename_format='{filename}/{instrument}.{codec}',
            synchronous=True):
        """ Performs source separation and export result to file using
        given audio adapter.

        Filename format should be a Python formattable string that could use
        following parameters : {instrument}, {filename} and {codec}.

        :param audio_descriptor:    Describe song to separate, used by audio
                                    adapter to retrieve and load audio data,
                                    in case of file based audio adapter, such
                                    descriptor would be a file path.
        :param destination:         Target directory to write output to.
        :param audio_adapter:       (Optional) Audio adapter to use for I/O.
        :param offset:              (Optional) Offset of loaded song.
        :param duration:            (Optional) Duration of loaded song.
        :param codec:               (Optional) Export codec.
        :param bitrate:             (Optional) Export bitrate.
        :param filename_format:     (Optional) Filename format.
        :param synchronous:         (Optional) True is should by synchronous.
        """
        waveform, _ = audio_adapter.load(
            audio_descriptor,
            offset=offset,
            duration=duration,
            sample_rate=self._sample_rate)
        sources = self.separate(waveform)
        filename = splitext(basename(audio_descriptor))[0]
        generated = []
        for instrument, data in sources.items():
            path = join(destination, filename_format.format(
                filename=filename,
                instrument=instrument,
                codec=codec))
            if path in generated:
                raise SpleeterError((
                    f'Separated source path conflict : {path},'
                    'please check your filename format'))
            generated.append(path)
            task = self._pool.apply_async(audio_adapter.save, (
                path,
                data,
                self._sample_rate,
                codec,
                bitrate))
            self._tasks.append(task)
        if synchronous:
            self.join() 
Example 73
Project: fleeg-platform   Author: Fleeg   File: utils.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def get_page_info(url):
    page = Article(url)
    page_og = OpenGraph()
    image_url = None
    global_type = None
    page_content = None

    def get_page_head():
        headers = {
            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 '
                          '(KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2',
        }

        try:
            resp_headers = requests.head(url, headers=headers).headers
        except requests.exceptions.RequestException:
            raise LinkException('Failed to read link.')
        return resp_headers

    def get_title_from_url():
        name_url = splitext(basename(urlsplit(url).path))[0]
        words = re.findall(r'[a-zA-Z0-9]+', name_url)
        return ' '.join([word.capitalize() for word in words])

    def summary_from_text(txt, size=250):
        return txt[:size] if isinstance(txt, str) and len(txt) > size else txt

    def build_tags(*args):
        tags = reduce(operator.add, args)
        return list(filter(lambda x: bool(x), set(tags)))

    page_type, page_subtype = get_page_head()['Content-Type'].split('/')
    page_subtype = re.findall(r'[a-zA-Z0-9]+', page_subtype)[0]

    if page_type == 'image':
        image_url = url
        global_type = page_type

    if page_type == 'text':
        page.download()
        page_content = page.html

        if page_subtype == 'html':
            page_og = OpenGraph(html=page_content)
            page.parse()

    page_text = page.text or page_content

    return {
        'type': page_og.type or global_type or page_subtype,
        'title': page_og.title or page.title or get_title_from_url(),
        'summary': page_og.description or page.meta_description or summary_from_text(page_text),
        'image': page_og.image or page.meta_img or page.top_image or image_url,
        'tags': build_tags(page.meta_keywords, list(page.tags)),
        'publish_date': page.publish_date or None,
        'text': page_text,
    } 
Example 74
Project: Sigmedia-AVSR   Author: georgesterpu   File: write_records_tcd.py    GNU General Public License v3.0 4 votes vote down vote up
def main():

    dataset_dir = '/run/media/john_tukey/download/datasets/tcdtimit/'
    train_list = './datasets/tcdtimit/splits/speaker-dependent/train.scp'
    test_list = './datasets/tcdtimit/splits/speaker-dependent/test.scp'

    train = get_files(train_list, dataset_dir)
    test = get_files(test_list, dataset_dir)

    label_map = dict()
    for file in train+test:
        label_map[path.splitext(file)[0]] = path.splitext(file.split('tcdtimit/')[-1])[0]

    writer = TFRecordWriter(
        train_files=train,
        test_files=test,
        label_map=label_map,
        )

    writer.write_labels_records(
        unit='character',
        unit_list_file='/run/media/john_tukey/download/datasets/MV-LRS/misc/character_list',
        label_file='./datasets/tcdtimit/configs/character_labels',
        train_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/characters_train_sd.tfrecord',
        test_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/characters_test_sd.tfrecord',
    )

    writer.write_audio_records(
        content_type='feature',
        extension='wav',
        transform='logmel_stack_w8s3',
        # snr_list=[10, 0, -5],
        target_sr=22050,
        # noise_type='street',
        train_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/logmel_train_sd_stack',
        test_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/logmel_test_sd_stack',
    )

    writer.write_bmp_records(
        train_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/rgb36lips_train_sd.tfrecord',
        test_record_name='/run/media/john_tukey/download/datasets/tcdtimit/tfrecords/rgb36lips_test_sd.tfrecord',
        bmp_dir='/run/media/john_tukey/download/datasets/tcdtimit/aligned_openface/',
        output_resolution=(36, 36),
        crop_lips=True,
    ) 
Example 75
Project: PIL-RCE-Ghostscript-CVE-2018-16509   Author: farisv   File: app.py    MIT License 4 votes vote down vote up
def upload_file():
    if request.method == 'POST':
        file = request.files.get('image', None)

        if not file:
            flash('No image found')
            return redirect(request.url)

        filename = file.filename
        ext = path.splitext(filename)[1]

        if (ext not in ['.jpg', '.jpeg', '.png', '.gif', '.bmp']):
            flash('Invalid extension')
            return redirect(request.url)

        tmp = tempfile.mktemp("test")
        img_path = "{}.{}".format(tmp, ext)

        file.save(img_path)

        img = Image.open(img_path)
        w, h = img.size
        ratio = 256.0 / max(w, h)

        resized_img = img.resize((int(w * ratio), int(h * ratio)))
        resized_img.save(img_path)

        r = make_response()
        r.data = open(img_path, "rb").read()
        r.headers['Content-Disposition'] = 'attachment; filename=resized_{}'.format(filename)

        unlink(img_path)

        return r

    return render_template_string('''
    <!doctype html>
    <title>Image Resizer</title>
    <h1>Upload an Image to Resize</h1>
    {% with messages = get_flashed_messages() %}
    {% if messages %}
        <ul class=flashes>
        {% for message in messages %}
        <li>{{ message }}</li>
        {% endfor %}
        </ul>
    {% endif %}
    {% endwith %}
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=image>
         <input type=submit value=Upload>
    </form>
    ''') 
Example 76
Project: Dispersion-based-Clustering   Author: gddingcs   File: dataset.py    MIT License 4 votes vote down vote up
def load(self, verbose=True):
        splits = read_json(osp.join(self.root, 'splits.json'))
        if self.split_id >= len(splits):
            raise ValueError("split_id exceeds total splits {}"
                             .format(len(splits)))
        self.split = splits[self.split_id]

        train_pids = np.asarray(self.split['train'])


        self.meta = read_json(osp.join(self.root, 'meta.json'))
        identities = self.meta['identities']

        self.train = _pluck(identities, train_pids, relabel=True)
        self.query = _pluck(identities, self.split['query'])
        self.gallery = _pluck(identities, self.split['gallery'])
        self.num_train_ids = len(train_pids)

        if 'query' in self.meta:               
            query_fnames = self.meta['query']
            gallery_fnames = self.meta['gallery']
            self.query = []
            for fname_list in query_fnames:
                name = osp.splitext(fname_list[0])[0]
                pid, cam, vid,  _ = map(int, name.split('_'))
                self.query.append((tuple(fname_list), pid, cam, vid))
            self.gallery = []
            for fname_list in gallery_fnames:
                name = osp.splitext(fname_list[0])[0]
                pid, cam, vid, _ = map(int, name.split('_'))
                self.gallery.append((tuple(fname_list), pid, cam, vid))



        if verbose:
            print(self.__class__.__name__, "dataset loaded")
            print("  subset   | # ids | # tracklets")
            print("  ---------------------------")
            print("  train    | {:5d} | {:8d}"
                  .format(self.num_train_ids, len(self.train)))
            print("  query    | {:5d} | {:8d}"
                  .format(len(self.split['query']), len(self.query)))
            print("  gallery  | {:5d} | {:8d}"
                  .format(len(self.split['gallery']), len(self.gallery)))
            print() 
Example 77
Project: Semi-Reid   Author: Flowerfan524   File: Market1501_STD.py    MIT License 4 votes vote down vote up
def load(self, num_val=0.3, verbose=True):
        splits = read_json(osp.join(self.root, 'splits.json'))
        if self.split_id >= len(splits):
            raise ValueError("split_id exceeds total splits {}"
                             .format(len(splits)))
        self.split = splits[self.split_id]

        # Randomly split train / val
        trainval_pids = np.asarray(self.split['trainval'])
        #np.random.shuffle(trainval_pids)
        num = len(trainval_pids)
        if isinstance(num_val, float):
            num_val = int(round(num * num_val))
        if num_val >= num or num_val < 0:
            raise ValueError("num_val exceeds total identities {}"
                             .format(num))
        val_pids = sorted(trainval_pids[-num_val:])

        self.meta = read_json(osp.join(self.root, 'meta.json'))
        identities = self.meta['identities']
        self.val = _pluck(identities, val_pids, relabel=True)
        self.trainval = _pluck(identities, trainval_pids, relabel=True)
        self.num_val_ids = len(val_pids)
        self.num_trainval_ids = len(trainval_pids)

        def get_pid_camid(fname):
            name = osp.splitext(fname)[0]
            pid,cam,_ = map(int,name.split('_'))
            return (fname,pid,cam,1)

        self.query = [get_pid_camid(fname)
                      for fname in self.split['query']]
        self.gallery = [get_pid_camid(fname)
                        for fname in self.split['gallery']]

        if verbose:
            print(self.__class__.__name__, "dataset loaded")
            print("  subset   | # ids | # images")
            print("  ---------------------------")
            print("  val      | {:5d} | {:8d}"
                  .format(self.num_val_ids, len(self.val)))
            print("  trainval | {:5d} | {:8d}"
                  .format(self.num_trainval_ids, len(self.trainval)))
            print("  query    | {:5d} | {:8d}"
                  .format(len(self.split['query']), len(self.query)))
            print("  gallery  | {:5d} | {:8d}"
                  .format(len(self.split['gallery']), len(self.gallery))) 
Example 78
Project: straditize   Author: Chilipp   File: data.py    GNU General Public License v3.0 4 votes vote down vote up
def load_samples(self, fname=None):
        """Load the samples of a text file

        This method askes for a filename to update the samples. The first
        column in this file is taken as the sample locations. If the
        y-axis translation is already done, the new data is assumed to be in
        this transformed unit.

        Parameters
        ----------
        fname: str
            The path to the file to use. If None, a QFileDialog is opened and
            we ask for a name"""
        if fname is None or not isinstance(fname, six.string_types):
            fname = QFileDialog.getOpenFileName(
                self.straditizer_widgets, 'samples',
                self.straditizer_widgets.menu_actions._start_directory,
                'CSV files (*.csv);;'
                'Excel files (*.xls *.xlsx);;'
                'Straditize projects (*.nc *.nc4 *.pkl);;'
                'All files (*)'
                )
            if with_qt5:  # the filter is passed as well
                fname = fname[0]
        if not fname:
            return
        base, ext = osp.splitext(fname)
        if ext in ['.nc', '.nc4']:
            with xr.open_dataset(fname) as ds:
                df = self.straditizer.from_dataset(ds, plot=False).final_df
        elif ext == '.pkl':
            with open(fname, 'rb') as f:
                df = pickle.load(f).final_df
        elif ext in ['.xls', '.xlsx']:
            df = pd.read_excel(fname, index_col=0)
        else:
            df = pd.read_csv(fname, index_col=0)
        samples = df.index.values
        try:
            samples = self.straditizer.data2px_y(samples)
        except ValueError:
            pass
        # HACK: truncate to the available data region
        # This could be better solved through a dialog with the user...
        maxy = len(self.reader._full_df) - 1
        samples[samples < 0] = 0
        samples[samples > maxy] = maxy
        self.straditizer.data_reader.add_samples(
            np.unique(samples.astype(int)))
        self.straditizer_widgets.refresh() 
Example 79
Project: YAPyPy   Author: Xython   File: auto_test.py    MIT License 4 votes vote down vote up
def test_all(self):
        for each in filter(lambda p: p[-1].endswith('.py'), yapypy.collect()):
            filename = each.__str__()

            if each.parent().exists():
                pass
            else:
                each.parent().mkdir()

            with each.open('r') as fr:
                collector = DocStringsCollector()
                mod = ast.parse(fr.read())
                collector.visit(mod)

            mod_name, _ = splitext(each.relative())

            for idx, [fn_name, lineno, title, prepare_code,
                      test_code] in enumerate(collector.docs):

                print(f'tests of {mod_name}.{title or fn_name} started...')

                context = {'self': self}
                prepare_code = dedent_all(prepare_code)
                test_code = dedent_all(test_code)
                fixer = FixLineno(lineno)
                try:
                    node = ast.parse(prepare_code, filename, mode='exec')

                    fixer.visit(node)
                    code = compile(node, filename, "exec")
                except SyntaxError as exc:
                    exc.lineno = lineno
                    exc.filename = filename
                    raise exc
                bc = Bytecode.from_code(code)
                bc.filename = filename
                bc.first_lineno = lineno
                exec(bc.to_code(), context)

                # not correct but as a workaround

                fixer = FixLineno(lineno + test_code.count('\n'))
                try:
                    node = parse(test_code, filename).result
                    # pprint(node)
                    fixer.visit(node)
                    code = py_compile(node, filename, is_entrypoint=True)
                except SyntaxError as exc:
                    exc.lineno = lineno
                    exc.filename = filename
                    raise exc
                bc = Bytecode.from_code(code)
                bc.filename = filename
                bc.first_lineno = lineno
                code_obj = bc.to_code()

                exec(code_obj, context)
                print(f'tests of {mod_name}.{title or fn_name} passed.') 
Example 80
Project: platzi-hello-gae   Author: xertica-cloud   File: upload.py    GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, label=None, validators=None,
                 base_path=None, relative_path=None,
                 namegen=None, allowed_extensions=None,
                 **kwargs):
        """
            Constructor.

            :param label:
                Display label
            :param validators:
                Validators
            :param base_path:
                Absolute path to the directory which will store files
            :param relative_path:
                Relative path from the directory. Will be prepended to the file name for uploaded files.
                Flask-Admin uses `urlparse.urljoin` to generate resulting filename, so make sure you have
                trailing slash.
            :param namegen:
                Function that will generate filename from the model and uploaded file object.
                Please note, that model is "dirty" model object, before it was committed to database.

                For example::

                    import os.path as op

                    def prefix_name(obj, file_data):
                        parts = op.splitext(file_data.filename)
                        return secure_filename('file-%s%s' % parts)

                    class MyForm(BaseForm):
                        upload = FileUploadField('File', namegen=prefix_name)

            :param allowed_extensions:
                List of allowed extensions. If not provided, will allow any file.
        """
        self.base_path = base_path
        self.relative_path = relative_path

        self.namegen = namegen or namegen_filename
        self.allowed_extensions = allowed_extensions
        self._should_delete = False

        super(FileUploadField, self).__init__(label, validators, **kwargs)