def test_against_uncertainties_package(): try: from uncertainties import ufloat from uncertainties import umath from math import sqrt as math_sqrt except ImportError: return X, varX = 0.5, 0.04 Y, varY = 3, 0.09 N = 3 ux = ufloat(X, math_sqrt(varX)) uy = ufloat(Y, math_sqrt(varY)) def _compare(result, u): Z, varZ = result assert abs(Z-u.n)/u.n < 1e-13 and (varZ-u.s**2)/u.s**2 < 1e-13, \ "expected (%g,%g) got (%g,%g)"%(u.n,u.s**2,Z,varZ) def _check_pow(u): _compare(pow(X, varX, N), u) def _check_unary(op, u): _compare(op(X, varX), u) def _check_binary(op, u): _compare(op(X, varX, Y, varY), u) _check_pow(ux**N) _check_binary(add, ux+uy) _check_binary(sub, ux-uy) _check_binary(mul, ux*uy) _check_binary(div, ux/uy) _check_binary(pow2, ux**uy) _check_unary(exp, umath.exp(ux)) _check_unary(log, umath.log(ux)) _check_unary(sin, umath.sin(ux)) _check_unary(cos, umath.cos(ux)) _check_unary(tan, umath.tan(ux)) _check_unary(arcsin, umath.asin(ux)) _check_unary(arccos, umath.acos(ux)) _check_unary(arctan, umath.atan(ux)) _check_binary(arctan2, umath.atan2(ux, uy))
def test_against_uncertainties_package(): try: from uncertainties import ufloat from uncertainties import umath from math import sqrt as math_sqrt except ImportError: return X, varX = 0.5, 0.04 Y, varY = 3, 0.09 N = 3 ux = ufloat(X, math_sqrt(varX)) uy = ufloat(Y, math_sqrt(varY)) def _compare(result, u): Z, varZ = result assert abs(Z-u.n)/u.n < 1e-13 and (varZ-u.s**2)/u.s**2 < 1e-13, \ "expected (%g,%g) got (%g,%g)"%(u.n, u.s**2, Z, varZ) def _check_pow(u): _compare(pow(X, varX, N), u) def _check_unary(op, u): _compare(op(X, varX), u) def _check_binary(op, u): _compare(op(X, varX, Y, varY), u) _check_pow(ux**N) _check_binary(add, ux + uy) _check_binary(sub, ux - uy) _check_binary(mul, ux * uy) _check_binary(div, ux / uy) _check_binary(pow2, ux**uy) _check_unary(exp, umath.exp(ux)) _check_unary(log, umath.log(ux)) _check_unary(sin, umath.sin(ux)) _check_unary(cos, umath.cos(ux)) _check_unary(tan, umath.tan(ux)) _check_unary(arcsin, umath.asin(ux)) _check_unary(arccos, umath.acos(ux)) _check_unary(arctan, umath.atan(ux)) _check_binary(arctan2, umath.atan2(ux, uy))
def CartToSphU(cn, ce, cd): ''' CartToSphU converts from Cartesian to spherical coordinates CartToSphU(cn,ce,cd) returns the trend (trd) and plunge (plg) of a line for input north (cn), east (ce), and down (cd) direction cosines. Notice that the input direction cosines have uncertainties NOTE: Trend and plunge are returned in radians and they have uncertainties in radians CartToSphU uses function ZeroTwoPi It also uses the uncertainties package from Eric O. Lebigot Based on Python function CartToSph ''' pi = math.pi # Plunge plg = umath.asin(cd) # Eq. 4.13a # Trend: If north direction cosine is zero, trend # is east or west. Choose which one by the sign of # the east direction cosine if cn == 0.0: if ce < 0.0: trd = 3.0 / 2.0 * pi # Eq. 4.14d, trend is west else: trd = pi / 2.0 # Eq. 4.14c, trend is east # Else else: trd = umath.atan(ce / cn) # Eq. 4.14a if cn < 0.0: # Add pi trd = trd + pi # Eq. 4.14b # Make sure trend is between 0 and 2*pi trd = ZeroTwoPi(trd) return trd, plg
def lambda_from_abc(rF, rR, a, b, c): '''Returns the steer axis tilt, lamba, for the parameter set based on the offsets from the steer axis. Parameters ---------- rF : float or ufloat Front wheel radius. rR : float or ufloat Rear wheel radius. a : float or ufloat The rear wheel offset. The minimum distance from the steer axis to the center of the rear wheel. b : float or ufloat The front wheel offset. The minimum distance from the steer axis to the center of the front wheel. c : float or ufloat The steer axis distance. The distance along the steer axis between the intersection of the front and rear wheel offset lines. ''' def lam_equality(lam, rF, rR, a, b, c): return umath.sin(lam) - (rF - rR + c * umath.cos(lam)) / (a + b) guess = umath.atan(c / (a + b)) # guess based on equal wheel radii # The following assumes that the uncertainty calculated for the guess is # the same as the uncertainty for the true solution. This is not true! and # will surely breakdown the further the guess is away from the true # solution. There may be a way to calculate the correct uncertainity, but # that needs to be figured out. I guess I could use least squares and do it # the same way as get_period. args = (rF.nominal_value, rR.nominal_value, a.nominal_value, b.nominal_value, c.nominal_value) lam = newton(lam_equality, guess.nominal_value, args=args) return ufloat(lam, guess.std_dev)
def lambda_from_abc(rF, rR, a, b, c): '''Returns the steer axis tilt, lamba, for the parameter set based on the offsets from the steer axis. Parameters ---------- rF : float or ufloat Front wheel radius. rR : float or ufloat Rear wheel radius. a : float or ufloat The rear wheel offset. The minimum distance from the steer axis to the center of the rear wheel. b : float or ufloat The front wheel offset. The minimum distance from the steer axis to the center of the front wheel. c : float or ufloat The steer axis distance. The distance along the steer axis between the intersection of the front and rear wheel offset lines. ''' def lam_equality(lam, rF, rR, a, b, c): return umath.sin(lam) - (rF - rR + c * umath.cos(lam)) / (a + b) guess = umath.atan(c / (a + b)) # guess based on equal wheel radii # The following assumes that the uncertainty calculated for the guess is # the same as the uncertainty for the true solution. This is not true! and # will surely breakdown the further the guess is away from the true # solution. There may be a way to calculate the correct uncertainity, but # that needs to be figured out. I guess I could use least squares and do it # the same way as get_period. args = (rF.nominal_value, rR.nominal_value, a.nominal_value, b.nominal_value, c.nominal_value) lam = newton(lam_equality, guess.nominal_value, args=args) return ufloat((lam, guess.std_dev()))
def grid(path, name, key): metadata, data = read_nid(path) height, x, y = load_corrected_image(metadata, data, key=key) peaks = peak_local_max( height, min_distance=6, ).astype('float64') peaks[:, 0] = peaks[:, 0] * x.max() / height.shape[0] peaks[:, 1] = peaks[:, 1] * y.max() / height.shape[1] fig = plt.figure() ax = fig.add_subplot(1, 1, 1, aspect=1) plot = ax.pcolormesh(x, y, height, cmap='inferno') plot.set_rasterized(True) ax.plot(peaks[:, 1], peaks[:, 0], 'w.', ms=3) fig.colorbar(plot, ax=ax, label=r'$z \mathbin{/} \si{\nano\meter}$') # print(peaks) d1 = np.array([0, 0]) diagonal_points = [] for a in range(13): d1 = get_next_point_on_diagonal(d1, peaks) if len(d1) == 0: break diagonal_points.append(d1) diagonal_distances = np.array([ distance.euclidean(a, b) for a, b in zip(diagonal_points[:-1], diagonal_points[1:])] ) gd = ufloat(diagonal_distances.mean(), diagonal_distances.std()) print('Gitterkonstante a_1: {}'.format(gd)) diagonal_points = np.array(diagonal_points) x_coords = diagonal_points[:, 1] y_coords = diagonal_points[:, 0] popt, pcov = curve_fit(line, x_coords, y_coords) m, b = correlated_values(popt, pcov) xs = np.linspace(0, 2.44, 50) ax.plot(xs, line(xs, m.n, b.n), color='lightgray') angle_diagonal = umath.atan(m) # print(angle_diagonal) for d in diagonal_points: ax.plot(d[1], d[0], 'o', color='#46d7ff', alpha=0.7) d1 = [0.33, 0] horizontal_points = [] for a in range(10): d1 = get_next_point_on_horizontal(d1, peaks) if len(d1) == 0: break horizontal_points.append(d1) for d in horizontal_points: ax.plot(d[1], d[0], 'o', color='#dfec56', alpha=0.7) horizontal_points = np.array(horizontal_points) x_coords = horizontal_points[:, 1] y_coords = horizontal_points[:, 0] popt, pcov = curve_fit(line, x_coords, y_coords) m, b = correlated_values(popt, pcov) angle_horizontal = umath.atan(m) angle = angle_diagonal - angle_horizontal # should be 60 correction_factor = np.tan(np.pi/3)/umath.tan(angle) print('Gemessener Winkel ist {}. Soll ist 60. Korrekturfaktor ist {}'.format( rad2deg(angle), correction_factor )) ax.plot(xs, line(xs, m.n, b.n), color='lightgray') horizontal_distances = np.array([ distance.euclidean(a, b) for a, b in zip(horizontal_points[:-1], horizontal_points[1:]) ]) gh = ufloat(horizontal_distances.mean(), horizontal_distances.std()) print('Gitterkonstante a_2: {}'.format(gh)) ax.set_xlim(0, x.max()) ax.set_ylim(0, y.max()) g_string = '\SI{{{:.3f} \pm {:.3f}}}{{\\nano\\meter}}'.format(gh.n, gh.s) with open('build/grid_constant_horizontal_{}.tex'.format(name), 'w') as f: f.write(g_string) g_string = r'$\num{{{:.2f} \pm {:.2f}}}$'.format(correction_factor.n, correction_factor.s) with open('build/correction_factor_{}.tex'.format(name), 'w') as f: f.write(g_string) g_string = '\SI{{{:.3f} \pm {:.3f}}}{{\\nano\\meter}}'.format(gd.n, gd.s) with open('build/grid_constant_diagonal_{}.tex'.format(name), 'w') as f: f.write(g_string) angle = angle_diagonal - angle_horizontal g_string = '\\ang{{{:.2f} \pm {:.2f}}}'.format(np.rad2deg(angle.n), np.rad2deg(angle.s)) with open('build/grid_angle_{}.tex'.format(name), 'w') as f: f.write(g_string) # plt.show() ax.set_xlabel(r'$x \mathbin{/} \si{\nano\meter}$') ax.set_ylabel(r'$y \mathbin{/} \si{\nano\meter}$') fig.tight_layout(pad=0) fig.savefig('build/plots/hopg_{}.pdf'.format(name))