def process_dataset(self, metadata, flags=(), sample=None): numbers = datatools.frameset_to_list(metadata['frames']) filename = os.path.join(metadata['directory'], metadata['filename'].format(numbers[0])) suffix = 'anom' if 'anomalous' in flags else 'native' params = { 'uuid': str(uuid.uuid4()), 'title': 'MX analysis in progress ...', 'state': self.manager.State.ACTIVE, 'data': metadata, 'sample_id': metadata['sample_id'], 'name': metadata['name'], 'file_names': [filename], 'anomalous': 'anomalous' in flags, 'activity': 'proc-{}'.format(suffix), 'type': metadata['type'], } params = datatools.update_for_sample(params, sample, overwrite=False) self.manager.add_item(params, False) try: report = yield self.beamline.dps.process_mx(params, params['directory'], misc.get_project_name()) except Exception as e: logger.error('MX analysis failed: {}'.format(str(e))) self.failed(e, params['uuid'], self.ResultType.MX) returnValue({}) else: report['data_id'] = [_f for _f in [metadata.get('id')] if _f] self.save_report(report) self.succeeded(report, params['uuid'], self.ResultType.MX) returnValue(report)
def generate_run_list(self): runs = [] pos = 0 item = self.run_store.get_item(pos) sample = self.sample_store.get_current() while item: if item.state in [item.StateType.DRAFT, item.StateType.ACTIVE]: run = {'uuid': item.uuid} run.update(item.info) # convert points to coordinates and then # make sure point is not empty if end_point is set for name in ['p0', 'p1']: if run.get(name) not in [-1, 0, None]: run[name] = self.run_editor.get_point(run[name]) elif name in run: del run[name] if 'p1' in run and 'p0' not in run: run['p0'] = run.pop('p1') run = datatools.update_for_sample(run, sample) runs.append(run) pos += 1 item = self.run_store.get_item(pos) return runs
def process_powder(self, metadata, flags=(), sample=None): file_names = [ os.path.join(metadata['directory'], metadata['filename'].format(number)) for number in datatools.frameset_to_list(metadata['frames']) ] params = { 'uuid': str(uuid.uuid4()), 'title': 'XRD Analysis in progress ...', 'state': self.manager.State.ACTIVE, 'data': metadata, 'sample_id': metadata['sample_id'], 'name': metadata['name'], 'file_names': file_names, 'calib': 'calibrate' in flags, 'activity': 'proc-xrd', 'type': metadata['type'], } params = datatools.update_for_sample(params, sample, overwrite=False) self.manager.add_item(params, False) try: report = yield self.beamline.dps.process_xrd(params, params['directory'], misc.get_project_name()) except Exception as e: logger.error('XRD analysis failed: {}'.format(str(e))) self.failed(e, params['uuid'], self.ResultType.XRD) returnValue({}) else: report['data_id'] = [_f for _f in [metadata.get('id')] if _f] self.save_report(report) self.succeeded(report, params['uuid'], self.ResultType.XRD) returnValue(report)
def start_raster(self, *args, **kwargs): if self.props.state == self.StateType.ACTIVE: self.widget.raster_progress_lbl.set_text("Pausing raster ...") self.collector.pause() elif self.props.state == self.StateType.PAUSED: self.widget.raster_progress_lbl.set_text("Resuming raster ...") self.collector.resume() elif self.props.state == self.StateType.READY: self.widget.raster_progress_lbl.set_text("Starting raster ...") params = { 'name': datetime.now().strftime('%y%m%d-%H%M'), 'uuid': str(uuid.uuid4()), 'activity': 'raster', 'energy': self.beamline.energy.get_position(), 'delta': RASTER_DELTA, 'attenuation': self.beamline.attenuator.get(), } params.update(self.microscope.grid_params) params.update(self.form.get_values()) params.update({ 'distance': resol_to_dist(params['resolution'], self.beamline.detector.mm_size, params['energy']), 'origin': self.beamline.goniometer.stage.get_xyz(), }) params = datatools.update_for_sample(params, self.sample_store.get_current()) self.collector.configure(params) self.collector.start()
def take_snapshot(self): params = self.form.get_values() params['angle'] = self.beamline.goniometer.omega.get_position() params['energy'] = self.beamline.energy.get_position() params['distance'] = resol_to_dist(params['resolution'], self.beamline.detector.mm_size, params['energy']) params['attenuation'] = self.beamline.attenuator.get() params['delta'] = 1.0 params['uuid'] = str(uuid.uuid4()) params['name'] = datetime.now().strftime('%y%m%d-%H%M%S') params['activity'] = 'humidity' params = datatools.update_for_sample(params, self.sample_store.get_current()) self.collector.configure(params) self.collector.start()
def process_raster(self, params, flags=(), sample=None): params.update({ 'uuid': str(uuid.uuid4()), 'activity': 'proc-raster', }) params = datatools.update_for_sample(params, sample, overwrite=False) try: report = yield self.beamline.dps.analyse_frame(params['filename'], misc.get_project_name(), rastering=True) except Exception as e: logger.error('Raster analysis failed: {}'.format(str(e))) self.failed(e, params['uuid'], self.ResultType.RASTER) returnValue({}) else: report['data_id'] = [_f for _f in [params.get('id')] if _f] self.succeeded(report, params['uuid'], self.ResultType.RASTER) returnValue(report)
def start(self, *args, **kwargs): if self.props.state == self.StateType.ACTIVE: self.progress_lbl.set_text("Pausing {} ...".format(self.desc)) self.scanner.pause() elif self.props.state == self.StateType.PAUSED: self.progress_lbl.set_text("Resuming {} ...".format(self.desc)) self.scanner.resume() elif self.props.state == self.StateType.READY: self.progress_lbl.set_text("Starting {} ...".format(self.desc)) params = self.form.get_values() params['uuid'] = str(uuid.uuid4()) params['name'] = datetime.now().strftime('%y%m%d-%H%M') params['activity'] = '{}-scan'.format(self.prefix) params = datatools.update_for_sample( params, self.sample_store.get_current()) self.props.config = params self.scanner.configure(**self.props.config) self.scanner.start()
def process_multiple(self, *metadatas, **kwargs): sample = kwargs.get('sample', None) flags = kwargs.get('flags', ()) file_names = [] names = [] for metadata in metadatas: numbers = datatools.frameset_to_list(metadata['frames']) file_names.append(os.path.join(metadata['directory'], metadata['filename'].format(numbers[0]))) names.append(metadata['name']) metadata = metadatas[0] suffix = 'mad' if 'mad' in flags else 'merge' params = { 'uuid': str(uuid.uuid4()), 'title': 'MX {} analysis in progress ...'.format(suffix.upper()), 'state': self.manager.State.ACTIVE, 'data': metadata, 'sample_id': metadata['sample_id'], 'name': '-'.join(names), 'file_names': file_names, 'anomalous': 'anomalous' in flags, 'merge': 'merge' in flags, 'mad': 'mad' in flags, 'activity': 'proc-{}'.format(suffix), 'type': metadata['type'], } params = datatools.update_for_sample(params, sample, overwrite=False) self.manager.add_item(params, False) try: report = yield self.beamline.dps.process_mx(params, params['directory'], misc.get_project_name()) except Exception as e: logger.error('MX analysis failed: {}'.format(str(e))) self.failed(e, params['uuid'], self.ResultType.MX) returnValue({}) else: report['data_id'] = [_f for _f in [metadata.get('id') for metadata in metadatas] if _f] self.save_report(report) self.succeeded(report, params['uuid'], self.ResultType.MX) returnValue(report)
def center_diffraction(self): self.center_loop(3, 1) scores = [] if self.score < 0.5: logger.error('Loop-centering failed, aborting!') return scores.append(self.score) aperture = self.beamline.aperture.get() resolution = RASTER_RESOLUTION energy = self.beamline.energy.get_position() for step in ['edge', 'face']: logger.info('Performing raster scan on {}'.format(step)) self.beamline.goniometer.wait(start=False) if step == 'face': self.beamline.goniometer.omega.move_by(-90, wait=True) time.sleep(2.0) params = { 'name': datetime.now().strftime('%y%m%d-%H%M'), 'uuid': str(uuid.uuid4()), 'activity': 'raster', 'energy': energy, 'delta': RASTER_DELTA, 'exposure': RASTER_EXPOSURE, 'attenuation': self.beamline.attenuator.get(), 'aperture': aperture, 'distance': converter.resol_to_dist(resolution, self.beamline.detector.mm_size, energy), 'origin': self.beamline.goniometer.stage.get_xyz(), 'resolution': resolution, } if step == 'edge': params.update({ 'angle': self.beamline.goniometer.omega.get_position(), 'width': min(aperture * 5, 200.0), 'height': min(aperture * 5, 200.0), 'frames': 5, 'lines': 5, }) else: params.update({ 'angle': self.beamline.goniometer.omega.get_position(), 'width': min(aperture * 4, 200.0), 'height': min(aperture * 4, 200.0), 'frames': 4, 'lines': 4, }) params = datatools.update_for_sample( params, self.sample_store.get_current()) logger.info('Finding best diffraction spot in grid') self.collector.configure(params) self.collector.run() grid_scores = numpy.array([ (index, misc.frame_score(report)) for index, report in sorted(self.collector.results.items()) ]) best = grid_scores[:, 1].argmax() index = int(grid_scores[best, 0]) score = grid_scores[best, 1] logger.info(f'Best diffraction at {index}: score={score}') grid = self.collector.get_grid() point = grid[best] self.beamline.goniometer.stage.move_xyz(point[0], point[1], point[2], wait=True) scores.append(score / 100.) self.score = numpy.mean(scores)
def run(self): self.set_state(busy=True, started=None) pos = 0 self.pause_message = '' for sample in self.samples: if self.stopped: break self.emit('sample-started', sample['uuid']) for task in self.tasks: if self.paused: self.intervene() if self.stopped: break pos += 1 self.emit('progress', *self.get_progress(pos, task, sample)) logger.info('Sample: {}/{}, Task: {}'.format( sample['group'], sample['name'], self.TaskNames[task['type']])) if task['type'] == self.Task.PAUSE: self.intervene( 'As requested, automation has been paused for manual intervention. \n' 'Please resume after intervening to continue the sequence. ' ) elif task['type'] == self.Task.CENTER: if self.beamline.automounter.is_mounted(sample['port']): method = task['options'].get('method') self.centering.configure(method=method) self.centering.run() if self.centering.score < 0.5: self.intervene( 'Not confident about the centering, automation has been paused\n' 'Please resume after manual centering. ') else: self.emit('error', 'Sample not mounted. Aborting!') self.stop() elif task['type'] == self.Task.MOUNT: success = auto.auto_mount_manual(self.beamline, sample['port']) if success and self.beamline.automounter.is_mounted( sample['port']): mounted = self.beamline.automounter.get_state("sample") barcode = mounted.get('barcode') if sample['barcode'] and barcode and barcode != sample[ 'barcode']: logger.error('Barcode mismatch: {} vs {}'.format( barcode, sample['barcode'])) else: logger.debug('Success: {}, Mounted: {}'.format( success, self.beamline.automounter.is_mounted( sample['port']))) self.emit( 'error', 'Mouting Failed. Unable to continue automation!') self.stop() elif task['type'] == self.Task.ACQUIRE: if self.beamline.automounter.is_mounted(sample['port']): params = {} params.update(task['options']) params.update({ 'name': sample['name'], 'uuid': str(uuid.uuid4()) }) params = datatools.update_for_sample(params, sample) logger.debug( 'Acquiring frames for sample {}, in directory {}.'. format(sample['name'], params['directory'])) self.collector.configure( [params], take_snapshots=True, analysis=params.get('analysis'), anomalous=params.get('anomalous', False)) sample['results'] = self.collector.execute() else: self.emit( 'error', 'Sample not mounted. Unable to continue automation!' ) self.stop() self.emit('sample-done', sample['uuid']) if self.stopped: self.set_state(stopped=None, busy=False) logger.info('Automation stopped') if not self.stopped: auto.auto_dismount_manual(self.beamline) self.set_state(done=None, busy=False) logger.info('Automation complete')