<s> """<STR_LIT>""" <EOL> import vtk <EOL> import os , sys <EOL> import traceback <EOL> import numpy as np <EOL> import time <EOL> import SimpleITK as sitk <EOL> class Util : <EOL> OK = <NUM_LIT:0> <EOL> ERROR = <NUM_LIT:1> <EOL> @ staticmethod <EOL> def print_last_exception ( ) : <EOL> """<STR_LIT>""" <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> print ( "<STR_LIT>" ) <EOL> traceback . print_exception ( exc_type , exc_value , exc_traceback , file = sys . stdout ) <EOL> @ staticmethod <EOL> def get_dimensions ( vtk_mrml_volume_node ) : <EOL> """<STR_LIT>""" <EOL> bounds = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> vtk_mrml_volume_node . GetRASBounds ( bounds ) <EOL> rastoijk = vtk . vtkMatrix4x4 ( ) <EOL> vtk_mrml_volume_node . GetRASToIJKMatrix ( rastoijk ) <EOL> bounds = [ bounds [ <NUM_LIT:0> ] , bounds [ <NUM_LIT:2> ] , bounds [ <NUM_LIT:5> ] , <NUM_LIT:1> ] <EOL> dimensions = rastoijk . MultiplyPoint ( bounds ) <EOL> return [ int ( round ( dimensions [ <NUM_LIT:0> ] ) ) , int ( round ( dimensions [ <NUM_LIT:1> ] ) ) , int ( round ( dimensions [ <NUM_LIT:2> ] ) ) ] <EOL> @ staticmethod <EOL> def is_windows ( ) : <EOL> """<STR_LIT>""" <EOL> return os . sys . platform == "<STR_LIT:win32>" <EOL> @ staticmethod <EOL> def create_directory ( path ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( path ) : <EOL> return False <EOL> os . makedirs ( path ) <EOL> os . chmod ( path , <NUM_LIT:0> <NUM_LIT> ) <EOL> return True <EOL> @ staticmethod <EOL> def get_file_extension ( file_path ) : <EOL> """<STR_LIT>""" <EOL> ( f , ext ) = os . path . splitext ( file_path ) <EOL> return ext <EOL> @ staticmethod <EOL> def ras_to_ijk ( volume_node , ras_coords , convert_to_int = True ) : <EOL> """<STR_LIT>""" <EOL> rastoijk = vtk . vtkMatrix4x4 ( ) <EOL> volume_node . GetRASToIJKMatrix ( rastoijk ) <EOL> cl = list ( ras_coords ) <EOL> cl . append ( <NUM_LIT:1> ) <EOL> l = list ( rastoijk . MultiplyPoint ( cl ) [ : - <NUM_LIT:1> ] ) <EOL> if convert_to_int : <EOL> return [ int ( l [ <NUM_LIT:0> ] ) , int ( l [ <NUM_LIT:1> ] ) , int ( l [ <NUM_LIT:2> ] ) ] <EOL> return l <EOL> @ staticmethod <EOL> def ijk_to_ras ( volume_node , ijk_coords ) : <EOL> """<STR_LIT>""" <EOL> ijktoras = vtk . vtkMatrix4x4 ( ) <EOL> volume_node . GetIJKToRASMatrix ( ijktoras ) <EOL> cl = list ( ijk_coords ) <EOL> cl . append ( <NUM_LIT:1> ) <EOL> return list ( ijktoras . MultiplyPoint ( cl ) [ : - <NUM_LIT:1> ] ) <EOL> @ staticmethod <EOL> def __switch_ras_lps__ ( coords ) : <EOL> """<STR_LIT>""" <EOL> lps_to_ras_matrix = vtk . vtkMatrix4x4 ( ) <EOL> lps_to_ras_matrix . SetElement ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> lps_to_ras_matrix . SetElement ( <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> cl = list ( coords ) <EOL> cl . append ( <NUM_LIT:1> ) <EOL> return list ( lps_to_ras_matrix . MultiplyPoint ( cl ) [ : - <NUM_LIT:1> ] ) <EOL> @ staticmethod <EOL> def ras_to_lps ( coords ) : <EOL> """<STR_LIT>""" <EOL> return Util . __switch_ras_lps__ ( coords ) <EOL> @ staticmethod <EOL> def lps_to_ras ( coords ) : <EOL> """<STR_LIT>""" <EOL> return Util . __switch_ras_lps__ ( coords ) <EOL> @ staticmethod <EOL> def get_lps_to_ijk_transformation_matrix ( volume_node ) : <EOL> """<STR_LIT>""" <EOL> matrix = vtk . vtkMatrix4x4 ( ) <EOL> volume_node . GetRASToIJKMatrix ( matrix ) <EOL> id_matrix = vtk . vtkMatrix4x4 ( ) <EOL> id_matrix . Identity ( ) <EOL> id_matrix . SetElement ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) <EOL> id_matrix . SetElement ( <NUM_LIT:1> , <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> result = vtk . vtkMatrix4x4 ( ) <EOL> result . Multiply4x4 ( matrix , id_matrix , result ) <EOL> return result <EOL> @ staticmethod <EOL> def itk_numpy_array ( itk_image ) : <EOL> """<STR_LIT>""" <EOL> arr = sitk . GetArrayFromImage ( itk_image ) <EOL> itk_image = sitk . Image ( ) <EOL> return arr , itk_image . GetSpacing ( ) , itk_image . GetOrigin ( ) <EOL> @ staticmethod <EOL> def itk_numpy_coordinate ( itk_coordinate ) : <EOL> """<STR_LIT>""" <EOL> l = list ( itk_coordinate ) <EOL> l . reverse ( ) <EOL> return l <EOL> @ staticmethod <EOL> def vtk_numpy_coordinate ( vtk_coordinate ) : <EOL> """<STR_LIT>""" <EOL> l = list ( vtk_coordinate ) <EOL> l . reverse ( ) <EOL> return l <EOL> @ staticmethod <EOL> def numpy_itk_coordinate ( numpy_coordinate , convert_to_int = True ) : <EOL> """<STR_LIT>""" <EOL> if convert_to_int : <EOL> return [ int ( numpy_coordinate [ <NUM_LIT:2> ] ) , int ( numpy_coordinate [ <NUM_LIT:1> ] ) , int ( numpy_coordinate [ <NUM_LIT:0> ] ) ] <EOL> return [ numpy_coordinate [ <NUM_LIT:2> ] , numpy_coordinate [ <NUM_LIT:1> ] , numpy_coordinate [ <NUM_LIT:0> ] ] <EOL> @ staticmethod <EOL> def numpy_vtk_coordinate ( numpy_coordinate ) : <EOL> """<STR_LIT>""" <EOL> l = list ( numpy_coordinate ) <EOL> l . reverse ( ) <EOL> return l <EOL> @ staticmethod <EOL> def convert_vtk_matrix_to_list ( matrix ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( matrix , type ( vtk . vtkMatrix4x4 ( ) ) ) : <EOL> dim = <NUM_LIT:4> <EOL> elif isinstance ( matrix , type ( vtk . vtkMatrix3x3 ( ) ) ) : <EOL> dim = <NUM_LIT:3> <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> threshold = <NUM_LIT> <EOL> result = [ ] <EOL> for i in range ( dim ) : <EOL> row = [ ] <EOL> for j in range ( dim ) : <EOL> n = matrix . GetElement ( i , j ) <EOL> if abs ( n ) < threshold : <EOL> n = <NUM_LIT:0> <EOL> row . append ( n ) <EOL> result . append ( row ) <EOL> return result <EOL> @ staticmethod <EOL> def get_labelmap_slices ( npArray ) : <EOL> """<STR_LIT>""" <EOL> return Util . get_labelmap_slices_from_numpy_array ( npArray ) <EOL> @ staticmethod <EOL> def get_labelmap_slices ( np_array ) : <EOL> """<STR_LIT>""" <EOL> positions = np . where ( np_array > <NUM_LIT:0> ) <EOL> slices = np . unique ( positions [ <NUM_LIT:0> ] ) <EOL> boundArray = np_array [ slices , : , : ] <EOL> labelMaps = np . unique ( boundArray ) <EOL> result = { } <EOL> for label in ( label for label in labelMaps if label > <NUM_LIT:0> ) : <EOL> s = np . where ( boundArray == label ) <EOL> result [ label ] = slices [ np . unique ( s [ <NUM_LIT:0> ] ) ] <EOL> return result <EOL> @ staticmethod <EOL> def get_labelmap_slices_2 ( np_array ) : <EOL> """<STR_LIT>""" <EOL> labelMaps = np . unique ( np_array ) <EOL> result = { } <EOL> for label in ( label for label in labelMaps if label > <NUM_LIT:0> ) : <EOL> result [ label ] = Util . get_slices_for_label_from_numpy_array ( np_array , label ) <EOL> return result <EOL> @ staticmethod <EOL> def get_labelmap_slices_3 ( np_array ) : <EOL> """<STR_LIT>""" <EOL> labelMaps = np . unique ( np_array ) <EOL> result = { } <EOL> for label in ( label for label in labelMaps if label > <NUM_LIT:0> ) : <EOL> s = np . where ( np_array == label ) <EOL> result [ label ] = np . unique ( s [ <NUM_LIT:0> ] ) <EOL> return result <EOL> @ staticmethod <EOL> def get_slices_for_label_from_numpy_array ( np_array , label ) : <EOL> """<STR_LIT>""" <EOL> t = ( np_array == label ) <EOL> tproj = t . any ( axis = <NUM_LIT:1> ) <EOL> tproj = tproj . any ( axis = <NUM_LIT:1> ) <EOL> return np . where ( tproj ) [ <NUM_LIT:0> ] <EOL> @ staticmethod <EOL> def centroid ( numpyArray , labelId = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> mean = np . mean ( np . where ( numpyArray == labelId ) , axis = <NUM_LIT:1> ) <EOL> return np . asarray ( np . round ( mean , <NUM_LIT:0> ) , np . int ) </s>
