def test_uniform_interface(self):
        # Try to load the file
        with pkg_resources.resource_stream(
                "easygdf.tests", "data/initial_distribution.gdf") as f:
            all_data = easygdf.load_initial_distribution(f)

        # Directly save it
        test_file = os.path.join(
            tempfile.gettempdir(),
            "save_initial_distributionuniform_interface.gdf")
        with open(test_file, "wb") as f:
            easygdf.save_initial_distribution(f, **all_data)
    def test_load(self):
        # Try to load the file
        with pkg_resources.resource_stream(
                "easygdf.tests", "data/initial_distribution.gdf") as f:
            all_data = easygdf.load_initial_distribution(f)

        # Confirm that the keys are the same
        self.assertEqual(self.ref.keys(), all_data.keys())
        for k in self.ref:
            if isinstance(self.ref[k], np.ndarray):
                np.testing.assert_almost_equal(self.ref[k], all_data[k])
            else:
                self.assertEqual(self.ref[k], all_data[k])
    def test_integer_casting(self):
        """
        Confirms that integer 64 bit arrays get cast to 32 bits and saved.  This is related to github issue 5 because
        GDF appears to not support 64 bit integers.

        :return:
        """
        # Test conversion from int64 -> int32
        test_file = os.path.join(
            tempfile.gettempdir(),
            "save_initial_distribution_test_integer_casting_1.gdf")
        with open(test_file, "wb") as f:
            easygdf.save(f, [
                {
                    'name': 'ID',
                    'value': np.zeros(32, dtype=np.int64),
                    'children': []
                },
            ])
        self.assertEqual(
            easygdf.load_initial_distribution(test_file)['ID'].dtype,
            np.dtype('int32'))

        # Test conversion from uint64 -> uint32
        test_file = os.path.join(
            tempfile.gettempdir(),
            "save_initial_distribution_test_integer_casting_2.gdf")
        with open(test_file, "wb") as f:
            easygdf.save(f, [
                {
                    'name': 'ID',
                    'value': np.zeros(32, dtype=np.uint64),
                    'children': []
                },
            ])
        self.assertEqual(
            easygdf.load_initial_distribution(test_file)['ID'].dtype,
            np.dtype('uint32'))
    def test_save_length_normalization_B(self):
        # Write it to the temp directory
        test_file = os.path.join(
            tempfile.gettempdir(),
            "save_initial_distribution_length_normalization_B.gdf")
        with open(test_file, "wb") as f:
            easygdf.save_initial_distribution(f, Bx=np.linspace(0, 1, 11))

        # Read it back
        with open(test_file, "rb") as f:
            all_data = easygdf.load_initial_distribution(f)

        # Check array lengths
        arr_names = ['x', 'y', 'z', 'Bx', 'By', 'Bz']
        for a in arr_names:
            self.assertEqual(all_data[a].size, 11)
    def test_save(self):
        # Write it to the temp directory
        test_file = os.path.join(tempfile.gettempdir(),
                                 "save_initial_distribution.gdf")
        with open(test_file, "wb") as f:
            easygdf.save_initial_distribution(f, **self.ref)

        # Read it back
        with open(test_file, "rb") as f:
            all_data = easygdf.load_initial_distribution(f)

        # Confirm that the keys are the same
        self.assertEqual(self.ref.keys(), all_data.keys())
        for k in self.ref:
            if isinstance(self.ref[k], np.ndarray):
                np.testing.assert_almost_equal(self.ref[k], all_data[k])
            else:
                self.assertEqual(self.ref[k], all_data[k])
    def test_save2(self):
        """
        Tests saving another valid input.  Difference between this and test_save is that we use velocity as well as all
        optional elements.
        :return:
        """
        # Write it to the temp directory
        test_file = os.path.join(tempfile.gettempdir(),
                                 "save_initial_distribution2.gdf")
        with open(test_file, "wb") as f:
            easygdf.save_initial_distribution(f, **self.ref2)

        # Read it back
        with open(test_file, "rb") as f:
            all_data = easygdf.load_initial_distribution(f)

        # Confirm that the keys are the same
        self.assertEqual(self.ref2.keys(), all_data.keys())
        for k in self.ref2:
            if isinstance(self.ref2[k], np.ndarray):
                np.testing.assert_almost_equal(self.ref2[k], all_data[k])
            else:
                self.assertEqual(self.ref2[k], all_data[k])