def _apply_correction(data, dtheta, Hinv, use_pm, use_mp): """Apply the efficiency correction in eff to the data.""" # Identify active cross sections if use_pm and use_mp: parts = ALL_XS elif use_mp: parts = MP_XS elif use_pm: parts = PM_XS else: parts = NSF_XS # TODO: interpolation with mixed resolution # Interpolate data so that it aligns with ++. If smoothing is # desired, apply the interpolated smoothing before calling polcor, # in which case the interpolation does nothing. assert parts[0] == '++' x = data['++'].Qz y = [U(data['++'].v, data['++'].dv)] for p in parts[1:]: px = data[p].Qz py = U(data[p].v, data[p].dv) y.append(interp(x, px, py, left=np.NaN, right=np.NaN)) Y = np.vstack(y) # Look up correction matrix for each point using the ++ cross section correction_index = util.nearest(data['++'].angular_resolution, dtheta) # Apply the correction at each point X, dX = np.zeros(Y.shape), np.zeros(Y.shape) for point, polarization in enumerate(correction_index): x = Hinv[polarization] * UM(Y[:, point]).T X[:, point], dX[:, point] = nominal_values(x).flat, std_devs(x).flat # Put the corrected intensities back into the datasets # interpolate back to the original Qz in that dataset: for k, xs in enumerate(parts): x = data[xs].Qz px = data['++'].Qz py = U(X[k, :], dX[k, :]) y = interp(x, px, py, left=np.NaN, right=np.NaN) data[xs].v, data[xs].dv = nominal_values(y), std_devs(y) data[xs].vlabel = 'counts per incident count' data[xs].vunits = None
def plot_sa(data): """ Plot spin asymmetry data. """ from matplotlib import pyplot as plt from uncertainties.unumpy import uarray as U, nominal_values, std_devs from dataflow.lib.errutil import interp # TODO: interp doesn't test for matching resolution data = dict((d.polarization, d) for d in data) pp, mm = data['++'], data['--'] v_pp = U(pp.v, pp.dv) v_mm = interp(pp.x, mm.x, U(mm.v, mm.dv)) sa = (v_pp - v_mm) / (v_pp + v_mm) v, dv = nominal_values(sa), std_devs(sa) plt.errorbar(pp.x, v, yerr=dv, fmt='.', label=pp.name) plt.xlabel("%s (%s)"%(pp.xlabel, pp.xunits) if pp.xunits else pp.xlabel) plt.ylabel(r'$(R^{++} -\, R^{--}) / (R^{++} +\, R^{--})$')
def _interp_intensity(dT, data): return interp(dT, data.angular_resolution, U(data.v, data.dv))