def compute_reciprocal_errors(self, key="r"): # JG: BaseContainer? r""" Compute reciprocal erros following LaBrecque et al. (1996) according to: .. math:: \epsilon = \left|\frac{2(|R_n| - |R_r|)}{|R_n| + |R_r|}\right| Parameters ---------- key : str Parameter to calculate the reciprocal error for (default is "r"). Examples -------- >>> import reda >>> ert = reda.ERT() >>> ert.data = reda.utils.norrec.get_test_df() >>> ert.data = pd.DataFrame([ ... [1,2,3,4,95], ... [3,4,2,1,-105]], columns=list("abmnr") ... ) >>> ert.compute_reciprocal_errors() generating ids assigning ids >>> ert.data["error"].mean() == 0.1 True """ # Assign norrec ids if not already present if "id" not in self.data.keys(): self.data = assign_norrec_to_df(self.data) # Average repetitions data = average_repetitions(self.data, "r") # Get configurations with reciprocals data = data.groupby("id").filter(lambda b: not b.shape[0] == 1) n = self.data.shape[0] - data.shape[0] if n > 0: print("Could not find reciprocals for %d configurations" % n) # Calc reciprocal error grouped = data.groupby("id") def _error(group): R_n = group["r"].iloc[0] R_r = group["r"].iloc[1] return abs(2 * (abs(R_n) - abs(R_r)) / (abs(R_n) + abs(R_r))) error = grouped.apply(_error) error.name = "error" self.data = pd.merge(self.data, error.to_frame().reset_index(), how='outer', on='id')
def test_norrec_assignments1(): import reda.utils.norrec as redanr df = redanr.get_test_df() redanr.assign_norrec_to_df(df) df1 = redanr.average_repetitions(df, ['r', ]) g = df1.groupby('id') diffs_R = g['r'].diff() def apply_nr_diff(row): return diffs_R.iloc[row['id']] df1['norrec_diff'] = df1.apply(apply_nr_diff, axis=1)