Skip to content

Commit

Permalink
The ruff war begins
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland committed Jan 8, 2024
1 parent 8190023 commit 03dcb8d
Show file tree
Hide file tree
Showing 31 changed files with 103 additions and 156 deletions.
16 changes: 5 additions & 11 deletions examples/s/d/nn/_project/aggregate_surfaces.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"""Use fmu-dataio for aggregated surfaces created by an aggregation service."""

import logging
from pathlib import Path

import fmu.dataio
import numpy as np
import xtgeo
import yaml

import fmu.dataio


def main():
"""Aggregate one surface across X realizations from the example case and store the
Expand Down Expand Up @@ -56,9 +54,6 @@ def main():
# This is the ID we assign to this set of aggregations
aggregation_id = "something_very_unique" # IRL this will usually be a uuid

# We aggregate these source surfaces and collect results in list of dictionaries
aggregations = []

# Initialize an AggregatedData object for this set of aggregations
exp = fmu.dataio.AggregatedData(
source_metadata=source_metadata,
Expand All @@ -81,8 +76,8 @@ def main():

# ==============================================================================
# Example 2: We only want the metadata (e.g. we are in a cloud service)
metadata = exp.generate_metadata(aggregated_surface, operation=operation)
print(f"Example 2: Metadata generated")
exp.generate_metadata(aggregated_surface, operation=operation)
print("Example 2: Metadata generated")

# At this point, we have the surface, the operation and the metadata
# These can be collected into e.g. a list or a dictionary for further usage,
Expand Down Expand Up @@ -128,8 +123,7 @@ def _parse_yaml(fname):
"""

with open(fname) as stream:
data = yaml.safe_load(stream)
return data
return yaml.safe_load(stream)


def _metadata_filename(fname):
Expand Down Expand Up @@ -202,7 +196,7 @@ def _get_source_surfaces_from_sumo(
Not implemented.
"""
raise NotImplementedError()
raise NotImplementedError


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import pathlib

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as ut

import fmu.dataio as dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

FOLDER = pathlib.Path("../output/grids")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
"""
import pathlib

import fmu.dataio
import pandas as pd
from fmu.config import utilities as ut

import fmu.dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

IN_ROXAR = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"""
from pathlib import Path

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as ut

import fmu.dataio as dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

FILES = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Export faultpolygons via dataio with metadata."""
from pathlib import Path

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as utils

import fmu.dataio as dataio

CFG = utils.yaml_load("../../fmuconfig/output/global_variables.yml")

HORISONNAMES = CFG["rms"]["horizons"]["TOP_RES"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
"""
from pathlib import Path

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as ut

import fmu.dataio as dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

# property attributes, the key is "pattern" and the value is generic name to be used:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"""
from pathlib import Path

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as ut

import fmu.dataio as dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

FILES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"""
from pathlib import Path

import fmu.dataio as dataio
import xtgeo
from fmu.config import utilities as ut

import fmu.dataio as dataio

CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml")

FILES = {
Expand Down
2 changes: 1 addition & 1 deletion src/fmu/dataio/_design_kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def run(
line = line.replace(f"<{key}>", str(value))

if not all_matched(line, template_file_name, template):
valid = False
pass

result_file.write(line)

Expand Down
10 changes: 4 additions & 6 deletions src/fmu/dataio/_filedata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from copy import deepcopy
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Optional
from typing import Any
from warnings import warn

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -158,9 +158,7 @@ def _get_filestem(self) -> str:
# BUG(?): What about germen letter like "Ü"?
stem = stem.replace("æ", "ae")
stem = stem.replace("ø", "oe")
stem = stem.replace("å", "aa")

return stem
return stem.replace("å", "aa")

def _get_path(self) -> tuple[Path, Path | None]:
"""Construct and get the folder path(s)."""
Expand Down Expand Up @@ -215,8 +213,8 @@ def _get_path_generic(
"starting with '/'. This is strongly discouraged and is only "
"allowed if classvariable allow_forcefolder_absolute is set to True"
)
else:
warn("Using absolute paths in forcefolder is not recommended!")

warn("Using absolute paths in forcefolder is not recommended!")

# absolute if starts with "/", otherwise relative to outroot
dest = Path(self.dataio.forcefolder).absolute()
Expand Down
4 changes: 2 additions & 2 deletions src/fmu/dataio/_fmu_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from dataclasses import dataclass, field
from os import environ
from pathlib import Path
from typing import Any, Optional
from typing import Any
from warnings import warn

from fmu.config import utilities as ut
Expand Down Expand Up @@ -330,7 +330,7 @@ def generate_ert2_metadata(self) -> None:
"Generate ERT2 metadata continues, and real ID %s", self.real_id
)

mreal = meta["realization"] = dict()
mreal = meta["realization"] = {}
mreal["id"] = self.real_id
mreal["uuid"] = real_uuid
mreal["name"] = self.real_name
Expand Down
25 changes: 12 additions & 13 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dataclasses import dataclass, field
from datetime import timezone
from pathlib import Path
from typing import Any, Optional
from typing import Any
from warnings import warn

from fmu.dataio._definitions import SCHEMA, SOURCE, VERSION
Expand All @@ -37,7 +37,7 @@ class ConfigurationError(ValueError):


def default_meta_dollars() -> dict:
dollars = dict()
dollars = {}
dollars["$schema"] = SCHEMA
dollars["version"] = VERSION
dollars["source"] = SOURCE
Expand All @@ -46,7 +46,7 @@ def default_meta_dollars() -> dict:

def generate_meta_tracklog() -> list[dict]:
"""Create the tracklog metadata, which here assumes 'created' only."""
meta = list()
meta = []

dtime = datetime.datetime.now(timezone.utc).isoformat()
user = getpass.getuser()
Expand All @@ -56,6 +56,7 @@ def generate_meta_tracklog() -> list[dict]:

def generate_meta_masterdata(config: dict) -> dict | None:
"""Populate metadata from masterdata section in config."""

if not config:
# this may be a temporary solution for a while, which will be told to the user
# in related checks in dataio.py.
Expand All @@ -65,7 +66,8 @@ def generate_meta_masterdata(config: dict) -> dict | None:
UserWarning,
)
return None
elif "masterdata" not in config.keys():

if "masterdata" not in config:
raise ValueError("A config exists, but 'masterdata' are not present.")

return config["masterdata"]
Expand Down Expand Up @@ -109,7 +111,7 @@ def generate_meta_access(config: dict) -> dict | None:
raise ConfigurationError("The 'access.asset' field not found in the config")

# initialize and populate with defaults from config
a_meta = dict() # shortform
a_meta = {} # shortform

# if there is a config, the 'asset' tag shall be present
a_meta["asset"] = a_cfg["asset"]
Expand Down Expand Up @@ -368,11 +370,10 @@ def _reuse_existing_metadata(self, meta: dict) -> dict:
newmeta = meta.copy()
if self.dataio.reuse_metadata_rule == "preprocessed":
return glue_metadata_preprocessed(oldmeta, newmeta)
else:
raise ValueError(
f"The reuse_metadata_rule {self.dataio.reuse_metadata_rule} is not "
"supported."
)
raise ValueError(
f"The reuse_metadata_rule {self.dataio.reuse_metadata_rule} is not "
"supported."
)
return meta

def generate_export_metadata(
Expand Down Expand Up @@ -414,6 +415,4 @@ def generate_export_metadata(
if skip_null:
meta = drop_nones(meta)

meta = self._reuse_existing_metadata(meta)

return meta
return self._reuse_existing_metadata(meta)
30 changes: 15 additions & 15 deletions src/fmu/dataio/_objectdata_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
from dataclasses import dataclass, field
from datetime import datetime as dt
from pathlib import Path
from typing import Any, Optional, Tuple, cast
from typing import Any, cast
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -179,7 +179,7 @@ def _derive_name_stratigraphy(self) -> dict:
else:
logger.info("The name in strat...")
result["name"] = strat[name].get("name", name)
result["alias"] = strat[name].get("alias", list())
result["alias"] = strat[name].get("alias", [])
if result["name"] != "name":
result["alias"].append(name) # type: ignore
result["stratigraphic"] = strat[name].get("stratigraphic", False)
Expand All @@ -196,7 +196,7 @@ def _validate_get_ext(
fmt: str, subtype: str, validator: dict[str, Any]
) -> object | None:
"""Validate that fmt (file format) matches data and return legal extension."""
if fmt not in validator.keys():
if fmt not in validator:
raise ConfigurationError(
f"The file format {fmt} is not supported.",
f"Valid {subtype} formats are: {list(validator.keys())}",
Expand Down Expand Up @@ -353,8 +353,8 @@ def _derive_spec_bbox_regularsurface(self) -> tuple[dict, dict]:
logger.info("Derive bbox and specs for RegularSurface")
regsurf = self.obj

specs = dict()
bbox = dict()
specs = {}
bbox = {}

xtgeo_specs = regsurf.metadata.required
for spec, val in xtgeo_specs.items():
Expand All @@ -377,8 +377,8 @@ def _derive_spec_bbox_polygons(self) -> tuple[dict, dict]:
logger.info("Derive bbox and specs for Polygons")
poly = self.obj

specs = dict()
bbox = dict()
specs = {}
bbox = {}
# number of polygons:
specs["npolys"] = np.unique(poly.dataframe[poly.pname].values).size
xmin, xmax, ymin, ymax, zmin, zmax = poly.get_boundary()
Expand Down Expand Up @@ -418,8 +418,8 @@ def _derive_spec_bbox_cube(self) -> tuple[dict, dict]:
logger.info("Derive bbox and specs for Cube")
cube = self.obj

specs = dict()
bbox = dict()
specs = {}
bbox = {}

xtgeo_specs = cube.metadata.required
for spec, val in xtgeo_specs.items():
Expand Down Expand Up @@ -455,8 +455,8 @@ def _derive_spec_bbox_cpgrid(self) -> tuple[dict, dict]:
logger.info("Derive bbox and specs for Gride (geometry)")
grid = self.obj

specs = dict()
bbox = dict()
specs = {}
bbox = {}

xtgeo_specs = grid.metadata.required
for spec, val in xtgeo_specs.items():
Expand Down Expand Up @@ -594,10 +594,10 @@ def _derive_timedata_legacy(self) -> dict[str, Any]:
tdata = self.dataio.timedata

tresult = dict[str, Any]()
tresult["time"] = list()
tresult["time"] = []
if len(tdata) == 1:
elem = tdata[0]
tresult["time"] = list()
tresult["time"] = []
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
self.time0 = str(elem[0])
if len(elem) == 2:
Expand Down Expand Up @@ -641,7 +641,7 @@ def _derive_timedata_newformat(self) -> dict[str, Any]:

if len(tdata) == 1:
elem = tdata[0]
tresult["t0"] = dict()
tresult["t0"] = {}
xfield = {"value": dt.strptime(str(elem[0]), "%Y%m%d").isoformat()}
self.time0 = str(elem[0])
if len(elem) == 2:
Expand Down Expand Up @@ -769,7 +769,7 @@ def derive_metadata(self) -> None:
for key, val in tresult.items():
meta[key] = val
else:
meta["time"] = dict()
meta["time"] = {}
for key, val in tresult.items():
meta["time"][key] = val

Expand Down
Loading

0 comments on commit 03dcb8d

Please sign in to comment.