Skip to content

Commit

Permalink
Remove slow performant function in RftPlotter
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt committed Aug 11, 2023
1 parent a2c587e commit 5299b48
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,16 @@ def __init__(
self.ertdatadf["ABSDIFF"] = abs(
self.ertdatadf["SIMULATED"] - self.ertdatadf["OBSERVED"]
)
self.ertdatadf["YEAR"] = pd.to_datetime(self.ertdatadf["DATE"]).dt.year
self.ertdatadf = self.ertdatadf.sort_values(by="DATE")
self.ertdatadf["DATE_IDX"] = self.ertdatadf["DATE"].apply(
lambda x: list(self.ertdatadf["DATE"].unique()).index(x)
)

self.ertdatadf["DATETIME"] = pd.to_datetime(self.ertdatadf["DATE"])
self.ertdatadf["YEAR"] = self.ertdatadf["DATETIME"].dt.year

self.date_marks = self.set_date_marks()
self.ertdatadf_inactive = filter_frame(
self.ertdatadf,
{
"ACTIVE": 0,
},
)
self.ertdatadf = self.ertdatadf.sort_values(by="DATETIME")

self.ertdatadf_inactive = filter_frame(self.ertdatadf, {"ACTIVE": 0})
self.ertdatadf = filter_frame(self.ertdatadf, {"ACTIVE": 1})

self.ertdatadf = filter_frame(
self.ertdatadf,
{
"ACTIVE": 1,
},
)
self.ertdatadf["STDDEV"] = self.ertdatadf.groupby(
["WELL", "DATE", "ZONE", "ENSEMBLE", "TVD"]
)["SIMULATED"].transform("std")
Expand All @@ -148,6 +139,10 @@ def zone_names(self) -> List[str]:
def dates(self) -> List[str]:
return sorted(list(self.ertdatadf["DATE"].unique()))

@property
def datetime_dates(self) -> List[np.datetime64]:
return sorted(list(self.ertdatadf["DATETIME"].unique()))

def date_in_well(self, well: str) -> List[str]:
df = self.ertdatadf.loc[self.ertdatadf["WELL"] == well]
return [str(d) for d in list(df["DATE"].unique())]
Expand All @@ -168,26 +163,28 @@ def enscolors(self) -> dict:
def parameters(self) -> List[str]:
return self.param_model.parameters

def set_date_marks(self) -> Dict[str, Dict[str, Any]]:
def set_date_marks(self) -> Dict[int, Dict[str, Any]]:
marks = {}
idx_steps = np.linspace(
start=0,
stop=self.ertdatadf["DATE_IDX"].max(),
num=min(4, len(self.ertdatadf["DATE_IDX"].unique())),
dtype=int,
)
date_steps = self.ertdatadf.loc[self.ertdatadf["DATE_IDX"].isin(idx_steps)][
"DATE"
].unique()

for i, date_index in enumerate(idx_steps):
marks[str(date_index)] = {
"label": f"{date_steps[i]}",
"style": {
"white-space": "nowrap",
"font-weight": "bold",
},
}

# Set number of datemarks for the slider
num_samples = 4

# Generate evenly spaced indices
indices = np.linspace(0, len(self.datetime_dates) - 1, num_samples).astype(int)

# Sample dates using the calculated indices
sampled_dates = [self.datetime_dates[i] for i in indices]

for i, date in enumerate(self.datetime_dates):
if date in sampled_dates:
timestring = pd.to_datetime(date).strftime("%Y-%m-%d")
marks[i] = {
"label": timestring,
"style": {
"white-space": "nowrap",
"font-weight": "bold",
},
}
return marks

def get_param_real_and_value_df(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ def __init__(
self,
ensembles: List[str],
zones: List[str],
date_marks: Dict[str, Dict[str, Any]],
date_range_min: int,
date_range_max: int,
date_marks: Dict[int, Dict[str, Any]],
unique_dates_count: int,
) -> None:
super().__init__("Map settings")
self._ensembles = ensembles
self._zone_names = zones
self._date_marks = date_marks
self._date_range_min = date_range_min
self._date_range_max = date_range_max
self._unique_dates_count = unique_dates_count

def layout(self) -> List[Component]:
return [
Expand Down Expand Up @@ -79,12 +77,9 @@ def layout(self) -> List[Component]:
wcc.RangeSlider(
label="Filter date range",
id=self.register_component_unique_id(self.Ids.DATE_RANGE),
min=self._date_range_min,
max=self._date_range_max,
value=[
self._date_range_min,
self._date_range_max,
],
min=0,
max=self._unique_dates_count - 1,
value=[0, self._unique_dates_count - 1],
marks=self._date_marks,
),
wcc.Label(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@


class MapFigure:
def __init__(self, ertdf: pd.DataFrame, ensemble: str, zones: List[str]) -> None:
def __init__(
self, ertdf: pd.DataFrame, ensemble: str, zones: List[str], dates: List[str]
) -> None:
self._min_date, self._max_date = dates
self._ertdf = ertdf.loc[
(ertdf["ENSEMBLE"] == ensemble)
& (ertdf["ZONE"].isin(zones))
& (ertdf["DATE"] >= self._min_date)
& (ertdf["DATE"] <= self._max_date)
]
self._ertdf = (
ertdf.loc[(ertdf["ENSEMBLE"] == ensemble) & (ertdf["ZONE"].isin(zones))]
self._ertdf.drop(columns="ZONE")
.groupby(["WELL", "DATE", "ENSEMBLE"])
.mean(numeric_only=False)
.reset_index()
)

self._traces: List[Dict[str, Any]] = []

def add_misfit_plot(
self,
sizeby: ColorAndSizeByType,
colorby: ColorAndSizeByType,
dates: List[float],
) -> None:
df = self._ertdf.loc[
(self._ertdf["DATE_IDX"] >= dates[0])
& (self._ertdf["DATE_IDX"] <= dates[1])
]
df = self._ertdf
self._traces.append(
{
"x": df["EAST"],
Expand Down Expand Up @@ -106,6 +110,7 @@ def layout(self) -> Dict[str, Any]:
"margin": {"t": 50, "l": 50},
"xaxis": {"constrain": "domain", "showgrid": False},
"yaxis": {"scaleanchor": "x", "showgrid": False},
"title": f"Date filter: {self._min_date} - {self._max_date}",
}

@property
Expand Down
10 changes: 5 additions & 5 deletions webviz_subsurface/plugins/_rft_plotter/_views/_map_view/_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def __init__(self, datamodel: RftPlotterDataModel) -> None:
ensembles=self._datamodel.ensembles,
zones=self._datamodel.zone_names,
date_marks=self._datamodel.date_marks,
date_range_min=self._datamodel.ertdatadf["DATE_IDX"].min(),
date_range_max=self._datamodel.ertdatadf["DATE_IDX"].max(),
unique_dates_count=len(self._datamodel.dates),
),
self.Ids.MAP_SETTINGS,
)
Expand Down Expand Up @@ -125,13 +124,14 @@ def _update_map(
ensemble: str,
sizeby: ColorAndSizeByType,
colorby: ColorAndSizeByType,
dates: List[float],
dateidx: List[int],
zones: List[str],
) -> Union[str, List[wcc.Graph]]:
figure = MapFigure(self._datamodel.ertdatadf, ensemble, zones)
dates = [self._datamodel.dates[idx] for idx in dateidx]
figure = MapFigure(self._datamodel.ertdatadf, ensemble, zones, dates)
if self._datamodel.faultlinesdf is not None:
figure.add_fault_lines(self._datamodel.faultlinesdf)
figure.add_misfit_plot(sizeby, colorby, dates)
figure.add_misfit_plot(sizeby, colorby)

return [
wcc.Graph(
Expand Down

0 comments on commit 5299b48

Please sign in to comment.