Skip to content
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
15 changes: 10 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ markers = [
ignore_directives = ["argparse", "automodule"]

[tool.ruff]
lint.ignore = [
"C901",
]
lint.select = [
src = ["src"]
line-length = 88

[tool.ruff.lint]
select = [
"C",
"E",
"F",
Expand All @@ -178,8 +179,12 @@ lint.select = [
"RSE",
"SIM",
"W",
"PD",
]
ignore = [
"C901",
"PD013",
]
line-length = 88

[tool.ruff.lint.isort]
combine-as-imports = true
36 changes: 19 additions & 17 deletions src/subscript/check_swatinit/check_swatinit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def main() -> None:
print(f"Dumping volume plot to {args.volplotfile}")
pyplot.savefig(args.volplotfile)

if (args.plotfile or args.plot) and args.eqlnum not in qc_frame["EQLNUM"].values:
if (args.plotfile or args.plot) and args.eqlnum not in qc_frame[
"EQLNUM"
].to_numpy():
sys.exit(f"Error: EQLNUM {args.eqlnum} does not exist in grid. No plotting.")
if args.plot or args.plotfile:
plotter.plot_qc_panels(qc_frame[qc_frame["EQLNUM"] == args.eqlnum])
Expand Down Expand Up @@ -129,7 +131,7 @@ def reorder_dframe_for_nonnans(dframe: pd.DataFrame) -> pd.DataFrame:
will aid data analysis application to deduce correct datatypes for
columns"""
null_count = "__NULL_COUNT__"
dframe[null_count] = dframe.isnull().sum(axis=1)
dframe[null_count] = dframe.isna().sum(axis=1)
return (
dframe.sort_values(null_count).drop(null_count, axis=1).reset_index(drop=True)
)
Expand Down Expand Up @@ -253,11 +255,11 @@ def make_qc_gridframe(eclfiles: res2df.ResdataFiles) -> pd.DataFrame:
# GLOBAL_INDEX is 0-indexed.
grid_df["SWATINIT_DECK"] = pd.Series(swatinit_deckdata)[
grid_df["GLOBAL_INDEX"].astype(int).tolist()
].values
].to_numpy()

if "SWATINIT" not in grid_df:
# OPM-flow does not include SWATINIT in the INIT file.
grid_df.rename({"SWATINIT_DECK": "SWATINIT"}, axis="columns", inplace=True)
grid_df = grid_df.rename({"SWATINIT_DECK": "SWATINIT"}, axis="columns")
elif "SWATINIT_DECK" in grid_df:
# (if SWATINIT is inputted using binary data in Eclipse deck, the code above
# is not able to extract it)
Expand Down Expand Up @@ -352,7 +354,7 @@ def qc_flag(qc_frame: pd.DataFrame) -> pd.DataFrame:
qc_col[
(~np.isclose(qc_frame["OIP_INIT"], 0))
& (~np.isclose(qc_frame["SWAT"], qc_frame["SWATINIT"], atol=1e-6))
& (~pd.isnull(qc_frame["PC_SCALING"]))
& (~pd.isna(qc_frame["PC_SCALING"]))
] = __FINE_EQUIL__

# SWATINIT=1 above contact:
Expand Down Expand Up @@ -409,9 +411,7 @@ def qc_flag(qc_frame: pd.DataFrame) -> pd.DataFrame:

# Tag the remainder with "unknown", when/if this happens, it is a bug or a
# feature request:
qc_col.fillna(__UNKNOWN__, inplace=True)

return qc_col
return qc_col.fillna(__UNKNOWN__)


def qc_volumes(qc_frame: pd.DataFrame) -> Dict[str, float]:
Expand Down Expand Up @@ -496,8 +496,10 @@ def _evaluate_pc(
np.interp(
swat,
swl
+ (satfunc[sat_name].values - sw_min) / (sw_max - sw_min) * (swu - swl),
satfunc[pc_name].values * pc_scaling,
+ (satfunc[sat_name].to_numpy() - sw_min)
/ (sw_max - sw_min)
* (swu - swl),
satfunc[pc_name].to_numpy() * pc_scaling,
)
)
return p_cap
Expand Down Expand Up @@ -534,15 +536,15 @@ def compute_pc(qc_frame: pd.DataFrame, satfunc_df: pd.DataFrame) -> pd.Series:

for satnum, satnum_frame in qc_frame.groupby("SATNUM"):
if "SWLPC" in satnum_frame:
swls = satnum_frame["SWLPC"].values
swls = satnum_frame["SWLPC"].to_numpy()
elif "SWL" in satnum_frame:
swls = satnum_frame["SWL"].values
swls = satnum_frame["SWL"].to_numpy()
else:
swls = None
swus = satnum_frame["SWU"].values if "SWU" in satnum_frame else None
swus = satnum_frame["SWU"].to_numpy() if "SWU" in satnum_frame else None
p_cap[satnum_frame.index] = _evaluate_pc(
satnum_frame["SWAT"].values,
satnum_frame["PC_SCALING"].values,
satnum_frame["SWAT"].to_numpy(),
satnum_frame["PC_SCALING"].to_numpy(),
swls,
swus,
satfunc_df[satfunc_df["SATNUM"] == satnum],
Expand Down Expand Up @@ -596,7 +598,7 @@ def merge_equil(grid_df: pd.DataFrame, equil_df: pd.DataFrame) -> pd.DataFrame:
assert "PRESSURE" in equil_df

# Be compatible with future change in res2df:
equil_df.rename({"ACCURACY": "OIP_INIT"}, axis="columns", inplace=True)
equil_df = equil_df.rename({"ACCURACY": "OIP_INIT"}, axis="columns")

contacts = list({"OWC", "GOC", "GWC"}.intersection(set(equil_df.columns)))
# Rename and slice the equil dataframe:
Expand All @@ -607,7 +609,7 @@ def merge_equil(grid_df: pd.DataFrame, equil_df: pd.DataFrame) -> pd.DataFrame:
equil_df = equil_df[equil_df["KEYWORD"] == "EQUIL"]
equil_df = equil_df[["Z_DATUM", "PRESSURE_DATUM", "EQLNUM", "OIP_INIT"] + contacts]
equil_df["EQLNUM"] = equil_df["EQLNUM"].astype(int)
assert not pd.isnull(equil_df).any().any(), (
assert not pd.isna(equil_df).any().any(), (
f"BUG: NaNs in equil dataframe:\n{equil_df}"
)
return grid_df.merge(equil_df, on="EQLNUM", how="left")
Expand Down
14 changes: 7 additions & 7 deletions src/subscript/check_swatinit/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def plot_qc_panels(
pyplot.subplot(2, 2, 4)
pc_depth(qc_frame)

oip_init = qc_frame["OIP_INIT"].values[0]
eqlnum = qc_frame["EQLNUM"].values[0]
oip_init = qc_frame["OIP_INIT"].to_numpy()[0]
eqlnum = qc_frame["EQLNUM"].to_numpy()[0]
pyplot.suptitle(f"EQLNUM: {eqlnum}, OIP_INIT: {oip_init}")
if show:
pyplot.show()
Expand Down Expand Up @@ -150,15 +150,15 @@ def pc_depth(
def add_contacts_to_plot(qc_frame: pd.DataFrame, axis: pyplot.Axes) -> None:
"""Annotate axes with named horizontal lines for contacts."""
if "OWC" in qc_frame:
owc = qc_frame["OWC"].values[0] # OWC is assumed constant in the dataframe
owc = qc_frame["OWC"].to_numpy()[0] # OWC is assumed constant in the dataframe
axis.axhline(owc, color="black", linestyle="--", linewidth=1)
axis.annotate(f"OWC={owc:g}", (0, owc))
if "GOC" in qc_frame:
goc = qc_frame["GOC"].values[0]
goc = qc_frame["GOC"].to_numpy()[0]
axis.axhline(goc, color="black", linestyle="--", linewidth=1)
axis.annotate(f"GOC={goc:g}", (0, goc))
if "GWC" in qc_frame:
gwc = qc_frame["GWC"].values[0]
gwc = qc_frame["GWC"].to_numpy()[0]
axis.axhline(gwc, color="black", linestyle="--", linewidth=1)
axis.annotate(f"GWC={gwc:g}", (0, gwc))

Expand Down Expand Up @@ -199,10 +199,10 @@ def wvol_waterfall(qc_vols: Dict[str, float]) -> None:
blank.loc["SWAT_WVOL"] = 0

fig = trans.plot(kind="bar", alpha=0.7, stacked=True, legend=None, bottom=blank)
fig.plot(step.index, step.values, "k")
fig.plot(step.index, step.to_numpy(), "k")
pyplot.gcf().subplots_adjust(bottom=0.25)

blanktrans = blank.values + trans["volume"].values
blanktrans = blank.to_numpy() + trans["volume"].to_numpy()
span = blank.max() - blanktrans[1:-1].min()

if np.isclose(span, 0.0):
Expand Down
4 changes: 2 additions & 2 deletions src/subscript/csv2ofmvol/csv2ofmvol.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def read_pdm_csv_files(
# Reindex:
data = data.set_index(["WELL", "DATE"])

if not [data.columns.values]:
if not [data.columns.to_numpy()]:
raise ValueError("No data columns found")

# Drop duplicate multiindices (WELL, DATE)
Expand Down Expand Up @@ -220,7 +220,7 @@ def df2vol(data: pd.DataFrame) -> str:

# Fill empty cells with zeros, empty cells can stem from concatenation
# of dataframes with gas and water injectors.
voldata.fillna(value=0.0, inplace=True)
voldata = voldata.fillna(value=0.0)

volstr = ""
volstr += "*METRIC\n"
Expand Down
2 changes: 1 addition & 1 deletion src/subscript/csv_merge/csv_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def csv_merge_main(
if len(merged_df[col].unique()) == 1:
columnstodelete.append(col)
logger.info("Dropping constant columns %s", str(columnstodelete))
merged_df.drop(columnstodelete, inplace=True, axis=1)
merged_df = merged_df.drop(columnstodelete, axis=1)

if merged_df.empty:
logger.error("No data to output")
Expand Down
2 changes: 1 addition & 1 deletion src/subscript/csv_stack/csv_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def csv_stack(

# Stack the multiindex columns, this will add a lot of rows to
# our ensemble, and condense the number of columns
dframe = dframe.stack()
dframe = dframe.stack(future_stack=True)

# The values from non-multiindex-columns must be propagated to
# the rows that emerged from the stacking. If you use the
Expand Down
3 changes: 1 addition & 2 deletions src/subscript/fmuobs/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ def resinsight_df2df(ri_dframe: pd.DataFrame) -> pd.DataFrame:
if ri_dframe.empty:
return pd.DataFrame()

dframe = ri_dframe.copy()
dframe.rename({"VECTOR": "KEY"}, axis="columns", inplace=True)
dframe = ri_dframe.copy().rename({"VECTOR": "KEY"}, axis="columns")
dframe["LABEL"] = (
dframe["KEY"].astype(str)
+ "-"
Expand Down
50 changes: 25 additions & 25 deletions src/subscript/fmuobs/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def dfsummary2ertobs(obs_df: pd.DataFrame) -> str:
for _, row in smry_df.iterrows():
ertobs_str += "SUMMARY_OBSERVATION " + str(row["LABEL"]) + "\n"
ertobs_str += "{\n"
if "COMMENT" in row and not pd.isnull(row["COMMENT"]):
if "COMMENT" in row and not pd.isna(row["COMMENT"]):
ertobs_str += (
" -- "
+ str(row["COMMENT"]).replace("\n", "\n -- ").strip()
+ "\n"
)
if "DATE" in row and not pd.isnull(row["DATE"]):
if "DATE" in row and not pd.isna(row["DATE"]):
ertobs_str += (
" DATE = "
+ str(pd.to_datetime(row["DATE"]).strftime(ERT_ISO_DATE_FORMAT))
+ ";\n"
)
for dataname in ["KEY", "DAYS", "RESTART", "VALUE", "ERROR", "SOURCE"]:
if dataname in row and not pd.isnull(row[dataname]):
if dataname in row and not pd.isna(row[dataname]):
ertobs_str += " " + dataname + " = " + str(row[dataname]) + ";\n"
ertobs_str += "};\n"
return ertobs_str
Expand All @@ -70,12 +70,14 @@ def dfblock2ertobs(obs_df: pd.DataFrame) -> str:
)
for obslabel, block_df in block_obs_df.groupby("LABEL"):
ertobs_str += "BLOCK_OBSERVATION " + obslabel + "\n{\n"
if "COMMENT" in block_df and not pd.isnull(block_df["COMMENT"]).any():
if "COMMENT" in block_df and not pd.isna(block_df["COMMENT"]).any():
if len(block_df["COMMENT"].dropna().unique()) != 1:
logger.warning("Inconsistency in COMMENT in block dataframe")
ertobs_str += (
" -- "
+ str(block_df["COMMENT"].values[0]).replace("\n", "\n -- ").strip()
+ str(block_df["COMMENT"].to_numpy()[0])
.replace("\n", "\n -- ")
.strip()
+ "\n"
)
for dataname in ["FIELD", "DATE"]:
Expand All @@ -85,7 +87,7 @@ def dfblock2ertobs(obs_df: pd.DataFrame) -> str:
" "
+ dataname
+ " = "
+ str(block_df[dataname].values[0])
+ str(block_df[dataname].to_numpy()[0])
+ ";\n"
)
else:
Expand All @@ -94,15 +96,15 @@ def dfblock2ertobs(obs_df: pd.DataFrame) -> str:
f"block dataframe for one label has multiple {dataname}"
)
for _, row in block_df.iterrows():
if "SUBCOMMENT" in row and not pd.isnull("SUBCOMMENT"):
if "SUBCOMMENT" in row and not pd.isna("SUBCOMMENT"):
ertobs_str += (
" -- "
+ str(row["SUBCOMMENT"]).strip().replace("\n", "\n -- ").strip()
+ "\n"
)
ertobs_str += " OBS " + row["OBS"] + " {"
for dataname in ["I", "J", "K", "VALUE", "ERROR", "SOURCE"]:
if dataname in row and not pd.isnull(row[dataname]):
if dataname in row and not pd.isna(row[dataname]):
ertobs_str += " " + dataname + " = " + str(row[dataname]) + ";"
ertobs_str += "};\n"
ertobs_str += "};\n"
Expand Down Expand Up @@ -138,17 +140,17 @@ def dfhistory2ertobs(obs_df: pd.DataFrame) -> str:
.to_dict(orient="records")[0]
)
for dataname in ["ERROR", "ERROR_MODE", "ERROR_MIN"]:
if dataname in default_row and not pd.isnull(default_row[dataname]):
if dataname in default_row and not pd.isna(default_row[dataname]):
ertobs_str += (
" " + dataname + " = " + str(default_row[dataname]) + ";\n"
)
for _, row in history_df.iterrows():
if "SEGMENT" in row and not pd.isnull(row["SEGMENT"]):
if "SEGMENT" in row and not pd.isna(row["SEGMENT"]):
if row["SEGMENT"] == "DEFAULT":
continue
ertobs_str += " SEGMENT " + row["SEGMENT"] + " {"
for dataname in ["START", "STOP", "ERROR", "ERROR_MODE"]:
if dataname in row and not pd.isnull(row[dataname]):
if dataname in row and not pd.isna(row[dataname]):
ertobs_str += " " + dataname + " = " + str(row[dataname]) + ";"
ertobs_str += "};\n"
ertobs_str += "};\n"
Expand Down Expand Up @@ -185,7 +187,7 @@ def dfgeneral2ertobs(obs_df: pd.DataFrame) -> str:
"INDEX_LIST",
"ERROR_COVAR",
]:
if dataname in row and not pd.isnull(row[dataname]):
if dataname in row and not pd.isna(row[dataname]):
ertobs_str += " " + dataname + " = " + str(row[dataname]) + ";\n"
ertobs_str += "};\n"

Expand Down Expand Up @@ -240,11 +242,11 @@ def summary_df2obsdict(smry_df: pd.DataFrame) -> List[dict]:
assert isinstance(smry_df, pd.DataFrame)
if "CLASS" in smry_df:
assert len(smry_df["CLASS"].unique()) == 1
smry_df.drop("CLASS", axis=1, inplace=True)
smry_df = smry_df.drop("CLASS", axis=1)

smry_obs_list = []
if isinstance(smry_df, pd.DataFrame):
smry_df.dropna(axis=1, how="all", inplace=True)
smry_df = smry_df.dropna(axis=1, how="all")

if "DATE" not in smry_df:
raise ValueError("Can't have summary observation without a date")
Expand All @@ -258,10 +260,10 @@ def summary_df2obsdict(smry_df: pd.DataFrame) -> List[dict]:
for smrykey, smrykey_df in smry_df.groupby("KEY"):
smry_obs_element = {}
smry_obs_element["key"] = smrykey
if "COMMENT" in smrykey_df and not pd.isnull(smrykey_df["COMMENT"]).all():
if "COMMENT" in smrykey_df and not pd.isna(smrykey_df["COMMENT"]).all():
smry_obs_element["comment"] = smrykey_df["COMMENT"].unique()[0]
if isinstance(smrykey_df, pd.DataFrame):
smrykey_df.drop("KEY", axis=1, inplace=True)
smrykey_df = smrykey_df.drop("KEY", axis=1)
if "SUBCOMMENT" in smrykey_df:
smrykey_df["COMMENT"] = smrykey_df["SUBCOMMENT"]
del smrykey_df["SUBCOMMENT"]
Expand Down Expand Up @@ -317,14 +319,12 @@ def block_df2obsdict(block_df: pd.DataFrame) -> List[dict]:
block_obs_list = []
if "CLASS" in block_df:
assert len(block_df["CLASS"].unique()) == 1
block_df.drop("CLASS", axis=1, inplace=True)
block_df = block_df.drop("CLASS", axis=1)

if "DATE" not in block_df:
raise ValueError("Can't have rft/block observation without a date")

block_df = convert_dframe_date_to_str(block_df)

block_df.dropna(axis=1, how="all", inplace=True)
block_df = convert_dframe_date_to_str(block_df).dropna(axis=1, how="all")

for blocklabel, blocklabel_df in block_df.groupby(["LABEL", "DATE"]):
blocklabel_dict = {}
Expand Down Expand Up @@ -372,13 +372,13 @@ def df2obsdict(obs_df: pd.DataFrame) -> dict:
return {}

# Process SUMMARY_OBSERVATION:
if "SUMMARY_OBSERVATION" in obs_df["CLASS"].values:
if "SUMMARY_OBSERVATION" in obs_df["CLASS"].to_numpy():
obsdict[CLASS_SHORTNAME["SUMMARY_OBSERVATION"]] = summary_df2obsdict(
obs_df.set_index("CLASS").loc[["SUMMARY_OBSERVATION"]]
)

# Process BLOCK_OBSERVATION:
if "BLOCK_OBSERVATION" in obs_df["CLASS"].values:
if "BLOCK_OBSERVATION" in obs_df["CLASS"].to_numpy():
obsdict[CLASS_SHORTNAME["BLOCK_OBSERVATION"]] = block_df2obsdict(
obs_df.set_index("CLASS").loc[["BLOCK_OBSERVATION"]]
)
Expand Down Expand Up @@ -408,9 +408,9 @@ def df2resinsight_df(obs_df: pd.DataFrame) -> pd.DataFrame:
ri_dframe = obs_df.copy()

# Only SUMMARY_OBSERVATION is supported:
ri_dframe = ri_dframe[ri_dframe["CLASS"] == "SUMMARY_OBSERVATION"]

ri_dframe.rename({"KEY": "VECTOR"}, axis="columns", inplace=True)
ri_dframe = ri_dframe[ri_dframe["CLASS"] == "SUMMARY_OBSERVATION"].rename(
{"KEY": "VECTOR"}, axis="columns"
)

# Ensure all vectors are present:
for ri_vec in ri_column_names:
Expand Down
Loading