Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP: Give deprecation warning for arguments in kwargs #701

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions src/fmu/dataio/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,25 @@ def read_metadata(filename: str | Path) -> dict:
class ExportData:
"""Class for exporting data with rich metadata in FMU.

This class sets up the general metadata content to be applied in export. The idea is
that one ExportData instance can be re-used for several similar export() jobs. For
example::

edata = dataio.ExportData(
config=CFG, content="depth", unit="m", vertical_domain={"depth": "msl"},
timedata=None, is_prediction=True, is_observation=False,
tagname="faultlines", workflow="rms structural model",
)
This class sets up the general metadata content to be applied in export.
For example::

for name in ["TopOne", TopTwo", "TopThree"]:
poly = xtgeo.polygons_from_roxar(PRJ, hname, POL_FOLDER)

out = ed.export(poly, name=name)

Almost all keyword settings like ``name``, ``tagname`` etc can be set in both the
ExportData instance and directly in the ``generate_metadata`` or ``export()``
function, to provide flexibility for different use cases. If both are set, the
``export()`` setting will win followed by ``generate_metadata() and finally
ExportData()``.
ed = dataio.ExportData(
config=CFG,
content="depth",
unit="m",
vertical_domain={"depth": "msl"},
timedata=None,
is_prediction=True,
is_observation=False,
tagname="faultlines",
workflow="rms structural model",
name=name
)
out = ed.export(poly)

A note on 'pwd' and 'rootpath' and 'casepath': The 'pwd' is the process working
directory, which is folder where the process (script) starts. The 'rootpath' is the
Expand Down Expand Up @@ -707,6 +706,9 @@ def _update_check_settings(self, newsettings: dict) -> None:
self._validate_and_establish_fmucontext()
self._rootpath = self._establish_rootpath()

self._classification = self._get_classification()
self._rep_include = self._get_rep_include()
Comment on lines +709 to +710
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into the update_check_settings() as it is only needed to run if any of classification/rep_include/access_ssdl is coming in through kwargs. And hence it can be removed together with the entire update_check_settings() method when the deprecation period has ended.


def _establish_rootpath(self) -> Path:
"""
Establish the rootpath. The rootpath is the folder that acts as the
Expand Down Expand Up @@ -774,8 +776,8 @@ def generate_metadata(
Args:
obj: XTGeo instance, a Pandas Dataframe instance or other supported object.
compute_md5: If True, compute a MD5 checksum for the exported file.
**kwargs: For other arguments, see ExportData() input keys. If they
exist both places, this function will override!
**kwargs: Using other ExportData() input keys is now deprecated, input the
arguments when initializing the ExportData() instance instead.

Returns:
A dictionary with all metadata.
Expand All @@ -789,7 +791,14 @@ def generate_metadata(
logger.info("Generate metadata...")
logger.info("KW args %s", kwargs)

self._update_check_settings(kwargs)
if kwargs:
warnings.warn(
"In the future it will not be possible to enter following arguments "
f"inside the export() / generate_metadata() methods: {list(kwargs)}. "
"Please move them up to initialization of the ExportData instance.",
FutureWarning,
)
self._update_check_settings(kwargs)

if isinstance(obj, (str, Path)):
if self.casepath is None:
Expand All @@ -800,9 +809,6 @@ def generate_metadata(
is_observation=self.is_observation,
).generate_metadata(obj)

self._classification = self._get_classification()
self._rep_include = self._get_rep_include()

self._update_fmt_flag()
fmudata = self._get_fmu_provider() if self._fmurun else None

Expand All @@ -827,8 +833,8 @@ def export(

Args:
obj: XTGeo instance, a Pandas Dataframe instance or other supported object.
**kwargs: For other arguments, see ExportData() input keys. If they
exist both places, this function will override!
**kwargs: Using other ExportData() input keys is now deprecated, input the
arguments when initializing the ExportData() instance instead.

Returns:
String: full path to exported item.
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def _create_aggregated_surface_dataset(rmsglobalconfig, regsurf, content):
for i in range(10): # TODO! 10
use_regsurf = regsurf.copy()
use_regsurf.values += float(i)
expfile = edata.export(use_regsurf, name="mymap_" + str(i), realization=i)
expfile = edata.export(use_regsurf, name=f"mymap_{i}")
aggs.append(expfile)

# next task is to do an aggradation, and now the metadata already exists
Expand Down
1 change: 0 additions & 1 deletion tests/test_units/test_aggregated_surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ def test_regsurf_aggregated_diffdata(fmurun_w_casemetadata, rmsglobalconfig, reg
expfile = edata.export(
use_regsurf,
name="mymap_" + str(i),
realization=i,
timedata=[[20300201], [19990204]],
)
aggs.append(expfile)
Expand Down
29 changes: 17 additions & 12 deletions tests/test_units/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def test_missing_or_wrong_config_exports_with_warning(monkeypatch, tmp_path, reg
monkeypatch.chdir(tmp_path)

with pytest.warns(UserWarning, match=pydantic_warning()):
edata = ExportData(config={}, content="depth")
edata = ExportData(config={}, content="depth", name="mysurface")

meta = edata.generate_metadata(regsurf)
assert "masterdata" not in meta

# check that obj is created but no metadata is found
out = edata.export(regsurf, name="mysurface")
out = edata.export(regsurf)
assert "mysurface" in out
assert Path(out).exists()
with pytest.raises(OSError, match="Cannot find requested metafile"):
Expand All @@ -81,10 +81,10 @@ def test_config_miss_required_fields(monkeypatch, tmp_path, globalconfig1, regsu
del cfg["model"]

with pytest.warns(UserWarning, match=pydantic_warning()):
edata = ExportData(config=cfg, content="depth")
edata = ExportData(config=cfg, content="depth", name="mysurface")

with pytest.warns(UserWarning):
out = edata.export(regsurf, name="mysurface")
out = edata.export(regsurf)

assert "mysurface" in out

Expand Down Expand Up @@ -175,9 +175,9 @@ def test_deprecated_keys(globalconfig1, regsurf, key, value, expected_msg):
with pytest.warns(UserWarning, match=expected_msg):
ExportData(config=globalconfig1, content="depth", **kval)

# under override
with pytest.warns(UserWarning, match=expected_msg):
edata = ExportData(config=globalconfig1, content="depth")
# under override should give FutureWarning for these
edata = ExportData(config=globalconfig1, content="depth")
with pytest.warns(FutureWarning, match="move them up to initialization"):
edata.generate_metadata(regsurf, **kval)


Expand Down Expand Up @@ -308,7 +308,8 @@ def test_content_given_init_or_later(globalconfig1, regsurf):
assert mymeta["data"]["content"] == "time"

# override by adding content at generate_metadata
mymeta = eobj.generate_metadata(regsurf, content="depth")
with pytest.warns(FutureWarning, match="move them up to initialization"):
mymeta = eobj.generate_metadata(regsurf, content="depth")

assert mymeta["data"]["content"] == "depth" # last content shall win

Expand Down Expand Up @@ -479,7 +480,8 @@ def test_workflow_as_string(fmurun_w_casemetadata, monkeypatch, globalconfig1, r

# doing actual export with a few ovverides
edata = ExportData(config=globalconfig1)
meta = edata.generate_metadata(regsurf, workflow="My test workflow")
with pytest.warns(FutureWarning, match="move them up to initialization"):
meta = edata.generate_metadata(regsurf, workflow="My test workflow")
assert meta["fmu"]["workflow"]["reference"] == workflow


Expand All @@ -496,8 +498,9 @@ def test_set_display_name(regsurf, globalconfig2):
assert mymeta["data"]["name"] == "MyName"
assert mymeta["display"]["name"] == "MyDisplayName"

# also test when setting directly in the method call
mymeta = eobj.generate_metadata(regsurf, display_name="MyOtherDisplayName")
# also test when setting directly in the method call (not allowed in the future)
with pytest.warns(FutureWarning, match="move them up to initialization"):
mymeta = eobj.generate_metadata(regsurf, display_name="MyOtherDisplayName")

assert mymeta["data"]["name"] == "MyName"
assert mymeta["display"]["name"] == "MyOtherDisplayName"
Expand Down Expand Up @@ -564,7 +567,9 @@ def test_fmucontext_case_casepath(fmurun_prehook, rmsglobalconfig, regsurf):
assert "fmu" not in meta

# no warning when casepath is provided and metadata is valid
meta = edata.generate_metadata(regsurf, casepath=fmurun_prehook)
# should however issue a warning to move it to initialization
with pytest.warns(FutureWarning, match="move them up to initialization"):
meta = edata.generate_metadata(regsurf, casepath=fmurun_prehook)
assert "fmu" in meta
assert meta["fmu"]["case"]["name"] == "somecasename"

Expand Down
8 changes: 4 additions & 4 deletions tests/test_units/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def test_export_dict_w_meta(globalconfig2, dictionary, request, monkeypatch, tmp
name = dictionary
in_dict = request.getfixturevalue(dictionary)
print(f"{name}: {in_dict}")
exd = ExportData(config=globalconfig2, content="parameters")
out_dict, out_meta = read_dict_and_meta(exd.export(in_dict, name=name))
exd = ExportData(config=globalconfig2, content="parameters", name=name)
out_dict, out_meta = read_dict_and_meta(exd.export(in_dict))
assert in_dict == out_dict
assert_dict_correct(out_dict, out_meta, name)

Expand All @@ -134,10 +134,10 @@ def test_invalid_dict(
"""
monkeypatch.chdir(tmp_path)
in_dict = {"volumes": drogon_volumes, "summary": drogon_summary}
exd = ExportData(config=globalconfig2, content="parameters")
exd = ExportData(config=globalconfig2, content="parameters", name="invalid")
with pytest.raises(TypeError) as exc_info:
print(exc_info)
exd.export(in_dict, name="invalid")
exd.export(in_dict)
assert exc_info[1] == "Object of type Table is not JSON serializable"


Expand Down
Loading