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 _add_level(self): irrad = self.irradiation db = self.db with db.session_ctx(): irrad = db.get_irradiation(irrad) nind = 0 if irrad.levels: level = irrad.levels[-1] self.z = level.z if level.holder: self.selected_tray = next( (t for t in self.trays if t == level.holder), None) if level.name in ALPHAS: nind = ALPHAS.index(level.name) + 1 try: self.name = ALPHAS[nind] except IndexError: self.warning_dialog('Too many levels max level={}'.format( ALPHAS[-1])) return av = AddView(model=self) info = av.edit_traits() while 1: if info.result: for attr, msg in ( ('name', 'No name enter for this level. Would you like to enter one?' ), ('selected_production', 'No Production Ratios selected for this level. Would you like to select one?' ), ('selected_tray', 'No tray selected for this level. Would like to select one?' )): info = self._check_attr_set(av, attr, msg) if info == 'break': break elif info is not None: continue if not next( (li for li in irrad.levels if li.name == self.name), None): db.add_irradiation_level(self.name, irrad, self.selected_tray, self.z, self.level_note) return self.name else: self.warning_dialog( 'Level {} already exists for Irradiation {}'. format(self.name, self.irradiation)) else: break
def _set_step(self, v): if isinstance(v, str): v = v.upper() if v in ALPHAS: self._step = ALPHAS.index(v) else: self._step = v
def _add_level(self): irrad = self.irradiation db = self.db with db.session_ctx(): irrad = db.get_irradiation(irrad) nind = 0 if irrad.levels: level = irrad.levels[-1] self.z = level.z if level.production: self.selected_production = next((p for p in self.productions if p.name == level.production.name), None) if level.holder: self.selected_tray = next((t for t in self.trays if t == level.holder.name), None) if level.name in ALPHAS: nind = ALPHAS.index(level.name) + 1 try: self.name = ALPHAS[nind] except IndexError: self.warning_dialog('Too many levels max level={}'.format(ALPHAS[-1])) return av = AddView(model=self) info = av.edit_traits() while 1: if info.result: for attr, msg in (('name', 'No name enter for this level. Would you like to enter one?'), ('selected_production', 'No Production Ratios selected for this level. Would you like to select one?'), ('selected_tray', 'No tray selected for this level. Would like to select one?')): info = self._check_attr_set(av, attr, msg) if info == 'break': break elif info is not None: continue if not next((li for li in irrad.levels if li.name == self.name), None): db.add_irradiation_level(self.name, irrad, self.selected_tray, self.selected_production.name, self.z) self._save_production() return self.name else: self.warning_dialog('Level {} already exists for Irradiation {}'.format(self.name, self.irradiation)) else: break
def get_greatest_step(self, identifier, aliquot): ret = 0 if self.db: identifier = self.get_identifier(identifier) ret = self.db.get_latest_analysis(identifier, aliquot) if ret: _, s = ret if s is not None and s in ALPHAS: ret = ALPHAS.index(s) # if s is not None else -1 else: ret = -1 return ret
def get_analysis_runid(self, idn, aliquot, step=None): with self.session_ctx() as sess: q = sess.query(AnalysisTbl) q = q.join(IrradiationPositionTbl) if step: if isinstance(step, (str, unicode)): step = ALPHAS.index(step) q = q.filter(AnalysisTbl.increment == step) if aliquot: q = q.filter(AnalysisTbl.aliquot == aliquot) q = q.filter(IrradiationPositionTbl.identifier == idn) return self._query_one(q)
def get_greatest_step(self, ln, aliquot): """ return greatest step for this labnumber and aliquot. return step as an integer. A=0, B=1... """ with self.session_ctx() as sess: if ln: ln = self.get_identifier(ln) if not ln: return q = sess.query(AnalysisTbl.step) q = q.join(IrradiationPositionTbl) q = q.filter(IrradiationPositionTbl.identifier == ln) q = q.filter(AnalysisTbl.aliquot == aliquot) # q = q.order_by(cast(meas_AnalysisTable.step, INTEGER(unsigned=True)).desc()) q = q.order_by(AnalysisTbl.increment.desc()) result = self._query_one(q) if result: step = result[0] return ALPHAS.index(step) if step else -1
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
def _transfer_analysis(self, rec, exp, overwrite=True, monitor_mapping=None): dest = self.dvc.db proc = self.processor src = proc.db # args = rec.split('-') # idn = '-'.join(args[:-1]) # t = args[-1] # try: # aliquot = int(t) # step = None # except ValueError: # aliquot = int(t[:-1]) # step = t[-1] m = IDENTIFIER_REGEX.match(rec) if not m: m = SPECIAL_IDENTIFIER_REGEX.match(rec) if not m: self.warning('invalid runid {}'.format(rec)) return else: idn = m.group('identifier') aliquot = m.group('aliquot') try: step = m.group('step') or None except IndexError: step = None if idn == '4359': idn = 'c-01-j' elif idn == '4358': idn = 'c-01-o' # check if analysis already exists. skip if it does if dest.get_analysis_runid(idn, aliquot, step): self.warning('{} already exists'.format(make_runid(idn, aliquot, step))) return dban = src.get_analysis_runid(idn, aliquot, step) iv = IsotopeRecordView() iv.uuid = dban.uuid self.debug('make analysis idn:{}, aliquot:{} step:{}'.format(idn, aliquot, step)) try: an = proc.make_analysis(iv, unpack=True, use_cache=False, use_progress=False) except: self.warning('Failed to make {}'.format(make_runid(idn, aliquot, step))) return self._transfer_meta(dest, dban, monitor_mapping) # return dblab = dban.labnumber if dblab.irradiation_position: irrad = dblab.irradiation_position.level.irradiation.name level = dblab.irradiation_position.level.name irradpos = dblab.irradiation_position.position else: irrad = 'NoIrradiation' level = 'A' irradpos = self._get_irradpos(dest, irrad, level, dblab.identifier) # irrad, level, irradpos = '', '', 0 extraction = dban.extraction ms = dban.measurement.mass_spectrometer.name if not dest.get_mass_spectrometer(ms): self.debug('adding mass spectrometer {}'.format(ms)) dest.add_mass_spectrometer(ms) dest.commit() ed = extraction.extraction_device.name if extraction.extraction_device else None if not ed: ed = 'No Extract Device' if not dest.get_extraction_device(ed): self.debug('adding extract device {}'.format(ed)) dest.add_extraction_device(ed) dest.commit() if step is None: inc = -1 else: inc = ALPHAS.index(step) username = '' if dban.user: username = dban.user.name if not dest.get_user(username): self.debug('adding user. username:{}'.format(username)) dest.add_user(username) dest.commit() if monitor_mapping: sample_name, material_name, project_name = monitor_mapping else: dbsam = dblab.sample sample_name = dbsam.name material_name = dbsam.material.name project_name = format_repository_identifier(dbsam.project.name) rs = AutomatedRunSpec(labnumber=idn, username=username, material=material_name, project=project_name, sample=sample_name, irradiation=irrad, irradiation_level=level, irradiation_position=irradpos, repository_identifier=exp, mass_spectrometer=ms, uuid=dban.uuid, _step=inc, comment=dban.comment or '', aliquot=int(aliquot), extract_device=ed, duration=extraction.extract_duration, cleanup=extraction.cleanup_duration, beam_diameter=extraction.beam_diameter, extract_units=extraction.extract_units or '', extract_value=extraction.extract_value, pattern=extraction.pattern or '', weight=extraction.weight, ramp_duration=extraction.ramp_duration or 0, ramp_rate=extraction.ramp_rate or 0, collection_version='0.1:0.1', queue_conditionals_name='', tray='') meas = dban.measurement # get spectrometer parameters # gains gains = {} gain_history = dban.gain_history if gain_history: gains = {d.detector.name: d.value for d in gain_history.gains} # deflections deflections = {d.detector.name: d.deflection for d in meas.deflections} # source src = {k: getattr(meas.spectrometer_parameters, k) for k in QTEGRA_SOURCE_KEYS} ps = PersistenceSpec(run_spec=rs, tag=an.tag.name, arar_age=an, timestamp=dban.analysis_timestamp, defl_dict=deflections, gains=gains, spec_dict=src, use_repository_association=True, positions=[p.position for p in extraction.positions]) self.debug('transfer analysis with persister') self.persister.per_spec_save(ps, commit=False, msg_prefix='Database Transfer') return True