def test_exclude_subruns(nexusfile, projectfile): """Test converting NeXus to project and convert to diffraction pattern Note: project file cannot be the same as NeXus file as the output file will be removed by pytest Parameters ---------- nexusfile projectfile Returns ------- """ sub_runs = [2, 4, 5] # convert the nexus file to a project file and do the "simple" checks converter = NeXusConvertingApp(nexusfile, None) hidra_ws = converter.convert() reducer = ReductionApp() reducer.load_hidra_workspace(hidra_ws) reducer.reduce_data(instrument_file=None, calibration_file=None, mask=None, sub_runs=sub_runs, van_file=None) reducer.save_diffraction_data(projectfile) reduced_ws = HidraWorkspace('test_powder_pattern') reduced_project = HidraProjectFile(projectfile) reduced_ws.load_hidra_project(reduced_project, load_raw_counts=False, load_reduced_diffraction=True) assert sub_runs == reduced_ws.get_sub_runs() reducer.reduce_data(instrument_file=None, calibration_file=None, mask=None, sub_runs=[], van_file=None) for sub_run in sub_runs: np.testing.assert_allclose(reducer.get_diffraction_data(sub_run), reduced_ws.get_reduced_diffraction_data(sub_run)) # cleanup reduced_project.close() os.remove(projectfile)
def test_powder_pattern_service(project_file_name, mask_file_name, gold_file): """Test the powder pattern calculator (service) with HB2B-specific reduction routine Parameters ---------- project_file_name mask_file_name gold_file Returns ------- """ if mask_file_name is not None: pytest.skip('Not Ready Yet for Masking') # load gold file gold_data_dict = parse_gold_file(gold_file) # Parse input file test_ws = HidraWorkspace('test_powder_pattern') test_project = HidraProjectFile(project_file_name) test_ws.load_hidra_project(test_project, load_raw_counts=True, load_reduced_diffraction=False) test_project.close() # Start reduction service pyrs_service = HB2BReductionManager() pyrs_service.init_session(session_name='test_powder', hidra_ws=test_ws) # Reduce raw counts pyrs_service.reduce_diffraction_data('test_powder', False, 1000, sub_run_list=None, mask=mask_file_name, mask_id=None, vanadium_counts=None, normalize_by_duration=False) # Get sub runs sub_runs = test_ws.get_sub_runs() for index, sub_run_i in enumerate(sub_runs): # Get gold data of pattern (i). gold_data_i = gold_data_dict[str(sub_run_i)] # Get powder data of pattern (i). pattern = pyrs_service.get_reduced_diffraction_data( 'test_powder', sub_run_i) # data_dict[str(sub_run_i)] = pattern # validate correct two-theta reduction np.testing.assert_allclose(pattern[0], gold_data_dict[str(sub_run_i)][0], rtol=1E-8) # remove NaN intensity arrays pattern[1][np.where(np.isnan(pattern[1]))] = 0. gold_data_i[1][np.where(np.isnan(gold_data_i[1]))] = 0. # validate correct intesnity reduction np.testing.assert_allclose(pattern[1], gold_data_i[1], rtol=1E-8, equal_nan=True)
def test_powder_pattern_engine(project_file_name, mask_file_name, gold_file): """Test the powder pattern calculator (service) with HB2B-specific reduction routine Parameters ---------- project_file_name mask_file_name gold_file Returns ------- """ if mask_file_name is not None: pytest.skip('Masking is not implemented yet') # Parse input file test_ws = HidraWorkspace('test_powder_pattern') test_project = HidraProjectFile(project_file_name) test_ws.load_hidra_project(test_project, load_raw_counts=True, load_reduced_diffraction=False) test_project.close() # Sub runs sub_runs = test_ws.get_sub_runs() # Import gold file gold_pattern = parse_gold_file(gold_file) data_dict = dict() # Start reduction service pyrs_service = HB2BReductionManager() pyrs_service.init_session(session_name='test_powder', hidra_ws=test_ws) # Reduce raw counts pyrs_service.reduce_diffraction_data('test_powder', False, 1000, sub_run_list=None, mask=mask_file_name, mask_id=None, vanadium_counts=None, normalize_by_duration=False) for index, sub_run_i in enumerate(sub_runs): # Get gold data of pattern (i). gold_data_i = gold_pattern[str(sub_run_i)] # Get powder data of pattern (i). pattern = pyrs_service.get_reduced_diffraction_data( 'test_powder', sub_run_i) # ensure NaN are removed gold_data_i[1][np.where(np.isnan(gold_data_i[1]))] = 0. pattern[1][np.where(np.isnan(pattern[1]))] = 0. # Verify np.testing.assert_allclose(pattern[1], gold_data_i[1], rtol=1E-8) data_dict[str(sub_run_i)] = pattern # if mask_file_name: # name = 'data/HB2B_1017_Mask_Gold.h5' # else: # name = 'data/HB2B_1017_NoMask_Gold.h5' # write_gold_file(name, data_dict) return