# Path: hub_toolbox/io.py
# def random_sparse_matrix(size, density=0.05):
#     """Generate a random sparse similarity matrix.
# 
#     Values are bounded by [0, 1]. Diagonal is all ones. The final density is
#     approximately 2*`density`.
# 
#     Parameters
#     ----------
#     size : int
#         Shape of the matrix (`size` x `size`)
# 
#     density : float, optional, default=0.05
#         The matrix' density will be approximately 2 * `density`
# 
#     Returns
#     -------
#     S : csr_matrix
#         Random matrix
#     """
#     S = sparse.rand(size, size, density, 'csr')
#     S += S.T
#     S /= S.max()
#     S -= sparse.diags(S.diagonal(), 0)
#     S += sparse.diags(np.ones(size), 0)
#     return S
# 
# def load_dexter():
#     """Load the example data set (dexter).
# 
#     Returns
#     -------
#     D : ndarray
#         Distance matrix
# 
#     classes : ndarray
#         Class label vector
# 
#     vectors : ndarray
#         Vector data matrix
#     """
#     from hub_toolbox.distances import cosine_distance
#         
#     n = 300
#     dim = 20000
# 
#     # Read class labels
#     classes_file = os.path.dirname(os.path.realpath(__file__)) +\
#         '/example_datasets/dexter_train.labels'
#     classes = np.loadtxt(classes_file)  
# 
#     # Read data
#     vectors = np.zeros((n, dim))
#     data_file = os.path.dirname(os.path.realpath(__file__)) + \
#         '/example_datasets/dexter_train.data'
#     with open(data_file, mode='r') as fid:
#         data = fid.readlines()
#     row = 0
#     for line in data:
#         line = line.strip().split() # line now contains pairs of dim:val
#         for word in line:
#             col, val = word.split(':')
#             vectors[row][int(col)-1] = int(val)
#         row += 1
# 
#     # Calc distance
#     D = cosine_distance(vectors)
#     return D, classes, vectors
# 
# Path: hub_toolbox/io.py
# def load_dexter():
# def check_is_nD_array(arr:np.ndarray, n:int, arr_type=''):
# def check_distance_matrix_shape(D:np.ndarray):
# def check_distance_matrix_shape_fits_vectors(D:np.ndarray, vectors:np.ndarray):
# def check_distance_matrix_shape_fits_labels(D:np.ndarray, classes:np.ndarray):
# def check_vector_matrix_shape_fits_labels(X:np.ndarray, classes:np.ndarray):
# def check_sample_shape_fits(D:np.ndarray, idx:np.ndarray):
# def check_valid_metric_parameter(metric:str):
# def matrix_split(rows, cols, elem_size=8, nr_matrices=4): # pragma: no cover
# def random_sparse_matrix(size, density=0.05):
# def save_csr_matrix(file, matrix):
# def load_csr_matrix(file):
#     def __init__(self, unit='kB'):
#     def _factor(self):
#     def total(self):
#     def used(self):
#     def used_real(self):
#     def shared(self):
#     def buffers(self):
#     def cached(self):
#     def user_free(self):
#     def swap(self):
#     def swap_free(self):
#     def swap_used(self):
#     D = cosine_distance(vectors)
#     S = sparse.rand(size, size, density, 'csr')
# class FreeMemLinux(object): # pragma: no cover


import unittest
import tempfile
import numpy as np
from scipy.sparse.csr import csr_matrix
from hub_toolbox.io import random_sparse_matrix, load_dexter
from hub_toolbox import io
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This file is part of the HUB TOOLBOX available at
https://github.com/OFAI/hub-toolbox-python3/
The HUB TOOLBOX is licensed under the terms of the GNU GPLv3.

(c) 2016-2018, Roman Feldbauer
Austrian Research Institute for Artificial Intelligence (OFAI)
Contact: <roman.feldbauer@ofai.at>
"""

class TestIO(unittest.TestCase):

    def setUp(self):
        np.random.seed(626)
        self.matrix_n = 500
        self.density = 0.02
        self.similarity = random_sparse_matrix(
            size=self.matrix_n, density=self.density)

    def tearDown(self):
        del self.matrix_n, self.density, self.similarity

    def test_save_and_load_csr_matrix(self):
        tmp = tempfile.mkstemp(suffix='.npz')[1]

# Path: hub_toolbox/io.py
# def random_sparse_matrix(size, density=0.05):
#     """Generate a random sparse similarity matrix.
# 
#     Values are bounded by [0, 1]. Diagonal is all ones. The final density is
#     approximately 2*`density`.
# 
#     Parameters
#     ----------
#     size : int
#         Shape of the matrix (`size` x `size`)
# 
#     density : float, optional, default=0.05
#         The matrix' density will be approximately 2 * `density`
# 
#     Returns
#     -------
#     S : csr_matrix
#         Random matrix
#     """
#     S = sparse.rand(size, size, density, 'csr')
#     S += S.T
#     S /= S.max()
#     S -= sparse.diags(S.diagonal(), 0)
#     S += sparse.diags(np.ones(size), 0)
#     return S
# 
# def load_dexter():
#     """Load the example data set (dexter).
# 
#     Returns
#     -------
#     D : ndarray
#         Distance matrix
# 
#     classes : ndarray
#         Class label vector
# 
#     vectors : ndarray
#         Vector data matrix
#     """
#     from hub_toolbox.distances import cosine_distance
#         
#     n = 300
#     dim = 20000
# 
#     # Read class labels
#     classes_file = os.path.dirname(os.path.realpath(__file__)) +\
#         '/example_datasets/dexter_train.labels'
#     classes = np.loadtxt(classes_file)  
# 
#     # Read data
#     vectors = np.zeros((n, dim))
#     data_file = os.path.dirname(os.path.realpath(__file__)) + \
#         '/example_datasets/dexter_train.data'
#     with open(data_file, mode='r') as fid:
#         data = fid.readlines()
#     row = 0
#     for line in data:
#         line = line.strip().split() # line now contains pairs of dim:val
#         for word in line:
#             col, val = word.split(':')
#             vectors[row][int(col)-1] = int(val)
#         row += 1
# 
#     # Calc distance
#     D = cosine_distance(vectors)
#     return D, classes, vectors
# 
# Path: hub_toolbox/io.py
# def load_dexter():
# def check_is_nD_array(arr:np.ndarray, n:int, arr_type=''):
# def check_distance_matrix_shape(D:np.ndarray):
# def check_distance_matrix_shape_fits_vectors(D:np.ndarray, vectors:np.ndarray):
# def check_distance_matrix_shape_fits_labels(D:np.ndarray, classes:np.ndarray):
# def check_vector_matrix_shape_fits_labels(X:np.ndarray, classes:np.ndarray):
# def check_sample_shape_fits(D:np.ndarray, idx:np.ndarray):
# def check_valid_metric_parameter(metric:str):
# def matrix_split(rows, cols, elem_size=8, nr_matrices=4): # pragma: no cover
# def random_sparse_matrix(size, density=0.05):
# def save_csr_matrix(file, matrix):
# def load_csr_matrix(file):
#     def __init__(self, unit='kB'):
#     def _factor(self):
#     def total(self):
#     def used(self):
#     def used_real(self):
#     def shared(self):
#     def buffers(self):
#     def cached(self):
#     def user_free(self):
#     def swap(self):
#     def swap_free(self):
#     def swap_used(self):
#     D = cosine_distance(vectors)
#     S = sparse.rand(size, size, density, 'csr')
# class FreeMemLinux(object): # pragma: no cover


# Path: tests/io_test.py
import unittest
import tempfile
import numpy as np
from scipy.sparse.csr import csr_matrix
from hub_toolbox.io import random_sparse_matrix, load_dexter
from hub_toolbox import io

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This file is part of the HUB TOOLBOX available at
https://github.com/OFAI/hub-toolbox-python3/
The HUB TOOLBOX is licensed under the terms of the GNU GPLv3.

(c) 2016-2018, Roman Feldbauer
Austrian Research Institute for Artificial Intelligence (OFAI)
Contact: <roman.feldbauer@ofai.at>
"""

class TestIO(unittest.TestCase):

    def setUp(self):
        np.random.seed(626)
        self.matrix_n = 500
        self.density = 0.02
        self.similarity = random_sparse_matrix(
            size=self.matrix_n, density=self.density)

    def tearDown(self):
        del self.matrix_n, self.density, self.similarity

    def test_save_and_load_csr_matrix(self):
        tmp = tempfile.mkstemp(suffix='.npz')[1]

        io_sim = io.load_csr_matrix(io.save_csr_matrix(tmp, self.similarity))