
import os
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms

#import cv2
#from google.colab.patches import cv2_imshow

def test():
    print('welcome to python probabilistic ML library')


def show_image(img_path,size=None,ratio=None):
     img = reshape_image(img_path, size, ratio)
     cv2_imshow(img)
     

def reshape_image(img_path,size=None,ratio=None):
     if not size:
         size=[0,480]
     img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
     img=image_resize(img,height=size[1], inter = cv2.INTER_AREA)
     if ratio:
         img=cv2.resize(img,size,fx=ratio[0],fy=ratio[1])
     return img
        
        
     
def image_resize(image, width = None, height = None, inter=None):
     # From https://stackoverflow.com/questions/44650888/resize-an-image-without-distortion-opencv
     # initialize the dimensions of the image to be resized and
     # grab the image size
     dim = None
     (h, w) = image.shape[:2]

     # if both the width and height are None, then return the
     # original image
     if width is None and height is None:
         return image

     # check to see if the width is None
     if width is None:
         # calculate the ratio of the height and construct the
         # dimensions
         r = height / float(h)
         dim = (int(w * r), height)

     # otherwise, the height is None
     else:
         # calculate the ratio of the width and construct the
         # dimensions
         r = width / float(w)
         dim = (width, int(h * r))

     # resize the image
     resized = cv2.resize(image, dim, interpolation = inter)

     # return the resized image
     return resized
    
    
def save_fig(fname, *args, **kwargs):
    '''Save current plot window to the figures directory.'''
    if "PYPROBML" in os.environ:
        root = os.environ["PYPROBML"]
        figdir = os.path.join(root, 'figures')
    else:
        figdir = '../figures' # default directory one above where code lives
    if not os.path.exists(figdir):
        os.mkdir(figdir)
    fname_full = os.path.join(figdir, fname)
    print('saving image to {}'.format(fname_full))
    plt.tight_layout()
    plt.savefig(fname_full, *args, **kwargs)
    
    
def savefig(fname, *args, **kwargs):
    save_fig(fname, *args, **kwargs)


def git_ssh(git_command, email, username, verbose=False):
    '''Execute a git command via ssh from colab.
    Details in https://github.com/probml/pyprobml/blob/master/book1/intro/colab_intro.ipynb
    Authors: Mahmoud Soliman <mjs@aucegypt.edu> and Kevin Murphy <murphyk@gmail.com>
    '''
    git_command=git_command.replace(r"https://github.com/","git@github.com:")
    print('executing command via ssh:', git_command)
    # copy keys from drive to local .ssh folder
    if verbose:
        print('Copying keys from gdrive to local VM')
    os.system('rm -rf ~/.ssh')
    os.system('mkdir ~/.ssh')
    os.system('cp  -r /content/drive/MyDrive/ssh/* ~/.ssh/')
    os.system('ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts')
    os.system('ssh -T git@github.com') # test
    # git commands
    if verbose:
        print('Executing git commands')
    os.system('git config --global user.email {}'.format(email))
    os.system('git config --global user.name {}'.format(username))
    os.system(git_command)
    # cleanup
    if verbose:
        print('Cleanup local VM')
    os.system('rm -r ~/.ssh/')
    os.system('git config --global user.email ""')
    os.system('git config --global user.name ""')
    

# Source:
# https://matplotlib.org/devdocs/gallery/statistics/confidence_ellipse.html
def confidence_ellipse(x, y, ax, n_std=3.0, facecolor='none', **kwargs):
    if x.size != y.size:
        raise ValueError("x and y must be the same size")

    cov = np.cov(x, y)
    pearson = cov[0, 1] / np.sqrt(cov[0, 0] * cov[1, 1])

    ell_radius_x = np.sqrt(1 + pearson)
    ell_radius_y = np.sqrt(1 - pearson)
    ellipse = Ellipse((0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2,
                      facecolor=facecolor, **kwargs)

    scale_x = np.sqrt(cov[0, 0]) * n_std
    mean_x = np.mean(x)

    scale_y = np.sqrt(cov[1, 1]) * n_std
    mean_y = np.mean(y)

    transf = transforms.Affine2D() \
        .rotate_deg(45) \
        .scale(scale_x, scale_y) \
        .translate(mean_x, mean_y)

    ellipse.set_transform(transf + ax.transData)
    return ax.add_patch(ellipse)


def convergence_test(fval, previous_fval, threshold=1e-4, warn=False):
    eps = 2e-10
    converged = 0
    delta_fval = np.abs(fval - previous_fval)
    avg_fval = (np.abs(fval) + abs(previous_fval) + eps) / 2.0
    if (delta_fval / avg_fval) < threshold:
        converged = 1

    if warn and (fval - previous_fval) < -2 * eps:
        print('convergenceTest:fvalDecrease', 'objective decreased!')
    return converged

@@ -142,65 +142,8 @@
 ms%0A%0A
-#import cv2%0A#from google.colab.patches import cv2_imshow%0A
 %0Adef
@@ -212,459 +212,20 @@
 ')%0A%0A
-%0A
 def 
-show_image(img_path,size=None,ratio=None):%0A     img = reshape_image(img_path, size, ratio)%0A     cv2_imshow(img)%0A     %0A%0Adef reshape_image(img_path,size=None,ratio=None):%0A     if not size:%0A         size=%5B0,480%5D%0A     img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)%0A     img=image_resize(img,height=size%5B1%5D, inter = cv2.INTER_AREA)%0A     if ratio:%0A         img=cv2.resize(img,size,fx=ratio%5B0%5D,fy=ratio%5B1%5D)%0A     return img%0A        %0A        %0A     %0Adef 
+compute_
 imag
@@ -267,20 +267,8 @@
 t = 
-None, inter=
 None
@@ -511,157 +511,8 @@
 2%5D%0A%0A
-     # if both the width and height are None, then return the%0A     # original image%0A     if width is None and height is None:%0A         return image%0A%0A
     
@@ -931,140 +931,18 @@
     
-# resize the image%0A     resized = cv2.resize(image, dim, interpolation = inter)%0A%0A     # return the resized image%0A     return resized
+return dim
 %0A   

tweak utils for show images in colab