示例#1
0
 def test_load_version_mismatch(self):
     """
     Loads file with a version that is too new.  Confirms warning is created.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/version_mismatch.gdf") as f:
         with self.assertWarns(Warning):
             easygdf.load(f)
示例#2
0
 def test_load_too_much_recursion(self):
     """
     Loads file with enough groups to cause recursion error.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/too_much_recursion.gdf") as f:
         with self.assertRaises(RecursionError):
             easygdf.load(f)
示例#3
0
 def test_load_invalid_dtype_array(self):
     """
     Loads file with an invalid dtype for an array block.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream(
             "easygdf.tests", "data/invalid_dtype_array.gdf") as f:
         with self.assertRaises(TypeError):
             easygdf.load(f)
示例#4
0
 def test_load_invalid_size_single(self):
     """
     Loads file with an invalid size for a single block.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream(
             "easygdf.tests", "data/invalid_size_single.gdf") as f:
         with self.assertRaises(ValueError):
             easygdf.load(f)
示例#5
0
 def test_load_group_begin_and_end(self):
     """
     Loads file with a block that has a group begin and group end bit set.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream(
             "easygdf.tests", "data/group_begin_and_end.gdf") as f:
         with self.assertRaises(ValueError):
             easygdf.load(f)
示例#6
0
 def test_load_end_root(self):
     """
     Loads file with an end block in the root group.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/group_end_root.gdf") as f:
         with self.assertRaises(ValueError):
             easygdf.load(f)
示例#7
0
 def test_load_wrong_magic_number(self):
     """
     Loads file with the wrong magic number.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/wrong_magic_number.gdf") as f:
         with self.assertRaises(easygdf.GDFIOError):
             easygdf.load(f)
示例#8
0
 def test_load_both_single_array(self):
     """
     Loads file with block that has both single and array bits set.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/both_single_array.gdf") as f:
         with self.assertRaises(ValueError):
             easygdf.load(f)
示例#9
0
 def test_load_null_array(self):
     """
     Confirms exception is thrown when loading file with array that has null dtype
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/null_array.gdf") as f:
         with self.assertRaises(TypeError):
             easygdf.load(f)
示例#10
0
 def test_load_unterminated_group(self):
     """
     Loads file with a group that is never ended correctly.  Confirms exception is thrown.
     :return:
     """
     # Attempt to open the file with the method under test
     with pkg_resources.resource_stream("easygdf.tests",
                                        "data/unterminated_group.gdf") as f:
         with self.assertRaises(IOError):
             easygdf.load(f)
示例#11
0
    def test_load_str_URI(self):
        """
        Attempts to load a file specified with a string instead of an open file
        :return: None
        """
        # Get the filename and open it with easygdf
        fname = pkg_resources.resource_filename("easygdf.tests",
                                                "data/test.gdf")
        all_data = easygdf.load(fname)

        # Write out the expected header
        fh = {
            'creation_time':
            datetime.datetime(2020,
                              11,
                              25,
                              17,
                              34,
                              24,
                              tzinfo=datetime.timezone.utc),
            'creator':
            'ASCI2GDF',
            'destination':
            '',
            'gdf_version': (1, 1),
            'creator_version': (1, 0),
            'destination_version': (0, 0),
            'dummy': (0, 0)
        }

        # Compare with what we got
        all_data.pop("blocks")
        self.assertEqual(all_data, fh)
示例#12
0
def trim_screens_tout():
    # Load the screen/tout file
    with open(screen_tout_file, "rb") as f:
        d = easygdf.load(f)

    # Trim down the blocks
    trimmed_blocks = []
    seen_touts = 0
    seen_screens = 0
    for b in d["blocks"]:
        if b["name"] == "position":
            if seen_screens < n_blocks:
                trimmed_blocks.append(b)
                seen_screens += 1

        elif b["name"] == "time":
            if seen_touts < n_blocks:
                trimmed_blocks.append(b)
                seen_touts += 1

        else:
            trimmed_blocks.append(b)

    # Trim down the arrays to the correct number of particles
    particle_blocks = []
    for b in trimmed_blocks:
        new_block = {"name": b["name"], "param": b["param"], "children": []}
        for c in b["children"]:
            new_block["children"].append({
                "name":
                c["name"],
                "param":
                round_sigfigs(c["param"][:n_particles], 4),
                "children": []
            })
        particle_blocks.append(new_block)
    d["blocks"] = particle_blocks

    # Save the trimmed down blocks
    with open(os.path.join(data_files_path, "screens_touts.gdf"), "wb") as f:
        easygdf.save(f, **d)

    # Convert the blocks to the format we expect
    screens_touts = {"screens": [], "touts": []}
    conv = {"time": "touts", "position": "screens"}
    for b in particle_blocks:
        # If it's a screen or tout, extract the children and add it
        if b["name"] in conv:
            group = {}
            for c in b["children"]:
                group[c["name"]] = c["param"]
            group[b["name"]] = b["param"]
            screens_touts[conv[b["name"]]].append(group)

        # If it's an auxiliary block, add it to the root
        else:
            screens_touts[b["name"]] = b["param"]

    # Convert it to
    print(screens_touts)
示例#13
0
def trim_initial_distribution():
    # Load the initial distribution file
    with open(initial_distribution_file, "rb") as f:
        d = easygdf.load(f)

    # Clip any arrays to the correct size
    trimmed_blocks = []
    for b in d["blocks"]:
        if isinstance(b["param"], np.ndarray):
            trimmed_blocks.append({
                "name":
                b["name"],
                "param":
                round_sigfigs(b["param"][:n_particles], 4)
            })
    d["blocks"] = trimmed_blocks

    # Save the file
    with open(os.path.join(data_files_path, "initial_distribution.gdf"),
              "wb") as f:
        easygdf.save(f, **d)

    # Create an example of the data structure we want
    ds = {x["name"]: x["param"] for x in d["blocks"]}
    d.pop("blocks")
    ds.update(d)
    print(ds)
示例#14
0
    def test_load_simple_header(self):
        """
        Attempts to load a known GDF file created with asci2gdf and checks the header against contents
        :return: None
        """
        # Attempt to open the file with the method under test
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/test.gdf") as f:
            all_data = easygdf.load(f)

        # Write out the expected header
        fh = {
            'creation_time':
            datetime.datetime(2020,
                              11,
                              25,
                              17,
                              34,
                              24,
                              tzinfo=datetime.timezone.utc),
            'creator':
            'ASCI2GDF',
            'destination':
            '',
            'gdf_version': (1, 1),
            'creator_version': (1, 0),
            'destination_version': (0, 0),
            'dummy': (0, 0)
        }

        # Compare with what we got
        all_data.pop("blocks")
        self.assertEqual(all_data, fh)
示例#15
0
    def test_header_write(self):
        """
        Saves a header and confirms it is the same after reading it back
        :return:
        """
        # Create a header to save
        my_time = datetime.datetime.fromtimestamp(int(
            datetime.datetime.timestamp(datetime.datetime.now())),
                                                  tz=datetime.timezone.utc)
        fh = {
            "creation_time": my_time,
            "creator": "easygdf",
            "destination": "hardgdf",
            "gdf_version": (1, 1),
            "creator_version": (3, 4),
            "destination_version": (5, 6),
            "dummy": (7, 8),
        }

        # Save it and reload it
        testfile = os.path.join(tempfile.gettempdir(), "save_header.gdf")
        with open(testfile, "wb") as f:
            easygdf.save(f, [], **fh)
        with open(testfile, "rb") as f:
            test = easygdf.load(f)

        # Check that the headers are the same
        test.pop("blocks")
        self.assertEqual(fh, test)
示例#16
0
    def test_load_simple_blocks(self):
        """
        Attempts to load a known GDF file created with asci2gdf and checks the blocks against known contents
        :return: None
        """
        # Attempt to open the file with the method under test
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/test.gdf") as f:
            all_data = easygdf.load(f)

        # Create the reference data
        reference_data = [
            {
                "name": "X",
                "value": np.linspace(0, 5, 6),
                "children": []
            },
            {
                "name": "Y",
                "value": np.linspace(5, 0, 6),
                "children": []
            },
        ]

        # Confirm that the data is the same
        self.assertEqual(len(all_data["blocks"]), len(reference_data))
        for ref_block, test_block in zip(reference_data, all_data["blocks"]):
            # Confirm that the key, param, and children are the same
            self.assertEqual(test_block["name"], ref_block["name"])
            np.testing.assert_allclose(test_block["value"], ref_block["value"])
            self.assertEqual(test_block["children"], ref_block["children"])
示例#17
0
    def test_save_all_dtypes(self):
        """
        Saves each possible valid datatype and
        :return:
        """
        # Make a place to hold blocks which we will write
        ref_blocks = []

        # Dump all of the possible numpy array types into blocks
        dtypes = [
            "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32",
            "uint64", "float_", "float32", "float64"
        ]
        for dtype in dtypes:
            ref_blocks.append({
                "name": "array_" + dtype,
                "value": np.linspace(0, 5, 6, dtype=dtype),
            })

        # Add each single type
        ref_blocks.append({
            "name": "single_str",
            "value": "deadbeef",
        })
        ref_blocks.append({
            "name": "single_int",
            "value": 1729,
        })
        ref_blocks.append({
            "name": "single_float",
            "value": 3.14,
        })
        ref_blocks.append({
            "name": "single_none",
            "value": None,
        })

        # Save everything as a GDF file
        testfile = os.path.join(tempfile.gettempdir(), "save_all_dtypes.gdf")
        with open(testfile, "wb") as f:
            easygdf.save(f, ref_blocks)

        # Read it back in
        with open(testfile, "rb") as f:
            data = easygdf.load(f)

        # Go through the blocks and make sure they match
        for test, ref in zip(data["blocks"], ref_blocks):
            self.assertEqual(test["name"], ref["name"])
            if isinstance(ref["value"], np.ndarray):
                np.testing.assert_equal(test["value"], ref["value"])
            else:
                self.assertEqual(test["value"], ref["value"])
示例#18
0
    def test_load_empty_group(self):
        """
        Attempts to load a file with an empty group.
        :return: None
        """
        # Attempt to open the file with the method under test
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/empty_group.gdf") as f:
            all_data = easygdf.load(f)

        # Check that the block shows up w/o children
        self.assertEqual(len(all_data["blocks"][0]["children"]), 0)
示例#19
0
    def test_load_nested_groups(self):
        """
        Loads file with multiple levels of groups.
        :return:
        """
        # Attempt to open the file with the method under test
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/nested_groups.gdf") as f:
            all_data = easygdf.load(f)

        self.assertEqual(
            all_data["blocks"][0]["children"][0]["children"][0]["value"],
            "deadbeef")
示例#20
0
    def test_uniform_interface(self):
        """
        This test confirms that the output of the load function can be dumped directly into the input of the save
        function of the library with no modifications.  This is one of my requirements for the library and would like a
        check on it.

        :return: None
        """
        # Open up one of our test problems
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/test.gdf") as f:
            all_data = easygdf.load(f)

        # Save it again
        test_file = os.path.join(tempfile.gettempdir(),
                                 "easygdf_interface_test.gdf")
        with open(test_file, "wb") as f:
            easygdf.save(f, **all_data)
示例#21
0
    def test_load_all_dtypes(self):
        """
        Loads file with every dtype represented and compares with expected values
        :return:
        """
        # Attempt to open the file with the method under test
        with pkg_resources.resource_stream("easygdf.tests",
                                           "data/all_dtypes.gdf") as f:
            all_data = easygdf.load(f)

        # Check each block to confirm we got the correct value(s) back
        blocks = all_data["blocks"]

        # Single valued types
        self.assertEqual(blocks[0]["value"], 0.0)
        self.assertEqual(blocks[1]["value"], 0.0)
        self.assertEqual(blocks[2]["value"], 0)
        self.assertEqual(blocks[3]["value"], 0)
        self.assertEqual(blocks[4]["value"], 0)
        self.assertEqual(blocks[5]["value"], 0)
        self.assertEqual(blocks[6]["value"], 0)
        self.assertEqual(blocks[7]["value"], 0)
        self.assertEqual(blocks[8]["value"], 0)
        self.assertEqual(blocks[9]["value"], 0)
        self.assertEqual(blocks[10]["value"], "deadbeef")
        self.assertEqual(blocks[11]["value"], None)
        self.assertEqual(blocks[12]["value"],
                         struct.pack("16s", bytes("deadbeef", "ascii")))

        # Array types
        np.testing.assert_equal(blocks[13]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[14]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[15]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[16]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[17]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[18]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[19]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[20]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[21]["value"], np.linspace(0, 5, 6))
        np.testing.assert_equal(blocks[22]["value"], np.linspace(0, 5, 6))
        self.assertEqual(blocks[23]["value"],
                         struct.pack("16s", bytes("deadbeef", "ascii")))
示例#22
0
    def test_save_groups(self):
        """
        Try some blocks that have children
        :return:
        """
        # The reference blocks
        ref = [{
            "name":
            "A",
            "value":
            0,
            "children": [{
                "name":
                "B",
                "value":
                "string",
                "children": [{
                    "name":
                    "C",
                    "value":
                    1.2,
                    "children": [{
                        "name": "D",
                        "value": "another string",
                        "children": []
                    }]
                }]
            }]
        }]

        # Write it and read it back
        testfile = os.path.join(tempfile.gettempdir(), "save_groups.gdf")
        with open(testfile, "wb") as f:
            easygdf.save(f, ref)
        with open(testfile, "rb") as f:
            test = easygdf.load(f)["blocks"]

        # Check that they match
        self.assertEqual(test, ref)
示例#23
0
import easygdf
import numpy as np

# Let's write an example file with a variety of data types
blocks = [{
    "name": "an array",
    "value": np.array([0, 1, 2, 3])
}, {
    "name": "a string",
    "value": "Hello world!"
}, {
    "name": "a group",
    "value": 3.14,
    "children": [{
        "name": "child",
        "value": 1.0
    }]
}]
easygdf.save("minimal.gdf", blocks)

# Now we'll read it back and print out some info about each block
d = easygdf.load("minimal.gdf")
for b in d["blocks"]:
    print("name='{0}'; value='{1}'; n_children={2}".format(
        b["name"], b["value"], len(b["children"])))