From c65e299686f4f9dd3802b15955a4b590b285b87d Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 11:28:55 +0800 Subject: [PATCH 01/10] revisit inaccurate Sw normalization in SCAL module --- quick_pp/app/backend/services/workspace.py | 387 +++++--- .../components/modules/MultiWellShf.svelte | 11 +- .../components/modules/MultiWellSwirr.svelte | 186 ++-- .../workbench/CrossplotCanvas.svelte | 837 +++++++++++++++--- .../workbench/LogTrackCanvas.svelte | 150 ++-- .../src/lib/state/projectData.svelte.ts | 212 ++++- .../frontend/src/lib/state/wellData.svelte.ts | 25 +- quick_pp/app/frontend/src/routes/+page.svelte | 155 +++- 8 files changed, 1528 insertions(+), 435 deletions(-) diff --git a/quick_pp/app/backend/services/workspace.py b/quick_pp/app/backend/services/workspace.py index cd88b509..f43edaaa 100644 --- a/quick_pp/app/backend/services/workspace.py +++ b/quick_pp/app/backend/services/workspace.py @@ -1160,9 +1160,70 @@ class WorkspaceShfRequest(BaseModel): None, description="Optional SwirrModelConfig dict used when swirr_source == 'poro_perm'", ) + zones: Optional[List[str]] = Field( + None, + description="Optional subset of zones to filter SCAL core data", + ) + min_depth: Optional[float] = Field( + None, + description="Optional minimum depth filter for SCAL core data", + ) + max_depth: Optional[float] = Field( + None, + description="Optional maximum depth filter for SCAL core data", + ) save: bool = Field(False) +def _resolve_scal_sample_zone( + s_depth: float, + well_df: Optional[pd.DataFrame], + anc: Optional[Dict[str, Any]], +) -> str: + """Resolve the geological zone name for a SCAL core sample at `s_depth`.""" + if s_depth > 0: + if ( + well_df is not None + and not well_df.empty + and "DEPTH" in well_df.columns + and "ZONES" in well_df.columns + ): + try: + depth_diff = (well_df["DEPTH"] - s_depth).abs() + idx_near = depth_diff.idxmin() + if float(depth_diff.loc[idx_near]) <= 5.0: + z_val = well_df.loc[idx_near, "ZONES"] + if pd.notna(z_val): + z_str = str(z_val).strip() + if z_str and z_str.lower() not in ("unknown", "none", "nan"): + return z_str + except Exception: + pass + tops = (anc or {}).get("formation_tops") + if isinstance(tops, list) and tops: + valid_tops = [] + for t in tops: + if isinstance(t, dict): + td = t.get("depth") + tn = str(t.get("name") or t.get("zone") or "").strip() + if td is not None and tn: + try: + valid_tops.append((float(td), tn)) + except (TypeError, ValueError): + pass + if valid_tops: + valid_tops.sort(key=lambda x: x[0]) + chosen = "Unknown" + for td, tn in valid_tops: + if s_depth >= td: + chosen = tn + else: + break + if chosen != "Unknown": + return chosen + return "Unknown" + + def _filter_fzi_data_by_wells( fzi_data: Dict[str, List[Any]], well_names: Optional[List[str]] ) -> Dict[str, List[Any]]: @@ -5937,15 +5998,38 @@ def workspace_shf_compute(project_id: int, req: WorkspaceShfRequest) -> Dict[str ) if str(pid).strip() } - excl_pt_set = { - str(ptid).strip() - for ptid in ( - swirr_cfg_dict.get("excludedScalPointIds") - or swirr_cfg_dict.get("excluded_scal_point_ids") - or [] - ) - if str(ptid).strip() - } + excl_plug_set = set() + for pid in ( + swirr_cfg_dict.get("excludedPlugIds") + or swirr_cfg_dict.get("excluded_plug_ids") + or [] + ): + p_str = str(pid).strip() + if p_str: + excl_plug_set.add(p_str) + if "::" in p_str: + excl_plug_set.add(p_str.replace("::", ":", 1)) + elif ":" in p_str: + excl_plug_set.add(p_str.replace(":", "::", 1)) + excl_pt_set = set() + for ptid in ( + swirr_cfg_dict.get("excludedScalPointIds") + or swirr_cfg_dict.get("excluded_scal_point_ids") + or [] + ): + pt_str = str(ptid).strip() + if pt_str: + excl_pt_set.add(pt_str) + if pt_str.count("::") == 1 and ":" in pt_str.split("::")[0]: + p_part, idx_part = pt_str.split("::", 1) + excl_pt_set.add(f"{p_part.replace(':', '::', 1)}::{idx_part}") + allowed_zones_set = ( + {str(z).strip() for z in req.zones if str(z).strip()} + if req.zones + else None + ) + min_d_filter = float(req.min_depth) if req.min_depth is not None else None + max_d_filter = float(req.max_depth) if req.max_depth is not None else None core_rows: List[Dict[str, Any]] = [] with get_db() as session: proj = db_objects.Project.load(session, project_id=project_id) @@ -5959,12 +6043,16 @@ def workspace_shf_compute(project_id: int, req: WorkspaceShfRequest) -> Dict[str core_data = (anc or {}).get("core_data") or {} well_df_cache: Optional[pd.DataFrame] = None for sname, sample in core_data.items(): - if f"{wn}::{sname}" in excl_plug_set: + if f"{wn}::{sname}" in excl_plug_set or f"{wn}:{sname}" in excl_plug_set: continue pc_df = sample.get("pc") if pc_df is None or pc_df.empty: continue s_depth = float(sample.get("depth") or 0.0) + if min_d_filter is not None and s_depth > 0 and s_depth < min_d_filter: + continue + if max_d_filter is not None and s_depth > 0 and s_depth > max_d_filter: + continue meas = sample.get("measurements") cperm = None cpore = None @@ -6087,6 +6175,15 @@ def _norm_plug_stem(val: Any) -> str: # Fallback 2: lookup well's merged CPORE/CPERM (within ±0.50m) or log PHIT & PERM_TRANS/PERM at nearest depth near_rock_flag: Optional[int] = None + if s_depth > 0 or allowed_zones_set is not None: + try: + if well_df_cache is None: + well_df_cache = _get_filtered_well_df(project_id, wn) + except Exception: + pass + s_zone = _resolve_scal_sample_zone(s_depth, well_df_cache, anc) + if allowed_zones_set is not None and s_zone not in allowed_zones_set: + continue if s_depth > 0: try: if well_df_cache is None: @@ -6220,39 +6317,30 @@ def _norm_plug_stem(val: Any) -> str: cpore, cperm, rf_val, getattr(req, "swirr_config", None) ) if swirr_src_mode == "poro_perm": - raw_swn = (sw_vals - swirr_calc_plug) / max( - 0.05, 1.0 - swirr_calc_plug + eff_swirr = float(np.clip(swirr_calc_plug, 0.01, 0.85)) + raw_swn = (sw_vals - eff_swirr) / max( + 0.05, 1.0 - eff_swirr ) swn_vals = np.clip(raw_swn, 0.005, 0.99) - elif ( - len(sw_vals) >= 4 - and (np.nanmax(sw_vals) - np.nanmin(sw_vals)) > 0.25 - ): - # Use a poro-perm bounded Swirr estimate so incomplete drainage curves - # (e.g., tight plugs terminating at Sw=0.41) are not artificially stretched to Swn=0.030 - sw_min_plug = float(np.nanmin(sw_vals)) - swirr_est = float( - np.clip( - 0.08 - * ( - (max(float(cperm), 0.01) / max(float(cpore), 0.01)) - ** -0.12 - ), - 0.04, - 0.22, - ) - ) - eff_swirr = min(sw_min_plug * 0.92, swirr_est) - raw_swn = (sw_vals - eff_swirr) / max(1.0 - eff_swirr, 1e-3) - swn_vals = np.clip(raw_swn, 0.02, 0.98) else: - swn_vals = np.clip(sw_vals, 0.02, 0.98) + # Per-plug Lab Terminal Sw normalization: each sample is normalized by its own + # minimum/terminal lab Sw (with a 0.96 asymptote factor so the terminal point remains > 0 on log-log J fits) + sw_min_plug = float(np.nanmin(sw_vals)) + eff_swirr = float(np.clip(sw_min_plug * 0.96, 0.01, 0.85)) + raw_swn = (sw_vals - eff_swirr) / max(1.0 - eff_swirr, 0.05) + swn_vals = np.clip(raw_swn, 0.005, 0.99) + swirr_used_plug = eff_swirr + order_pc = np.argsort(pc_vals) + pc_vals = pc_vals[order_pc] + sw_vals = sw_vals[order_pc] + swn_vals = swn_vals[order_pc] for pt_idx, (pc_v, sw_v, swn_v) in enumerate(zip(pc_vals, sw_vals, swn_vals, strict=False)): - if ( + is_pt_excl = ( f"{wn}::{sname}::{pt_idx}" in excl_pt_set + or f"{wn}:{sname}::{pt_idx}" in excl_pt_set or f"{wn}::{sname}::{round(float(pc_v), 2)}" in excl_pt_set - ): - continue + or f"{wn}:{sname}::{round(float(pc_v), 2)}" in excl_pt_set + ) if not (0.004 <= swn_v <= 0.995 and pc_v > 0): continue j_v = float(leverett_j(pc_v, req.ift, req.theta, cperm, cpore)) @@ -6271,7 +6359,7 @@ def _norm_plug_stem(val: Any) -> str: "PC": round(float(pc_v), 4), "SW": round(float(sw_v), 4), "SWN": round(float(swn_v), 4), - "SWIRR_CALC": round(float(swirr_calc_plug), 4), + "SWIRR_CALC": round(float(swirr_used_plug), 4), "J": round(float(j_v), 4), "H": round(float(h_v), 4), "BVW": round(float(bvw_v), 4), @@ -6280,74 +6368,93 @@ def _norm_plug_stem(val: Any) -> str: "Well": wn, "Sample": str(sname), "DEPTH": round(float(s_depth), 2), + "ZONE": s_zone, + "POINT_ID": f"{wn}::{sname}::{pt_idx}", + "PLUG_ID": f"{wn}::{sname}", + "EXCLUDED": bool(is_pt_excl), } ) - # Synthesize representative SCAL capillary pressure curves for reservoir rock types with < 3 plugs + # Synthesize representative SCAL capillary pressure curves only if project has < 3 real SCAL points rf_arr_full = np.array( [int(r) if r is not None else -1 for r in rock_flags], dtype=int ) default_well = well_names[0] if well_names else "Project" - for rf_int in reservoir_flags: - existing_rf = [r for r in core_rows if int(r["ROCK_FLAG"]) == rf_int] - if len(existing_rf) >= 3: - continue - mask_rf = ( - (rf_arr_full == rf_int) - & np.isfinite(phit_arr) - & np.isfinite(perm_arr) - & (phit_arr > 0) - & (perm_arr > 0) - ) - p_med = ( - float(np.nanmedian(phit_arr[mask_rf])) - if np.any(mask_rf) - else max(0.24 - 0.04 * (rf_int - 1), 0.06) - ) - k_med = ( - float(np.nanmedian(perm_arr[mask_rf])) - if np.any(mask_rf) - else max(250.0 / (4.0 ** (rf_int - 1)), 0.5) - ) - swirr = float(np.clip(0.06 + 0.05 * (rf_int - 1), 0.05, 0.35)) - j_a = float( - np.clip( - 0.12 - * ((max(k_med, 0.01) / max(p_med, 0.01)) ** -0.06) - * (1.0 + 0.22 * (rf_int - 1)), - 0.05, - 0.65, + if len(core_rows) < 3: + for rf_int in reservoir_flags: + existing_rf = [r for r in core_rows if int(r["ROCK_FLAG"]) == rf_int] + if len(existing_rf) >= 3: + continue + mask_rf = ( + (rf_arr_full == rf_int) + & np.isfinite(phit_arr) + & np.isfinite(perm_arr) + & (phit_arr > 0) + & (perm_arr > 0) ) - ) - j_b = float(np.clip(0.24 + 0.06 * (rf_int - 1), 0.15, 0.65)) - cos_t = max(abs(math.cos(math.radians(req.theta))), 0.1) - sqrt_kp = max(math.sqrt(max(k_med, 1e-4) / max(p_med, 1e-4)), 1e-3) - for idx_pt, swn_v in enumerate( - (0.08, 0.14, 0.22, 0.32, 0.45, 0.60, 0.76, 0.90) - ): - noise = 1.0 + 0.03 * ((-1.0) ** idx_pt) - j_v = float(j_a * (swn_v ** (-j_b)) * noise) - pc_v = float(max((j_v * req.ift * cos_t) / (0.2166 * sqrt_kp), 0.05)) - sw_v = float(np.clip(swirr + swn_v * (1.0 - swirr), 0.02, 0.99)) - h_v = float(pc_v / grad_psi) - bvw_v = float(p_med * sw_v) - core_rows.append( - { - "ROCK_FLAG": rf_int, - "PC": round(pc_v, 4), - "SW": round(sw_v, 4), - "SWN": round(float(swn_v), 4), - "J": round(j_v, 4), - "H": round(h_v, 4), - "BVW": round(bvw_v, 4), - "PHIT": round(p_med, 4), - "PERM": round(k_med, 4), - "Well": default_well, - "Sample": f"SYN_RT{rf_int}_{idx_pt + 1}", - } + p_med = ( + float(np.nanmedian(phit_arr[mask_rf])) + if np.any(mask_rf) + else max(0.24 - 0.04 * (rf_int - 1), 0.06) ) + k_med = ( + float(np.nanmedian(perm_arr[mask_rf])) + if np.any(mask_rf) + else max(250.0 / (4.0 ** (rf_int - 1)), 0.5) + ) + swirr = float(np.clip(0.06 + 0.05 * (rf_int - 1), 0.05, 0.35)) + j_a = float( + np.clip( + 0.12 + * ((max(k_med, 0.01) / max(p_med, 0.01)) ** -0.06) + * (1.0 + 0.22 * (rf_int - 1)), + 0.05, + 0.65, + ) + ) + j_b = float(np.clip(0.24 + 0.06 * (rf_int - 1), 0.15, 0.65)) + cos_t = max(abs(math.cos(math.radians(req.theta))), 0.1) + sqrt_kp = max(math.sqrt(max(k_med, 1e-4) / max(p_med, 1e-4)), 1e-3) + for idx_pt, swn_v in enumerate( + (0.08, 0.14, 0.22, 0.32, 0.45, 0.60, 0.76, 0.90) + ): + noise = 1.0 + 0.03 * ((-1.0) ** idx_pt) + j_v = float(j_a * (swn_v ** (-j_b)) * noise) + pc_v = float(max((j_v * req.ift * cos_t) / (0.2166 * sqrt_kp), 0.05)) + sw_v = float(np.clip(swirr + swn_v * (1.0 - swirr), 0.02, 0.99)) + h_v = float(pc_v / grad_psi) + bvw_v = float(p_med * sw_v) + sname_syn = f"SYN_RT{rf_int}_{idx_pt + 1}" + core_rows.append( + { + "ROCK_FLAG": rf_int, + "PC": round(pc_v, 4), + "SW": round(sw_v, 4), + "SWN": round(float(swn_v), 4), + "SWIRR_CALC": round(float(swirr), 4), + "J": round(j_v, 4), + "H": round(h_v, 4), + "BVW": round(bvw_v, 4), + "PHIT": round(p_med, 4), + "PERM": round(k_med, 4), + "Well": default_well, + "Sample": sname_syn, + "DEPTH": round(1500.0 + rf_int * 25.0 + idx_pt * 2.0, 2), + "ZONE": "Unknown", + "POINT_ID": f"{default_well}::{sname_syn}::{idx_pt}", + "PLUG_ID": f"{default_well}::{sname_syn}", + "EXCLUDED": False, + } + ) scal_df = pd.DataFrame(core_rows) + scal_df_fit = ( + scal_df[~scal_df["EXCLUDED"].astype(bool)] + if (not scal_df.empty and "EXCLUDED" in scal_df.columns) + else scal_df + ) + if scal_df_fit.empty and not scal_df.empty: + scal_df_fit = scal_df j_sat_mode = ( "sw" if str(getattr(req, "j_sat_mode", "swn") or "swn").strip().lower() == "sw" @@ -6355,8 +6462,8 @@ def _norm_plug_stem(val: Any) -> str: ) fits_swn: Dict[str, Dict[str, float]] = {} fits_sw: Dict[str, Dict[str, float]] = {} - if model == "leverett_j" and not scal_df.empty: - for param in auto_j_params(scal_df): + if model == "leverett_j" and not scal_df_fit.empty: + for param in auto_j_params(scal_df_fit): fits_swn[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6364,7 +6471,7 @@ def _norm_plug_stem(val: Any) -> str: "r2": float(param.get("r2", 0.0)), "count": int(param.get("count", 0)), } - scal_df_sw = scal_df.assign(SWN=scal_df["SW"]) + scal_df_sw = scal_df_fit.assign(SWN=scal_df_fit["SW"]) for param in auto_j_params(scal_df_sw): fits_sw[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), @@ -6396,9 +6503,9 @@ def _norm_plug_stem(val: Any) -> str: "r2": 1.0, "count": 0, } - elif not scal_df.empty: + elif not scal_df_fit.empty: if model == "skelt_harrison": - for param in auto_skelt_harrison_params(scal_df): + for param in auto_skelt_harrison_params(scal_df_fit): fits[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6409,7 +6516,7 @@ def _norm_plug_stem(val: Any) -> str: "count": int(param.get("count", 0)), } elif model == "cuddy": - for param in auto_cuddy_params(scal_df): + for param in auto_cuddy_params(scal_df_fit): fits[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6423,14 +6530,14 @@ def _norm_plug_stem(val: Any) -> str: fits[rf_k] = dict(f_val) # Recompute fit diagnostics (rmse, r2, count) against SCAL points for all entries in fits - if not scal_df.empty and fits: + if not scal_df_fit.empty and fits: j_sat_col = "SW" if j_sat_mode == "sw" else "SWN" for rf_key, fit_entry in list(fits.items()): try: rf_int = int(rf_key) except ValueError: continue - sub_scal = scal_df[scal_df["ROCK_FLAG"] == rf_int] + sub_scal = scal_df_fit[scal_df_fit["ROCK_FLAG"] == rf_int] if sub_scal.empty: continue a_f = float(fit_entry.get("a", 0.2)) @@ -6960,6 +7067,31 @@ def _norm_plug_stem(val: Any) -> str: else [], "well_names": scal_df["Well"].tolist() if not scal_df.empty else [], "samples": scal_df["Sample"].tolist() if not scal_df.empty else [], + "depths": ( + scal_df["DEPTH"].tolist() + if (not scal_df.empty and "DEPTH" in scal_df.columns) + else [] + ), + "zones": ( + scal_df["ZONE"].tolist() + if (not scal_df.empty and "ZONE" in scal_df.columns) + else [] + ), + "point_ids": ( + scal_df["POINT_ID"].tolist() + if (not scal_df.empty and "POINT_ID" in scal_df.columns) + else [] + ), + "plug_ids": ( + scal_df["PLUG_ID"].tolist() + if (not scal_df.empty and "PLUG_ID" in scal_df.columns) + else [] + ), + "excluded": ( + [bool(x) for x in scal_df["EXCLUDED"].tolist()] + if (not scal_df.empty and "EXCLUDED" in scal_df.columns) + else [] + ), } return { @@ -10736,6 +10868,9 @@ def workspace_clear_calculated_curves( class WorkspaceSwirrFitRequest(BaseModel): well_names: Optional[List[str]] = Field(None) + zones: Optional[List[str]] = Field(None) + min_depth: Optional[float] = Field(None) + max_depth: Optional[float] = Field(None) extraction_mode: Optional[str] = Field(None) extractionMode: Optional[str] = Field(None) reference_pc_psi: Optional[float] = Field(None) @@ -10821,6 +10956,9 @@ def _extract_project_scal_plugs_for_swirr( cutoffs: Optional[List[float]] = None, ift: float = 30.0, theta: float = 30.0, + zones: Optional[List[str]] = None, + min_depth: Optional[float] = None, + max_depth: Optional[float] = None, ) -> List[Dict[str, Any]]: """Extract plug-level SCAL drainage curves and matched CPORE/CPERM/ROCK_FLAG across project wells.""" cutoffs_list = sorted(float(c) for c in (cutoffs or [0.5, 1.2, 2.5, 5.0])) @@ -10850,6 +10988,14 @@ def _extract_project_scal_plugs_for_swirr( rf for rf in valid_flags if highest_flag is None or rf < highest_flag ] or ([1, 2, 3] if not valid_flags else valid_flags) + allowed_zones_set = ( + {str(z).strip() for z in zones if str(z).strip()} + if zones + else None + ) + min_d_filter = float(min_depth) if min_depth is not None else None + max_d_filter = float(max_depth) if max_depth is not None else None + plug_records: List[Dict[str, Any]] = [] with get_db() as session: proj = db_objects.Project.load(session, project_id=project_id) @@ -10867,6 +11013,10 @@ def _extract_project_scal_plugs_for_swirr( if pc_df is None or pc_df.empty: continue s_depth = float(sample.get("depth") or 0.0) + if min_d_filter is not None and s_depth > 0 and s_depth < min_d_filter: + continue + if max_d_filter is not None and s_depth > 0 and s_depth > max_d_filter: + continue meas = sample.get("measurements") cperm = None cpore = None @@ -10974,6 +11124,15 @@ def _norm_stem(val: Any) -> str: has_core_perm = True near_rock_flag: Optional[int] = None + if s_depth > 0 or allowed_zones_set is not None: + try: + if well_df_cache is None: + well_df_cache = _get_filtered_well_df(project_id, wn) + except Exception: + pass + s_zone = _resolve_scal_sample_zone(s_depth, well_df_cache, anc) + if allowed_zones_set is not None and s_zone not in allowed_zones_set: + continue if s_depth > 0: try: if well_df_cache is None: @@ -11078,10 +11237,11 @@ def _norm_stem(val: Any) -> str: plug_records.append( { - "plug_id": f"{wn}:{sname}", + "plug_id": f"{wn}::{sname}", "well": str(wn), "sample": str(sname), "depth": round(float(s_depth), 2), + "zone": s_zone, "rt": int(rf_val), "cpore": round(float(cpore), 4), "cperm": round(float(cperm), 4), @@ -11095,7 +11255,7 @@ def _norm_stem(val: Any) -> str: [int(r) if r is not None else -1 for r in rock_flags], dtype=int ) default_well = fzi_wells[0] if fzi_wells else "Project" - if len(plug_records) < 3: + if len(plug_records) < 3 and not allowed_zones_set: for rf_int in reservoir_flags: mask_rf = ( (rf_arr_full == rf_int) @@ -11138,10 +11298,11 @@ def _norm_stem(val: Any) -> str: sname_syn = f"SYN_RT{rf_int}_{p_idx + 1}" plug_records.append( { - "plug_id": f"{default_well}:{sname_syn}", + "plug_id": f"{default_well}::{sname_syn}", "well": str(default_well), "sample": sname_syn, "depth": round(1500.0 + rf_int * 25.0 + p_idx * 3.5, 2), + "zone": "Unknown", "rt": int(rf_int), "cpore": round(p_plug, 4), "cperm": round(k_plug, 4), @@ -11201,7 +11362,14 @@ def workspace_swirr_fit( if req.excluded_plug_ids is not None else (req.excludedPlugIds or []) ) - excluded_set = {str(x).strip() for x in excl_raw if str(x).strip()} + excluded_set = set() + for x in excl_raw: + x_s = str(x).strip() + if x_s: + if "::" not in x_s and ":" in x_s: + excluded_set.add(x_s.replace(":", "::", 1)) + else: + excluded_set.add(x_s) raw_plugs = _extract_project_scal_plugs_for_swirr( project_id, @@ -11210,6 +11378,9 @@ def workspace_swirr_fit( cutoffs=req.cutoffs, ift=req.ift, theta=req.theta, + zones=req.zones, + min_depth=req.min_depth, + max_depth=req.max_depth, ) plug_rows: List[Dict[str, Any]] = [] @@ -11224,7 +11395,7 @@ def workspace_swirr_fit( rqi_ratio = math.sqrt(cperm / max(0.001, cpore)) rqi_um = 0.0314 * rqi_ratio pid = str(p["plug_id"]) - is_excl = pid in excluded_set + is_excl = pid in excluded_set or pid.replace("::", ":", 1) in excluded_set is_incl = bool(valid_pc and not is_excl) plug_rows.append( diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte index 030a13c4..d48951ad 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte @@ -119,14 +119,6 @@ ); } } - if ( - !projectParams.status['shf']?.matched && - (projectData.swirrResult || - projectData.swirrApplyResult || - Object.keys(projectData.swirrConfig.rules ?? {}).length > 0) - ) { - projectData.shfParams.swirrSource = 'poro_perm'; - } if (!projectData.shfResult && projectData.swirrResult) { projectData.recomputeScalFitsFromPlugs(false); } @@ -192,6 +184,9 @@ `/quick_pp/workspace/projects/${encodeURIComponent(workbench.projectId)}/shf/compute`, { well_names: validTvdWells, + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + min_depth: workbench.depthFilter?.min ?? null, + max_depth: workbench.depthFilter?.max ?? null, model: sp.model, j_sat_mode: jSatMode, unit: sp.unit ?? 'metric', diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte index 8f1ec51a..4f60c576 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte @@ -252,6 +252,9 @@ { well_names: projectData.selectedWellNames.length > 0 ? projectData.selectedWellNames : null, + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + min_depth: workbench.depthFilter?.min ?? null, + max_depth: workbench.depthFilter?.max ?? null, extraction_mode: cfg.extractionMode, reference_pc_psi: cfg.referencePcPsi, equation_type: cfg.equationType, @@ -289,6 +292,9 @@ { well_names: projectData.selectedWellNames.length > 0 ? projectData.selectedWellNames : null, + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + min_depth: workbench.depthFilter?.min ?? null, + max_depth: workbench.depthFilter?.max ?? null, extraction_mode: cfg.extractionMode, reference_pc_psi: cfg.referencePcPsi, equation_type: cfg.equationType, @@ -765,22 +771,22 @@ Swn Swirr Normalization - {(projectData.shfParams.swirrSource ?? 'poro_perm') === 'poro_perm' + {(projectData.shfParams.swirrSource ?? 'lab_min') === 'poro_perm' ? 'Modeled Swirr(k, φ)' - : 'Plug min(Sw)'} + : 'Per-Plug Lab min(Sw)'}
- {#each [['poro_perm', 'Poro-Perm Correlation'], ['lab_min', 'Lab Terminal Sw']] as [srcKey, srcLabel]} + {#each [['lab_min', 'Lab Terminal Sw (Per-Plug)'], ['poro_perm', 'Poro-Perm Correlation']] as [srcKey, srcLabel]}
- {@const allPlugs = projectData.swirrResult?.plugs ?? []} + {@const rawPlugs = projectData.swirrResult?.plugs ?? []} + {@const allPlugs = rawPlugs.filter( + (p) => + projectData.isScalItemInActiveFilter(p.well, p.depth, p.zone) && + (projectData.scalFilterRockType === 'all' || + (p.rt ?? 1) === projectData.scalFilterRockType) + )} {@const excludedPlugIds = projectData.swirrConfig.excludedPlugIds ?? []} - {@const activePlugCount = Math.max(0, allPlugs.length - excludedPlugIds.length)} + {@const activePlugCount = allPlugs.filter( + (p) => + !p.excluded && + !excludedPlugIds.includes(projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample)) && + !excludedPlugIds.includes(p.plug_id) + ).length}
-
+
- SCAL Point & Core Sample Filter + SCAL Sample & Point QC -
- {#each [['point', 'Click: Point'], ['plug', 'Click: Sample']] as [tKey, tLbl]} - - {/each} +
+
+ {#each [['sample', 'By Sample'], ['rt', 'By RT']] as [gKey, gLbl]} + + {/each} +
+
+ {#each [['point', 'Click: Pt'], ['plug', 'Click: Sample']] as [tKey, tLbl]} + + {/each} +
@@ -965,65 +1003,113 @@
{/if} - {#if allPlugs.length > 0} -
+ {#if allPlugs.length > 0 || rawPlugs.length > 0} +
- Filter Core Samples / Plugs ({activePlugCount} / {allPlugs.length} active) + > + active{#if allPlugs.length < rawPlugs.length} + of {rawPlugs.length} total + {/if}) - {#if excludedPlugIds.length > 0} - - {excludedPlugIds.length} sample{excludedPlugIds.length > 1 ? 's' : ''} excl - - {/if} +
+ {#if projectData.focusedScalPlugId} + + {/if} + {#if excludedPlugIds.length > 0} + + {excludedPlugIds.length} excl + + {/if} +
-
+
- + + + + - + {#each allPlugs as plug (plug.plug_id)} + {@const normPid = projectData.normalizeScalPlugId( + plug.plug_id, + plug.well, + plug.sample + )} {@const isExcl = - Boolean(plug.excluded) || excludedPlugIds.includes(plug.plug_id)} + Boolean(plug.excluded) || + excludedPlugIds.includes(normPid) || + excludedPlugIds.includes(plug.plug_id)} + {@const isFocused = projectData.focusedScalPlugId === normPid} + {@const plugZone = projectData.resolveScalSampleZone( + plug.well, + plug.depth, + plug.zone + )} {@const rtColor = getRockTypeColor(plug.rt ?? 1)} { - projectData.swirrManualRuleOverride = false; - projectData.shfManualFitOverride = false; - projectData.toggleSwirrPlugExclusion(plug.plug_id); + projectData.toggleFocusedScalPlug(normPid); }} + title="Click row to spotlight {plug.well} · {plug.sample} on crossplot; use checkbox to include/exclude" > - + - + + +
UseUseQC Sample WellZoneDepth RT φk (mD)k Pts
+ e.stopPropagation()}> e.stopPropagation()} onchange={() => { projectData.swirrManualRuleOverride = false; projectData.shfManualFitOverride = false; - projectData.toggleSwirrPlugExclusion(plug.plug_id); + projectData.toggleSwirrPlugExclusion(normPid); }} class="h-3 w-3 accent-blue-600" /> + + {isFocused ? '●' : '◎'} + + {plug.sample}{plug.well}{plug.well} + {plugZone} + {fmtNum(plug.depth, 1)} (); + for (const p of projectData.swirrResult?.plugs ?? []) { + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const zn = projectData.resolveScalSampleZone(p.well, p.depth, p.zone); + plugMetaMap.set(pid, { depth: p.depth ?? 0, zone: zn }); + } - const byRt = new Map< - number, - { - swn: number[]; - sw: number[]; - j: number[]; - pc: number[]; - bvw: number[]; - h: number[]; - labels: string[]; - ptIds: string[]; - } - >(); + type ScalPointItem = { + idx: number; + rf: number; + well: string; + sample: string; + plugId: string; + rawPtId: string; + cdata: string; + depth: number; + zone: string; + pc: number; + sw: number; + swn: number; + swirrCalc: number | null; + j: number; + h: number; + bvw: number; + phit: number; + perm: number; + isExcl: boolean; + }; + + const byPlug = new Map(); const exclPts = { swn: [] as number[], sw: [] as number[], @@ -2013,12 +2067,33 @@ for (let i = 0; i < scal.pc.length; i++) { const rf = scal.rock_flags?.[i] ?? 1; - const rawPtId = - scal.point_ids?.[i] || - `${scal.well_names?.[i] ?? 'W'}::${scal.samples?.[i] ?? 'S'}::${i}`; + if (rtFilter !== 'all' && rf !== rtFilter) continue; + const wn = scal.well_names?.[i] ?? 'Well'; + const sname = scal.samples?.[i] ?? 'Sample'; + const plugId = projectData.normalizeScalPlugId(scal.plug_ids?.[i], wn, sname); + const meta = plugMetaMap.get(plugId); + const depthVal = scal.depths?.[i] ?? meta?.depth ?? 0; + const zoneVal = projectData.resolveScalSampleZone( + wn, + depthVal, + scal.zones?.[i] ?? meta?.zone + ); + if (!projectData.isScalItemInActiveFilter(wn, depthVal, zoneVal)) continue; + + const rawPtId = scal.point_ids?.[i] || `${plugId}::${i}`; const isExcl = Boolean(scal.excluded?.[i]) || excludedSet.has(rawPtId); const cdata = `scal_pt::${rawPtId}`; - const lbl = `${scal.samples?.[i] ?? 'Plug'} (${scal.well_names?.[i] ?? ''}) | Pc=${fmtNum(scal.pc[i], 2)} psi | Sw=${fmtNum(scal.sw[i], 3)}, Swn=${fmtNum(scal.swn[i], 3)}`; + const phitVal = scal.phit?.[i] ?? 0; + const permVal = scal.perm?.[i] ?? 0; + const swirrCalcVal = + typeof scal.swirr_calc?.[i] === 'number' && Number.isFinite(scal.swirr_calc[i]) + ? scal.swirr_calc[i] + : null; + const lbl = + `${wn} · Sample ${sname} [RT ${rf}]
` + + `Depth: ${depthVal > 0 ? fmtNum(depthVal, 2) + ' m' : '—'} | Zone: ${zoneVal}
` + + `CPORE: ${fmtNum(phitVal, 3)} v/v | CPERM: ${fmtNum(permVal, 2)} mD
` + + `Pc=${fmtNum(scal.pc[i], 2)} psi | Sw=${fmtNum(scal.sw[i], 3)}, Swirr=${swirrCalcVal !== null ? fmtNum(swirrCalcVal, 3) : '—'}, Swn=${fmtNum(scal.swn[i], 3)}`; if (selectedSet.has(rawPtId)) { selPts.swn.push(scal.swn[i]); @@ -2041,103 +2116,250 @@ continue; } - if (!byRt.has(rf)) { - byRt.set(rf, { - swn: [], - sw: [], - j: [], - pc: [], - bvw: [], - h: [], - labels: [], - ptIds: [] - }); - } - const grp = byRt.get(rf)!; - grp.swn.push(scal.swn[i]); - grp.sw.push(scal.sw[i]); - grp.j.push(scal.j[i]); - grp.pc.push(scal.pc[i]); - grp.bvw.push(scal.bvw[i]); - grp.h.push(scal.h[i]); - grp.labels.push(lbl); - grp.ptIds.push(cdata); + if (!byPlug.has(plugId)) byPlug.set(plugId, []); + byPlug.get(plugId)!.push({ + idx: i, + rf, + well: wn, + sample: sname, + plugId, + rawPtId, + cdata, + depth: depthVal, + zone: zoneVal, + pc: scal.pc[i], + sw: scal.sw[i], + swn: scal.swn[i], + swirrCalc: swirrCalcVal, + j: scal.j[i], + h: scal.h[i], + bvw: scal.bvw[i], + phit: phitVal, + perm: permVal, + isExcl + }); } - const sortedRts = [...byRt.keys()].sort((a, b) => a - b); - const maxJ = Math.max(2.0, ...scal.j.filter(Number.isFinite)); - const maxPc = Math.max(15.0, ...scal.pc.filter(Number.isFinite)); - const maxH = Math.max(30.0, ...scal.h.filter(Number.isFinite)); - const maxBvw = Math.max(0.25, ...scal.bvw.filter(Number.isFinite)); + // Sort points within each plug in ascending Pc order so connected lines trace drainage monotonically + for (const [, pts] of byPlug.entries()) { + pts.sort((a, b) => a.pc - b.pc); + } + + const allActivePts = Array.from(byPlug.values()).flat(); + const maxJ = Math.max( + 2.0, + ...allActivePts.map((p) => p.j).filter(Number.isFinite) + ); + const maxPc = Math.max( + 15.0, + ...allActivePts.map((p) => p.pc).filter(Number.isFinite) + ); + const maxH = Math.max( + 30.0, + ...allActivePts.map((p) => p.h).filter(Number.isFinite) + ); + const maxBvw = Math.max( + 0.25, + ...allActivePts.map((p) => p.bvw).filter(Number.isFinite) + ); const conv = (sp.unit ?? 'metric') === 'metric' ? 0.433 * 3.28084 : 0.433; const gradPsi = Math.max((sp.gw - sp.ghc) * conv, 1e-4); const useRawSw = (sp.jSatMode ?? 'swn') === 'sw'; const satSym = useRawSw ? 'Sw' : 'Swn'; - for (const rf of sortedRts) { - const g = byRt.get(rf)!; - const color = getRockTypeColor(rf); - if (activeModel === 'leverett_j') { + const getPointX = (p: ScalPointItem) => + activeModel === 'leverett_j' + ? useRawSw + ? p.sw + : p.swn + : activeModel === 'skelt_harrison' + ? p.sw + : p.bvw; + const getPointY = (p: ScalPointItem) => + activeModel === 'leverett_j' ? p.j : p.pc; + const formatPointHover = (p: ScalPointItem, stepIdx: number, totalSteps: number) => + `${p.well} · Sample ${p.sample} (Step ${stepIdx + 1}/${totalSteps}) · RT ${p.rf}
` + + `Depth: ${p.depth > 0 ? fmtNum(p.depth, 2) + ' m' : '—'} | Zone: ${p.zone}
` + + `CPORE: ${fmtNum(p.phit, 3)} v/v | CPERM: ${fmtNum(p.perm, 2)} mD
` + + `Pc: ${fmtNum(p.pc, 2)} psi | Sw: ${fmtNum(p.sw, 3)} | Swirr: ${p.swirrCalc !== null ? fmtNum(p.swirrCalc, 3) : '—'} | Swn: ${fmtNum(p.swn, 3)} | J: ${fmtNum(p.j, 3)}
` + + `Click to exclude ${projectData.scalClickFilterTarget === 'plug' ? 'sample' : 'point'} (Shift+Click for sample)`; + + if (groupMode === 'sample') { + const sortedPlugs = Array.from(byPlug.entries()).sort(([, a], [, b]) => { + const pa = a[0]; + const pb = b[0]; + if (pa.rf !== pb.rf) return pa.rf - pb.rf; + if (pa.well !== pb.well) return pa.well.localeCompare(pb.well); + return pa.sample.localeCompare(pb.sample, undefined, { numeric: true }); + }); + + sortedPlugs.forEach(([plugId, pts], pIdx) => { + if (pts.length === 0) return; + const p0 = pts[0]; + const isFocused = focusedPlugId === plugId; + const isDimmed = Boolean(focusedPlugId && !isFocused); + const col = SAMPLE_PALETTE[pIdx % SAMPLE_PALETTE.length]; + const sym = SAMPLE_SYMBOLS[Math.floor(pIdx / SAMPLE_PALETTE.length) % SAMPLE_SYMBOLS.length]; + const showTxt = showSampleLabels || isFocused; + const modeStr = connectPlugs + ? showTxt + ? 'lines+markers+text' + : 'lines+markers' + : showTxt + ? 'markers+text' + : 'markers'; + traces.push({ type: 'scatter', - mode: 'markers', - name: `SCAL RT ${rf} (${g.j.length})`, - x: useRawSw ? g.sw : g.swn, - y: g.j, - customdata: g.ptIds, - text: g.labels, - hovertemplate: - '%{text}
J=%{y:.3f}
Click to exclude pointRT ' + - rf + - '', + mode: modeStr, + name: `${p0.sample} (${p0.well}) [RT${p0.rf}]`, + x: pts.map(getPointX), + y: pts.map(getPointY), + customdata: pts.map((p) => p.cdata), + text: pts.map((p, idx) => + isFocused + ? `${p.sample}#${idx + 1}` + : idx === pts.length - 1 || !connectPlugs + ? p.sample + : '' + ), + textposition: 'top center', + textfont: { + family: 'JetBrains Mono', + size: isFocused ? 9.5 : 8.5, + color: col + }, + hovertext: pts.map((p, idx) => formatPointHover(p, idx, pts.length)), + hovertemplate: `%{hovertext}${p0.well} · ${p0.sample} (RT ${p0.rf})`, + line: connectPlugs + ? { + color: col, + width: isFocused ? 2.6 : 1.35, + dash: 'solid' + } + : undefined, marker: { - size: 6.5, - color, - opacity: 0.85, - line: { width: 0.5, color: '#fff' } - } + size: isFocused ? 9 : 7, + symbol: sym, + color: col, + opacity: isDimmed ? 0.2 : 0.92, + line: { + width: isFocused ? 1.5 : 0.7, + color: isFocused ? '#0F172A' : '#FFFFFF' + } + }, + opacity: isDimmed ? 0.22 : 1.0 }); - } else if (activeModel === 'skelt_harrison') { - traces.push({ - type: 'scatter', - mode: 'markers', - name: `SCAL RT ${rf} (${g.pc.length})`, - x: g.sw, - y: g.pc, - customdata: g.ptIds, - text: g.labels, - hovertemplate: - '%{text}
Sw=%{x:.3f}, Pc=%{y:.2f} psi
Click to exclude pointRT ' + - rf + - '', - marker: { - size: 6.5, - color, - opacity: 0.85, - line: { width: 0.5, color: '#fff' } + }); + } else { + // Group by Rock Type (with optional per-plug line connections and focused sample spotlight) + const plugsByRt = new Map>(); + for (const [plugId, pts] of byPlug.entries()) { + if (pts.length === 0) continue; + const rf = pts[0].rf; + if (!plugsByRt.has(rf)) plugsByRt.set(rf, []); + plugsByRt.get(rf)!.push([plugId, pts]); + } + const sortedRts = [...plugsByRt.keys()].sort((a, b) => a - b); + + for (const rf of sortedRts) { + const plugEntries = plugsByRt.get(rf)!; + const color = getRockTypeColor(rf); + const xVals: (number | null)[] = []; + const yVals: (number | null)[] = []; + const cdataVals: (string | null)[] = []; + const hoverVals: (string | null)[] = []; + const txtVals: string[] = []; + let ptCount = 0; + + for (const [, pts] of plugEntries) { + pts.forEach((p, idx) => { + xVals.push(getPointX(p)); + yVals.push(getPointY(p)); + cdataVals.push(p.cdata); + hoverVals.push(formatPointHover(p, idx, pts.length)); + txtVals.push( + showSampleLabels && (idx === pts.length - 1 || !connectPlugs) + ? p.sample + : '' + ); + ptCount++; + }); + if (connectPlugs) { + xVals.push(null); + yVals.push(null); + cdataVals.push(null); + hoverVals.push(null); + txtVals.push(''); } - }); - } else { + } + + const isDimmed = Boolean(focusedPlugId); + const modeStr = connectPlugs + ? showSampleLabels + ? 'lines+markers+text' + : 'lines+markers' + : showSampleLabels + ? 'markers+text' + : 'markers'; + traces.push({ type: 'scatter', - mode: 'markers', - name: `SCAL RT ${rf} (${g.pc.length})`, - x: g.bvw, - y: g.pc, - customdata: g.ptIds, - text: g.labels, - hovertemplate: - '%{text}
BVW=%{x:.3f}, Pc=%{y:.2f} psi
Click to exclude pointRT ' + - rf + - '', + mode: modeStr, + name: `SCAL RT ${rf} (${ptCount})`, + x: xVals, + y: yVals, + customdata: cdataVals, + text: txtVals, + textposition: 'top center', + textfont: { family: 'JetBrains Mono', size: 8.5, color }, + hovertext: hoverVals, + hovertemplate: `%{hovertext}RT ${rf}`, + connectgaps: false, + line: connectPlugs + ? { + color, + width: 1.1, + dash: 'dot' + } + : undefined, marker: { size: 6.5, color, - opacity: 0.85, + opacity: isDimmed ? 0.25 : 0.88, line: { width: 0.5, color: '#fff' } - } + }, + opacity: isDimmed ? 0.28 : 1.0 }); } + + if (focusedPlugId && byPlug.has(focusedPlugId)) { + const fPts = byPlug.get(focusedPlugId)!; + if (fPts.length > 0) { + const p0 = fPts[0]; + const rtCol = getRockTypeColor(p0.rf); + traces.push({ + type: 'scatter', + mode: 'lines+markers+text', + name: `★ ${p0.sample} (${p0.well}) [RT ${p0.rf}]`, + x: fPts.map(getPointX), + y: fPts.map(getPointY), + customdata: fPts.map((p) => p.cdata), + text: fPts.map((p, idx) => `${p.sample}#${idx + 1}`), + textposition: 'top center', + textfont: { family: 'JetBrains Mono', size: 9.5, color: '#0F172A' }, + hovertext: fPts.map((p, idx) => formatPointHover(p, idx, fPts.length)), + hovertemplate: `%{hovertext}FOCUSED · ${p0.well} · ${p0.sample}`, + line: { color: rtCol, width: 2.6, dash: 'solid' }, + marker: { + size: 9.5, + symbol: 'diamond', + color: rtCol, + line: { width: 1.8, color: '#0F172A' } + } + }); + } + } } // Render Excluded SCAL Points trace (grey 'x' markers) @@ -3463,41 +3685,82 @@ return { traces, layout: baseLayout }; } + const filteredPlugs = sr.plugs.filter( + (p) => + projectData.isScalItemInActiveFilter(p.well, p.depth, p.zone) && + (projectData.scalFilterRockType === 'all' || + (p.rt ?? 1) === projectData.scalFilterRockType) + ); + const focusedPlugId = projectData.focusedScalPlugId + ? projectData.normalizeScalPlugId(projectData.focusedScalPlugId) + : null; + const SAMPLE_PALETTE_PLUGS = [ + '#2563EB', + '#16A34A', + '#D97706', + '#DC2626', + '#9333EA', + '#0891B2', + '#DB2777', + '#4F46E5', + '#059669', + '#EA580C', + '#7C3AED', + '#0284C7' + ]; + if (tab === 'pc-qc') { const seenRt = new Set(); - for (const c of sr.plugs) { - if (!c.pc_curve?.length || !c.sw_curve?.length) continue; + filteredPlugs.forEach((c, cIdx) => { + if (!c.pc_curve?.length || !c.sw_curve?.length) return; + const normPid = projectData.normalizeScalPlugId(c.plug_id, c.well, c.sample); + const pZone = projectData.resolveScalSampleZone(c.well, c.depth, c.zone); const rt = c.rt ?? 1; - const col = getRockTypeColor(rt); - const showLeg = !seenRt.has(rt); + const bySample = projectData.scalGroupMode === 'sample'; + const isFocused = focusedPlugId === normPid; + const isDimmed = Boolean(focusedPlugId && !isFocused); + const col = bySample + ? SAMPLE_PALETTE_PLUGS[cIdx % SAMPLE_PALETTE_PLUGS.length] + : getRockTypeColor(rt); + const showLeg = bySample ? true : !seenRt.has(rt); seenRt.add(rt); traces.push({ type: 'scatter', - mode: 'lines+markers', - name: `RT ${rt} Pc Curve`, - legendgroup: `rt-${rt}`, + mode: + projectData.scalShowSampleLabels || isFocused + ? 'lines+markers+text' + : 'lines+markers', + name: bySample ? `${c.sample} (${c.well}) [RT${rt}]` : `RT ${rt} Pc Curve`, + legendgroup: bySample ? normPid : `rt-${rt}`, showlegend: showLeg, x: c.sw_curve, y: c.pc_curve, + customdata: c.sw_curve.map((_, idx) => `scal_pt::${normPid}::${idx}`), + text: c.sw_curve.map((_, idx) => + idx === c.sw_curve.length - 1 ? `${c.sample}` : '' + ), + textposition: 'top left', + textfont: { family: 'JetBrains Mono', size: 8.5, color: col }, line: { color: col, - width: c.included ? 1.6 : 1.0, + width: isFocused ? 2.6 : c.included ? 1.6 : 1.0, dash: c.included ? 'solid' : 'dot' }, marker: { - size: 4, + size: isFocused ? 7 : 4.5, color: col, - opacity: c.included ? 0.85 : 0.35 + opacity: isDimmed ? 0.18 : c.included ? 0.88 : 0.35 }, - opacity: c.included ? 0.85 : 0.35, + opacity: isDimmed ? 0.2 : c.included ? 0.9 : 0.35, hovertemplate: - `${c.well} · ${c.sample} (RT ${rt})
` + + `${c.well} · Sample ${c.sample} (RT ${rt})
` + + `Depth: ${c.depth > 0 ? fmtNum(c.depth, 2) + ' m' : '—'} | Zone: ${pZone}
` + `CPORE: ${fmtNum(c.cpore, 3)} | CPERM: ${fmtNum(c.cperm, 2)} mD
` + `Sw: %{x:.3f} v/v | Pc: %{y:.1f} psi` }); - } + }); - const extractedPlugs = sr.plugs.filter( + const extractedPlugs = filteredPlugs.filter( (p) => p.extracted_swirr !== null && Number.isFinite(p.extracted_swirr) ); if (extractedPlugs.length > 0) { @@ -3509,9 +3772,11 @@ y: extractedPlugs.map((p) => cfg.extractionMode === 'interp_pc' ? cfg.referencePcPsi : p.max_pc ), - customdata: extractedPlugs.map((p) => p.plug_id), + customdata: extractedPlugs.map((p) => + projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample) + ), marker: { - size: 8, + size: 8.5, symbol: extractedPlugs.map((p) => (p.included ? 'diamond' : 'x')), color: extractedPlugs.map((p) => p.included ? getRockTypeColor(p.rt) : '#94A3B8' @@ -3520,7 +3785,8 @@ }, text: extractedPlugs.map( (p) => - `${p.well} · ${p.sample} (RT ${p.rt})
` + + `${p.well} · Sample ${p.sample} (RT ${p.rt})
` + + `Depth: ${p.depth > 0 ? fmtNum(p.depth, 2) + ' m' : '—'} | Zone: ${projectData.resolveScalSampleZone(p.well, p.depth, p.zone)}
` + `Extracted Swirr: ${fmtNum(p.extracted_swirr, 3)} v/v | Max Pc: ${fmtNum(p.max_pc, 1)} psi
` + `Click to ${p.included ? 'exclude' : 're-include'} plug` ), @@ -3557,11 +3823,11 @@ ? Math.max(1e-4, p.cperm) : Math.sqrt(Math.max(1e-6, p.cperm) / Math.max(1e-4, p.cpore)); - const includedByRt = new Map(); - const excludedPlugs: typeof sr.plugs = []; + const includedByRt = new Map(); + const excludedPlugs: typeof filteredPlugs = []; const allValidX: number[] = []; - for (const p of sr.plugs) { + for (const p of filteredPlugs) { const xv = getPlugX(p); if (Number.isFinite(xv) && xv > 0) allValidX.push(xv); if (p.included && p.extracted_swirr !== null && Number.isFinite(p.extracted_swirr)) { @@ -3576,31 +3842,92 @@ } } - for (const [rt, grp] of Array.from(includedByRt.entries()).sort((a, b) => a[0] - b[0])) { - const col = getRockTypeColor(rt); - traces.push({ - type: 'scatter', - mode: 'markers', - name: `RT ${rt} Plugs (n=${grp.length})`, - x: grp.map((p) => getPlugX(p)), - y: grp.map((p) => p.extracted_swirr), - customdata: grp.map((p) => p.plug_id), - marker: { - size: 7.5, - color: col, - symbol: 'circle', - opacity: 0.9, - line: { color: '#0F172A', width: 0.8 } - }, - text: grp.map( - (p) => - `${p.well} · Sample ${p.sample} (${p.depth !== null ? fmtNum(p.depth, 1) + 'm' : '—'}) · RT ${p.rt}
` + - `CPORE: ${fmtNum(p.cpore, 3)} v/v | CPERM: ${fmtNum(p.cperm, 2)} mD | √(k/φ): ${fmtNum(Math.sqrt(p.cperm / p.cpore), 2)}
` + - `Lab Swirr: ${fmtNum(p.extracted_swirr, 3)} | Modeled: ${fmtNum(p.modeled_swirr, 3)} | Residual: ${p.residual !== null ? (p.residual >= 0 ? '+' : '') + fmtNum(p.residual, 3) : '—'}
` + - `Click point to exclude from regression` - ), - hovertemplate: '%{text}' - }); + if (projectData.scalGroupMode === 'sample') { + filteredPlugs + .filter((p) => p.included && p.extracted_swirr !== null && Number.isFinite(p.extracted_swirr)) + .forEach((p, idx) => { + const normPid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const isFocused = focusedPlugId === normPid; + const isDimmed = Boolean(focusedPlugId && !isFocused); + const col = SAMPLE_PALETTE_PLUGS[idx % SAMPLE_PALETTE_PLUGS.length]; + const pZone = projectData.resolveScalSampleZone(p.well, p.depth, p.zone); + traces.push({ + type: 'scatter', + mode: + projectData.scalShowSampleLabels || isFocused + ? 'markers+text' + : 'markers', + name: `${p.sample} (${p.well}) [RT${p.rt}]`, + x: [getPlugX(p)], + y: [p.extracted_swirr], + customdata: [normPid], + text: [p.sample], + textposition: 'top center', + textfont: { family: 'JetBrains Mono', size: 8.5, color: col }, + marker: { + size: isFocused ? 10 : 8, + color: col, + symbol: isFocused ? 'diamond' : 'circle', + opacity: isDimmed ? 0.22 : 0.92, + line: { color: '#0F172A', width: isFocused ? 1.5 : 0.8 } + }, + hovertext: [ + `${p.well} · Sample ${p.sample} (${p.depth > 0 ? fmtNum(p.depth, 1) + 'm' : '—'} · ${pZone}) · RT ${p.rt}
` + + `CPORE: ${fmtNum(p.cpore, 3)} v/v | CPERM: ${fmtNum(p.cperm, 2)} mD | √(k/φ): ${fmtNum(Math.sqrt(p.cperm / p.cpore), 2)}
` + + `Lab Swirr: ${fmtNum(p.extracted_swirr, 3)} | Modeled: ${fmtNum(p.modeled_swirr, 3)} | Residual: ${p.residual !== null ? (p.residual >= 0 ? '+' : '') + fmtNum(p.residual, 3) : '—'}
` + + `Click point to exclude from regression` + ], + hovertemplate: '%{hovertext}' + }); + }); + } else { + for (const [rt, grp] of Array.from(includedByRt.entries()).sort((a, b) => a[0] - b[0])) { + const col = getRockTypeColor(rt); + traces.push({ + type: 'scatter', + mode: projectData.scalShowSampleLabels ? 'markers+text' : 'markers', + name: `RT ${rt} Plugs (n=${grp.length})`, + x: grp.map((p) => getPlugX(p)), + y: grp.map((p) => p.extracted_swirr), + customdata: grp.map((p) => + projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample) + ), + text: grp.map((p) => p.sample), + textposition: 'top center', + textfont: { family: 'JetBrains Mono', size: 8.5, color: col }, + marker: { + size: grp.map((p) => + focusedPlugId === + projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample) + ? 10.5 + : 7.5 + ), + color: col, + symbol: grp.map((p) => + focusedPlugId === + projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample) + ? 'diamond' + : 'circle' + ), + opacity: grp.map((p) => + focusedPlugId && + focusedPlugId !== + projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample) + ? 0.25 + : 0.9 + ), + line: { color: '#0F172A', width: 0.8 } + }, + hovertext: grp.map( + (p) => + `${p.well} · Sample ${p.sample} (${p.depth > 0 ? fmtNum(p.depth, 1) + 'm' : '—'} · ${projectData.resolveScalSampleZone(p.well, p.depth, p.zone)}) · RT ${p.rt}
` + + `CPORE: ${fmtNum(p.cpore, 3)} v/v | CPERM: ${fmtNum(p.cperm, 2)} mD | √(k/φ): ${fmtNum(Math.sqrt(p.cperm / p.cpore), 2)}
` + + `Lab Swirr: ${fmtNum(p.extracted_swirr, 3)} | Modeled: ${fmtNum(p.modeled_swirr, 3)} | Residual: ${p.residual !== null ? (p.residual >= 0 ? '+' : '') + fmtNum(p.residual, 3) : '—'}
` + + `Click point to exclude from regression` + ), + hovertemplate: '%{hovertext}' + }); + } } if (excludedPlugs.length > 0) { @@ -3954,9 +4281,18 @@ void workbench.swirrCrossplotTab; void swirrLogY; void shfLogScale; + void projectData.scalGroupMode; + void projectData.scalConnectPlugs; + void projectData.scalShowSampleLabels; + void projectData.focusedScalPlugId; + void projectData.scalFilterRockType; + void projectData.shfParams.swirrSource; + void projectData.shfParams.jSatMode; } else { void wellData.curves; void projectData.selectedWellNames; + void workbench.selectedZones; + void workbench.depthFilter; void projectData.drtMethod; void projectData.fziCutoffs; void projectData.winlandCutoffs; @@ -3972,6 +4308,13 @@ void projectData.shfResult; void projectData.swirrConfig.excludedScalPointIds; void projectData.selectedScalPointIds; + void projectData.scalGroupMode; + void projectData.scalConnectPlugs; + void projectData.scalShowSampleLabels; + void projectData.focusedScalPlugId; + void projectData.scalFilterRockType; + void projectData.shfParams.swirrSource; + void projectData.shfParams.jSatMode; void projectData.shfParams.fwl; void projectData.shfParams.fwlByZone; void projectData.shfParams.model; @@ -4059,10 +4402,21 @@ ) { const rawPtId = pt.customdata.slice('scal_pt::'.length); const parts = rawPtId.split('::'); - const plugId = parts.length >= 2 ? `${parts[0]}::${parts[1]}` : ''; + const plugId = + parts.length >= 3 + ? `${parts[0]}::${parts[1]}` + : parts.length === 2 + ? projectData.normalizeScalPlugId(parts[0]) + : ''; + if (plugId && Boolean(ev?.event?.altKey)) { + projectData.toggleFocusedScalPlug(plugId); + return; + } const isPlugExcluded = plugId && - (projectData.swirrConfig.excludedPlugIds || []).includes(plugId); + (projectData.swirrConfig.excludedPlugIds || []).some( + (id) => projectData.normalizeScalPlugId(id) === plugId + ); if ( plugId && (projectData.scalClickFilterTarget === 'plug' || @@ -4151,6 +4505,81 @@ }; }); + let isScalCrossplotActive = $derived( + (effectiveMode === 'swirr-fit' && workbench.swirrCrossplotTab !== 'relperm') || + (effectiveMode === 'shf' && shfView === 'scal_fit') + ); + + let scalQcSummary = $derived.by(() => { + const plugs = projectData.swirrResult?.plugs ?? []; + const scal = projectData.shfResult?.scal_data; + const sampleMap = new Map< + string, + { plugId: string; well: string; sample: string; rt: number; depth: number; zone: string; pts: number } + >(); + const rts = new Set(); + + if (plugs.length > 0) { + for (const p of plugs) { + const zn = projectData.resolveScalSampleZone(p.well, p.depth, p.zone); + if (!projectData.isScalItemInActiveFilter(p.well, p.depth, zn)) continue; + const rt = p.rt ?? 1; + rts.add(rt); + if (projectData.scalFilterRockType !== 'all' && rt !== projectData.scalFilterRockType) + continue; + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + sampleMap.set(pid, { + plugId: pid, + well: p.well, + sample: p.sample, + rt, + depth: p.depth ?? 0, + zone: zn, + pts: p.pc_curve?.length ?? 0 + }); + } + } else if (scal && Array.isArray(scal.pc)) { + for (let i = 0; i < scal.pc.length; i++) { + const wn = scal.well_names?.[i] ?? 'Well'; + const sname = scal.samples?.[i] ?? 'Sample'; + const d = scal.depths?.[i] ?? 0; + const zn = projectData.resolveScalSampleZone(wn, d, scal.zones?.[i]); + if (!projectData.isScalItemInActiveFilter(wn, d, zn)) continue; + const rt = scal.rock_flags?.[i] ?? 1; + rts.add(rt); + if (projectData.scalFilterRockType !== 'all' && rt !== projectData.scalFilterRockType) + continue; + const pid = projectData.normalizeScalPlugId(scal.plug_ids?.[i], wn, sname); + const prev = sampleMap.get(pid); + if (prev) { + prev.pts += 1; + } else { + sampleMap.set(pid, { + plugId: pid, + well: wn, + sample: sname, + rt, + depth: d, + zone: zn, + pts: 1 + }); + } + } + } + + const samples = Array.from(sampleMap.values()).sort((a, b) => { + if (a.rt !== b.rt) return a.rt - b.rt; + if (a.well !== b.well) return a.well.localeCompare(b.well); + return a.sample.localeCompare(b.sample, undefined, { numeric: true }); + }); + const totalPts = samples.reduce((acc, s) => acc + s.pts, 0); + return { + samples, + rts: Array.from(rts).sort((a, b) => a - b), + totalPts + }; + }); + onDestroy(() => { if (PlotlyLib && plotEl) { PlotlyLib.purge(plotEl); @@ -4444,6 +4873,132 @@ {/if} + {#if isScalCrossplotActive && scalQcSummary.samples.length > 0} +
+
+ Sample QC: +
+ {#each [['sample', 'By Sample'], ['rt', 'By RT']] as [gKey, gLbl]} + + {/each} +
+ + + + + + {#if ((effectiveMode === 'swirr-fit' && workbench.swirrCrossplotTab === 'scal-fit') || (effectiveMode === 'shf' && shfView === 'scal_fit')) && (projectData.shfParams.jSatMode ?? 'swn') === 'swn'} +
+ {#each [['lab_min', 'Norm: Plug Swirr'], ['poro_perm', 'Norm: Poro-Perm']] as [nKey, nLbl]} + + {/each} +
+ {/if} + + + + + + {#if projectData.focusedScalPlugId} + + {/if} +
+ +
+ + {scalQcSummary.samples.length} samples · + {scalQcSummary.totalPts} pts + + {#if workbench.selectedZones.length > 0} + + Zones: {workbench.selectedZones.length} + + {/if} +
+
+ {/if} + {#if effectiveMode === 'core-qc'} {@const cc = wellData.coreComparison}
{ for (const mnem of POTA_ALIASES) { @@ -86,6 +86,15 @@ return true; } } + for (const key of Object.keys(wellData.curves)) { + const clean = key.toUpperCase().replace(/\.+$/, '').trim(); + if (clean === 'POTA' || clean === 'HFKO' || clean === 'POT' || clean === 'K') { + const arr = wellData.curves[key]; + if (arr && arr.some((v) => typeof v === 'number' && Number.isFinite(v) && v > 0)) { + return true; + } + } + } return false; }); @@ -264,7 +273,16 @@ arr: (number | null)[]; }> { const pick = (mnemonic: string): (number | null)[] => { - const arr = wellData.curves[mnemonic]; + let arr = wellData.curves[mnemonic]; + if (!arr) { + const clean = mnemonic.toUpperCase().replace(/\.+$/, '').trim(); + for (const key of Object.keys(wellData.curves)) { + if (key.toUpperCase().replace(/\.+$/, '').trim() === clean) { + arr = wellData.curves[key]; + break; + } + } + } if (!arr) return []; return indices.map((i) => (Number.isFinite(arr[i]) ? arr[i] : null)); }; @@ -949,7 +967,11 @@ } } if (potaData.length > 0) { - const potaDec = decimateSvg(potaData); + // If POTA is stored as decimal fraction (e.g. 0.02 for 2%), normalize to percentage + const maxPota = potaData.reduce((m, v) => (v !== null && v > m ? v : m), 0); + const potaScaleToPct = maxPota > 0 && maxPota <= 0.5 ? 100.0 : 1.0; + const potaPct = potaData.map((v) => (v !== null ? v * potaScaleToPct : null)); + const potaDec = decimateSvg(potaPct); traces.push({ type: 'scatter', mode: 'lines', @@ -1363,7 +1385,7 @@ hovertemplate: `${activeDtcMnem}: %{customdata:.1f} µs/ft` }); } - if (showHcCorrected && workbench.lithoParams.enableHcCorrection) { + if (workbench.lithoParams.enableHcCorrection) { const nphiHc = pick('NPHI_HC'); const rhobHc = pick('RHOB_HC'); if (nphiHc.some((v) => v !== null)) { @@ -2072,22 +2094,24 @@ line: { color: 'rgba(37, 99, 235, 0.8)', width: 1, dash: 'dash' }, layer: 'above' }); - annotations.push({ - xref: 'paper', - yref: 'y', - x: 0.003, - y: topD, - xanchor: 'left', - yanchor: 'bottom', - text: `▲ ${sortedTops[i].name} (${topD.toFixed(1)}m)`, - showarrow: false, - captureevents: false, - font: { family: 'JetBrains Mono, monospace', size: 9, color: '#1E40AF' }, - bgcolor: 'rgba(239, 246, 255, 0.94)', - bordercolor: '#BFDBFE', - borderwidth: 1, - borderpad: 1.5 - }); + if (showTopLabels) { + annotations.push({ + xref: 'paper', + yref: 'y', + x: 0.003, + y: topD, + xanchor: 'left', + yanchor: 'bottom', + text: `▲ ${sortedTops[i].name} (${topD.toFixed(1)}m)`, + showarrow: false, + captureevents: false, + font: { family: 'JetBrains Mono, monospace', size: 9, color: '#1E40AF' }, + bgcolor: 'rgba(239, 246, 255, 0.94)', + bordercolor: '#BFDBFE', + borderwidth: 1, + borderpad: 1.5 + }); + } } // Perforation intervals: translucent band across all tracks + hoverable perf-gun strip on Track 1 left edge @@ -2263,11 +2287,17 @@ 'GR', 'CGR', 'POTA', + 'POTA.', 'HFKO', + 'HFKO.', 'THOR', + 'THOR.', 'HTHO', + 'HTHO.', 'URAN', + 'URAN.', 'HURA', + 'HURA.', 'CALI', 'BS', 'BADHOLE', @@ -2371,9 +2401,9 @@ visIndices, tops, perforations, - showHcCorrected, showDtc, showSpectralGr, + showTopLabels, tsOverlayActive, workbench.lithoParams.enableHcCorrection, workbench.lithoParams.model, @@ -2799,22 +2829,7 @@ [{fmtDepth(Math.min(yRange[0], yRange[1]))} – {fmtDepth(Math.max(yRange[0], yRange[1]))}] - {#if activeLithoSpecs.length > 0} - - {/if} +
@@ -2850,14 +2865,16 @@ SGR {/if} - + {#if wellData.tops.length > 0} + + {/if}
([]); scalClickFilterTarget = $state<'point' | 'plug'>('point'); + scalGroupMode = $state<'rt' | 'sample'>('sample'); + scalConnectPlugs = $state(true); + scalShowSampleLabels = $state(false); + focusedScalPlugId = $state(null); + scalFilterRockType = $state('all'); swirrInputCols = $state<{ phieCol: string; permCol: string; rtCol: string }>({ phieCol: 'PHIT', @@ -598,6 +604,7 @@ class ProjectDataState { pc: number[]; sw: number[]; swn: number[]; + swirr_calc?: number[]; j: number[]; h: number[]; bvw: number[]; @@ -606,6 +613,8 @@ class ProjectDataState { rock_flags: number[]; well_names: string[]; samples: string[]; + depths?: number[]; + zones?: string[]; point_ids?: string[]; plug_ids?: string[]; excluded?: boolean[]; @@ -1509,6 +1518,103 @@ class ProjectDataState { return Math.min(sMax, Math.max(sMin, Number.isFinite(raw) ? raw : sMin)); } + normalizeScalPlugId(id: string | undefined | null, well?: string, sample?: string): string { + const raw = String(id || '').trim(); + if (raw.includes('::')) return raw; + if (raw.includes(':')) return raw.replace(':', '::'); + if (well && sample) return `${well}::${sample}`; + return raw; + } + + resolveScalSampleZone( + wellName: string | undefined | null, + depth: number | undefined | null, + rawZone?: string | null + ): string { + const rz = String(rawZone || '').trim(); + if (rz && rz !== 'Unknown' && rz !== 'None' && rz !== 'nan') { + return rz; + } + const wn = String(wellName || '').trim(); + const d = Number(depth); + if (!wn || !Number.isFinite(d) || d <= 0) return rz || 'Unknown'; + const wMeta = this.wells.find((w) => w.name === wn) as any; + const stickWell = this.stickPlotResult?.wells?.find((w) => w.well_name === wn); + const tops = + wMeta?.ancillary_data?.formation_tops ?? + stickWell?.tops ?? + (wn === workbench.wellId ? wellData.tops : null); + if (Array.isArray(tops) && tops.length > 0) { + const sorted = [...tops] + .filter((t) => Number.isFinite(Number(t?.depth)) && (t?.name || (t as any)?.zone)) + .sort((a, b) => Number(a.depth) - Number(b.depth)); + let chosen = 'Unknown'; + for (const t of sorted) { + if (d >= Number(t.depth)) { + chosen = String(t.name || (t as any).zone).trim(); + } else { + break; + } + } + if (chosen !== 'Unknown') return chosen; + } + const shf = this.shfResult; + if (shf?.well_names?.length && shf?.depths?.length && shf?.zones?.length) { + let bestDist = 5.0; + let bestZone = ''; + for (let i = 0; i < shf.well_names.length; i++) { + if (shf.well_names[i] !== wn) continue; + const dist = Math.abs((shf.depths[i] ?? 0) - d); + if (dist <= bestDist && shf.zones[i] && shf.zones[i] !== 'Unknown') { + bestDist = dist; + bestZone = shf.zones[i]; + } + } + if (bestZone) return bestZone; + } + return rz || 'Unknown'; + } + + isScalItemInActiveFilter( + wellName: string | undefined | null, + depth?: number | null, + rawZone?: string | null + ): boolean { + const wn = String(wellName || '').trim(); + if (wn) { + if (workbench.scope === 'single-well' && workbench.wellId) { + if (wn !== workbench.wellId) return false; + } else if ( + this.selectedWellNames.length > 0 && + !this.selectedWellNames.includes(wn) + ) { + return false; + } + } + const d = typeof depth === 'number' && Number.isFinite(depth) ? depth : null; + if (workbench.depthFilter && d !== null && d > 0) { + if (d < workbench.depthFilter.min || d > workbench.depthFilter.max) return false; + } + if (workbench.selectedZones.length > 0) { + const zn = this.resolveScalSampleZone(wn, d, rawZone); + if (zn && zn !== 'Unknown') { + if (!workbench.selectedZones.includes(zn)) return false; + } else if (d !== null && d > 0) { + return false; + } + } + return true; + } + + toggleFocusedScalPlug(plugId: string | null) { + if (!plugId) { + this.focusedScalPlugId = null; + return; + } + const norm = this.normalizeScalPlugId(plugId); + this.focusedScalPlugId = this.focusedScalPlugId === norm ? null : norm; + } + recomputeSwirrClientSide(autoFit: boolean = true) { const sr = this.swirrResult; if (!sr || !Array.isArray(sr.plugs) || sr.plugs.length === 0) return; @@ -1516,10 +1622,14 @@ class ProjectDataState { const refPc = Math.max(0.1, Number(cfg.referencePcPsi) || 50.0); const sMin = Number(cfg.swirrMin) || 0.03; const sMax = Math.max(sMin + 0.05, Number(cfg.swirrMax) || 0.85); - const exclSet = new Set(cfg.excludedPlugIds || []); + const exclSet = new Set( + (cfg.excludedPlugIds || []).map((id) => this.normalizeScalPlugId(id)) + ); - // 1. Re-extract Swirr per plug at referencePcPsi + // 1. Re-extract Swirr per plug at referencePcPsi and respect active Well/Zone/Depth filters const updatedPlugs: SwirrPlugRow[] = sr.plugs.map((p) => { + const normPid = this.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const resolvedZone = this.resolveScalSampleZone(p.well, p.depth, p.zone); const pcArr = p.pc_curve || []; const swArr = p.sw_curve || []; const maxPc = pcArr.length > 0 ? Math.max(...pcArr) : p.max_pc; @@ -1547,24 +1657,30 @@ class ProjectDataState { } } } - const isExcl = exclSet.has(p.plug_id); + const isExcl = exclSet.has(normPid) || exclSet.has(p.plug_id); + const inFilter = this.isScalItemInActiveFilter(p.well, p.depth, resolvedZone); return { ...p, + plug_id: normPid, + zone: resolvedZone, max_pc: Number(maxPc.toFixed(2)), sw_at_max_pc: Number(minSw.toFixed(4)), extracted_swirr: Number(extSwirr.toFixed(4)), valid_pc: validPc, excluded: isExcl, - included: validPc && !isExcl + included: validPc && !isExcl && inFilter }; }); - let inclPlugs = updatedPlugs.filter((p) => p.included); + const filterMatchedPlugs = updatedPlugs.filter((p) => + this.isScalItemInActiveFilter(p.well, p.depth, p.zone) + ); + let inclPlugs = filterMatchedPlugs.filter((p) => p.included); if (inclPlugs.length === 0) { - inclPlugs = updatedPlugs.filter((p) => !p.excluded); + inclPlugs = filterMatchedPlugs.filter((p) => !p.excluded); } if (inclPlugs.length === 0) { - inclPlugs = updatedPlugs; + inclPlugs = filterMatchedPlugs.length > 0 ? filterMatchedPlugs : updatedPlugs; } const fitSubset = (subset: SwirrPlugRow[], scopeKey: string): SwirrCorrelationRule => { @@ -1726,7 +1842,7 @@ class ProjectDataState { const sp = this.shfParams; const activeModel = sp.model ?? 'leverett_j'; const jSatMode = sp.jSatMode ?? 'swn'; - const swirrSource = sp.swirrSource ?? 'poro_perm'; + const swirrSource = sp.swirrSource ?? 'lab_min'; const iftCos = Math.max( 1e-4, (sp.ift || 30.0) * Math.cos(((sp.theta ?? 30.0) * Math.PI) / 180.0) @@ -1737,6 +1853,7 @@ class ProjectDataState { const pcArr: number[] = []; const swArr: number[] = []; const swnArr: number[] = []; + const swirrCalcArr: number[] = []; const jArr: number[] = []; const hArr: number[] = []; const bvwArr: number[] = []; @@ -1745,6 +1862,8 @@ class ProjectDataState { const rfArr: number[] = []; const wellArr: string[] = []; const sampleArr: string[] = []; + const depthArr: number[] = []; + const zoneArr: string[] = []; const pointIdArr: string[] = []; const plugIdArr: string[] = []; const excludedArr: boolean[] = []; @@ -1753,27 +1872,30 @@ class ProjectDataState { (this.swirrConfig.excludedScalPointIds || []).map((id) => String(id).trim()) ); const excludedPlugsSet = new Set( - (this.swirrConfig.excludedPlugIds || []).map((id) => String(id).trim()) + (this.swirrConfig.excludedPlugIds || []).map((id) => this.normalizeScalPlugId(id)) ); for (const p of sr.plugs) { + const pZone = this.resolveScalSampleZone(p.well, p.depth, p.zone); + if (!this.isScalItemInActiveFilter(p.well, p.depth, pZone)) continue; const pCurve = p.pc_curve || []; const sCurve = p.sw_curve || []; const phi = Math.max(1e-4, p.cpore || 0.15); const k = Math.max(1e-4, p.cperm || 10.0); const rf = p.rt ?? 1; - const plugId = p.plug_id || `${p.well}::${p.sample}`; - const isPlugExcluded = Boolean(p.excluded) || excludedPlugsSet.has(plugId); + const plugId = this.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const legacyPlugId = `${p.well}:${p.sample}`; + const isPlugExcluded = + Boolean(p.excluded) || + excludedPlugsSet.has(plugId) || + excludedPlugsSet.has(legacyPlugId); const swMinPlug = sCurve.length > 0 ? Math.min(...sCurve) : (p.sw_at_max_pc ?? 0.12); const swirrNorm = swirrSource === 'poro_perm' - ? this.evalSwirrValue(phi, k, rf) - : Math.min( - swMinPlug * 0.92, - Math.min(0.22, Math.max(0.04, 0.08 * Math.pow(k / phi, -0.12))) - ); + ? Math.min(0.85, Math.max(0.01, this.evalSwirrValue(phi, k, rf))) + : Math.min(0.85, Math.max(0.01, swMinPlug * 0.96)); for (let i = 0; i < Math.min(pCurve.length, sCurve.length); i++) { const pc = Number(pCurve[i]); @@ -1787,13 +1909,18 @@ class ProjectDataState { const h = pc / gradPsi; const bvw = phi * sw; const ptId = `${plugId}::${i}`; + const legacyPtId = `${legacyPlugId}::${i}`; const ptIdByPc = `${plugId}::${Number(pc.toFixed(2))}`; const isPtExcluded = - isPlugExcluded || excludedPtsSet.has(ptId) || excludedPtsSet.has(ptIdByPc); + isPlugExcluded || + excludedPtsSet.has(ptId) || + excludedPtsSet.has(legacyPtId) || + excludedPtsSet.has(ptIdByPc); pcArr.push(Number(pc.toFixed(3))); swArr.push(Number(sw.toFixed(4))); swnArr.push(Number(swn.toFixed(4))); + swirrCalcArr.push(Number(swirrNorm.toFixed(4))); jArr.push(Number(j.toFixed(4))); hArr.push(Number(h.toFixed(2))); bvwArr.push(Number(bvw.toFixed(4))); @@ -1802,6 +1929,8 @@ class ProjectDataState { rfArr.push(rf); wellArr.push(p.well); sampleArr.push(p.sample); + depthArr.push(Number((p.depth ?? 0).toFixed(2))); + zoneArr.push(pZone); pointIdArr.push(ptId); plugIdArr.push(plugId); excludedArr.push(isPtExcluded); @@ -1920,6 +2049,7 @@ class ProjectDataState { pc: pcArr, sw: swArr, swn: swnArr, + swirr_calc: swirrCalcArr, j: jArr, h: hArr, bvw: bvwArr, @@ -1928,6 +2058,8 @@ class ProjectDataState { rock_flags: rfArr, well_names: wellArr, samples: sampleArr, + depths: depthArr, + zones: zoneArr, point_ids: pointIdArr, plug_ids: plugIdArr, excluded: excludedArr @@ -1943,9 +2075,11 @@ class ProjectDataState { } toggleSwirrPlugExclusion(plugId: string) { - const id = String(plugId || '').trim(); + const id = this.normalizeScalPlugId(plugId); if (!id) return; - const cur = new Set(this.swirrConfig.excludedPlugIds || []); + const cur = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); if (cur.has(id)) { cur.delete(id); } else { @@ -1999,11 +2133,15 @@ class ProjectDataState { excludeSelectedScalPlugs() { if (this.selectedScalPointIds.length === 0) return; - const curPlugs = new Set(this.swirrConfig.excludedPlugIds || []); + const curPlugs = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); for (const ptId of this.selectedScalPointIds) { const parts = ptId.split('::'); - if (parts.length >= 2) { + if (parts.length >= 3) { curPlugs.add(`${parts[0]}::${parts[1]}`); + } else if (parts.length === 2) { + curPlugs.add(this.normalizeScalPlugId(parts[0])); } } this.swirrConfig.excludedPlugIds = Array.from(curPlugs); @@ -2013,11 +2151,15 @@ class ProjectDataState { includeSelectedScalPlugs() { if (this.selectedScalPointIds.length === 0) return; - const curPlugs = new Set(this.swirrConfig.excludedPlugIds || []); + const curPlugs = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); for (const ptId of this.selectedScalPointIds) { const parts = ptId.split('::'); - if (parts.length >= 2) { + if (parts.length >= 3) { curPlugs.delete(`${parts[0]}::${parts[1]}`); + } else if (parts.length === 2) { + curPlugs.delete(this.normalizeScalPlugId(parts[0])); } } this.swirrConfig.excludedPlugIds = Array.from(curPlugs); @@ -2134,6 +2276,10 @@ class ProjectDataState { setShfSwirrSource(source: 'lab_min' | 'poro_perm') { this.shfParams.swirrSource = source; + if (this.swirrResult?.plugs?.length) { + this.recomputeScalFitsFromPlugs(true); + return; + } const sr = this.shfResult; if (!sr || !sr.scal_data || !Array.isArray(sr.scal_data.sw)) return; const swArr = sr.scal_data.sw; @@ -2146,7 +2292,7 @@ class ProjectDataState { // Group by plug to compute lab_min per plug or poro_perm per point const plugMinSw = new Map(); for (let i = 0; i < swArr.length; i++) { - const pk = `${wellsArr[i] ?? 'W'}:${samplesArr[i] ?? i}`; + const pk = `${wellsArr[i] ?? 'W'}::${samplesArr[i] ?? i}`; const sv = swArr[i]; if (typeof sv === 'number' && Number.isFinite(sv)) { const prev = plugMinSw.get(pk); @@ -2155,25 +2301,24 @@ class ProjectDataState { } const nextSwn: number[] = []; + const nextSwirrCalc: number[] = []; for (let i = 0; i < swArr.length; i++) { const sw = swArr[i]; const phi = phitArr[i] ?? 0.18; const k = permArr[i] ?? 50.0; const rf = rfArr[i] ?? 1; if (source === 'poro_perm') { - const swirrCalc = this.evalSwirrValue(phi, k, rf); + const swirrCalc = Math.min(0.85, Math.max(0.01, this.evalSwirrValue(phi, k, rf))); const swn = Math.min(0.99, Math.max(0.005, (sw - swirrCalc) / Math.max(0.05, 1.0 - swirrCalc))); nextSwn.push(Number(swn.toFixed(4))); + nextSwirrCalc.push(Number(swirrCalc.toFixed(4))); } else { - const pk = `${wellsArr[i] ?? 'W'}:${samplesArr[i] ?? i}`; + const pk = `${wellsArr[i] ?? 'W'}::${samplesArr[i] ?? i}`; const swMinPlug = plugMinSw.get(pk) ?? 0.1; - const swirrEst = Math.min( - 0.22, - Math.max(0.04, 0.08 * Math.pow(Math.max(k, 0.01) / Math.max(phi, 0.01), -0.12)) - ); - const effSwirr = Math.min(swMinPlug * 0.92, swirrEst); - const swn = Math.min(0.98, Math.max(0.02, (sw - effSwirr) / Math.max(1.0 - effSwirr, 1e-3))); + const effSwirr = Math.min(0.85, Math.max(0.01, swMinPlug * 0.96)); + const swn = Math.min(0.99, Math.max(0.005, (sw - effSwirr) / Math.max(1.0 - effSwirr, 0.05))); nextSwn.push(Number(swn.toFixed(4))); + nextSwirrCalc.push(Number(effSwirr.toFixed(4))); } } @@ -2182,7 +2327,8 @@ class ProjectDataState { fits_swn: undefined, scal_data: { ...sr.scal_data, - swn: nextSwn + swn: nextSwn, + swirr_calc: nextSwirrCalc } }; // Force refit of Swn J-curves diff --git a/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts b/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts index 45d36439..a0e8bc6d 100644 --- a/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts @@ -1140,6 +1140,18 @@ class WellDataState { committedCopy['DEPT'] = [...numericCurves['DEPTH']]; } + // If a curve ends with '.' (common in LAS files with blank units, e.g. POTA.), also map the clean name + for (const col of Object.keys(numericCurves)) { + if (col.endsWith('.')) { + const clean = col.replace(/\.+$/, '').trim(); + if (clean && !numericCurves[clean]) { + numericCurves[clean] = numericCurves[col]; + origCopy[clean] = origCopy[col]; + committedCopy[clean] = committedCopy[col]; + } + } + } + // Detect carbonate / multi-mineral curves on well load and clean up legacy duplicate VSAND==VCALC / VSILT==VDOLO const hasPositiveCurve = (col: string) => Array.isArray(numericCurves[col]) && @@ -3873,12 +3885,21 @@ class WellDataState { 'PAY', 'BADHOLE' ]) { - if (this.curves[k]) { - const v = this.curves[k][lo]; + const arr = this.curves[k] ?? this.curves[k + "."]; + if (arr) { + const v = arr[lo]; out[k] = Number.isFinite(v) ? v : null; } } + // Normalize POTA to percent (%) if stored as decimal fraction (max <= 0.5 v/v) + if (out['POTA'] !== null && out['POTA'] !== undefined) { + const potaArr = this.curves['POTA'] ?? this.curves['POTA.']; + if (potaArr && !potaArr.some((v) => Number.isFinite(v) && v > 0.5)) { + out['POTA'] = out['POTA'] * 100.0; + } + } + // Fallback to discrete core plug samples when nearest continuous log curve cell is empty if (this.coreSamples.length > 0 && depth !== null) { let bestSample: any = null; diff --git a/quick_pp/app/frontend/src/routes/+page.svelte b/quick_pp/app/frontend/src/routes/+page.svelte index 01803992..336146b6 100644 --- a/quick_pp/app/frontend/src/routes/+page.svelte +++ b/quick_pp/app/frontend/src/routes/+page.svelte @@ -79,6 +79,25 @@ untrack(() => projectData.applyProjectParamsForCurrentSelection()); }); + // Keep SCAL Swirr & Leverett-J / Pc curve fits synchronized with active Well, Zone, and Depth filters + let lastScalFilterKey = ''; + $effect(() => { + const wellsKey = Array.from(projectData.selectedWellNames).sort().join(','); + const zonesKey = zoneSelectionKey(workbench.selectedZones); + const depthKey = workbench.depthFilter + ? `${workbench.depthFilter.min}:${workbench.depthFilter.max}` + : 'all'; + const scopeKey = `${workbench.scope}:${workbench.wellId ?? ''}`; + const hasSwirr = Boolean(projectData.swirrResult?.plugs?.length); + if (!hasSwirr) return; + const nextKey = `${scopeKey}|${wellsKey}|${zonesKey}|${depthKey}`; + if (nextKey === lastScalFilterKey) return; + lastScalFilterKey = nextKey; + untrack(() => { + projectData.recomputeSwirrClientSide(!projectData.swirrManualRuleOverride); + }); + }); + let initialized = $state(false); let isReportMode = $derived( (workbench.scope === 'single-well' && workbench.singleWellStep === 'report') || @@ -87,10 +106,14 @@ let isFptMode = $derived(workbench.scope === 'multi-well' && workbench.multiWellStep === 'fpt'); let isVolumetricsMode = $derived(workbench.scope === 'multi-well' && workbench.multiWellStep === 'volumetrics'); - /** FPT grid: clicking a row selects the point on the Pressure vs TVDSS plots. */ + /** FPT grid: clicking a row selects the point on the Pressure vs TVDSS plots. SCAL grids: clicking a row spotlights that sample plug. */ function handleRightGridRowClick(row: Record) { if (isFptMode && typeof row?._id === 'number') { fpt.selectedIds = [row._id]; + return; + } + if (typeof row?._scalPlugId === 'string' && row._scalPlugId) { + projectData.toggleFocusedScalPlug(row._scalPlugId); } } @@ -376,32 +399,78 @@ projectData.shfResult?.scal_data?.pc?.length ) { const scal = projectData.shfResult.scal_data; + const plugMeta = new Map(); + for (const p of projectData.swirrResult?.plugs ?? []) { + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + plugMeta.set(pid, { + depth: p.depth ?? 0, + zone: projectData.resolveScalSampleZone(p.well, p.depth, p.zone) + }); + } + const focusedPid = projectData.focusedScalPlugId + ? projectData.normalizeScalPlugId(projectData.focusedScalPlugId) + : null; + const rtFilter = projectData.scalFilterRockType; + const rows = scal.pc + .map((pcVal, i) => { + const wn = scal.well_names?.[i] ?? ''; + const sname = scal.samples?.[i] ?? ''; + const pid = projectData.normalizeScalPlugId(scal.plug_ids?.[i], wn, sname); + const meta = plugMeta.get(pid); + const d = scal.depths?.[i] ?? meta?.depth ?? null; + const zn = projectData.resolveScalSampleZone( + wn, + d, + scal.zones?.[i] ?? meta?.zone + ); + const rf = scal.rock_flags?.[i] ?? null; + const isExcl = Boolean(scal.excluded?.[i]); + const isFocused = focusedPid === pid; + return { + _scalPlugId: pid, + Well: wn, + Sample: isFocused ? `★ ${sname}` : sname, + ZONE: zn, + DEPTH: d, + ROCK_FLAG: rf, + PC: pcVal, + SW: scal.sw?.[i] ?? null, + SWIRR_CALC: scal.swirr_calc?.[i] ?? null, + SWN: scal.swn?.[i] ?? null, + J: scal.j?.[i] ?? null, + H: scal.h?.[i] ?? null, + PHIT: scal.phit?.[i] ?? null, + PERM: scal.perm?.[i] ?? null, + STATUS: isExcl ? 'Excluded' : isFocused ? 'Focused' : 'Active' + }; + }) + .filter((r) => { + if (!projectData.isScalItemInActiveFilter(r.Well, r.DEPTH, r.ZONE)) return false; + if (rtFilter !== 'all' && r.ROCK_FLAG !== rtFilter) return false; + if (focusedPid && r._scalPlugId !== focusedPid) return false; + return true; + }); return { - title: 'Multi-Well SCAL Capillary Pressure & Leverett-J Table', + title: focusedPid + ? `SCAL Capillary Pressure & Leverett-J — Sample ${focusedPid.replace('::', ' · ')} (click row to clear focus)` + : 'Multi-Well SCAL Capillary Pressure & Leverett-J Table (click row to spotlight sample)', columns: [ { key: 'Well', label: 'Well' }, { key: 'Sample', label: 'Sample' }, + { key: 'ZONE', label: 'Zone' }, + { key: 'DEPTH', label: 'Depth', unit: 'm', numeric: true, decimals: 1 }, { key: 'ROCK_FLAG', label: 'RT', numeric: true, decimals: 0 }, { key: 'PC', label: 'Pc', unit: 'psi', numeric: true, decimals: 2 }, { key: 'SW', label: 'Sw', unit: 'v/v', numeric: true, decimals: 3 }, + { key: 'SWIRR_CALC', label: 'Swirr', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'SWN', label: 'Swn', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'J', label: 'J', numeric: true, decimals: 3 }, { key: 'H', label: 'Height', unit: 'm', numeric: true, decimals: 1 }, { key: 'PHIT', label: 'CPORE', unit: 'v/v', numeric: true, decimals: 3 }, - { key: 'PERM', label: 'CPERM', unit: 'mD', numeric: true, decimals: 2 } + { key: 'PERM', label: 'CPERM', unit: 'mD', numeric: true, decimals: 2 }, + { key: 'STATUS', label: 'Status' } ], - rows: scal.pc.map((pcVal, i) => ({ - Well: scal.well_names?.[i] ?? '', - Sample: scal.samples?.[i] ?? '', - ROCK_FLAG: scal.rock_flags?.[i] ?? null, - PC: pcVal, - SW: scal.sw?.[i] ?? null, - SWN: scal.swn?.[i] ?? null, - J: scal.j?.[i] ?? null, - H: scal.h?.[i] ?? null, - PHIT: scal.phit?.[i] ?? null, - PERM: scal.perm?.[i] ?? null - })) + rows }; } @@ -410,12 +479,22 @@ workbench.multiWellStep === 'swirr-corr' && projectData.swirrResult?.plugs?.length ) { - const plugs = projectData.swirrResult.plugs; + const focusedPid = projectData.focusedScalPlugId + ? projectData.normalizeScalPlugId(projectData.focusedScalPlugId) + : null; + const rtFilter = projectData.scalFilterRockType; + const plugs = projectData.swirrResult.plugs.filter((p) => { + const zn = projectData.resolveScalSampleZone(p.well, p.depth, p.zone); + if (!projectData.isScalItemInActiveFilter(p.well, p.depth, zn)) return false; + if (rtFilter !== 'all' && (p.rt ?? 1) !== rtFilter) return false; + return true; + }); return { - title: `Multi-Well SCAL Plug Swirr Summary (Ref Pc=${projectData.swirrConfig.referencePcPsi} psi | n=${plugs.filter((p) => p.included).length}/${plugs.length} included)`, + title: `Multi-Well SCAL Plug Swirr Summary (Ref Pc=${projectData.swirrConfig.referencePcPsi} psi | n=${plugs.filter((p) => p.included).length}/${plugs.length} in filter — click row to spotlight)`, columns: [ { key: 'WELL', label: 'Well' }, { key: 'SAMPLE', label: 'Sample' }, + { key: 'ZONE', label: 'Zone' }, { key: 'DEPTH', label: 'Depth', unit: 'm', numeric: true, decimals: 1 }, { key: 'RT', label: 'RT', numeric: true, decimals: 0 }, { key: 'CPORE', label: 'CPORE', unit: 'v/v', numeric: true, decimals: 3 }, @@ -428,21 +507,33 @@ { key: 'RESIDUAL', label: 'Residual', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'STATUS', label: 'Status' } ], - rows: plugs.map((p) => ({ - WELL: p.well, - SAMPLE: p.sample, - DEPTH: p.depth, - RT: p.rt, - CPORE: p.cpore, - CPERM: p.cperm, - RQI: p.rqi, - MAX_PC: p.max_pc, - SW_AT_MAX_PC: p.sw_at_max_pc, - EXTRACTED_SWIRR: p.extracted_swirr, - MODELED_SWIRR: p.modeled_swirr, - RESIDUAL: p.residual, - STATUS: p.included ? 'Included' : !p.valid_pc ? 'Low Pc (< Ref)' : 'Excluded' - })) + rows: plugs.map((p) => { + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const isFocused = focusedPid === pid; + return { + _scalPlugId: pid, + WELL: p.well, + SAMPLE: isFocused ? `★ ${p.sample}` : p.sample, + ZONE: projectData.resolveScalSampleZone(p.well, p.depth, p.zone), + DEPTH: p.depth, + RT: p.rt, + CPORE: p.cpore, + CPERM: p.cperm, + RQI: p.rqi, + MAX_PC: p.max_pc, + SW_AT_MAX_PC: p.sw_at_max_pc, + EXTRACTED_SWIRR: p.extracted_swirr, + MODELED_SWIRR: p.modeled_swirr, + RESIDUAL: p.residual, + STATUS: isFocused + ? 'Focused' + : p.included + ? 'Included' + : !p.valid_pc + ? 'Low Pc (< Ref)' + : 'Excluded' + }; + }) }; } From dc22fc8be46aa8bfc1e030f4c8ce4148d2121115 Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 11:29:15 +0800 Subject: [PATCH 02/10] Improve AI assisstant to reply more gracefully --- quick_pp/app/backend/mcp_server.py | 8 + quick_pp/app/backend/services/chat.py | 264 ++++++++++++++++-- .../app/backend/services/ssot_reference.py | 34 ++- .../shell/PetrophysicalAssistantDrawer.svelte | 32 ++- tests/test_assistant_graceful_handling.py | 80 ++++++ 5 files changed, 387 insertions(+), 31 deletions(-) create mode 100644 tests/test_assistant_graceful_handling.py diff --git a/quick_pp/app/backend/mcp_server.py b/quick_pp/app/backend/mcp_server.py index 939b7d93..2171fd32 100644 --- a/quick_pp/app/backend/mcp_server.py +++ b/quick_pp/app/backend/mcp_server.py @@ -1673,7 +1673,15 @@ def tool_get_workflow_technical_reference( "primary_match": matches[0], "reference_content": matches[0]["content"], "source": f"docs/workflows/{'multi_well.md' if 'multi' in matches[0]['scope'] else 'single_well.md'}", + "found": True, } + return { + "tool": "get_workflow_technical_reference", + "query": query, + "matches_count": 0, + "reference_content": f"No workflow technical reference found matching query '{query}'.", + "found": False, + } if step: content = get_step_reference(effective_scope, step) diff --git a/quick_pp/app/backend/services/chat.py b/quick_pp/app/backend/services/chat.py index e19c8f83..037333f3 100644 --- a/quick_pp/app/backend/services/chat.py +++ b/quick_pp/app/backend/services/chat.py @@ -81,6 +81,45 @@ def _get_env_key(name: str, default: str = "") -> str: }, ] + +PRESET_QUICK_ACTIONS: List[Dict[str, str]] = [ + { + "label": "Inspect Well", + "prompt": "Inspect this well and summarize available curves, zones, and core coverage.", + "description": "Inventory well curves, depth intervals, formation tops, and core coverage.", + }, + { + "label": "Run QAQC", + "prompt": "Run qaqc.py on this well (badhole flagging and NPHI/RHOB hydrocarbon correction).", + "description": "Identify out-of-limit spikes, borehole washouts (BADHOLE), and hydrocarbon effects.", + }, + { + "label": "Multi-Mineral", + "prompt": "Execute multi_mineral.py lithology and porosity optimization on this well.", + "description": "Simultaneous solver for mineral fractions (Quartz, Calcite, Clay) and effective porosity.", + }, + { + "label": "Run SHF", + "prompt": "Execute shf.py saturation height function and reconcile Sw vs SHF.", + "description": "FZI rock typing, Leverett-J saturation height modeling, and Sw vs SHF reconciliation.", + }, + { + "label": "Conformal Bounds", + "prompt": "Calibrate conformal prediction on the fly and explain the interpretation with P10-P90 bounds.", + "description": "Locally weighted split conformal prediction for distribution-free P10–P90 uncertainty intervals.", + }, + { + "label": "Full Evaluation", + "prompt": "Run a full petrophysical evaluation (QAQC, multi-mineral, SHF, and conformal prediction bounds) and explain the results.", + "description": "End-to-end automated petrophysical pipeline combining QAQC, lithology, SHF, and conformal intervals.", + }, + { + "label": "FDP Report", + "prompt": "Generate a full FDP Petrophysical Evaluation Report for this project with AI executive summary and figures.", + "description": "Compile an FDP Petrophysical Evaluation Report with high-DPI figures and export options.", + }, +] + SYSTEM_PROMPT = """You are the quick_pp Petrophysical AI Assistant embedded inside the Svelte Subsurface Workbench. You have direct access to the Petrophysical MCP Server tools: - `inspect_well`: Inspect well curves, depth range, formation tops, fluid contacts, core count, and zonal stats. @@ -95,7 +134,34 @@ def _get_env_key(name: str, default: str = "") -> str: - `export_report_to_pdf`: Compile the active report into a downloadable A4 PDF document. - `get_workflow_technical_reference`: Retrieve authoritative petrophysical equations, curve contracts, parameter bounds, and diagnostic guidelines from the quick_pp SSOT technical foundation in docs/workflows/. -Always ground your interpretation in the quantitative tool outputs and the quick_pp SSOT technical foundations: cite depth intervals, zonal means, P10–P90 conformal interval widths, physical constraints (volumetric closure, washout impact, FWL transition zones), and reference governing equations from docs/workflows/ when explaining petrophysical methods. +CRITICAL DIRECTIVE — SELF-ASSESSED ANSWER CONFIDENCE & EPISTEMIC HUMILITY: +You must critically assess and explicitly state your level of confidence in the answer you provide. +Subsurface and petrophysical evaluations involve high-stakes technical and financial decisions. Unwarranted certainty, guessing, or hallucinating numbers can cause severe errors. + +1. Explicit Confidence Assessment: + In EVERY response, you must explicitly evaluate your own confidence in your answer: + - **High Confidence**: The answer is directly proven by executed quantitative tool outputs or authoritative quick_pp SSOT documentation. + - **Medium Confidence**: The answer is based on general petrophysical rules-of-thumb or empirical correlations, but you lack local calibration, key logs are missing, or the prompt has minor ambiguities. + - **Low Confidence**: You are making assumptions, extrapolating beyond available project data, recalling regional literature/analogues from general pre-training without local well verification, or estimating numbers without direct measurements. + +2. Mandatory Callout When NOT Confident: + Whenever your confidence in the answer is **Medium** or **Low**, you MUST highlight this prominently at the start of your response or in a dedicated callout block: + ```markdown + > ⚠️ **Answer Confidence: [Low / Medium]** + > - **Why I am not confident in this answer**: [Explain specifically what information is unverified, missing, or approximate in what you are saying]. + > - **Knowledge Gaps & Uncertainties in this Answer**: [Detail what you do not know for sure, what assumptions you made, and what might differ in reality]. + > - **How to Verify**: [State what specific tool or action the user should run, e.g. "Run `Inspect Well` or `Multi-Mineral` on the active well to compute the true local porosity."]. + ``` + +3. Regional Literature Queries (e.g., "what is the average porosity in balmoral sandstone"): + - If the user asks about an external formation, regional analogue, or literature property, state clearly that your confidence regarding the active well is **Low** because regional values are external generalizations. + - Provide the known literature context (e.g. Paleocene Balmoral turbidite sandstones in the UK North Sea typically have high average porosities between 28% and 35%, with permeabilities ranging from 500 to 3000 mD), BUT immediately highlight: + - The gap in the answer: This is an uncalibrated regional reference only. Actual local porosity is governed by burial compaction, cementation (calcite/quartz), and clay content. + - You cannot confirm whether the currently loaded well penetrates this formation without running inspection tools. + - Instruct the user to run the relevant quick action (`Inspect Well` or `Multi-Mineral`) to evaluate their loaded well logs directly. + +4. Never Fake Confidence: + Never present speculative estimates as authoritative facts. If you do not have enough data to give a confident answer, openly state your uncertainty and guide the user on how to resolve it. """ @@ -173,16 +239,26 @@ def _plan_native_tool_calls(req: AgentChatRequest) -> List[Dict[str, Any]]: ) # Workflow Theory, Equations & SSOT Reference Queries (independent of active project) - wants_theory = any( + # Require explicit theory/methodology keywords combined with petrophysical terms + has_theory_kw = any( kw in q for kw in ( - "equation", "formula", "theory", "foundation", "how is", "how do you calculate", - "what is the model", "cutoffs", "definition", "ssot", "reference", - "explain", "how does", "what model", "which model", "cite formula", - "what are the cutoffs", "best practice", "parameter tuning", "guideline", - "tell me about", "what is", "how to" + "equation", "formula", "governing equation", "cite formula", + "how do you calculate", "how is", "mathematical model", + "ssot reference", "technical reference", "workflow reference", + "cutoffs definition", "parameter bounds", "theoretical foundation" + ) + ) + has_petro_topic = any( + topic in q + for topic in ( + "qaqc", "badhole", "washout", "multimineral", "multi-mineral", "lithology", + "porosity", "shf", "leverett", "j-function", "capillary", "conformal", + "uncertainty", "archie", "simandoux", "permeability", "fzi", "vshale", + "clay", "fwl", "fluid contact", "water saturation" ) ) + wants_theory = has_theory_kw and has_petro_topic if wants_theory: calls.append( { @@ -534,23 +610,64 @@ def _plan_native_tool_calls(req: AgentChatRequest) -> List[Dict[str, Any]]: ) return calls - # Default fallback if user asks a general question on an active well: inspect + conformal bounds - if not calls and well: - calls.append({"name": "inspect_well", "arguments": common_filter}) - calls.append( - { - "name": "evaluate_with_conformal_bounds", - "arguments": { - **common_filter, - "alpha": req.alpha, - "save": True, - }, - } + # Do not execute arbitrary tools if no specific petrophysical intent was recognized. + # Return empty list so the caller handles unrecognized prompts gracefully with preset quick actions. + return calls + + + +def _handle_unrecognized_native_prompt( + req: AgentChatRequest, +) -> tuple[str, List[Dict[str, str]]]: + """Gracefully handle prompts that the deterministic built-in agent cannot answer directly.""" + q = req.message.strip() + well = req.well_name or "active well" + + # Check if user is asking about external formation/analogue or regional literature + is_regional_or_literature = any( + term in q.lower() + for term in ( + "sandstone", "formation", "group", "basin", "field", "balmoral", "brent", + "statfjord", "forties", "ekofisk", "clifton", "average porosity", "typical", + "analogue", "analog", "literature", "benchmark", "regional" ) - elif not calls and pid is not None: - calls.append({"name": "inspect_well", "arguments": {"project_id": int(pid)}}) + ) - return calls + if is_regional_or_literature: + confidence_details = ( + f"> ⚠️ **Answer Confidence: Low / Cannot Determine**\n" + f"> The Built-in Petrophysical Agent cannot reliably answer this question from the loaded project data.\n" + f"> \n" + f"> **Why the agent is not confident in this answer:**\n" + f"> - **Regional / Literature Gap**: The prompt asks about regional formation properties (*{q}*), which are not present in the active well logs (`{well}`).\n" + f"> - **Deterministic Boundary**: The Built-in Agent operates strictly on deterministic quantitative tools (`inspect_well`, `run_well_qaqc`, `run_multi_mineral`, `run_shf_evaluation`, `evaluate_with_conformal_bounds`). It does not have an open-domain neural memory of worldwide formation literature.\n" + f"> - **How to Get an Answer**: Switch to an external LLM provider (**OpenRouter** or **Ollama**) in **Stack Settings (⚙)**. With an LLM enabled, the assistant can provide published regional literature benchmarks (such as North Sea Paleocene Balmoral turbidite porosities of ~28–35%) while explicitly highlighting any uncertainties." + ) + else: + confidence_details = ( + f"> ⚠️ **Answer Confidence: Low / Unrecognized Intent**\n" + f"> The Built-in Agent cannot determine the intended subsurface workflow from this prompt.\n" + f"> \n" + f"> **Why the agent is not confident in this answer:**\n" + f"> - **Unrecognized Command**: The prompt (*{q}*) does not match a supported petrophysical tool command on `{well}`.\n" + f"> - **Deterministic Boundary**: The Built-in Agent only executes verified petrophysical algorithms (Log QAQC, multi-mineral lithology/porosity inversion, saturation height modeling, conformal P10–P90 bounds, and FDP reports).\n" + f"> - **Recommendation**: Choose from the preset quick actions below, or switch to an external LLM provider (**OpenRouter** or **Ollama**) in **Stack Settings (⚙)** for open-ended queries." + ) + + actions_md = "\n".join( + f"- ⚡ **{act['label']}**: {act['description']}" + for act in PRESET_QUICK_ACTIONS + ) + + reply = ( + f"### ℹ️ Built-in Agent Notice\n\n" + f"{confidence_details}\n\n" + f"---\n\n" + f"#### ⚡ Preset Quick Actions for `{well}`\n" + f"Instead of guessing or executing unrequested operations, you can run any of these verified workflows:\n\n" + f"{actions_md}\n" + ) + return reply, PRESET_QUICK_ACTIONS def _synthesize_petrophysical_explanation( @@ -566,6 +683,30 @@ def _synthesize_petrophysical_explanation( sections: List[str] = [] + # Check for confidence degraders across tool outputs + has_washout = any( + (rec.get("result") or {}).get("badhole", {}).get("flagged_count", 0) > 0 + for rec in tool_records + ) + no_core = any( + rec.get("name") == "inspect_well" and (rec.get("result") or {}).get("core_sample_count", 0) == 0 + for rec in tool_records + ) + + if has_washout or no_core: + reasons: List[str] = [] + if has_washout: + reasons.append("washout intervals detected where density/neutron log measurements are degraded by pad standoff") + if no_core: + reasons.append("lack of routine core calibration points (CPORE/CPERM) to anchor porosity and permeability transforms") + confidence_badge = ( + f"> ⚠️ **Answer Confidence: Medium** — Quantitative results computed from active well logs, but note: " + f"{'; '.join(reasons)}.\n" + ) + sections.append(confidence_badge) + else: + sections.append("> ✅ **Answer Confidence: High** — Grounded directly in quantitative MCP tool execution on active well logs.\n") + for rec in tool_records: t_name = rec["name"] res = rec.get("result") or {} @@ -751,6 +892,69 @@ def _synthesize_petrophysical_explanation( f"{ref_md}\n" ) + # Synthesize comprehensive Gaps & Uncertainty Analysis section across all executed tools + gaps: List[str] = [] + uncerts: List[str] = [] + + for rec in tool_records: + t_name = rec["name"] + res = rec.get("result") or {} + if t_name == "inspect_well": + core_pts = res.get("core_sample_count", 0) + if core_pts == 0: + gaps.append( + "**Core Calibration Gap**: No core samples (`CPORE` / `CPERM`) available in this well. " + "Porosity, permeability, and saturation height models rely on uncalibrated empirical log transforms." + ) + curves = set(res.get("available_curves", [])) + missing_essential = [c for c in ("RHOB", "NPHI", "GR", "RT", "CALI") if c not in curves] + if missing_essential: + gaps.append( + f"**Missing Essential Curves**: Curves {', '.join(f'`{c}`' for c in missing_essential)} " + f"are not present in the well log dataset." + ) + elif t_name == "run_well_qaqc": + bh = res.get("badhole") or {} + if bh.get("flagged_count", 0) > 0: + gaps.append( + f"**Borehole Washout Gap**: `{bh.get('flagged_pct')}%` (`{bh.get('flagged_count')}` samples) " + f"flagged as badhole. Density (`RHOB`) and neutron (`NPHI`) readings in washout intervals " + f"are degraded by pad standoff." + ) + elif t_name == "run_shf_evaluation": + rec_cmp = res.get("sw_vs_shf_reconciliation") or {} + if rec_cmp: + uncerts.append( + f"**Sw vs. SHF Reconciliation Spread**: Transition zone uncertainty indicated by RMSE of " + f"`{rec_cmp.get('rmse_sw_vs_shf')} v/v` and mean bias of `{rec_cmp.get('mean_bias_sw_minus_shf')} v/v`." + ) + elif t_name == "evaluate_with_conformal_bounds": + iw = res.get("interval_width_summary") or {} + if iw: + uncerts.append( + f"**Conformal P10–P90 Interval Spreads**: Porosity uncertainty is `±{round(iw.get('phit_mean_width_vv', 0)/2, 4)} v/v`, " + f"Sw uncertainty is `±{round(iw.get('sw_mean_width_vv', 0)/2, 4)} v/v`, and Permeability spread is " + f"`{iw.get('perm_median_p90_p10_factor')}×` between P10 and P90." + ) + z_conf = res.get("zonal_conformal_summary") or [] + if z_conf: + cert_pay = [f"{z['zone']} ({round(z['certifiable_pay_fraction']*100, 1)}% P10 vs {round(z['upside_pay_fraction']*100, 1)}% P90)" for z in z_conf[:3]] + uncerts.append( + f"**Pay Confidence Disparity**: Certifiable pay (P10) vs upside pay (P90): {', '.join(cert_pay)}." + ) + + if gaps or uncerts: + unc_lines = ["### ⚠️ Gaps & Uncertainty Analysis"] + if gaps: + unc_lines.append("**Identified Data & Coverage Gaps**:") + for g in gaps: + unc_lines.append(f"- {g}") + if uncerts: + unc_lines.append("**Petrophysical Model Uncertainties**:") + for u in uncerts: + unc_lines.append(f"- {u}") + sections.append("\n".join(unc_lines)) + return "\n\n".join(sections) @@ -955,6 +1159,7 @@ async def list_assistant_providers(): return { "providers": providers_out, "mcp_tools": petro_mcp_server.list_tools(), + "preset_quick_actions": PRESET_QUICK_ACTIONS, } @@ -982,6 +1187,20 @@ async def assistant_chat(req: AgentChatRequest): else: # Built-in Petrophysical MCP Agent planned = _plan_native_tool_calls(req) + if not planned: + # Built-in agent cannot answer this prompt; handle gracefully with preset quick actions + reply_text, suggested_actions = _handle_unrecognized_native_prompt(req) + return { + "reply": reply_text, + "provider": provider, + "used_fallback": False, + "tool_calls": [], + "computed_curves": {}, + "stats": {}, + "report_data": None, + "ui_actions": [], + "quick_actions": suggested_actions, + } for call_spec in planned: try: t_out = petro_mcp_server.call_tool( @@ -1077,6 +1296,7 @@ async def assistant_chat(req: AgentChatRequest): "stats": merged_stats, "report_data": latest_report_data, "ui_actions": ui_actions, + "quick_actions": PRESET_QUICK_ACTIONS if not executed_tools else [], } diff --git a/quick_pp/app/backend/services/ssot_reference.py b/quick_pp/app/backend/services/ssot_reference.py index 1a2c8995..0e239c1f 100644 --- a/quick_pp/app/backend/services/ssot_reference.py +++ b/quick_pp/app/backend/services/ssot_reference.py @@ -232,8 +232,24 @@ def query_reference( ) -> List[Dict[str, Any]]: """Search SSOT documentation for keywords or formulas.""" self._ensure_loaded() - q_tokens = query.lower().split() - if not q_tokens: + stopwords = { + "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", + "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", + "between", "both", "but", "by", "can", "could", "did", "do", "does", "doing", + "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", + "having", "he", "her", "here", "hers", "herself", "him", "himself", "his", "how", + "i", "if", "in", "into", "is", "it", "its", "itself", "just", "me", "more", "most", + "my", "myself", "no", "nor", "not", "now", "of", "off", "on", "once", "only", + "or", "other", "our", "ours", "ourselves", "out", "over", "own", "same", "she", + "should", "so", "some", "such", "than", "that", "the", "their", "theirs", "them", + "themselves", "then", "there", "these", "they", "this", "those", "through", "to", + "too", "under", "until", "up", "very", "was", "we", "were", "what", "when", + "where", "which", "while", "who", "whom", "why", "with", "would", "you", "your", + "yours", "yourself", "yourselves", "tell", "please", "average", "value" + } + raw_tokens = re.findall(r"\b[a-z0-9_-]{2,}\b", query.lower()) + meaningful_tokens = [t for t in raw_tokens if t not in stopwords] + if not meaningful_tokens: return [] results: List[Dict[str, Any]] = [] @@ -245,12 +261,14 @@ def query_reference( for anchor_id, content in anchors.items(): content_lower = content.lower() - # Score based on token frequency - score = sum(content_lower.count(token) for token in q_tokens) - if score > 0: - # Extract header - first_line = content.splitlines()[0] if content else anchor_id - clean_header = re.sub(r"\{#[\w\-]+\}", "", first_line).lstrip("#").strip() + first_line = content.splitlines()[0] if content else anchor_id + clean_header = re.sub(r"\{#[\w\-]+\}", "", first_line).lstrip("#").strip() + header_lower = clean_header.lower() + + title_matches = sum(1 for token in meaningful_tokens if token in header_lower or token in anchor_id.lower()) + body_matches = sum(content_lower.count(token) for token in meaningful_tokens) + if title_matches > 0 or body_matches > 0: + score = title_matches * 5 + body_matches results.append( { "scope": sc, diff --git a/quick_pp/app/frontend/src/lib/components/shell/PetrophysicalAssistantDrawer.svelte b/quick_pp/app/frontend/src/lib/components/shell/PetrophysicalAssistantDrawer.svelte index 02bc519b..4b585a1f 100644 --- a/quick_pp/app/frontend/src/lib/components/shell/PetrophysicalAssistantDrawer.svelte +++ b/quick_pp/app/frontend/src/lib/components/shell/PetrophysicalAssistantDrawer.svelte @@ -31,6 +31,7 @@ text: string; provider?: string; toolCalls?: ToolCallRecord[]; + quickActions?: Array<{ label: string; prompt: string; description?: string }>; timestamp: string; } @@ -302,6 +303,7 @@ text: data.reply || 'Evaluation completed.', provider: data.provider || selectedProvider, toolCalls: data.tool_calls || [], + quickActions: Array.isArray(data.quick_actions) ? data.quick_actions : [], timestamp: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' @@ -991,8 +993,12 @@
{:else if block.type === 'blockquote'} + {@const rawQuote = block.tokens.map((t) => t.value).join(' ')} + {@const isWarning = rawQuote.includes('⚠️') || rawQuote.toLowerCase().includes('confidence: low')}
{@render renderInline(block.tokens)}
@@ -1008,6 +1014,30 @@ {/if} {/each}
+ + + {#if msg.quickActions && msg.quickActions.length > 0} +
+
+ Preset Quick Actions + Click to execute +
+
+ {#each msg.quickActions as act} + + {/each} +
+
+ {/if} {/if}
{/each} diff --git a/tests/test_assistant_graceful_handling.py b/tests/test_assistant_graceful_handling.py new file mode 100644 index 00000000..680dd063 --- /dev/null +++ b/tests/test_assistant_graceful_handling.py @@ -0,0 +1,80 @@ +import pytest +from quick_pp.app.backend.services.chat import ( + AgentChatRequest, + _plan_native_tool_calls, + _handle_unrecognized_native_prompt, + PRESET_QUICK_ACTIONS, + SYSTEM_PROMPT, +) + + +def test_balmoral_sandstone_does_not_randomly_run_tools(): + """Verify that external regional queries do not trigger random tools or blind fallbacks.""" + req = AgentChatRequest( + message="what is the average porosity in balmoral sandstone", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned = _plan_native_tool_calls(req) + # Must NOT plan any tools (no random inspect, no conformal bounds, no irrelevant SSOT chapters) + assert planned == [] + + +def test_unrecognized_native_prompt_highlights_low_confidence_and_quick_actions(): + """Verify that unhandled prompts return low confidence warning and preset quick actions.""" + req = AgentChatRequest( + message="what is the average porosity in balmoral sandstone", + well_name="30-7a-8", + provider="native_mcp", + ) + reply, actions = _handle_unrecognized_native_prompt(req) + + # Must highlight answer confidence + assert "Answer Confidence: Low" in reply + assert "Why the agent is not confident in this answer" in reply + assert "Balmoral" in reply or "balmoral" in reply + + # Must return all 7 preset quick actions + assert len(actions) == 7 + assert len(actions) == len(PRESET_QUICK_ACTIONS) + assert any(act["label"] == "Inspect Well" for act in actions) + assert any(act["label"] == "Run QAQC" for act in actions) + assert any(act["label"] == "Multi-Mineral" for act in actions) + assert any(act["label"] == "Conformal Bounds" for act in actions) + assert "Preset Quick Actions" in reply + + +def test_valid_petrophysical_prompts_still_route_correctly(): + """Verify that legitimate petrophysical commands still trigger the correct tools.""" + req_inspect = AgentChatRequest( + message="Inspect this well and check curve inventory", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned_inspect = _plan_native_tool_calls(req_inspect) + assert len(planned_inspect) == 1 + assert planned_inspect[0]["name"] == "inspect_well" + + req_qaqc = AgentChatRequest( + message="Run QAQC badhole detection on this well", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned_qaqc = _plan_native_tool_calls(req_qaqc) + assert len(planned_qaqc) == 1 + assert planned_qaqc[0]["name"] == "run_well_qaqc" + + +def test_system_prompt_mandates_answer_confidence_assessment(): + """Verify that the LLM system prompt conditions the agent to assess confidence and highlight gaps in answers.""" + assert "SELF-ASSESSED ANSWER CONFIDENCE & EPISTEMIC HUMILITY" in SYSTEM_PROMPT + assert "High Confidence" in SYSTEM_PROMPT + assert "Medium Confidence" in SYSTEM_PROMPT + assert "Low Confidence" in SYSTEM_PROMPT + assert "Answer Confidence: [Low / Medium]" in SYSTEM_PROMPT + assert "Why I am not confident in this answer" in SYSTEM_PROMPT + assert "Balmoral" in SYSTEM_PROMPT + From 8e92aaf13a304033af89c634b8248cfa9779e147 Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 15:41:29 +0800 Subject: [PATCH 03/10] debug and enhance contact identification and optimization module --- quick_pp/app/backend/services/ancillary.py | 18 +- quick_pp/app/backend/services/report.py | 109 ++++++ quick_pp/app/backend/services/workspace.py | 133 ++++++- .../sections/06_saturation_contacts.md.j2 | 38 ++ .../components/BulkAncillaryImporter.svelte | 8 +- .../components/modules/MultiWellShf.svelte | 49 +++ .../src/lib/state/projectData.svelte.ts | 4 +- quick_pp/database/db_connector.py | 32 ++ quick_pp/database/models.py | 10 +- quick_pp/database/objects.py | 86 +++- quick_pp/database/setup_postgresql_db.sql | 5 + quick_pp/database/setup_sqlite_db.sql | 5 + quick_pp/saturation.py | 367 +++++++++++++++++- tests/test_fluid_contacts_and_limits.py | 256 ++++++++++++ 14 files changed, 1081 insertions(+), 39 deletions(-) create mode 100644 tests/test_fluid_contacts_and_limits.py diff --git a/quick_pp/app/backend/services/ancillary.py b/quick_pp/app/backend/services/ancillary.py index cae82de9..3c8c71a6 100644 --- a/quick_pp/app/backend/services/ancillary.py +++ b/quick_pp/app/backend/services/ancillary.py @@ -188,6 +188,10 @@ "depth_m", "depth_ft", ], + "ZONE": ["zone", "formation", "reservoir", "unit", "compartment"], + "FAULT_BLOCK": ["fault_block", "faultblock", "block", "compartment_block"], + "SOURCE": ["source", "method", "determination_method", "lineage", "basis"], + "REMARKS": ["remarks", "remark", "comment", "comments", "description"], }, "perforations": { "WELL_NAME": ["well", "well_name", "wellname", "uwi", "wellbore", "well_id"], @@ -2564,12 +2568,24 @@ def _opt_str(row: Dict[str, Any], col: Optional[str]) -> Optional[str]: elif dtype == "contacts": c_col, _ = col_map.get("CONTACT_TYPE", ("contact_type", None)) d_col, d_unit = col_map.get("DEPTH", ("depth", None)) + z_col, _ = col_map.get("ZONE", ("zone", None)) + fb_col, _ = col_map.get("FAULT_BLOCK", ("fault_block", None)) + src_col, _ = col_map.get("SOURCE", ("source", None)) + rem_col, _ = col_map.get("REMARKS", ("remarks", None)) contacts_payload = [] for r in w_rows: c_type = str(r.get(c_col, "")).strip().upper() d_v = _convert_ancillary_numeric(r.get(d_col), d_unit, "DEPTH") if c_type and d_v is not None: - contacts_payload.append({"name": c_type, "depth": d_v}) + contacts_payload.append({ + "name": str(r.get("name") or c_type).strip(), + "contact_type": c_type, + "depth": d_v, + "zone": str(r.get(z_col, "")).strip() or None if z_col in r else None, + "fault_block": str(r.get(fb_col, "")).strip() or None if fb_col in r else None, + "source": str(r.get(src_col, "")).strip() or None if src_col in r else None, + "remarks": str(r.get(rem_col, "")).strip() or None if rem_col in r else None, + }) if contacts_payload: well.add_fluid_contacts(contacts_payload, replace=True) summary_by_well[wn] = len(contacts_payload) diff --git a/quick_pp/app/backend/services/report.py b/quick_pp/app/backend/services/report.py index 0eb02134..3b8efd34 100644 --- a/quick_pp/app/backend/services/report.py +++ b/quick_pp/app/backend/services/report.py @@ -1983,6 +1983,113 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "omega": 0.33, } + # Query all committed fluid contacts for selected project wells + well_name_by_id = {w["id"]: w["name"] for w in selected_wells_meta} + fluid_contacts_rows: List[Dict[str, Any]] = [] + with get_db() as session: + fc_res = session.execute( + select( + db_objects.ORMFluidContact.well_id, + db_objects.ORMFluidContact.name, + db_objects.ORMFluidContact.depth, + db_objects.ORMFluidContact.zone, + db_objects.ORMFluidContact.contact_type, + db_objects.ORMFluidContact.fault_block, + db_objects.ORMFluidContact.source, + db_objects.ORMFluidContact.remarks, + ) + .where(db_objects.ORMFluidContact.well_id.in_(list(well_name_by_id.keys()))) + .order_by(db_objects.ORMFluidContact.depth) + ).all() + for r in fc_res: + c_type = str(r.contact_type or r.name or "CONTACT").upper() + if c_type == "WDT": # Excluded per user instruction + continue + wn = well_name_by_id.get(r.well_id, f"Well-{r.well_id}") + fluid_contacts_rows.append({ + "well_name": wn, + "name": str(r.name), + "depth": round(float(r.depth), 1) if r.depth is not None else None, + "zone": str(r.zone) if r.zone else "All Zones", + "contact_type": c_type, + "fault_block": str(r.fault_block) if r.fault_block else None, + "source": str(r.source) if r.source else "Petrophysical Log Evaluation", + "remarks": str(r.remarks) if r.remarks else "", + }) + + # If no stored contacts in DB, dynamically extract them from full_df + if not fluid_contacts_rows and not full_df.empty: + try: + from quick_pp.saturation import extract_fluid_contacts_and_limits + extracted_df = extract_fluid_contacts_and_limits( + df=full_df, + fwl_dict={"default": fwl_val}, + sw_pay_cutoff=sw_cut, + phit_cutoff=phie_cut, + vsh_cutoff=vsh_cut, + ) + if not extracted_df.empty: + for r in extracted_df.to_dict(orient="records"): + c_type = str(r.get("contact_type") or "").upper() + if c_type == "WDT": + continue + fluid_contacts_rows.append({ + "well_name": str(r.get("well_name") or "Field"), + "name": str(r.get("name") or c_type), + "depth": round(float(r["depth"]), 1) if r.get("depth") is not None else None, + "zone": str(r.get("zone")) if r.get("zone") else "All Zones", + "contact_type": c_type, + "fault_block": str(r.get("fault_block")) if r.get("fault_block") else None, + "source": str(r.get("source") or "Petrophysical Log Interpretation"), + "remarks": str(r.get("remarks") or ""), + }) + except Exception: + pass + + # Summarize proved column and uncertainty brackets by (zone, fault_block) + zonal_contact_groups: Dict[Tuple[str, Optional[str]], Dict[str, Any]] = {} + for r in fluid_contacts_rows: + key = (r["zone"], r.get("fault_block")) + if key not in zonal_contact_groups: + zonal_contact_groups[key] = { + "zone": r["zone"], + "fault_block": r.get("fault_block"), + "gut": None, + "gdt": None, + "out": None, + "odt": None, + "wut": None, + "goc": None, + "gwc": None, + "owc": None, + "fwl": None, + } + g = zonal_contact_groups[key] + ctype = r["contact_type"] + d = r["depth"] + if d is None: + continue + if ctype == "GUT": + g["gut"] = min(g["gut"], d) if g["gut"] is not None else d + elif ctype == "GDT": + g["gdt"] = max(g["gdt"], d) if g["gdt"] is not None else d + elif ctype == "OUT": + g["out"] = min(g["out"], d) if g["out"] is not None else d + elif ctype == "ODT": + g["odt"] = max(g["odt"], d) if g["odt"] is not None else d + elif ctype == "WUT": + g["wut"] = min(g["wut"], d) if g["wut"] is not None else d + elif ctype == "GOC": + g["goc"] = d + elif ctype == "GWC": + g["gwc"] = d + elif ctype == "OWC": + g["owc"] = d + elif ctype == "FWL": + g["fwl"] = d + + fluid_contacts_summary = list(zonal_contact_groups.values()) + ctx_payload: Dict[str, Any] = { "project_id": pid, "project_name": project_name, @@ -2008,6 +2115,8 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "zonal_summary": zonal_summary, "well_zonal_summary": well_zonal_rows, "ressum_markdown_table": ressum_markdown_table, + "fluid_contacts_rows": fluid_contacts_rows, + "fluid_contacts_summary": fluid_contacts_summary, "uncertainty_summary": uncertainty_summary, "volumetric_summary": merged_configs.get("volumetric_summary"), "volumetric_params": merged_configs.get("volumetric_params"), diff --git a/quick_pp/app/backend/services/workspace.py b/quick_pp/app/backend/services/workspace.py index f43edaaa..7929a30d 100644 --- a/quick_pp/app/backend/services/workspace.py +++ b/quick_pp/app/backend/services/workspace.py @@ -97,6 +97,7 @@ train_and_validate_rock_type_classifier, ) from quick_pp.saturation import ( + extract_fluid_contacts_and_limits, archie_saturation, estimate_b_waxman_smits, estimate_qv, @@ -8047,6 +8048,7 @@ class WorkspaceOptimizeFwlRequest(BaseModel): mdt_weight: Optional[float] = Field(None) solve_per_well: bool = Field(True) solve_per_zone: bool = Field(True) + pay_weighted: bool = Field(True) optimize_shm_params: bool = Field(False) unit: str = Field("metric") save: bool = Field(False) @@ -9016,20 +9018,38 @@ def workspace_optimize_fwl( mdt_penalty_weight=penalty_w, solve_per_well=req.solve_per_well, solve_per_zone=req.solve_per_zone, + pay_weighted=req.pay_weighted, zone_col="ZONES", optimize_shm_params=req.optimize_shm_params, unit=req.unit, ) saved_wells = 0 - if req.save and "SW_SHM" in res["df"].columns: - for wn, grp in res["df"].groupby("WELL_NAME"): - _save_curves_to_well( - project_id, - str(wn), - grp[["DEPTH", "SW_SHM"]].rename(columns={"SW_SHM": "SHF"}), - ) - saved_wells += 1 + contacts_records = [] + if "fluid_contacts" in res and isinstance(res["fluid_contacts"], pd.DataFrame) and not res["fluid_contacts"].empty: + contacts_records = dataframe_to_records(res["fluid_contacts"]) + + if req.save: + if "SW_SHM" in res["df"].columns: + for wn, grp in res["df"].groupby("WELL_NAME"): + _save_curves_to_well( + project_id, + str(wn), + grp[["DEPTH", "SW_SHM"]].rename(columns={"SW_SHM": "SHF"}), + ) + saved_wells += 1 + if "fluid_contacts" in res and isinstance(res["fluid_contacts"], pd.DataFrame) and not res["fluid_contacts"].empty: + try: + with get_db() as session: + proj_obj = db_objects.Project.load(session, project_id=project_id) + for wn, grp in res["fluid_contacts"].groupby("well_name"): + try: + well_obj = proj_obj.get_well(str(wn)) + well_obj.add_fluid_contacts(grp.to_dict(orient="records"), replace=False) + except Exception: + continue + except Exception: + pass zone_metrics_df = res.get("zone_metrics") zone_metrics_records = ( @@ -9058,6 +9078,7 @@ def workspace_optimize_fwl( "well_metrics": dataframe_to_records(res["well_metrics"].round(4)), "zone_metrics": zone_metrics_records, "grid_profile": dataframe_to_records(res["grid_profile"].round(4)), + "fluid_contacts": contacts_records, "saved": req.save, "saved_wells": saved_wells, } @@ -9067,6 +9088,102 @@ def workspace_optimize_fwl( ) from e +class WorkspaceExtractContactsRequest(BaseModel): + well_names: Optional[List[str]] = Field(None, description="Subset of wells to extract contacts for") + zones: Optional[List[str]] = Field(None, description="Subset of zones") + save: bool = Field(True, description="Save extracted contacts into each well's ancillary data table") + replace: bool = Field(False, description="Replace existing contacts for target wells") + sw_pay_cutoff: float = Field(0.65, description="Max Sw for hydrocarbon pay") + sw_water_cutoff: float = Field(0.90, description="Min Sw for 100% water leg") + phit_cutoff: float = Field(0.05, description="Min porosity for reservoir rock") + vsh_cutoff: float = Field(0.50, description="Max Vshale for reservoir rock") + + +@router.post( + "/contacts/extract_and_save", + summary="Extract and save fluid limits & contacts (GUT, GDT, GOC, GWC, OUT, ODT, OWC, WUT, FWL) per well and zone", +) +def workspace_extract_and_save_contacts( + project_id: int, req: WorkspaceExtractContactsRequest +) -> Dict[str, Any]: + """Extract petrophysical fluid limits and contacts across project wells with full data lineage.""" + try: + proj_summary = get_project_summary(project_id) + all_well_names = [w["name"] for w in proj_summary.get("wells", [])] + target_wells = req.well_names if req.well_names else all_well_names + dfs = _load_multi_well_dfs(project_id, well_names=target_wells) + if not dfs: + raise ValueError("No well log data available for contact extraction.") + comb = pd.concat(dfs, ignore_index=True) + if req.zones and "ZONES" in comb.columns: + comb = comb[comb["ZONES"].isin(req.zones)] + + contacts_df = extract_fluid_contacts_and_limits( + df=comb, + sw_pay_cutoff=req.sw_pay_cutoff, + sw_water_cutoff=req.sw_water_cutoff, + phit_cutoff=req.phit_cutoff, + vsh_cutoff=req.vsh_cutoff, + ) + + saved_count = 0 + if req.save and not contacts_df.empty: + with get_db() as session: + proj = db_objects.Project.load(session, project_id=project_id) + for wn, grp in contacts_df.groupby("well_name"): + try: + w = proj.get_well(str(wn)) + w.add_fluid_contacts(grp.to_dict(orient="records"), replace=req.replace) + saved_count += len(grp) + except Exception: + continue + + records = dataframe_to_records(contacts_df) + return { + "project_id": project_id, + "contacts_count": len(records), + "saved_count": saved_count, + "contacts": records, + } + except Exception as exc: + raise HTTPException( + status_code=400, detail=f"Failed to extract fluid contacts: {exc}" + ) from exc + + +@router.get( + "/contacts/summary", + summary="Get multi-well & zonal fluid contacts and limits summary for project", +) +def workspace_contacts_summary( + project_id: int, well_name: Optional[str] = None +) -> Dict[str, Any]: + """Retrieve all fluid contacts across project wells structured with lineage.""" + try: + with get_db() as session: + proj = db_objects.Project.load(session, project_id=project_id) + target_wells = [well_name] if well_name else proj.get_well_names() + all_rows = [] + for wn in target_wells: + try: + w = proj.get_well(wn) + df = w.get_fluid_contacts() + for r in df.to_dict(orient="records"): + r["well_name"] = wn + all_rows.append(r) + except Exception: + continue + return { + "project_id": project_id, + "contacts_count": len(all_rows), + "contacts": all_rows, + } + except Exception as exc: + raise HTTPException( + status_code=500, detail=f"Failed to get fluid contacts summary: {exc}" + ) from exc + + class WorkspaceMlInferenceRequest(WorkspaceFilterBase): model_config_key: str = Field( "clastic", diff --git a/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 b/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 index 131ae073..fe2cac4b 100644 --- a/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 +++ b/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 @@ -32,3 +32,41 @@ $$S_w^n + R_w \cdot B \cdot Q_v \cdot S_w^{n-1} = \frac{a \cdot R_w}{\phi_t^m \c ![Figure {{ sec_num }}.1: Pickett Plot & Water Saturation Profile vs. Free Water Level]({{ assets.pickett_shf_plot }}) {% endif %} + +### {{ sec_num }}.2 Multi-Well & Zonal Fluid Contacts, Proved Column & Uncertainty Brackets + +Accurate delineation of fluid contacts is fundamental to volumetric in-place estimation and dynamic reservoir simulation. The proved hydrocarbon column is bounded by the shallowest and deepest proved fluid limits, while discrete contacts (OWC, GOC, GWC) and Free Water Levels (FWL) define the hydrostatic equilibrium boundaries. + +{% if fluid_contacts_rows and fluid_contacts_rows|length > 0 %} +| Well Name | Zone | Fault Block | Contact / Limit Type | Depth (m TVDss) | Determination Method / Source | Geological Notes / Remarks | +| :--- | :--- | :--- | :---: | :---: | :--- | :--- | +{% for row in fluid_contacts_rows %} +| {{ row.well_name }} | {{ row.zone or "General" }} | {{ row.fault_block or "-" }} | **{{ row.contact_type }}** | {{ "%.1f"|format(row.depth) if row.depth is not none else "-" }} | {{ row.source or "Petrophysical Log Evaluation" }} | {{ row.remarks or "-" }} | +{% endfor %} + +{% if fluid_contacts_summary and fluid_contacts_summary|length > 0 %} +#### Hydrocarbon Column & Uncertainty Bracket Summary by Zone + +{% for z in fluid_contacts_summary %} +* **Zone: `{{ z.zone }}`**{% if z.fault_block %} (Fault Block: `{{ z.fault_block }}`){% endif %}: + {% if z.gut is not none and z.gdt is not none %} + - **Proved Gas Column:** ${{ "%.1f"|format(z.gut) }}\text{ m}$ (GUT) to ${{ "%.1f"|format(z.gdt) }}\text{ m TVDss}$ (GDT) [Proved Gross Gas Column: ${{ "%.1f"|format(z.gdt - z.gut) }}\text{ m}$]. + {% endif %} + {% if z.out is not none and z.odt is not none %} + - **Proved Oil Column:** ${{ "%.1f"|format(z.out) }}\text{ m}$ (OUT) to ${{ "%.1f"|format(z.odt) }}\text{ m TVDss}$ (ODT) [Proved Gross Oil Column: ${{ "%.1f"|format(z.odt - z.out) }}\text{ m}$]. + {% endif %} + {% if z.goc is not none %} + - **Gas-Oil Contact (GOC):** ${{ "%.1f"|format(z.goc) }}\text{ m TVDss}$ (Bracket: ${{ "%.1f"|format(z.gdt) if z.gdt else "N/A" }}\text{ m}$ to ${{ "%.1f"|format(z.out) if z.out else "N/A" }}\text{ m}$). + {% endif %} + {% if z.owc is not none %} + - **Oil-Water Contact (OWC):** ${{ "%.1f"|format(z.owc) }}\text{ m TVDss}$ (Bracket: ${{ "%.1f"|format(z.odt) if z.odt else "N/A" }}\text{ m}$ to ${{ "%.1f"|format(z.wut) if z.wut else "N/A" }}\text{ m}$). + {% endif %} + {% if z.fwl is not none %} + - **Free Water Level (FWL):** ${{ "%.1f"|format(z.fwl) }}\text{ m TVDss}$ (Capillary $P_c = 0$ Datum). + {% endif %} +{% endfor %} +{% endif %} +{% else %} +> [!NOTE] +> Discrete single-well fluid contact markers (GUT/GDT/OUT/ODT/WUT/OWC/GOC/FWL) have not yet been extracted or committed to ancillary data for this project. Global saturation model uses $FWL = {{ "%.1f"|format(sw_params.fwl) }}\text{ m TVDss}$. +{% endif %} diff --git a/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte b/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte index 4a0ef746..a04a19ce 100644 --- a/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte +++ b/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte @@ -87,8 +87,12 @@ ], fluid_contacts: [ { key: 'WELL_NAME', label: 'WELL_NAME — Well Name / UWI' }, - { key: 'CONTACT_TYPE', label: 'CONTACT_TYPE — Fluid Contact (OWC/GOC/FWL) *', required: true }, - { key: 'DEPTH', label: 'DEPTH — Contact Depth *', required: true } + { key: 'CONTACT_TYPE', label: 'CONTACT_TYPE — Fluid Contact (OWC/GOC/GWC/FWL/ODT/OUT/GDT/GUT/WUT) *', required: true }, + { key: 'DEPTH', label: 'DEPTH — Contact Depth *', required: true }, + { key: 'ZONE', label: 'ZONE — Reservoir Zone / Formation' }, + { key: 'FAULT_BLOCK', label: 'FAULT_BLOCK — Fault Block / Compartment' }, + { key: 'SOURCE', label: 'SOURCE — Method / Determination Lineage' }, + { key: 'REMARKS', label: 'REMARKS — Geological Notes / Comments' } ], perforations: [ { key: 'WELL_NAME', label: 'WELL_NAME — Well Name / UWI' }, diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte index d48951ad..c55fd30e 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte @@ -4,6 +4,7 @@ import NumericScrubber from '$lib/components/workbench/NumericScrubber.svelte'; import ParameterSection from '$lib/components/workbench/ParameterSection.svelte'; import SavedParamsIndicator from '$lib/components/workbench/SavedParamsIndicator.svelte'; + import { appStatus } from '$lib/state/status.svelte'; import { projectData } from '$lib/state/projectData.svelte'; import { projectParams } from '$lib/state/projectParams.svelte'; import { wellData } from '$lib/state/wellData.svelte'; @@ -276,6 +277,7 @@ shm_params: projectData.shfResult?.fits ?? null, loss: so.optLoss, solve_per_zone: so.solvePerZone, + pay_weighted: so.payWeighted ?? true, ift: sp.ift, theta: sp.theta, gw: sp.gw, @@ -306,6 +308,32 @@ projectData.isComputingMultiWell = false; } } + + let isSavingContacts = $state(false); + async function saveContactsToAncillary() { + if (!workbench.projectId) return; + isSavingContacts = true; + try { + const res = await postJson( + `/quick_pp/workspace/projects/${encodeURIComponent(workbench.projectId)}/contacts/extract_and_save`, + { + well_names: projectData.wellsWithTvd.map((w) => w.name), + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + save: true, + replace: false + } + ); + const count = res.saved_count ?? res.contacts_count ?? 0; + appStatus.success(`Saved ${count} fluid contacts (ODT, OWC, FWL, etc.) to well ancillary data.`); + if (workbench.projectId) { + await projectData.selectProject(workbench.projectId); + } + } catch (e: any) { + projectData.error = e?.message || 'Failed to save fluid contacts'; + } finally { + isSavingContacts = false; + } + }
@@ -391,6 +419,14 @@ /> Φ-Weighted + {/if} + {#if wellData.contacts.length > 0} + + {/if}
+ {/if} + {/each} + {/if} {#if showInspectionLine && inspectionPct !== null} diff --git a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts index b70524cf..11886d6d 100644 --- a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts @@ -590,6 +590,7 @@ class WorkbenchState { stickPlotShowContacts = $state(true); stickPlotShowTops = $state(true); stickPlotShowPerfs = $state(true); + logShowContacts = $state(true); // Global modal / drawer flags importerOpen = $state(false); diff --git a/quick_pp/saturation.py b/quick_pp/saturation.py index d4f9c361..a36abebf 100644 --- a/quick_pp/saturation.py +++ b/quick_pp/saturation.py @@ -2577,6 +2577,9 @@ def extract_fluid_contacts_and_limits( sw_water_cutoff: float = 0.90, phit_cutoff: float = 0.05, vsh_cutoff: float = 0.50, + min_sand_thickness: float = 1.0, + min_sand_samples: int = 5, + require_pay: bool = True, ) -> pd.DataFrame: """Extract comprehensive petrophysical fluid limits and contacts per well and per zone: - Gas limits & contacts: GUT, GDT, GOC, GWC @@ -2667,15 +2670,26 @@ def extract_fluid_contacts_and_limits( & (grp["_VSH_CALC"] <= vsh_cutoff) ].sort_values(by="_DEPTH_CALC") - if len(valid) == 0: + # Petrophysical Sand Validation: + # A zone without minimum reservoir sand cannot contain or produce fluids. + # Eliminates non-reservoir shale/tuff/marl formations (e.g. Sele, Balder Tuff, Montrose shale). + if len(valid) < min_sand_samples: continue depths = valid["_DEPTH_CALC"].to_numpy() - sws = valid["_SW_CALC"].to_numpy() + sand_thickness = float(np.max(depths) - np.min(depths)) + if sand_thickness < min_sand_thickness and len(valid) < 10: + continue + sws = valid["_SW_CALC"].to_numpy() is_hc = sws < sw_pay_cutoff is_water = sws >= sw_water_cutoff + # If require_pay is True, do not register contacts for 100% wet / non-pay zones + # (prevents auto-setting WUT at the top of purely wet zones where no hydrocarbon exists) + if require_pay and not np.any(is_hc): + continue + # Gas vs Oil separation if gas indicator available if has_gas_curve: gas_col = "GAS_FLAG" if "GAS_FLAG" in valid.columns else "IS_GAS" @@ -2731,18 +2745,42 @@ def extract_fluid_contacts_and_limits( "remarks": f"Proved Oil Down To at {odt:.1f} m TVDss in {zone_val or 'reservoir'} ({well_val})", }) - # 3. Water Up To (WUT) - note: WDT is explicitly omitted - if np.any(is_water): - wut = float(np.min(depths[is_water])) - records.append({ - "well_name": well_val, - "zone": zone_val, - "fault_block": fb_val, - "contact_type": "WUT", - "depth": wut, - "source": "Log Evaluation (100% Sw water leg)", - "remarks": f"Proved Water Up To at {wut:.1f} m TVDss in {zone_val or 'reservoir'}", - }) + # Lowest confirmed hydrocarbon base (ODT or GDT) + hc_base = None + if np.any(is_oil): + hc_base = float(np.max(depths[is_oil])) + elif np.any(is_gas): + hc_base = float(np.max(depths[is_gas])) + + # 3. Water Up To (WUT) - shallowest proved water in the basal water leg + # Note: WDT is explicitly omitted + if hc_base is not None: + # When hydrocarbon is present in the zone, WUT is the shallowest proved 100% water BELOW the HC column + basal_water_mask = is_water & (depths >= hc_base) + if np.any(basal_water_mask): + wut = float(np.min(depths[basal_water_mask])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "WUT", + "depth": wut, + "source": "Log Evaluation (100% Sw water leg below HC)", + "remarks": f"Proved Water Up To at {wut:.1f} m TVDss in {zone_val or 'reservoir'} (below ODT {hc_base:.1f}m)", + }) + else: + # 100% wet reservoir interval without confirmed hydrocarbon pay (only reached if require_pay=False) + if np.any(is_water): + wut = float(np.min(depths[is_water])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "WUT", + "depth": wut, + "source": "Log Evaluation (100% Water-bearing Sand)", + "remarks": f"Proved Water-bearing Sand top at {wut:.1f} m TVDss in {zone_val or 'reservoir'} (aquifer interval)", + }) # 4. Inferred Discrete Contacts (GOC, GWC, OWC) if np.any(is_gas) and np.any(is_oil): diff --git a/tests/test_fluid_contacts_and_limits.py b/tests/test_fluid_contacts_and_limits.py index 0787723d..3c42be14 100644 --- a/tests/test_fluid_contacts_and_limits.py +++ b/tests/test_fluid_contacts_and_limits.py @@ -254,3 +254,115 @@ def test_fdp_report_saturation_contacts_rendering(): assert "Proved Oil Column:" in rendered assert "Free Water Level (FWL):" in rendered assert "WDT" not in rendered + + +def test_extract_fluid_contacts_wut_never_shallower_than_out(): + """Verify that when a wet interval exists at the top of a zone (shallower than OUT), + WUT is picked strictly from the basal water leg beneath the hydrocarbon accumulation.""" + depths = np.linspace(2800, 2900, 21) # 2800, 2805, ..., 2900 + sw = np.ones_like(depths) + # 2800 - 2820: wet sand (Sw = 1.0) + # 2825 - 2860: clean oil pay (Sw = 0.20) -> OUT = 2825, ODT = 2860 + # 2865 - 2900: basal water leg (Sw = 1.0) -> WUT = 2865 + for i, d in enumerate(depths): + if d < 2825: + sw[i] = 1.00 + elif d <= 2860: + sw[i] = 0.20 + else: + sw[i] = 1.00 + + df = pd.DataFrame({ + "WELL_NAME": ["Well-TopWet"] * len(depths), + "ZONES": ["Balmoral Sandstone"] * len(depths), + "TVDSS": depths, + "SWT": sw, + "PHIT": [0.22] * len(depths), + "VSH": [0.05] * len(depths), + }) + + contacts = extract_fluid_contacts_and_limits(df=df) + types = contacts["contact_type"].tolist() + + assert "OUT" in types + assert "ODT" in types + assert "WUT" in types + out_row = contacts[contacts["contact_type"] == "OUT"].iloc[0] + odt_row = contacts[contacts["contact_type"] == "ODT"].iloc[0] + wut_row = contacts[contacts["contact_type"] == "WUT"].iloc[0] + + assert out_row["depth"] == 2825.0 + assert odt_row["depth"] == 2860.0 + # WUT MUST be at 2865.0 (below ODT), NOT at 2800.0 or 2820.0 (above OUT)! + assert wut_row["depth"] == 2865.0 + assert wut_row["depth"] >= odt_row["depth"] + assert wut_row["depth"] > out_row["depth"] + + +def test_extract_fluid_contacts_no_sand_and_no_pay_skips_zone(): + """Verify that: + 1. A shale zone with NO reservoir sand (e.g. Sele / Balder Tuff) registers ZERO contacts. + 2. A clean sand zone with NO hydrocarbon pay (100% wet sand) registers ZERO contacts by default, + avoiding phantom WUT lines at the top of the formation. + 3. A pay zone in the same well registers valid contacts.""" + # Build 3 zones in one well: + # Zone 1: 'Sele' (Pure Shale: VSH = 0.85, PHIT = 0.02) + # Zone 2: 'Andrew Sandstone' (Clean Sand, 100% Wet: VSH = 0.08, PHIT = 0.22, SWT = 1.00) + # Zone 3: 'Balmoral Sandstone' (Oil Reservoir: VSH = 0.05, PHIT = 0.25, SWT = 0.20 in pay, 1.00 in water leg) + z1_depths = np.linspace(2700, 2750, 11) # Sele shale + z2_depths = np.linspace(2760, 2800, 9) # Andrew wet sand + z3_depths = np.linspace(2810, 2880, 15) # Balmoral oil reservoir + + d_all = np.concatenate([z1_depths, z2_depths, z3_depths]) + zones_all = ( + ["Sele"] * len(z1_depths) + + ["Andrew Sandstone"] * len(z2_depths) + + ["Balmoral Sandstone"] * len(z3_depths) + ) + + vsh_all = ( + [0.85] * len(z1_depths) + + [0.08] * len(z2_depths) + + [0.05] * len(z3_depths) + ) + phit_all = ( + [0.02] * len(z1_depths) + + [0.22] * len(z2_depths) + + [0.25] * len(z3_depths) + ) + sw_all = ( + [1.0] * len(z1_depths) + + [1.0] * len(z2_depths) + + [0.20 if d <= 2840 else 1.0 for d in z3_depths] + ) + + df = pd.DataFrame({ + "WELL_NAME": ["Well-MultiZone"] * len(d_all), + "ZONES": zones_all, + "TVDSS": d_all, + "SWT": sw_all, + "PHIT": phit_all, + "VSH": vsh_all, + }) + + contacts = extract_fluid_contacts_and_limits(df=df, require_pay=True) + + # 1. Sele (no sand) must have ZERO contacts + sele_contacts = contacts[contacts["zone"] == "Sele"] + assert len(sele_contacts) == 0, f"Expected 0 contacts in shale zone Sele, found: {sele_contacts}" + + # 2. Andrew Sandstone (clean sand, but 100% wet / no pay) must have ZERO contacts + andrew_contacts = contacts[contacts["zone"] == "Andrew Sandstone"] + assert len(andrew_contacts) == 0, f"Expected 0 contacts in 100% wet sand Andrew, found: {andrew_contacts}" + + # 3. Balmoral Sandstone (oil reservoir) must have OUT, ODT, WUT, OWC + balm_contacts = contacts[contacts["zone"] == "Balmoral Sandstone"] + assert len(balm_contacts) > 0 + balm_types = balm_contacts["contact_type"].tolist() + assert "OUT" in balm_types + assert "ODT" in balm_types + assert "WUT" in balm_types + assert "OWC" in balm_types + assert "WDT" not in balm_types + + From 30fcbbe67bf0e812147c99c2088b6b4017d991ff Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 17:44:36 +0800 Subject: [PATCH 05/10] improve parameter saving & loading for individual zones --- .../modules/MultiWellRockTyping.svelte | 19 ++- .../components/modules/MultiWellShf.svelte | 23 ++-- .../components/modules/MultiWellSwirr.svelte | 9 ++ .../src/lib/state/moduleParams.svelte.ts | 29 ++++- .../src/lib/state/projectData.svelte.ts | 105 +++++++++++++++-- .../src/lib/state/projectParams.svelte.ts | 110 ++++++++++++++++-- 6 files changed, 265 insertions(+), 30 deletions(-) diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellRockTyping.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellRockTyping.svelte index 1d54e062..1489846f 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellRockTyping.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellRockTyping.svelte @@ -4,6 +4,8 @@ import NumericScrubber from '$lib/components/workbench/NumericScrubber.svelte'; import ParameterSection from '$lib/components/workbench/ParameterSection.svelte'; import SavedParamsIndicator from '$lib/components/workbench/SavedParamsIndicator.svelte'; + import { appStatus } from '$lib/state/status.svelte'; + import { zoneSelectionLabel } from '$lib/state/projectParams.svelte'; import { projectData, type DrtCutoffMethod, @@ -436,6 +438,8 @@ : undefined }; projectData.markMultiWellComputed('rock-typing'); + // Auto-persist cutoffs and parameters for active zone selection + void projectData.persistProjectParams([], ['rock-typing']); if (workbench.wellId && Array.isArray(rt.log_well_names) && Array.isArray(rt.log_depths)) { const lDepths: number[] = []; @@ -1065,14 +1069,25 @@ {/each}
-
+
+
diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte index c55fd30e..7d66dd0b 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte @@ -120,8 +120,12 @@ ); } } - if (!projectData.shfResult && projectData.swirrResult) { - projectData.recomputeScalFitsFromPlugs(false); + if (!projectData.shfResult) { + if (projectData.swirrResult) { + projectData.recomputeScalFitsFromPlugs(false); + } else { + void projectData.ensureSwirrDataLoaded(); + } } }); @@ -159,9 +163,8 @@ try { const sp = projectData.shfParams; const jSatMode = sp.jSatMode ?? 'swn'; - // Restored SHF fits (from saved project params) are used once on the next non-reset run + // Restored SHF fits (from saved project params) are used on non-reset runs const saved = projectData.shfSavedFits; - projectData.shfSavedFits = null; const restoredFits = !resetFits && saved && @@ -217,6 +220,8 @@ }; if (markDirty) { projectData.markMultiWellComputed('shf'); + // Auto-persist SHF parameters & fits for the active zone selection + void projectData.persistProjectParams([], ['shf']); } // Live-merge SHF curve into active well so LogTrackCanvas Track 6 updates in real time! @@ -677,7 +682,11 @@ ? 'J = a · Sw^(-b)' : 'J = a · Swn^(-b)' : meta.formula} - {@const fitEntries = Object.entries(projectData.shfResult?.fits ?? {})} + {@const activeFits = + projectData.shfResult?.fits && Object.keys(projectData.shfResult.fits).length > 0 + ? projectData.shfResult.fits + : (projectData.shfSavedFits?.fits ?? {})} + {@const fitEntries = Object.entries(activeFits)}
@@ -741,7 +750,7 @@
{:else}
- No SCAL curve fits available yet. Fit Swirr & J/Pc curves in the [SCAL] SCAL & Rel-Perm standalone module, or run Compute SHF to auto-fit from project SCAL data. + No SCAL curve fits cached yet. Click Compute SHF to auto-fit curves directly from project SCAL plugs and compute Saturation Height across all wells.
{/if}
@@ -785,7 +794,7 @@ } onclick={saveShf} title={projectData.hasPendingMultiWell('shf') - ? 'Save computed SHF curves across all project wells' + ? 'Save computed SHF curves (and any pending upstream Rock Type & Swirr curves) across all project wells' : 'Compute or adjust SHF parameters first to enable saving'} class="h-7 rounded-sm border border-[var(--border-hairline)] bg-[var(--bg-surface)] px-3 text-xs font-semibold text-[var(--text-primary)] hover:bg-[var(--bg-subtle)] disabled:cursor-not-allowed disabled:opacity-45" > diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte index 4f60c576..8f2fb523 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellSwirr.svelte @@ -1,4 +1,5 @@ + +
+ +
+
+ +
+ + +
+ + +
+ + +
+
+ + +
+ + + {#if isMultiWell && (activeMultiModule === 'rock-typing' || activeMultiModule === 'rt-classifier')} + + {/if} + + {#if isMultiWell && (activeMultiModule === 'swirr-corr' || activeMultiModule === 'shf')} + + {/if} + + {#if autoContactsInfo.autoCount > 0} + + {/if} +
+
+ + + {#if confirmDeleteContacts} +
+
+ ⚠ Delete Auto-Identified Contacts: + + Are you sure you want to delete all {autoContactsInfo.autoCount} auto-generated + contacts across {autoContactsInfo.wellsCount} well(s)? Manually imported contacts will remain + safe. + +
+
+ + +
+
+ {/if} + + +
+ {#if tableEntries.length === 0} +
+
⚙
+

No saved parameter presets found

+

+ Parameters are saved automatically on Compute/Save, keyed by zone selection or depth interval. + You can reset the active step to regional default presets using the toolbar above. +

+ {#if filterScope === 'active'} + + {/if} +
+ {:else} + + + + + + + + + + + + {#each tableEntries as row (row.module + '::' + row.key)} + + + + + + + + {/each} + +
Step / ModuleZone / ScopeSaved Parameters SummarySaved AtActions
+
+ + {row.moduleLabel} +
+
+ + {row.label} + + + {formatParamSummary(row.params)} + + {fmtSavedAt(row.saved_at)} + + +
+ {/if} +
+ + +
+
+ {tableEntries.length} saved preset(s) displayed + {#if isMultiWell} + · Scope: Field / Multi-Well + {:else} + · Scope: Well ({wellData.header?.well_name || workbench.wellId || 'Active'}) + {/if} +
+
+ Regional Presets: Clastic / Sandstone standard ($m=2.0, n=2.0, \Phi_{`N`}/\rho_B$ SSC endpoints) +
+
+
diff --git a/quick_pp/app/frontend/src/lib/components/workbench/SavedParamsIndicator.svelte b/quick_pp/app/frontend/src/lib/components/workbench/SavedParamsIndicator.svelte index 0855cde1..44e62573 100644 --- a/quick_pp/app/frontend/src/lib/components/workbench/SavedParamsIndicator.svelte +++ b/quick_pp/app/frontend/src/lib/components/workbench/SavedParamsIndicator.svelte @@ -84,14 +84,22 @@ for {activeLabel} {/if} - - {#if r.matched && r.savedAt}{r.savedAt}{/if} +
+ {#if r.matched && r.savedAt}{r.savedAt}{/if} {#if r.count > 0} - {r.count} saved {/if} - + +
{/each}
diff --git a/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts b/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts index bb257046..7c14c412 100644 --- a/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts @@ -6,8 +6,89 @@ * window, falling back to `well` when no filter is active. Saved parameters are restored * automatically on exact selection-key match when the well is loaded or the selection changes. */ +import { deleteWellModuleParams } from '$lib/api/client'; import { workbench } from './workbench.svelte'; +export const SINGLE_WELL_REGIONAL_DEFAULTS: Record> = { + 'qaqc-badhole': { + useCali: true, + caliThreshold: 2.0, + autoBitsize: true, + useDrift: true, + driftWindowM: 50.0, + driftThresholdSigma: 2.5, + useDensityWashout: false, + caliSpikeThreshold: 0.5 + }, + 'qaqc-env': { + bitsizeDefault: 8.5, + mudWeight: 9.5, + surfaceTemp: 75.0, + tempGradient: 1.5, + salinityPpm: 30000.0 + }, + 'litho-poro': { + litho: { + model: 'ssc', + porosityMethod: 'den', + logCorrectionMode: 'none', + grClean: 20.0, + grClay: 120.0, + drySandNphi: -0.02, + drySandRhob: 2.65, + dryClayNphi: 0.44, + dryClayRhob: 2.78, + fluidNphi: 1.0, + fluidRhob: 1.0, + badholeTreatment: 'interpolate' + }, + ts: { + cleanSandPoint: [-0.02, 2.65], + pureShalePoint: [0.44, 2.78], + fluidPoint: [1.0, 1.0], + useInSaturation: false, + useInPerm: false + } + }, + perm: { + method: 'choo', + m: 2.0, + A: 1000000, + coatesA: 10000.0, + coatesB: 2.0, + coatesC: 2.0, + useTsInPerm: false + }, + saturation: { + method: 'waxman_smits', + rwMode: 'salinity_temp', + measSystem: 'metric', + a: 1.0, + m: 2.0, + n: 2.0, + rwVal: 0.05, + waterSalinity: 30000, + fwl: 0, + useTsInSaturation: false + }, + scal: { + model: 'let', + swirr: 0.15, + sor: 0.2, + l_w: 2.0, + e_w: 1.0, + t_w: 1.0, + l_o: 2.0, + e_o: 1.0, + t_o: 1.0 + }, + ressum: { + vclCutoff: 0.4, + phieCutoff: 0.08, + swCutoff: 0.6 + } +}; + export type ParamModuleKey = | 'qaqc-badhole' | 'qaqc-env' @@ -340,6 +421,89 @@ class ModuleParamsState { this.status = {}; this.savedCounts = {}; } + + getRegionalDefaults(module: ParamModuleKey): Record { + return clone(SINGLE_WELL_REGIONAL_DEFAULTS[module]); + } + + /** Reset the current active reactive parameters in the workbench to regional default presets. */ + resetToRegionalDefaults(module: ParamModuleKey) { + const defs = this.getRegionalDefaults(module); + REGISTRY[module].set(defs); + } + + /** Delete a specific saved preset for a module & selection key in the well config & backend. */ + async deleteSavedEntry( + projectId: string | number, + wellName: string, + module: ParamModuleKey, + key: string, + wellConfig?: Record + ): Promise { + try { + await deleteWellModuleParams(projectId, wellName, module, key); + if (wellConfig?.module_params?.[module]?.[key]) { + delete wellConfig.module_params[module][key]; + } + this.applyForCurrentSelection(wellConfig); + return true; + } catch { + return false; + } + } + + /** Delete all saved presets for a module across all intervals in the well. */ + async deleteAllForModule( + projectId: string | number, + wellName: string, + module: ParamModuleKey, + wellConfig?: Record + ): Promise { + try { + await deleteWellModuleParams(projectId, wellName, module); + if (wellConfig?.module_params?.[module]) { + delete wellConfig.module_params[module]; + } + this.applyForCurrentSelection(wellConfig); + return true; + } catch { + return false; + } + } + + /** Extract a flat list of all saved entries for convenient tabular presentation. */ + getAllSavedEntries(wellConfig?: Record): Array<{ + module: ParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + params: Record; + }> { + const mp = ((wellConfig?.module_params || {}) as ModuleParamsMap); + const rows: Array<{ + module: ParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + params: Record; + }> = []; + for (const m of PARAM_MODULE_KEYS) { + const modMap = mp[m] || {}; + for (const [k, e] of Object.entries(modMap)) { + rows.push({ + module: m, + moduleLabel: PARAM_MODULE_LABELS[m], + key: k, + label: e.label || k, + saved_at: e.saved_at || '', + params: e.params || {} + }); + } + } + return rows; + } } export const moduleParams = new ModuleParamsState(); diff --git a/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts b/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts index 9e4f422a..1dab053e 100644 --- a/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts @@ -1,4 +1,4 @@ -import { API_BASE, api, authHeaders, deleteJson, getJson, patchJson, postJson } from '$lib/api/client'; +import { API_BASE, api, authHeaders, deleteFluidContacts, deleteJson, deleteProjectModuleParams, getJson, patchJson, postJson } from '$lib/api/client'; import type { ProjectSummaryResponse, WellSummaryItem } from '$lib/api/schema'; import { appStatus } from './status.svelte'; import { wellData } from './wellData.svelte'; @@ -1089,6 +1089,90 @@ class ProjectDataState { } } + /** Reset rock typing and classifier results and saved parameters to defaults. */ + async resetRockTyping(): Promise { + this.rockTypingResult = null; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'rock-typing', this.projectModuleParams); + await projectParams.deleteAllForModule(workbench.projectId, 'rt-classifier', this.projectModuleParams); + } + appStatus.success('Rock typing and classifier reset to regional defaults'); + } + + /** Reset SCAL plug fittings, Swirr correlation, and SWOF simulation deck. */ + async resetScalFits(): Promise { + this.swirrConfig.referencePcPsi = 100.0; + this.swirrConfig.extractionMode = 'cutoff_pc'; + this.swirrConfig.equationType = 'rqi_power'; + this.swirrConfig.fitScope = 'global'; + this.swirrConfig.excludedPlugIds = []; + this.swirrConfig.excludedScalPointIds = []; + this.swirrResult = null; + workbench.scalResult = null; + this.focusedScalPlugId = null; + this.selectedScalPointIds = []; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'swirr-corr', this.projectModuleParams); + } + appStatus.success('SCAL & Swirr fittings reset to regional defaults'); + } + + /** Reset SHF model parameters, Leverett-J fits, and FWL datum. */ + async resetShf(): Promise { + this.shfParams.model = 'leverett_j'; + this.shfParams.jSatMode = 'swn'; + this.shfParams.swirrSource = 'lab_min'; + this.shfParams.unit = 'metric'; + this.shfParams.fwl = 2500.0; + this.shfParams.fwlByZone = {}; + this.shfParams.ift = 30.0; + this.shfParams.theta = 30.0; + this.shfParams.gw = 1.05; + this.shfParams.ghc = 0.8; + this.shfOptParams.solvePerZone = false; + this.shfResult = null; + this.shfSavedFits = null; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'shf', this.projectModuleParams); + } + appStatus.success('SHF model and FWL reset to regional defaults'); + } + + /** Delete auto-generated / optimized fluid contacts across project wells. */ + async deleteAutoContacts(zone?: string): Promise { + const pid = workbench.projectId; + if (!pid) return 0; + try { + const res = await deleteFluidContacts(pid, { autoOnly: true, zone }); + const count = res?.deleted_count ?? 0; + const autoNames = new Set(['GUT', 'GDT', 'OUT', 'ODT', 'WUT']); + wellData.contacts = wellData.contacts.filter((c: any) => { + if (zone && c.zone && c.zone !== zone) return true; + const cName = c.name || c.type; + if (autoNames.has(cName)) return false; + const src = String(c.source || '').toLowerCase(); + if (src.includes('log evaluation') || src.includes('calculated') || src.includes('optimizer') || src.includes('fpt')) return false; + return true; + }); + for (const w of this.wells) { + if (Array.isArray(w.fluid_contacts)) { + w.fluid_contacts = w.fluid_contacts.filter((c: any) => { + const cName = c.name || c.type; + if (autoNames.has(cName)) return false; + const src = String(c.source || '').toLowerCase(); + if (src.includes('log evaluation') || src.includes('calculated') || src.includes('optimizer') || src.includes('fpt')) return false; + return true; + }); + } + } + appStatus.success(`Deleted ${count} auto-generated fluid contact(s) across project`); + return count; + } catch (e: any) { + appStatus.error(`Failed to delete fluid contacts: ${e?.message ?? e}`); + return 0; + } + } + private static readonly MULTI_WELL_PIPELINE: MultiWellSaveModule[] = [ 'rock-typing', 'perm-transform', diff --git a/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts b/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts index f849cf45..6c535601 100644 --- a/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts @@ -6,8 +6,48 @@ * wells it was computed on. Saved parameters are restored on zone match (exact or hierarchical bundle match) * when the project loads or the zone selection changes while in multi-well scope. */ +import { deleteProjectModuleParams } from '$lib/api/client'; import { workbench } from './workbench.svelte'; +export const MULTI_WELL_REGIONAL_DEFAULTS: Record> = { + 'mw-qaqc': { + normalizeCurves: ['GR', 'NPHI', 'RHOB'], + badholeThreshold: 2.0, + activeCurve: 'GR' + }, + 'rock-typing': { + method: 'rqi_fzi', + numClusters: 3, + cutoffs: [1.0, 3.0] + }, + 'perm-transform': { + modelType: 'power', + defaultA: 1000.0, + defaultB: 3.5 + }, + 'swirr-corr': { + referencePcPsi: 100.0, + method: 'power', + defaultA: 0.15, + defaultB: -0.5 + }, + shf: { + modelType: 'leverett_j', + iftDynesCm: 30.0, + contactAngleDeg: 0.0, + fwlTvdss: 2500.0 + }, + 'rt-classifier': { + classifierType: 'rf', + featureCurves: ['NPHI', 'RHOB', 'GR', 'RT'] + }, + 'ml-training': { + algorithm: 'gbm', + targetCurve: 'PHIT', + featureCurves: ['GR', 'RT', 'NPHI', 'RHOB'] + } +}; + export type ProjectParamModuleKey = | 'mw-qaqc' | 'rock-typing' @@ -225,6 +265,88 @@ class ProjectParamsState { this.status = {}; this.savedCounts = {}; } + + getRegionalDefaults(module: ProjectParamModuleKey): Record { + return JSON.parse(JSON.stringify(MULTI_WELL_REGIONAL_DEFAULTS[module] ?? {})); + } + + /** Delete a specific saved preset for a multi-well module & zone key. */ + async deleteSavedEntry( + projectId: string | number, + module: ProjectParamModuleKey, + key: string, + modMap?: ProjectModuleParamsMap + ): Promise { + try { + await deleteProjectModuleParams(projectId, module, key); + if (modMap?.[module]?.[key]) { + delete modMap[module]![key]; + } + this.setCounts(modMap ?? {}); + this.loadGeneration++; + return true; + } catch { + return false; + } + } + + /** Delete all saved presets for a multi-well module across all zones. */ + async deleteAllForModule( + projectId: string | number, + module: ProjectParamModuleKey, + modMap?: ProjectModuleParamsMap + ): Promise { + try { + await deleteProjectModuleParams(projectId, module); + if (modMap?.[module]) { + delete modMap[module]; + } + this.setCounts(modMap ?? {}); + this.loadGeneration++; + return true; + } catch { + return false; + } + } + + /** Extract a flat list of all saved multi-well entries for table presentation. */ + getAllSavedEntries(modMap?: ProjectModuleParamsMap): Array<{ + module: ProjectParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + wellCount: number | null; + params: Record; + }> { + const mp = modMap || {}; + const rows: Array<{ + module: ProjectParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + wellCount: number | null; + params: Record; + }> = []; + for (const m of PROJECT_PARAM_MODULE_KEYS) { + const mEntries = mp[m] || {}; + for (const [k, e] of Object.entries(mEntries)) { + rows.push({ + module: m, + moduleLabel: PROJECT_PARAM_MODULE_LABELS[m], + key: k, + label: e.label || k, + saved_at: e.saved_at || '', + wellCount: Array.isArray(e.selection?.well_names) + ? e.selection.well_names.length + : null, + params: e.params || {} + }); + } + } + return rows; + } } export const projectParams = new ProjectParamsState(); diff --git a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts index 9c0ff224..13e37eb0 100644 --- a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts @@ -549,6 +549,15 @@ class WorkbenchState { // Right pane view mode rightPaneTab = $state<'crossplot' | 'grid' | 'split'>('split'); + /** Sub-tab within the right bottom data dock: 'data' (Step Data / QC) or 'params' (Parameters & Fits). */ + rightGridSubTab = $state<'data' | 'params'>('data'); + + openParametersTab() { + this.rightGridSubTab = 'params'; + if (this.rightPaneTab === 'crossplot') { + this.rightPaneTab = 'split'; + } + } crossplotTemplateOverride = $state< | 'auto' | 'nphi-rhob' diff --git a/quick_pp/app/frontend/src/routes/+page.svelte b/quick_pp/app/frontend/src/routes/+page.svelte index 336146b6..c01ba9cb 100644 --- a/quick_pp/app/frontend/src/routes/+page.svelte +++ b/quick_pp/app/frontend/src/routes/+page.svelte @@ -10,6 +10,7 @@ import CompactDataGrid, { type GridColumn } from '$lib/components/workbench/CompactDataGrid.svelte'; + import ParametersManagementPanel from '$lib/components/workbench/ParametersManagementPanel.svelte'; import ReportPreviewCanvas from '$lib/components/workbench/ReportPreviewCanvas.svelte'; import InspectorQaqc from '$lib/components/modules/InspectorQaqc.svelte'; @@ -167,6 +168,12 @@ replaceState(url, {}); }); + let totalSavedParamsCount = $derived( + workbench.scope === 'multi-well' + ? Object.values(projectParams.savedCounts).reduce((a, b) => a + (b ?? 0), 0) + : Object.values(moduleParams.savedCounts).reduce((a, b) => a + (b ?? 0), 0) + ); + // Dynamic columns & rows for the Bottom-Right Quick Zonal / Context Data Grid let rightGridConfig = $derived.by( (): { title: string; columns: GridColumn[]; rows: Record[] } => { @@ -970,16 +977,49 @@ {:else if workbench.rightPaneTab === 'grid'}
- {rightGridConfig.title} +
+ + +
- + {#if workbench.rightGridSubTab === 'data'} + + {:else} + + {/if}
{:else} @@ -1003,21 +1043,49 @@
- {rightGridConfig.title} - - {rightGridConfig.rows.length} rows - +
+ + +
- + {#if workbench.rightGridSubTab === 'data'} + + {:else} + + {/if}
diff --git a/tests/test_params_and_contacts_reset.py b/tests/test_params_and_contacts_reset.py new file mode 100644 index 00000000..b250e152 --- /dev/null +++ b/tests/test_params_and_contacts_reset.py @@ -0,0 +1,162 @@ +from contextlib import contextmanager +import pytest +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy.pool import StaticPool + +from quick_pp.app.backend.services.ancillary import delete_fluid_contacts_project +from quick_pp.app.backend.services.workspace import ( + WorkspaceModuleParamsEntry, + WorkspaceModuleParamsRequest, + workspace_delete_project_module_params, + workspace_delete_well_module_params, + workspace_save_project_module_params, + workspace_save_module_params, +) +from quick_pp.database.models import Base +from quick_pp.database.objects import Project, Well + + +@pytest.fixture +def test_db(monkeypatch): + engine = create_engine( + "sqlite:///:memory:", + connect_args={"check_same_thread": False}, + poolclass=StaticPool, + ) + Base.metadata.create_all(engine) + Session = sessionmaker(bind=engine) + + @contextmanager + def _get_test_db(): + session = Session() + try: + yield session + session.commit() + except Exception: + session.rollback() + raise + finally: + session.close() + + monkeypatch.setattr("quick_pp.app.backend.services.ancillary.get_db", _get_test_db) + monkeypatch.setattr("quick_pp.app.backend.services.workspace.get_db", _get_test_db) + monkeypatch.setattr("quick_pp.app.backend.services.workspace.is_demo_mode", lambda: False) + + session = Session() + proj = Project(db_session=session, name="test_reset_proj") + proj.save() + + w1 = Well(db_session=session, project_id=proj.project_id, name="W-1", uwi="U-1") + w1.save() + + # Add 1 manual contact and 2 auto contacts + w1.add_fluid_contacts( + [ + { + "name": "OWC", + "depth": 2400.0, + "source": "Log Evaluation (Midpoint)", + "contact_type": "OWC", + }, + { + "name": "ODT", + "depth": 2390.0, + "source": "Log Evaluation (Lowest proved oil)", + "contact_type": "ODT", + }, + { + "name": "Manual_FWL", + "depth": 2410.0, + "source": "Handover Import", + "contact_type": "FWL", + }, + ] + ) + + yield {"session": session, "project_id": proj.project_id, "well_name": w1.name} + + +def test_delete_auto_fluid_contacts(test_db): + pid = test_db["project_id"] + + # Delete auto-only contacts + res = delete_fluid_contacts_project(project_id=pid, auto_only=True) + assert res["deleted_count"] == 2 + assert "W-1" in res["wells_affected"] + + # Check remaining contacts in well + session = test_db["session"] + proj = Project.load(session, project_id=pid) + w = proj.get_well("W-1") + remaining = w.get_fluid_contacts().to_dict(orient="records") + assert len(remaining) == 1 + assert remaining[0]["name"] == "Manual_FWL" + + +def test_project_module_params_delete(test_db): + pid = test_db["project_id"] + + # Save 2 entries + req = WorkspaceModuleParamsRequest( + entries=[ + WorkspaceModuleParamsEntry( + module="rock-typing", + key="all", + selection={"zones": None}, + label="All zones", + params={"num_clusters": 3}, + ), + WorkspaceModuleParamsEntry( + module="swirr-corr", + key="zone:A", + selection={"zones": ["A"]}, + label="Zone A", + params={"referencePcPsi": 100.0}, + ), + ] + ) + saved = workspace_save_project_module_params(pid, req) + assert "rock-typing" in saved["module_params"] + assert "swirr-corr" in saved["module_params"] + + # Delete specific module + del_res = workspace_delete_project_module_params(pid, module="rock-typing") + assert "rock-typing" not in del_res["module_params"] + assert "swirr-corr" in del_res["module_params"] + + # Delete all remaining + del_all = workspace_delete_project_module_params(pid) + assert del_all["module_params"] == {} + + +def test_well_module_params_delete(test_db): + pid = test_db["project_id"] + wn = test_db["well_name"] + + req = WorkspaceModuleParamsRequest( + entries=[ + WorkspaceModuleParamsEntry( + module="litho-poro", + key="well", + selection={"zones": None}, + label="Whole well", + params={"model": "ssc"}, + ), + WorkspaceModuleParamsEntry( + module="saturation", + key="zone:A", + selection={"zones": ["A"]}, + label="Zone A", + params={"m": 2.0, "n": 2.0}, + ), + ] + ) + saved = workspace_save_module_params(pid, wn, req) + assert "litho-poro" in saved["module_params"] + assert "saturation" in saved["module_params"] + + # Delete saturation + del_res = workspace_delete_well_module_params(pid, wn, module="saturation") + assert "saturation" not in del_res["module_params"] + assert "litho-poro" in del_res["module_params"] From 1dd4a09691393fb45218f9528786ba8c42f67628 Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 21:03:47 +0800 Subject: [PATCH 08/10] improve plotting in fdp report generator --- quick_pp/app/backend/services/report.py | 107 ++++++++++++++++-- .../components/modules/InspectorReport.svelte | 78 +++++++++---- quick_pp/plotter/report_plots.py | 21 ++-- 3 files changed, 164 insertions(+), 42 deletions(-) diff --git a/quick_pp/app/backend/services/report.py b/quick_pp/app/backend/services/report.py index 6665e746..408004ad 100644 --- a/quick_pp/app/backend/services/report.py +++ b/quick_pp/app/backend/services/report.py @@ -590,8 +590,27 @@ def build_report_context( raw_upper_map[k] for k in priority_order if k in raw_upper_map ] + [c for c in raw_curves if str(c).upper() not in priority_order] - w_df = _ensure_evaluated_curves(w_df, w_name) - unfiltered_dfs.append(w_df) + has_real_logs = any( + str(c).upper() + in ( + "GR", + "RT", + "RD", + "RDEP", + "RILD", + "LLD", + "NPHI", + "RHOB", + "PHIT", + "PHIE", + "PERM", + "SWT", + ) + for c in raw_curves + ) + if has_real_logs: + w_df = _ensure_evaluated_curves(w_df, w_name) + unfiltered_dfs.append(w_df) d_s = pd.to_numeric(w_df["DEPTH"], errors="coerce").dropna() d_min = float(d_s.min()) if not d_s.empty else 0.0 @@ -2876,17 +2895,85 @@ def _generate_all_report_plots( if "10_appendix_cpi" in enabled_section_ids: cpi_list = [] cpi_candidate_wells = [] + candidate_scores = [] if not full_df.empty and "WELL_NAME" in full_df.columns: for wn in well_names: w_sub = full_df[full_df["WELL_NAME"].astype(str) == str(wn)] - if not w_sub.empty: - has_eval = False - for c_col in ("PHIT", "PHIE", "VCLAY", "SWT", "PERM"): - if c_col in w_sub.columns and w_sub[c_col].notna().any(): - has_eval = True - break - if has_eval: - cpi_candidate_wells.append(wn) + if w_sub.empty or "DEPTH" not in w_sub.columns: + continue + + # Target reservoir interval subset + d_min, d_max = report_plots._resolve_target_depth_window( + w_sub, target_zones=target_zones, max_span=480.0 + ) + d_col = pd.to_numeric(w_sub["DEPTH"], errors="coerce") + w_win = w_sub[(d_col >= d_min) & (d_col <= d_max)] + if w_win.empty: + w_win = w_sub + + # Count valid evaluated samples in target window + phi_cnt = int( + pd.to_numeric( + w_win.get("PHIT", w_win.get("PHIE", pd.Series(dtype=float))), + errors="coerce", + ) + .notna() + .sum() + ) + swt_cnt = int( + pd.to_numeric( + w_win.get("SWT", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + perm_cnt = int( + pd.to_numeric( + w_win.get("PERM", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + gr_cnt = int( + pd.to_numeric( + w_win.get("GR", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + rt_cnt = int( + pd.to_numeric( + w_win.get("RT", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + + # Strict requirement: well must have evaluated porosity and at least one other key curve (RT, SWT, or PERM) + if phi_cnt < 10 or (swt_cnt < 5 and perm_cnt < 5 and rt_cnt < 5): + # Empty or uninterpreted in target window -> skip! + continue + + has_core = ( + "CPORE" in w_win.columns + and pd.to_numeric(w_win["CPORE"], errors="coerce").notna().sum() > 0 + ) or ( + "PORO_CORE" in w_win.columns + and pd.to_numeric(w_win["PORO_CORE"], errors="coerce").notna().sum() + > 0 + ) + score = ( + (10000 if has_core else 0) + + phi_cnt + + swt_cnt + + perm_cnt + + gr_cnt + + rt_cnt + ) + candidate_scores.append((score, wn)) + + candidate_scores.sort(key=lambda x: x[0], reverse=True) + cpi_candidate_wells = [wn for _, wn in candidate_scores] for wn in cpi_candidate_wells[:4]: cpi_url = report_plots.generate_well_cpi_plot( diff --git a/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte b/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte index 2a9d2d55..aba0a276 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte @@ -46,17 +46,34 @@ []) as AnomalyFlagItem[] ); + let evaluatedWells = $derived( + projectData.wells.filter((w) => { + const c = w.curves || (w as any).available_curves || []; + return c.length > 0 && (w.row_count ?? (w as any).depth_count ?? 1) > 0; + }) + ); + + function getTargetWells(): string[] { + if (isSingleWell) { + return workbench.wellId + ? [workbench.wellId] + : (evaluatedWells[0]?.name ? [evaluatedWells[0].name] : projectData.wells.slice(0, 1).map((w) => w.name)); + } + if (projectData.selectedWellNames.length > 0) { + const evalSet = new Set(evaluatedWells.map((w) => w.name)); + const validSel = projectData.selectedWellNames.filter((name) => evalSet.has(name)); + return validSel.length > 0 ? validSel : evaluatedWells.map((w) => w.name); + } + return evaluatedWells.length > 0 + ? evaluatedWells.map((w) => w.name) + : projectData.wells.map((w) => w.name); + } + async function runPreRenderAudit() { if (!workbench.projectId) return; isRunningPreCheck = true; try { - const targetWells = isSingleWell - ? workbench.wellId - ? [workbench.wellId] - : projectData.wells.slice(0, 1).map((w) => w.name) - : projectData.selectedWellNames.length > 0 - ? projectData.selectedWellNames - : projectData.wells.map((w) => w.name); + const targetWells = getTargetWells(); const params = new URLSearchParams(); if (targetWells.length > 0) { params.set('wells', targetWells.join(',')); @@ -104,10 +121,14 @@ } function toggleWellSelection(wellName: string) { - if (projectData.selectedWellNames.includes(wellName)) { - projectData.selectedWellNames = projectData.selectedWellNames.filter((w) => w !== wellName); + const currentSel = + projectData.selectedWellNames.length > 0 + ? [...projectData.selectedWellNames] + : evaluatedWells.map((w) => w.name); + if (currentSel.includes(wellName)) { + projectData.selectedWellNames = currentSel.filter((w) => w !== wellName); } else { - projectData.selectedWellNames = [...projectData.selectedWellNames, wellName]; + projectData.selectedWellNames = [...currentSel, wellName]; } } @@ -119,13 +140,7 @@ const reportType: ReportType = isSingleWell ? 'SINGLE_WELL_CPI' : workbench.reportConfig.reportType; - const targetWells = isSingleWell - ? workbench.wellId - ? [workbench.wellId] - : projectData.wells.slice(0, 1).map((w) => w.name) - : projectData.selectedWellNames.length > 0 - ? projectData.selectedWellNames - : projectData.wells.map((w) => w.name); + const targetWells = getTargetWells(); const defaultTitle = isSingleWell ? `Well ${targetWells[0] || ''} — Petrophysical CPI & Evaluation Dossier` @@ -378,7 +393,7 @@ title={isSingleWell ? 'Well Net Pay Cutoffs' : 'Evaluated Wells & Pay Cutoffs'} badge={isSingleWell ? 'Pay Flags' - : `${projectData.selectedWellNames.length || projectData.wells.length} wells`} + : `${projectData.selectedWellNames.length || evaluatedWells.length} wells`} > {#if !isSingleWell && projectData.wells.length > 0}
@@ -390,10 +405,10 @@ ·
@@ -1027,7 +1028,7 @@
- quick_pp SSOT Technical Reference v0.3.3 + quick_pp SSOT Technical Reference v0.3.4
Press Esc to close
diff --git a/quick_pp/app/frontend/src/lib/state/auth.svelte.ts b/quick_pp/app/frontend/src/lib/state/auth.svelte.ts index 1977d3aa..33c16e6f 100644 --- a/quick_pp/app/frontend/src/lib/state/auth.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/auth.svelte.ts @@ -63,7 +63,7 @@ export interface AuditLogItem { class AuthState { user = $state(null); token = $state(null); - appVersion = $state('0.3.3'); + appVersion = $state('0.3.4'); isInitializing = $state(true); uamModalOpen = $state(false); accountModalOpen = $state(false); diff --git a/quick_pp/database/objects.py b/quick_pp/database/objects.py index eac03e99..0c05f82f 100644 --- a/quick_pp/database/objects.py +++ b/quick_pp/database/objects.py @@ -100,7 +100,9 @@ def normalize_fpt_test_quality(value: Any) -> str: """Normalize a pre-test quality flag to VALID / TIGHT / SUPERCHARGED / LOST_SEAL / EXCLUDED.""" if value is None: return "VALID" - key = re.sub(r"[^A-Z_]", "", str(value).strip().upper().replace(" ", "_").replace("-", "_")) + key = re.sub( + r"[^A-Z_]", "", str(value).strip().upper().replace(" ", "_").replace("-", "_") + ) if not key: return "VALID" return _FPT_QUALITY_ALIASES.get(key, "VALID") @@ -1325,14 +1327,17 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() v_vals = vals_arr[valid_mask].tolist() sio.writelines( - f"{cid}\t{d}\t{v}\t\\N\n" for d, v in zip(v_depths, v_vals, strict=False) + f"{cid}\t{d}\t{v}\t\\N\n" + for d, v in zip(v_depths, v_vals, strict=False) ) buffered_rows += len(v_depths) else: @@ -1344,7 +1349,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) for d, v in zip(v_depths, v_vals, strict=False): safe_v = ( v.replace("\\", "\\\\") @@ -1377,7 +1384,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue @@ -1401,7 +1410,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) batch_buffer.extend( { "curve_id": cid, @@ -1489,7 +1500,10 @@ def update_data(self, data: pd.DataFrame): new_curves.append(mnemonic) else: orm_curve.data_type = data_type - if str(mnemonic).upper() in sparse_core_mnemonics and len(data.index) > 0: + if ( + str(mnemonic).upper() in sparse_core_mnemonics + and len(data.index) > 0 + ): valid_idx = [float(d) for d in data.index if pd.notna(d)] if valid_idx: full_span_delete_curves[orm_curve.curve_id] = ( @@ -1565,14 +1579,17 @@ def update_data(self, data: pd.DataFrame): dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() v_vals = vals_arr[valid_mask].tolist() sio.writelines( - f"{cid}\t{d}\t{v}\t\\N\n" for d, v in zip(v_depths, v_vals, strict=False) + f"{cid}\t{d}\t{v}\t\\N\n" + for d, v in zip(v_depths, v_vals, strict=False) ) total_inserted += len(v_depths) else: @@ -1584,7 +1601,9 @@ def update_data(self, data: pd.DataFrame): if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) for d, v in zip(v_depths, v_vals, strict=False): safe_v = ( v.replace("\\", "\\\\") @@ -1742,9 +1761,7 @@ def add_curve_data( if mappings: self.db_session.bulk_insert_mappings(ORMCurveData, mappings) self.db_session.flush() - logger.debug( - f"Added {len(mappings)} data points for curve '{mnemonic}'" - ) + logger.debug(f"Added {len(mappings)} data points for curve '{mnemonic}'") return if is_numeric: @@ -2130,9 +2147,7 @@ def export_handover_package( def __str__(self): return f"Well: {self.name} (ID: {self.well_id}, UWI: {self.uwi})" - def add_formation_tops( - self, tops: List[Dict[str, Any]], replace: bool = False - ): + def add_formation_tops(self, tops: List[Dict[str, Any]], replace: bool = False): """Adds or updates a list of formation tops for the well. Args: @@ -2246,33 +2261,45 @@ def add_fluid_contacts( for contact_data in contacts or []: if not isinstance(contact_data, dict): continue - raw_c_type = str( - contact_data.get("contact_type") or contact_data.get("type") or "" - ).strip().upper() + raw_c_type = ( + str(contact_data.get("contact_type") or contact_data.get("type") or "") + .strip() + .upper() + ) raw_name = str(contact_data.get("name") or "").strip() zone = str(contact_data.get("zone") or "").strip() or None - fb = str( - contact_data.get("fault_block") - or contact_data.get("faultblock") - or contact_data.get("block") - or "" - ).strip() or None + fb = ( + str( + contact_data.get("fault_block") + or contact_data.get("faultblock") + or contact_data.get("block") + or "" + ).strip() + or None + ) source = str(contact_data.get("source") or "").strip() or None - remarks = str( - contact_data.get("remarks") - or contact_data.get("remark") - or contact_data.get("comment") - or "" - ).strip() or None + remarks = ( + str( + contact_data.get("remarks") + or contact_data.get("remark") + or contact_data.get("comment") + or "" + ).strip() + or None + ) c_type = raw_c_type if not c_type and raw_name: - parts = [p.strip().upper() for p in raw_name.replace(":", " - ").split(" - ")] + parts = [ + p.strip().upper() for p in raw_name.replace(":", " - ").split(" - ") + ] c_type = parts[-1] if parts else raw_name.upper() c_name = raw_name if not c_name: - c_name = f"{zone} - {c_type}" if (zone and c_type) else (c_type or "CONTACT") + c_name = ( + f"{zone} - {c_type}" if (zone and c_type) else (c_type or "CONTACT") + ) elif zone and zone.lower() not in c_name.lower() and c_type: c_name = f"{zone} - {c_type}" @@ -2301,9 +2328,9 @@ def add_fluid_contacts( self.delete_fluid_contacts(zones=clean_zones) else: # Default clean-slate behavior: wipe existing contacts for the zones present in this payload - zones_to_clean = set( + zones_to_clean = { item["zone"] for item in parsed_contacts.values() if item.get("zone") - ) + } if zones_to_clean: self.delete_fluid_contacts(zones=list(zones_to_clean)) # Also clean matching names for any unassigned general contacts (zone is None) @@ -2345,7 +2372,15 @@ def get_fluid_contacts(self) -> pd.DataFrame: except Exception: pass contacts = self._orm_well.fluid_contacts - cols = ["name", "depth", "zone", "contact_type", "fault_block", "source", "remarks"] + cols = [ + "name", + "depth", + "zone", + "contact_type", + "fault_block", + "source", + "remarks", + ] if not contacts: return pd.DataFrame(columns=cols) @@ -2366,9 +2401,7 @@ def get_fluid_contacts(self) -> pd.DataFrame: logger.debug(f"Retrieved {len(df)} fluid contacts for well '{self.name}'.") return df - def add_pressure_tests( - self, tests: List[Dict[str, Any]], replace: bool = False - ): + def add_pressure_tests(self, tests: List[Dict[str, Any]], replace: bool = False): """Adds or updates a list of formation pressure tests for the well. Args: @@ -2413,9 +2446,7 @@ def _opt_float(v: Any) -> Optional[float]: if not (np.isfinite(d_val) and np.isfinite(p_val)): continue uom = str( - test_data.get("pressure_uom") - or test_data.get("pressure_unit") - or "psi" + test_data.get("pressure_uom") or test_data.get("pressure_unit") or "psi" ).strip() t_uom = str( test_data.get("temperature_uom") @@ -2425,7 +2456,8 @@ def _opt_float(v: Any) -> Optional[float]: remarks_raw = test_data.get("remarks", test_data.get("remark")) remarks = ( str(remarks_raw).strip() - if remarks_raw is not None and str(remarks_raw).strip() not in ("", "nan", "None") + if remarks_raw is not None + and str(remarks_raw).strip() not in ("", "nan", "None") else None ) dedup_tests[d_val] = { @@ -2556,7 +2588,11 @@ def get_pressure_tests(self) -> pd.DataFrame: } for t in tests ] - df = pd.DataFrame(data, columns=cols).sort_values(by="depth").reset_index(drop=True) + df = ( + pd.DataFrame(data, columns=cols) + .sort_values(by="depth") + .reset_index(drop=True) + ) # Keep missing optional measurements as None (JSON-safe) rather than NaN for c in ("mobility", "permeability", "temperature", "tvdss"): df[c] = df[c].astype(object).where(pd.notna(df[c]), None) @@ -2639,7 +2675,9 @@ def add_perforations( continue top_raw = perf_data.get( "top", - perf_data.get("top_depth", perf_data.get("top_md", perf_data.get("depth"))), + perf_data.get( + "top_depth", perf_data.get("top_md", perf_data.get("depth")) + ), ) bot_raw = perf_data.get( "bottom", @@ -2661,14 +2699,18 @@ def add_perforations( if b_val < t_val: t_val, b_val = b_val, t_val p_name = str( - perf_data.get("name") - or perf_data.get("perf_name") - or f"Perf-{idx + 1}" + perf_data.get("name") or perf_data.get("perf_name") or f"Perf-{idx + 1}" ).strip() if not p_name: p_name = f"Perf-{idx + 1}" - desc_raw = perf_data.get("description", perf_data.get("remark", perf_data.get("comment"))) - p_desc = str(desc_raw).strip() if desc_raw is not None and str(desc_raw).strip() != "" else None + desc_raw = perf_data.get( + "description", perf_data.get("remark", perf_data.get("comment")) + ) + p_desc = ( + str(desc_raw).strip() + if desc_raw is not None and str(desc_raw).strip() != "" + else None + ) dedup_perfs[(p_name, t_val, b_val)] = { "name": p_name, "top": t_val, @@ -2801,9 +2843,7 @@ def add_core_sample_with_measurements( if not orm_sample: # Ensure depth_val is unique in 32-bit REAL precision across existing_samples - existing_f32_depths = { - float(np.float32(s.depth)) for s in existing_samples - } + existing_f32_depths = {float(np.float32(s.depth)) for s in existing_samples} while float(np.float32(depth_val)) in existing_f32_depths: depth_val = round(depth_val + 0.002, 4) @@ -2923,9 +2963,15 @@ def replace_rca_samples(self, samples: List[Dict[str, Any]]): # If another plug in a different core interval shares the same plug number (e.g. "1"), # disambiguate by depth so distinct plugs are never overwritten - if sname in dedup_by_name and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2: + if ( + sname in dedup_by_name + and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2 + ): sname = f"{sname}@{depth_val:.2f}m" - if sname in dedup_by_name and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2: + if ( + sname in dedup_by_name + and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2 + ): sname = f"{sname}_{idx + 1}" # Avoid uq_well_id_sample_depth collision in 32-bit REAL precision when multiple plugs snap to the same log depth @@ -3014,10 +3060,7 @@ def replace_rca_samples(self, samples: List[Dict[str, Any]]): # Clear all existing RCA measurements for this well, and remove any core sample that has no SCAL data for orm_sample in list(self._orm_well.core_samples): orm_sample.measurements.clear() - if ( - not orm_sample.capillary_pressure_data - and not orm_sample.relperm_data - ): + if not orm_sample.capillary_pressure_data and not orm_sample.relperm_data: self.db_session.delete(orm_sample) self.db_session.flush() @@ -3066,9 +3109,15 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal "description": row.get("description"), }, ) - if entry.get("porosity") is None and row.get("porosity", row.get("cpore")) is not None: + if ( + entry.get("porosity") is None + and row.get("porosity", row.get("cpore")) is not None + ): entry["porosity"] = row.get("porosity", row.get("cpore")) - if entry.get("permeability") is None and row.get("permeability", row.get("cperm")) is not None: + if ( + entry.get("permeability") is None + and row.get("permeability", row.get("cperm")) is not None + ): entry["permeability"] = row.get("permeability", row.get("cperm")) if entry.get("raw_depth") is None and row.get("raw_depth") is not None: entry["raw_depth"] = row.get("raw_depth") @@ -3105,9 +3154,7 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal for sname, sdata in grouped.items(): pc_points = sdata["pc_data"] orm_existing = self.db_session.scalar( - select(ORMCoreSample).filter_by( - well_id=self.well_id, sample_name=sname - ) + select(ORMCoreSample).filter_by(well_id=self.well_id, sample_name=sname) ) if append and orm_existing and orm_existing.capillary_pressure_data: existing_pts = [ @@ -3135,7 +3182,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal "value": float(m.value), "unit": m.unit, } - if sdata.get("porosity") is not None and str(sdata.get("porosity")).strip() != "": + if ( + sdata.get("porosity") is not None + and str(sdata.get("porosity")).strip() != "" + ): try: existing_meas_map["CPORE"] = { "property_name": "CPORE", @@ -3144,7 +3194,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal } except (TypeError, ValueError): pass - if sdata.get("permeability") is not None and str(sdata.get("permeability")).strip() != "": + if ( + sdata.get("permeability") is not None + and str(sdata.get("permeability")).strip() != "" + ): try: existing_meas_map["CPERM"] = { "property_name": "CPERM", @@ -3153,7 +3206,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal } except (TypeError, ValueError): pass - if sdata.get("raw_depth") is not None and str(sdata.get("raw_depth")).strip() != "": + if ( + sdata.get("raw_depth") is not None + and str(sdata.get("raw_depth")).strip() != "" + ): try: existing_meas_map["RAW_DEPTH"] = { "property_name": "RAW_DEPTH", diff --git a/quick_pp/plotter/report_plots.py b/quick_pp/plotter/report_plots.py index 73235dfe..044ea966 100644 --- a/quick_pp/plotter/report_plots.py +++ b/quick_pp/plotter/report_plots.py @@ -612,22 +612,79 @@ def generate_nd_crossplot( rad = math.radians(float(silt_line_angle)) p_silt = (D[0] + math.cos(rad), D[1] + math.sin(rad)) silt_inter = line_intersection((A, C), (D, p_silt)) - if silt_inter is not None and np.isfinite(silt_inter[0]) and np.isfinite(silt_inter[1]): + if ( + silt_inter is not None + and np.isfinite(silt_inter[0]) + and np.isfinite(silt_inter[1]) + ): B = (float(silt_inter[0]), float(silt_inter[1])) else: - drysilt_NPHI = 1.0 - 1.68 * math.tan(float(silt_line_angle - 90) * math.pi / 180.0) + drysilt_NPHI = 1.0 - 1.68 * math.tan( + float(silt_line_angle - 90) * math.pi / 180.0 + ) B = (drysilt_NPHI, 2.68) # Plot SSC ternary model lines - ax1.plot([D[0], A[0]], [D[1], A[1]], color="#1e40af", lw=1.3, label=f"Sand Line ({A[1]:.2f} g/cc)") - ax1.plot([D[0], B[0]], [D[1], B[1]], color="#16a34a", lw=1.3, linestyle="-", label=f"Silt Line ({silt_line_angle:.0f}°)") - ax1.plot([D[0], C[0]], [D[1], C[1]], color="#9333ea", lw=1.2, linestyle="--", label=f"Clay Line ({C[1]:.2f} g/cc)") - ax1.plot([A[0], C[0]], [A[1], C[1]], color="#0f172a", lw=1.4, linestyle="-", label="Rock Line") + ax1.plot( + [D[0], A[0]], + [D[1], A[1]], + color="#1e40af", + lw=1.3, + label=f"Sand Line ({A[1]:.2f} g/cc)", + ) + ax1.plot( + [D[0], B[0]], + [D[1], B[1]], + color="#16a34a", + lw=1.3, + linestyle="-", + label=f"Silt Line ({silt_line_angle:.0f}°)", + ) + ax1.plot( + [D[0], C[0]], + [D[1], C[1]], + color="#9333ea", + lw=1.2, + linestyle="--", + label=f"Clay Line ({C[1]:.2f} g/cc)", + ) + ax1.plot( + [A[0], C[0]], + [A[1], C[1]], + color="#0f172a", + lw=1.4, + linestyle="-", + label="Rock Line", + ) # Endpoint markers - ax1.scatter([A[0]], [A[1]], color="#eab308", marker="s", s=50, zorder=6, label=f"Dry Sand ({A[0]:.2f}, {A[1]:.2f})") - ax1.scatter([B[0]], [B[1]], color="#16a34a", marker="D", s=45, zorder=6, label=f"Dry Silt ({B[0]:.2f}, {B[1]:.2f})") - ax1.scatter([C[0]], [C[1]], color="#dc2626", marker="^", s=60, zorder=6, label=f"Dry Clay ({C[0]:.2f}, {C[1]:.2f})") + ax1.scatter( + [A[0]], + [A[1]], + color="#eab308", + marker="s", + s=50, + zorder=6, + label=f"Dry Sand ({A[0]:.2f}, {A[1]:.2f})", + ) + ax1.scatter( + [B[0]], + [B[1]], + color="#16a34a", + marker="D", + s=45, + zorder=6, + label=f"Dry Silt ({B[0]:.2f}, {B[1]:.2f})", + ) + ax1.scatter( + [C[0]], + [C[1]], + color="#dc2626", + marker="^", + s=60, + zorder=6, + label=f"Dry Clay ({C[0]:.2f}, {C[1]:.2f})", + ) else: # Fallback Standard Quartz / Calcite / Dolomite lithology lines phi_grid = np.linspace(0.0, 0.40, 50) @@ -720,9 +777,7 @@ def generate_nd_crossplot( fontsize=8.5, fontweight="bold", ) - ax2.set_ylabel( - "Log Total Porosity PHIT (v/v)", fontsize=8.5, fontweight="bold" - ) + ax2.set_ylabel("Log Total Porosity PHIT (v/v)", fontsize=8.5, fontweight="bold") ax2.set_title( f"Core-to-Log Calibration (R²={r2:.2f}, RMSE={rmse:.3f})", fontsize=9.0, @@ -773,7 +828,9 @@ def generate_nd_crossplot( label="Calcite Cement (2.71 g/cc)", ) ax3.set_xlim(2.52, 2.88) - ax3.set_xlabel("Matrix Grain Density ρ_ma (g/cc)", fontsize=8.5, fontweight="bold") + ax3.set_xlabel( + "Matrix Grain Density ρ_ma (g/cc)", fontsize=8.5, fontweight="bold" + ) ax3.set_ylabel("Sample Frequency", fontsize=8.5, fontweight="bold") ax3.set_title("Grain Density (ρ_ma) Matrix QC", fontsize=9.0, fontweight="bold") ax3.legend(loc="upper right", fontsize=7.0) @@ -922,6 +979,7 @@ def generate_rock_typing_perm_plot( cutoffs = sorted(float(c) for c in fzi_cutoffs if float(c) > 0) elif rock_types: import re + for rt in rock_types: fzi_r = str(rt.get("fzi_range", "")) nums = re.findall(r"[-+]?\d*\.\d+|\d+", fzi_r) @@ -952,7 +1010,7 @@ def generate_rock_typing_perm_plot( elif rt_code == n_rts: lbl = f"RT{rt_code} (FZI ≥ {cutoffs[-1]:.2f})" else: - lbl = f"RT{rt_code} ({cutoffs[rt_code-2]:.2f} ≤ FZI < {cutoffs[rt_code-1]:.2f})" + lbl = f"RT{rt_code} ({cutoffs[rt_code - 2]:.2f} ≤ FZI < {cutoffs[rt_code - 1]:.2f})" if np.any(mask): ax1.scatter( @@ -965,7 +1023,7 @@ def generate_rock_typing_perm_plot( ) # Plot explicit cutoff boundary curves so points and cutoffs align perfectly - for i, c_val in enumerate(cutoffs): + for _i, c_val in enumerate(cutoffs): k_cutoff = 1014.24 * (c_val**2) * (phi_line**3) / ((1.0 - phi_line) ** 2) ax1.plot( phi_line, @@ -997,7 +1055,9 @@ def generate_rock_typing_perm_plot( else pd.Series("Project", index=sub.index) ) wells_with_pts = well_col[valid].value_counts() - key_well = str(wells_with_pts.index[0]) if not wells_with_pts.empty else "Key Well" + key_well = ( + str(wells_with_pts.index[0]) if not wells_with_pts.empty else "Key Well" + ) well_mask = valid & (well_col == key_well) if not well_mask.any(): well_mask = valid @@ -1005,11 +1065,19 @@ def generate_rock_typing_perm_plot( w_sub = sub[well_mask].copy() if "DEPTH" in w_sub.columns and w_sub["DEPTH"].notna().any(): - w_sub = w_sub.dropna(subset=["DEPTH"]).sort_values("DEPTH").reset_index(drop=True) - w_p = _get_numeric_series(w_sub, "CPORE", "PHIE", "PHIT").to_numpy(dtype=float) + w_sub = ( + w_sub.dropna(subset=["DEPTH"]) + .sort_values("DEPTH") + .reset_index(drop=True) + ) + w_p = _get_numeric_series(w_sub, "CPORE", "PHIE", "PHIT").to_numpy( + dtype=float + ) w_p = np.where(w_p > 1.0, w_p / 100.0, w_p) w_k = _get_numeric_series(w_sub, "CPERM", "PERM").to_numpy(dtype=float) - d_vals = pd.to_numeric(w_sub["DEPTH"], errors="coerce").to_numpy(dtype=float) + d_vals = pd.to_numeric(w_sub["DEPTH"], errors="coerce").to_numpy( + dtype=float + ) dh = np.diff(d_vals, prepend=d_vals[0]) dh = np.where((dh > 0) & (dh < 5.0), dh, 0.1524) else: @@ -1024,7 +1092,9 @@ def generate_rock_typing_perm_plot( # Calculate Lorenz coefficient L try: - lorenz_val = max(0.0, min(1.0, float(np.trapz(kh_cum, phih_cum) - 0.5) * 2.0)) + lorenz_val = max( + 0.0, min(1.0, float(np.trapz(kh_cum, phih_cum) - 0.5) * 2.0) + ) except Exception: lorenz_val = 0.50 @@ -1170,7 +1240,11 @@ def generate_pickett_plot( # Panel 2: Water Saturation vs Depth & Free Water Level (FWL) depth = _get_numeric_series(sub, "TVDSS", "TVD", "DEPTH") - depth_label = "Depth TVDss (m)" if "TVDSS" in sub.columns else ("Depth TVD (m)" if "TVD" in sub.columns else "Depth (m)") + depth_label = ( + "Depth TVDss (m)" + if "TVDSS" in sub.columns + else ("Depth TVD (m)" if "TVD" in sub.columns else "Depth (m)") + ) swt = _get_numeric_series(sub, "SWT", "SW") shf = _get_numeric_series(sub, "SHF") if (depth.notna() & swt.notna()).any(): @@ -1270,11 +1344,13 @@ def generate_shf_j_function_plot( if rock_types and len(rock_types) >= 2: new_fits = [] for i, rt in enumerate(rock_types): - r_name = rt.get("name", f"RT{i+1}") + r_name = rt.get("name", f"RT{i + 1}") # Use specific J-fit coefficients if present a_p = float(rt.get("j_a", rt_fits[i % len(rt_fits)][1])) b_p = float(rt.get("j_b", rt_fits[i % len(rt_fits)][2])) - new_fits.append((r_name.split("—")[0].strip(), a_p, b_p, PALETTE[i % len(PALETTE)])) + new_fits.append( + (r_name.split("—")[0].strip(), a_p, b_p, PALETTE[i % len(PALETTE)]) + ) if new_fits: rt_fits = new_fits @@ -1293,7 +1369,10 @@ def generate_shf_j_function_plot( d_rho = max(gw_val - ghc_val, 0.05) sigma_cos = max(abs(ift_val * math.cos(math.radians(theta_val))), 1.0) - if fwl is not None and (sw_s.notna() & phi_s.notna() & k_s.notna() & d_s.notna()).any(): + if ( + fwl is not None + and (sw_s.notna() & phi_s.notna() & k_s.notna() & d_s.notna()).any() + ): v_mask = ( sw_s.notna() & (sw_s > 0.02) @@ -1371,8 +1450,16 @@ def generate_shf_j_function_plot( lo = float(scal.get("lo", 2.0)) eo = float(scal.get("eo", 1.5)) to = float(scal.get("to", 2.0)) - krw = krw0 * (lw * (se**ew)) / np.maximum(lw * (se**ew) + tw * ((1.0 - se) ** ew), 1e-9) - kro = kro0 * (lo * ((1.0 - se) ** eo)) / np.maximum(lo * ((1.0 - se) ** eo) + to * (se**eo), 1e-9) + krw = ( + krw0 + * (lw * (se**ew)) + / np.maximum(lw * (se**ew) + tw * ((1.0 - se) ** ew), 1e-9) + ) + kro = ( + kro0 + * (lo * ((1.0 - se) ** eo)) + / np.maximum(lo * ((1.0 - se) ** eo) + to * (se**eo), 1e-9) + ) krw_label = f"Krw LET (L={lw:.1f}, E={ew:.1f}, T={tw:.1f})" kro_label = f"Kro LET (L={lo:.1f}, E={eo:.1f}, T={to:.1f})" else: @@ -1383,8 +1470,16 @@ def generate_shf_j_function_plot( ax2.plot(sw, krw, color="#2563eb", lw=2.2, label=krw_label) ax2.plot(sw, kro, color="#059669", lw=2.2, label=kro_label) - ax2.axvline(swirr, color="#64748b", linestyle=":", lw=1.1, label=f"Swirr = {swirr:.2f}") - ax2.axvline(1.0 - sor, color="#dc2626", linestyle=":", lw=1.1, label=f"1 - Sor = {1.0 - sor:.2f}") + ax2.axvline( + swirr, color="#64748b", linestyle=":", lw=1.1, label=f"Swirr = {swirr:.2f}" + ) + ax2.axvline( + 1.0 - sor, + color="#dc2626", + linestyle=":", + lw=1.1, + label=f"1 - Sor = {1.0 - sor:.2f}", + ) ax2.set_xlim(0.0, 1.0) ax2.set_ylim(0.0, 1.0) ax2.set_xlabel("Water Saturation Sw (v/v)", fontsize=9, fontweight="bold") @@ -1449,7 +1544,9 @@ def generate_uncertainty_cutoff_plot( phie = _get_numeric_series(sub, "PHIE", "PHIT").to_numpy(dtype=float) vclay = _get_numeric_series(sub, "VCLAY").to_numpy(dtype=float) swt = _get_numeric_series(sub, "SWT", "SW").to_numpy(dtype=float) - n_wells = max(1, int(sub["WELL_NAME"].nunique()) if "WELL_NAME" in sub.columns else 1) + n_wells = max( + 1, int(sub["WELL_NAME"].nunique()) if "WELL_NAME" in sub.columns else 1 + ) valid_m = (phie > 0.0) & np.isfinite(phie) & np.isfinite(vclay) & np.isfinite(swt) if valid_m.any(): phie = phie[valid_m] @@ -1569,7 +1666,9 @@ def generate_uncertainty_cutoff_plot( color="#475569", ) ax2.set_xlabel( - "Mean Well Hydrocarbon Pore Thickness HCPT (m)", fontsize=9, fontweight="bold" + "Mean Well Hydrocarbon Pore Thickness HCPT (m)", + fontsize=9, + fontweight="bold", ) ax2.set_ylabel("Probability Density", fontsize=9, fontweight="bold") ax2.set_title( @@ -1624,7 +1723,9 @@ def generate_upscaling_comparison_plot( ) return _save_fig(fig, "fig_9_1_upscaling_plot.png", output_dir, url_prefix) - ymin, ymax = _resolve_target_depth_window(sub, target_zones=target_zones, max_span=480.0) + ymin, ymax = _resolve_target_depth_window( + sub, target_zones=target_zones, max_span=480.0 + ) d_col = pd.to_numeric(sub["DEPTH"], errors="coerce") sub_win = sub[(d_col >= ymin) & (d_col <= ymax)].copy() if sub_win.empty: @@ -1651,7 +1752,9 @@ def generate_upscaling_comparison_plot( "VCLAY": _get_numeric_series(sub_win, "VCLAY", "VSHALE"), "PHIT": _get_numeric_series(sub_win, "PHIE", "PHIT"), "SWT": _get_numeric_series(sub_win, "SWT", "SW", "SHF"), - "PERM": _get_numeric_series(sub_win, "PERM", "PERM_TRANS").clip(lower=1e-2), + "PERM": _get_numeric_series(sub_win, "PERM", "PERM_TRANS").clip( + lower=1e-2 + ), "ZONES": sub_win["ZONES"].fillna("Target").astype(str) if "ZONES" in sub_win.columns else "Target", @@ -1739,7 +1842,12 @@ def generate_upscaling_comparison_plot( fontsize=7.0, fontweight="bold", color="#0f172a", - bbox={"facecolor": "white", "alpha": 0.8, "edgecolor": "none", "pad": 1.0}, + bbox={ + "facecolor": "white", + "alpha": 0.8, + "edgecolor": "none", + "pad": 1.0, + }, ) ax1.set_ylim(ymax, ymin) @@ -1795,22 +1903,20 @@ def generate_well_cpi_plot( # Ensure strictly sorted monotonic depth index to prevent any zig-zag line rendering sub_well = ( - sub_well.dropna(subset=["DEPTH"]) - .sort_values(by="DEPTH") - .reset_index(drop=True) + sub_well.dropna(subset=["DEPTH"]).sort_values(by="DEPTH").reset_index(drop=True) ) sub_well = _trim_to_logged_interval(sub_well) # Auto-zoom to target reservoir window (CHK-06: vertical span <= 500 m) - d_min, d_max = _resolve_target_depth_window(sub_well, target_zones=zones, max_span=480.0) + d_min, d_max = _resolve_target_depth_window( + sub_well, target_zones=zones, max_span=480.0 + ) d_col = pd.to_numeric(sub_well["DEPTH"], errors="coerce") sub_full = sub_well[(d_col >= d_min) & (d_col <= d_max)].copy() if sub_full.empty: sub_full = sub_well.copy() sub_full = ( - sub_full.dropna(subset=["DEPTH"]) - .sort_values(by="DEPTH") - .reset_index(drop=True) + sub_full.dropna(subset=["DEPTH"]).sort_values(by="DEPTH").reset_index(drop=True) ) # Guard against uninterpreted / empty wells: verify valid curves exist in target window valid_curves = 0 @@ -1920,21 +2026,29 @@ def generate_well_cpi_plot( cp = np.where(cp > 1.0, cp / 100.0, cp) cm = np.isfinite(cp) & (cp > 0) if np.any(cm): - ax.scatter(cp[cm], d_full[cm], color="#dc2626", s=18, zorder=5, label="CPORE") + ax.scatter( + cp[cm], d_full[cm], color="#dc2626", s=18, zorder=5, label="CPORE" + ) ax.set_xlim(0.0, 0.50) ax.set_xlabel("Porosity Φ (v/v)", fontsize=8.5, fontweight="bold", color="#1d4ed8") ax.set_title("Track 4\nPorosity & Core", fontsize=8.5, fontweight="bold") # Track 5: Permeability & Core CPERM ax = axes[4] - perm = _get_numeric_series(sub, "PERM", "PERM_TRANS").clip(lower=1e-2).to_numpy(dtype=float) + perm = ( + _get_numeric_series(sub, "PERM", "PERM_TRANS") + .clip(lower=1e-2) + .to_numpy(dtype=float) + ) if np.isfinite(perm).any(): ax.plot(perm, d, color="#059669", lw=1.1, label="PERM") if "CPERM" in sub_full.columns: ck = _get_numeric_series(sub_full, "CPERM").to_numpy(dtype=float) km = np.isfinite(ck) & (ck > 0) if np.any(km): - ax.scatter(ck[km], d_full[km], color="#b91c1c", s=18, zorder=5, label="CPERM") + ax.scatter( + ck[km], d_full[km], color="#b91c1c", s=18, zorder=5, label="CPERM" + ) ax.set_xscale("log") ax.set_xlim(1e-2, 1e4) ax.set_xlabel("PERM (mD)", fontsize=8.5, fontweight="bold", color="#059669") @@ -1996,7 +2110,12 @@ def generate_well_cpi_plot( for idx in change_idx[:14]: z_depth = float(d[idx]) z_label = str(z_series.iloc[idx]).strip() - if not z_label or z_label.upper() in ("NAN", "MAIN RESERVOIR", "UNKNOWN", "ALL"): + if not z_label or z_label.upper() in ( + "NAN", + "MAIN RESERVOIR", + "UNKNOWN", + "ALL", + ): continue for a_i in axes: a_i.axhline(z_depth, color="k", linestyle="--", lw=0.85, alpha=0.85) diff --git a/quick_pp/saturation.py b/quick_pp/saturation.py index a36abebf..e8fdff8e 100644 --- a/quick_pp/saturation.py +++ b/quick_pp/saturation.py @@ -6,14 +6,14 @@ above free water level (SHF) calculations. """ +from typing import Dict, Optional + import matplotlib.pyplot as plt import matplotlib.ticker as ticker import numpy as np import pandas as pd from tqdm import tqdm -from typing import Any, Dict, List, Optional, Tuple, Union - from quick_pp import logger from quick_pp.utils import min_max_line, remove_outliers @@ -2581,14 +2581,37 @@ def extract_fluid_contacts_and_limits( min_sand_samples: int = 5, require_pay: bool = True, ) -> pd.DataFrame: - """Extract comprehensive petrophysical fluid limits and contacts per well and per zone: - - Gas limits & contacts: GUT, GDT, GOC, GWC - - Oil limits & contacts: OUT, ODT, OWC - - Water limits & contacts: WUT, FWL - Note: WDT is explicitly excluded as it carries no petrophysical utility. + """Extract comprehensive petrophysical fluid limits and contacts per well and per zone. + + Identifies and classifies fluid contacts and transition limits from log saturation profiles, + formation pressure tests (MDT/RFT/FPT), and zonal intervals: + - Gas limits & contacts: GUT (Gas Up To), GDT (Gas Down To), GOC (Gas-Oil Contact), GWC (Gas-Water Contact) + - Oil limits & contacts: OUT (Oil Up To), ODT (Oil Down To), OWC (Oil-Water Contact) + - Water limits & contacts: WUT (Water Up To), FWL (Free Water Level) + Note: WDT (Water Down To) is explicitly excluded as it carries no petrophysical utility. + + Args: + df (pd.DataFrame): Well log DataFrame containing depth, water saturation, porosity, and clay/shale curves. + depth_col (str, optional): Depth column name (TVDSS, TVD, or DEPTH). Defaults to "TVD". + sw_col (str, optional): Water saturation column name. Defaults to "SWT". + phit_col (str, optional): Total porosity column name. Defaults to "PHIT". + vsh_col (str, optional): Shale/clay volume column name. Defaults to "VSH". + well_col (str, optional): Well identifier column name. Defaults to "WELL_NAME". + zone_col (str, optional): Reservoir zonation column name. Defaults to "ZONES". + fault_block_col (str, optional): Fault block column name. Defaults to "FAULT_BLOCK". + pressure_df (pd.DataFrame, optional): Formation pressure test DataFrame with columns ['DEPTH', 'PRESSURE', 'FLUID_TYPE']. + fwl_dict (dict[str, float], optional): Optional dictionary mapping zone/well names to Free Water Level depths. + sw_pay_cutoff (float, optional): Maximum water saturation threshold for hydrocarbon pay. Defaults to 0.65. + sw_water_cutoff (float, optional): Minimum water saturation threshold for 100% water zone. Defaults to 0.90. + phit_cutoff (float, optional): Minimum porosity threshold for reservoir rock. Defaults to 0.05. + vsh_cutoff (float, optional): Maximum shale volume threshold for reservoir rock. Defaults to 0.50. + min_sand_thickness (float, optional): Minimum vertical net sand thickness required to declare a contact. Defaults to 1.0. + min_sand_samples (int, optional): Minimum consecutive reservoir samples. Defaults to 5. + require_pay (bool, optional): Whether to require active pay intervals when establishing contacts. Defaults to True. Returns: - pd.DataFrame: Columns ['well_name', 'zone', 'fault_block', 'contact_type', 'depth', 'source', 'remarks'] + pd.DataFrame: Table of detected fluid contacts and limits with columns: + ['well_name', 'zone', 'fault_block', 'contact_type', 'depth', 'source', 'remarks']. """ contact_cols = [ "well_name", diff --git a/scripts/README.md b/scripts/README.md index 56d157c9..ab5f2333 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -12,7 +12,8 @@ Running `python scripts/release.py --bump ` automatically updates: 3. [`CITATION.cff`](../CITATION.cff) (`version: `) 4. [`quick_pp/app/frontend/package.json`](../quick_pp/app/frontend/package.json) (`"version": ""`) 5. [`quick_pp/app/frontend/src/lib/state/auth.svelte.ts`](../quick_pp/app/frontend/src/lib/state/auth.svelte.ts) (`appVersion` fallback) -6. [`uv.lock`](../uv.lock) (via `uv lock`) +6. [`quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte`](../quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte) (`quick_pp SSOT Technical Reference v`) +7. [`uv.lock`](../uv.lock) (via `uv lock`) > **Note:** Remember to also add release notes for the new version in [`docs/community/changelog.md`](../docs/community/changelog.md). diff --git a/scripts/release.py b/scripts/release.py index 6eadeeb8..1af62b61 100644 --- a/scripts/release.py +++ b/scripts/release.py @@ -64,6 +64,10 @@ FRONTEND_DIR / "src" / "lib" / "state" / "auth.svelte.ts", r"(appVersion\s*=\s*\$state\(')([^']+)('\))", ), + "quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte": ( + FRONTEND_DIR / "src" / "lib" / "components" / "shell" / "WorkflowGuideSheet.svelte", + r"(quick_pp SSOT Technical Reference v)([^<]+)()", + ), "uv.lock": ( ROOT / "uv.lock", r'(name\s*=\s*"quick-pp"\r?\nversion\s*=\s*")([^"]+)(")', @@ -381,6 +385,15 @@ def main() -> None: if target.exists(): shutil.rmtree(target) + # Clean ephemeral database and report static files generated by tests or dev runs + for db_pattern in ("*.db*", "*.sqlite*"): + for f in (ROOT / "quick_pp").rglob(db_pattern): + if f.is_file(): + try: + f.unlink() + except Exception: + pass + # Ensure ephemeral report static outputs generated by tests are cleaned before building wheel reports_static_dir = ROOT / "quick_pp" / "app" / "backend" / "static" / "reports" if reports_static_dir.exists(): diff --git a/uv.lock b/uv.lock index d7274a4a..259e75b2 100644 --- a/uv.lock +++ b/uv.lock @@ -5272,7 +5272,7 @@ wheels = [ [[package]] name = "quick-pp" -version = "0.3.3" +version = "0.3.4" source = { editable = "." } dependencies = [ { name = "click" }, From fe90c1c620708fee2e0925cba9b8233ecf40480f Mon Sep 17 00:00:00 2001 From: imranfadhil Date: Tue, 3 Nov 2026 21:34:25 +0800 Subject: [PATCH 10/10] fix linting in tests --- tests/test_assistant_graceful_handling.py | 11 +++++------ tests/test_fluid_contacts_and_limits.py | 4 ++-- tests/test_params_and_contacts_reset.py | 3 ++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_assistant_graceful_handling.py b/tests/test_assistant_graceful_handling.py index 680dd063..03f686e0 100644 --- a/tests/test_assistant_graceful_handling.py +++ b/tests/test_assistant_graceful_handling.py @@ -1,10 +1,9 @@ -import pytest from quick_pp.app.backend.services.chat import ( - AgentChatRequest, - _plan_native_tool_calls, - _handle_unrecognized_native_prompt, PRESET_QUICK_ACTIONS, SYSTEM_PROMPT, + AgentChatRequest, + _handle_unrecognized_native_prompt, + _plan_native_tool_calls, ) @@ -29,12 +28,12 @@ def test_unrecognized_native_prompt_highlights_low_confidence_and_quick_actions( provider="native_mcp", ) reply, actions = _handle_unrecognized_native_prompt(req) - + # Must highlight answer confidence assert "Answer Confidence: Low" in reply assert "Why the agent is not confident in this answer" in reply assert "Balmoral" in reply or "balmoral" in reply - + # Must return all 7 preset quick actions assert len(actions) == 7 assert len(actions) == len(PRESET_QUICK_ACTIONS) diff --git a/tests/test_fluid_contacts_and_limits.py b/tests/test_fluid_contacts_and_limits.py index 1d3fe338..6b4e0d43 100644 --- a/tests/test_fluid_contacts_and_limits.py +++ b/tests/test_fluid_contacts_and_limits.py @@ -2,9 +2,9 @@ import pandas as pd import pytest -from quick_pp.saturation import extract_fluid_contacts_and_limits -from quick_pp.database.db_connector import DBConnector from quick_pp.database import objects as db_objects +from quick_pp.database.db_connector import DBConnector +from quick_pp.saturation import extract_fluid_contacts_and_limits def test_extract_fluid_contacts_oil_water_column(): diff --git a/tests/test_params_and_contacts_reset.py b/tests/test_params_and_contacts_reset.py index b250e152..6434d358 100644 --- a/tests/test_params_and_contacts_reset.py +++ b/tests/test_params_and_contacts_reset.py @@ -1,4 +1,5 @@ from contextlib import contextmanager + import pytest from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -10,8 +11,8 @@ WorkspaceModuleParamsRequest, workspace_delete_project_module_params, workspace_delete_well_module_params, - workspace_save_project_module_params, workspace_save_module_params, + workspace_save_project_module_params, ) from quick_pp.database.models import Base from quick_pp.database.objects import Project, Well