Example #1
0
 def setUp(self):
     """Basic set up."""
     self.dense = CDense([[1, 0, 0, 0, 5], [2, 4, 0, 0, 0], [3, 4, 5, 0,
                                                             0]])
     self.dense_vector = CDense([1, 0, 0, 0, 3])
     self.sparse_matrix = CSparse(self.dense)
     self.sparse_vector = CSparse(self.dense_vector)
 def _randomize_values_for_attack(self, x_init):
     min_val = 0 + self.shift_values
     max_val = 255 + self.shift_values
     x_init[self.indexes_to_perturb] = CDense(
         torch.randint(low=min_val,
                       high=max_val,
                       size=(1, len(self.indexes_to_perturb))))
     return x_init
Example #3
0
    def test_init_array(self):
        """Test CArray initialization using arrays."""

        self.logger.info("Initializing CArray with another CArray...")
        arrays_list = [CArray([[2, 3], [22, 33]]),
                       CArray([2, 3]),
                       CArray([[2], [3]]),
                       CArray(3),
                       CArray([[[2, 3], [22, 33]], [[4, 5], [44, 55]]])]

        for init_elem in arrays_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertEqual(init_elem.issparse, array.issparse)
            self.assert_array_equal(array, init_elem)
            self.assertEqual(init_elem.shape, array.shape)
            self.assertEqual(init_elem.input_shape, array.input_shape)

        self.logger.info("Initializing CArray with a CDense...")
        dense_list = [CDense([[2, 3], [22, 33]]),
                      CDense([2, 3]),
                      CDense([[2], [3]]),
                      CDense([3]),
                      CDense([[[2, 3], [22, 33]], [[4, 5], [44, 55]]])]

        for init_elem in dense_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertTrue(array.isdense)
            self.assert_array_equal(array, init_elem)
            self.assertEqual(array.shape, init_elem.shape)
            self.assertEqual(init_elem.input_shape, array.input_shape)

        self.logger.info("Initializing CArray with an ndarray...")
        dense_list = [np.array([[2, 3], [22, 33]]),
                      np.array([2, 3]),
                      np.array([[2], [3]]),
                      np.array([3]),
                      np.array([[[2, 3], [22, 33]], [[4, 5], [44, 55]]])]

        for init_elem in dense_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertTrue(array.isdense)
            if init_elem.ndim <= 2:
                self.assert_array_equal(array, init_elem)
                self.assertEqual(array.shape, init_elem.shape)
            else:  # N-Dimensional ndarray
                self.assertEqual(array.shape[0], init_elem.shape[0])
                self.assertEqual(array.shape[1], sum(init_elem.shape[1:]))
            self.assertEqual(array.input_shape, init_elem.shape)

        self.logger.info("Initializing CArray with a sparse CArray...")
        sparse_list = [
            CArray([[2, 3], [22, 33]], tosparse=True),
            CArray([2, 3], tosparse=True),
            CArray([[2], [3]], tosparse=True),
            CArray(3, tosparse=True),
            CArray([[[2, 3], [22, 33]], [[4, 5], [44, 55]]], tosparse=True)]

        for init_elem in sparse_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertTrue(array.issparse)
            self.assert_array_equal(array, init_elem)
            self.assertEqual(init_elem.input_shape, array.input_shape)

        self.logger.info("Initializing CArray with a CSparse...")
        sparse_list = [CSparse([[2, 3], [22, 33]]),
                       CSparse([2, 3]),
                       CSparse([[2], [3]]),
                       CSparse([3]),
                       CSparse([[[2, 3], [22, 33]], [[4, 5], [44, 55]]])]

        for init_elem in sparse_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertTrue(array.issparse)
            self.assert_array_equal(array, init_elem)
            self.assertEqual(array.shape, init_elem.shape)
            self.assertEqual(array.input_shape, init_elem.input_shape)

        self.logger.info("Initializing CArray with a csr_matrix...")
        sparse_list = [scs.csr_matrix([[2, 3], [22, 33]]),
                       scs.csr_matrix([2, 3]),
                       scs.csr_matrix([[2], [3]]),
                       scs.csr_matrix([3])]

        for init_elem in sparse_list:
            self.logger.info(init_elem)
            array = CArray(init_elem)
            self.assertTrue(array.issparse)
            self.assert_array_equal(array.todense(), init_elem.todense())
            self.assertEqual(array.shape, init_elem.shape)
            self.assertEqual(array.input_shape, init_elem.shape)
    def test_save_load(self):

        self.logger.info("UNITTEST - CDense - save/load matrix")

        test_file = fm.join(fm.abspath(__file__), 'test.txt')

        # Cleaning test file
        try:
            fm.remove_file(test_file)
        except (OSError, IOError) as e:
            if e.errno != 2:
                raise e

        a = CDense().zeros((1000, 1000))

        with self.timer():
            a.save(test_file)

        with self.timer():
            b = CDense().load(test_file,
                              startrow=100,
                              cols=CDense(np.arange(0, 100)))

        self.assertFalse((a[100:, 0:100] != b).any())

        self.logger.info("UNITTEST - CDense - save/load vector")

        a = CDense().zeros(1000, dtype=int)

        with self.timer():
            a.save(test_file, overwrite=True)

        with self.timer():
            b = CDense().load(test_file,
                              cols=list(range(100, 1000)),
                              dtype=int).ravel()

        self.assertFalse((a[0, 100] != b).any())

        if np.__version__ < '1.18':
            with self.assertRaises(IndexError) as e:
                CDense().load(test_file, startrow=10)
            self.logger.info("Expected error: {:}".format(e.exception))
        else:
            with self.logger.catch_warnings():
                self.logger.filterwarnings(
                    "ignore", message="genfromtxt: Empty input file")
                a = CDense().load(test_file, startrow=10)
                self.assertEqual(a.size, 0)

        self.logger.info("UNITTEST - CDense - save/load row vector")

        a = CDense().zeros((1, 1000))

        with self.timer():
            a.save(test_file, overwrite=True)

        with self.timer():
            b = CDense().load(test_file, cols=CDense.arange(100, 1000))

        self.assertFalse((a[:, 100:] != b).any())

        # For some reasons np.genfromtxt does not close the file here
        # Let's handle the resource warning about unclosed file
        with self.logger.catch_warnings():
            self.logger.filterwarnings("ignore", message="unclosed file")
            if np.__version__ < '1.18':
                with self.assertRaises(IndexError) as e:
                    CDense().load(test_file, startrow=10)
                    self.logger.info("Expected error: {:}".format(e.exception))
            else:
                self.logger.filterwarnings(
                    "ignore", message="genfromtxt: Empty input file")
                a = CDense().load(test_file, startrow=10)
                self.assertEqual(a.size, 0)

        self.logger.info("UNITTEST - CDense - save/load negative vector")

        a = -CDense().zeros(1000)

        a.save(test_file, overwrite=True)
        with open(test_file, mode='at+') as fhandle:
            with self.timer():
                a.save(fhandle, overwrite=True)

        b = CDense().load(test_file)
        # Simulating double save \w append
        a = a.atleast_2d().append(a.atleast_2d(), axis=0)

        self.assertFalse((a != b).any())

        a = CDense(['a', 'b'])

        with self.timer():
            a.save(test_file, overwrite=True)

        b = CDense().load(test_file, dtype=str).ravel()

        self.assertFalse((a != b).any())

        # Cleaning test file
        try:
            fm.remove_file(test_file)
        except (OSError, IOError) as e:
            if e.errno != 2:
                raise e
    def test_init_array(self):
        """Test CArray initialization using arrays."""

        self.logger.info("Initializing CArray with another CArray...")
        arrays_list = [
            CArray([[2, 3], [22, 33]]),
            CArray([2, 3]),
            CArray([[2], [3]]),
            CArray(3)
        ]

        for init_elem in arrays_list:
            array = CArray(init_elem)
            self.assertTrue(init_elem.issparse == array.issparse)
            self.assertFalse((array != init_elem).any())

        self.logger.info("Initializing CArray with a CDense or an ndarray...")
        dense_list = [
            CDense([[2, 3], [22, 33]]),
            CDense([2, 3]),
            CDense([[2], [3]]),
            CDense([3]),
            np.array([[2, 3], [22, 33]]),
            np.array([2, 3]),
            np.array([[2], [3]]),
            np.array([3])
        ]

        for init_elem in dense_list:
            array = CArray(init_elem)
            self.assertTrue(array.isdense)
            self.assertTrue(array.shape == init_elem.shape)

        self.logger.info("Initializing CArray with a sparse CArray...")
        sparse_list = [
            CArray([[2, 3], [22, 33]], tosparse=True),
            CArray([2, 3], tosparse=True),
            CArray([[2], [3]], tosparse=True),
            CArray(3, tosparse=True)
        ]

        for init_elem in sparse_list:
            array = CArray(init_elem)
            self.assertTrue(array.issparse)
            self.assertFalse((array != init_elem).any())

        self.logger.info("Initializing CArray with a CSparse or csr_matrix...")
        sparse_list = [
            CSparse([[2, 3], [22, 33]]),
            CSparse([2, 3]),
            CSparse([[2], [3]]),
            CSparse([3]),
            scs.csr_matrix([[2, 3], [22, 33]]),
            scs.csr_matrix([2, 3]),
            scs.csr_matrix([[2], [3]]),
            scs.csr_matrix([3])
        ]

        for init_elem in sparse_list:
            array = CArray(init_elem)
            self.assertTrue(array.issparse)
            self.assertTrue(array.shape == init_elem.shape)