def test_significant_digits(): s = significant_digits(0.637282, 1e-3) assert s[0] == "0.6373" s = significant_digits(0.9837473385, 0.000065323) assert s[0] == "0.98375" assert s[1] == "0.00007" s = significant_digits(0.0338577, 0.0015473) assert s[0] == "0.0339" assert s[1] == "0.0015"
def calc_beta_inst(name, position, results_df, magnet1_df, magnet2_df, kmod_input_params): betas = np.zeros((2, 2)) sign = np.array([[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]]) for i, plane in enumerate(PLANES): waist = results_df.loc[:, f"{WAIST}{plane}"].values if magnet1_df.headers['POLARITY'] == 1 and magnet2_df.headers[ 'POLARITY'] == -1: waist = -waist if plane == 'Y': waist = -waist beta = propagate_beta_in_drift( (results_df.loc[:, f"{BETA}{WAIST}{plane}"].values + sign[:, 0] * results_df.loc[:, f"{ERR}{BETA}{WAIST}{plane}"].values), ((waist - position) + sign[:, 1] * results_df.loc[:, f"{ERR}{WAIST}{plane}"].values)) beta_err = get_err(beta[1::2] - beta[0]) if kmod_input_params.no_sig_digits: betas[i, 0], betas[i, 1] = beta[0], beta_err else: betas[i, 0], betas[i, 1] = tfstools.significant_digits( beta[0], beta_err, return_floats=True) return name, betas[0, 0], betas[0, 1], betas[1, 0], betas[1, 1]
def calc_betastar(kmod_input_params, results_df, l_star): sign = return_sign_for_err(2) for plane in PLANES: betastar = propagate_beta_in_drift( (results_df.loc[:, f"{BETA}{WAIST}{plane}"].values + sign[:, 0] * results_df.loc[:, f"{ERR}{BETA}{WAIST}{plane}"].values), (results_df.loc[:, f"{WAIST}{plane}"].values + sign[:, 1] * results_df.loc[:, f"{ERR}{WAIST}{plane}"].values)) betastar_err = get_err(betastar[1::2] - betastar[0]) if kmod_input_params.no_sig_digits: results_df[f"{BETA}{STAR}{plane}"], results_df[ f"{ERR}{BETA}{STAR}{plane}"] = (betastar[0], betastar_err) else: results_df[f"{BETA}{STAR}{plane}"], results_df[ f"{ERR}{BETA}{STAR}{plane}"] = tfstools.significant_digits( betastar[0], betastar_err, return_floats=True) # reindex df to put betastar first cols = results_df.columns.tolist() cols = [cols[0]] + cols[-4:] + cols[1:-4] results_df = results_df.reindex(columns=cols) for plane in PLANES: results_df[f"{PHASEADV}{plane}"], results_df[ f"{ERR}{PHASEADV}{plane}"] = phase_adv_from_kmod( l_star, betastar[0], betastar_err, results_df.loc[:, f"{WAIST}{plane}"].values, results_df.loc[:, f"{ERR}{WAIST}{plane}"].values) return results_df
def test_significant_digits(): s = significant_digits(0.637282, 1e-3) assert s[0] == "0.6373" s = significant_digits(0.9837473385, 0.000065323) assert s[0] == "0.98375" assert s[1] == "0.00007" s = significant_digits(0.0338577, 0.0015473) assert s[0] == "0.0339" assert s[1] == "0.0015" s = significant_digits(0.0338577, 0.0015473, return_floats=True) assert s[0] == 0.0339 assert s[1] == 0.0015 with pytest.raises(ValueError): s = significant_digits(0.0338577, 0.0)
def _get_scaled_labels(val, std, scale): scaled_vas, scaled_std = val*scale, std*scale if abs(scaled_std) > 1 or scaled_std == 0: return f'{int(round(scaled_vas)):d}', f'{int(round(scaled_std)):d}' return significant_digits(scaled_vas, scaled_std)