def save_gains(self, ms, gains_dict): p = self._gain_path(ms) # with open(p, 'w') as wfile: jdump(gains_dict, p) if self.add_paths(p): self.commit('Updated gains')
def dump(self): obj = {'name': self.name, 'omit_dict': self.omit_dict} if not self.path: self.path = analysis_path(self.record_id, self.experiment_identifier, modifier='tag', mode='w') # with open(self.path, 'w') as wfile: # json.dump(obj, wfile, indent=4) jdump(obj, self.path)
def _save_spectrometer_file(self, path): obj = dict(spectrometer=dict(self.per_spec.spec_dict), gains=dict(self.per_spec.gains), deflections=dict(self.per_spec.defl_dict)) hexsha = self.dvc.get_meta_head() obj['commit'] = str(hexsha) jdump(obj, path)
def _save_monitor(self): if self.per_spec.monitor: p = self._make_path(modifier='monitor') checks = [] for ci in self.per_spec.monitor.checks: data = ''.join([struct.pack('>ff', x, y) for x, y in ci.data]) params = dict(name=ci.name, parameter=ci.parameter, criterion=ci.criterion, comparator=ci.comparator, tripped=ci.tripped, data=data) checks.append(params) jdump(checks, p)
def update_flux(self, irradiation, level, pos, identifier, j, e, decay, analyses, add=True): p = self.get_level_path(irradiation, level) with open(p, 'r') as rfile: jd = json.load(rfile) njd = [ji if ji['position'] != pos else {'position': pos, 'j': j, 'j_err': e, 'decay_constants': decay, 'identifier': identifier, 'analyses': [{'uuid': ai.uuid, 'record_id': ai.record_id, 'status': ai.is_omitted()} for ai in analyses]} for ji in jd] # n = {'decay_constants': decay, 'positions': njd} # with open(p, 'w') as wfile: # json.dump(njd, wfile, indent=4) jdump(njd, p) if add: self.add(p, commit=False)
def post_extraction_save(self, rblob, oblob, snapshots): p = self._make_path(modifier='extraction') if rblob: rblob = base64.b64encode(rblob[0]) if oblob: oblob = base64.b64encode(oblob[0]) obj = {'request': rblob, 'response': oblob} for e in EXTRACTION_ATTRS: v = getattr(self.per_spec.run_spec, e) obj[e] = v ps = [] for i, pp in enumerate(self.per_spec.positions): pos, x, y, z = None, None, None, None if isinstance(pp, tuple): if len(pp) == 2: x, y = pp elif len(pp) == 3: x, y, z = pp else: pos = pp try: ep = self.per_spec.extraction_positions[i] x = ep[0] y = ep[1] if len(ep) == 3: z = ep[2] except IndexError: self.debug('no extraction position for {}'.format(pp)) pd = {'x': x, 'y': y, 'z': z, 'position': pos, 'is_degas': self.per_spec.run_spec.identifier == 'dg'} ps.append(pd) obj['positions'] = ps hexsha = self.dvc.get_meta_head() obj['commit'] = str(hexsha) jdump(obj, p)
def _save_peak_center(self, pc): self.info('DVC saving peakcenter') p = self._make_path(modifier='peakcenter') obj = {} if pc: obj['reference_detector'] = pc.reference_detector obj['reference_isotope'] = pc.reference_isotope if pc.result: xs, ys, _mx, _my = pc.result obj.update({'low_dac': xs[0], 'center_dac': xs[1], 'high_dac': xs[2], 'low_signal': ys[0], 'center_signal': ys[1], 'high_signal': ys[2]}) data = pc.get_data() if data: fmt = '>ff' obj['fmt'] = fmt for det, pts in data: obj[det] = base64.b64encode(''.join([struct.pack(fmt, *di) for di in pts])) jdump(obj, p)
def add_level(self, irrad, level): p = self.get_level_path(irrad, level) jdump([], p) self.add(p, commit=False)
def _save_analysis(self, timestamp, **kw): isos = {} dets = {} signals = [] baselines = [] sniffs = [] blanks = {} intercepts = {} cbaselines = {} icfactors = {} if self.use_isotope_classifier: clf = self.isotope_classifier endianness = '>' for iso in self.per_spec.isotope_group.isotopes.values(): sblob = base64.b64encode(iso.pack(endianness, as_hex=False)) snblob = base64.b64encode(iso.sniff.pack(endianness, as_hex=False)) signals.append({'isotope': iso.name, 'detector': iso.detector, 'blob': sblob}) sniffs.append({'isotope': iso.name, 'detector': iso.detector, 'blob': snblob}) isod = {'detector': iso.detector} if self.use_isotope_classifier: klass, prob = clf.predict_isotope(iso) isod.update(classification=klass, classification_probability=prob) isos[iso.name] = isod if iso.detector not in dets: bblob = base64.b64encode(iso.baseline.pack(endianness, as_hex=False)) baselines.append({'detector': iso.detector, 'blob': bblob}) dets[iso.detector] = {'deflection': self.per_spec.defl_dict.get(iso.detector), 'gain': self.per_spec.gains.get(iso.detector)} icfactors[iso.detector] = {'value': float(nominal_value(iso.ic_factor)), 'error': float(std_dev(iso.ic_factor)), 'fit': 'default', 'references': []} cbaselines[iso.detector] = {'fit': iso.baseline.fit, 'value': float(nominal_value(iso.baseline.uvalue)), 'error': float(std_dev(iso.baseline.uvalue))} intercepts[iso.name] = {'fit': iso.fit, 'value': float(nominal_value(iso.uvalue)), 'error': float(std_dev(iso.uvalue))} blanks[iso.name] = {'fit': 'previous', 'references': [{'runid': self.per_spec.previous_blank_runid, 'exclude': False}], 'value': float(nominal_value(iso.blank.uvalue)), 'error': float(std_dev(iso.blank.uvalue))} obj = self._make_analysis_dict() from pychron.experiment import __version__ as eversion from pychron.dvc import __version__ as dversion if not self.per_spec.timestamp: obj['timestamp'] = timestamp.isoformat() else: obj['timestamp'] = self.per_spec.timestamp.isoformat() obj['collection_version'] = '{}:{}'.format(eversion, dversion) obj['detectors'] = dets obj['isotopes'] = isos obj.update(**kw) # save the scripts ms = self.per_spec.run_spec.mass_spectrometer for si in ('measurement', 'extraction'): name = getattr(self.per_spec, '{}_name'.format(si)) blob = getattr(self.per_spec, '{}_blob'.format(si)) self.dvc.meta_repo.update_script(ms, name, blob) # save experiment self.dvc.update_experiment_queue(ms, self.per_spec.experiment_queue_name, self.per_spec.experiment_queue_blob) hexsha = str(self.dvc.get_meta_head()) obj['commit'] = hexsha # dump runid.json p = self._make_path() jdump(obj, p) p = self._make_path(modifier='intercepts') jdump(intercepts, p) # dump runid.blank.json p = self._make_path(modifier='blanks') jdump(blanks, p) p = self._make_path(modifier='baselines') jdump(cbaselines, p) p = self._make_path(modifier='icfactors') jdump(icfactors, p) # dump runid.data.json p = self._make_path(modifier='.data') data = {'commit': hexsha, 'encoding': 'base64', 'format': '{}ff'.format(endianness), 'signals': signals, 'baselines': baselines, 'sniffs': sniffs} jdump(data, p)
def _add_interpreted_age(self, ia, d): p = analysis_path('{}.ia'.format(ia.identifier), ia.experiment_identifier) jdump(d, p)
def _dump(self, obj, path=None, modifier=None): if path is None: path = self._analysis_path(modifier) jdump(obj, path)