def is_9DOF(self): tra, scale, rot, rot_axis = self.to_translation_scale_rotation() if tra is None: return False tra_mat = tr.translation_matrix(tra) scale_mat = numpy.diag([scale[0],scale[1],scale[2],1.0]) rot_mat = tr.rotation_matrix(rot,rot_axis) mat = numpy.dot(tra_mat, numpy.dot(scale_mat, rot_mat ) ) return tr.is_same_transform(mat,self.data)
def is_9DOF(self): tra, scale, rot, rot_axis = self.to_translation_scale_rotation() if tra is None: return False tra_mat = tr.translation_matrix(tra) scale_mat = numpy.diag([scale[0],scale[1],scale[2],1.0]) rot_mat = tr.rotation_matrix(rot,rot_axis) mat = numpy.dot(tra_mat, numpy.dot(scale_mat, rot_mat ) ) return tr.is_same_transform(mat,self.data)
def load_from_file(self, filename, channels=['B']): f = open(filename) self._reset() while 1: l = f.readline() if l == '': break d = l.split() channel = d[0][1] do_process = False if channels is None: do_process = True elif channels == []: do_process = True elif channels == '': do_process = True else: if channel in channels: do_process = True else: do_process = False if do_process: data = d[1] x = float32(d[2]) y = float32(d[3]) z = float32(d[4]) q0 = float32(d[5]) q1 = float32(d[6]) q2 = float32(d[7]) q3 = float32(d[8]) q = [q0, q1, q2, q3] #q = tr.quaternion_conjugate(q) status = int32(d[11]) uncertainty = float32(d[12]) self._n_time_points += 1 tra_mat = tr.translation_matrix([x, y, z]) rot_mat = tr.quaternion_matrix(q) self._motion.append(numpy.dot(tra_mat, rot_mat)) rotation = quaternion_to_rotation(q) self._tx.append(x) self._ty.append(y) self._tz.append(z) self._rx.append(rotation[0]) self._ry.append(rotation[1]) self._rz.append(rotation[2]) self._q0.append(q[0]) self._q1.append(q[1]) self._q2.append(q[2]) self._q3.append(q[3])
def load_from_file(self,filename, channels=['B']): f = open(filename) self._reset() while 1: l = f.readline() if l=='': break d = l.split() channel = d[0][1] do_process = False if channels is None: do_process = True elif channels == []: do_process = True elif channels == '': do_process = True else: if channel in channels: do_process = True else: do_process = False if do_process: data = d[1] x = float32(d[2]) y = float32(d[3]) z = float32(d[4]) q0 = float32(d[5]) q1 = float32(d[6]) q2 = float32(d[7]) q3 = float32(d[8]) q = [q0,q1,q2,q3] #q = tr.quaternion_conjugate(q) status = int32(d[11]) uncertainty = float32(d[12]) self._n_time_points += 1 tra_mat = tr.translation_matrix([x,y,z]) rot_mat = tr.quaternion_matrix(q) self._motion.append( numpy.dot(tra_mat,rot_mat) ) rotation = quaternion_to_rotation(q) self._tx.append(x) self._ty.append(y) self._tz.append(z) self._rx.append(rotation[0]) self._ry.append(rotation[1]) self._rz.append(rotation[2]) self._q0.append(q[0]) self._q1.append(q[1]) self._q2.append(q[2]) self._q3.append(q[3])
def to_translation_scale_rotation(self): # FIXME mat = self.data.copy() tra = tr.translation_from_matrix(mat) tra_mat = tr.translation_matrix(tra) mat = numpy.dot(numpy.linalg.inv(tra_mat), mat) factor, origin, direction = tr.scale_from_matrix(mat) scale_mat = tr.scale_matrix(factor, origin, direction) scale = numpy.diag(scale_mat) mat = numpy.dot(numpy.linalg.inv(scale_mat), mat) try: rot,rot_axis,point = tr.rotation_from_matrix(mat) except ValueError: #print "Not a rotation matrix. " return None, None, None, None #print "Rotation axis: ",rot_axis return tra, scale, rot, rot_axis
def to_translation_scale_rotation(self): # FIXME mat = self.data.copy() tra = tr.translation_from_matrix(mat) tra_mat = tr.translation_matrix(tra) mat = numpy.dot(numpy.linalg.inv(tra_mat), mat) factor, origin, direction = tr.scale_from_matrix(mat) scale_mat = tr.scale_matrix(factor, origin, direction) scale = numpy.diag(scale_mat) mat = numpy.dot(numpy.linalg.inv(scale_mat), mat) try: rot,rot_axis,point = tr.rotation_from_matrix(mat) except ValueError: #print "Not a rotation matrix. " return None, None, None, None #print "Rotation axis: ",rot_axis return tra, scale, rot, rot_axis
def load_data_files(self, path, from_dicom_comments=True, files_start_with=None, files_end_with=".dcm", exclude_files_end_with=['.dat','.txt','.py','.pyc','.nii','.gz','.png','.jpg','.jpeg','.eps','.hdr','.l'] ): """Load vNAV dicom files from given path and extract motion information. If from_dicom_comments==True, use the information stored in the dicom comments. If from_dicom_comments==False, use the information stored in the dicom MOCO field. As of April 2014, these two express motion in different coordinate systems. The dicom comments report absolute motion in scanner coordinates (origin is the magnetic iso-center of the scanner). The dicom MOCO field is currently for proprietary use (??). """ self._n_time_points = 0 self._duration = [] self._motion = [] self._paths = [] self._tx = []; self._ty = []; self._tz = []; self._rx = []; self._ry = []; self._rz = []; self._tx_comm = []; self._ty_comm = []; self._tz_comm = []; self._rx_comm = []; self._ry_comm = []; self._rz_comm = []; self._q0_comm = []; self._q1_comm = []; self._q2_comm = []; self._q3_comm = []; self._a0_comm = []; self._a1_comm = []; self._a2_comm = []; self._a3_comm = []; N=0 # pick the first dicom file found in path files = os.listdir(path) files.sort() # CASE 1: there exist files named with .dcm extension for file_name in files: file_valid = True if files_start_with is not None: if not file_name.startswith(files_start_with): file_valid = False if files_end_with is not None: if not file_name.endswith(files_end_with): file_valid = False for s in exclude_files_end_with: if file_name.endswith(s): file_valid = False if file_valid: full_path = path+os.sep+file_name # read moco information from files self._paths.append(full_path) try: f = dicom.read_file(full_path) except: print "Could not read file ",full_path return t = f.get(0x00191025).value r = f.get(0x00191026).value self._tx.append(t[0]); self._ty.append(t[1]); self._tz.append(t[2]); self._rx.append(r[0]); self._ry.append(r[1]); self._rz.append(r[2]); motion_dicom_moco = [] # extract moco information stored in the dicom comment field if from_dicom_comments: s = f.get(0x00204000).value if N: a = numpy.float32(s.split(' ')[1:5]) t = numpy.float32(s.split(' ')[6:9]) freq = numpy.float32(s.split(' ')[10]) r = angle_axis_to_rotation(a[0],a[1:4]) else: t = numpy.float32([0,0,0]) r = numpy.float32([0,0,0]) a = numpy.float32([0,1,0,0]) #FIXME: is this right? q = angle_axis_to_quaternion(a.copy()[0],a.copy()[1:4]) self._a0_comm.append(a[0]); self._a1_comm.append(a[1]); self._a2_comm.append(a[2]); self._a3_comm.append(a[3]); self._tx_comm.append(t[0]); self._ty_comm.append(t[1]); self._tz_comm.append(t[2]) self._q0_comm.append(q[0]); self._q1_comm.append(q[1]); self._q2_comm.append(q[2]); self._q3_comm.append(q[3]); self._rx_comm.append(r[0]); self._ry_comm.append(r[1]); self._rz_comm.append(r[2]); tra_mat = tr.translation_matrix(t) rot_mat = tr.quaternion_matrix(q) motion_dicom_comments = numpy.dot(tra_mat,rot_mat) #xaxis, yaxis, zaxis = [1, 0, 0], [0, 1, 0], [0, 0, 1] #Rx = tr.rotation_matrix(r[0], xaxis) #Ry = tr.rotation_matrix(r[1], yaxis) #Rz = tr.rotation_matrix(r[2], zaxis) #rot_mat = tr.concatenate_matrices(Rx, Ry, Rz) #rot_mat = Ry.copy() #motion_dicom_comments = numpy.dot(tra_mat,rot_mat) #motion_dicom_comments = rot_mat.copy() N += 1 if from_dicom_comments: self._motion.append(motion_dicom_comments) else: self._motion.append(motion_dicom_moco) acquisition_number = f.get(0x00200012).value creation_time = f.get(0x00080013).value # print "Acquisition number: ", acquisition_number # print "Creation time: ",creation_time self._n_time_points = N
def load_data_files(self, path, from_dicom_comments=True, files_start_with=None, files_end_with=None, exclude_files_end_with=[ '.dat', '.txt', '.py', '.pyc', '.nii', '.gz' ]): """Load vNAV dicom files from given path and extract motion information. If from_dicom_comments==True, use the information stored in the dicom comments. If from_dicom_comments==False, use the information stored in the dicom MOCO field. As of April 2014, these two express motion in different coordinate systems. The dicom comments report absolute motion in scanner coordinates (origin is the magnetic iso-center of the scanner). The dicom MOCO field is currently for Siemens use (??). """ self._n_time_points = 0 self._duration = [] self._motion = [] self._paths = [] self._tx = [] self._ty = [] self._tz = [] self._rx = [] self._ry = [] self._rz = [] self._tx_comm = [] self._ty_comm = [] self._tz_comm = [] self._rx_comm = [] self._ry_comm = [] self._rz_comm = [] self._q0_comm = [] self._q1_comm = [] self._q2_comm = [] self._q3_comm = [] self._a0_comm = [] self._a1_comm = [] self._a2_comm = [] self._a3_comm = [] N = 0 # pick the first dicom file found in path files = os.listdir(path) # CASE 1: there exist files named with .dcm extension for file_name in files: file_valid = True if files_start_with is not None: if not file_name.startswith(files_start_with): file_valid = False if files_end_with is not None: if not file_name.endswith(files_end_with): file_valid = False for s in exclude_files_end_with: if file_name.endswith(s): file_valid = False if file_valid: full_path = path + os.sep + file_name # read moco information from files self._paths.append(full_path) f = dicom.read_file(full_path) t = f.get(0x00191025).value r = f.get(0x00191026).value self._tx.append(t[0]) self._ty.append(t[1]) self._tz.append(t[2]) self._rx.append(r[0]) self._ry.append(r[1]) self._rz.append(r[2]) motion_dicom_moco = [] # extract moco information stored in the dicom comment field s = f.get(0x00204000).value if N: a = numpy.float32(s.split(' ')[1:5]) t = numpy.float32(s.split(' ')[6:9]) freq = numpy.float32(s.split(' ')[10]) r = angle_axis_to_rotation(a[0], a[1:4]) else: t = numpy.float32([0, 0, 0]) r = numpy.float32([0, 0, 0]) a = numpy.float32([0, 1, 0, 0]) #FIXME: is this right? q = angle_axis_to_quaternion(a.copy()[0], a.copy()[1:4]) self._a0_comm.append(a[0]) self._a1_comm.append(a[1]) self._a2_comm.append(a[2]) self._a3_comm.append(a[3]) self._tx_comm.append(t[0]) self._ty_comm.append(t[1]) self._tz_comm.append(t[2]) self._q0_comm.append(q[0]) self._q1_comm.append(q[1]) self._q2_comm.append(q[2]) self._q3_comm.append(q[3]) self._rx_comm.append(r[0]) self._ry_comm.append(r[1]) self._rz_comm.append(r[2]) tra_mat = tr.translation_matrix(t) rot_mat = tr.quaternion_matrix(q) motion_dicom_comments = numpy.dot(tra_mat, rot_mat) #xaxis, yaxis, zaxis = [1, 0, 0], [0, 1, 0], [0, 0, 1] #Rx = tr.rotation_matrix(r[0], xaxis) #Ry = tr.rotation_matrix(r[1], yaxis) #Rz = tr.rotation_matrix(r[2], zaxis) #rot_mat = tr.concatenate_matrices(Rx, Ry, Rz) #rot_mat = Ry.copy() #motion_dicom_comments = numpy.dot(tra_mat,rot_mat) #motion_dicom_comments = rot_mat.copy() N += 1 if from_dicom_comments: self._motion.append(motion_dicom_comments) else: self._motion.append(motion_dicom_moco) acquisition_number = f.get(0x00200012).value creation_time = f.get(0x00080013).value # print "Acquisition number: ", acquisition_number # print "Creation time: ",creation_time self._n_time_points = N
def __init__(self, translation, rotation_angle, rotation_direction, rotation_point=None, map_from="",map_to=""): rot = tr.rotation_matrix(rotation_angle, rotation_direction, rotation_point) tra = tr.translation_matrix(translation) mat = numpy.dot(tra,rot) Transform_Affine.__init__(self, mat,map_from,map_to)
def __init__(self, direction, map_from="",map_to=""): mat = tr.translation_matrix(direction) Transform_Affine.__init__(self, mat,map_from,map_to)
def from_translation_rotation(self, tra, rot, axis=[0,0,1]): return numpy.dot(tr.translation_matrix(tra), tr.rotation_matrix(rot,axis) )
def __init__(self, translation, rotation_angle, rotation_direction, rotation_point=None, map_from="",map_to=""): rot = tr.rotation_matrix(rotation_angle, rotation_direction, rotation_point) tra = tr.translation_matrix(translation) mat = numpy.dot(tra,rot) Transform_Affine.__init__(self, mat,map_from,map_to)
def __init__(self, direction, map_from="",map_to=""): mat = tr.translation_matrix(direction) Transform_Affine.__init__(self, mat,map_from,map_to)
def from_translation_rotation(self, tra, rot, axis=[0,0,1]): return numpy.dot(tr.translation_matrix(tra), tr.rotation_matrix(rot,axis) )