|
| 1 | +import numpy as np |
| 2 | +from skimage import io |
| 3 | +from typing import Optional |
| 4 | +import glob |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import copy |
| 7 | + |
| 8 | + |
| 9 | +def load_image(filename: str) -> np.ndarray: |
| 10 | + """ |
| 11 | + Given the filename of a TIFF, creates numpy array with pixel intensities |
| 12 | +
|
| 13 | + Args: |
| 14 | + filename: full path to the file to import (including extension) |
| 15 | +
|
| 16 | + Returns: |
| 17 | + A numpy array of the image |
| 18 | +
|
| 19 | + """ |
| 20 | + |
| 21 | + return io.imread(filename).astype(float) |
| 22 | + |
| 23 | + |
| 24 | +def save_img(img: np.ndarray, direc: str): |
| 25 | + """ |
| 26 | + Saves 2D array as .tif file |
| 27 | +
|
| 28 | + Args: |
| 29 | + img: numpy array of the image to save |
| 30 | + direc: file path to save to (including '.tif' extension) |
| 31 | +
|
| 32 | + """ |
| 33 | + |
| 34 | + io.imsave(direc, img.astype('float32')) |
| 35 | + |
| 36 | + |
| 37 | +def save_img_jpeg(img: np.ndarray, direc: str, cmin: Optional[float] = None, cmax: Optional[float] = None, |
| 38 | + cmap: str = 'gray'): |
| 39 | + """ |
| 40 | + Saves 2D array as jpeg, according to min and max pixel intensities |
| 41 | +
|
| 42 | + Args: |
| 43 | + img: numpy array of the image to save |
| 44 | + direc: file path to save to (including '.jpeg' extension) |
| 45 | + cmin: optional, sets intensity scaling (along with cmax) |
| 46 | + cmax: optional, sets intensity scaling (along with cmin) |
| 47 | + cmap: colour map (use string corresponding to matplotlib colormap) |
| 48 | +
|
| 49 | + """ |
| 50 | + |
| 51 | + plt.imsave(direc, img, vmin=cmin, vmax=cmax, cmap=cmap) |
| 52 | + |
| 53 | + |
| 54 | +def _direcslist(dest: str, levels: int = 0, exclude: Optional[tuple] = ('!',), |
| 55 | + exclusive: Optional[tuple] = None) -> list: |
| 56 | + lis = sorted(glob.glob(f'{dest}/*/')) |
| 57 | + |
| 58 | + for level in range(levels): |
| 59 | + newlis = [] |
| 60 | + for e in lis: |
| 61 | + newlis.extend(sorted(glob.glob(f'{e}/*/'))) |
| 62 | + lis = newlis |
| 63 | + lis = [x[:-1] for x in lis] |
| 64 | + |
| 65 | + # Excluded directories |
| 66 | + lis_copy = copy.deepcopy(lis) |
| 67 | + if exclude is not None: |
| 68 | + for x in lis: |
| 69 | + for i in exclude: |
| 70 | + if i in x: |
| 71 | + lis_copy.remove(x) |
| 72 | + break |
| 73 | + |
| 74 | + # Exclusive directories |
| 75 | + if exclusive is not None: |
| 76 | + lis2 = [] |
| 77 | + for x in lis_copy: |
| 78 | + for i in exclusive: |
| 79 | + if i in x: |
| 80 | + lis2.append(x) |
| 81 | + else: |
| 82 | + lis2 = lis_copy |
| 83 | + |
| 84 | + return sorted(lis2) |
| 85 | + |
| 86 | + |
| 87 | +def direcslist(dest: str, levels: int = 0, exclude: Optional[tuple] = ('!',), |
| 88 | + exclusive: Optional[tuple] = None) -> list: |
| 89 | + """ |
| 90 | + Gives a list of directories within a given directory (full path) |
| 91 | + Todo: os.walk |
| 92 | +
|
| 93 | + Args: |
| 94 | + dest: path of parent directory |
| 95 | + levels: number of levels to go down. E.g. if 0, only return folders within the parent folder; if 1, return |
| 96 | + folders within folders within the parent folder |
| 97 | + exclude: exclude directories containing any strings within this tuple |
| 98 | + exclusive: exclude directories that don't contain all the strings within this tuple |
| 99 | +
|
| 100 | + Returns: |
| 101 | + list of directories |
| 102 | +
|
| 103 | + """ |
| 104 | + |
| 105 | + if type(dest) is list: |
| 106 | + out = [] |
| 107 | + for d in dest: |
| 108 | + out.extend(_direcslist(d, levels, exclude, exclusive)) |
| 109 | + return out |
| 110 | + else: |
| 111 | + return _direcslist(dest, levels, exclude, exclusive) |
0 commit comments