def test_one_important_number(some_numbers): errors = np.ones(10) errors[1:] = np.inf assert stats.circular_mean(some_numbers, errors=errors) == some_numbers[0] with pytest.warns(RuntimeWarning): assert stats.circular_error(some_numbers, errors=errors, t_value_corr=False) ==\ stats.circular_error(errors[0], errors=1., t_value_corr=False)
def test_two_important_number(some_numbers): errors = np.ones(10) errors[2:] = np.inf assert stats.circular_mean( some_numbers, errors=errors) == stats.circular_mean(some_numbers[:2]) assert stats.circular_error(some_numbers, errors=errors, t_value_corr=False) ==\ stats.circular_error(some_numbers[:2], errors=np.ones(2), t_value_corr=False)
def test_circular_empties(): empty = np.array([]) with pytest.warns(RuntimeWarning): stats.circular_mean(empty) with pytest.warns(RuntimeWarning): stats.circular_error(empty)
def test_circular_nans(a_nan): assert np.isnan(stats.circular_mean(a_nan)) assert np.isnan(stats.circular_error(a_nan))
def test_circular_zeros(zeros): assert stats.circular_mean(zeros) == 0 assert stats.circular_error(zeros) == 0
def get_phases(meas_input, input_files, model, plane, compensate=None, no_errors=False): """ Computes phase advances among all BPMs. Args: meas_input: OpticsInput object input_files: InputFiles object model: model tfs_panda to be used plane: "X" or "Y" compensate: (driven_tune,free_tune,ac2bpm object) no_errors: if True measured errors shall not be propagated (only their spread) Returns: dictionary of DataFrames indexed (BPMi x BPMj) yielding phase advance phi_ij "MEAS" measured phase advances "ERRMEAS" errors of measured phase advances "MODEL" model phase advances +------++--------+--------+--------+--------+ | || BPM1 | BPM2 | BPM3 | BPM4 | +======++========+========+========+========+ | BPM1 || 0 | phi_12 | phi_13 | phi_14 | +------++--------+--------+--------+--------+ | BPM2 || phi_21 | 0 | phi_23 | phi_24 | +------++--------+--------+--------+--------+ | BPM3 || phi_31 | phi_32 | 0 | phi_34 | +------++--------+--------+--------+--------+ The phase advance between BPM_i and BPM_j can be obtained via: phase_advances["MEAS"].loc[BPMi,BPMj] list of output data frames(for files) """ phase_frame = pd.DataFrame(model).loc[:, ['S', 'MU' + plane]] how = 'outer' if meas_input.union else 'inner' phase_frame = pd.merge(phase_frame, input_files.joined_frame( plane, ['MU' + plane, 'ERR_MU' + plane], zero_dpp=True, how=how), how='inner', left_index=True, right_index=True) phases_mdl = phase_frame.loc[:, 'MU' + plane].values phase_advances = { "MODEL": _get_square_data_frame( (phases_mdl[np.newaxis, :] - phases_mdl[:, np.newaxis]) % 1.0, phase_frame.index) } phases_meas = input_files.get_data( phase_frame, 'MU' + plane) * meas_input.accelerator.get_beam_direction() phases_errors = input_files.get_data(phase_frame, 'ERR_MU' + plane) if compensate is not None: (driven_tune, free_tune, ac2bpmac) = compensate k_bpmac = ac2bpmac[2] phase_corr = ac2bpmac[1] - phases_meas[k_bpmac] + (0.5 * driven_tune) phases_meas = phases_meas + phase_corr[np.newaxis, :] r = get_lambda(driven_tune % 1.0, free_tune % 1.0) phases_meas[k_bpmac:, :] = phases_meas[k_bpmac:, :] - driven_tune psi = (np.arctan((1 - r) / (1 + r) * np.tan(2 * np.pi * phases_meas)) / (2 * np.pi)) % 0.5 phases_meas = np.where(phases_meas % 1.0 > 0.5, psi + .5, psi) phases_meas[k_bpmac:, :] = phases_meas[k_bpmac:, :] + free_tune if phases_meas.ndim < 2: phase_advances["MEAS"] = _get_square_data_frame( (phases_meas[np.newaxis, :] - phases_meas[:, np.newaxis]) % 1.0, phase_frame.index) phase_advances["ERRMEAS"] = _get_square_data_frame( np.zeros((len(phases_meas), len(phases_meas))), phase_frame.index) return phase_advances if meas_input.union: mask = np.isnan(phases_meas) phases_meas[mask], phases_errors[mask] = 0.0, np.inf if no_errors: phases_errors[~mask] = 1e-10 elif no_errors: phases_errors = None phases_3d = phases_meas[np.newaxis, :, :] - phases_meas[:, np.newaxis, :] if phases_errors is not None: errors_3d = phases_errors[ np.newaxis, :, :] + phases_errors[:, np.newaxis, :] else: errors_3d = None phase_advances["MEAS"] = _get_square_data_frame( stats.circular_mean(phases_3d, period=1, errors=errors_3d, axis=2) % 1.0, phase_frame.index) phase_advances["ERRMEAS"] = _get_square_data_frame( stats.circular_error(phases_3d, period=1, errors=errors_3d, axis=2), phase_frame.index) return phase_advances, [ _create_output_df(phase_advances, phase_frame, plane), _create_output_df(phase_advances, phase_frame, plane, tot=True) ]
def get_phases(meas_input, input_files, model, plane, compensate=None, no_errors=False): """ Computes phase advances among all BPMs. Args: meas_input: OpticsInput object input_files: InputFiles object model: model tfs_panda to be used plane: "X" or "Y" compensate: (driven_tune,free_tune,ac2bpm object) no_errors: if True measured errors shall not be propagated (only their spread) Returns: dictionary of DataFrames indexed (BPMi x BPMj) yielding phase advance phi_ij "MEAS" measured phase advances "ERRMEAS" errors of measured phase advances "MODEL" model phase advances +------++--------+--------+--------+--------+ | || BPM1 | BPM2 | BPM3 | BPM4 | +======++========+========+========+========+ | BPM1 || 0 | phi_12 | phi_13 | phi_14 | +------++--------+--------+--------+--------+ | BPM2 || phi_21 | 0 | phi_23 | phi_24 | +------++--------+--------+--------+--------+ | BPM3 || phi_31 | phi_32 | 0 | phi_34 | +------++--------+--------+--------+--------+ The phase advance between BPM_i and BPM_j can be obtained via: phase_advances["MEAS"].loc[BPMi,BPMj] list of output data frames(for files) """ phase_frame = pd.DataFrame(model).loc[:, ['S', 'MU' + plane]] how = 'outer' if meas_input.union else 'inner' phase_frame = pd.merge(phase_frame, input_files.joined_frame(plane, ['MU' + plane, 'ERR_MU' + plane], zero_dpp=True, how=how), how='inner', left_index=True, right_index=True) phases_mdl = phase_frame.loc[:, 'MU' + plane].values phase_advances = {"MODEL": _get_square_data_frame( (phases_mdl[np.newaxis, :] - phases_mdl[:, np.newaxis]) % 1.0, phase_frame.index)} phases_meas = input_files.get_data(phase_frame, 'MU' + plane) * meas_input.accelerator.get_beam_direction() phases_errors = input_files.get_data(phase_frame, 'ERR_MU' + plane) if compensate is not None: (driven_tune, free_tune, ac2bpmac) = compensate k_bpmac = ac2bpmac[2] phase_corr = ac2bpmac[1] - phases_meas[k_bpmac] + (0.5 * driven_tune) phases_meas = phases_meas + phase_corr[np.newaxis, :] r = get_lambda(driven_tune % 1.0, free_tune % 1.0) phases_meas[k_bpmac:, :] = phases_meas[k_bpmac:, :] - driven_tune psi = (np.arctan((1 - r) / (1 + r) * np.tan(2 * np.pi * phases_meas)) / (2 * np.pi)) % 0.5 phases_meas = np.where(phases_meas % 1.0 > 0.5, psi + .5, psi) phases_meas[k_bpmac:, :] = phases_meas[k_bpmac:, :] + free_tune if phases_meas.ndim < 2: phase_advances["MEAS"] = _get_square_data_frame( (phases_meas[np.newaxis, :] - phases_meas[:, np.newaxis]) % 1.0, phase_frame.index) phase_advances["ERRMEAS"] = _get_square_data_frame( np.zeros((len(phases_meas), len(phases_meas))), phase_frame.index) return phase_advances if meas_input.union: mask = np.isnan(phases_meas) phases_meas[mask], phases_errors[mask] = 0.0, np.inf if no_errors: phases_errors[~mask] = 1e-10 elif no_errors: phases_errors = None phases_3d = phases_meas[np.newaxis, :, :] - phases_meas[:, np.newaxis, :] if phases_errors is not None: errors_3d = phases_errors[np.newaxis, :, :] + phases_errors[:, np.newaxis, :] else: errors_3d = None phase_advances["MEAS"] = _get_square_data_frame(stats.circular_mean( phases_3d, period=1, errors=errors_3d, axis=2) % 1.0, phase_frame.index) phase_advances["ERRMEAS"] = _get_square_data_frame(stats.circular_error( phases_3d, period=1, errors=errors_3d, axis=2), phase_frame.index) return phase_advances, [_create_output_df(phase_advances, phase_frame, plane), _create_output_df(phase_advances, phase_frame, plane, tot=True)]