def save(pyramid, keys, root): sizes, octave = pyramid[0] octave = filters.norm(octave, 5, 5, 0, 255) key_levels = binby(keys, lambda x: int(x.octave)) for l, level in enumerate(octave): drawn = level if l in key_levels: drawn = keypoints.draw(level, key_levels[l]) save(drawn, pyfs.join(root, "%f.png" % sizes[l]), norm=False)
def load(path): keypoints = [] with open(path, "rb") as src: lines = src.read().split("\n") image_path = pyfs.join(pyfs.dpath(path), lines[0]) for line in lines[2:]: keypoint = decode(line) if keypoint: keypoints += [keypoint] return image_path, keypoints
def join(self, path=None): if path and path.startswith(self.base): return path path = pyfs.join(self.base, path) if self.base not in path: # if the path we are creating is not in the cache # or on the way to it then we have a problem raise ValueError("path: %s is outside jail: %s" % (path, self.base)) return path
def hashargs(args=[], kwargs={}, split=8): ''' (a) -> {b:c} -> int -> str given a set of positional and keyword function arguments provide a deterministic FS path for hashing. Uses md5. All keys and values must be convertible to string form. Currently splits the path into 8 characters to reduce number of possible entries in any given directory. ''' md5 = hashlib.md5() for arg in args: md5.update(str(arg)) for key in sorted(kwargs): md5.update(str(key)) md5.update(str(kwargs[key])) hexdigest = md5.hexdigest() components = split_in(hexdigest, split) return pyfs.join(*components)
def process(path, aligned, args, gpu): t0 = time.time() aligned_dw = unpack.label(aligned, 'dw') aligned_log = pyfs.rext(aligned, 'shifts') aligned_stk = pyfs.rext(aligned, 'mrcs') print(path, '->', aligned) print(' ' * len(path), '->', aligned_log) tmpdir = tempfile.mkdtemp() tmp_unzipped = pyfs.join(tmpdir, 'decompressed.mrc') tmp_unpacked = pyfs.join(tmpdir, 'unpacked.mrc') tmp_aligned = pyfs.join(tmpdir, 'aligned.mrc') tmp_aligned_dw = pyfs.join(tmpdir, 'aligned_DW.mrc') tmp_logfile = pyfs.join(tmpdir, 'aligned0-Full.log') tmp_stkfile = pyfs.join(tmpdir, 'aligned_Stk.mrc') tmp_unzipped = pbunzip2(path, tmp_unzipped) if args.unpack: unpack.unpack(tmp_unzipped, tmp_unpacked, args.defects, args.norm, mode='byte') motioncor2(tmp_unpacked, tmp_aligned, args, gpu=gpu) else: args.mocor2_norm = args.norm motioncor2(tmp_unzipped, tmp_aligned, args, gpu=gpu) mv(tmp_aligned, aligned) mv(tmp_aligned_dw, aligned_dw) mv(tmp_logfile, aligned_log) mv(tmp_stkfile, aligned_stk) shutil.rmtree(tmpdir, False) print('aligning: %s took: %.2f secs' % (path, time.time() - t0))
def label(path, label): parts = pyfs.split(path) filename, ext = pyfs.sext(parts[-1]) final_filename = '%s_%s.mrc' % (filename, label) return pyfs.join(*parts[:-1], final_filename)
import pyfs # load formatting plugins formats = pyfs.loader(pyfs.join(pyfs.dpath(__file__), "formats")) def save(data, path, format=None, **kwargs): if not format: format = pyfs.gext(path, last=True) return formats[format].save(data, path, **kwargs) def load(path, format=None, **kwargs): if not format: format = pyfs.gext(path, last=True) if format not in formats: raise ValueError( 'file {file!s} is of unknown format'.format(file=path)) return formats[format].load(path, **kwargs) def load_stats(path, **kwargs): path = pyfs.rext(path, "yaml") print "load stat:", path return load(path) def save_stats(stats, path, **kwargs): path = pyfs.rext(path, "yaml") save(stats, path)
def save(image, keys, root): image = filters.norm(image, 5, 5, 0, 255) key_sizes = binby(keys, lambda x: int(x.size)) for size in key_sizes: drawn = keypoints.draw(image, key_sizes[size]) save(drawn, pyfs.join(root, "%f.png"%size))
def save_octave(radii, octave, root): levels = filters.norm(octave, 1, 1, 0, 255) for size, level in zip(radii, levels): save(level, pyfs.join(root, "%2.2f.png"%(size)), norm=False)
def path(self): return pyfs.join(self.root, self.name, hashargs(self.args, self.kwgs))
def tempit(path): if path is None: return None return pyfs.join(tmpdir, path)