# -*- coding: utf-8 -*- #%% #Basic setup import numpy as np from ismrmrdtools import simulation, coils, show matrix_size = 256 csm = simulation.generate_birdcage_sensitivities(matrix_size) phan = simulation.phantom(matrix_size) coil_images = np.tile(phan, (8, 1, 1)) * csm show.imshow(abs(coil_images), tile_shape=(4, 2)) (csm_est, rho) = coils.calculate_csm_walsh(coil_images) combined_image = np.sum(csm_est * coil_images, axis=0) show.imshow(abs(csm_est), tile_shape=(4, 2), scale=(0, 1)) show.imshow(abs(combined_image), scale=(0, 1))
import numpy as np import scipy as sp from ismrmrdtools import sense, grappa, show, simulation, transform,coils from importlib import reload #%% #import some data exercise_data = sp.io.loadmat('hansen_exercises2.mat') csm = np.transpose(exercise_data['smaps']) pat = np.transpose(exercise_data['sp']) data = np.transpose(exercise_data['data']) kspace = np.logical_or(pat==1,pat==3).astype('float32')*(data) acc_factor = 4 alias_img = transform.transform_kspace_to_image(kspace,dim=(1,2)) * np.sqrt(acc_factor) show.imshow(abs(alias_img)) (unmix_grappa,gmap_grappa) = grappa.calculate_grappa_unmixing(data, acc_factor, data_mask=pat>1, csm=csm,kernel_size=(4,5)) #(unmix_grappa,gmap_grappa) = grappa.calculate_grappa_unmixing(data, acc_factor, data_mask=pat>1) show.imshow(abs(gmap_grappa),colorbar=True) recon_grappa = np.squeeze(np.sum(alias_img * unmix_grappa,0)) show.imshow(abs(recon_grappa),colorbar=True) sp.io.savemat('tmp_data.mat',{'pat_py': pat,'data_py': data,'csm_py': csm,'alias_img_py':alias_img,'unmix_grappa_py':unmix_grappa}) #%% #Reload some modules reload(show) reload(sense) reload(grappa) reload(simulation)
from rms_coil_combine import RMSCoilCombine #%% Setup gadgets g3 = RMSCoilCombine() g2 = AccumulateAndRecon(g3) g1 = Remove2xOversampling(g2) #%% Load file filename = 'testdata.h5' if not os.path.isfile(filename): print("%s is not a valid file" % filename) raise SystemExit dset = ismrmrd.Dataset(filename, 'dataset', create_if_needed=False) #%% Send in data #First ISMRMRD XML header g1.process_config(dset.read_xml_header()) g2.process_config(dset.read_xml_header()) g3.process_config(dset.read_xml_header()) # Loop through the rest of the acquisitions and stuff for acqnum in range(0,dset.number_of_acquisitions()): acq = dset.read_acquisition(acqnum) g1.process(acq.getHead(),acq.data) #%% #Get result and display res = g3.get_results() show.imshow(np.squeeze(abs(res[0][1])))
from rms_coil_combine import RMSCoilCombine #%% Setup gadgets g3 = RMSCoilCombine() g2 = AccumulateAndRecon(g3) g1 = Remove2xOversampling(g2) #%% Load file filename = 'testdata.h5' if not os.path.isfile(filename): print("%s is not a valid file" % filename) raise SystemExit dset = ismrmrd.Dataset(filename, 'dataset', create_if_needed=False) #%% Send in data #First ISMRMRD XML header g1.process_config(dset.read_xml_header()) g2.process_config(dset.read_xml_header()) g3.process_config(dset.read_xml_header()) # Loop through the rest of the acquisitions and stuff for acqnum in range(0,dset.number_of_acquisitions()): acq = dset.read_acquisition(acqnum) g1.process(acq.getHead(),np.transpose(acq.data)) #%% #Get result and display res = g3.get_results() show.imshow(np.squeeze(abs(res[0][1])))
y = acq.idx.kspace_encode_step_1 z = acq.idx.kspace_encode_step_2 all_data[rep, contrast, slice, :, z, y, :] = acq.data # Reconstruct images images = np.zeros((nreps, ncontrasts, nslices, eNz, eNy, rNx), dtype=np.float32) for rep in range(nreps): for contrast in range(ncontrasts): for slice in range(nslices): # FFT if eNz>1: #3D im = transform.transform_kspace_to_image(all_data[rep,contrast,slice,:,:,:,:], [1,2,3]) else: #2D im = transform.transform_kspace_to_image(all_data[rep,contrast,slice,:,0,:,:], [1,2]) # Sum of squares im = np.sqrt(np.sum(np.abs(im) ** 2, 0)) # Stuff into the output if eNz>1: #3D images[rep,contrast,slice,:,:,:] = im else: #2D images[rep,contrast,slice,0,:,:] = im # Show an image show.imshow(np.squeeze(images[0,0,0,:,:,:]))
# Reconstruct images images = np.zeros((nreps, ncontrasts, nslices, eNz, eNy, rNx), dtype=np.float32) for rep in range(nreps): for contrast in range(ncontrasts): for slice in range(nslices): # FFT if eNz > 1: #3D im = transform.transform_kspace_to_image( all_data[rep, contrast, slice, :, :, :, :], [1, 2, 3]) else: #2D im = transform.transform_kspace_to_image( all_data[rep, contrast, slice, :, 0, :, :], [1, 2]) # Sum of squares im = np.sqrt(np.sum(np.abs(im)**2, 0)) # Stuff into the output if eNz > 1: #3D images[rep, contrast, slice, :, :, :] = im else: #2D images[rep, contrast, slice, 0, :, :] = im # Show an image show.imshow(np.squeeze(images[0, 0, 0, :, :, :]))
def test_bssfp_data(self): '''Sample bSSFP data set.''' dset = pyport(file=self.sample, debug=False) import numpy as np import ismrmrd import ismrmrd.xsd from ismrmrdtools import show, transform header = ismrmrd.xsd.CreateFromDocument(dset.read_xml_header()) enc = header.encoding[0] # Matrix size eNx = enc.encodedSpace.matrixSize.x eNy = enc.encodedSpace.matrixSize.y eNz = enc.encodedSpace.matrixSize.z rNx = enc.reconSpace.matrixSize.x _rNy = enc.reconSpace.matrixSize.y _rNz = enc.reconSpace.matrixSize.z # Field of View _eFOVx = enc.encodedSpace.fieldOfView_mm.x _eFOVy = enc.encodedSpace.fieldOfView_mm.y _eFOVz = enc.encodedSpace.fieldOfView_mm.z _rFOVx = enc.reconSpace.fieldOfView_mm.x _rFOVy = enc.reconSpace.fieldOfView_mm.y _rFOVz = enc.reconSpace.fieldOfView_mm.z # Number of Slices, Reps, Contrasts, etc. ncoils = header.acquisitionSystemInformation.receiverChannels if enc.encodingLimits.slice is not None: nslices = enc.encodingLimits.slice.maximum + 1 else: nslices = 1 if enc.encodingLimits.average is not None: nreps = enc.encodingLimits.average.maximum + 1 else: nreps = 1 if enc.encodingLimits.contrast is not None: ncontrasts = enc.encodingLimits.contrast.maximum + 1 else: ncontrasts = 1 # TODO loop through the acquisitions looking for noise scans firstacq = 0 for acqnum in range(dset.number_of_acquisitions()): acq = dset.read_acquisition(acqnum) # TODO: Currently ignoring noise scans if acq.isFlagSet(ismrmrd.ACQ_IS_NOISE_MEASUREMENT): print("Found noise scan at acq ", acqnum) continue else: firstacq = acqnum print("Imaging acquisition starts acq ", acqnum) break # Initialiaze a storage array all_data = np.zeros( (nreps, ncontrasts, nslices, ncoils, eNz, eNy, rNx), dtype=np.complex64) #pylint: disable=E1101 # Loop through the rest of the acquisitions and stuff for acqnum in range(firstacq, dset.number_of_acquisitions()): acq = dset.read_acquisition(acqnum) # TODO: this is where we would apply noise pre-whitening # Remove oversampling if needed if eNx != rNx: xline = transform.transform_kspace_to_image(acq.data, [1]) x0 = int((eNx - rNx) / 2) x1 = int((eNx - rNx) / 2 + rNx) xline = xline[:, x0:x1] acq.resize(rNx, acq.active_channels, acq.trajectory_dimensions) #pylint: disable=E1101 acq.center_sample = int(rNx / 2) # need to use the [:] notation here to fill the data acq.data[:] = transform.transform_image_to_kspace(xline, [1]) # Stuff into the buffer rep = acq.idx.average #pylint: disable=E1101 contrast = acq.idx.contrast #pylint: disable=E1101 slise = acq.idx.slice #pylint: disable=E1101 y = acq.idx.kspace_encode_step_1 #pylint: disable=E1101 z = acq.idx.kspace_encode_step_2 #pylint: disable=E1101 all_data[rep, contrast, slise, :, z, y, :] = acq.data # Reconstruct images images = np.zeros((nreps, ncontrasts, nslices, eNz, eNy, rNx), dtype=np.float32) #pylint: disable=E1101 for rep in range(nreps): for contrast in range(ncontrasts): for slise in range(nslices): # FFT if eNz > 1: #3D im = transform.transform_kspace_to_image( all_data[rep, contrast, slise, ...], [1, 2, 3]) else: #2D im = transform.transform_kspace_to_image( all_data[rep, contrast, slise, :, 0, ...], [1, 2]) # Sum of squares im = np.sqrt(np.sum(np.abs(im)**2, 0)) # Stuff into the output if eNz > 1: #3D images[rep, contrast, slise, ...] = im else: #2D images[rep, contrast, slise, 0, ...] = im # Show an image show.imshow(np.squeeze(images[0, 0, 0, ...]))
all_data = all_data.astype('complex64') #%% # Coil combination coil_images = transform.transform_kspace_to_image( np.squeeze(np.mean(all_data, 0)), (1, 2)) (csm, rho) = coils.calculate_csm_walsh(coil_images) csm_ss = np.sum(csm * np.conj(csm), 0) csm_ss = csm_ss + 1.0 * (csm_ss < np.spacing(1)).astype('float32') if acc_factor > 1: coil_data = np.squeeze(np.mean(all_data, 0)) reload(grappa) (unmix, gmap) = grappa.calculate_grappa_unmixing(coil_data, acc_factor) #(unmix,gmap) = sense.calculate_sense_unmixing(acc_factor,csm) show.imshow(abs(gmap), colorbar=True, scale=(1, 2)) recon = np.zeros((nreps, ncontrasts, nslices, eNz, eNy, rNx), dtype=np.complex64) for r in range(0, nreps): recon_data = transform.transform_kspace_to_image( np.squeeze(all_data[r, :, :, :, :, :, :]), (1, 2)) * np.sqrt(acc_factor) if acc_factor > 1: recon[r, :, :, :, :] = np.sum(unmix * recon_data, 0) else: recon[r, :, :, :, :] = np.sum(np.conj(csm) * recon_data, 0) show.imshow(np.squeeze(np.std(np.abs(recon), 0)), colorbar=True, scale=(1, 2))
# -*- coding: utf-8 -*- #%% #Basic setup import time import numpy as np from ismrmrdtools import simulation, coils, show matrix_size = 256 csm = simulation.generate_birdcage_sensitivities(matrix_size) phan = simulation.phantom(matrix_size) coil_images = phan[np.newaxis, :, :] * csm show.imshow(abs(coil_images), tile_shape=(4, 2)) tstart = time.time() (csm_est, rho) = coils.calculate_csm_walsh(coil_images) print("Walsh coil estimation duration: {}s".format(time.time() - tstart)) combined_image = np.sum(csm_est * coil_images, axis=0) show.imshow(abs(csm_est), tile_shape=(4, 2), scale=(0, 1)) show.imshow(abs(combined_image), scale=(0, 1)) tstart = time.time() (csm_est2, rho2) = coils.calculate_csm_inati_iter(coil_images) print("Inati coil estimation duration: {}s".format(time.time() - tstart)) combined_image2 = np.sum(csm_est2 * coil_images, axis=0) show.imshow(abs(csm_est2), tile_shape=(4, 2), scale=(0, 1)) show.imshow(abs(combined_image2), scale=(0, 1))
# Basic setup import numpy as np import scipy as sp from ismrmrdtools import sense, grappa, show, simulation, transform, coils #%% # import some data exercise_data = sp.io.loadmat("hansen_exercises2.mat") csm = np.transpose(exercise_data["smaps"]) pat = np.transpose(exercise_data["sp"]) data = np.transpose(exercise_data["data"]) kspace = np.logical_or(pat == 1, pat == 3).astype("float32") * (data) acc_factor = 4 alias_img = transform.transform_kspace_to_image(kspace, dim=(1, 2)) * np.sqrt(acc_factor) show.imshow(abs(alias_img)) (unmix_grappa, gmap_grappa) = grappa.calculate_grappa_unmixing( data, acc_factor, data_mask=pat > 1, csm=csm, kernel_size=(4, 5) ) # (unmix_grappa,gmap_grappa) = grappa.calculate_grappa_unmixing(data, acc_factor, data_mask=pat>1) show.imshow(abs(gmap_grappa), colorbar=True) recon_grappa = np.squeeze(np.sum(alias_img * unmix_grappa, 0)) show.imshow(abs(recon_grappa), colorbar=True) sp.io.savemat( "tmp_data.mat", {"pat_py": pat, "data_py": data, "csm_py": csm, "alias_img_py": alias_img, "unmix_grappa_py": unmix_grappa}, ) #%%
y = acq.idx.kspace_encode_step_1 z = acq.idx.kspace_encode_step_2 all_data[rep, contrast, slice, :, z, y, :] = acq.data all_data = all_data.astype('complex64') #%% # Coil combination coil_images = transform.transform_kspace_to_image(np.squeeze(np.mean(all_data,0)),(1,2)) (csm,rho) = coils.calculate_csm_walsh(coil_images) csm_ss = np.sum(csm * np.conj(csm),0) csm_ss = csm_ss + 1.0*(csm_ss < np.spacing(1)).astype('float32') if acc_factor > 1: coil_data = np.squeeze(np.mean(all_data,0)) reload(grappa) (unmix,gmap) = grappa.calculate_grappa_unmixing(coil_data, acc_factor) #(unmix,gmap) = sense.calculate_sense_unmixing(acc_factor,csm) show.imshow(abs(gmap),colorbar=True,scale=(1,2)) recon = np.zeros((nreps, ncontrasts, nslices, eNz, eNy, rNx), dtype=np.complex64) for r in range(0,nreps): recon_data = transform.transform_kspace_to_image(np.squeeze(all_data[r,:,:,:,:,:,:]),(1,2))*np.sqrt(acc_factor) if acc_factor > 1: recon[r,:,:,:,:] = np.sum(unmix * recon_data,0) else: recon[r,:,:,:,:] = np.sum(np.conj(csm) * recon_data,0) show.imshow(np.squeeze(np.std(np.abs(recon),0)),colorbar=True,scale=(1,2))