def test_stack_detector_data_missing(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) # Three variants of missing data: # 1. Source missing del data['FXE_DET_LPD1M-1/DET/3CH0:xtdf'] # 2. Key missing del data['FXE_DET_LPD1M-1/DET/7CH0:xtdf']['image.data'] # 3. Empty array missing = [ 'FXE_DET_LPD1M-1/DET/{}CH0:xtdf'.format(m) for m in (1, 5, 9, 15) ] for module in missing: data[module]['image.data'] = np.zeros((0, 1, 256, 256), dtype=np.uint16) comb = stack_detector_data(data, 'image.data', fillvalue=22) assert comb.shape == (128, 1, 16, 256, 256) assert not (comb[:, :, 0] == 22).any() # Control assert (comb[:, :, 3] == 22).all() # Source missing assert (comb[:, :, 7] == 22).all() # Key missing assert (comb[:, :, 5] == 22).all() # Empty array # default fillvalue for int is 0 comb = stack_detector_data(data, 'image.data') assert (comb[:, :, 3] == 0).all() with pytest.raises(ValueError): comb = stack_detector_data(data, 'image.data', fillvalue=np.nan)
def _get_modules_file(self, data, src): """Override. In the file, the data is separated into arrays of different modules. The layout of data for each module is: - calibrated, (memory cells, x, y) - raw, (memory cells, 2, x, y) - calibrated, "image.data", (memory cells, modules, y, x) - raw, "image.data", (memory cell, 2, modules, y, x) [:, 0, ...] -> data [:, 1, ...] -> gain -> (memory cells, modules, y, x) """ modules_data = stack_detector_data(data[src], src.split(' ')[1], real_array=False) dtype = modules_data.dtype if dtype == _IMAGE_DTYPE: return modules_data if dtype == _RAW_IMAGE_DTYPE: return modules_data[:, 0, ...] raise AssemblingError(f"Unknown detector data type: {dtype}!")
def test_stack_detector_data(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) comb = stack_detector_data(data, 'image.data') assert comb.shape == (128, 1, 16, 256, 256)
def test_stack_detector_data_stackview(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) # Three variants of missing data: # 1. Source missing del data['FXE_DET_LPD1M-1/DET/3CH0:xtdf'] # 2. Key missing del data['FXE_DET_LPD1M-1/DET/7CH0:xtdf']['image.data'] # 3. Empty array missing = ['FXE_DET_LPD1M-1/DET/{}CH0:xtdf'.format(m) for m in (1, 5, 9, 15)] for module in missing: data[module]['image.data'] = np.zeros((0, 1, 256, 256), dtype=np.uint16) comb = stack_detector_data(data, 'image.data', fillvalue=22, real_array=False) assert comb.shape == (128, 1, 16, 256, 256) assert not (comb[:, :, 0] == 22).any() # Control assert (comb[:, :, 3] == 22).all() # Source missing assert (comb[:, :, 7] == 22).all() # Key missing assert (comb[:, :, 5] == 22).all() # Empty array # Slice across all modules pulse = comb[0, 0] assert pulse.shape == (16, 256, 256) assert not (pulse[0] == 22).any() assert (pulse[3] == 22).all() assert (pulse[7] == 22).all() assert (pulse[5] == 22).all() pulse_arr = pulse.asarray() assert pulse_arr.shape == (16, 256, 256) assert pulse_arr.max() == 22 assert pulse_arr.min() == 0
def agipd_frame(mock_run): run = RunDirectory(mock_run) print(run.instrument_sources) print(run.keys_for_source('SPB_DET_AGIPD1M-1/DET/10CH0:xtdf')) tid, train_data = run.select('*/DET/*', 'image.data').train_from_index(0) ary = stack_detector_data(train_data, 'image.data') return ary[0]
def test_stack_detector_data_jungfrau(mock_jungfrau_run): run = RunDirectory(mock_jungfrau_run) _, data = run.select('*JF4M/DET/*', 'data.adc').train_from_index(0) comb = stack_detector_data(data, 'data.adc', modules=8, pattern=r'/DET/JNGFR(\d+)', starts_at=1) assert comb.shape == (16, 8, 512, 1024)
def test_stack_detector_data_type_error(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) module = 'FXE_DET_LPD1M-1/DET/3CH0:xtdf' data[module]['image.data'] = data[module]['image.data'].astype(np.float32) with pytest.raises(ValueError) as excinfo: comb = stack_detector_data(data, 'image.data') assert "dtype('float32')" in str(excinfo.value)
def test_stack_detector_data_wrong_shape(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) misshaped = ['FXE_DET_LPD1M-1/DET/{}CH0:xtdf'.format(m) for m in (0, 15)] for module in misshaped: data[module]['image.data'] = np.zeros((128, 1, 512, 128), dtype=np.uint16) with pytest.raises(ValueError) as excinfo: comb = stack_detector_data(data, 'image.data') assert '(128, 1, 512, 128)' in str(excinfo.value)
def test_stack_detector_data_extra_mods(mock_fxe_raw_run): test_run = RunDirectory(mock_fxe_raw_run) tid, data = test_run.train_from_id(10000, devices=[('*/DET/*', 'image.data')]) data.setdefault( 'FXE_DET_LPD1M-1/DET/16CH0:xtdf', {'image.data': np.zeros((128, 1, 256, 256), dtype=np.uint16)}, ) with pytest.raises(IndexError) as excinfo: comb = stack_detector_data(data, 'image.data') assert "16" in str(excinfo.value)
def _get_modules_file(self, data, src): """Override. - calibrated, "image.data", (memory cells, modules, y, x) - raw, "image.data", (memory cell, 1, modules, y, x) -> (memory cells, modules, y, x) """ modules_data = stack_detector_data( data[src], src.split(' ')[1], real_array=False) dtype = modules_data.dtype if dtype == _IMAGE_DTYPE: return modules_data if dtype == _RAW_IMAGE_DTYPE: return modules_data.squeeze(axis=1) raise AssemblingError(f"Unknown detector data type: {dtype}!")
def get_train_stack(self): """Get a 4D array representing detector data in a train (pulses, modules, slow_scan, fast_scan) """ tid = self.sb_train_id.value() if tid == self._cached_train_stack[0]: return self._cached_train_stack[1] self.main_widget.log.info('Reading train #: %s', tid) _, data = self.rundir.select('*/DET/*', 'image.data').train_from_id(tid) img = stack_detector_data(data, 'image.data') # Probaply raw data with gain dimension - take the data dim if len(img.shape) == 5: img = img[:, 0] # TODO: confirm if first gain dim is data arr = np.clip(img, 0, None) self._cached_train_stack = (tid, arr) return arr