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: object

Sample-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 proper Q. Both are stored so inverse() 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_sign entry 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, or None when Q^T A is 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:

numpy.ndarray

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 by euler_to_matrix(); a tensor keeps its dtype and device.

Returns:

(…, 3, 3) transformed matrices.

Return type:

torch.Tensor

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:

torch.Tensor

euler(eul, convention='bunge', angle_type='degrees')[source]#

Transform Euler angles via the orientation matrix (no angle arithmetic).

Parameters:
  • eul – (…, 3) Euler angles.

  • convention (str) – 'bunge', 'kocks' or 'roe'.

  • angle_type (str) – 'degrees' or 'radians', for input and output.

Returns:

(…, 3) Euler angles in the same convention and unit.

Return type:

numpy.ndarray

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:

numpy.ndarray

inverse()[source]#

Return the inverse, with parts (A^T, Q^T, -A^T t).

Return type:

FrameTransform

to_dict()[source]#

Return a JSON-serialisable record of the transform.

Return type:

dict

classmethod from_dict(record)[source]#

Rebuild a transform from to_dict()’s record.

Parameters:

record (dict) – a mapping as produced by to_dict().

Returns:

the stored transform.

Return type:

FrameTransform

Raises:

ValueError – on a foreign record, a non-orthogonal position_matrix, or one that disagrees with a non-null axis_sign.

to_json(path)[source]#

Write to_dict() to path as JSON and return path.

Parameters:

path (str)

Return type:

str

classmethod from_json(path)[source]#

Read a transform written by to_json().

Parameters:

path (str)

Return type:

FrameTransform

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.

  • coord_cols (Sequence[str]) – the three position columns.

  • euler_cols (Sequence[str]) – the three Euler columns, or None to skip.

  • convention (str) – 'bunge', 'kocks' or 'roe'.

  • angle_type (str) – 'degrees', 'radians' or 'auto'.

  • tensor_prefixes (Sequence[str]) – <prefix>11..33 tensor columns (Q eps Q^T); missing ones are skipped.

  • extra_matrix_prefixes (Sequence[str]) – <prefix>11..33 active orientation-matrix columns (Q R; MIDAS O); missing ones are skipped.

Returns:

dst.

Return type:

str

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:
  • df_or_csv – input CSV path or pandas.DataFrame.

  • dst (str) – output CSV path.

  • coord_cols (Sequence[str]) – the three position columns.

  • euler_cols (Sequence[str]) – the three Euler columns, or None to skip.

  • convention (str) – Euler convention.

  • angle_type (str) – 'degrees', 'radians' or 'auto'.

Returns:

dst.

Return type:

str

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.

Parameters:
  • npy_in (str) – input .npy path.

  • npy_out (str) – output .npy path.

  • convention (str) – Euler convention of columns 1..3.

  • angle_type (str) – 'degrees', 'radians' or 'auto'.

Returns:

npy_out.

Return type:

str

Raises:

ValueError – if the last axis has fewer than 7 entries.

apply_ee_csv(src, dst)[source]#

Move a headerless x, y, z + 9 row-major strain file (reconstruction_cpfe_ee.csv).

Writes dst comma-delimited without a header, plus <dst>.transform.json.

Parameters:
  • src (str) – input file, comma- or whitespace-delimited.

  • dst (str) – output CSV path.

Returns:

dst.

Return type:

str

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 are False. Use it for the in_overlap_a/in_overlap_b masks of detect_splits.

Parameters:
  • points – (N, 3) points in the target frame.

  • solid_mask – (nx, ny, nz) boolean array, axis k along coordinate k.

  • origin (Sequence[float]) – source-frame centre of voxel (0, 0, 0).

  • spacing – voxel size, scalar or (dx, dy, dz).

  • margin_vox (int) – erode the solid by this many voxels first (default 0).

Returns:

(N,) boolean mask.

Return type:

numpy.ndarray

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 > 0 is solid. The result feeds FrameTransform.inside_voxel_solid().

Parameters:
  • grid – (nx, ny, nz, >=7) array or .npy path (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:

tuple

Raises:

ValueError – on a malformed grid, or a single-voxel axis with no spacing.