upxo.pxtal.twinned_simple_3d.feature_props_3d module

feature_props_3d.py

Grain and twin morphological property extraction for the twinned simple 3D pipeline.

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_grain_volumes(lgi: ndarray, grain_ids: List[int] | None = None) → Dict[int, int][source]

Return voxel count per grain using a single np.bincount pass (O(N_vox)).

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_twin_volume_fraction(lgi: ndarray, twin_gids: List[int]) → float[source]

Compute twin VF using a single np.bincount pass (O(N_vox)).

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_equivalent_diameters(volumes: Dict[int, int], voxel_size: float) → Dict[int, float][source]
upxo.pxtal.twinned_simple_3d.feature_props_3d.grain_role_statistics(volumes: Dict[int, int], twin_role: Dict[int, str], voxel_size: float) → Dict[str, Dict][source]
upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_ebsd_mc_comparison_stats(ebsd_ref: dict, sgc_ref: dict) → dict[source]

Compute side-by-side descriptive statistics comparing the EBSD reference microstructure against the synthetic SGC structure.

Parameters:
  • ebsd_ref (dict) – miso_deg_full - 1-D ndarray EBSD MDF angles (full, with twins) miso_deg_merged - 1-D ndarray EBSD MDF angles (parent-state, merged) twin_thick_um - 1-D ndarray EBSD twin lamella thicknesses (um) tvf_2d - scalar EBSD 2D twin area fraction

  • sgc_ref (dict) – miso_deg_posttwin- 1-D ndarray SGC post-twin MDF angles twin_thick_3d_um - 1-D ndarray SGC actual 3D twin thicknesses (um) tvf_2d_slices - 1-D ndarray per-slice 2D twin VF values tvf_3d - scalar SGC 3D twin volume fraction

Returns:

Nested dict with keys ‘mdf’, ‘twin_thickness’, ‘twin_volume_fraction’, each containing ‘ebsd’ and ‘mc’ sub-dicts of descriptive statistics. Grain size (host/parent/twin/complete) is NOT part of this – see compute_category_grain_size_stats, which replaced it with a properly 2D-vs-2D, role-split comparison.

Return type:

dict

upxo.pxtal.twinned_simple_3d.feature_props_3d.print_ebsd_mc_comparison(stats: dict) → None[source]

Print a formatted side-by-side comparison table from the output of compute_ebsd_mc_comparison_stats().

upxo.pxtal.twinned_simple_3d.feature_props_3d.assemble_ebsd_sgs_comparison_data(cleaner, tg, rg, parent_info, mdf, twin_thickness, n_slices_per_axis: int = 5, axes=('x', 'y', 'z')) → tuple[source]

Assemble the (ebsd_ref, sgc_ref) raw-array input pair consumed by compute_ebsd_mc_comparison_stats(). Pulled out of SummaryReportPage.on_compute so Distribution Viewer can reuse the exact same assembly for its raw-array overlay plots instead of duplicating it.

Parameters:
  • cleaner (StructureCleaner3D) – Post-twin cleaned structure (lgi_clean, twin_role_clean, all_quats_clean).

  • tg (TwinGenerator3D) – Post-introduction twin generator (twin_halfwidths_vox, summary(), compute_achieved_2d_tvf, base).

  • rg (repgen2d) – EBSD reference (lfi_ebsd_merged, quat_ebsd, prop_ebsd).

  • parent_info (dict) – Output of rg.identify_parent_grains.

  • mdf (dict) – Full EBSD MDF, output of rg.compute_mdf_ebsd (must carry 'miso_deg').

  • twin_thickness (dict) – Output of rg.compute_mc_twin_thickness (must carry 'thick_um').

  • n_slices_per_axis – Forwarded to tg.compute_achieved_2d_tvf for the SGS 2D-slice twin-area-fraction distribution.

  • axes – Forwarded to tg.compute_achieved_2d_tvf for the SGS 2D-slice twin-area-fraction distribution.

Returns:

(ebsd_ref, sgc_ref) – See compute_ebsd_mc_comparison_stats()’s parameter docs.

Return type:

tuple of dict

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_sgs_role_property_distributions(lgi_clean: ndarray, twin_role_clean: Dict[int, str], twin_parent_of_clean: Dict[int, int], selected_props: List[str], selected_levels: List[str], n_slices_per_axis: int = 5, axes=('x', 'y', 'z'), voxel_size: float = 1.0) → Dict[str, Dict[str, ndarray]][source]

Per-twin-role-level distributions of area / aspect_ratio / perimeter / solidity / n_neighbours, sampled from 2D cross-sections of the cleaned 3D structure – the SGS-side equivalent of EBSD’s inherently-2D grain morphology, so the two are directly comparable. Mirrors TwinGenerator3D.compute_achieved_2d_tvf’s slice-sampling convention (evenly-spaced slices per axis, native axis-label mapping, no re-labelling – a grain’s 2D cross-section keeps its 3D grain ID, so twin_role_clean/twin_parent_of_clean apply directly).

Parameters:
  • lgi_clean (ndarray (nz, ny, nx)) – cleaner.lgi_clean – pipeline-native axis order.

  • twin_role_clean (dict) – cleaner.twin_role_clean / cleaner.twin_parent_of_clean.

  • twin_parent_of_clean (dict) – cleaner.twin_role_clean / cleaner.twin_parent_of_clean.

  • selected_props (list of str) – Subset of ('area', 'aspect_ratio', 'perimeter', 'solidity', 'n_neighbours', 'eqdia').

  • selected_levels (list of str) – Subset of ('nonhost', 'host', 'primary', 'seca', 'secb', 'complete') – ‘complete’ is not mutually exclusive with the other five: every region counts toward it in addition to its own specific level, when both are selected.

  • n_slices_per_axis – Evenly-spaced 2D cross-sections sampled per axis in axes.

  • axes – Evenly-spaced 2D cross-sections sampled per axis in axes.

  • voxel_size (float) – Physical voxel edge length (um); scales area (um^2) and perimeter (um).

Returns:

{prop_name: {level_key: ndarray}}.

Return type:

dict

upxo.pxtal.twinned_simple_3d.feature_props_3d.sgs_gid_to_category(gid, twin_role_clean: Dict[int, str]) → str[source]

Maps a cleaned synthetic structure’s per-grain twin_role_clean label onto one of SGS_GRAIN_SIZE_CATEGORIES – the synthetic-side counterpart of ebsdviz.compute_four_category_grain_sets’s EBSD classification. An ‘intermediate’ EBSD grain (twin of its host AND itself hosting a secondary twin) has no separate synthetic label – the region left after carving a secondary twin out of a primary twin simply stays role=’primary_twin’ – so both sides agree to count that configuration as ‘twin’, not ‘host’ (matches compute_four_category_grain_sets pooling EBSD’s intermediates into its ‘twin’ category).

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_sgs_category_ced_by_slice(lgi_clean: ndarray, twin_role_clean: Dict[int, str], category: str, axis: str, n_slices_per_axis: int, voxel_size: float = 1.0) → List[ndarray][source]

Per-slice circle-equivalent-diameter (CED) arrays for one 4-category grain population, sampled from 2D cross-sections of the FINAL cleaned structure along one axis – the per-slice granularity the KDE curve-set plot needs (one thin curve per slice, see viz_distribution_kde.py), as distinct from compute_sgs_role_property_distributions’s already-pooled-across- slices output.

category : one of SGS_GRAIN_SIZE_CATEGORIES (‘complete’ = no filter).

Returns:

One array per sampled slice position, each holding that slice’s matching grains’ CED values (um). A slice with none has an empty array – callers decide whether to skip it for KDE fitting.

Return type:

list of ndarray

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_host_ced_by_axis(lgi: ndarray, host_grain_ids, axis: str, n_slices_per_axis: int, voxel_size: float = 1.0) → ndarray[source]

Pooled circle-equivalent-diameter (CED, um) of the grains in host_grain_ids, from 2D cross-sections of lgi sampled along one axis – the pre-twin-generation counterpart of compute_sgs_category_ced_by_slice, keyed on direct grain-id-set membership (host_grain_ids, from Twin-Host Allocation) instead of a post-twin twin_role_clean label, since no twin exists yet at this pipeline stage (the context for Twin Generation’s minimum lamella thickness). Pooled across every sampled slice on this one axis into a single flat array – feeds one mean/min/max/Q1 summary, not a per-slice KDE curve set.

upxo.pxtal.twinned_simple_3d.feature_props_3d.ced_summary_stats(arr: ndarray) → dict[source]

{‘mean’, ‘min’, ‘max’, ‘q1’, ‘n’} for one CED array – the mean/min/max/lower-quartile reference stats shown alongside a thickness-setting control, as distinct from _stat_block’s mean/median/std/n (built for the Summary Report’s comparison tables). All 0.0/0 if empty.

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_sgs_category_igs(lgi_clean: ndarray, twin_role_clean: Dict[int, str], category: str, axes, n_slices_per_axis, voxel_size: float, mean_ced_um: float) → dict[source]

Pooled 2D intercept grain size (IGS) for one 4-category grain population, across every selected axis’s 2D cross-sections of the final cleaned structure, via planar_intercept_grain_size with a category mask (out-of-category voxels treated as background so a line never counts an intercept against the “wrong” population).

For every sampled 2D slice, BOTH of that slice’s own in-plane directions are sampled (its rows and its columns), each sized independently from that direction’s own physical length divided by mean_ced_um – not the 3D domain’s axis length, and not the other in-plane direction’s length.

n_slices_per_axisint or dict

Scalar (same count for every axis) or {axis: count} – same dict-or-scalar convention as compute_slice_properties_by_axis.

mean_ced_um : this SAME category’s own measured mean CED (from compute_sgs_category_ced_by_slice, pooled across whatever slices/axes the caller used) – sizes the line count for this category only; never the EBSD side’s CED, and never another category’s CED (small, twin-sized features would otherwise go under-sampled – see the design discussion this mirrors).

Returns:

{‘mean’, ‘median’, ‘std’, ‘n’, ‘raw_segments’} pooled across every sampled line on every sampled slice on every requested axis.

Return type:

dict

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_ebsd_category_ced(prop_ebsd: dict, category_gids) → ndarray[source]

EBSD-side CED values (um) for one 4-category grain set – the EBSD-side counterpart of compute_sgs_category_ced_by_slice. No slicing needed: the EBSD map is already 2D, and prop_ebsd[gid] [‘eq_diameter’] is already in physical units (see ebsd_reader._char_lfi).

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_ebsd_category_igs(lfi_ebsd: ndarray, category_gids, voxel_size: float, mean_ced_um: float) → dict[source]

EBSD-side pooled 2D intercept grain size for one 4-category grain set, sampled directly on the real EBSD map (both in-plane directions) via planar_intercept_grain_size, masked to this category only – the EBSD-side counterpart of compute_sgs_category_igs.

voxel_size : EBSD step size (um/pixel). mean_ced_um : this SAME category’s own EBSD-measured mean CED (from compute_ebsd_category_ced) – never the synthetic side’s CED.

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_category_grain_size_stats(lfi_ebsd: ndarray, prop_ebsd: dict, parent_info: dict, lgi_clean: ndarray, twin_role_clean: Dict[int, str], ebsd_voxel_size: float, sgs_voxel_size: float, axes=('x', 'y', 'z'), n_slices_per_axis=5) → dict[source]

{category: {‘ebsd_ced’, ‘sgs_ced’, ‘ebsd_igs’, ‘sgs_igs’}} pooled Mean/Median/Std/N stats for every one of SGS_GRAIN_SIZE_CATEGORIES – the numbers the Summary Report’s 4 grain-size tables are built from (replaces the old pre-twin-3D-vs-EBSD-2D host_eqdia comparison).

CED is pooled across every requested axis/slice on the synthetic side and measured directly (no slicing) on the EBSD side. IGS is derived the same way, each side sizing its own sample-line count from that SAME category’s own just-computed mean CED on that same side – never borrowed from the other side or another category.

n_slices_per_axis : int or dict, same convention as compute_sgs_category_igs / compute_slice_properties_by_axis.

upxo.pxtal.twinned_simple_3d.feature_props_3d.compute_slice_properties_by_axis(lgi: ndarray, selected_props, voxel_size: float = 1.0, axes=('x', 'y', 'z'), n_slices_per_axis=5, connectivity_2d: int = 4, keep_ids=None) → Dict[str, Dict[str, List[ndarray]]][source]

2D grain-morphology properties from evenly-spaced cross-sections of a 3D grain-label field, kept separate per axis and per slice.

Each section is re-labelled in 2D (cc3d) so a grain that meets the plane in several disjoint pieces counts as several 2D grains, the same convention as a map of a flat section. lgi keeps the pipeline’s native (nz, ny, nx) order: axis 'x' is array axis 2, 'y' is 1 and 'z' is 0.

Parameters:
  • lgi (ndarray (nz, ny, nx), int)

  • selected_props (iterable of str) – Subset of {'area', 'eqdia', 'perimeter', 'aspect_ratio', 'solidity', 'major_axis', 'minor_axis'}. Lengths and areas are scaled by voxel_size. minor_axis of a twin’s section is its apparent 2D thickness.

  • axes (iterable of str) – Subset of 'x', 'y', 'z'.

  • n_slices_per_axis (int or dict) – Evenly-spaced slices per axis, or {'x': int, 'y': int, 'z': int}.

  • connectivity_2d (int) – cc3d connectivity for the 2D re-labelling (4 or 8).

  • keep_ids (iterable of int or None) – Measure only these 3D grains (e.g. the twin hosts); every other grain is treated as background. None measures every grain.

Return type:

dict {axis: {prop_name: [ndarray per slice, in slice order]}}

upxo.pxtal.twinned_simple_3d.feature_props_3d.ebsd_merged_property_values(rg, prop: str, hosts_only: bool = False) → ndarray[source]

Per-grain values of prop for the EBSD map with twins merged into their parent grains (rg.prop_ebsd_merged) – the EBSD counterpart of a structure that has no twins yet.

hosts_only : keep only the merged grains that contain at least one twin (the grains a twin-introduction step would host twins in).

upxo.pxtal.twinned_simple_3d.feature_props_3d.format_size(n)[source]

Human-readable file size (B/KB/MB/GB/TB).

upxo.pxtal.twinned_simple_3d.feature_props_3d.build_summary_markdown_lines(role_stats, tvf_final_3d, tvf_ebsd_target, comparison_stats=None, category_grain_size_stats=None)[source]

Formats grain-role volume statistics + twin volume fraction summary (and, if given, the full EBSD-vs-MC comparison, and the 4-category CED/IGS grain-size comparison) as Markdown lines. Pure formatting, no file I/O – shared by every report writer that needs this summary, so all embed byte-identical content when the same data is available.

category_grain_size_statsdict or None

Output of compute_category_grain_size_stats – one table per SGS_GRAIN_SIZE_CATEGORIES key, each with a CED row-set and an IGS row-set (Mean/Median/Std/N, EBSD vs MC). Supersedes the old single “Host / Parent Grain Equivalent Diameter” row that used to live under comparison_stats (that compared a 2D EBSD sectional diameter against a pre-twin 3D volumetric one – replaced entirely by this, not kept alongside it).