def _convert_asm_shot(self, descriptor, x_pixel_size=109.9, y_pixel_size=109.9, bragg_peak_radius=458): """ Convert a Kitty-generated assembled image h5 file to an odin.xray.Shot. """ logger.info('Loading assembled image in: %s' % descriptor['data_file']) for field in self.essential_fields: if field not in descriptor.keys(): raise ValueError('Essential data field %s not in YAML file!' % field) energy = float(descriptor['photon_eV']) / 1000.0 # eV -> keV path_length = float(descriptor['detector_mm']) * 1000.0 # mm -> um logger.debug('Energy: %f keV' % energy) logger.debug('Path length: %f microns' % path_length) # extract intensity data f = tables.File(descriptor['data_file']) try: path = descriptor['i_asm'] except KeyError as e: raise ValueError('Essential data field `i_asm` not in YAML file!') i = f.getNode(path).read() f.close() logger.debug('Read field %s in file: %s' % (descriptor['i_asm'], descriptor['data_file'])) # find the center (center is in pixel units) #dgeo = xray.DetectorGeometry(i) center = (853., 861.) #dgeo.center corner = (-center[0] * x_pixel_size, -center[1] * y_pixel_size, 0.0) logger.debug('Found center: %s' % str(center)) # compile a grid_list object basis = (x_pixel_size, y_pixel_size, 0.0) shape = (i.shape[0], i.shape[1], 1) grid_list = [(basis, shape, corner)] # generate the detector object b = xray.Beam(100, energy=energy) d = xray.Detector.from_basis(grid_list, path_length, b.k) logger.debug('Generated detector object...') # generate the shot s = xray.Shot(i.flatten(), d) return s
def _extract_energy(self, entry): """ Scrape the following important metadata out of the .cxi file: -- beam energy -- """ energy_nodes = self._get_nodes('energy', root=entry, strict=True) if len(energy_nodes) > 1: raise RuntimeError( 'Ambiguity in beam energy... %d energy nodes found' % len(energy_nodes)) energy = float(energy_nodes[0].read()) / 1000.0 beam = xray.Beam(1., energy=energy) # todo: compute flux return beam
def test_evaluate_qmag(self): # doubles as a test for _evaluate_theta x = np.zeros((5, 3)) x[:, 0] = np.random.randn(5) x[:, 2] = self.l S = x.copy() S = S / np.sqrt(np.sum(np.power(S, 2), axis=1))[:, None] S -= self.d.beam_vector b = xray.Beam(1, energy=self.energy) qref = b.k * np.sqrt(np.sum(np.power(S, 2), axis=1)) qmag = self.d.evaluate_qmag(x) assert_allclose(qref, qmag)
def _convert_raw_shot(self, descriptor): """ Convert a Kitty-generated raw image h5 file to an odin.xray.Shot. """ logger.info('Loading raw image in: %s' % descriptor['data_file']) for field in self.essential_fields: if field not in descriptor.keys(): raise ValueError('Essential data field %s not in YAML file!' % field) energy = float(descriptor['photon_eV']) path_length = float(descriptor['detector_mm']) * 1000.0 # mm -> um # load all the relevant data from many h5s... (dumb) f = tables.File(descriptor['x_raw']) x = f.root.data.data.read() f.close() f = tables.File(descriptor['y_raw']) y = f.root.data.data.read() f.close() z = np.zeros_like(x) f = tables.File(descriptor['data_file']) path = descriptor['i_raw'] i = f.getNode(path).read() f.close() # turn that data into a basis representation grid_list, flat_i = self._lcls_raw_to_basis(x, y, z, i) # generate the detector object b = xray.Beam(100, energy=energy) d = xray.Detector.from_basis(grid_list, path_length, b.k) # generate the shot s = xray.Shot(flat_i, d) return s
def as_shotset(self): """ Convert the CBF file to an ODIN shotset representation. Returns ------- cbf : odin.xray.Shotset The CBF file as an ODIN shotset. """ p = np.array(list(self.corner) + [self.path_length]) f = np.array([self.pixel_size[0], 0.0, 0.0]) # fast is x s = np.array([0.0, self.pixel_size[1], 0.0]) # slow is y bg = xray.BasisGrid() bg.add_grid(p, s, f, self.intensities_shape) # todo better value for photons b = xray.Beam(1e4, wavelength=self.wavelength) d = xray.Detector(bg, b.k) s = xray.Shotset(self.intensities.flatten().astype(np.float64), d, self.mask) return s
def test_unit_convs(self): beam = xray.Beam(self.n_photons, energy=1.0) assert_allclose(beam.wavelength, 12.398, rtol=1e-3) assert_allclose(beam.frequency, 2.4190e17, rtol=1e-3) assert_allclose(beam.wavenumber, (2.0 * np.pi) / 12.398, rtol=1e-3)