Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JB Lovland committed Nov 16, 2023
1 parent 0830cc6 commit 39fa58c
Show file tree
Hide file tree
Showing 84 changed files with 196 additions and 321 deletions.
42 changes: 16 additions & 26 deletions examples/multiprocess_surfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,65 @@
import logging

import xtgeo
from xtgeo.common.xtgeo_dialog import testdatafolder

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


TESTFILE = "../../xtgeo-testdata/surfaces/reek/1/basereek_rota.gri"
NTHREAD = 20


def _get_files_as_regularsurfaces_thread(option=1):
def _get_files_as_regularsurfaces_thread(option: int = 1) -> xtgeo.Surfaces:
surfs = []

with concurrent.futures.ThreadPoolExecutor() as executor:
with concurrent.futures.ThreadPoolExecutor(max_workers=NTHREAD) as executor:
if option == 1:
futures = {executor.submit(_get_regsurff, i): i for i in range(NTHREAD)}
else:
futures = {executor.submit(_get_regsurfi, i): i for i in range(NTHREAD)}

for future in concurrent.futures.as_completed(futures):
try:
surf = future.result()
surfs.append(future.result())
except Exception as exc: # pylint: disable=broad-except
logger.error("Error: %s", exc)
else:
surfs.append(surf)

regular_surfaces = xtgeo.Surfaces(surfs)
return regular_surfaces
return xtgeo.Surfaces(surfs)


def _get_files_as_regularsurfaces_multiprocess(option=1):
def _get_files_as_regularsurfaces_multiprocess(option: int = 1) -> xtgeo.Surfaces:
surfs = []

with concurrent.futures.ProcessPoolExecutor() as executor:
with concurrent.futures.ProcessPoolExecutor(max_workers=NTHREAD) as executor:
if option == 1:
futures = {executor.submit(_get_regsurff, i): i for i in range(NTHREAD)}
else:
futures = {executor.submit(_get_regsurfi, i): i for i in range(NTHREAD)}

for future in concurrent.futures.as_completed(futures):
try:
surf = future.result()
surfs.append(future.result())
except Exception as exc: # pylint: disable=broad-except
logger.error("Error: %s", exc)
else:
surfs.append(surf)

regular_surfaces = xtgeo.Surfaces(surfs)
return regular_surfaces
return xtgeo.Surfaces(surfs)


def _get_regsurff(i):
def _get_regsurff(i: int) -> xtgeo.Surfaces:
logger.info("Start %s", i)

sfile = TESTFILE

logger.info("File is %s", sfile)
rf = xtgeo.surface_from_file(sfile)
logger.info("File is %s", testdatafolder)
rf = xtgeo.surface_from_file(testdatafolder)
logger.info("End %s", i)
return rf


def _get_regsurfi(i):
def _get_regsurfi(i: int) -> xtgeo.Surfaces:
logger.info("Start %s", i)

sfile = TESTFILE
with open(sfile, "rb") as fin:
with open(testdatafolder, "rb") as fin:
stream = io.BytesIO(fin.read())

logger.info("File is %s", sfile)
logger.info("File is %s", testdatafolder)
rf = xtgeo.surface_from_file(stream, fformat="irap_binary")
logger.info("End %s", i)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies = [
"shapely>=1.6.2",
"tables;platform_system != 'Darwin' and python_version > '3.8'", # TODO: mac...
"tables<3.9;platform_system != 'Darwin' and python_version == '3.8'", # TODO: mac...
"tqdm",
"tqdm>=4.66",
]

[project.urls]
Expand Down
4 changes: 4 additions & 0 deletions src/xtgeo/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
XTGShowProgress,
logger,
timeit,
warndeprecated,
warnuser,
)
from xtgeo.xyz._xyz_data import _XYZData

__all__ = [
"inherit_docstring",
"logger",
"timeit",
"warnuser",
"warndeprecated",
"WellNotFoundError",
"XTGDescription",
"XTGeoDialog",
Expand Down
6 changes: 3 additions & 3 deletions src/xtgeo/common/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np

from xtgeo import XTGeoCLibError
from xtgeo.common import XTGeoDialog, logger
from xtgeo.common import logger
from xtgeo.cxtgeo import _cxtgeo


Expand Down Expand Up @@ -61,8 +61,8 @@ def ijk_to_ib(
if ib < 0:
raise IndexError(f"Negative index: {ib}")
if ibbase == 0 and ib > nx * ny * nz - 1:
xtg.warn("Something is wrong with IJK conversion")
xtg.warn(f"I J K, NX, NY, NZ IB: {i} {j} {k} {nx} {ny} {nz} {ib}")
logger.warning("Something is wrong with IJK conversion")
logger.warning(f"I J K, NX, NY, NZ IB: {i} {j} {k} {nx} {ny} {nz} {ib}")

return ib

Expand Down
2 changes: 1 addition & 1 deletion src/xtgeo/common/sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from xtgeo.cxtgeo import _cxtgeo

from ._xyz_enum import _AttrType
from .xtgeo_dialog import XTGeoDialog, logger
from .xtgeo_dialog import logger

if TYPE_CHECKING:
import numpy.typing as npt
Expand Down
118 changes: 49 additions & 69 deletions src/xtgeo/common/xtgeo_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@
Usage of logging in scripts::
import xtgeo
xtg = xtgeo.common.XTGeoDialog()
from xtgeo.common import logger
logger.info('This is logging of %s', something)
Other than logging, there is also a template for user interaction, which shall
be used in client scripts::
xtg.echo('This is a message')
xtg.warn('This is a warning')
xtg.warning('This is a warning')
xtg.error('This is an error, will continue')
xtg.critical('This is a big error, will exit')
In addition there are other classes:
* XTGShowProgress()
* XTGDescription()
"""

from __future__ import annotations

import contextlib
Expand All @@ -41,24 +38,43 @@
import pathlib
import platform
import warnings
from typing import Callable, Final, Iterator, Literal
from typing import Callable, Final, Iterator

import tqdm

import xtgeo

logging.basicConfig(
level=os.environ.get(
logging.getLevelName("XTG_LOGGING_LEVEL"),
logging.INFO,
level=logging.getLevelName(
os.environ.get(
"XTG_LOGGING_LEVEL",
logging.INFO,
)
),
format=os.environ.get(
"XTG_LOGGING_FORMAT",
"%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s - %(message)s",
"%(levelname)s - %(asctime)s - %(filename)s:%(lineno)d - %(message)s",
),
)


def get_xtgeo_info() -> str:
"""Prints a banner for a XTGeo app to STDOUT.
Args:
variant (str): Variant of info
Returns:
info (str): A string with XTGeo system info
"""

return (
f"XTGeo version {xtgeo.__version__} (Python "
f"{platform.python_version()} on {platform.system()})"
)


@contextlib.contextmanager
def timeit() -> Iterator[Callable[[], datetime.timedelta]]:
"""
Expand All @@ -83,11 +99,20 @@ def timeit() -> Iterator[Callable[[], datetime.timedelta]]:
done = datetime.datetime.now()


def _xtgeo_root_folder() -> pathlib.Path:
return pathlib.Path("./").resolve()


def _testdatafolder() -> pathlib.Path:
testpath = os.environ.get("XTG_TESTPATH", "../xtgeo-testdata")
if not os.path.isdir(testpath):
raise RuntimeError(f"XTG_TESTPATH({testpath}) must be a directory.")
return pathlib.Path(testpath)
testpath = pathlib.Path(
os.environ.get(
"XTG_TESTPATH",
_xtgeo_root_folder().parent / "xtgeo-testdata",
)
)
if testpath.is_dir():
return testpath
raise RuntimeError(f"XTG_TESTPATH({testpath}) must be a directory.")


class XTGShowProgress(tqdm.tqdm):
Expand Down Expand Up @@ -143,64 +168,19 @@ def _smartfmt(atxt: list[str]) -> str:
return "{:40s} {:>2s} {} {} {} {} {} {} {}".format(*atxt)


class XTGeoDialog(logging.Logger): # pylint: disable=too-many-public-methods
...
"""System for handling dialogs and messages in XTGeo.
This module cooperates with Python logging module.
"""

def __init__(self) -> None:
"""Initializing XTGeoDialog."""
super().__init__(
"xtgeo",
level=os.environ.get(
logging.getLevelName("XTG_LOGGING_LEVEL"),
logging.DEBUG,
),
)

@staticmethod
def get_xtgeo_info(variant: Literal["clibinfo"] = "clibinfo") -> str:
"""Prints a banner for a XTGeo app to STDOUT.
Args:
variant (str): Variant of info
Returns:
info (str): A string with XTGeo system info
def warnuser(string: str) -> None:
"""Show User warnings, using Python warnings"""
warnings.simplefilter("default", UserWarning)
warnings.warn(string, UserWarning, stacklevel=2)

"""

if variant == "clibinfo":
return (
f"XTGeo version {xtgeo.__version__} (Python "
f"{platform.python_version()} on {platform.system()})"
)

return "Invalid"

def insane(self, *args, **kw) -> None: # type: ignore
self.critical(*args, **kw)

@staticmethod
def warndeprecated(string: str) -> None:
"""Show Deprecation warnings using Python warnings"""

warnings.simplefilter("default", DeprecationWarning)
warnings.warn(string, DeprecationWarning, stacklevel=2)

@staticmethod
def warnuser(string: str) -> None:
"""Show User warnings, using Python warnings"""

warnings.simplefilter("default", UserWarning)
warnings.warn(string, UserWarning, stacklevel=2)
def warndeprecated(string: str) -> None:
"""Show Deprecation warnings using Python warnings"""
warnings.simplefilter("default", DeprecationWarning)
warnings.warn(string, DeprecationWarning, stacklevel=2)

def say(self, txt: str) -> None:
return self.info(txt)

logger: Final = logging.getLogger(name="XTGeo")
XTGeoDialog: Final = logger # Alias

logger: Final = XTGeoDialog()
testdatafolder: Final = _testdatafolder()
2 changes: 1 addition & 1 deletion src/xtgeo/cube/_cube_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _export_segy_segyio(self, sfile, template=None, pristine=False):
try:
shutil.copyfile(self._segyfile, sfile)
except Exception as errormsg:
xtg.warn(f"Error message: {errormsg}")
logger.warning(f"Error message: {errormsg}")
raise

logger.debug("Input segy file copied ...")
Expand Down
4 changes: 2 additions & 2 deletions src/xtgeo/cube/_cube_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import xtgeo.common.calc as xcalc
import xtgeo.common.sys as xsys
import xtgeo.cxtgeo._cxtgeo as _cxtgeo
from xtgeo.common import XTGeoDialog, logger
from xtgeo.common import logger, warnuser


def import_segy(sfile: xtgeo._XTGeoFile) -> Dict:
Expand Down Expand Up @@ -112,7 +112,7 @@ def _import_segy_all_traces(segyfile: segyio.segy.SegyFile) -> Dict:
def _process_cube_values(values: np.ndarray) -> np.ndarray:
"""Helper function to validate/check values."""
if values.dtype != np.float32:
logger.warnuser(f"Values are converted from {values.dtype} to float32")
warnuser(f"Values are converted from {values.dtype} to float32")
values = values.astype(np.float32)
if np.any(np.isnan(values)):
raise ValueError(
Expand Down
4 changes: 2 additions & 2 deletions src/xtgeo/cube/_cube_roxapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from xtgeo import RoxUtils
from xtgeo.common import XTGeoDialog, logger
from xtgeo.common import logger


def import_cube_roxapi(self, project, name, folder=None): # pragma: no cover
Expand Down Expand Up @@ -98,7 +98,7 @@ def _roxapi_cube_to_xtgeo(self, rox, rcube): # pragma: no cover
self._traceidcodes = np.ones((self._ncol, self._nrow), dtype=np.int32)

if rcube.is_empty:
xtg.warn("Cube has no data; assume 0")
logger.warning("Cube has no data; assume 0")
else:
self.values = rcube.get_values()

Expand Down
2 changes: 1 addition & 1 deletion src/xtgeo/cube/_cube_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import xtgeo
import xtgeo.cxtgeo._cxtgeo as _cxtgeo
from xtgeo import XTGeoCLibError
from xtgeo.common import XTGeoDialog, logger
from xtgeo.common import logger
from xtgeo.common.calc import _swap_axes

# pylint: disable=protected-access
Expand Down
2 changes: 1 addition & 1 deletion src/xtgeo/cube/cube1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import xtgeo
import xtgeo.common.sys as xtgeosys
from xtgeo.common import XTGDescription, XTGeoDialog, logger
from xtgeo.common import XTGDescription, logger
from xtgeo.cube import _cube_export, _cube_import, _cube_roxapi, _cube_utils


Expand Down
4 changes: 0 additions & 4 deletions src/xtgeo/grid3d/_grdecl_format.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import warnings
from contextlib import contextmanager

import xtgeo

xtg = xtgeo.common.XTGeoDialog()

from xtgeo.common import logger


Expand Down
Loading

0 comments on commit 39fa58c

Please sign in to comment.