def test_run_single_train_from_id(): test_run = RunDirectory(RUNPATH) tid, data = test_run.train_from_id(1472810853) assert len(data) == 10 tid, data = test_run.train_from_id(1472810854) assert len(data) == 11 img = data['SPB_DET_AGIPD1M-1/DET/8CH0:xtdf']['image.data'] assert img.shape == (60, 512, 128) with pytest.raises(KeyError) as info: tid, data = test_run.train_from_id(123456789) print(info)
def test_stack_detector_data(mock_fxe_run): test_run = RunDirectory(mock_fxe_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_train_by_id_fxe_run(mock_fxe_run): run = RunDirectory(mock_fxe_run) _, data = run.train_from_id(10024) assert 'FXE_DET_LPD1M-1/DET/15CH0:xtdf' in data assert 'image.data' in data['FXE_DET_LPD1M-1/DET/15CH0:xtdf'] assert 'FXE_XAD_GEC/CAM/CAMERA' in data assert 'firmwareVersion.value' in data['FXE_XAD_GEC/CAM/CAMERA']
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
def test_stack_detector_data(): test_run = RunDirectory(RUNPATH) tid, data = test_run.train_from_id(1472810853) comb = stack_detector_data(data, 'image.data', only='AGIPD1M-1') print(comb.shape) assert comb.shape == (60, 16, 512, 128) np.testing.assert_equal(comb[:, 0, ...], data['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf']['image.data'])
def test_stack_data_2(): test_run = RunDirectory(RUNPATH) tid, data = test_run.train_from_id(1472810853) skip = ['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf', 'SPB_DET_AGIPD1M-1/DET/9CH0:xtdf'] comb = stack_data(data, 'image.data', axis=0, xcept=skip) print(comb.shape) assert comb.shape == (8, 60, 512, 128) np.testing.assert_equal(comb[0, ...], data['SPB_DET_AGIPD1M-1/DET/1CH0:xtdf']['image.data'])
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_2(): test_run = RunDirectory(RUNPATH) tid, data = test_run.train_from_id(1472810853) skip = ['SPB_DET_AGIPD1M-1/DET/{}CH0:xtdf'.format(i) for i in range(3,8)] comb = stack_detector_data(data, 'image.data', axis=0, only='AGIPD1M-1', xcept=skip) print(comb.shape) assert comb.shape == (16, 60, 512, 128) for i in (0, 1, 2, 8, 9, 10): np.testing.assert_equal(comb[i, ...], data['SPB_DET_AGIPD1M-1/DET/{}CH0:xtdf'.format(i)]['image.data']) for i in (3, 4, 5, 6, 7, 11, 12, 13, 14, 15): np.testing.assert_equal(comb[i, ...], np.full((60, 512, 128), np.nan, dtype=np.float32))
def test_stack_detector_data_wrong_pulses(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 (12, 13)] for module in misshaped: data[module]['image.data'] = np.zeros((64, 1, 256, 256), dtype=np.uint16) with pytest.raises(ValueError) as excinfo: comb = stack_detector_data(data, 'image.data') assert '(64, 1, 256, 256)' 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 test_train_by_id_fxe_run_selection(mock_fxe_run): run = RunDirectory(mock_fxe_run) _, data = run.train_from_id(10024, [('*/DET/*', 'image.data')]) assert 'FXE_DET_LPD1M-1/DET/15CH0:xtdf' in data assert 'image.data' in data['FXE_DET_LPD1M-1/DET/15CH0:xtdf'] assert 'FXE_XAD_GEC/CAM/CAMERA' not in data
def get_frame_from_karabo(run_index, frame_index): run = RunDirectory(os.path.join(base_dir, 'r{:04d}'.format(run_index))) ids = get_collected_ids(run) tid, data = run.train_from_id(ids[frame_index]) return data[instrument_source][instrument_key]