def orientation(im, ori=None, set=False, get=False, set_data=False, verbose=1, fname_out=''): verbose = 0 if get else verbose printv('Get dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = get_dimension(im) printv(str(nx) + ' x ' + str(ny) + ' x ' + str(nz) + ' x ' + str(nt), verbose) # if data are 2d or 3d, get orientation from header using fslhd if (nz == 1 or nt == 1) and len(im.data.shape) < 5: if get: try: printv('Get orientation...', verbose) im_out = None ori = get_orientation(im) except Exception, e: printv('ERROR: an error occurred: ' + str(e), verbose, 'error') return ori elif set: # set orientation printv('Change orientation...', verbose) im_out = set_orientation(im, ori) elif set_data: im_out = set_orientation(im, ori, True) else: im_out = None
def orientation(im, ori=None, set=False, get=False, set_data=False, verbose=1, fname_out=''): verbose = 0 if get else verbose printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = get_dimension(im) printv(str(nx) + ' x ' + str(ny) + ' x ' + str(nz)+ ' x ' + str(nt), verbose) # if data are 2d or 3d, get orientation from header using fslhd if (nz == 1 or nt==1) and len(im.data.shape)<5: if get: try: printv('\nGet orientation...', verbose) im_out = None ori = get_orientation(im) except Exception, e: printv('ERROR: an error occurred: \n'+str(e), verbose,'error') return ori elif set: # set orientation printv('\nChange orientation...', verbose) im_out = set_orientation(im, ori) elif set_data: im_out = set_orientation(im, ori, True) else: im_out = None
def compute_dist_2im_2d(self): nx1, ny1, nz1, nt1, px1, py1, pz1, pt1 = get_dimension(self.im1) nx2, ny2, nz2, nt2, px2, py2, pz2, pt2 = get_dimension(self.im2) assert px1 == px2 and py1 == py2 and px1 == py1 self.dim_pix = py1 if self.param.thinning: dat1 = self.thinning1.thinned_image.data dat2 = self.thinning2.thinned_image.data else: dat1 = bin_data(self.im1.data) dat2 = bin_data(self.im2.data) self.distances = HausdorffDistance(dat1, dat2, self.param.verbose) self.res = 'Hausdorff\'s distance : ' + str(self.distances.H * self.dim_pix) + ' mm\n\n' \ 'First relative Hausdorff\'s distance : ' + str(self.distances.h1 * self.dim_pix) + ' mm\n' \ 'Second relative Hausdorff\'s distance : ' + str(self.distances.h2 * self.dim_pix) + ' mm'
def compute_dist_2im_3d(self): nx1, ny1, nz1, nt1, px1, py1, pz1, pt1 = get_dimension(self.im1) nx2, ny2, nz2, nt2, px2, py2, pz2, pt2 = get_dimension(self.im2) # assert round(pz1, 5) == round(pz2, 5) and round(py1, 5) == round(py2, 5) assert nx1 == nx2 self.dim_pix = py1 if self.param.thinning: dat1 = self.thinning1.thinned_image.data dat2 = self.thinning2.thinned_image.data else: dat1 = bin_data(self.im1.data) dat2 = bin_data(self.im2.data) self.distances = [] for slice1, slice2 in zip(dat1, dat2): self.distances.append(HausdorffDistance(slice1, slice2, self.param.verbose))
def compute_dist_2im_2d(self): nx1, ny1, nz1, nt1, px1, py1, pz1, pt1 = get_dimension(self.im1) nx2, ny2, nz2, nt2, px2, py2, pz2, pt2 = get_dimension(self.im2) assert px1 == px2 and py1 == py2 and px1 == py1 self.dim_pix = py1 if self.param.thinning: dat1 = self.thinning1.thinned_image.data dat2 = self.thinning2.thinned_image.data else: dat1 = bin_data(self.im1.data) dat2 = bin_data(self.im2.data) self.distances = HausdorffDistance(dat1, dat2, self.param.verbose) self.res = 'Hausdorff\'s distance : ' + str(self.distances.H*self.dim_pix) + ' mm\n\n' \ 'First relative Hausdorff\'s distance : ' + str(self.distances.h1*self.dim_pix) + ' mm\n' \ 'Second relative Hausdorff\'s distance : ' + str(self.distances.h2*self.dim_pix) + ' mm'
def compute_dist_1im_3d(self): nx1, ny1, nz1, nt1, px1, py1, pz1, pt1 = get_dimension(self.im1) self.dim_pix = py1 if self.param.thinning: dat1 = self.thinning1.thinned_image.data else: dat1 = bin_data(self.im1.data) self.distances = [] for i, dat_slice in enumerate(dat1[:-1]): self.distances.append(HausdorffDistance(bin_data(dat_slice), bin_data(dat1[i+1]), self.param.verbose))
def compute_dist_1im_3d(self): nx1, ny1, nz1, nt1, px1, py1, pz1, pt1 = get_dimension(self.im1) self.dim_pix = py1 if self.param.thinning: dat1 = self.thinning1.thinned_image.data else: dat1 = bin_data(self.im1.data) self.distances = [] for i, dat_slice in enumerate(dat1[:-1]): self.distances.append(HausdorffDistance(bin_data(dat_slice), bin_data(dat1[i + 1]), self.param.verbose))
def orientation(im, ori=None, set=False, get=False, set_data=False, verbose=1): verbose = 0 if get else verbose printv('\nGet dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = get_dimension(im) printv(str(nx) + ' x ' + str(ny) + ' x ' + str(nz)+ ' x ' + str(nt), verbose) # if data are 3d, directly set or get orientation if nt == 1: if get: # get orientation printv('\nGet orientation...', verbose) im_out = None return get_orientation(im) elif set: # set orientation printv('\nChange orientation...', verbose) im_out = set_orientation(im, ori) elif set_data: im_out = set_orientation(im, ori, True) else: im_out = None else: # 4D data: split along T dimension printv('\nSplit along T dimension...', verbose) im_split_list = split_data(im, 3) for im_s in im_split_list: im_s.save(verbose=verbose) if get: # get orientation printv('\nGet orientation...', verbose) im_out=None return get_orientation(im_split_list[0]) elif set: # set orientation printv('\nChange orientation...', verbose) im_changed_ori_list = [] for im_s in im_split_list: im_set = set_orientation(im_s, ori) im_changed_ori_list.append(im_set) printv('\nMerge file back...', verbose) im_out = concat_data(im_changed_ori_list, 3) elif set_data: printv('\nSet orientation of the data only is not compatible with 4D data...', verbose, 'error') else: im_out = None im_out.setFileName(im.file_name+'_'+ori+im.ext) return im_out
def orientation(im, ori=None, set=False, get=False, set_data=False, verbose=1, fname_out=None): """ :param fname_out: when set is True, where to save the output file (default: in cwd, named basename_orientation.ext) :returns: when get, the orientation; when set, the changed image data (not saved) """ verbose = 0 if get else verbose if fname_out is None: fname_out = "{}_{}{}".format(im.file_name, ori, im.ext) printv('Get dimensions of data...', verbose) nx, ny, nz, nt, px, py, pz, pt = get_dimension(im) printv( str(nx) + ' x ' + str(ny) + ' x ' + str(nz) + ' x ' + str(nt), verbose) # if data are 2d or 3d, get orientation from header using fslhd if (nz == 1 or nt == 1) and len(im.data.shape) < 5: if get: try: printv('Get orientation...', verbose) im_out = None ori = get_orientation(im) except Exception as e: printv('ERROR: an error occurred: ' + str(e), verbose, 'error') return ori elif set: # set orientation printv('Change orientation...', verbose) tmp_folder = tmp_create(verbose) curdir = os.getcwd() os.chdir(tmp_folder) im_out = set_orientation(im, ori) os.chdir(curdir) sct.rmtree(tmp_folder) elif set_data: tmp_folder = tmp_create(verbose) curdir = os.getcwd() os.chdir(tmp_folder) im_out = set_orientation(im, ori, True) os.chdir(curdir) sct.rmtree(tmp_folder) else: im_out = None else: # 4D data: split along T dimension # or 5D data: split along 5th dimension # Create a temporary directory and go in it tmp_folder = tmp_create(verbose) curdir = os.getcwd() os.chdir(tmp_folder) if len(im.data.shape) == 5 and im.data.shape[-1] not in [0, 1]: # 5D data printv('Split along 5th dimension...', verbose) im_split_list = multicomponent_split(im) dim = 5 else: # 4D data printv('Split along T dimension...', verbose) im_split_list = split_data(im, 3) dim = 4 for im_s in im_split_list: im_s.save(verbose=verbose) if get: # get orientation printv('Get orientation...', verbose) im_out = None ori = get_orientation(im_split_list[0]) os.chdir(curdir) sct.rmtree(tmp_folder) return ori elif set: # set orientation printv('Change orientation...', verbose) im_changed_ori_list = [] for im_s in im_split_list: im_set = set_orientation(im_s, ori) im_changed_ori_list.append(im_set) printv('Merge file back...', verbose) if dim == 4: im_out = concat_data(im_changed_ori_list, 3) elif dim == 5: fname_changed_ori_list = [ im_ch_ori.absolutepath for im_ch_ori in im_changed_ori_list ] im_out = multicomponent_merge(fname_changed_ori_list) elif set_data: # set orientation printv('Change orientation of data...', verbose) im_changed_ori_list = [] for im_s in im_split_list: im_set = set_orientation(im_s, ori, True) im_changed_ori_list.append(im_set) printv('Merge file back...', verbose) if dim == 4: im_out = concat_data(im_changed_ori_list, 3) elif dim == 5: printv( 'Set data orientation on 5D data is not supported. Sorry.', verbose, 'error') else: im_out = None # Go back to previous directory: os.chdir(curdir) sct.rmtree(tmp_folder) if im_out is not None: im_out.setFileName(fname_out) return im_out