<commit_before>'''
Created on 22.01.2014

@author: marscher
'''
import unittest

import pystallone as st
import numpy as np

class TestPyStallone(unittest.TestCase):
    def setUp(self):
        # setup a random numpy array for each test case
        self.n = 100000
        self.a = np.random.random(self.n)
        
    def testConversionND_Float32(self):
        a = self.a.astype(np.float32)
        b = st.ndarray_to_stallone_array(a)

    def testConversionND_Float64(self):
        a = self.a.astype(np.float64)
        b = st.ndarray_to_stallone_array(a)
        
    def testConversionND_Int32(self):
        a = self.a.astype(np.int32)
        b = st.ndarray_to_stallone_array(a)
    
    def testConversionND_Int64(self):
        a = self.a.astype(np.int64)
        b = st.ndarray_to_stallone_array(a)
        
    def testIDoubleArray2ND_double1d(self):
        jarr = st.JArray(st.JDouble)(self.a)
        a = st.API.doublesNew.array(jarr)
        b = st.stallone_array_to_ndarray(a)
        
        self.assertEqual(type(self.a), type(b), 'differing types')
        self.assertEqual(self.a.dtype, b.dtype, 'differing datatypes')
        self.assertEqual(self.a.shape, b.shape, 'differing shape')
        self.assertTrue(np.allclose(self.a, b), 'conversion failed')
         
    def testIDoubleArray2ND_double2d(self):
        self.a = self.a.reshape(( 10, self.n/ 10 ))
        # create 2d Jarray
        jarr = st.JArray(st.JDouble, 2)(self.a)
        # wrap it
        a = st.API.doublesNew.array(jarr)
        # convert back
        b = st.stallone_array_to_ndarray(a)
        self.assertEqual(type(self.a), type(b), 'differing types')
        self.assertEqual(self.a.dtype, b.dtype, 'differing datatypes')
        self.assertEqual(self.a.shape, b.shape, 'differing shape')
        self.assertTrue(np.allclose(self.a, b), 'conversion failed')

    def testJArr2ND_int1d(self):
        self.a = np.random.randint(1, size=self.n)
        jarr = st.JArray(st.JInt)(self.a)
         
        a = st.API.intsNew.arrayFrom(jarr)
        b = st.stallone_array_to_ndarray(a)
         
        self.assertEqual(type(self.a), type(b), 'differing types')
        self.assertEqual(self.a.dtype, b.dtype, 'differing datatypes:'\
                         ' should be %s, but is %s' % (self.a.dtype, b.dtype))
        self.assertEqual(self.a.shape, b.shape, 'differing shape')
        self.assertTrue(np.allclose(self.a, b), 'conversion failed')
         
    def testJArr2ND_int2d(self):
        self.a = np.random.randint(1, size=self.n)
        self.a = self.a.reshape((10, self.n / 10))
        jarr = st.JArray(st.JInt, 2)(self.a)
        # FIXME: 2d jarray not compatible with table(int[][]) prototype?!
        print type(jarr)
        a = st.API.intsNew.table(jarr)
        b = st.stallone_array_to_ndarray(a)
        self.assertEqual(type(self.a), type(b), 'differing types')
        self.assertEqual(self.a.dtype, b.dtype, 'differing datatypes')
        self.assertEqual(self.a.shape, b.shape, 'differing shape')
        self.assertTrue(np.allclose(self.a, b), 'conversion failed')

if __name__ == "__main__":
    unittest.main()<commit_msg>[pystallone.test] moved asserts to functions<commit_after>'''
Created on 22.01.2014

@author: marscher
'''
import unittest

import pystallone as st
import numpy as np

class TestPyStallone(unittest.TestCase):
    def setUp(self):
        # setup a random numpy array for each test case
        self.n = 100000
        self.a = np.random.random(self.n)

    def convertToNPandCompare(self, stArray, npArray):
        """
        converts stArray to a numpy array and compares with npArray
        """
        newNPArr = st.stallone_array_to_ndarray(stArray)
        
        self.assertEqual(npArray.shape, newNPArr.shape, 'differing shape')
        self.assertTrue(np.allclose(npArray, newNPArr), 'conversion failed')
        self.assertEqual(npArray.dtype, newNPArr.dtype, 
                         'differing datatypes: is %s, should be %s' 
                          %(npArray.dtype, newNPArr.dtype))
        self.assertEqual(type(npArray), type(newNPArr), 'differing types')
        
    def compareNP(self, a, b):
        self.assertEqual(type(a), type(b), 'differing types: should be %s, but is %s' 
                         % (type(a), type(b)))
        self.assertEqual(a.dtype, b.dtype, 'differing datatypes:'\
                         ' should be %s, but is %s' % (a.dtype, b.dtype))
        self.assertEqual(a.shape, b.shape, 'differing shape: should be %s, but is %s'
                         % (a.shape, b.shape))
        self.assertTrue(np.allclose(a, b), 'conversion failed')


    def testConversionND_Float32(self):
        a = self.a.astype(np.float32)
        b = st.ndarray_to_stallone_array(a)
        self.convertToNPandCompare(b, a)

    def testConversionND_Float64(self):
        a = self.a.astype(np.float64)
        b = st.ndarray_to_stallone_array(a)
        self.convertToNPandCompare(b, a)


    def testConversionND_Int32(self):
        a = self.a.astype(np.int32)
        b = st.ndarray_to_stallone_array(a)
        self.convertToNPandCompare(b, a)
        

    def testConversionND_Int64(self):
        a = self.a.astype(np.int64)
        b = st.ndarray_to_stallone_array(a)
        self.convertToNPandCompare(b, a)

    def testIDoubleArray2ND_double1d(self):
        jarr = st.JArray(st.JDouble)(self.a)
        a = st.API.doublesNew.array(jarr)
        b = st.stallone_array_to_ndarray(a)
        self.compareNP(self.a, b)
    
    def testIDoubleArray2ND_double2d(self):
        self.a = self.a.reshape(( 10, self.n/ 10 ))
        # create 2d Jarray
        jarr = st.JArray(st.JDouble, 2)(self.a)
        # wrap it
        a = st.API.doublesNew.array(jarr)
        # convert back
        b = st.stallone_array_to_ndarray(a)
        
        self.compareNP(self.a, b)

    def testJArr2ND_int1d(self):
        self.a = np.random.randint(1, size=self.n)
        jarr = st.JArray(st.JInt)(self.a)
         
        a = st.API.intsNew.arrayFrom(jarr)
        b = st.stallone_array_to_ndarray(a)
         
        self.compareNP(self.a, b)
    
    def testJArr2ND_int2d(self):
        self.a = np.random.randint(1, size=self.n)
        self.a = self.a.reshape((10, self.n / 10))
        jarr = st.JArray(st.JInt, 2)(self.a)
        # FIXME: 2d jarray not compatible with table(int[][]) prototype?!
        print type(jarr)
        a = st.API.intsNew.table(jarr)
        b = st.stallone_array_to_ndarray(a)
        self.compareNP(self.a, b)

if __name__ == "__main__":
    unittest.main()
{'fraction_non_alphanumeric': 0.1261973544169074, 'fraction_numerical': 0.015812680553443818, 'mean_word_length': 2.369877049180328, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6485446', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2366, 'n_words': 428}