Example #1
0
def test_read_structured_omf_file():
    """Takes a file as an input and returns the vecorfield of data it contains
    """
    for i in range(len(filenames)):
        file = filenames[i]
        byte = bytes[i]
        nodes = filenames_nodes[i]
        data = filenames_data_types[i]
        # actual result
        act = omf.read_structured_omf_file(file)
        # all files should be retuned as numpy arrays
        assert type(act) == np.ndarray
        assert len(act) == nodes[0] * nodes[1] * nodes[2]
        assert [int(dim) for dim in act.shape] == [len(act), 3]

        # check binary4, binary8 files return expected data values
        if i < 6:
            exp = omf.read_structured_binary_oommf_data(
                file, byte, nodes, data)
        # check ascii files return expected data values
        else:
            exp = omf.read_structured_ascii_oommf_data(file, byte, nodes)
        assert exp.all() == act.all()

        # test print statements show if debug
        if sysvers == 2:
            debug = [0, 1]
            for db in debug:
                result = StringIO()
                sys.stdout = result
                omf.read_structured_omf_file(file, db)
                result_string = result.getvalue()
                # binary4 & binary8 files
                if debug == 1 and i < 6:
                    floatsizes = [4, 4, 8, 8, 4, 4]
                    floatsize = floatsizes[i]
                    assert result_string == """Number of cells \
(Nx={},Ny={},Nz={})
Expect floats of length {} bytes.
Expect to find data in file {} at position {}.
verification_tag is okay (=> reading byte order correctly)\n"""\
.format(nodes[0], nodes[1], nodes[2], floatsize, file, byte)
                # ascii files
                elif debug == 1 and i > 5:
                    assert result_string == """Number of cells \
(Nx={},Ny={},Nz={})\n""".format(nodes[0], nodes[1], nodes[2])
                # debug = False
                elif debug == 0:
                    assert result_string == ''
Example #2
0
def test_read_structured_omf_file():
    """Takes a file as an input and returns the vecorfield of data it contains
    """
    for i in range(len(filenames)):
        file = filenames[i]
        byte = bytes[i]
        nodes = filenames_nodes[i]
        data = filenames_data_types[i]
        # actual result
        act = omf.read_structured_omf_file(file)
        # all files should be retuned as numpy arrays
        assert type(act) == np.ndarray
        assert len(act) == nodes[0] * nodes[1] * nodes[2]
        assert [int(dim) for dim in act.shape] == [len(act), 3]

        # check binary4, binary8 files return expected data values
        if i < 6:
            exp = omf.read_structured_binary_oommf_data(file, byte, nodes,
                                                        data)
        # check ascii files return expected data values
        else:
            exp = omf.read_structured_ascii_oommf_data(file, byte, nodes)
        assert exp.all() == act.all()

        # test print statements show if debug
        if sysvers == 2:
            debug = [0, 1]
            for db in debug:
                result = StringIO()
                sys.stdout = result
                omf.read_structured_omf_file(file, db)
                result_string = result.getvalue()
                # binary4 & binary8 files
                if debug == 1 and i < 6:
                    floatsizes = [4, 4, 8, 8, 4, 4]
                    floatsize = floatsizes[i]
                    assert result_string == """Number of cells \
(Nx={},Ny={},Nz={})
Expect floats of length {} bytes.
Expect to find data in file {} at position {}.
verification_tag is okay (=> reading byte order correctly)\n"""\
.format(nodes[0], nodes[1], nodes[2], floatsize, file, byte)
                # ascii files
                elif debug == 1 and i > 5:
                    assert result_string == """Number of cells \
(Nx={},Ny={},Nz={})\n""".format(nodes[0], nodes[1], nodes[2])
                # debug = False
                elif debug == 0:
                    assert result_string == ''
Example #3
0
def test_read_structured_oommf_data():
    """ function takes a file, the byte at which data starts, the nodes of the
    data and the data type, returns a vectorfield."""
    for i in range(len(filenames)):
        file = filenames[i]
        byte = bytes[i]
        nodes = filenames_nodes[i]
        data = filenames_data_types[i]
        # actual result
        act = omf.read_structured_oommf_data(file, byte, nodes, data)
        # all files retuned as numpy arrays
        assert type(act) == np.ndarray
        assert len(act) == nodes[0] * nodes[1] * nodes[2]
        assert [int(dim) for dim in act.shape] == [len(act), 3]

        # check binary4, binary8 files return expected data values
        if i < 6:
            exp = omf.read_structured_binary_oommf_data(
                file, byte, nodes, data)
        # check ascii files return expected data values
        else:
            exp = omf.read_structured_ascii_oommf_data(file, byte, nodes)
        assert exp.all() == act.all()

        # test function detects unexpected datatype
        if sysvers == 2:
            data_unknown = data + 'unknown'
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_oommf_data(file, byte, nodes, data + 'unknown')
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """expected ascii or binary4 or binary8 for\
 datatype, but got {}\n""".format(data_unknown)
        assert x == 1
Example #4
0
def test_read_structured_oommf_data():
    """ function takes a file, the byte at which data starts, the nodes of the
    data and the data type, returns a vectorfield."""
    for i in range(len(filenames)):
        file = filenames[i]
        byte = bytes[i]
        nodes = filenames_nodes[i]
        data = filenames_data_types[i]
        # actual result
        act = omf.read_structured_oommf_data(file, byte, nodes, data)
        # all files retuned as numpy arrays
        assert type(act) == np.ndarray
        assert len(act) == nodes[0] * nodes[1] * nodes[2]
        assert [int(dim) for dim in act.shape] == [len(act), 3]

        # check binary4, binary8 files return expected data values
        if i < 6:
            exp = omf.read_structured_binary_oommf_data(file, byte, nodes,
                                                        data)
        # check ascii files return expected data values
        else:
            exp = omf.read_structured_ascii_oommf_data(file, byte, nodes)
        assert exp.all() == act.all()

        # test function detects unexpected datatype
        if sysvers == 2:
            data_unknown = data+'unknown'
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_oommf_data(file, byte, nodes, data+'unknown')
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """expected ascii or binary4 or binary8 for\
 datatype, but got {}\n""".format(data_unknown)
        assert x == 1
Example #5
0
def test_read_structured_ascii_oommf_data():
    """the actual function takes as inputs, the filename, its byte at which
    data begins, and its nodes values in a tuple. Returns an array of the
    vectorfield."""

    # test ascii files that don't end data with '# End: Data Text'.
    # These files instead have '# test'.
    # contain same data as equivalent files small/spiral/plate.omf
    # function should recognise incorrect format.
    ascii_test_files = [
        os.path.join('..', 'Examples', 'smalltest.omf'),
        os.path.join('..', 'Examples', 'platetest.omf'),
        os.path.join('..', 'Examples', 'spiraltest.omf')
    ]
    test_bytes = bytes[6:]
    test_nodes = filenames_nodes[6:]
    for i in range(len(ascii_test_files)):
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_ascii_oommf_data(ascii_test_files[i],
                                                 test_bytes[i], test_nodes[i])
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """I found a # in the first column. \
Complete row is # test
I only expected '# End: Data Text'.
cowardly stopping here\n"""
        assert x == 1

    # test function detects if a vector has more or less than 3 components
    # use example file created
    example_file = os.path.join('..', 'Examples', 'smalltest2.omf')
    if sysvers == 2:
        result = StringIO()
        sys.stdout = result
    try:
        omf.read_structured_ascii_oommf_data(example_file, 488, (5, 3, 1))
        x = 0
    except Exception:
        x = 1
        if sysvers == 2:
            result_string = result.getvalue()
            assert result_string == """vector_str= ['-0.89075911', '0.01617681']
vector    = [-0.89075911, 0.01617681]
datum = -0.89075911     0.01617681   \n"""
    assert x == 1

    # test if too much/too little data is detected and correct output is...
    # ...returned.
    # Files created that have too little/too much data
    unexp_data = [
        os.path.join('..', 'Examples', 'smallmuchdata.omf'),
        os.path.join('..', 'Examples', 'platemuchdata.omf'),
        os.path.join('..', 'Examples', 'spiralmuchdata.omf'),
        os.path.join('..', 'Examples', 'smalllittledata.omf'),
        os.path.join('..', 'Examples', 'platelittledata.omf'),
        os.path.join('..', 'Examples', 'spirallittledata.omf')
    ]
    unexp_bytes = bytes[6:] * 2
    unexp_nodes = filenames_nodes[6:] * 2
    for i in range(len(unexp_data)):
        node_product = unexp_nodes[i][0] * unexp_nodes[i][1] * unexp_nodes[i][2]
        # the files either have +1 vector or -1 vector than expected
        if i < 3:
            actual_nodes = node_product + 1
        else:
            actual_nodes = node_product - 1
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_ascii_oommf_data(unexp_data[i], unexp_bytes[i],
                                                 unexp_nodes[i])
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """Hmm, expected nx*ny*ny = {} items, \
but got {} .
Cowardly stopping here.\n""".format(node_product, actual_nodes)
        assert x == 1

    # test expected output if ascii file correct format.
    for i in range(len(ascii_files)):
        node_product = ascii_nodes[i][0] * ascii_nodes[i][1] * ascii_nodes[i][2]
        # expected result
        exp = omfread_original.read_structured_ascii_oommf_data(
            ascii_files[i], ascii_bytes[i], ascii_nodes[i])
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        # actual result
        act = omf.read_structured_ascii_oommf_data(ascii_files[i],
                                                   ascii_bytes[i],
                                                   ascii_nodes[i])
        if sysvers == 2:
            result_string = result.getvalue()
            assert result_string == "Hint: Reading ASCII-OOMMF file is slow (that\
 could be changed) and the files are large. Why not save data as binary?\n"

        # check ouput is a numpy array of correct length
        assert type(act) == np.ndarray
        assert len(act) == node_product
        # check data identical to original version
        assert act.all() == exp.all()
Example #6
0
def test_read_structured_ascii_oommf_data():
    """the actual function takes as inputs, the filename, its byte at which
    data begins, and its nodes values in a tuple. Returns an array of the
    vectorfield."""

    # test ascii files that don't end data with '# End: Data Text'.
    # These files instead have '# test'.
    # contain same data as equivalent files small/spiral/plate.omf
    # function should recognise incorrect format.
    ascii_test_files = [os.path.join('..', 'Examples', 'smalltest.omf'),
                        os.path.join('..', 'Examples', 'platetest.omf'),
                        os.path.join('..', 'Examples', 'spiraltest.omf')]
    test_bytes = bytes[6:]
    test_nodes = filenames_nodes[6:]
    for i in range(len(ascii_test_files)):
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_ascii_oommf_data(ascii_test_files[i],
                                                 test_bytes[i],
                                                 test_nodes[i])
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """I found a # in the first column. \
Complete row is # test
I only expected '# End: Data Text'.
cowardly stopping here\n"""
        assert x == 1

    # test function detects if a vector has more or less than 3 components
    # use example file created
    example_file = os.path.join('..', 'Examples', 'smalltest2.omf')
    if sysvers == 2:
        result = StringIO()
        sys.stdout = result
    try:
        omf.read_structured_ascii_oommf_data(example_file, 488, (5, 3, 1))
        x = 0
    except Exception:
        x = 1
        if sysvers == 2:
            result_string = result.getvalue()
            assert result_string == """vector_str= ['-0.89075911', '0.01617681']
vector    = [-0.89075911, 0.01617681]
datum = -0.89075911     0.01617681   \n"""
    assert x == 1

    # test if too much/too little data is detected and correct output is...
    # ...returned.
    # Files created that have too little/too much data
    unexp_data = [os.path.join('..', 'Examples', 'smallmuchdata.omf'),
                  os.path.join('..', 'Examples', 'platemuchdata.omf'),
                  os.path.join('..', 'Examples', 'spiralmuchdata.omf'),
                  os.path.join('..', 'Examples', 'smalllittledata.omf'),
                  os.path.join('..', 'Examples', 'platelittledata.omf'),
                  os.path.join('..', 'Examples', 'spirallittledata.omf')]
    unexp_bytes = bytes[6:] * 2
    unexp_nodes = filenames_nodes[6:] * 2
    for i in range(len(unexp_data)):
        node_product = unexp_nodes[i][0]*unexp_nodes[i][1]*unexp_nodes[i][2]
        # the files either have +1 vector or -1 vector than expected
        if i < 3:
            actual_nodes = node_product + 1
        else:
            actual_nodes = node_product - 1
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        try:
            omf.read_structured_ascii_oommf_data(unexp_data[i],
                                                 unexp_bytes[i],
                                                 unexp_nodes[i])
            x = 0
        except Exception:
            x = 1
            if sysvers == 2:
                result_string = result.getvalue()
                assert result_string == """Hmm, expected nx*ny*ny = {} items, \
but got {} .
Cowardly stopping here.\n""".format(node_product, actual_nodes)
        assert x == 1

    # test expected output if ascii file correct format.
    for i in range(len(ascii_files)):
        node_product = ascii_nodes[i][0]*ascii_nodes[i][1]*ascii_nodes[i][2]
        # expected result
        exp = omfread_original.read_structured_ascii_oommf_data(ascii_files[i],
                                                                ascii_bytes[i],
                                                                ascii_nodes[i])
        if sysvers == 2:
            result = StringIO()
            sys.stdout = result
        # actual result
        act = omf.read_structured_ascii_oommf_data(ascii_files[i],
                                                   ascii_bytes[i],
                                                   ascii_nodes[i])
        if sysvers == 2:
            result_string = result.getvalue()
            assert result_string == "Hint: Reading ASCII-OOMMF file is slow (that\
 could be changed) and the files are large. Why not save data as binary?\n"
        # check ouput is a numpy array of correct length
        assert type(act) == np.ndarray
        assert len(act) == node_product
        # check data identical to original version
        assert act.all() == exp.all()