"""Calculate masked local-contrast profiles without claiming physical depth."""
import cv2
import numpy as np

def focus_profiles(planes,masks,sigma=3):
 """Mean smoothed squared Laplacian per plane and 2D mask.

 The provided masks may be oracle references; the caller must label their
 provenance. Contrast peaks can reflect background, registration or artifacts.
 """
 if not isinstance(planes,np.ndarray) or planes.ndim!=3 or planes.shape[0]<1 or not np.issubdtype(planes.dtype,np.number):raise ValueError('Expected numeric Z×Y×X planes')
 if not np.isfinite(planes).all() or not isinstance(sigma,(int,float)) or sigma<=0:raise ValueError('Invalid values or sigma')
 if not isinstance(masks,list) or not masks:raise ValueError('Expected masks')
 for m in masks:
  if not isinstance(m,np.ndarray) or m.dtype!=np.bool_ or m.shape!=planes.shape[1:] or not m.any():raise ValueError('Expected nonempty matching boolean masks')
 energy=[cv2.GaussianBlur(cv2.Laplacian(p.astype('float32'),cv2.CV_32F,ksize=3)**2,(0,0),sigma) for p in planes]
 return [[float(e[m].mean()) for e in energy] for m in masks]
