Skip to content

Commit f290bf0

Browse files
committed
Make _observations.py use polars
1 parent 7e50c28 commit f290bf0

2 files changed

Lines changed: 26 additions & 33 deletions

File tree

src/ert/config/_observations.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919

2020
import numpy as np
21-
import pandas as pd
2221
import polars as pl
2322
import scipy as sp
2423
from pydantic import BaseModel, ConfigDict, Field, model_serializer
@@ -686,11 +685,7 @@ def from_csv(
686685
f"The CSV file ({filename}) does not exist or is not accessible.",
687686
filename,
688687
)
689-
csv_file = pd.read_csv(
690-
filename,
691-
encoding="utf-8",
692-
on_bad_lines="error",
693-
)
688+
csv_file = pl.read_csv(filename, encoding="utf-8")
694689

695690
required_columns = {
696691
"WELL_NAME",
@@ -701,7 +696,7 @@ def from_csv(
701696
"EAST",
702697
"TVD",
703698
}
704-
missing_required_columns = required_columns - set(csv_file.keys())
699+
missing_required_columns = required_columns - set(csv_file.columns)
705700
if missing_required_columns:
706701
raise ObservationConfigError.with_context(
707702
f"The rft observations file {filename} is missing required column(s) "
@@ -711,9 +706,10 @@ def from_csv(
711706

712707
rft_observations = []
713708
invalid_observations = []
714-
for row in csv_file.itertuples(index=True):
715-
east_val = validate_float(str(row.EAST), "EAST")
716-
north_val = validate_float(str(row.NORTH), "NORTH")
709+
columns = set(csv_file.columns)
710+
for index, row in enumerate(csv_file.iter_rows(named=True)):
711+
east_val = validate_float(str(row["EAST"]), "EAST")
712+
north_val = validate_float(str(row["NORTH"]), "NORTH")
717713
radius = radius if radius is not None else DEFAULT_LOCALIZATION_RADIUS
718714

719715
shape_id = shape_registry.register(
@@ -725,22 +721,20 @@ def from_csv(
725721
)
726722

727723
rft_observation = cls(
728-
name=f"{observation_dict['name']}[{row.Index}]",
729-
well=str(row.WELL_NAME),
730-
date=str(row.DATE),
724+
name=f"{observation_dict['name']}[{index}]",
725+
well=str(row["WELL_NAME"]),
726+
date=str(row["DATE"]),
731727
property=observed_property,
732-
value=validate_float(
733-
str(getattr(row, observed_property)), observed_property
734-
),
735-
error=validate_float(str(row.ERROR), "ERROR"),
728+
value=validate_float(str(row[observed_property]), observed_property),
729+
error=validate_float(str(row["ERROR"]), "ERROR"),
736730
east=east_val,
737731
north=north_val,
738732
shape_id=shape_id,
739-
tvd=validate_float(str(row.TVD), "TVD"),
740-
md=validate_float(str(row.MD), "MD") if "MD" in csv_file else None,
733+
tvd=validate_float(str(row["TVD"]), "TVD"),
734+
md=validate_float(str(row["MD"]), "MD") if "MD" in columns else None,
741735
zone=(
742-
str(row.ZONE)
743-
if "ZONE" in csv_file and row.ZONE is not None
736+
str(row["ZONE"])
737+
if "ZONE" in columns and row["ZONE"] is not None
744738
else None
745739
),
746740
)
@@ -1007,11 +1001,10 @@ class SeismicObservation(BaseObservation):
10071001
TOLERANCE: ClassVar[float] = 0.1
10081002

10091003
@staticmethod
1010-
def _load_observations(filepath: Path) -> pd.DataFrame:
1011-
df = pd.read_csv(
1004+
def _load_observations(filepath: Path) -> pl.DataFrame:
1005+
df = pl.read_csv(
10121006
filepath,
10131007
encoding="utf-8",
1014-
on_bad_lines="error",
10151008
)
10161009

10171010
required_columns = {
@@ -1020,7 +1013,7 @@ def _load_observations(filepath: Path) -> pd.DataFrame:
10201013
"OBS",
10211014
"OBS_ERROR",
10221015
}
1023-
missing_required_columns = required_columns - set(df.keys())
1016+
missing_required_columns = required_columns - set(df.columns)
10241017
if missing_required_columns:
10251018
raise ObservationConfigError.with_context(
10261019
f"The seismic observations file {filepath} "
@@ -1119,11 +1112,11 @@ def from_obs_dict(
11191112
boundary_id = shape_registry.register(boundary)
11201113

11211114
seismic_observations = []
1122-
for row in df.itertuples():
1123-
east = validate_float(str(row.X_UTME), "X_UTME")
1124-
north = validate_float(str(row.Y_UTMN), "Y_UTMN")
1125-
value = validate_float(str(row.OBS), "OBS")
1126-
error = validate_float(str(row.OBS_ERROR), "OBS_ERROR")
1115+
for row in df.iter_rows(named=True):
1116+
east = validate_float(str(row["X_UTME"]), "X_UTME")
1117+
north = validate_float(str(row["Y_UTMN"]), "Y_UTMN")
1118+
value = validate_float(str(row["OBS"]), "OBS")
1119+
error = validate_float(str(row["OBS_ERROR"]), "OBS_ERROR")
11271120

11281121
# Currently supports only default localization radius as behavior of
11291122
# LOCALIZATION keyword is undefined. All shapes are being registered

tests/ert/unit_tests/config/test_observation_declaration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,9 @@ def test_that_seismic_observation_instantiates(file_context_token):
841841
create_seismic_observation(
842842
name="NAME",
843843
filepath=Path("obs.csv"),
844-
east=461231.5537527473,
844+
east=461231.55375274725,
845845
north=5933187.729869121,
846-
value=-0.0003566695393886,
846+
value=-0.00035666953938864876,
847847
error=0.005,
848848
shape_id=0,
849849
boundary_id=None,
@@ -853,7 +853,7 @@ def test_that_seismic_observation_instantiates(file_context_token):
853853
filepath=Path("obs.csv"),
854854
east=461156.9532936567,
855855
north=5933317.28138355,
856-
value=-0.0005293887515127,
856+
value=-0.0005293887515127136,
857857
error=0.005,
858858
shape_id=1,
859859
boundary_id=None,

0 commit comments

Comments
 (0)