Skip to content

Commit ff1d43f

Browse files
committed
fix: Return null axis_codes if non-finites detected
1 parent 454eb71 commit ff1d43f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/files/nifti.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ function argMax(arr: number[]): number {
125125
*
126126
* @returns character codes describing the orientation of an image affine.
127127
*/
128-
export function axisCodes(affine: number[][]): string[] {
128+
export function axisCodes(affine: number[][]): string[] | null {
129129
// This function is an extract of the Python function transforms3d.affines.decompose44
130130
// (https://github.com/matthew-brett/transforms3d/blob/6a43a98/transforms3d/affines.py#L10-L153)
131131
//
132132
// As an optimization, this only orthogonalizes the basis,
133133
// and does not normalize to unit vectors.
134134

135+
// Bad qforms result in NaNs in the rotation matrix
136+
if (affine.some((row) => row.some((val) => !Number.isFinite(val)))) {
137+
return null
138+
}
139+
135140
// Operate on columns, which are the cosines that project input coordinates onto output axes
136141
const [cosX, cosY, cosZ] = [0, 1, 2].map((j) => [0, 1, 2].map((i) => affine[i][j]))
137142

0 commit comments

Comments
 (0)