def _make_correlation(self, idx, ytitle): fi = self.figures[0] plots = list(fi.options.get_plotable_aux_plots()) tag = plots[idx].plot_name n = len(self.figures) r, c = filled_grid(n) g = RegressionGraph(container_dict={ 'kind': 'g', 'shape': (r, c) }, window_title='Correlation') for i, fi in enumerate(self.figures): gi = fi.analysis_group p = g.new_plot(xtitle='age', ytitle=ytitle, title='{}({})'.format(gi.sample, gi.identifier)) xs = [nominal_value(a.uage) for a in gi.clean_analyses()] ys = [nominal_value(a.get_value(tag)) for a in gi.clean_analyses()] g.new_series(xs, ys, fit='linear', use_error_envelope=False, plotid=i) g.add_correlation_statistics(plotid=i) g.set_x_limits(pad='0.1', plotid=i) g.set_y_limits(pad='0.1', plotid=i) g.refresh() open_view(g)
def _load_air_computed(self, an, new_list): if self.experiment_type == AR_AR: if new_list: c = an.arar_constants ratios = [('40Ar/36Ar', 'Ar40/Ar36', nominal_value(c.atm4036)), ('40Ar/38Ar', 'Ar40/Ar38', nominal_value(c.atm4038))] cv = self._make_ratios(ratios) self.computed_values = cv self._update_ratios() try: niso, diso = self._get_ratio('Ar40/Ar36') if niso and diso: noncorrected = self._get_non_corrected_ratio(niso, diso) v, e = nominal_value(noncorrected), std_dev(noncorrected) ref = 295.5 self.summary_str = u'Ar40/Ar36={} {}{}({}%) IC={:0.5f}'.format( floatfmt(v), PLUSMINUS, floatfmt(e), format_percent_error(v, e), nominal_value(noncorrected / ref)) except: pass else: # todo add ratios for other isotopes. e.g Ne pass
def _load_cocktail_computed(self, an, new_list): if new_list: c = an.arar_constants ratios = [] refs = {'40Ar/38Ar': nominal_value(c.atm4038), '40Ar/36Ar': nominal_value(c.atm4036)} if self.recall_options: names = [r.tagname for r in self.recall_options.cocktail_options.ratios] ratios = [(name, name, refs.get(name, 1)) for name in names] cv = self._make_ratios(ratios) an.calculate_age() cv.append(ComputedValue(name='F', tag='uf', uvalue=an.uF)) cv.append(ComputedValue(name='40Ar*', tag='rad40_percent', uvalue=an.rad40_percent)) cv.append(ComputedValue(name='Age', tag='uage', uvalue=an.uage)) self.computed_values = cv self._update_ratios() else: self._update_ratios()
def calculate_atmospheric(a38, a36, k38, ca38, ca36, decay_time, production_ratios=None, arar_constants=None): """ McDougall and Harrison Roddick 1983 Foland 1993 iteratively calculate atm36 """ if production_ratios is None: production_ratios = {} if arar_constants is None: arar_constants = ArArConstants() pr = production_ratios m = pr.get('Cl3638', 0) * nominal_value(arar_constants.lambda_Cl36) * decay_time atm3836 = nominal_value(arar_constants.atm3836) # lcl36 = arar_constants.lambda_Cl36.nominal_value # atm3836 = arar_constants.atm3836.nominal_value # m = pr.get('Cl3638', 0) * lcl36 * decay_time # atm36 = ufloat(0, 0, tag='atm36') atm36 = 0 for _ in range(5): ar38atm = atm3836 * atm36 cl38 = a38 - ar38atm - k38 - ca38 cl36 = cl38 * m atm36 = a36 - ca36 - cl36 return atm36, cl36, cl38
def _save_currents(self, dban): dvc = self.dvc if dvc.update_currents_enabled: ps = self.per_spec db = dvc.db for key, iso in ps.isotope_group.isotopes.items(): param = db.add_parameter('{}_intercept'.format(key)) db.add_current(dban, iso.value, iso.error, param, iso.units) param = db.add_parameter('{}_blank'.format(key), iso.blank.units) db.add_current(dban, iso.blank.value, iso.blank.error, param, iso.blank.units) param = db.add_parameter('{}_bs_corrected'.format(key)) v = iso.get_baseline_corrected_value() db.add_current(dban, nominal_value(v), std_dev(v), param, iso.units) param = db.add_parameter('{}_ic_corrected'.format(key)) v = iso.get_ic_corrected_value() db.add_current(dban, nominal_value(v), std_dev(v), param, iso.units) param = db.add_parameter(key) v = iso.get_non_detector_corrected_value() db.add_current(dban, nominal_value(v), std_dev(v), param, iso.units) param = db.add_parameter(iso.baseline.name, iso.baseline.units) db.add_current(dban, iso.baseline.value, iso.baseline.error, param, iso.baseline.units)
def Gr_janaf(T,flag=False): """ calculate the free energy of reaction FeO + 1/4O2 = FeO1.5 for pure endmembers by this study Params ---- T : list or scalar, temperature flag : whether or not include uncertainties Output ---- G : energy list or scalar ---- Note: It is a combination of Gr_janaf_new and Gr_janaf_old. Refer to the previous versions of `tools` of how to we derive the coefficients a,b,c,d based on these two old methods Several ways to improve it in the """ a = uct.ufloat(-331035.9211346371,1.72e+02) b = uct.ufloat(-190.3795512883899,0.484) c = uct.ufloat(14.785873706952849,0.0859) d = uct.ufloat(-0.0016487959655627517,44e-6) e = uct.ufloat(9348044.389346942,1.22e+03) f = uct.ufloat(10773.299613088355,1.44) G = cal_G(T,a,b,c,d,e,f) if flag: return G else: try: return [uct.nominal_value(i) for i in G] except: return uct.nominal_value(G)
def _add_baseline(self, spec, dbiso, dbdet, odet): iso = dbiso.Label self.debug('add baseline dbdet= {}. original det= {}'.format( iso, odet)) det = dbdet.detector_type.Label tb, vb = spec.get_baseline_data(iso, odet) pos = spec.get_baseline_position(iso) blob = self._build_timeblob(tb, vb) db = self.db label = '{} Baseline'.format(det.upper()) ncnts = len(tb) db_baseline = db.add_baseline(blob, label, ncnts, dbiso) db.flush() # if spec.is_peak_hop: # det = spec.peak_hop_detector # bs = spec.get_baseline_uvalue(iso) bs, fncnts = spec.get_filtered_baseline_uvalue(iso) # sem = bs.std_dev / (fncnts) ** 0.5 if fncnts else 0 bfit = spec.get_baseline_fit(iso) self.debug('baseline {}. v={}, e={}'.format(iso, nominal_value(bs), std_dev(bs))) infoblob = self._make_infoblob(nominal_value(bs), std_dev(bs), fncnts, pos) db_changeable = db.add_baseline_changeable_item( self.data_reduction_session_id, bfit, infoblob) # baseline and baseline changeable items need matching BslnID db_changeable.BslnID = db_baseline.BslnID db.flush()
def _plot_ratio(self, po, i): xs = [nominal_value(ai) for ai in self._unpack_attr(po.xtitle)] ys = [nominal_value(ai) for ai in self._unpack_attr(po.ytitle)] plot, scatter, line = self.graph.new_series(x=array(xs), y=array(ys), fit='linear', add_inspector=False, marker=po.marker, marker_size=po.marker_size) opt = self.options nsigma = opt.error_bar_nsigma for axk in 'xy': caps = getattr(opt, '{}_end_caps'.format(axk)) visible = getattr(po, '{}_error'.format(axk)) attr = getattr(po, '{}title'.format(axk)) es = [std_dev(ai) for ai in self._unpack_attr(attr)] self._add_error_bars(scatter, es, axk, nsigma, end_caps=caps, visible=visible)
def _calculate_integrated_mean_error(self, weighting, ks, rs): sks = ks.sum() weights = None fs = rs / ks errors = array([std_dev(f) for f in fs]) values = array([nominal_value(f) for f in fs]) if weighting == 'Volume': vpercent = ks / sks weights = [nominal_value(wi) for wi in (vpercent * errors)**2] elif weighting == 'Variance': weights = 1 / errors**2 if weights is not None: wmean, sum_weights = average(values, weights=weights, returned=True) if weighting == 'Volume': werr = sum_weights**0.5 else: werr = sum_weights**-0.5 f = ufloat(wmean, werr) else: f = rs.sum() / sks return f
def test_counter(): if (sys.version_info < (2, 6, 0)): from nose.plugins.skip import SkipTest raise SkipTest mcu = Arduino() reg = mcu.registers.proxy mcu.pins.reset() p = mcu.pin(5) p.write_mode(OUTPUT) p.pwm.write_value(128) print 'frequencies_available:', p.pwm.frequencies_available for fset in p.pwm.frequencies_available: p.pwm.frequency = fset assert abs(p.pwm.frequency - fset) <= 1 print '---------------------------' print 'fset=', fset print '---------------------------' for ms in [10, 20, 50, 100, 200, 500, 1000]: for _ in range(1): t = ms / 1000.0 with mcu.counter: mcu.counter.run(t) f = mcu.counter.frequency t = mcu.counter.gate_time err = f - fset print 't=%s f=%s ' % (t, f) ok_(abs(nominal_value(err)) <= 0.1+std_dev(err), (abs(nominal_value(err)),std_dev(err)))
def _add_baseline(self, spec, dbiso, dbdet, odet): iso = dbiso.Label self.debug('add baseline dbdet= {}. original det= {}'.format(iso, odet)) det = dbdet.detector_type.Label tb, vb = spec.get_baseline_data(iso, odet) pos = spec.get_baseline_position(iso) blob = self._build_timeblob(tb, vb) db = self.db label = '{} Baseline'.format(det.upper()) ncnts = len(tb) db_baseline = db.add_baseline(blob, label, ncnts, dbiso) db.flush() # if spec.is_peak_hop: # det = spec.peak_hop_detector # bs = spec.get_baseline_uvalue(iso) bs, fncnts = spec.get_filtered_baseline_uvalue(iso) # sem = bs.std_dev / (fncnts) ** 0.5 if fncnts else 0 bfit = spec.get_baseline_fit(iso) self.debug('baseline {}. v={}, e={}'.format(iso, nominal_value(bs), std_dev(bs))) infoblob = self._make_infoblob(nominal_value(bs), std_dev(bs), fncnts, pos) db_changeable = db.add_baseline_changeable_item(self.data_reduction_session_id, bfit, infoblob) # baseline and baseline changeable items need matching BslnID db_changeable.BslnID = db_baseline.BslnID db.flush()
def _plot_ratio(self, po, i): xs = [nominal_value(ai) for ai in self._unpack_attr(po.xtitle)] ys = [nominal_value(ai) for ai in self._unpack_attr(po.ytitle)] self.graph.new_series(x=array(xs), y=array(ys), add_inspector=False, marker=po.marker, marker_size=po.marker_size)
def _load_unknown_computed(self, an, new_list): attrs = (('Age', 'uage_w_j_err'), # ('Age', 'age', None, None, 'age_err'), ('w/o J', 'wo_j', '', 'uage', 'age_err_wo_j'), ('K/Ca', 'kca'), ('K/Cl', 'kcl'), ('40Ar*', 'rad40_percent'), ('F', 'uF'), ('w/o Irrad', 'wo_irrad', '', 'uF', 'F_err_wo_irrad')) if new_list: def comp_factory(n, a, value=None, value_tag=None, error_tag=None): if value is None: value = getattr(an, a) display_value = True if value_tag: value = getattr(an, value_tag) display_value = False if error_tag: e = getattr(an, error_tag) else: e = std_dev(value) return ComputedValue(name=n, tag=a, value=nominal_value(value) or 0, value_tag=value_tag or '', display_value=display_value, error=e or 0) cv = [comp_factory(*args) for args in attrs] self.computed_values = cv else: age = an.uage nage, sage = nominal_value(age), std_dev(age) try: self.summary_str = u'Age={} {}{}({}%)'.format(floatfmt(nage), PLUSMINUS, floatfmt(sage), format_percent_error(nage, sage)) except: pass for ci in self.computed_values: attr = ci.tag if attr == 'wo_j': ci.error = an.age_err_wo_j or 0 ci.value = nominal_value(getattr(an, ci.value_tag)) elif attr == 'wo_irrad': ci.error = an.F_err_wo_irrad or 0 ci.value = nominal_value(getattr(an, ci.value_tag)) else: v = getattr(an, attr) if v is not None: ci.value = nominal_value(v) ci.error = std_dev(v)
def _build_integrated_age_label(self, tga, n): txt = 'NaN' if not isnan(nominal_value(tga)): age, error = nominal_value(tga.nominal_value), std_dev(tga) error *= self.options.nsigma txt = self._build_label_text(age, error, n, sig_figs=self.options.integrated_sig_figs) return u'Integrated Age= {}'.format(txt)
def _build_integrated_age_label(self, tga, n): txt = 'NaN' if not isnan(nominal_value(tga)): age, error = nominal_value(tga.nominal_value), std_dev(tga) error *= self.options.nsigma txt = self._build_label_text(age, error, n, sig_figs=self.options.integrated_sig_figs) return u'Total Integrated Age= {}'.format(txt)
def _load_air_computed(self, an, new_list): if new_list: c = an.arar_constants ratios = [('40Ar/36Ar', 'Ar40/Ar36', nominal_value(c.atm4036)), ('40Ar/38Ar', 'Ar40/Ar38', nominal_value(c.atm4038))] cv = self._make_ratios(ratios) self.computed_values = cv self._update_ratios(an)
def fix_nan(value): """Helper function to set unwanted NaN's to 0.""" if np.isnan(unc.std_dev(value)): if np.isnan(unc.nominal_value(value)): return unc.ufloat(0.0, 0.0) else: return unc.ufloat(unc.nominal_value(value),0) else: return value
def _load_unknown_computed(self, an, new_list): attrs = (('Age', 'uage_w_j_err'), ('w/o J', 'wo_j', '', 'uage', 'age_err_wo_j'), ('K/Ca', 'kca'), ('K/Cl', 'kcl'), ('40Ar*', 'radiogenic_yield'), ('F', 'uF'), ('w/o Irrad', 'wo_irrad', '', 'uF', 'F_err_wo_irrad')) if new_list: def comp_factory(n, a, value=None, value_tag=None, error_tag=None): if value is None: value = getattr(an, a) display_value = True if value_tag: value = getattr(an, value_tag) display_value = False if error_tag: e = getattr(an, error_tag) else: e = std_dev(value) return self._computed_value_factory( name=n, tag=a, value=nominal_value(value) or 0, value_tag=value_tag or '', display_value=display_value, error=e or 0) cv = [comp_factory(*args) for args in attrs] self.computed_values = cv else: age = an.uage nage, sage = nominal_value(age), std_dev(age) try: self.summary_str = u'Age={} {}{}({}%)'.format( floatfmt(nage), PLUSMINUS, floatfmt(sage), format_percent_error(nage, sage)) except: pass for ci in self.computed_values: ci.sig_figs = self.sig_figs attr = ci.tag if attr == 'wo_j': ci.error = an.age_err_wo_j or 0 ci.value = nominal_value(getattr(an, ci.value_tag)) elif attr == 'wo_irrad': ci.error = an.F_err_wo_irrad or 0 ci.value = nominal_value(getattr(an, ci.value_tag)) else: v = getattr(an, attr) if v is not None: ci.value = nominal_value(v) ci.error = std_dev(v)
def _load_air_computed(self, an, new_list): if new_list: c = an.arar_constants ratios = [('40Ar/36Ar', 'Ar40/Ar36', nominal_value(c.atm4036)), ('40Ar/38Ar', 'Ar40/Ar38', nominal_value(c.atm4038))] cv = self._make_ratios(ratios) self.computed_values = cv self._update_ratios()
def calculate_decay_factors(self): arc = self.arar_constants # only calculate decayfactors once if not self.ar39decayfactor: dc37 = nominal_value(arc.lambda_Ar37) dc39 = nominal_value(arc.lambda_Ar39) a37df, a39df = calculate_arar_decay_factors(dc37, dc39, self.chron_segments) self.ar37decayfactor = a37df self.ar39decayfactor = a39df
def _make_intermediate_summary(self, sh, ag, cols, label): row = self._current_row age_idx = next((i for i, c in enumerate(cols) if c.label == 'Age'), 0) cum_idx = next( (i for i, c in enumerate(cols) if c.attr == 'cumulative_ar39'), 0) fmt = self._get_number_format('summary_age') kcafmt = self._get_number_format('summary_kca') fmt.set_bottom(1) kcafmt.set_bottom(1) fmt2 = self._workbook.add_format({'bottom': 1, 'bold': True}) border = self._workbook.add_format({'bottom': 1}) for i in range(age_idx + 1): sh.write_blank(row, i, '', fmt) startcol = 1 sh.write(row, startcol, '{:02n}'.format(ag.aliquot), fmt2) sh.write_string(row, startcol + 1, label, fmt2) cols[startcol + 1].calculate_width(label) age = ag.uage tn = ag.total_n if label == 'plateau': if not ag.plateau_steps: age = None else: txt = 'n={}/{} steps={}'.format(ag.nsteps, tn, ag.plateau_steps_str) sh.write(row, startcol + 2, txt, border) sh.write(row, cum_idx + 1, format_mswd(ag.get_plateau_mswd_tuple()), border) else: txt = 'n={}/{}'.format(ag.nanalyses, tn) sh.write(row, startcol + 2, txt, border) sh.write(row, cum_idx + 1, format_mswd(ag.get_mswd_tuple()), border) if age is not None: sh.write_number(row, age_idx, nominal_value(age), fmt) sh.write_number(row, age_idx + 1, std_dev(age), fmt) else: sh.write(row, age_idx, 'No plateau', border) sh.write_number(row, age_idx + 2, nominal_value(ag.kca), kcafmt) sh.write_number(row, age_idx + 3, std_dev(ag.kca), kcafmt) if label == 'plateau': sh.write_number(row, cum_idx, ag.plateau_total_ar39(), fmt) else: sh.write_number(row, cum_idx, ag.valid_total_ar39(), fmt) self._current_row += 1
def _plot_ratio(self, po, i): xs = [nominal_value(ai) for ai in self._unpack_attr(po.xtitle)] ys = [nominal_value(ai) for ai in self._unpack_attr(po.ytitle)] args = self.graph.new_series(x=array(xs), y=array(ys), # display_index=ArrayDataSource(data=n), # plotid=pid, add_inspector=False, marker=po.marker, marker_size=po.marker_size)
def calculate_decay_factors(self): arc = self.arar_constants # only calculate decayfactors once if not self.ar39decayfactor: dc37 = nominal_value(arc.lambda_Ar37) dc39 = nominal_value(arc.lambda_Ar39) a37df, a39df = calculate_arar_decay_factors( dc37, dc39, self.chron_segments) self.ar37decayfactor = a37df self.ar39decayfactor = a39df
def get_baseline_corrected_value(self, include_baseline_error=None): if include_baseline_error is None: include_baseline_error = self.include_baseline_error b = self.baseline.uvalue if not include_baseline_error: b = nominal_value(b) nv = self.uvalue - b return ufloat(nominal_value(nv), std_dev(nv), tag=self.name) else: return self.uvalue - b
def _value_string(self, t): if t == 'uF': a, e = self.f, self.f_err elif t == 'uage': a, e = nominal_value(self.uage), std_dev(self.uage) else: v = self.get_value(t) if isinstance(v, Isotope): v = v.get_intensity() a, e = nominal_value(v), std_dev(v) return a, e
def _value_string(self, t): if t == 'uF': a, e = self.F, self.F_err elif t == 'uage': a, e = nominal_value(self.uage), std_dev(self.uage) else: v = self.get_value(t) if isinstance(v, Isotope): v = v.get_intensity() a, e = nominal_value(v), std_dev(v) return a, e
def _calculate_values(self, ag, others): vs = [''] aa = [nominal_value(a.uage) for a in ag.clean_analyses()] for other in others: if other == ag: pv = '' else: bb = [nominal_value(a.uage) for a in other.clean_analyses()] tstat, pv = ttest_ind(aa, bb, equal_var=False) vs.append(pv) return vs
def __residenceTime(self): """ Calculate the residence time of a single step event. """ # if previous errors were detected, the # event is already rejected, don't process it # any further if self.mdProcessingStatus != 'normal': return # set numpy warning handling. # raise divide by zero errors so we # can catch them np.seterr(divide='raise') ocmu = np.abs(uncertainties.nominal_value(self.mdOpenChCurrent)) ocsd = np.abs(uncertainties.std_dev(self.mdOpenChCurrent)) bcmu = np.abs(uncertainties.nominal_value(self.mdBlockedCurrent)) bcsd = np.abs(uncertainties.std_dev(self.mdBlockedCurrent)) # refine the start estimate idx = self.eStartEstimate try: while np.abs((np.abs(self.eventData[idx]) - ocmu) / ocsd) > 5.0: idx -= 1 # Set the start point self.mdEventStart = idx + 1 # Next move the count forward so we are in the blocked channel region of the pulse while np.abs((np.abs(self.eventData[idx]) - bcmu) / bcsd) > 0.5: idx += 1 # Search for the event end. 7*sigma allows us to prevent false # positives while np.abs((np.abs(self.eventData[idx]) - bcmu) / bcsd) < 7.0: idx += 1 # Finally backtrack to find the true event end while np.abs((np.abs(self.eventData[idx]) - bcmu) / bcsd) > 5.0: idx -= 1 except (IndexError, FloatingPointError): self.rejectEvent('eResTime') return self.mdEventEnd = idx - 1 # residence time in ms self.mdResTime = 1000. * ( (self.mdEventEnd - self.mdEventStart) / float(self.Fs))
def calculate_atmospheric(a38, a36, k38, ca38, ca36, decay_time, production_ratios=None, arar_constants=None): """ McDougall and Harrison Roddick 1983 Foland 1993 calculate atm36, cl36, cl38 # starting with the following equations atm36 = a36 - ca36 - cl36 m = cl3638*lambda_cl36*decay_time cl36 = cl38 * m cl38 = a38 - k38 - ca38 - ar38atm ar38atm = atm3836 * atm36 # rearranging to solve for atm36 cl38 = a38 - k38 - c38 - atm3836 * atm36 cl36 = m * (a38 - k38 - ca38 - atm3836 * atm36) = m (a38 - k38 - ca38) - m * atm3836 * atm36 atm36 = a36 - ca36 - m (a38 - k38 - ca38) + m * atm3836 * atm36 atm36 - m * atm3836 * atm36 = a36 - ca36 - m (a38 - k38 - ca38) atm36 * (1 - m*atm3836) = a36 - ca36 - m (a38 - k38 - ca38) atm36 = (a36 - ca36 - m (a38 - k38 - c38))/(1 - m*atm3836) """ if production_ratios is None: production_ratios = {} if arar_constants is None: arar_constants = ArArConstants() pr = production_ratios m = pr.get('Cl3638', 0) * nominal_value( arar_constants.lambda_Cl36) * decay_time atm3836 = nominal_value(arar_constants.atm3836) atm36 = (a36 - ca36 - m * (a38 - k38 - ca38)) / (1 - m * atm3836) ar38atm = atm3836 * atm36 cl38 = a38 - ar38atm - k38 - ca38 cl36 = cl38 * m return atm36, cl36, cl38
def _make_intermediate_summary(self, sh, ag, cols, label): row = self._current_row age_idx = next((i for i, c in enumerate(cols) if c.label == 'Age'), 0) cum_idx = next((i for i, c in enumerate(cols) if c.attr == 'cumulative_ar39'), 0) fmt = self._get_number_format('summary_age') kcafmt = self._get_number_format('summary_kca') fmt.set_bottom(1) kcafmt.set_bottom(1) fmt2 = self._workbook.add_format({'bottom': 1, 'bold': True}) border = self._workbook.add_format({'bottom': 1}) for i in range(age_idx + 1): sh.write_blank(row, i, '', fmt) startcol = 1 sh.write(row, startcol, '{:02n}'.format(ag.aliquot), fmt2) sh.write_rich_string(row, startcol + 1, label, fmt2) cols[startcol + 1].calculate_width(label) age = ag.uage tn = ag.total_n if label == 'plateau': if not ag.plateau_steps: age = None else: txt = 'n={}/{} steps={}'.format(ag.nsteps, tn, ag.plateau_steps_str) sh.write(row, startcol + 2, txt, border) sh.write(row, cum_idx + 1, format_mswd(ag.get_plateau_mswd_tuple()), border) else: txt = 'n={}/{}'.format(ag.nanalyses, tn) sh.write(row, startcol + 2, txt, border) sh.write(row, cum_idx + 1, format_mswd(ag.get_mswd_tuple()), border) if age is not None: sh.write_number(row, age_idx, nominal_value(age), fmt) sh.write_number(row, age_idx + 1, std_dev(age), fmt) else: sh.write(row, age_idx, 'No plateau', border) sh.write_number(row, age_idx + 2, nominal_value(ag.kca), kcafmt) sh.write_number(row, age_idx + 3, std_dev(ag.kca), kcafmt) if label == 'plateau': sh.write_number(row, cum_idx, ag.plateau_total_ar39(), fmt) else: sh.write_number(row, cum_idx, ag.valid_total_ar39(), fmt) self._current_row += 1
def __init__(self, ratios, name, *args, **kw): super(Result, self).__init__(*args, **kw) vs = array([nominal_value(ri) for ri in ratios]) es = array([std_dev(ri) for ri in ratios]) self.name = name m = ratios.mean() self.value = nominal_value(m) self.error = std_dev(m) wm, we = calculate_weighted_mean(vs, es) self.wm_value = wm self.wm_error = we self.mswd = calculate_mswd(vs, es, wm=wm)
def _get_value(self, attr, n=3, **kw): v = '' item = self.item if hasattr(item, attr): v = getattr(self.item, attr) if v: v = floatfmt(nominal_value(v), n=n, **kw) elif hasattr(item, 'isotopes'): if attr in item.isotopes: v = item.isotopes[attr].get_intensity() v = floatfmt(nominal_value(v), n=n, **kw) return v
def _get_value(self, attr, n=3, **kw): v = "" item = self.item if hasattr(item, attr): v = getattr(self.item, attr) if v: v = floatfmt(nominal_value(v), n=n, **kw) elif hasattr(item, "isotopes"): if attr in item.isotopes: v = item.isotopes[attr].get_intensity() v = floatfmt(nominal_value(v), n=n, **kw) return v
def _air_ratio(self): a4038 = self.isotope_group.get_ratio('Ar40/Ar38', non_ic_corr=True) a4036 = self.isotope_group.get_ratio('Ar40/Ar36', non_ic_corr=True) # e4038 = uformat_percent_error(a4038, include_percent_sign=True) # e4036 = uformat_percent_error(a4036, include_percent_sign=True) lines = [self._make_header('Ratios'), 'Ar40/Ar36= {} {}'.format(floatfmt(nominal_value(a4036)), errorfmt(nominal_value(a4036), std_dev(a4036))), 'Ar40/Ar38= {} {}'.format(floatfmt(nominal_value(a4038)), errorfmt(nominal_value(a4038), std_dev(a4038)))] return self._make_lines(lines)
def __residenceTime(self): """ Calculate the residence time of a single step event. """ # if previous errors were detected, the # event is already rejected, don't process it # any further if self.mdProcessingStatus != 'normal': return # set numpy warning handling. # raise divide by zero errors so we # can catch them np.seterr(divide='raise') ocmu=np.abs(uncertainties.nominal_value(self.mdOpenChCurrent)) ocsd=np.abs(uncertainties.std_dev(self.mdOpenChCurrent)) bcmu=np.abs(uncertainties.nominal_value(self.mdBlockedCurrent)) bcsd=np.abs(uncertainties.std_dev(self.mdBlockedCurrent)) # refine the start estimate idx=self.eStartEstimate try: while np.abs((np.abs(self.eventData[idx])-ocmu)/ocsd) > 5.0: idx-=1 # Set the start point self.mdEventStart=idx+1 # Next move the count forward so we are in the blocked channel region of the pulse while np.abs((np.abs(self.eventData[idx])-bcmu)/bcsd) > 0.5: idx+=1 # Search for the event end. 7*sigma allows us to prevent false # positives while np.abs((np.abs(self.eventData[idx])-bcmu)/bcsd) < 7.0: idx+=1 # Finally backtrack to find the true event end while np.abs((np.abs(self.eventData[idx])-bcmu)/bcsd) > 5.0: idx-=1 except ( IndexError, FloatingPointError ): self.rejectEvent('eResTime') return self.mdEventEnd=idx-1 # residence time in ms self.mdResTime=1000.*((self.mdEventEnd-self.mdEventStart)/float(self.Fs))
def _make_summary(self, sh, cols, group): fmt = self._bold start_col = 0 if self._options.include_kca: idx = next((i for i, c in enumerate(cols) if c[1] == 'K/Ca')) sh.write_rich_string(self._current_row, start_col, u'K/Ca {}'.format(PLUSMINUS_ONE_SIGMA), fmt) kca = group.weighted_kca if self._options.use_weighted_kca else group.arith_kca sh.write(self._current_row, idx, nominal_value(kca)) sh.write(self._current_row, idx + 1, std_dev(kca)) self._current_row += 1 idx = next((i for i, c in enumerate(cols) if c[1] == 'Age')) sh.write_rich_string( self._current_row, start_col, u'Weighted Mean Age {}'.format(PLUSMINUS_ONE_SIGMA), fmt) sh.write(self._current_row, idx, nominal_value(group.weighted_age)) sh.write(self._current_row, idx + 1, std_dev(group.weighted_age)) self._current_row += 1 if self._options.include_plateau_age and hasattr(group, 'plateau_age'): sh.write_rich_string(self._current_row, start_col, u'Plateau {}'.format(PLUSMINUS_ONE_SIGMA), fmt) sh.write(self._current_row, 3, 'steps {}'.format(group.plateau_steps_str)) sh.write(self._current_row, idx, nominal_value(group.plateau_age)) sh.write(self._current_row, idx + 1, std_dev(group.plateau_age)) self._current_row += 1 if self._options.include_isochron_age: sh.write_rich_string( self._current_row, start_col, u'Isochron Age {}'.format(PLUSMINUS_ONE_SIGMA), fmt) sh.write(self._current_row, idx, nominal_value(group.isochron_age)) sh.write(self._current_row, idx + 1, std_dev(group.isochron_age)) self._current_row += 1 if self._options.include_integrated_age and hasattr( group, 'integrated_age'): sh.write_rich_string( self._current_row, start_col, u'Integrated Age {}'.format(PLUSMINUS_ONE_SIGMA), fmt) sh.write(self._current_row, idx, nominal_value(group.integrated_age)) sh.write(self._current_row, idx + 1, std_dev(group.integrated_age)) self._current_row += 1
def _load_unknown_computed(self, an, new_list): attrs = ( ('Age', 'uage'), # ('Age', 'age', None, None, 'age_err'), ('w/o J', 'wo_j', '', 'uage', 'age_err_wo_j'), ('K/Ca', 'kca'), ('K/Cl', 'kcl'), ('40Ar*', 'rad40_percent'), ('F', 'uF'), ('w/o Irrad', 'wo_irrad', '', 'uF', 'F_err_wo_irrad')) if new_list: def comp_factory(n, a, value=None, value_tag=None, error_tag=None): if value is None: value = getattr(an, a) display_value = True if value_tag: value = getattr(an, value_tag) display_value = False if error_tag: e = getattr(an, error_tag) else: e = std_dev(value) return ComputedValue(name=n, tag=a, value=nominal_value(value) or 0, value_tag=value_tag, display_value=display_value, error=e or 0) cv = [comp_factory(*args) for args in attrs] self.computed_values = cv else: for ci in self.computed_values: attr = ci.tag if attr == 'wo_j': ci.error = an.age_err_wo_j ci.value = nominal_value(getattr(an, ci.value_tag)) elif attr == 'wo_irrad': ci.error = an.F_err_wo_irrad ci.value = nominal_value(getattr(an, ci.value_tag)) else: v = getattr(an, attr) if v is not None: ci.value = nominal_value(v) ci.error = std_dev(v)
def load_measurement(self, an, ar): # j = self._get_j(an) j = ar.j jf = 'NaN' if j is not None: jj = floatfmt(nominal_value(j), n=7, s=5) pe = format_percent_error(nominal_value(j), std_dev(j), include_percent_sign=True) jf = u'{} \u00b1{:0.2e}({})'.format(jj, std_dev(j), pe) a39 = ar.ar39decayfactor a37 = ar.ar37decayfactor ms = [MeasurementValue(name='Branch', value=an.branch), MeasurementValue(name='DAQ Version', value=an.collection_version), MeasurementValue(name='UUID', value=an.uuid), MeasurementValue(name='RepositoryID', value=an.repository_identifier), MeasurementValue(name='Spectrometer', value=an.mass_spectrometer), MeasurementValue(name='Run Date', value=an.rundate.strftime('%Y-%m-%d %H:%M:%S')), MeasurementValue(name='Irradiation', value=self._get_irradiation(an)), MeasurementValue(name='J', value=jf), MeasurementValue(name='Position Error', value=floatfmt(an.position_jerr, use_scientific=True)), MeasurementValue(name='Lambda K', value=nominal_value(ar.arar_constants.lambda_k), units='1/a'), MeasurementValue(name='Project', value=an.project), MeasurementValue(name='Sample', value=an.sample), MeasurementValue(name='Material', value=an.material), MeasurementValue(name='Comment', value=an.comment), MeasurementValue(name='Ar39Decay', value=floatfmt(a39)), MeasurementValue(name='Ar37Decay', value=floatfmt(a37)), MeasurementValue(name='Sens.', value=floatfmt(an.sensitivity, use_scientific=True), units=an.sensitivity_units)] self.measurement_values = ms
def _init_plot_values(self, default_width=5): xy = [] for line in self.lines: if isinstance(line.energy, unc.core.AffineScalarFunc): xy.append((line.energy.n - line.energy.s / 2, 0)) xy.append((line.energy.n, unc.nominal_value(line.intensity))) xy.append((line.energy.n + line.energy.s / 2, 0)) else: xy.append((line.energy - default_width / 2, 0)) xy.append((line.energy, unc.nominal_value(line.intensity))) xy.append((line.energy + default_width / 2, 0)) xy = ary(xy).reshape([-1, 2]) x, y = xy[:, 0], xy[:, 1] return x, y
def _load_unknown_computed(self, an, new_list): attrs = (('Age', 'uage'), # ('Age', 'age', None, None, 'age_err'), ('w/o J', 'wo_j', '', 'uage', 'age_err_wo_j'), ('K/Ca', 'kca'), ('K/Cl', 'kcl'), ('40Ar*', 'rad40_percent'), ('F', 'uF'), ('w/o Irrad', 'wo_irrad', '', 'uF', 'F_err_wo_irrad')) if new_list: def comp_factory(n, a, value=None, value_tag=None, error_tag=None): if value is None: value = getattr(an, a) display_value = True if value_tag: value = getattr(an, value_tag) display_value = False if error_tag: e = getattr(an, error_tag) else: e = std_dev(value) return ComputedValue(name=n, tag=a, value=nominal_value(value) or 0, value_tag=value_tag, display_value=display_value, error=e or 0) cv = [comp_factory(*args) for args in attrs] self.computed_values = cv else: for ci in self.computed_values: attr = ci.tag if attr == 'wo_j': ci.error = an.age_err_wo_j ci.value = nominal_value(getattr(an, ci.value_tag)) elif attr == 'wo_irrad': ci.error = an.F_err_wo_irrad ci.value = nominal_value(getattr(an, ci.value_tag)) else: v = getattr(an, attr) if v is not None: ci.value = nominal_value(v) ci.error = std_dev(v)
def _update_ratios(self, an): for ci in self.computed_values: nd = ci.detectors n, d = nd.split('/') niso, diso = self._get_isotope(n), self._get_isotope(d) noncorrected = self._get_non_corrected_ratio(niso, diso) corrected, ic = self._get_corrected_ratio(niso, diso) ci.trait_set(value=floatfmt(nominal_value(corrected)), error=floatfmt(std_dev(corrected)), noncorrected_value=nominal_value(noncorrected), noncorrected_error=std_dev(noncorrected), ic_factor=nominal_value(ic))
def _get_value(self, item, attr): val = None if attr in ('aliquot', 'step'): val = getattr(item, attr) elif attr == 'run date': val = item.rundate elif attr in ('age', 'age error'): val = getattr(item, 'uage') val = nominal_value(val) if attr == 'age' else std_dev(val) elif attr in ('kca', 'kca error'): val = getattr(item, 'kca') val = nominal_value(val) if attr == 'kca' else std_dev(val) return val
def __init__(self,centre, width, area, eta=0.25, asymmetry=0,background=0,**kwargs): #bypass __getattr__ - do this before anything else is created object.__setattr__(self, '_pset', Parameters()) self._pset['centre'] = Parameter(name='centre', value=nominal_value(centre), vary=True, min=centre-2*width, max=centre+2*width, expr=None) self._pset['width'] = Parameter(name='width', value=nominal_value(width), vary=True, min=nominal_value(width)/3, max=3*nominal_value(width), expr=None) self._pset['area'] = Parameter(name='area', value=nominal_value(area), vary=True, min=0, max=None, expr=None) self._pset['eta'] = Parameter(name='eta', value=nominal_value(eta), vary=True, min=-0.001, max=1.001, expr=None) self._pset['asymmetry'] = Parameter(name='asymmetry', value=nominal_value(asymmetry), vary=False, min=None, max=None, expr=None) #not currently used as a refinable parameter self._pset['background'] = Parameter(name='background', value=nominal_value(background), vary=False, min=0, max=None, expr=None) self.ymin_on_ymax = kwargs.get('ymin_on_ymax',0.001) self.hkl = kwargs.get('hkl',None) self.name = kwargs.get('name',None) shape = kwargs.get('shape','pv') self.shape = shape.lower() self.backup()
def _monte_carlo_error_propagation(self, vr, m): lambda_total = self._lambda_t el = self._lambda_ec f = self._f vel = nominal_value(el) + std_dev(el) * randn(self._n) vt = nominal_value(lambda_total) + std_dev(lambda_total) * randn(self._n) vf = nominal_value(f) + std_dev(f) * randn(self._n) vt_mc = ones(1, m) * vt vf_mc = ones(1, m) * vf vel_mc = ones(1, m) * vel t_mc = log(vt_mc / vel_mc * vf_mc * vr + 1) / vt_mc return mean(t_mc), std(t_mc)
def measure(config): mcu = ArduinoTree() # mcu.soft_reset() vcc = mcu.vcc.read() timer = Stopwatch() measurements = [] # for _ in range(config.repeat): while 1: f = mcu.counter.read(config.gate_time) measurements.append(dict( t=timer.read(), frequency=nominal_value(f), )) if timer.last > config.interval: break data = dict( vcc=vcc, model=avr_name(mcu), measurements=measurements, gate_time=config.gate_time, ) return data
def wrapped_func(*args, **kwargs): args_float = map(uncertainties.nominal_value, args) # !! In Python 2.7+, dictionary comprehension: {argname:...} kwargs_float = dict( (arg_name, uncertainties.nominal_value(value)) for (arg_name, value) in kwargs.iteritems()) return func(*args_float, **kwargs_float)
def _add_isotope(self, analysis, spec, iso, det, refdet): db = self.db if DBVERSION >= 16.3: rdet = analysis.reference_detector.detector_type.Label else: rdet = analysis.ReferenceDetectorLabel if det == rdet: dbdet = refdet else: if spec.is_peak_hop: """ if is_peak_hop fool mass spec. e.g Ar40 det = H1 not CDD det=PEAK_HOP_MAP['Ar40']=='CDD' """ if iso in PEAK_HOP_MAP: det = PEAK_HOP_MAP[iso] if DBVERSION >= 16.3: dbdet = db.add_detector(det) else: dbdet = db.add_detector(det, Label=det) ic = spec.isotopes[iso].ic_factor dbdet.ICFactor = float(nominal_value(ic)) dbdet.ICFactorEr = float(std_dev(ic)) db.flush() n = spec.get_ncounts(iso) return db.add_isotope(analysis, dbdet, iso, NumCnts=n), dbdet
def small_sep02 ( mx , mxErr, lag=0): """ Given a frequency matrix and errors calculates the (scaled) small separation d02 and propagates errors. Notes ----- The parameter lag is the difference between the radial orders of the first modes of l=0 and l=2. """ (Nn, Nl) = np.shape(mx) d02 = np.zeros((1,Nn)) d02Err = np.zeros((1,Nn)) d02.fill(None) # values that can't be calculated are NaN for n in range(1-lag,Nn): if (mx[n,0] != 0. and mx[n-1+lag,2] != 0.): a = un.ufloat( (mx[n,0], mxErr[n,0]) ) b = un.ufloat( (mx[n-1+lag,2], mxErr[n-1+lag,2]) ) result = (a-b) / 3. d02[0,n-1+lag] = un.nominal_value(result) d02Err[0,n-1+lag] = un.std_dev(result) return d02, d02Err
def get_mean_raw(self, tau=None): vs = [] corrfunc = self._deadtime_correct for r in six.itervalues(self._cp): n = int(r['NShots']) nv = ufloat(float(r['Ar40']), float(r['Ar40err'])) * 6240 dv = ufloat(float(r['Ar36']), float(r['Ar36err'])) * 6240 if tau: dv = corrfunc(dv, tau * 1e-9) vs.append((n, nv / dv)) key = lambda x: x[0] vs = sorted(vs, key=key) mxs = [] mys = [] mes = [] for n, gi in groupby(vs, key=key): mxs.append(n) ys, es = list(zip(*[(nominal_value(xi[1]), std_dev(xi[1])) for xi in gi])) wm, werr = calculate_weighted_mean(ys, es) mys.append(wm) mes.append(werr) return mxs, mys, mes
def load_isotopes(self): isos = self.editor.model.isotopes ns = [] bks = [] bs = [] ics = [] dets = [] for k in self.editor.model.isotope_keys: iso = isos[k] iso.use_static = True ns.append(iso) bks.append(iso.blank) bs.append(iso.baseline) det = iso.detector if not det in dets: v, e = nominal_value(iso.ic_factor), std_dev(iso.ic_factor) ics.append(ICFactor(value=v, error=e, ovalue=v, oerror=e, name=det)) dets.append(det) self.isotopes = ns self.blanks = bks self.baselines = bs self.ic_factors = ics
def small_sep13 ( mx , mxErr, lag=0): """ Given a frequency matrix and errors calculates the (scaled) small separation d13 and propagates errors. Notes ----- The parameter lag is the difference between the radial orders of the first modes of l=1 and l=3. """ (Nn, Nl) = np.shape(mx) d13 = np.zeros((1,Nn)) d13Err = np.zeros((1,Nn)) d13.fill(None) # values that can't be calculated remain NaN for n in range(1-lag,Nn): if (mx[n,1] != 0. and mx[n-1+lag,3] != 0.): a = un.ufloat( (mx[n,1], mxErr[n,1]) ) b = un.ufloat( (mx[n-1+lag,3], mxErr[n-1+lag,3]) ) result = (a-b) / 5. d13[0,n-1+lag] = un.nominal_value(result) d13Err[0,n-1+lag] = un.std_dev(result) return d13, d13Err
def _get_j_err(self): j = self.j try: e = (std_dev(j) / nominal_value(j)) if j is not None else 0 except ZeroDivisionError: e = nan return e
def add_irradiation_production(self, name, pr, ifc): kw = {} for k, v in ifc.iteritems(): if k == 'cl3638': k = 'P36Cl38Cl' else: k = k.capitalize() kw[k] = float(nominal_value(v)) kw['{}Er'.format(k)] = float(std_dev(v)) kw['ClOverKMultiplier'] = pr['Cl_K'] kw['ClOverKMultiplierEr'] = 0 kw['CaOverKMultiplier'] = pr['Ca_K'] kw['CaOverKMultiplierEr'] = 0 v = binascii.crc32(''.join([str(v) for v in kw.itervalues()])) with self.session_ctx() as sess: q = sess.query(IrradiationProductionTable) q = q.filter(IrradiationProductionTable.ProductionRatiosID == v) if not self._query_one(q): i = IrradiationProductionTable(Label=name, ProductionRatiosID=v, **kw) self._add_item(i) return v
def age_equation(j, f, include_decay_error=False, lambda_k=None, scalar=None, arar_constants=None): if isinstance(j, tuple): j = ufloat(*j) elif isinstance(j, str): j = ufloat(j) if isinstance(f, tuple): f = ufloat(*f) elif isinstance(f, str): f = ufloat(f) if not lambda_k: if arar_constants is None: arar_constants = ArArConstants() lambda_k = arar_constants.lambda_k if not scalar: if arar_constants is None: arar_constants = ArArConstants() scalar = float(arar_constants.age_scalar) if not include_decay_error: lambda_k = nominal_value(lambda_k) try: return (lambda_k ** -1 * umath.log(1 + j * f)) / scalar except (ValueError, TypeError): return ufloat(0, 0)
def age_equation(j, f, include_decay_error=False, lambda_k=None, scalar=None, arar_constants=None): if isinstance(j, tuple): j = ufloat(*j) elif isinstance(j, str): j = ufloat(j) if isinstance(f, tuple): f = ufloat(*f) elif isinstance(f, str): f = ufloat(f) if not lambda_k: if arar_constants is None: arar_constants = ArArConstants() lambda_k = arar_constants.lambda_k if not scalar: if arar_constants is None: arar_constants = ArArConstants() scalar = float(arar_constants.age_scalar) if not include_decay_error: lambda_k = nominal_value(lambda_k) try: return (lambda_k**-1 * umath.log(1 + j * f)) / scalar except (ValueError, TypeError): return ufloat(0, 0)