def save_results_to_nifti(res, nifti, fitmethod, outname=None): if nifti.endswith('.nii'): nifti_base = nifti[:-4] elif nifti.endswith('.nii.gz'): nifti_base = nifti[:-7] if fitmethod == 'two_parameter_monoexp': t2map = out.take(indices=0, axis=FIT_AXIS) m0map = out.take(indices=1, axis=FIT_AXIS) R2map = out.take(indices=2, axis=FIT_AXIS) output_name_list = [] output_basename_list = ['t2map', 'm0map', 'R2map'] for item in output_basename_list: output_name_list.append(str(nifti_base) + '_' + item) output_list = [t2map, m0map, R2map] for num, item in enumerate(output_name_list): writer = niftiWriter(procpar, output_list[num]) writer.write(item) if fitmethod == 'three_parameter_monoexp': t2map = out.take(indices=0, axis=FIT_AXIS) m0map = out.take(indices=1, axis=FIT_AXIS) constmap = out.take(indeices=2, axis=FIT_AXIS) R2map = out.take(indices=3, axis=FIT_AXIS) output_name_list = [] output_basename_list = ['t2map', 'm0map', 'constmap', 'R2map'] for item in output_basename_list: output_name_list.append(str(nifti_base) + '_' + item) output_list = [t2map, m0map, R2map] for num, item in enumerate(output_name_list): writer = niftiWriter(procpar, output_list[num]) writer.write(item) if fitmethod == 'four_parameter_duoexp': m0map = out.take(indices=0, axis=FIT_AXIS) t2smap = out.take(indices=1, axis=FIT_AXIS) t2lmap = out.take(indices=2, axis=FIT_AXIS) fractmap = out.take(indeices=3, axis=FIT_AXIS) R2map = out.take(indices=4, axis=FIT_AXIS) output_name_list = [] output_basename_list = [ 'm0map', 't2smap', 't2lmap', 'fractmap', 'R2map' ] for item in output_basename_list: output_name_list.append(str(nifti_base) + '_' + item) output_list = [t2map, m0map, R2map] for num, item in enumerate(output_name_list): writer = niftiWriter(procpar, output_list[num]) writer.write(item)
def main(fid, pp): fr = fidReader(fid) fr.print_header() data, hdr = fr.read() rec = recon(pp, data) kspace, imspace = rec.recon() #rec.recon() w_im = niftiWriter(pp, imspace) w_k = niftiWriter(pp, kspace) w_k.write_from_fid('out_kspace') w_im.write_from_fid('out_imspace')
def fdf2nii(indir, out, saveprocpar): if indir.endswith('.img'): if out == None: out = str(indir[:-4].split('/')[-1] + '.nii') out_ppname = '.' + out[:-4] + '.procpar' else: out_ppname = '.' + out + '.procpar' else: print('Please give .img as input directory') return try: procpar = indir + '/procpar' except: print('procpar not found, quitting ...') return rdr = fdfReader(indir, out) (hdr, data) = rdr.read() writer = niftiWriter(procpar, data) writer.write(out) if saveprocpar: copy2(procpar, out_ppname) print('procpar saved as hidden: ' + str(out_ppname))
def make_fieldmap(minus_pi_data, plus_pi_data, procpar): """ Make phase correction factor from minus Pi shifted and plus Pi shifted data. Does unwrapping with prelude in two steps: first step is preliminary field map with output mask, then this output mask is holefilled and the unwrapping is done again """ output_name_fieldmap = self.proc_dir + '/fieldmap_wrapped' output_name_fieldmap_uw = self.proc_dir + '/fieldmap_unwrapped' output_name_fieldmap_mask = self.proc_dir + '/fieldmap_premask' output_name_fieldmap_mask_corr = self.proc_dir + '/fieldmap_mask' magnitude_name_for_prelude_corr = self.proc_dir + '/masked_abs' data = np.multiply(plus_pi_data, np.conj(minus_pi_data)) fieldmap = np.arctan2(np.imag(data), np.real(data)) niftiwriter = niftiWriter(procpar, fieldmap) niftiwriter.write(output_name_fieldmap) print('Unwrapping fieldmap ...') # make initial field map os.system('prelude -p '+output_name_fieldmap+' -a '+magnitude_name_for_prelude+ \ ' -u '+output_name_fieldmap_uw+' --savemask='+output_name_fieldmap_mask) # close gaps in mask os.system('fslmaths ' + output_name_fieldmap_mask + ' -fillh ' + output_name_fieldmap_mask_corr) # make new absolute with mask os.system('fslmaths '+magnitude_name_for_prelude+' -mul '+output_name_fieldmap_mask_corr+\ ' '+magnitude_name_for_prelude_corr) # make final field map os.system('prelude -p '+output_name_fieldmap+' -a '+magnitude_name_for_prelude_corr+ \ ' -u '+output_name_fieldmap_uw) print('Fieldmap done!') return
def fid2nii(indir, out=None, saveprocpar=True, save_kspace=False, save_imgspace=True): if not os.path.isdir(indir): raise (Exception('Please specify input fid directory')) if out == None: # if output is not given, take fid as basename for new dir out = indir[:-4] + '.nifti/' if not os.path.exists(out): os.makedirs(out) fid = indir + '/fid' procpar = indir + '/procpar' ppdict = procparReader(procpar).read() fid_data, fid_header = fidReader(fid, procpar).read() kspacemaker = kSpaceMaker(fid_data, procpar, fid_header) kspace = kspacemaker.make() imgspace = imgSpaceMaker(kspace, procpar).make() kspace_real = [] # put separate channel data into separate lements of list kspace_imag = [] for i in range(len(ppdict['rcvrs'])): kspace_real.append(np.real(kspace[i, ...])) kspace_imag.append(np.imag(kspace[i, ...])) writer1 = niftiWriter(procpar, kspace_real[-1]) writer1.write(out + 'kspace_real_ch' + str(i)) writer2 = niftiWriter(procpar, kspace_imag[-1]) writer2.write(out + 'kspace_imag_ch' + str(i)) sumimg = np.sum(np.absolute(imgspace), axis=0) writer_img = niftiWriter(procpar, sumimg) writer_img.write(out + 'imgspace_sum') if saveprocpar == True: copyfile(procpar, out + '/procpar')
#parser = ArgumentParser() #parser.add_argument('inputfile') #args = parser.parse_args() # test data is kmake_test.nii.gz TESTFILE_REAL = 'tempdata/kmake_test_real.nii.gz' TESTFILE_IMAG = 'tempdata/kmake_test_imag.nii.gz' # TODO # for testing add another dim to kspace data to axis0 TESTPP = 'tempdata/kmake_test.procpar' TEST_OUTPUT = 'tempdata/imake_test' kspace_real = nib.load(TESTFILE_REAL).get_fdata() kspace_imag = nib.load(TESTFILE_IMAG).get_fdata() kspace = np.vectorize(complex)(kspace_real, kspace_imag) imgspacemaker = imgSpaceMaker(kspace, TESTPP) img = imgspacemaker.make() print('img shape: ' + str(img.shape)) img_to_write = np.absolute(img[..., 0]) niPlotter(img_to_write).plot_slices() writer = niftiWriter(TESTPP, img_to_write) writer.write(TEST_OUTPUT)
str(f.shape)) print('dixon.ideal output shape : ' + str(ro.shape) + ' ; ' + str(y.shape)) abs_ro1 = np.absolute(np.vectorize(complex)(ro[0, ...], ro[1, ...])) abs_ro2 = np.absolute(np.vectorize(complex)(ro[2, ...], ro[3, ...])) print('abs_ro shape : ' + str(abs_ro1.shape)) # make ro percentage rel_ro1 = np.divide(abs_ro1, abs_ro1 + abs_ro2) rel_ro2 = np.divide(abs_ro2, abs_ro1 + abs_ro2) # plotting stuff #fig, axes = plt.subplots(4,1) #ro_plot = [] # writing to nifti writer1 = niftiWriter(procpar, rel_ro1) writer2 = niftiWriter(procpar, rel_ro2) writer3 = niftiWriter(procpar, f) writer4 = niftiWriter(procpar, phasemap1) writer5 = niftiWriter(procpar, phasemap2) writer6 = niftiWriter(procpar, phasemap3) writer7 = niftiWriter(procpar, magnitude1) writer8 = niftiWriter(procpar, magnitude2) writer9 = niftiWriter(procpar, magnitude3) writer10 = niftiWriter(procpar, y[0]) writer1.write('water') writer2.write('fat') writer3.write('fieldmap') writer4.write('phase1') writer5.write('phase2')
return kspace if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('input_fid_dir') # just give the whole directory args = parser.parse_args() fid = args.input_fid_dir + '/fid' procpar = args.input_fid_dir + '/procpar' fidrdr = fidReader(fid, procpar) data, fidheader = fidrdr.read() fidrdr.print_header() kspacemaker = kSpaceMaker(data, procpar, fidheader) #kspacemaker.print_fid_header() kspace = kspacemaker.make() kspace_real = np.real(kspace[0, ...]) kspace_imag = np.imag(kspace[0, ...]) writer_re = niftiWriter(procpar, kspace_real) writer_re.write('tempdata/kmake_test_real') writer_im = niftiWriter(procpar, kspace_imag) writer_im.write('tempdata/kmake_test_imag') plotter = niPlotter(kspace_real) plotter.plot_slices() os.system('cp ' + str(procpar) + ' tempdata/kmake_test.procpar')
def run(self): def make_fieldmap(minus_pi_data, plus_pi_data, procpar): """ Make phase correction factor from minus Pi shifted and plus Pi shifted data. Does unwrapping with prelude in two steps: first step is preliminary field map with output mask, then this output mask is holefilled and the unwrapping is done again """ output_name_fieldmap = self.proc_dir + '/fieldmap_wrapped' output_name_fieldmap_uw = self.proc_dir + '/fieldmap_unwrapped' output_name_fieldmap_mask = self.proc_dir + '/fieldmap_premask' output_name_fieldmap_mask_corr = self.proc_dir + '/fieldmap_mask' magnitude_name_for_prelude_corr = self.proc_dir + '/masked_abs' data = np.multiply(plus_pi_data, np.conj(minus_pi_data)) fieldmap = np.arctan2(np.imag(data), np.real(data)) niftiwriter = niftiWriter(procpar, fieldmap) niftiwriter.write(output_name_fieldmap) print('Unwrapping fieldmap ...') # make initial field map os.system('prelude -p '+output_name_fieldmap+' -a '+magnitude_name_for_prelude+ \ ' -u '+output_name_fieldmap_uw+' --savemask='+output_name_fieldmap_mask) # close gaps in mask os.system('fslmaths ' + output_name_fieldmap_mask + ' -fillh ' + output_name_fieldmap_mask_corr) # make new absolute with mask os.system('fslmaths '+magnitude_name_for_prelude+' -mul '+output_name_fieldmap_mask_corr+\ ' '+magnitude_name_for_prelude_corr) # make final field map os.system('prelude -p '+output_name_fieldmap+' -a '+magnitude_name_for_prelude_corr+ \ ' -u '+output_name_fieldmap_uw) print('Fieldmap done!') return def inverse_fourier_transform2D(kspace_data): # changed axes 2,3 print('pproc kdata shape {}'.format(kspace_data.shape)) kspace_data = np.fft.fftshift(kspace_data, axes=(0, 1)) imgspace_data = np.fft.ifft2(kspace_data, axes=(0, 1), norm='ortho') imgspace_data = np.fft.ifftshift(imgspace_data, axes=(0, 1)) return imgspace_data if not os.path.exists(self.proc_dir): os.makedirs(self.proc_dir) if not self.preproc_from_fid: open(self.preproc_data, 'w').close() procpar = [] roshift = [] for item in self.combined_names: # combined_names is a list of lists containing real and imaginary parts procpar.append(item[0] + '/procpar') ppr = procparReader(item[0] + '/procpar') shift = int(float(ppr.read()['roshift']) * 1000000) # write it in microsec for naming roshift.append(float(ppr.read()['roshift'])) hdr, data_re = fdfReader(item[0], 'out').read() hdr, data_im = fdfReader(item[1], 'out').read() kspace_data = np.vectorize(complex)(data_re, data_im) imgspace_data = inverse_fourier_transform2D(kspace_data) magnitude_data = np.absolute(imgspace_data) phase_data = np.arctan2(np.imag(imgspace_data), np.real(imgspace_data)) niftiwriter = niftiWriter(item[0] + '/procpar', magnitude_data) output_name_magnitude = self.proc_dir + '/' + self.pslabel + '_' + str( shift) + 'us_mag' niftiwriter.write(output_name_magnitude) niftiwriter = niftiWriter(item[1] + '/procpar', phase_data) output_name_phase = self.proc_dir + '/' + self.pslabel + '_' + str( shift) + 'us_ph' niftiwriter.write(output_name_phase) output_name_unwrapped = self.proc_dir + '/' + self.pslabel + '_' + str( shift) + 'us_unwrapped_ph' if float(ppr.read()['roshift']) == 0.00037: plus_pi_data = imgspace_data if float(ppr.read()['roshift']) == -0.00037: minus_pi_data = imgspace_data if float(ppr.read()['roshift']) == 0: magnitude_name_for_prelude = output_name_magnitude copyfile(item[0] + '/procpar', self.proc_dir + '/procpar') if self.unwrap_all: print('Unwrapping phasemaps ...') os.system('prelude -p '+output_name_phase+' -a '+output_name_magnitude+ \ ' -u '+output_name_unwrapped) with open(self.preproc_data, 'a') as openfile: line = str(float(ppr.read()['roshift']))+','\ +output_name_magnitude+','\ +output_name_phase+'\n' openfile.write(line) if self.preproc_from_fid: pass if self.make_fieldmap: make_fieldmap(minus_pi_data, plus_pi_data, procpar[0]) return