def calculate_plateau_age(ages, errors, k39, kind='inverse_variance'): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) pidx = find_plateaus(ages, errors, k39, overlap_sigma=2) if pidx: sx=slice(*pidx) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx
def calculate_plateau_age(ages, errors, k39, kind='inverse_variance'): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) pidx = find_plateaus(ages, errors, k39, overlap_sigma=2) if pidx: sx = slice(*pidx) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx
def _calculate_mean(self, attr, use_weights=True): args = self._get_values(attr) if args: vs, es = args if use_weights: av, werr = calculate_weighted_mean(vs, es) else: av = vs.mean() werr = vs.std(ddof=1) #if use_weights: # weights = 1 / es ** 2 #else: # weights = ones(vs.shape) #av, sum_weights = average(vs, weights=weights, returned=True) #if use_weights: # werr = sum_weights ** -0.5 #else: # werr = vs.std(ddof=1) else: av, werr = 0, 0 return ufloat(av, werr)
def calculate_plateau_age(ages, errors, k39, kind='inverse_variance', method='fleck 1977', options=None): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ # print 'ages=array({})'.format(ages) # print 'errors=array({})'.format(errors) # print 'k39=array({})'.format(k39) if options is None: options = {} ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) force_steps = options.get('force_steps', False) if force_steps: sstep, estep = force_steps sstep, estep = sstep.upper(), estep.upper() if not sstep: sidx = 0 else: sidx = ALPHAS.index(sstep) n = ages.shape[0] - 1 if not estep: eidx = n else: eidx = ALPHAS.index(estep) sidx, eidx = min(sidx, eidx), min(max(sidx, eidx), n) pidx = (sidx, eidx) if sidx < n else None else: from pychron.processing.plateau import Plateau p = Plateau(ages=ages, errors=errors, signals=k39, nsteps=options.get('nsteps', 3), gas_fraction=options.get('gas_fraction', 50)) pidx = p.find_plateaus(method) if pidx: sx = slice(pidx[0], pidx[1] + 1) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx
def _calculate_mean(self, attr, use_weights=True, error_kind=None): def sd(a, v, e): n = len(v) if n == 1: we = e[0] else: we = (sum((a - v) ** 2) / (n - 1)) ** 0.5 return we args = self._get_values(attr) sem = 0 if args: vs, es = args if use_weights: av, werr = calculate_weighted_mean(vs, es) if error_kind == 'both': sem = werr werr = sd(av, vs, es) elif error_kind == SD: werr = sd(av, vs, es) else: av = vs.mean() werr = vs.std(ddof=1) else: av, werr = 0, 0 if error_kind == 'both': return av, werr, sem, args else: return av, werr
def _calculate_mean(self, attr, use_weights=True, error_kind=None): def sd(a, v, e): n = len(v) if n == 1: we = e[0] else: we = (sum((a - v)**2) / (n - 1))**0.5 return we args = self._get_values(attr) sem = 0 if args: vs, es = args if use_weights and any(es): av, werr = calculate_weighted_mean(vs, es) if error_kind == 'both': sem = werr werr = sd(av, vs, es) elif error_kind == SD: werr = sd(av, vs, es) else: av = vs.mean() werr = vs.std(ddof=1) sem = werr / len(vs)**0.5 else: av, werr = 0, 0 if error_kind == 'both': return av, werr, sem, args else: return av, werr
def calculate_plateau_age(ages, errors, k39, kind='inverse_variance', method=FLECK, options=None, excludes=None): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ if options is None: options = {} ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) fixed_steps = options.get('fixed_steps', False) if fixed_steps and (fixed_steps[0] or fixed_steps[1]): sstep, estep = fixed_steps sstep, estep = sstep.upper(), estep.upper() if not sstep: sidx = 0 else: sidx = alpha_to_int(sstep) n = ages.shape[0] - 1 if not estep: eidx = n else: eidx = alpha_to_int(estep) sidx, eidx = min(sidx, eidx), min(max(sidx, eidx), n) pidx = (sidx, eidx) if sidx < n else None else: from pychron.processing.plateau import Plateau p = Plateau(ages=ages, errors=errors, signals=k39, excludes=excludes, overlap_sigma=options.get('overlap_sigma', 2), nsteps=options.get('nsteps', 3), gas_fraction=options.get('gas_fraction', 50)) pidx = p.find_plateaus(method) if pidx: sx = slice(pidx[0], pidx[1] + 1) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx
def _calculate_current(self, vs=None): if vs is None: vs = self._get_values() xs, es = vs if self.use_weighted_mean: return calculate_weighted_mean(xs, es) else: xs = array(xs) return xs.mean(), xs.std()
def _make_weighted_mean_row(self, ans): r = Row(fontsize=8) ages, errors = zip(*[(ai.age.nominal_value, ai.age.std_dev) for ai in ans]) wm, we = calculate_weighted_mean(ages, errors) r.add_item(value='<b>weighted mean</b>', span=2) r.add_blank_item(3) r.add_item(value='<b>{}</b>'.format(floatfmt(wm))) r.add_item(value=u'<b>\u00b1{}</b>'.format(floatfmt(we))) return r
def _calculate_stats(self, ages, errors, xs, ys): mswd, valid_mswd, n =self.analysis_group.get_mswd_tuple() if self.options.mean_calculation_kind == 'kernel': wm, we = 0, 0 delta = 1 maxs, _mins = find_peaks(ys, xs, delta=delta, lookahead=1) wm = max(maxs, axis=1)[0] else: wm, we = calculate_weighted_mean(ages, errors) we = self._calc_error(we, mswd) return wm, we, mswd, valid_mswd
def _calculate_stats(self, ages, errors, xs, ys): mswd, valid_mswd, n = self.analysis_group.get_mswd_tuple() if self.options.mean_calculation_kind == 'kernel': wm, we = 0, 0 delta = 1 maxs, _mins = find_peaks(ys, xs, delta=delta, lookahead=1) wm = max(maxs, axis=1)[0] else: wm, we = calculate_weighted_mean(ages, errors) we = self._calc_error(we, mswd) return wm, we, mswd, valid_mswd
def _calculate_mean(self, attr, use_weights=True, error_kind=None): args = self._get_values(attr) if args: vs, es = args if use_weights: av, werr = calculate_weighted_mean(vs, es) if error_kind == 'SD': n = len(vs) werr = (sum((av - vs)**2) / (n - 1))**0.5 else: av = vs.mean() werr = vs.std(ddof=1) else: av, werr = 0, 0 return av, werr
def _calculate_mean(self, attr, use_weights=True, error_kind=None): args = self._get_values(attr) if args: vs, es = args if use_weights: av, werr = calculate_weighted_mean(vs, es) if error_kind == 'SD': n = len(vs) werr = (sum((av - vs) ** 2) / (n - 1)) ** 0.5 else: av = vs.mean() werr = vs.std(ddof=1) else: av, werr = 0, 0 return av, werr
def load(self): ans = self.analyses ages = [ai.age for ai in ans] errors = [ai.age_err_wo_j for ai in ans] self.min_age = min(ages) self.max_age = max(ages) self.n = n = len(self.analyses) self.mean = sum(ages) / float(n) wm, we = calculate_weighted_mean(ages, errors) self.weighted_mean = wm mswd = calculate_mswd(ages, errors, wm=wm) self.mswd = mswd self.mswd_low, self.mswd_high = get_mswd_limits(n) self._create_ideogram() self._create_spectrum()
def calculate_plateau_age(ages, errors, k39, kind='inverse_variance', method='fleck 1977'): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ # print 'ages=array({})'.format(ages) # print 'errors=array({})'.format(errors) # print 'k39=array({})'.format(k39) ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) from pychron.processing.plateau import Plateau p = Plateau(ages=ages, errors=errors, signals=k39) pidx = p.find_plateaus(method) # pidx = find_plateaus(ages, errors, k39, # overlap_sigma=2) if pidx: sx = slice(*pidx) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx
def calculate_plateau_age(ages, errors, k39, kind='inverse_variance', method='fleck 1977', options=None, excludes=None): """ ages: list of ages errors: list of corresponding 1sigma errors k39: list of 39ArK signals return age, error """ # print 'ages=array({})'.format(ages) # print 'errors=array({})'.format(errors) # print 'k39=array({})'.format(k39) if options is None: options = {} ages = asarray(ages) errors = asarray(errors) k39 = asarray(k39) force_steps = options.get('force_steps', False) if force_steps: sstep, estep = force_steps sstep, estep = sstep.upper(), estep.upper() if not sstep: sidx = 0 else: sidx = ALPHAS.index(sstep) n = ages.shape[0] - 1 if not estep: eidx = n else: eidx = ALPHAS.index(estep) sidx, eidx = min(sidx, eidx), min(max(sidx, eidx), n) pidx = (sidx, eidx) if sidx < n else None else: from pychron.processing.plateau import Plateau p = Plateau(ages=ages, errors=errors, signals=k39, excludes=excludes, nsteps=options.get('nsteps', 3), gas_fraction=options.get('gas_fraction', 50)) pidx = p.find_plateaus(method) if pidx: sx = slice(pidx[0], pidx[1] + 1) plateau_ages = ages[sx] if kind == 'vol_fraction': weights = k39[sx] wm, we = average(plateau_ages, weights=weights) else: plateau_errors = errors[sx] wm, we = calculate_weighted_mean(plateau_ages, plateau_errors) return wm, we, pidx