Esempio n. 1
0
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")
Esempio n. 2
0
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")
Esempio n. 3
0
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")
Esempio n. 4
0
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")
Esempio n. 5
0
def test_to_file_codes():
    buff = io.BytesIO()
    roff_param = RoffParameter(
        1,
        1,
        2,
        "a",
        b"\x01\xFF",
        code_names=["a", "b"],
        code_values=np.array([1, 2], dtype=np.int32),
    )
    roff_param.to_file(buff)
    buff.seek(0)
    vals = roffio.read(buff)
    assert np.array_equal(vals["parameter"]["codeNames"], np.array(["a", "b"]))
    assert np.array_equal(vals["parameter"]["codeValues"], np.array([1, 2]))
    assert vals["dimensions"] == {"nX": 1, "nY": 1, "nZ": 2}
Esempio n. 6
0
def test_xtgeo_values():
    assert np.array_equal(
        RoffParameter(1, 1, 2, "", b"\x01\xFF").xtgeo_values(undef=200),
        np.array([[[200, 1]]], dtype=np.int32),
    )
    assert np.array_equal(
        RoffParameter(1, 1, 2, "",
                      np.array([1, -999],
                               dtype=np.int32)).xtgeo_values(undef=200),
        np.array([[[200, 1]]], dtype=np.int32),
    )
    assert np.array_equal(
        RoffParameter(1, 1, 2, "",
                      np.array([1, -999.0],
                               dtype=np.float32)).xtgeo_values(undef=200),
        np.array([[[200, 1]]], dtype=np.float64),
    )
Esempio n. 7
0
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]))
Esempio n. 8
0
def roff_parameters(draw, dim=dimensions):
    dims = draw(dim)

    name = draw(names)
    is_discrete = draw(st.booleans())
    if is_discrete:
        num_codes = draw(st.integers(min_value=2, max_value=10))
        code_names = draw(
            st.lists(min_size=num_codes, max_size=num_codes, elements=names))
        code_values = np.array(
            draw(
                st.lists(unique=True,
                         elements=codes,
                         min_size=num_codes,
                         max_size=num_codes)),
            dtype=np.int32,
        )
        is_byte_values = draw(st.booleans())
        if is_byte_values:
            values = draw(
                arrays(
                    shape=dims[0] * dims[1] * dims[2],
                    dtype=np.uint8,
                    elements=st.sampled_from(code_values),
                )).tobytes()
        else:
            values = draw(
                arrays(
                    shape=dims[0] * dims[1] * dims[2],
                    dtype=np.int32,
                    elements=st.sampled_from(code_values),
                ))
        return RoffParameter(*dims, name, values, code_names, code_values)

    else:
        values = draw(
            arrays(shape=dims[0] * dims[1] * dims[2],
                   dtype=np.float32,
                   elements=finites))
        return RoffParameter(*dims, name, values)
Esempio n. 9
0
def test_roff_property_read_write(rpara):
    buff = io.BytesIO()
    rpara.to_file(buff)

    buff.seek(0)
    assert RoffParameter.from_file(buff, rpara.name) == rpara
Esempio n. 10
0
def test_to_file(tmp_path):
    roff_param = RoffParameter(1, 1, 2, "", b"\x01\xFF")
    roff_param.to_file(tmp_path / "param.roff")
    vals = roffio.read(tmp_path / "param.roff")
    assert vals["parameter"] == {"name": "", "data": b"\x01\xff"}
    assert vals["dimensions"] == {"nX": 1, "nY": 1, "nZ": 2}
Esempio n. 11
0
def test_is_discrete():
    assert RoffParameter(1, 1, 1, "", np.array([1])).is_discrete
    assert not RoffParameter(1, 1, 1, "", np.array([1.0])).is_discrete
    assert RoffParameter(1, 1, 1, "", b"\x01").is_discrete
Esempio n. 12
0
@given(roff_parameters(), roff_parameters(), roff_parameters())
def test_eq_transitivity(roff_param1, roff_param2, roff_param3):
    if roff_param1 == roff_param2 and roff_param2 == roff_param3:
        assert roff_param1 == roff_param3


@given(roff_parameters())
def test_eq_typing(roff_param):
    assert roff_param != ""


@pytest.mark.parametrize(
    "param, expected_undef",
    [
        (RoffParameter(1, 1, 1, "", np.array([1], dtype=np.int32)), -999),
        (RoffParameter(1, 1, 1, "", np.array([1.0])), -999.0),
        (RoffParameter(1, 1, 1, "", b"\x01"), 255),
    ],
)
def test_undefined_values(param, expected_undef):
    assert param.undefined_value == expected_undef


def test_is_discrete():
    assert RoffParameter(1, 1, 1, "", np.array([1])).is_discrete
    assert not RoffParameter(1, 1, 1, "", np.array([1.0])).is_discrete
    assert RoffParameter(1, 1, 1, "", b"\x01").is_discrete


@pytest.mark.parametrize(