<commit_before>import os
import tempfile
import shutil
import unittest
import tables
import numpy

from simphony.cuds.lattice import Lattice
from simphony.cuds.lattice import LatticeNode
from simphony.cuds.lattice import make_hexagonal_lattice
from simphony.cuds.lattice import make_orthorombicp_lattice
from simphony.io.file_lattice import FileLattice
from numpy.testing import assert_array_equal
from numpy.testing import assert_equal
from simphony.testing.utils import compare_data_containers

from simphony.core.cuba import CUBA

from simphony.io.h5_cuds import H5CUDS


class CustomRecord(tables.IsDescription):

    class data(tables.IsDescription):

        material_id = tables.Int32Col(pos=0)
        velocity = tables.Float64Col(pos=1, shape=3)
        density = tables.Float64Col(pos=2)

    mask = tables.BoolCol(pos=1, shape=(3,))


class TestFileLattice(unittest.TestCase):

    def setUp(self):

        self.temp_dir = tempfile.mkdtemp()

        self.filename = os.path.join(self.temp_dir, 'test_file.cuds')
        self.file = H5CUDS.open(self.filename, mode='w')

        self.lattice = Lattice('test_lattice', 'Cubic', (1.0, 1.0, 1.0),
                               (4, 5, 6), (0.0, 0.0, 0.0))

        for i in xrange(4):
            for j in xrange(5):
                for k in xrange(6):
                    N = LatticeNode((i, j, k), {CUBA.MATERIAL_ID: 1})
                    self.lattice.update_node(N)

        for i in xrange(2):
            for j in xrange(3):
                for k in xrange(6):
                    N = LatticeNode((i+1, j+1, k),
                                    {CUBA.MATERIAL_ID: 0,
                                     CUBA.DENSITY: 1.0,
                                     CUBA.VELOCITY: [0.0, 0.0, 1.0]})
                    self.lattice.update_node(N)

        lat = self.lattice
        self.filelattice = FileLattice(self.file._handle, lat.name, lat.type,
                                       lat.base_vect, lat.size, lat.origin,
                                       CustomRecord)
        for N in lat.iter_nodes():
            self.filelattice.update_node(N)

        self.ortholat = make_orthorombicp_lattice('ortho', (1.0, 2.0, 3.0),
                                                  (2, 3, 4))
        self.orthofilelat = self.file.add_lattice(self.ortholat)
        self.hexalat = make_hexagonal_lattice('hexa2D', 1.0, (2, 3))
        self.hexafilelat = self.file.add_lattice(self.hexalat)

    def tearDown(self):
        if os.path.exists(self.filename):
            self.file.close()
        shutil.rmtree(self.temp_dir)

    def test_file_lattice_constructed_from_Lattice(self):
        """ Checks that FileLattice constructed from Lattice with a
        CustomRecord column description has correct attributes

        """
        self.assertEqual(self.filelattice.name, 'test_lattice')
        self.assertEqual(self.filelattice.type, 'Cubic')
        assert_array_equal(self.filelattice.base_vect,
                           numpy.array((1.0, 1.0, 1.0)))
        self.assertItemsEqual(self.filelattice.size, (4, 5, 6))
        assert_array_equal(self.filelattice.origin,
                           numpy.array((0.0, 0.0, 0.0)))

    def test_file_lattice_constructed_from_table_existing_in_file(self):
        """ Checks that FileLattice constructed from a known table already
        existing in H5CUDS-file provides correct attributes for FileLattice
        object

        """
        self.filelattice = FileLattice(self.file._handle, 'test_lattice')

        self.assertEqual(self.filelattice.name, 'test_lattice')
        self.assertEqual(self.filelattice.type, 'Cubic')
        assert_array_equal(self.filelattice.base_vect,
                           numpy.array((1.0, 1.0, 1.0)))
        self.assertItemsEqual(self.filelattice.size, (4, 5, 6))
        assert_array_equal(self.filelattice.origin,
                           numpy.array((0.0, 0.0, 0.0)))

    def test_get_node(self):
        """ Check that a LatticeNode can be retrieved correctly

        """
        N = self.filelattice.get_node((3, 3, 3))

        self.assertTrue(isinstance(N, LatticeNode))
        self.assertItemsEqual(N.index, (3, 3, 3))

        for key, value in self.lattice.get_node((3, 3, 3)).data.iteritems():
            assert_equal(N.data[key], value)

    def test_node_iterator(self):
        """ Checks the node iterator

        Checks that an iterator over all
        the nodes is returned when the function iter_nodes is called
        without arguments

        """
        fl_nodes = self.filelattice.iter_nodes()

        for M in fl_nodes:
            N = self.lattice.get_node(M.index)
            self.assertEqual(N.index, M.index)
            compare_data_containers(N.data, M.data, testcase=self)

    def test_node_iterator_subset(self):
        """ Checks the node iterator on a subset of nodes

        """
        fl_nodes = self.filelattice.iter_nodes([(0, 0, 0), (0, 1, 2)])

        for M in fl_nodes:
            N = self.lattice.get_node(M.index)
            self.assertEqual(N.index, M.index)
            compare_data_containers(N.data, M.data, testcase=self)

    def test_update_node(self):
        """ Check that a node can be updated correctly

        """
        N = LatticeNode((3, 2, 3),
                        {CUBA.MATERIAL_ID: 2, CUBA.DENSITY: 10.0,
                         CUBA.VELOCITY: (0.0, 0.0, 10.0)})

        self.filelattice.update_node(N)

        M = self.filelattice.get_node((3, 2, 3))

        self.assertEqual(N.index, M.index)
        compare_data_containers(N.data, M.data, testcase=self)

    def test_update_node_with_extra_keywords(self):
        """ Check that a node can be updated correctly when a LatticeNode has
        some CUBA-keys defined that do not exist in the FileLattice.

        """
        N = LatticeNode((2, 3, 4),
                        {CUBA.MATERIAL_ID: 2, CUBA.DENSITY: 10.0,
                         CUBA.VELOCITY: (0.0, 0.0, 100.0),
                         CUBA.DISTRIBUTION: (1.0, 1.1, 1.2, 1.3, 1.4)})

        self.filelattice.update_node(N)

        M = self.filelattice.get_node((2, 3, 4))

        self.assertEqual(N.index, M.index)
        for key in M.data:
            self.assertIn(key, M.data)
            assert_equal(N.data[key], M.data[key])

    def test_get_coordinate(self):
        """ Check that node coordinates are returned properly for various
        lattice types

        """
        assert_array_equal(self.filelattice.get_coordinate((1, 1, 1)),
                           numpy.array((1.0, 1.0, 1.0)))

        assert_array_equal(self.orthofilelat.get_coordinate((1, 1, 1)),
                           numpy.array((1.0, 2.0, 3.0)))

if __name__ == '__main__':
    unittest.main()<commit_msg>add tests for FileMesh using Full and Custom records<commit_after>import os
import tempfile
import shutil
import unittest
import tables
import numpy

from simphony.cuds.lattice import Lattice
from simphony.cuds.lattice import LatticeNode
from simphony.cuds.lattice import make_hexagonal_lattice
from simphony.cuds.lattice import make_orthorombicp_lattice
from simphony.io.file_lattice import FileLattice
from numpy.testing import assert_array_equal
from numpy.testing import assert_equal
from simphony.testing.utils import compare_data_containers
from simphony.core.cuba import CUBA
from simphony.io.h5_cuds import H5CUDS
from simphony.testing.abc_check_lattice import ABCCheckLattice


class CustomRecord(tables.IsDescription):

    class data(tables.IsDescription):

        material_id = tables.Int32Col(pos=0)
        velocity = tables.Float64Col(pos=1, shape=3)
        density = tables.Float64Col(pos=2)

    mask = tables.BoolCol(pos=1, shape=(3,))


class TestFileLattice(ABCCheckLattice, unittest.TestCase):
    """ Basic testing of the FileLattice.
    """

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.filename = os.path.join(self.temp_dir, 'test_file.cuds')
        self.addCleanup(self.cleanup)
        self.handle = H5CUDS.open(self.filename)
        ABCCheckLattice.setUp(self)

    def cleanup(self):
        if os.path.exists(self.filename):
            self.handle.close()
        shutil.rmtree(self.temp_dir)

    def container_factory(self, name, type_, base_vect, size, origin):
        lattice = Lattice(name, type_, base_vect, size, origin)
        self.handle.add_lattice(lattice)
        return self.handle.get_lattice(name)

    def supported_cuba(self):
        return set(CUBA)

    def test_initialization_from_existing_lattice_in_file(self):
        """ Checks that FileLattice constructed from a known table already
        existing in H5CUDS-file provides correct attributes for FileLattice
        object

        """
        lattice = FileLattice(self.handle._handle, 'foo')
        self.assertEqual(lattice.name, 'foo')
        self.assertEqual(lattice.type, 'Cubic')
        assert_array_equal(lattice.base_vect, self.base_vect)
        self.assertItemsEqual(lattice.size, self.size)
        assert_array_equal(lattice.origin, self.origin)


class TestFileLatticeCustom(ABCCheckLattice, unittest.TestCase):
    """ TestFileLattice using a custom record.

    """

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.filename = os.path.join(self.temp_dir, 'test_file.cuds')
        self.addCleanup(self.cleanup)
        self.handle = H5CUDS.open(self.filename)
        ABCCheckLattice.setUp(self)

    def cleanup(self):
        if os.path.exists(self.filename):
            self.handle.close()
        shutil.rmtree(self.temp_dir)

    def container_factory(self, name, type_, base_vect, size, origin):
        print base_vect
        return FileLattice(
            self.handle._handle, 'foo', type_, base_vect,
            size, origin, record=CustomRecord)

    def supported_cuba(self):
        return [CUBA.VELOCITY, CUBA.MATERIAL_ID, CUBA.DENSITY]


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.10507828181453231, 'fraction_numerical': 0.018867924528301886, 'mean_word_length': 2.3745343718252623, 'pattern_counts': {'":': 0, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2350769', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3143, 'n_words': 664}