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 == ''
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 == ''
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
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
def test_read_structured_binary_oommf_data(): """the actual function takes, as inputs, the filename; its byte at which data begins; its nodes values in a tuple; and the datatype (binary4, binary8 or ascii). Returns an array of the vectorfield. Utilises 'read_verification_tag' function.""" # test if ascii file given, exception is raised and print statement sent for i in range(len(ascii_files)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data(ascii_files[i], ascii_bytes[i], ascii_nodes[i], b'ascii') x = 0 except NotImplementedError: x = 1 if sysvers == 2: result_string = result.getvalue() assert result_string == "ascii -oommf data not supported \ here\n" assert x == 1 # test that an unknown data type is detected. for i in range(len(filenames)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data( filenames[i], bytes[i], filenames_nodes[i], filenames_data_types[i] + 'unknown') x = 0 except Exception: x = 1 if sysvers == 2: result_string = result.getvalue() assert result_string == "unknown datatype (expected 'binary4',\ 'binary8' [or 'ascii'], but got {}"\ .format(filenames_data_types[i]+'unknown\n') assert x == 1 # test that incorrect verification tag is detected for binary4 and... # ...binary8 files. Uses edited versions of files from 'filenames'... # ...which have tags 7.27159209092e+31 and 4.91466545592e+252... # ...rather than 1234567.0 and 123456789012345.0 respectively. b4_b8_files = [ os.path.join('..', 'Examples', 'cantedvortextest.omf'), os.path.join('..', 'Examples', 'ellipsoidwraptest.omf'), os.path.join('..', 'Examples', 'h2hleftedgetest.ohf'), os.path.join('..', 'Examples', 'yoyoleftedgetest.ohf'), os.path.join('..', 'Examples', 'stdprob3v-regtest.omf'), os.path.join('..', 'Examples', 'stdprobatest.omf') ] for i in range(len(b4_b8_files)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data(b4_b8_files[i], bytes[i], filenames_nodes[i], filenames_data_types[i]) x = 0 except AssertionError: x = 1 if sysvers == 2: result_string = result.getvalue() # if binary4, tag = 7.27159209092e+31 if i < 2 or i > 3: assert result_string == """The first item in a binary file is \ meant to be 1234567.0 but it is not. Instead, I read {}. Cowardly stopping here.\n""".format(7.271592090916425e+31) # if binary8, tag = 4.91466545592e+252 if 1 < i < 4: assert result_string == """The first item in a binary file is \ meant to be 123456789012345.0 but it is not. Instead, I read {}. Cowardly stopping here.\n""".format(4.91466545592e+252) assert x == 1 # test function output is as expected. # actual result for i in range(6): file = filenames[i] byte = bytes[i] nodes = filenames_nodes[i] data = filenames_data_types[i] act = omf.read_structured_binary_oommf_data(file, byte, nodes, data) # check result is a numpy array of correct length and shape assert type(act) == np.ndarray assert len(act) == filenames_nodes[i][0] * filenames_nodes[i][1] *\ filenames_nodes[i][2] assert [int(dim) for dim in act.shape] == [len(act), 3] # check data matches that of of orginal function exp = omfread_original.read_structured_binary_oommf_data( file, byte, nodes, data) assert act.all() == exp.all() # test verbose effects if sysvers == 2: for verbose in verboses: for i in range(6): file = filenames[i] byte = bytes[i] nodes = filenames_nodes[i] data = filenames_data_types[i] # the floatsizes of the first six files in 'filenames' floatsizes = [4, 4, 8, 8, 4, 4] floatsize = floatsizes[i] result = StringIO() sys.stdout = result omf.read_structured_binary_oommf_data(file, byte, nodes, data, verbose) result_string = result.getvalue() if verbose == 0: assert result_string == '' else: assert result_string == """Expect floats of length {} bytes. Expect to find data in file {} at position {}. verification_tag is okay (=> reading byte order correctly)\n"""\ .format(floatsize, file, byte)
def test_read_structured_binary_oommf_data(): """the actual function takes, as inputs, the filename; its byte at which data begins; its nodes values in a tuple; and the datatype (binary4, binary8 or ascii). Returns an array of the vectorfield. Utilises 'read_verification_tag' function.""" # test if ascii file given, exception is raised and print statement sent for i in range(len(ascii_files)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data(ascii_files[i], ascii_bytes[i], ascii_nodes[i], b'ascii') x = 0 except NotImplementedError: x = 1 if sysvers == 2: result_string = result.getvalue() assert result_string == "ascii -oommf data not supported \ here\n" assert x == 1 # test that an unknown data type is detected. for i in range(len(filenames)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data(filenames[i], bytes[i], filenames_nodes[i], filenames_data_types[i] + 'unknown') x = 0 except Exception: x = 1 if sysvers == 2: result_string = result.getvalue() assert result_string == "unknown datatype (expected 'binary4',\ 'binary8' [or 'ascii'], but got {}"\ .format(filenames_data_types[i]+'unknown\n') assert x == 1 # test that incorrect verification tag is detected for binary4 and... # ...binary8 files. Uses edited versions of files from 'filenames'... # ...which have tags 7.27159209092e+31 and 4.91466545592e+252... # ...rather than 1234567.0 and 123456789012345.0 respectively. b4_b8_files = [os.path.join('..', 'Examples', 'cantedvortextest.omf'), os.path.join('..', 'Examples', 'ellipsoidwraptest.omf'), os.path.join('..', 'Examples', 'h2hleftedgetest.ohf'), os.path.join('..', 'Examples', 'yoyoleftedgetest.ohf'), os.path.join('..', 'Examples', 'stdprob3v-regtest.omf'), os.path.join('..', 'Examples', 'stdprobatest.omf')] for i in range(len(b4_b8_files)): if sysvers == 2: result = StringIO() sys.stdout = result try: omf.read_structured_binary_oommf_data(b4_b8_files[i], bytes[i], filenames_nodes[i], filenames_data_types[i]) x = 0 except AssertionError: x = 1 if sysvers == 2: result_string = result.getvalue() # if binary4, tag = 7.27159209092e+31 if i < 2 or i > 3: assert result_string == """The first item in a binary file is \ meant to be 1234567.0 but it is not. Instead, I read {}. Cowardly stopping here.\n""".format(7.271592090916425e+31) # if binary8, tag = 4.91466545592e+252 if 1 < i < 4: assert result_string == """The first item in a binary file is \ meant to be 123456789012345.0 but it is not. Instead, I read {}. Cowardly stopping here.\n""".format(4.91466545592e+252) assert x == 1 # test function output is as expected. # actual result for i in range(6): file = filenames[i] byte = bytes[i] nodes = filenames_nodes[i] data = filenames_data_types[i] act = omf.read_structured_binary_oommf_data(file, byte, nodes, data) # check result is a numpy array of correct length and shape assert type(act) == np.ndarray assert len(act) == filenames_nodes[i][0] * filenames_nodes[i][1] *\ filenames_nodes[i][2] assert [int(dim) for dim in act.shape] == [len(act), 3] # check data matches that of of orginal function exp = omfread_original.read_structured_binary_oommf_data(file, byte, nodes, data) assert act.all() == exp.all() # test verbose effects if sysvers == 2: for verbose in verboses: for i in range(6): file = filenames[i] byte = bytes[i] nodes = filenames_nodes[i] data = filenames_data_types[i] # the floatsizes of the first six files in 'filenames' floatsizes = [4, 4, 8, 8, 4, 4] floatsize = floatsizes[i] result = StringIO() sys.stdout = result omf.read_structured_binary_oommf_data(file, byte, nodes, data, verbose) result_string = result.getvalue() if verbose == 0: assert result_string == '' else: assert result_string == """Expect floats of length {} bytes. Expect to find data in file {} at position {}. verification_tag is okay (=> reading byte order correctly)\n"""\ .format(floatsize, file, byte)