def test_default_values(roff_grid): buff = io.BytesIO() roff_grid.to_file(buff) buff.seek(0) values = roffio.read(buff) del values["translate"] del values["scale"] if "subgrids" in values: del values["subgrids"] del values["active"] buff2 = io.BytesIO() roffio.write(buff2, values) buff2.seek(0) roff_grid2 = RoffGrid.from_file(buff2) assert roff_grid2.xoffset == 0.0 assert roff_grid2.yoffset == 0.0 assert roff_grid2.zoffset == 0.0 assert roff_grid2.xscale == 1.0 assert roff_grid2.yscale == 1.0 assert roff_grid2.zscale == -1.0 assert roff_grid2.subgrids is None assert np.array_equal( roff_grid2.active, np.ones(roff_grid.nx * roff_grid.ny * roff_grid.nz, dtype=np.bool_), )
def to_file(self, filelike, roff_format=roffio.Format.BINARY): """ Writes the RoffParameter to a roff file Args: filelike (str or byte stream): The file to write to. roff_format (roffio.Format): The format to write the file in. """ data = OrderedDict({ "filedata": { "filetype": "parameter" }, "dimensions": { "nX": self.nx, "nY": self.ny, "nZ": self.nz }, "parameter": { "name": self.name }, }) if self.code_names is not None: data["parameter"]["codeNames"] = list(self.code_names) if self.code_values is not None: data["parameter"]["codeValues"] = self.code_values data["parameter"]["data"] = self.values roffio.write(filelike, data, roff_format=roff_format)
def test_from_file_no_filedata(simple_roff_parameter_contents): buff = io.BytesIO() del simple_roff_parameter_contents[0] roffio.write(buff, simple_roff_parameter_contents) buff.seek(0) with pytest.raises(ValueError, match="issing non-optional keyword"): RoffParameter.from_file(buff, "b")
def test_from_file_wrong_filetype(simple_roff_parameter_contents): buff = io.BytesIO() simple_roff_parameter_contents[0][1]["filetype"] = "unknown" roffio.write(buff, simple_roff_parameter_contents) buff.seek(0) with pytest.raises(ValueError, match="did not have filetype"): RoffParameter.from_file(buff, "b")
def test_from_file_missing_parameter(simple_roff_parameter_contents): buff = io.BytesIO() simple_roff_parameter_contents[0][1] roffio.write(buff, simple_roff_parameter_contents) buff.seek(0) with pytest.raises(ValueError, match="Did not find parameter"): RoffParameter.from_file(buff, "c")
def test_read_write_warn_cast(): buff = io.BytesIO() contents = {"t": {"a": np.array([1, 2], dtype=np.int64)}} with pytest.warns(UserWarning, match="cast"): roffio.write(buff, contents) buff.seek(0) assert np.array_equal(roffio.read(buff)["t"]["a"], np.array([1, 2], dtype=np.int32))
def test_write_adds_metadata(): f = io.BytesIO() roffio.write(f, {}) f.seek(0) read_contents = roffio.read(f) assert read_contents["version"]["major"] == 2 assert read_contents["version"]["minor"] == 0 assert read_contents["filedata"]["byteswaptest"] == 1
def test_from_file_param_last(simple_roff_parameter_contents): buff = io.BytesIO() roffio.write(buff, simple_roff_parameter_contents) buff.seek(0) roff_param = RoffParameter.from_file(buff, "b") assert np.array_equal(roff_param.name, "b") assert np.array_equal(roff_param.values, np.array([2.0]))
def test_not_a_grid(roff_grid): buff = io.BytesIO() roff_grid.to_file(buff) buff.seek(0) values = roffio.read(buff) values["filedata"]["filetype"] = "notgrid" buff.seek(0) roffio.write(buff, values) buff.seek(0) with pytest.raises(ValueError, match="did not have filetype set to grid"): RoffGrid.from_file(buff)
def test_from_file_double_dimensions(simple_roff_parameter_contents): buff = io.BytesIO() simple_roff_parameter_contents.append(("dimensions", { "nX": 2, "nY": 2, "nZ": 2 })) roffio.write(buff, simple_roff_parameter_contents) buff.seek(0) with pytest.raises(ValueError, match="Multiple tag"): RoffParameter.from_file(buff, "b")
def test_read_write_multitag(roff_format, buffer): contents = [ ("tagname", {"keyname": 1.0}), ("tagname", {"keyname": 2.0}), ] roffio.write(buffer, contents, roff_format=roff_format) buffer.seek(0) values = roffio.read(buffer) assert values["tagname"] == [{"keyname": 1.0}, {"keyname": 2.0}]
def test_read_write_list(roff_format, filelike): data = {"t": {"k": ["a", "b"]}} roffio.write(filelike, data, roff_format=roff_format) filelike.seek(0) read_contents = roffio.read(filelike) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") read_contents["t"]["k"] = list(read_contents["t"]["k"]) assert read_contents == data
def test_binary_write_read_is_ascii_write_read(roff_contents): bf = io.BytesIO() af = io.StringIO() roffio.write(bf, roff_contents, roff_format=roffio.Format.BINARY) roffio.write(af, roff_contents, roff_format=roffio.Format.ASCII) bf.seek(0) af.seek(0) read_binary_contents = roffio.read(bf) read_ascii_contents = roffio.read(af) read_binary_contents.pop("filedata") read_ascii_contents.pop("filedata") assert read_binary_contents == read_ascii_contents
def test_read_write_multikey(roff_format, buffer): contents = { "tagname": [ ("keyname", 1.0), ("keyname", 2.0), ], } roffio.write(buffer, contents, roff_format=roff_format) buffer.seek(0) values = roffio.read(buffer) assert values["tagname"] == {"keyname": [1.0, 2.0]}
def test_read_write_filestr(tmpdir, roff_data): filepath = os.path.join(tmpdir, "data.roff") roffio.write(filepath, roff_data) read_contents = roffio.read(filepath) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def test_read_write_is_identity(roff_data): f = io.BytesIO() roffio.write(f, roff_data) f.seek(0) read_contents = roffio.read(f) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def test_read_write_pathlib(tmp_path, roff_data): filepath = tmp_path / "data.roff" roffio.write(filepath, roff_data) read_contents = roffio.read(filepath) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def to_file(self, filelike, roff_format=roffio.Format.BINARY): """ Writes the RoffGrid to a roff file Args: filelike (str or byte stream): The file to write to. """ data = { "filedata": { "filetype": "grid" }, "dimensions": { "nX": self.nx, "nY": self.ny, "nZ": self.nz }, "translate": { "xoffset": np.float32(self.xoffset), "yoffset": np.float32(self.yoffset), "zoffset": np.float32(self.zoffset), }, "scale": { "xscale": np.float32(self.xscale), "yscale": np.float32(self.yscale), "zscale": np.float32(self.zscale), }, "cornerLines": { "data": self.corner_lines }, "zvalues": { "data": self.zvals }, "active": { "data": self.active }, } if self.subgrids is not None: data["subgrids"] = {"nLayers": self.subgrids} if self.split_enz is not None: data["zvalues"]["splitEnz"] = self.split_enz roffio.write(filelike, data, roff_format=roff_format)
def test_overwrite_byteswaptest_errors(): with pytest.raises(ValueError, match="not possible to set the byteswaptest"): roffio.write(io.BytesIO(), {"filedata": {"byteswaptest": -1}})
def test_overwrite_version_minor_errors(): with pytest.raises(ValueError, match="change roff file version"): roffio.write(io.BytesIO(), {"version": {"minor": -1}})
def test_overwrite_filetype(): f = io.BytesIO() roffio.write(f, {"filedata": {"filetype": "surface"}}) f.seek(0) assert roffio.read(f)["filedata"]["filetype"] == "surface"
def test_overwrite_creation_date(): f = io.BytesIO() roffio.write(f, {"filedata": {"creationDate": "today"}}) f.seek(0) assert roffio.read(f)["filedata"]["creationDate"] == "today"
def test_missing_non_optionals(): buff = io.BytesIO() roffio.write(buff, {"filedata": {"filetype": "grid"}}) buff.seek(0) with pytest.raises(ValueError, match="Missing non-optional"): RoffGrid.from_file(buff)
def test_just_one_eof(): f = io.BytesIO() roffio.write(f, {"eof": {}}) f.seek(0) assert roffio.read(f)["eof"] == {}