def loss(theta1, theta2, angles_rad, TE, TR): T = len(angles_rad) x1 = epg.FSE_signal(angles_rad, TE, theta1['T1'], theta1['T2']) * (1 - exp(-(TR - T * TE)/theta1['T1'])) x2 = epg.FSE_signal(angles_rad, TE, theta2['T1'], theta2['T2']) * (1 - exp(-(TR - T * TE)/theta2['T1'])) return 0.5 * np.linalg.norm(x1, ord=2)**2 + 0.5 * np.linalg.norm(x2, ord=2)**2 - np.dot(x1.ravel(), x2.ravel())
def loss_prime(theta1, theta2, angles_rad, TE, TR): T = len(angles_rad) x1 = epg.FSE_signal(angles_rad, TE, theta1['T1'], theta1['T2']).ravel() * ( 1 - exp(-(TR - T * TE) / theta1['T1'])) x2 = epg.FSE_signal(angles_rad, TE, theta2['T1'], theta2['T2']).ravel() * ( 1 - exp(-(TR - T * TE) / theta2['T1'])) T = len(angles_rad) alpha_prime = np.zeros((T, )) for i in range(T): x1_prime = epg.FSE_signal_prime_alpha_idx( angles_rad, TE, theta1['T1'], theta1['T2'], i).ravel() * (1 - exp(-(TR - T * TE) / theta1['T1'])) x2_prime = epg.FSE_signal_prime_alpha_idx( angles_rad, TE, theta2['T1'], theta2['T2'], i).ravel() * (1 - exp(-(TR - T * TE) / theta2['T1'])) M1 = np.dot(x1, x1_prime) M2 = np.dot(x2, x2_prime) M3 = np.dot(x1, x2_prime) M4 = np.dot(x2, x1_prime) alpha_prime[i] = M1 + M2 - M3 - M4 return alpha_prime
def normalized_loss(theta1, theta2, angles_rad, TE, TR): T = len(angles_rad) x1 = epg.FSE_signal(angles_rad, TE, theta1['T1'], theta1['T2']) * (1 - exp(-(TR - T * TE)/theta1['T1'])) x2 = epg.FSE_signal(angles_rad, TE, theta2['T1'], theta2['T2']) * (1 - exp(-(TR - T * TE)/theta2['T1'])) x1 = x1 / np.linalg.norm(x1, ord=2) x2 = x2 / np.linalg.norm(x2, ord=2) return - np.dot(x1.ravel(), x2.ravel())
def plot_vals(self, thetas): plt.subplot(2,1,1) plt.plot(range(self.T), RAD2DEG(self.angles_rad), 'o-') plt.xlim((0,self.T)) plt.subplot(2,1,2) for theta in thetas: plt.plot(range(self.T), epg.FSE_signal(self.angles_rad, self.TE, theta['T1'], theta['T2'])) plt.xlim((0,self.T)) plt.ylim((0,1))
def loss(thetas, angles_rad, TE, TR): T = len(angles_rad) l = 0 for theta in thetas: x1 = epg.FSE_signal(angles_rad, TE, theta['T1'], theta['T2']) * (1 - exp(-(TR - T * TE)/theta['T1'])) l += 0.5 * np.dot(x1.ravel(), x1.ravel()) return l
def loss_prime(thetas, angles_rad, TE, TR): T = len(angles_rad) alpha_prime = np.zeros((T,)) for theta in thetas: x1 = epg.FSE_signal(angles_rad, TE, theta['T1'], theta['T2']).ravel() * (1 - exp(-(TR - T * TE)/theta['T1'])) for i in range(T): x1_prime = epg.FSE_signal_prime_alpha_idx(angles_rad, TE, theta['T1'], theta['T2'], i).ravel() * (1 - exp(-(TR - T * TE)/theta['T1'])) alpha_prime[i] += np.dot(x1, x1_prime) return alpha_prime
def t1t2shuffle_prime_alpha_idx(angles_rad, TE, TRs, M0, T1, T2, idx): """derivative of signal equation w.r.t. the i'th flip angle""" T = angles_rad.size URs = TRs - (T+1)*TE # T + 1 to account for fast recovery fi = epg.FSE_signal(angles_rad, TE, T1, T2) fi_prime = epg.FSE_signal_prime_alpha_idx(angles_rad, TE, T1, T2, idx) Ej = T1_recovery(URs, T1)[None,:] a = 1 - Ej b = 1 - Ej * fi[-1] sig_prime = M0 * a / b**2 * (fi_prime * b + fi * Ej * fi_prime[-1]) return sig_prime.ravel(order='F')
def plot_vals(self, thetas): plt.subplot(2,1,1) plt.plot(range(self.T), RAD2DEG(self.angles_rad), 'o-') plt.title('ETL={} POW={:.1f} MAX={:.0f} MIN={:.0f}'.format( self.T, calc_power(self.angles_rad), RAD2DEG(np.max(self.angles_rad)), RAD2DEG(np.min(self.angles_rad)))) plt.xlim((0, self.T)) #plt.ylim((np.max((0,.9*np.min(RAD2DEG(self.angles_rad))), 180))) plt.ylim((.5*np.min(RAD2DEG(self.angles_rad)), 180)) plt.ylabel('flip angles (deg)') plt.subplot(2,1,2) #leg_str = [] for theta in thetas: plt.plot(range(self.T), epg.FSE_signal(self.angles_rad, self.TE, theta['T1'], theta['T2']) * (1 - exp(-(self.TR - self.T * self.TE)/theta['T1']))) #leg_str.append('T1/T2={:.0f}/{:.0f}'.format(1000*theta['T1'], 1000*theta['T2'])) #plt.legend(leg_str) plt.xlim((0,self.T)) plt.ylim((0, 1.)) plt.ylabel('signal level')
def forward(self, theta): return epg.FSE_signal(self.angles_rad, TE, theta['T1'], theta['T2']).ravel()
T = int(sys.argv[1]) else: T = 10 angles = 150 * np.ones((T, )) angles = read_angles('../data/flipangles.txt.408183520') TT = len(angles) if TT < T: T = TT else: angles = angles[:T] angles_rad = DEG2RAD(angles) S = epg.FSE_signal(angles_rad, TE, T1, T2) S2 = abs(S) theta1 = {'T1': 1000e-3, 'T2': 20e-3} theta2 = {'T1': 1000e-3, 'T2': 100e-3} t1 = time.time() NG = numerical_gradient(theta1, theta2, angles_rad, TE, TR) t2 = time.time() LP = loss_prime(theta1, theta2, angles_rad, TE, TR) t3 = time.time() NG_time = t2 - t1 LP_time = t3 - t2 print 'Numerical Gradient\t(%03.3f s)\t' % NG_time, NG