graintrace.frame_transform#
Stated specimen-frame change for FF / NF / EBSD datasets.
x' = Q (s * x) + t, R' = Q R (sample side, on the left of the active
R = euler_to_matrix(...)), eps' = Q eps Q^T. Q is a proper rotation, s a per-axis sign applied to
positions only, t a translation. There is no scale term.
- class graintrace.frame_transform.FrameTransform(rotation=None, axis_sign=(1, 1, 1), translation=(0.0, 0.0, 0.0), angle_unit='degrees', sequence='extrinsic')[source]#
Bases:
objectSample-frame change
x' = Q (s * x) + t,R' = Q R,eps' = Q eps Q^T.Positions use
A = Q diag(s)(possibly improper); orientations and tensors use the properQ. Both are stored soinverse()and@stay exact.- Parameters:
rotation –
None, a (3, 3) proper matrix, a scalar-first unit quaternion(w, x, y, z), a rotation vector, or(axis, angle)pairs such as[("z", 172.3), ("x", 180.0), ("y", -0.9)].axis_sign (Sequence[float]) – per-axis
+1/-1, applied to positions only.translation (Sequence[float]) – origin offset
t, applied after the rotation.angle_unit (str) –
"degrees"(default) or"radians".sequence (str) –
"extrinsic"(default, fixed axes) or"intrinsic".
- Raises:
ValueError – on an improper rotation or an
axis_signentry other than +-1.
Example
>>> import numpy as np >>> from graintrace.frame_transform import FrameTransform >>> T = FrameTransform(rotation=[("z", 90.0)]) >>> np.round(T.positions([[1.0, 0.0, 0.0]]), 12) array([[0., 1., 0.]])
- A#
Position matrix
A = Q diag(s).
- property axis_sign: ndarray | None#
The sign vector
s, orNonewhenQ^T Ais not a +-1 diagonal (composed transforms).
- positions(x)[source]#
Transform positions:
x' = A x + t.- Parameters:
x – (…, 3) positions.
- Returns:
(…, 3) positions in the target frame.
- Return type:
- orientation_matrices(g)[source]#
Transform orientation matrices on the sample side:
R' = Q R.- Parameters:
g – (…, 3, 3) active matrices
R(crystal -> sample), as returned byeuler_to_matrix(); a tensor keeps its dtype and device.- Returns:
(…, 3, 3) transformed matrices.
- Return type:
- mrp(p)[source]#
Transform neml2 v3 MRP orientations in neml2; returns the short set
|p'| <= 1.- Parameters:
p – (…, 3) neml2 v3 MRP orientations (either shadow set).
- Returns:
(…, 3) transformed MRP,
|p'| <= 1.- Return type:
- euler(eul, convention='bunge', angle_type='degrees')[source]#
Transform Euler angles via the orientation matrix (no angle arithmetic).
- Parameters:
- Returns:
(…, 3) Euler angles in the same convention and unit.
- Return type:
- tensors(eps)[source]#
Transform sample-frame rank-2 tensors:
eps' = Q eps Q^T.- Parameters:
eps – (…, 3, 3) tensors or (…, 9) row-major components.
- Returns:
transformed tensors, same shape as the input.
- Return type:
- classmethod from_dict(record)[source]#
Rebuild a transform from
to_dict()’s record.- Parameters:
- Returns:
the stored transform.
- Return type:
- Raises:
ValueError – on a foreign record, a non-orthogonal
position_matrix, or one that disagrees with a non-nullaxis_sign.
- classmethod from_json(path)[source]#
Read a transform written by
to_json().- Parameters:
path (str)
- Return type:
- apply_ff_csv(src, dst, coord_cols=('X', 'Y', 'Z'), euler_cols=('Eul0', 'Eul1', 'Eul2'), convention='bunge', angle_type='auto', tensor_prefixes=('eKen', 'eFab'), extra_matrix_prefixes=('O',))[source]#
Move an FF grain table into the target frame and write
<dst>.transform.json.- Parameters:
src (str) – input CSV path or
pandas.DataFrame.dst (str) – output CSV path.
euler_cols (Sequence[str]) – the three Euler columns, or
Noneto skip.convention (str) –
'bunge','kocks'or'roe'.angle_type (str) –
'degrees','radians'or'auto'.tensor_prefixes (Sequence[str]) –
<prefix>11..33tensor columns (Q eps Q^T); missing ones are skipped.extra_matrix_prefixes (Sequence[str]) –
<prefix>11..33active orientation-matrix columns (Q R; MIDASO); missing ones are skipped.
- Returns:
dst.- Return type:
- apply_pointcloud(df_or_csv, dst, coord_cols=('x', 'y', 'z'), euler_cols=('Eul1', 'Eul2', 'Eul3'), convention='bunge', angle_type='radians')[source]#
Move an NF / EBSD point cloud into the target frame and write
<dst>.transform.json.- Parameters:
- Returns:
dst.- Return type:
- apply_voxel_grid(npy_in, npy_out, convention='bunge', angle_type='radians')[source]#
Move a
[phase, Eul1..3, X, Y, Z]voxel grid (.npy) into the target frame.The array shape is kept; after a rotation that mixes axes the grid is no longer axis aligned.
- apply_ee_csv(src, dst)[source]#
Move a headerless
x, y, z+ 9 row-major strain file (reconstruction_cpfe_ee.csv).Writes
dstcomma-delimited without a header, plus<dst>.transform.json.- Parameters:
- Returns:
dst.- Return type:
- Raises:
ValueError – if the file does not have 12 columns.
- inside_voxel_solid(points, solid_mask, origin, spacing, margin_vox=0)[source]#
Flag target-frame points that land on solid voxels of a source-frame grid.
Points are mapped back with
inverse()and snapped to the nearest voxel centre; points outside the grid areFalse. Use it for thein_overlap_a/in_overlap_bmasks ofdetect_splits.- Parameters:
- Returns:
(N,) boolean mask.
- Return type:
- Raises:
ValueError – on a non-3D mask, non-positive spacing, negative margin or non-(N, 3) points.
- graintrace.frame_transform.voxel_solid_from_grid(grid, spacing=None)[source]#
Return
(solid_mask, origin, spacing)of a[phase, Eul1..3, X, Y, Z]voxel grid.Phase
> 0is solid. The result feedsFrameTransform.inside_voxel_solid().- Parameters:
grid – (nx, ny, nz, >=7) array or
.npypath (e.g.merged_segmented_fixed_grid.npy).spacing – voxel size; inferred from neighbouring voxel coordinates when
None.
- Returns:
boolean (nx, ny, nz) mask, (3,) centre of voxel
(0, 0, 0), (3,) spacing.- Return type:
- Raises:
ValueError – on a malformed grid, or a single-voxel axis with no
spacing.