<s> '''<STR_LIT>''' <EOL> from __future__ import division , print_function , absolute_import <EOL> import numpy as np <EOL> import numpy . linalg as npl <EOL> class OrientationError ( Exception ) : <EOL> pass <EOL> def io_orientation ( affine , tol = None ) : <EOL> '''<STR_LIT>''' <EOL> affine = np . asarray ( affine ) <EOL> q , p = affine . shape [ <NUM_LIT:0> ] - <NUM_LIT:1> , affine . shape [ <NUM_LIT:1> ] - <NUM_LIT:1> <EOL> RZS = affine [ : q , : p ] <EOL> zooms = np . sqrt ( np . sum ( RZS * RZS , axis = <NUM_LIT:0> ) ) <EOL> zooms [ zooms == <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> RS = RZS / zooms <EOL> P , S , Qs = npl . svd ( RS ) <EOL> if tol is None : <EOL> tol = S . max ( ) * max ( RS . shape ) * np . finfo ( S . dtype ) . eps <EOL> keep = ( S > tol ) <EOL> R = np . dot ( P [ : , keep ] , Qs [ keep ] ) <EOL> ornt = np . ones ( ( p , <NUM_LIT:2> ) , dtype = np . int8 ) * np . nan <EOL> for in_ax in range ( p ) : <EOL> col = R [ : , in_ax ] <EOL> if not np . allclose ( col , <NUM_LIT:0> ) : <EOL> out_ax = np . argmax ( np . abs ( col ) ) <EOL> ornt [ in_ax , <NUM_LIT:0> ] = out_ax <EOL> assert col [ out_ax ] != <NUM_LIT:0> <EOL> if col [ out_ax ] < <NUM_LIT:0> : <EOL> ornt [ in_ax , <NUM_LIT:1> ] = - <NUM_LIT:1> <EOL> else : <EOL> ornt [ in_ax , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> R [ out_ax , : ] = <NUM_LIT:0> <EOL> return ornt <EOL> def ornt_transform ( start_ornt , end_ornt ) : <EOL> '''<STR_LIT>''' <EOL> start_ornt = np . asarray ( start_ornt ) <EOL> end_ornt = np . asarray ( end_ornt ) <EOL> if start_ornt . shape != end_ornt . shape : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if start_ornt . shape [ <NUM_LIT:1> ] != <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( start_ornt . shape , ) ) <EOL> result = np . empty_like ( start_ornt ) <EOL> for end_in_idx , ( end_out_idx , end_flip ) in enumerate ( end_ornt ) : <EOL> for start_in_idx , ( start_out_idx , start_flip ) in enumerate ( start_ornt ) : <EOL> if end_out_idx == start_out_idx : <EOL> if start_flip == end_flip : <EOL> flip = <NUM_LIT:1> <EOL> else : <EOL> flip = - <NUM_LIT:1> <EOL> result [ start_in_idx , : ] = [ end_in_idx , flip ] <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> end_out_idx ) <EOL> return result <EOL> def apply_orientation ( arr , ornt ) : <EOL> '''<STR_LIT>''' <EOL> t_arr = np . asarray ( arr ) <EOL> ornt = np . asarray ( ornt ) <EOL> n = ornt . shape [ <NUM_LIT:0> ] <EOL> if t_arr . ndim < n : <EOL> raise OrientationError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if np . any ( np . isnan ( ornt [ : , <NUM_LIT:0> ] ) ) : <EOL> raise OrientationError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for ax , flip in enumerate ( ornt [ : , <NUM_LIT:1> ] ) : <EOL> if flip == - <NUM_LIT:1> : <EOL> t_arr = flip_axis ( t_arr , axis = ax ) <EOL> full_transpose = np . arange ( t_arr . ndim ) <EOL> full_transpose [ : n ] = np . argsort ( ornt [ : , <NUM_LIT:0> ] ) <EOL> t_arr = t_arr . transpose ( full_transpose ) <EOL> return t_arr <EOL> def inv_ornt_aff ( ornt , shape ) : <EOL> '''<STR_LIT>''' <EOL> ornt = np . asarray ( ornt ) <EOL> if np . any ( np . isnan ( ornt ) ) : <EOL> raise OrientationError ( "<STR_LIT>" ) <EOL> p = ornt . shape [ <NUM_LIT:0> ] <EOL> shape = np . array ( shape ) [ : p ] <EOL> axis_transpose = [ int ( v ) for v in ornt [ : , <NUM_LIT:0> ] ] <EOL> undo_reorder = np . eye ( p + <NUM_LIT:1> ) [ axis_transpose + [ p ] , : ] <EOL> undo_flip = np . diag ( list ( ornt [ : , <NUM_LIT:1> ] ) + [ <NUM_LIT:1.0> ] ) <EOL> center_trans = - ( shape - <NUM_LIT:1> ) / <NUM_LIT> <EOL> undo_flip [ : p , p ] = ( ornt [ : , <NUM_LIT:1> ] * center_trans ) - center_trans <EOL> return np . dot ( undo_flip , undo_reorder ) <EOL> @ np . deprecate_with_doc ( "<STR_LIT>" ) <EOL> def orientation_affine ( ornt , shape ) : <EOL> return inv_ornt_aff ( ornt , shape ) <EOL> def flip_axis ( arr , axis = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> arr = np . asanyarray ( arr ) <EOL> arr = arr . swapaxes ( <NUM_LIT:0> , axis ) <EOL> arr = np . flipud ( arr ) <EOL> return arr . swapaxes ( axis , <NUM_LIT:0> ) <EOL> def ornt2axcodes ( ornt , labels = None ) : <EOL> """<STR_LIT>""" <EOL> if labels is None : <EOL> labels = list ( zip ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> axcodes = [ ] <EOL> for axno , direction in np . asarray ( ornt ) : <EOL> if np . isnan ( axno ) : <EOL> axcodes . append ( None ) <EOL> continue <EOL> axint = int ( np . round ( axno ) ) <EOL> if axint != axno : <EOL> raise ValueError ( '<STR_LIT>' % axno ) <EOL> elif direction == <NUM_LIT:1> : <EOL> axcode = labels [ axint ] [ <NUM_LIT:1> ] <EOL> elif direction == - <NUM_LIT:1> : <EOL> axcode = labels [ axint ] [ <NUM_LIT:0> ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> axcodes . append ( axcode ) <EOL> return tuple ( axcodes ) <EOL> def axcodes2ornt ( axcodes , labels = None ) : <EOL> """<STR_LIT>""" <EOL> if labels is None : <EOL> labels = list ( zip ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> n_axes = len ( axcodes ) <EOL> ornt = np . ones ( ( n_axes , <NUM_LIT:2> ) , dtype = np . int8 ) * np . nan <EOL> for code_idx , code in enumerate ( axcodes ) : <EOL> for label_idx , codes in enumerate ( labels ) : <EOL> if code is None : <EOL> continue <EOL> if code in codes : <EOL> if code == codes [ <NUM_LIT:0> ] : <EOL> ornt [ code_idx , : ] = [ label_idx , - <NUM_LIT:1> ] <EOL> else : <EOL> ornt [ code_idx , : ] = [ label_idx , <NUM_LIT:1> ] <EOL> break <EOL> return ornt <EOL> def aff2axcodes ( aff , labels = None , tol = None ) : <EOL> """<STR_LIT>""" <EOL> ornt = io_orientation ( aff , tol ) <EOL> return ornt2axcodes ( ornt , labels ) </s>
