def MapCol(iterable, columns, func): """ iterable >> MapCol(columns, func) Apply given function to given columns of elements in iterable. >>> neg = lambda x: -x >>> [(1, 2), (3, 4)] >> MapCol(0, neg) >> Collect() [(-1, 2), (-3, 4)] >>> [(1, 2), (3, 4)] >> MapCol(1, neg) >> Collect() [(1, -2), (3, -4)] >>> [(1, 2), (3, 4)] >> MapCol((0, 1), neg) >> Collect() [(-1, -2), (-3, -4)] :param iterable of iterables iterable: Any iterable that contains iterables :param int|tuple of ints columns: Column index or tuple of indexes :param function func: Function to apply to elements :return: Iterator over lists :rtype: iterator of list """ colset = as_set(columns) for es in iterable: yield tuple(func(e) if i in colset else e for i, e in enumerate(es))
def ReadImage(sample, columns, pathfunc=None, as_grey=False, dtype='uint8'): """ Load images for samples. Loads images in jpg, gif, png, tif and bmp format. Images are returned as numpy arrays of shape (h, w, c) or (h, w) for color images or gray scale images respectively. See nutsml.imageutil.load_image for details. >>> from nutsflow import Collect >>> samples = ['tests/data/img_formats/nut_color.gif'] >>> img_samples = samples >> ReadImage(None) >> Collect() >>> samples = [('tests/data/img_formats/nut_color.gif', 'class0')] >>> img_samples = samples >> ReadImage(0) >> Collect() >>> imagepath = 'tests/data/img_formats/*.jpg' >>> samples = [(1, 'nut_color'), (2, 'nut_grayscale')] >>> img_samples = samples >> ReadImage(1, imagepath) >> Collect() >>> pathfunc = lambda sample: 'tests/data/img_formats/{1}.jpg'.format(*sample) >>> img_samples = samples >> ReadImage(1, pathfunc) >> Collect() :param tuple|list sample: ('nut_color', 1) :param None|int|tuple columns: Indices of columns in sample to be replaced by image (based on image id in that column) If None then a flat samples is assumed and a tuple with the image is returned. :param string|function|None pathfunc: Filepath with wildcard '*', which is replaced by the imageid provided in the sample, e.g. 'tests/data/img_formats/*.jpg' for sample ('nut_grayscale', 2) will become 'tests/data/img_formats/nut_grayscale.jpg' or Function to compute path to image file from sample, e.g. lambda sample: 'tests/data/img_formats/{1}.jpg'.format(*sample) or None, in this case the image id is take as filepath. :param bool as_grey: If true, load as grayscale image. :param dtype dtype: Numpy data type of the image. :return: Sample with image ids replaced by image (=ndarray) of shape (h, w, c) or (h, w) :rtype: tuple """ def load(fileid): """Load image for given fileid""" if isinstance(pathfunc, str): filepath = pathfunc.replace('*', fileid) elif hasattr(pathfunc, '__call__'): filepath = pathfunc(sample) else: filepath = fileid return load_image(filepath, as_grey=as_grey, dtype=dtype) if columns is None: return (load(sample), ) # image as tuple with one element colset = as_set(columns) elems = enumerate(sample) return tuple(load(e) if i in colset else e for i, e in elems)
def __init__(self, imgcol, annocols, figsize=None, pause=0.0001, interpolation=None, **annoargs): """ iterable >> ViewImageAnnotation(imgcol, annocols, figsize=None, pause, interpolation, **annoargs) | Images must be numpy arrays in one of the following formats: | MxN - luminance (grayscale, float array only) | MxNx3 - RGB (float or uint8 array) | MxNx4 - RGBA (float or uint8 array) | See | http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow Shapes with single-dimension axis are supported but not encouraged, e.g. MxNx1 will be converted to MxN. :param int imgcol: Index of data column that contains the image :param int|tuple annocols: Index or tuple of indices specifying the data column(s) that contain annotation (labels, or geometry) :param tuple figsize: Figure size in inch. :param float pause: Waiting time in seconds after each plot. Pressing a key skips the waiting time. :param string interpolation: Interpolation for imshow, e.g. 'nearest', 'bilinear', 'bicubic'. for details see http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow :param kwargs annoargs: Keyword arguments for visual properties of annotation, e.g. edgecolor='y', linewidth=1 """ fig = plt.figure(figsize=figsize) fig.canvas.set_window_title('ViewImageAnnotation') self.axes = fig.add_subplot(111) self.imgcol = imgcol self.annocols = as_set(annocols) self.pause = pause self.interpolation = interpolation self.annoargs = annoargs
def RegularImagePatches(iterable, imagecols, pshape, stride): """ samples >> RegularImagePatches(imagecols, shape, stride) Extract patches in a regular grid from images. >>> import numpy as np >>> img = np.reshape(np.arange(12), (3, 4)) >>> samples = [(img, 0)] >>> getpatches = RegularImagePatches(0, (2, 2), 2) >>> for p in samples >> getpatches: ... print(p) (array([[0, 1], [4, 5]]), 0) (array([[2, 3], [6, 7]]), 0) :param iterable iterable: Samples with images :param int|tuple imagecols: Indices of sample columns that contain images, where patches are extracted from. Images must be numpy arrays of shape h,w,c or h,w :param tuple shape: Shape of patch (h,w) :param int stride: Step size of grid patches are extracted from :return: Iterator over samples where images are replaced by patches. :rtype: generator """ colset = as_set(imagecols) for sample in iterable: patch_iters = ut.col_map(sample, imagecols, ni.patch_iter, pshape, stride) while True: patched = [] for i, p in enumerate(patch_iters): try: patched.append(next(p) if i in colset else p) except StopIteration: pass if len(patched) == len(sample): yield tuple(patched) else: break # one or all patch iterators are depleted
def col_map(sample, columns, func, *args, **kwargs): """ Map function to given columns of sample and keep other columns >>> sample = (1, 2, 3) >>> add_n = lambda x, n: x + n >>> col_map(sample, 1, add_n, 10) (1, 12, 3) >>> col_map(sample, (0, 2), add_n, 10) (11, 2, 13) :param tuple|list sample: Sample :param int|tuple columns: Single or multiple column indices. :param function func: Function to map :param args args: Arguments passed on to function :param kwargs kwargs: Keyword arguments passed on to function :return: Sample where function has been applied to elements in the given columns. """ colset = as_set(columns) f, a, kw = func, args, kwargs enum_iter = enumerate(sample) return tuple(f(e, *a, **kw) if i in colset else e for i, e in enum_iter)
def test_as_set(): assert as_set(1) == (1,) assert as_set((1, 2)) == {1, 2} assert as_set([1, 2]) == {1, 2}