def test_constructor_quantities(self): pulse_1 = sycomore.Pulse(1 * deg, 2 * deg) self.assertEqual(pulse_1.angle, 1 * deg) self.assertEqual(pulse_1.phase, 2 * deg) pulse_2 = sycomore.Pulse(2 * deg) self.assertEqual(pulse_2.angle, 2 * deg) self.assertEqual(pulse_2.phase, 0 * deg) with self.assertRaises(Exception): sycomore.Pulse(m, rad) with self.assertRaises(Exception): sycomore.Pulse(rad, m)
def test_diffusion(self): model = sycomore.como.Model( sycomore.Species(0 * Hz, 0 * Hz, 1 * um * um / ms), sycomore.Magnetization(0, 0, 1), [["foo", sycomore.TimeInterval(500 * ms, 0.1 * rad / um)]]) model.apply_pulse(sycomore.Pulse(40 * deg, 0 * deg)) model.apply_time_interval("foo") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0 + 0.003062528150606j) elif index == sycomore.Index(0): self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0.766044443118978) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1): self.assertAlmostEqual(grid[index].p, 0 - 0.003062528150606j) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0)
def test_ideal(self): model = sycomore.como.Model( self.species, self.m0, [["half_echo", sycomore.TimeInterval(self.TR/2.)]]) magnetization = [] for i in range(self.TR_count): model.apply_pulse( sycomore.Pulse(self.flip_angle, (math.pi/3+(i%2)*math.pi)*rad)) model.apply_time_interval("half_echo") magnetization.append(model.isochromat()) model.apply_time_interval("half_echo") root = os.environ["SYCOMORE_TEST_DATA"] with open(os.path.join(root, "baseline", "GRE_ideal.dat"), "rb") as fd: contents = fd.read() baseline = struct.unpack((int(len(contents)/8))*"d", contents) self.assertEqual(len(baseline), 3*self.TR_count) for i in range(self.TR_count): m_test = magnetization[i] m_baseline = baseline[3*i:3*(i+1)] self.assertAlmostEqual(m_test[0], m_baseline[0]) self.assertAlmostEqual(m_test[1], m_baseline[1]) self.assertAlmostEqual(m_test[2], m_baseline[2])
def test_off_resonance(self): species = sycomore.Species(0 * Hz, 0 * Hz, 0 * um * um / ms) m0 = sycomore.Magnetization(0, 0, 1) pulse = sycomore.Pulse(90 * deg, math.pi * rad) pulse_duration = 1 * ms pulse_support_size = 101 zero_crossings = 2 # NOTE: in the absence of relaxation and diffusion, the TR is meaningless TR = 500 * ms slice_thickness = 1 * mm t0 = pulse_duration / (2 * zero_crossings) sinc_pulse = sycomore.HardPulseApproximation( pulse, sycomore.linspace(pulse_duration, pulse_support_size), sycomore.sinc_envelope(t0), 1 / t0, slice_thickness, "rf") refocalization = sycomore.TimeInterval( (TR - pulse_duration) / 2., -sinc_pulse.get_gradient_moment() / 2) model = sycomore.como.Model( species, m0, [["rf", sinc_pulse.get_time_interval()], ["refocalization", refocalization]]) model.apply_pulse(sinc_pulse) model.apply_time_interval("refocalization") frequencies = sycomore.linspace(60. * rad / ms, 201) magnetization = [ model.isochromat(set(), sycomore.Point(), f) for f in frequencies ] root = os.environ["SYCOMORE_TEST_DATA"] with open(os.path.join(root, "baseline", "off_resonance.dat"), "rb") as fd: contents = fd.read() baseline = struct.unpack((int(len(contents) / 8)) * "d", contents) self.assertEqual(len(baseline), 2 * len(magnetization)) for i in range(len(magnetization)): self.assertAlmostEqual(sycomore.transversal(magnetization[i]), baseline[2 * i]) self.assertAlmostEqual(magnetization[i][2], baseline[2 * i + 1])
def test_pulse(self): model = sycomore.como.Model( sycomore.Species(1 * s, 0.1 * s), sycomore.Magnetization(0, 0, 1), [["dummy", sycomore.TimeInterval(0 * s)]]) model.apply_pulse(sycomore.Pulse(41 * deg, 27 * deg)) grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(0): self.assertAlmostEqual(grid[index].p, 0.210607912662250 - 0.413341301933443j) self.assertAlmostEqual(grid[index].z, 0.754709580222772) self.assertAlmostEqual(grid[index].m, 0.210607912662250 + 0.413341301933443j) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0)
def test_real(self): t0 = self.pulse_duration/(2*self.zero_crossings) sinc_pulse = sycomore.HardPulseApproximation( sycomore.Pulse(self.flip_angle, 0*rad), sycomore.linspace(self.pulse_duration, self.pulse_support_size), sycomore.sinc_envelope(t0), 1/t0, self.slice_thickness, "rf") half_echo = sycomore.TimeInterval( (self.TR-self.pulse_duration)/2., -sinc_pulse.get_gradient_moment()/2) model = sycomore.como.Model( self.species, self.m0, [ ["rf", sinc_pulse.get_time_interval()], ["half_echo", half_echo]]) magnetization = [] for i in range(self.TR_count): sinc_pulse.set_phase((math.pi/3+(i%2)*math.pi)*rad) model.apply_pulse(sinc_pulse) model.apply_time_interval("half_echo") magnetization.append(model.isochromat()) model.apply_time_interval("half_echo") root = os.environ["SYCOMORE_TEST_DATA"] with open(os.path.join(root, "baseline", "GRE_real.dat"), "rb") as fd: contents = fd.read() baseline = struct.unpack((int(len(contents)/8))*"d", contents) self.assertEqual(len(baseline), 3*self.TR_count) for i in range(self.TR_count): m_test = magnetization[i] m_baseline = baseline[3*i:3*(i+1)] self.assertAlmostEqual(m_test[0], m_baseline[0]) self.assertAlmostEqual(m_test[1], m_baseline[1]) self.assertAlmostEqual(m_test[2], m_baseline[2])
def test_time_interval(self): model = sycomore.como.Model( sycomore.Species(math.log(2) * Hz, math.log(2) * Hz), sycomore.Magnetization(0, 0, 1), [["foo", sycomore.TimeInterval(1 * s)], ["bar", sycomore.TimeInterval(1 * s)]]) model.apply_pulse(sycomore.Pulse(45 * deg, 90 * deg)) model.apply_time_interval("foo") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0.25) elif index == sycomore.Index(0, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0.5 * (1 + math.sqrt(2) / 2)) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1, 0): self.assertAlmostEqual(grid[index].p, 0.25) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0) model.apply_time_interval("bar") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1, -1): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0.125) elif index == sycomore.Index(0, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0.5 + 0.25 * (1 + math.sqrt(2) / 2)) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1, 1): self.assertAlmostEqual(grid[index].p, 0.125) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0) isochromat = model.isochromat() self.assertAlmostEqual(isochromat[0], 0.125 * math.sqrt(2)) self.assertAlmostEqual(isochromat[1], 0) self.assertAlmostEqual(isochromat[2], 0.5 + 0.25 * (1 + math.sqrt(2) / 2)) isochromat = model.isochromat( {sycomore.Index(0, 0), sycomore.Index(-1, -1)}) self.assertAlmostEqual(isochromat[0], 0.125 * math.sqrt(2) / 2) self.assertAlmostEqual(isochromat[1], 0) self.assertAlmostEqual(isochromat[2], 0.5 + 0.25 * (1 + math.sqrt(2) / 2))
def update(): start = time.time() slice_thickness = 1 * mm document = bokeh.plotting.curdoc() pulse_support_size = 101 T1 = document.get_model_by_id("T1").value * ms T2 = document.get_model_by_id("T2").value * ms flip_angle = document.get_model_by_id("flip_angle").value * deg duration = document.get_model_by_id("duration").value * ms zero_crossings = document.get_model_by_id("zero_crossings").value t0 = duration / (2 * zero_crossings) support = sycomore.linspace(duration, pulse_support_size) envelope = sycomore.sinc_envelope(t0) bandwidth = 1 / t0 sinc_pulse = sycomore.HardPulseApproximation( sycomore.Pulse(flip_angle, 0 * deg), support, envelope, bandwidth, slice_thickness, "") gradient_duration = sinc_pulse.get_time_interval().duration gradient_amplitude = (sinc_pulse.get_time_interval().gradient_moment[2] / (2 * numpy.pi * sycomore.gamma) / sinc_pulse.get_time_interval().duration) species = sycomore.Species(T1, T2) model = sycomore.epg.Discrete3D(species) for index, hard_pulse in enumerate(sinc_pulse.get_pulses()): model.apply_pulse(hard_pulse.angle, hard_pulse.phase) model.apply_time_interval(gradient_duration, [0 * T / m, 0 * T / m, gradient_amplitude]) # Unfold the F and the Z states: create an array for all orders, including # empty ones. max_order = numpy.max(model.orders, axis=0)[2] max_bin = int(max_order / model.bin_width) F = numpy.zeros(2 * max_bin + 1, model.states.dtype) Z = numpy.zeros(2 * max_bin + 1, model.states.dtype) for order, state in zip(model.orders, model.states): bin = int(order[2] / model.bin_width) # WARNING: since we de-bin the orders, we need to scale the population F[bin] = F.shape[0] * state[0] Z[bin] = F.shape[0] * state[2] if order != 0: F[-bin] = F.shape[0] * state[1].conj() Z[-bin] = F.shape[0] * state[2] # Perform iFFT, and shift it since the spatial axis must be centered on zero. M_transversal = numpy.fft.fftshift(numpy.fft.ifft(F)) M_longitudinal = numpy.fft.fftshift(numpy.fft.ifft(Z)) # Frequency ranges from -max_order to +max_order: the spatial step size # is then given by the following expression. step = (1 / (2 * max_order)).convert_to(mm) x_axis = step * numpy.arange(len(M_transversal)) x_axis -= 0.5 * (x_axis[0] + x_axis[-1]) # Crop between [-slice_thickness, +slice_thickness] slice_ = ( numpy.searchsorted(x_axis, -slice_thickness.convert_to(mm), "left"), numpy.searchsorted(x_axis, +slice_thickness.convert_to(mm), "right"), ) x_axis = x_axis[slice_[0]:slice_[1]] M_transversal = M_transversal[slice_[0]:slice_[1]] M_longitudinal = M_longitudinal[slice_[0]:slice_[1]] transversal_data = document.get_model_by_id("transversal_data") transversal_data.data = {"x": x_axis, "y": numpy.abs(M_transversal)} longitudinal_data = document.get_model_by_id("longitudinal_data") longitudinal_data.data = {"x": x_axis, "y": numpy.abs(M_longitudinal)} stop = time.time() document.get_model_by_id("runtime").text = "Runtime: {}".format( utils.to_eng_string(stop - start, "s", 3))
def test_pulse_profile(self): species = sycomore.Species(0*Hz, 0*Hz, 0*um*um/ms) m0 = sycomore.Magnetization(0, 0, 1) pulse = sycomore.Pulse(90*deg, math.pi*rad) pulse_duration = 1*ms pulse_support_size = 101 zero_crossings = 2 # NOTE: in the absence of relaxation and diffusion, the TR is meaningless TR = 500*ms; slice_thickness = 1*mm; sampling_support_size = 501 t0 = pulse_duration/(2*zero_crossings) sinc_pulse = sycomore.HardPulseApproximation( pulse, sycomore.linspace(pulse_duration, pulse_support_size), sycomore.sinc_envelope(t0), 1/t0, slice_thickness, "rf") refocalization = sycomore.TimeInterval( (TR-pulse_duration)/2., -sinc_pulse.get_gradient_moment()/2) sampling_locations = sycomore.linspace( sycomore.Point(0*m, 0*m, 2*slice_thickness), sampling_support_size) model = sycomore.como.Model( species, m0, [ ["rf", sinc_pulse.get_time_interval()], ["refocalization", refocalization]]) model.apply_pulse(sinc_pulse) before_refocalization = [ model.isochromat(set(), p) for p in sampling_locations] model.apply_time_interval("refocalization") after_refocalization = [ model.isochromat(set(), p) for p in sampling_locations] root = os.environ["SYCOMORE_TEST_DATA"] with open(os.path.join(root, "baseline", "pulse_profile.dat"), "rb") as fd: contents = fd.read() baseline = struct.unpack((int(len(contents)/8))*"d", contents) self.assertEqual(len(baseline), 2*3*len(sampling_locations)) for i in range(len(sampling_locations)): m_test = before_refocalization[i] m_baseline = baseline[3*i:3*(i+1)] self.assertAlmostEqual(m_test[0], m_baseline[0]) self.assertAlmostEqual(m_test[1], m_baseline[1]) self.assertAlmostEqual(m_test[2], m_baseline[2]) for i in range(len(sampling_locations)): m_test = after_refocalization[i] m_baseline = baseline[ 3*(i+len(sampling_locations)):3*(i+len(sampling_locations)+1)] self.assertAlmostEqual(m_test[0], m_baseline[0]) self.assertAlmostEqual(m_test[1], m_baseline[1]) self.assertAlmostEqual(m_test[2], m_baseline[2])