def rgb2hsv(red, green, blue): # https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB # red, green, blue in 0..1hue, sat and value all 0..1 # Returns hue, sat and value in 0..1 try: length = len(red) except Exception: length = 1 # Having to create and insert is due to an unfortunate limitation in ulab rgb = np.zeros((3, length), dtype=np.float) try: rgb[0] = red rgb[1] = green rgb[2] = blue except Exception: rgb[0] = red.flatten() rgb[1] = green.flatten() rgb[2] = blue.flatten() # These indexing games are because we can't index with integer arrays in ulab index = np.arange(length, dtype=np.uint16) index2d = np.zeros((length, 3), dtype=np.uint16) index2d[:, 0] = 0 index2d[:, 1] = 1 index2d[:, 2] = 2 brightest = np.numerical.argmax(rgb, axis=0) isBrightest = index2d == brightest value = rgb[isBrightest] cc = value - np.min(rgb, axis=0) hueSelect = np.array([0, 2, 4]) # Some unnecessary calculations, but still faster than a python loop hueCalc = np.array([green - blue, blue - red, red - green]) hue = (hueSelect[index == brightest] + hueCalc[isBrightest] / cc) / 6 sat = np.where(value == 0, 0.0, cc / value) return hue, sat, value
def __call__(self, tree): num = sum(len(ss) for ss in tree) indices = np.zeros((num, 2), dtype=int) start = 0 for ii in range(len(tree)): length = len(tree[ii]) ss = slice(start, start + length) indices[ss, 0] = ii indices[ss, 1] = np.arange(length) start += length while True: tree.clear() order = sorted([(randFloat(), ii) for ii in range(num)]) for _, ii in order: ss, tt = indices[ii] tree[ss][tt] = randomColor() yield for _, ii in order: ss, tt = indices[ii] tree[ss][tt] = BLACK yield
#PYCLOUDIOT : LIBRARY,2,10,creategrid_library.py, import ulab as np def create_grid(x): N = x.shape[0] z = np.zeros((N, N, 3)) z[:, :, 0] = x.reshape(-1, 1) z[:, :, 1] = x fast_grid = z.reshape(N * N, 3) return fast_grid #PYCLOUDIOT : MAIN,13,16,creategrid_main.py, #IMPORTS :creategrid_library.py ;ulab, N = 800 x = np.arange(0, 1, 1. / N) create_grid(x)
ref_result = [0.81649658, 0.81649658, 0.81649658] for p, q in zip(list(result), ref_result): print(math.isclose(p, q, rel_tol=1e-06, abs_tol=1e-06)) print("Testing np.median:") a = np.array([253, 254, 255], dtype=np.uint8) print(np.median(a)) print(np.median(a, axis=0)) a = np.array([range(255 - 3, 255), range(240 - 3, 240), range(250 - 3, 250)], dtype=np.float) print(np.median(a)) print(np.median(a, axis=0)) print(np.median(a, axis=1)) print("Testing np.roll:") ## Here is problem print(np.arange(10)) print(np.roll(np.arange(10), 2)) print(np.roll(np.arange(10), -2)) a = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(np.roll(a, 2)) print(np.roll(a, -2)) print("Testing np.clip:") print(np.clip(5, 3, 6)) ## Here is problem print(np.clip(7, 3, 6)) print(np.clip(1, 3, 6)) a = np.array([1, 2, 3, 4, 5, 6, 7], dtype=np.int8) print(np.clip(a, 3, 5)) a = np.array([1, 2, 3, 4, 5, 6, 7], dtype=np.float) print(np.clip(a, 3, 5))
print(np.array(np.array(range(5), dtype=np.uint16), dtype=np.int8)) print(np.array(np.array(range(5), dtype=np.uint16), dtype=np.uint16)) print(np.array(np.array(range(5), dtype=np.uint16), dtype=np.int16)) print(np.array(np.array(range(5), dtype=np.uint16), dtype=np.float)) print(np.array(np.array(range(5), dtype=np.int16), dtype=np.uint8)) print(np.array(np.array(range(5), dtype=np.int16), dtype=np.int8)) print(np.array(np.array(range(5), dtype=np.int16), dtype=np.uint16)) print(np.array(np.array(range(5), dtype=np.int16), dtype=np.int16)) print(np.array(np.array(range(5), dtype=np.int16), dtype=np.float)) print(np.array(np.array(range(5), dtype=np.float), dtype=np.uint8)) print(np.array(np.array(range(5), dtype=np.float), dtype=np.int8)) print(np.array(np.array(range(5), dtype=np.float), dtype=np.uint16)) print(np.array(np.array(range(5), dtype=np.float), dtype=np.int16)) print(np.array(np.array(range(5), dtype=np.float), dtype=np.float)) print("Array creation using ARANGE:") print(np.arange(10)) print(np.arange(2, 10)) print(np.arange(2, 10, 3)) print(np.arange(2, 10, 3, dtype=np.float)) print("Array concatenation:") a = np.array([1, 2, 3], dtype=np.float) b = np.array([4, 5, 6], dtype=np.float) print(np.concatenate((a, b))) print(np.concatenate((a, b), axis=0)) a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float) b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float) print(np.concatenate((a, b), axis=0)) print(np.concatenate((a, b), axis=1)) print(np.concatenate((b, a), axis=0)) print(np.concatenate((b, a), axis=1)) print("Identity array creation:")