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 modify_meta(p): jd = dvc_load(p) jd['aliquot'] = aliquot jd['increment'] = alpha_to_int(step) dvc_dump(jd, p)
def _add_level(self): irrad = self.irradiation db = self.db irrad = db.get_irradiation(irrad) if irrad.levels: level = irrad.levels[-1] self.z = level.z or 0 if level.holder: self.selected_tray = next( (t for t in self.trays if t == level.holder), '') nind = alpha_to_int(level.name) + 1 self.name = alphas(nind) 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): if self._save_level(): return self.name else: break else: self.warning_dialog( 'Level {} already exists for Irradiation {}'.format( self.name, self.irradiation)) else: break
def _bulk_runid(self, ai, aliquot, step): if not aliquot: aliquot = ai.aliquot if not step: step = ai.step self.dvc.db.modify_aliquot_step(ai.uuid, aliquot, alpha_to_int(step)) def modify_meta(p): jd = dvc_load(p) jd['aliquot'] = aliquot jd['increment'] = alpha_to_int(step) dvc_dump(jd, p) ps = [] repo_id = ai.repository_identifier sp = analysis_path(('', ai.record_id), repo_id) if sp: modify_meta(sp) ps.append(sp) # using runid path name new_runid = make_runid(ai.identifier, aliquot, step) for m in NPATH_MODIFIERS: sp = analysis_path(('', ai.record_id), repo_id, modifier=m) dp = analysis_path(('', new_runid), repo_id, modifier=m, mode='w') if sp and os.path.isfile(sp): if os.path.isfile(dp) and m != 'extraction': continue ps.append(sp) ps.append(dp) shutil.move(sp, dp) else: # using uuid path name # only need to modify metadata file sp = analysis_path(ai, repo_id) modify_meta(sp, aliquot, step) ps.append(sp) return repo_id, ps
def _map_paths(self, repo, src, dst): root = os.path.join(paths.repository_dataset_dir, repo) ps = [] def debug(msg, a, b): self.debug('{:<20s} {:<35s} >> {}'.format(msg, os.path.relpath(a, root), os.path.relpath(b, root))) sp = analysis_path(src, repo) dp = analysis_path(dst, repo, mode='w') ps.append(sp) ps.append(dp) if not os.path.isfile(sp): self.warning('not a file {}'.format(sp)) return dl, da, ds = strip_runid(dst) jd = dvc_load(sp) jd['identifier'] = dl jd['aliquot'] = da jd['increment'] = alpha_to_int(ds) dvc_dump(jd, dp) debug('----------', sp, dp) for modifier in ('baselines', 'blanks', 'extraction', 'intercepts', 'icfactors', 'peakcenter', '.data'): sp = analysis_path(src, repo, modifier=modifier) dp = analysis_path(dst, repo, modifier=modifier, mode='w') if sp and os.path.isfile(sp): debug('{:<20s}'.format(modifier), sp, dp) ps.append(sp) ps.append(dp) shutil.move(sp, dp) return ps
def _set_step(self, v): if isinstance(v, str): self._step = alpha_to_int(v) else: self._step = v
def make_increment(s): return alpha_to_int(s)
def test_reciprocal6(self): self.assertEqual(alphas(alpha_to_int('ZZZZ')), 'ZZZZ')
def test_reciprocal5(self): self.assertEqual(alphas(alpha_to_int('AAA')), 'AAA')
def test_reciprocal4(self): self.assertEqual(alphas(alpha_to_int('BA')), 'BA')
def test_reciprocal2(self): self.assertEqual(alphas(alpha_to_int('C')), 'C')
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 BaseException as e: # self.warning('Failed to make {}'.format(make_runid(idn, aliquot, step))) # self.warning('exception: {}'.format(e)) # 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 = alpha_to_int(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.decode('utf-8') 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, isotope_group=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, commit_tag='Database Transfer') return True