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 setUp(self): self.species = sycomore.Species(1000*ms, 100*ms, 0.89*um*um/ms) self.m0 = sycomore.Magnetization(0, 0, 1) self.flip_angle = 40*deg self.pulse_duration = 1*ms self.pulse_support_size = 101 self.zero_crossings = 2 self.TR = 500*ms self.slice_thickness = 1*mm self.TR_count = 10
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_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 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])