"""
tools to convert specified type
"""
import torch as t
import numpy as np


def tonumpy(data):
    if isinstance(data, np.ndarray):
        return data
    if isinstance(data, t.Tensor):
        return data.detach().cpu().numpy()


def totensor(data, cuda=True):
    if isinstance(data, np.ndarray):
        tensor = t.from_numpy(data)
    if isinstance(data, t.Tensor):
        tensor = data.detach()
    if cuda:
        tensor = tensor.cuda()
    return tensor


def scalar(data):
    if isinstance(data, np.ndarray):
        return data.reshape(1)[0]
    if isinstance(data, t.Tensor):
        return data.item()

if __name__=='__main__':
    a = np.array([[1,2,3], [4,5,6]])
    a.fill(-1)
    print(a)

    print(np.where((np.asarray(a)[:, 0] > 1)&
                   (np.asarray(a)[:, 1] > 1)&
                   (np.asarray(a)[:, 2] > 1)[0]), '\n\n',

                   (np.asarray(a)[:, 0] > 1)&
                   (np.asarray(a)[:, 1] > 1)&
                   (np.asarray(a)[:, 2] > 1))