<reponame>stealthness/sklearn-examples<gh_stars>0
'''
Trying to creating a python package of useful function
'''
import csv
import random
import matplotlib.pyplot as plt
import numpy as np


def import_dataset(file='I:/RES/ATT/D.txt'):
    reader = csv.reader(open(file, "r"), delimiter=",")
    x = list(reader)
    return np.array(x).astype("int")


def flatten_image(img, shape=(92, 112)):
    return img.reshape(shape)


def unflatten_image(v, shape):
    """
    Resize a 1-dim vector into a 2-dim vector
    :param v:
    :param shape:
    :return:
    """
    return v.resize(shape[0], shape[1])


def test_double(x):
    return 2*x


def sw_imshow(img, shape=None, cmap='gray', title='image'):
    if shape is not None:
        img = img.reshape(shape)
    plt.imshow(img.reshape(shape), cmap=cmap)
    plt.title(title)
    plt.show()


def imshow_mean_img(img, shape, title):
    if shape is not None:
        img = img.reshape(shape)
    plt.imshow(img.reshape(shape), cmap='gray')
    plt.title(title)
    plt.show()


def select_random_target(b):
    choice = random.randint(0, 400)
    return b.data[choice], b.target[choice]


def show_class_images(b, choice):
    print(f'choice is {choice}  {b.target}')
    index = b.target.index(choice)
    print(f'index: {index} url:{b.data_urls[index]}')
    fig = plt.figure()
    for i in range(10):
        img =b.data[index+i].reshape(b.shape)
        fig.add_subplot(2, 5, i+1)
        plt.imshow(img, cmap='gray')
        plt.axis('off')
    fig.suptitle(f'Class {b.target[index]} url:{b.data_urls[index]}')
    plt.show()


def show_two_images(img1, img2, **kwargs):
    fig = plt.figure()
    if 'title' in kwargs:
        fig.suptitle(kwargs['title'])
    fig.add_subplot(1, 2, 1)
    if 'cmap1' in kwargs:
        plt.imshow(img1, cmap=kwargs['cmap1'])
    else:
        plt.imshow(img1)
    plt.axis('off')
    fig.add_subplot(1, 2, 2)
    if 'cmap2' in kwargs:
        plt.imshow(img2, cmap=kwargs['cmap2'])
    else:
        plt.imshow(img2)
    plt.axis('off')
    plt.show()
