"""Self Repr.-1 -- Part C of the Twinned FCC walkthrough (OPTIONAL).
"Ng(boundary)/Ng(total)" for the parent domain, moving-rectangle sub-domain
extraction, morphological-parameter distributions for parent vs. subsets,
and representativeness scoring between them.
Disabled by default -- this is a deeper, optional representativeness study,
not required for the main pipeline to proceed. Kept short here; see
selfrepr_morphology.py / representativeness_metrics.py directly for the
full feature set (per-(metric,parameter) qualification thresholds,
publication-style plots, etc.) if you want more than this notebook's
teaching-level pass.
"""
from upxo.charops.mchar import boundary_grain_fraction
from upxo.pxtal.twinned_simple_3d.selfrepr_morphology import compute_morphological_parameters
from upxo.pxtal.twinned_simple_3d.subsetting_2d import (
generate_subset_tiles_2d, crop_lgi_2d, subset_centroid_um)
from upxo.pxtal.twinned_simple_3d.representativeness_metrics import compute_all_selected
[docs]
def boundary_grain_ratio(rg):
"""Ng(boundary)/Ng(total) for the parent EBSD domain.
Returns
-------
dict : {'n_boundary', 'n_internal', 'n_total', 'ratio'}
"""
return boundary_grain_fraction(rg.lfi_ebsd)
[docs]
def compute_morphology(rg, subsets, params=('area', 'perimeter')):
"""Morphological parameters for the parent domain and every subset
(same set of parameters, same units, for a fair comparison).
Returns
-------
dict : {'parent': {param: {gid: value}}, 'subsets': {(i, j): {param: {gid: value}}}}
"""
step_um = getattr(rg, 'ebsd_step', 1.0) or 1.0
parent = compute_morphological_parameters(rg.lfi_ebsd, prop_ebsd=rg.prop_ebsd, voxel_size=step_um)
subset_results = {
s['index']: compute_morphological_parameters(s['lgi'], prop_ebsd=None, voxel_size=step_um)
for s in subsets
}
return {'parent': {p: parent[p] for p in params},
'subsets': {idx: {p: r[p] for p in params} for idx, r in subset_results.items()}}
[docs]
def assess_representativeness(morph_results, metrics=('ks_sim', 'wasserstein_sim')):
"""For every subset and every parameter, scores how representative
that subset's distribution is of the parent's, using the requested
metrics (all "higher = more representative", bounded to [0, 1]).
Returns
-------
dict : {(subset_index, param): {metric: score}}
"""
assessment = {}
for idx, subset_params in morph_results['subsets'].items():
for param, subset_vals in subset_params.items():
ref_vals = list(morph_results['parent'][param].values())
cand_vals = list(subset_vals.values())
if not ref_vals or not cand_vals:
continue
scores = compute_all_selected(metrics, ref_vals, cand_vals)
if scores:
assessment[(idx, param)] = scores
return assessment