def test_apply_vanadium(project_file, van_project_file, target_project_file): """Test applying vanadium to the raw data in project file Parameters ---------- project_file : str raw HiDRA project file to convert to 2theta pattern van_project_file : str raw HiDra vanadium file target_project_file : str target HiDRA Returns ------- """ # Check files' existence checkFileExists(project_file, feedback='assert') checkFileExists(van_project_file, feedback='assert') # Load data # extract the powder patterns and add them to the project file reducer = ReductionApp() # instrument_file, calibration_file, mask, sub_runs reducer.load_project_file(project_file) reducer.reduce_data(sub_runs=None, instrument_file=None, calibration_file=None, mask=None, van_file=van_project_file, num_bins=950) reducer.save_diffraction_data(target_project_file)
def test_apply_mantid_mask(): """Test auto reduction script with Mantid mask file applied Returns ------- """ # Specify NeXus nexus_file = 'data/HB2B_938.nxs.h5' # Convert the NeXus to file to a project without mask and convert to 2theta diffraction pattern no_mask_project_file = 'HB2B_938_no_mask.h5' if os.path.exists(no_mask_project_file): os.remove(no_mask_project_file) # Convert to NeXust no_mask_hidra_ws = convertNeXusToProject(nexus_file, no_mask_project_file, skippable=False, mask_file_name=None) mask_array = no_mask_hidra_ws.get_detector_mask(is_default=True) assert mask_array is None, 'There shall not be any mask' # Convert the nexus file to a project file and do the "simple" checks no_mask_reducer = ReductionApp() no_mask_reducer.load_project_file(no_mask_project_file) no_mask_reducer.reduce_data(sub_runs=None, instrument_file=None, calibration_file=None, mask=None, van_file=None, num_bins=950) no_mask_reducer.save_diffraction_data(no_mask_project_file) # Convert the NeXus to file to a project with mask and convert to 2theta diffraction pattern project_file = 'HB2B_938_mask.h5' if os.path.exists(project_file): os.remove(project_file) # Convert masked_hidra_ws = convertNeXusToProject(nexus_file, project_file, skippable=False, mask_file_name='data/HB2B_Mask_12-18-19.xml') mask_array = masked_hidra_ws.get_detector_mask(True) # check on Mask: num_masked_pixels = (135602,) assert np.where(mask_array == 0)[0].shape[0] == 135602, 'Mask shall have 135602 pixels masked but not {}' \ ''.format(np.where(mask_array == 0)[0].shape[0]) reducer = ReductionApp() reducer.load_project_file(project_file) # convert to diffraction pattern with mask reducer.reduce_data(sub_runs=None, instrument_file=None, calibration_file=None, mask=mask_array, van_file=None, num_bins=950) reducer.save_diffraction_data(project_file) # Compare range of 2theta no_mask_data_set = no_mask_reducer.get_diffraction_data(sub_run=1) masked_data_set = reducer.get_diffraction_data(sub_run=1) print('[DEBUG...] No mask 2theta range: {}, {}'.format(no_mask_data_set[0].min(), no_mask_data_set[0].max())) print('[DEBUG...] Masked 2theta range: {}, {}'.format(masked_data_set[0].min(), masked_data_set[0].max())) # verify the masked reduced data shall have smaller or at least equal range of 2theta assert no_mask_data_set[0].min() <= masked_data_set[0].min() assert no_mask_data_set[0].max() >= masked_data_set[0].max()
def addPowderToProject(projectfile, calibration_file=None): checkFileExists(projectfile, feedback='assert') # extract the powder patterns and add them to the project file reducer = ReductionApp() # TODO should add versions for testing arguments: instrument_file, calibration_file, mask, sub_runs reducer.load_project_file(projectfile) reducer.reduce_data(sub_runs=None, instrument_file=None, calibration_file=calibration_file, mask=None) reducer.save_diffraction_data(projectfile) # tests for the created file assert os.path.exists(projectfile)
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 _create_powder_patterns(hidra_workspace, instrument, calibration, mask, subruns, project_file_name, append_mode): logger.notice( 'Adding powder patterns to Hidra Workspace{}'.format(hidra_workspace)) reducer = ReductionApp(bool(options.engine == 'mantid')) # reducer.load_project_file(projectfile) # load HidraWorkspace reducer.load_hidra_workspace(hidra_workspace) reducer.reduce_data(instrument_file=instrument, calibration_file=calibration, mask=mask, sub_runs=subruns, van_file=None) reducer.save_diffraction_data(project_file_name, append_mode)
def reduce_hidra_workflow(nexus, output_dir, progressbar, instrument=None, calibration=None, mask=None, vanadium_file=None, project_file_name=None): """Workflow of algorithms to reduce HB2B NeXus file to powder patterns Parameters ---------- nexus output_dir progressbar instrument calibration : str calibration file name mask : str or None Mask file (so far, only Mantid XML file) vanadium_file : str or None Vanadium file (reduced project file with vanadium counts at sub run 1) project_file_name : str or None if str, then the output file name won't use the default Returns ------- pyrs.core.workspaces.HidraWorkspace HiDRA workspace """ # Init logger logger = Logger('reduce_HB2B') # Create project file (name) for default if project_file_name is None: project_file_name = os.path.basename(nexus).split('.')[0] + '.h5' project_file_name = os.path.join(output_dir, project_file_name) # Remove previous existing file if os.path.exists(project_file_name): # overwrite existing file if os.access(project_file_name, os.W_OK): # log information logger.information('Will overwrite existing projectfile {}'.format( project_file_name)) else: # no permission raise RuntimeError( 'User does not have permission to overwrite existing HiDRA project file {}' ''.format(project_file_name)) else: # file does not exist so far base_dir = os.path.dirname(project_file_name) if not (os.path.exists(base_dir) and os.access(base_dir, os.W_OK)): raise RuntimeError( 'User specified HiDRA project file path {} either does not exist or ' 'user does not have write access.'.format(base_dir)) # END-IF-ELSE # Set progress bar if progressbar: progressbar.setVisible(True) progressbar.setValue(0.) # process the data converter = NeXusConvertingApp(nexus, mask) hidra_ws = converter.convert(use_mantid=False) # Update if progressbar: progressbar.setValue(50.) # add powder patterns # Calculate powder pattern logger.notice( 'Adding powder patterns to Hidra Workspace {}'.format(hidra_ws)) # Initialize a reducer reducer = ReductionApp() # add workspace to reducer reducer.load_hidra_workspace(hidra_ws) # reduce reducer.reduce_data(instrument_file=instrument, calibration_file=calibration, mask=None, van_file=vanadium_file, sub_runs=list(hidra_ws.get_sub_runs())) if progressbar: progressbar.setVisible(True) progressbar.setValue(95.) # Save reducer.save_diffraction_data(project_file_name) if progressbar: progressbar.setVisible(True) progressbar.setValue(100.) return hidra_ws