graintrace.user_data_class#
User-facing dataclasses for similarity metrics, weighting, and rare criteria.
- class graintrace.user_data_class.SimilarityMetric(name, feature_cols, func, dist_edges=None)[source]#
Bases:
objectA named feature-space distance metric and its required columns.
- Variables:
name (str) – Label for the metric. Used only in error messages, the progress line, and the graph-stage checkpoint metadata, where resuming compares it against the stored value. It is not a column stem and does not appear in any output frame.
feature_cols (List[str]) –
Column names the metric consumes, in the order its distance function expects them. They must exist in the stage’s input frame.
The graph stage’s reduction emits one
<col>_meanper feature column (pluscluster_id,n,x,y,z, and a<prefix>_norm_meanholding the mean per-point Frobenius norm whenever the feature columns contain a complete<prefix>_11…<prefix>_33set). The indicator stage that reads that frame must therefore name the_meancolumns, not these – which is why a two-stage Nye-norm run ends up selecting onnye_tensor_norm_mean_mean: the graph stage emitsnye_tensor_norm_mean, and the indicator stage’s own reduction suffixes it again.func (Callable[[numpy.ndarray, numpy.ndarray], float]) – Scalar distance between two feature vectors,
func(u, v) -> float. Used by the hierarchical indicator stage.dist_edges (Callable[[numpy.ndarray, numpy.ndarray], numpy.ndarray] | None) – Optional vectorized form,
dist_edges(X, edges) -> (E,), evaluating one distance per graph edge. When present the graph stage uses it instead of loopingfunc; both must agree numerically.
- Parameters:
- class graintrace.user_data_class.WeightConfig(mode='inverse', eps=1e-08, sigma=None, sigma_auto=None, power=2.0)[source]#
Bases:
objectConfiguration for converting edge distances into graph weights.
A larger weight means “more similar”, so every mode is a decreasing function of the edge distance
d.- Variables:
mode (str) – Weight kernel, one of
"inverse","rbf","exp"or"log_inv". Any other value raisesValueError.eps (float) – Regularizer that keeps a zero distance finite. Read by
"inverse"(\(1/(d + \varepsilon)\)) and"log_inv"(\(-\log(d + \varepsilon)\)); ignored otherwise.sigma (float | None) – Length scale of
"rbf"(\(e^{-(d/\sigma)^{p}}\)) and"exp"(\(e^{-d/\sigma}\)), in the units of the metric – degrees for a misorientation metric, MPa for a stress metric. WhenNone, it is estimated from the edge distances viasigma_auto.sigma_auto (Dict[str, Any] | None) – Estimation spec used only when
modeis"rbf"or"exp"andsigmaisNone. Only the"quantile"key is read;sigmabecomes that quantile of the surviving edge distances.power (float) – The exponent \(p\) of
"rbf"only;"inverse","exp"and"log_inv"ignore it.
- Parameters:
- class graintrace.user_data_class.RareCriteria(selector=None, size_quantile=0.05, min_size=1, max_rare=None)[source]#
Bases:
objectSelect rare merged clusters via
selector, or by the size quantile.selectorand the quantile fields are alternatives: whenselectoris given it decides entirely which clusters are rare, and the three fields below are not consulted.- Variables:
selector (Callable[[pandas.DataFrame], numpy.ndarray | List[int] | List[str]] | None) –
selector(df) -> ids, taking the merged per-cluster stats frame and returning the ids of the rare clusters. The helpers ingraintrace.rare_criteria_selection_libraryhave this shape.size_quantile (float) – Without a
selector, clusters in the bottom quantile of cluster sizenare rare – the built-in notion of “rare” is small, not extreme. Use aselectorto rank by a field value.min_size (int) – Absolute floor on cluster size, applied as well as the quantile, so single-point noise is not reported as a rare event.
max_rare (int | None) – Optional cap on how many clusters are returned, smallest first.
Nonemeans no cap.
- Parameters: