#AUTOGENERATED! DO NOT EDIT! File to edit: dev/90_core.ipynb (unless otherwise specified).

__all__ = ['TEST_PATH', 'TEST_JPG', 'TEST_PNG', 'TEST_JSON', 'TEST_CSV', 'progress_bar', 'resize_file', 'in_ipython',
           'in_colab', 'in_notebook', 'gpu_memory', 'IN_IPYTHON', 'IN_COLAB', 'IN_NOTEBOOK', 'np_open_image',
           'cv_open_image', 'old_colormap_segmentation_labels', 'generate_colormap_rgb', 'generate_colormap_rgba',
           'LABEL_COLORMAP_RGB', 'LABEL_COLORMAP_RGBA', 'show_img', 'plot_images', 'cmap_show', 'save_png_p',
           'show_directory_stats']

#Cell

import cv2
from fastai.vision import *
import os

#Cell
TEST_PATH = 'nbs_test_data'
TEST_JPG = 'nbs_test_data/235443 - 1.jpg'
TEST_PNG = 'nbs_test_data/235443 - 1.png'
TEST_JSON = 'nbs_test_data/235443 - 1.json'
TEST_CSV= 'nbs_test_data/235443 - 1.jpg.points.csv'

#Cell
def progress_bar(i,n):
    print(':', end='')
    if i % n == 0:
        print(' ', i)


#Cell
import csv
def resize_file(fn, scale=1):
    img = PIL.Image.open(fn)
    img_w, img_h = img.size
    img_w, img_h = int(img_w*scale), int(img_h*scale)
    return img.resize((img_w, img_h))


#Cell
def in_ipython():
    "Check if the code is running in the ipython environment (jupyter including)"
    program_name = os.path.basename(os.getenv('_', ''))
    if ('jupyter-notebook' in program_name or # jupyter-notebook
        'ipython'          in program_name or # ipython
        'JPY_PARENT_PID'   in os.environ):    # ipython-notebook
        return True
    else:
        return False

IN_IPYTHON = in_ipython()

#export
def in_colab():
    "Check if the code is running in Google Colaboratory"
    try:
        from google import colab
        return True
    except: return False

IN_COLAB = in_colab()

#export
def in_notebook():
    "Check if the code is running in a jupyter notebook"
    if in_colab(): return True
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell': return True   # Jupyter notebook, Spyder or qtconsole
        elif shell == 'TerminalInteractiveShell': return False  # Terminal running IPython
        else: return False  # Other type (?)
    except NameError: return False      # Probably standard Python interpreter

IN_NOTEBOOK = in_notebook()

#export
from pynvml import *
def gpu_memory():
    'Print memory total, free and used'
    nvmlInit()
    h = nvmlDeviceGetHandleByIndex(0)
    info = nvmlDeviceGetMemoryInfo(h)
    print('*** GPU memory ***')
    print(f'total    : {info.total}')
    print(f'free     : {info.free}')
    print(f'used     : {info.used}')

#Cell
def np_open_image(fn:str) -> np.ndarray:
    'open an image and return as numpy'
    return  np.asarray(PIL.Image.open(fn))

def cv_open_image(fn:str) -> np.ndarray:
    'open an image and return as numpy'
    return  cv2.imread(fn)




#Cell
def old_colormap_segmentation_labels(N=256):
    'create a colour map for  segmentation labels'
    def bitget(byteval, idx):
        return ((byteval & (1 << idx)) != 0)

    cmap = np.zeros((N, 3))
    for i in range(0, N):
        id = i
        r, g, b = 0, 0, 0
        for j in range(0, 8):
            r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
            g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
            b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
            id = (id >> 3)
        cmap[i, 0] = r
        cmap[i, 1] = g
        cmap[i, 2] = b
    # cmap = cmap.astype(np.float32) / 255
    cmap = cmap.astype(np.uint8).flatten()
    return cmap

#Cell
from matplotlib.colors import ListedColormap

def generate_colormap_rgb(cmap:str='hot', num_colors:int=256) -> np.ndarray:
    '''
    generate a color map from a matplotlib base
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''

    assert num_colors > 0 and num_colors <= 256, 'num_colors must be in range 0 to 256'
    cm = plt.get_cmap(cmap)
    pal = cm(np.arange(num_colors))
    xpal = pal.copy()
    for i in range(0,255,num_colors):
        xpal =  np.vstack((xpal, pal))
    xpal[0,:] = (0,0,0,1) # make the first color black
    return (xpal[:256,:3]*255).astype(np.uint8).flatten(), ListedColormap(xpal[:256,:4])

def generate_colormap_rgba(cmap:str='hot', num_colors:int=256) -> ListedColormap:
    '''
    generate a color map from a matplotlib base
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''

    assert num_colors > 0 and num_colors <= 256, 'num_colors must be in range 0 to 256'
    cm = plt.get_cmap(cmap)
    pal = cm(np.linspace(0, 1, num_colors))
    xpal = pal.copy()
    for i in range(0,255,num_colors):
        xpal =  np.vstack((xpal, pal))
    xpal[0,:] = (0,0,0,1) # make the first color black
    return ListedColormap(xpal[:256,:4])

LABEL_COLORMAP_RGB = generate_colormap_rgb(cmap='tab20', num_colors=20)
LABEL_COLORMAP_RGBA = generate_colormap_rgba(cmap='tab20', num_colors=20)
# generate_colormap(cmap='tab20', num_colors=20).shape

#Cell
LABEL_COLORMAP_RGB, LABEL_COLORMAP_RGBA  = generate_colormap_rgb(cmap='tab20', num_colors=20)
 # = generate_colormap_rgba(cmap='tab20', num_colors=20)

#Cell
def show_img(im, figsize=None, ax=None, alpha=None, label=None, mode=None, axis_on=False, title=None, cmap=None, vmin=0, vmax=None):
    if not ax: fig, ax = plt.subplots(figsize=figsize)
    if mode == 'P':
        cmap = LABEL_COLORMAP_RGBA
        vmax=255
        assert im.dtype == 'uint8', "image type must be uint8 for mode == 'P'"
    ax.imshow(im, alpha=alpha, cmap=cmap, vmin=vmin, vmax=vmax)
    if not axis_on:
        ax.set_axis_off()
    if label: ax.legend()
    if title: ax.set_title(title)
    return ax

#Cell
def plot_images(img_dict, figsize=None):
    ' plot numpy images side by side'
    fig, axes = plt.subplots(1, len(img_dict), figsize=figsize)
    for i, id in enumerate(img_dict.items()):
        show_img(id[1], ax=axes[i], title=id[0])
    plt.tight_layout()


#Cell
def cmap_show(num=50):
    grd = np.linspace(0, num, num); grd = np.vstack((grd, grd, grd, grd)).astype(np.uint8)
    # show_img(grd[:,:num], cmap=LABEL_COLORMAP_RGBA, vmax=255, axis_on=True)
    show_img(grd, mode='P', axis_on=True)
    plt.tight_layout()

#Cell
def save_png_p(img:np.ndarray, fn):
    '''
    Save an image as an 8 bit png file with an embedded LABEL_COLORMAP_RGB
    - img:
    - fn: filename to save
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''
    # palette = generate_colormap(cmap=cmap, num_colors=num_colors)
    img = PIL.Image.fromarray(img, mode='P')
    # img = PIL.Image.fromarray((img*255).astype(np.uint8), mode='P')
    img.putpalette(LABEL_COLORMAP_RGB)
    img.save(fn)


#Cell
def show_directory_stats(path):
    print(path)

    image_files = get_image_files(path)
    json_files = get_files(path, extensions=['.json'])

    for i, fn in enumerate(image_files):
        image_files[i] = fn.name.split('.')[0]
    for i, fn in enumerate(json_files):
        json_files[i] = fn.name.split('.')[0]

    print(f'Number of jpg files = {len(image_files)}')

    print(f"Number of json files = { len(json_files) }")
    set1 = set(image_files)
    set2 = set(json_files)

    missing = list(sorted(set1 - set2))
    added = list(sorted(set2 - set1))
    print('missing:', len(missing), missing)
    print('added:', len(added), added)
#AUTOGENERATED! DO NOT EDIT! File to edit: dev/90_core.ipynb (unless otherwise specified).

__all__ = ['TEST_PATH', 'TEST_JPG', 'TEST_PNG', 'TEST_JSON', 'TEST_CSV', 'progress_bar', 'resize_file', 'in_ipython',
           'in_colab', 'in_notebook', 'gpu_memory', 'IN_IPYTHON', 'IN_COLAB', 'IN_NOTEBOOK', 'np_open_image',
           'cv_open_image', 'old_colormap_segmentation_labels', 'generate_colormap_rgb', 'generate_colormap_rgba',
           'LABEL_COLORMAP_RGB', 'LABEL_COLORMAP_RGBA', 'show_img', 'plot_images', 'cmap_show', 'save_png_p',
           'show_directory_stats']

#Cell

import cv2
from fastai.vision import *
import os

#Cell
TEST_PATH = 'nbs_test_data'
TEST_JPG = 'nbs_test_data/235443 - 1.jpg'
TEST_PNG = 'nbs_test_data/235443 - 1.png'
TEST_JSON = 'nbs_test_data/235443 - 1.json'
TEST_CSV= 'nbs_test_data/235443 - 1.jpg.points.csv'

#Cell
def progress_bar(i,n):
    print(':', end='')
    if i % n == 0:
        print(' ', i)


#Cell
import csv
def resize_file(fn, scale=1):
    img = PIL.Image.open(fn)
    img_w, img_h = img.size
    img_w, img_h = int(img_w*scale), int(img_h*scale)
    return img.resize((img_w, img_h))


#Cell
def in_ipython():
    "Check if the code is running in the ipython environment (jupyter including)"
    program_name = os.path.basename(os.getenv('_', ''))
    if ('jupyter-notebook' in program_name or # jupyter-notebook
        'ipython'          in program_name or # ipython
        'JPY_PARENT_PID'   in os.environ):    # ipython-notebook
        return True
    else:
        return False

IN_IPYTHON = in_ipython()

#export
def in_colab():
    "Check if the code is running in Google Colaboratory"
    try:
        from google import colab
        return True
    except: return False

IN_COLAB = in_colab()

#export
def in_notebook():
    "Check if the code is running in a jupyter notebook"
    if in_colab(): return True
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell': return True   # Jupyter notebook, Spyder or qtconsole
        elif shell == 'TerminalInteractiveShell': return False  # Terminal running IPython
        else: return False  # Other type (?)
    except NameError: return False      # Probably standard Python interpreter

IN_NOTEBOOK = in_notebook()

#export
from pynvml import *
def gpu_memory():
    'Print memory total, free and used'
    nvmlInit()
    h = nvmlDeviceGetHandleByIndex(0)
    info = nvmlDeviceGetMemoryInfo(h)
    print('*** GPU memory ***')
    print(f'total    : {info.total}')
    print(f'free     : {info.free}')
    print(f'used     : {info.used}')

#Cell
def np_open_image(fn:str) -> np.ndarray:
    'open an image and return as numpy'
    return  np.asarray(PIL.Image.open(fn))

def cv_open_image(fn:str) -> np.ndarray:
    'open an image and return as numpy'
    return  cv2.imread(fn)




#Cell
def old_colormap_segmentation_labels(N=256):
    'create a colour map for  segmentation labels'
    def bitget(byteval, idx):
        return ((byteval & (1 << idx)) != 0)

    cmap = np.zeros((N, 3))
    for i in range(0, N):
        id = i
        r, g, b = 0, 0, 0
        for j in range(0, 8):
            r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
            g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
            b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
            id = (id >> 3)
        cmap[i, 0] = r
        cmap[i, 1] = g
        cmap[i, 2] = b
    # cmap = cmap.astype(np.float32) / 255
    cmap = cmap.astype(np.uint8).flatten()
    return cmap

#Cell
from matplotlib.colors import ListedColormap

def generate_colormap_rgb(cmap:str='hot', num_colors:int=256) -> np.ndarray:
    '''
    generate a color map from a matplotlib base
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''

    assert num_colors > 0 and num_colors <= 256, 'num_colors must be in range 0 to 256'
    cm = plt.get_cmap(cmap)
    pal = cm(np.arange(num_colors))
    xpal = pal.copy()
    for i in range(0,255,num_colors):
        xpal =  np.vstack((xpal, pal))
    xpal[0,:] = (0,0,0,1) # make the first color black
    return (xpal[:256,:3]*255).astype(np.uint8).flatten(), ListedColormap(xpal[:256,:4])

def generate_colormap_rgba(cmap:str='hot', num_colors:int=256) -> ListedColormap:
    '''
    generate a color map from a matplotlib base
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''

    assert num_colors > 0 and num_colors <= 256, 'num_colors must be in range 0 to 256'
    cm = plt.get_cmap(cmap)
    pal = cm(np.linspace(0, 1, num_colors))
    xpal = pal.copy()
    for i in range(0,255,num_colors):
        xpal =  np.vstack((xpal, pal))
    xpal[0,:] = (0,0,0,1) # make the first color black
    return ListedColormap(xpal[:256,:4])

LABEL_COLORMAP_RGB = generate_colormap_rgb(cmap='tab20', num_colors=20)
LABEL_COLORMAP_RGBA = generate_colormap_rgba(cmap='tab20', num_colors=20)
# generate_colormap(cmap='tab20', num_colors=20).shape

#Cell
LABEL_COLORMAP_RGB, LABEL_COLORMAP_RGBA  = generate_colormap_rgb(cmap='tab20', num_colors=20)
 # = generate_colormap_rgba(cmap='tab20', num_colors=20)

#Cell
def show_img(im, figsize=None, ax=None, alpha=None, label=None, mode=None, axis_on=False, title=None, cmap=None, vmin=0, vmax=None):
    if not ax: fig, ax = plt.subplots(figsize=figsize)
    if mode == 'P':
        cmap = LABEL_COLORMAP_RGBA
        vmax=255
        assert im.dtype == 'uint8', "image type must be uint8 for mode == 'P'"
    ax.imshow(im, alpha=alpha, cmap=cmap, vmin=vmin, vmax=vmax)
    if not axis_on:
        ax.set_axis_off()
    if label: ax.legend()
    if title: ax.set_title(title)
    return ax

#Cell
def plot_images(img_dict, figsize=None):
    ' plot numpy images side by side'
    fig, axes = plt.subplots(1, len(img_dict), figsize=figsize)
    for i, id in enumerate(img_dict.items()):
        show_img(id[1], ax=axes[i], title=id[0])
    plt.tight_layout()


#Cell
def cmap_show(num=50):
    grd = np.linspace(0, num, num); grd = np.vstack((grd, grd, grd, grd)).astype(np.uint8)
    # show_img(grd[:,:num], cmap=LABEL_COLORMAP_RGBA, vmax=255, axis_on=True)
    show_img(grd, mode='P', axis_on=True)
    plt.tight_layout()

#Cell
def save_png_p(img:np.ndarray, fn):
    '''
    Save an image as an 8 bit png file with an embedded LABEL_COLORMAP_RGB
    - img:
    - fn: filename to save
    - cmap: colormap ( use matplotlib cmaps )
    - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap
    '''
    # palette = generate_colormap(cmap=cmap, num_colors=num_colors)
    img = PIL.Image.fromarray(img, mode='P')
    # img = PIL.Image.fromarray((img*255).astype(np.uint8), mode='P')
    img.putpalette(LABEL_COLORMAP_RGB)
    img.save(fn)


#Cell
def show_directory_stats(path):
    print(path)

    image_files = get_image_files(path)
    json_files = get_files(path, extensions=['.json'])

    for i, fn in enumerate(image_files):
        image_files[i] = fn.name.split('.')[0]
    for i, fn in enumerate(json_files):
        json_files[i] = fn.name.split('.')[0]

    print(f'Number of jpg files = {len(image_files)}')

    print(f"Number of json files = { len(json_files) }")
    set1 = set(image_files)
    set2 = set(json_files)

    missing = list(sorted(set1 - set2))
    added = list(sorted(set2 - set1))
    print('missing:', len(missing), missing)
    print('added:', len(added), added)
#AUTOGENERATED! DO NOT EDIT! File to edit: dev/90_core.ipynb (unless otherwise specified). #Cell #Cell #Cell #Cell #Cell # jupyter-notebook # ipython # ipython-notebook #export #export # Jupyter notebook, Spyder or qtconsole # Terminal running IPython # Other type (?) # Probably standard Python interpreter #export #Cell #Cell # cmap = cmap.astype(np.float32) / 255 #Cell generate a color map from a matplotlib base     - cmap: colormap ( use matplotlib cmaps )     - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap # make the first color black generate a color map from a matplotlib base     - cmap: colormap ( use matplotlib cmaps )     - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap # make the first color black # generate_colormap(cmap='tab20', num_colors=20).shape #Cell # = generate_colormap_rgba(cmap='tab20', num_colors=20) #Cell #Cell #Cell # show_img(grd[:,:num], cmap=LABEL_COLORMAP_RGBA, vmax=255, axis_on=True) #Cell Save an image as an 8 bit png file with an embedded LABEL_COLORMAP_RGB     - img:     - fn: filename to save     - cmap: colormap ( use matplotlib cmaps )     - num_colors:  less than 256, for label images set `num_colors` to the number of colors in the `cmap` colormap # palette = generate_colormap(cmap=cmap, num_colors=num_colors) # img = PIL.Image.fromarray((img*255).astype(np.uint8), mode='P') #Cell