def convertNeXusToProject(nexusfile, projectfile, skippable, mask_file_name=None): ''' Parameters ========== nexusfile: str Path to Nexus file to reduce projectfile: str or None Path to the project file to save. If this is :py:obj:`None`, then the project file is not created skippable: bool Whether missing the nexus file skips the test or fails it mask_file_name: str or None Name of the masking file to use :return: ''' if skippable: checkFileExists(nexusfile, feedback='skip') else: checkFileExists(nexusfile, feedback='assert') # remove the project file if it currently exists if projectfile and os.path.exists(projectfile): os.remove(projectfile) converter = NeXusConvertingApp(nexusfile, mask_file_name=mask_file_name) hidra_ws = converter.convert(use_mantid=False) if projectfile is not None: converter.save(projectfile) # tests for the created file assert os.path.exists(projectfile), 'Project file {} does not exist'.format(projectfile) return hidra_ws
def test_log_time_average(): """Test the log time average calculation""" SUBRUNS_EXP = np.array([1, 2, 3]) processor = NeXusConvertingApp(FILE_1017) sub_run_times, sub_run_numbers = processor._splitter.times, processor._splitter.subruns # verify splitting information # 2theta is the motor that determines the first start time exp_times = np.array(['2019-11-10T16:31:02.645235328-0500', '2019-11-10T16:41:02.771317813-0500', # scan_index=1 '2019-11-10T16:41:14.238680196-0500', '2019-11-10T17:11:14.249705287-0500', # scan_index=2 '2019-11-10T17:11:33.208056929-0500', '2019-11-10T17:31:33.218615767-0500'], # scan_index=3 dtype='datetime64[ns]') np.testing.assert_equal(sub_run_numbers, SUBRUNS_EXP, err_msg='subrun numbers') np.testing.assert_equal(sub_run_times, exp_times, err_msg='subrun filtering') # previous calculations exp_durations = np.array([600., 1800., 1200.]) np.testing.assert_almost_equal(processor._splitter.durations, exp_durations, decimal=0) # split the sample logs sample_logs = processor.split_sample_logs(SUBRUNS_EXP) # verify two of the properties np.testing.assert_allclose(sample_logs['2theta'], [69.99525, 80., 97.50225])
def test_texture_reduction(nexusfile, 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 """ if not os.path.exists('/HFIR/HB2B/shared'): pytest.skip('Unable to access HB2B archive') CALIBRATION_FILE = "data/HB2B_calib_latest.json" VANADIUM_FILE = "/HFIR/HB2B/IPTS-22731/nexus/HB2B_1115.nxs.h5" # load gold file gold_data_dict = parse_gold_file(gold_file) # Parse input file converter = NeXusConvertingApp(nexusfile, mask_file_name) hidra_ws = converter.convert() # Start reduction service reducer = ReductionApp() reducer.load_hidra_workspace(hidra_ws) # Reduce raw counts reducer.reduce_data(instrument_file=None, calibration_file=CALIBRATION_FILE, mask=None, sub_runs=[], van_file=VANADIUM_FILE, eta_step=3.0) for sub_run_i in list(gold_data_dict.keys()): # Get gold data of pattern (i). gold_data_i = gold_data_dict[sub_run_i] # Get powder data of pattern (i). pattern = reducer.get_diffraction_data(1, sub_run_i) # validate correct two-theta reduction np.testing.assert_allclose(pattern[0], gold_data_dict[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 load_vanadium(self, van_project_file): """Load vanadium from HiDRA project file Parameters ---------- van_project_file : str vanadium HiDRA project file or NeXus file Returns ------- ~numpy.narray, float 1D array as vanadium counts and duration of vanadium run (second) """ checkdatatypes.check_file_name(van_project_file, True, False, False, 'Vanadium project/NeXus file') if van_project_file.endswith('.nxs.h5'): # Input is nexus file # reduce with PyRS/Python converter = NeXusConvertingApp(van_project_file, mask_file_name=None) self._van_ws = converter.convert(use_mantid=False) else: # Input is HiDRA project file self._van_ws = workspaces.HidraWorkspace(name=van_project_file) # PyRS HDF5 project_h5_file = HidraProjectFile( van_project_file, mode=HidraProjectFileMode.READONLY) # Load self._van_ws.load_hidra_project(project_h5_file, load_raw_counts=True, load_reduced_diffraction=False) # Close project file project_h5_file.close() # Process the vanadium counts sub_runs = self._van_ws.get_sub_runs() assert len( sub_runs ) == 1, 'There shall be more than 1 sub run in vanadium project file' # get vanadium data van_array = self._van_ws.get_detector_counts(sub_runs[0]).astype( np.float64) # get vanadium run duration van_duration = self._van_ws.get_sample_log_value( HidraConstants.SUB_RUN_DURATION, sub_runs[0]) return van_array, van_duration
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 _nexus_to_subscans(nexusfile, projectfile, mask_file_name, save_project_file): """Split raw data from NeXus file to sub runs/scans Parameters ---------- nexusfile : str HB2B event NeXus file's name projectfile : str Target HB2B HiDRA project file's name mask_file_name : str Mask file name; None for no mask save_project_file : str Project file to save to. None for not being saved Returns ------- pyrs.core.workspaces.HidraWorkspace Hidra workspace containing the raw counts and sample logs """ if os.path.exists(projectfile): logger.information( 'Removing existing projectfile {}'.format(projectfile)) os.remove(projectfile) logger.notice('Creating subscans from {} into project file {}'.format( nexusfile, projectfile)) converter = NeXusConvertingApp(nexusfile, mask_file_name) hydra_ws = converter.convert(use_mantid=False) # save project file as an option if save_project_file: converter.save(projectfile) return hydra_ws
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
def converter_HB2B_938(test_data_dir): r"""File with only one subrun""" return NeXusConvertingApp(os.path.join(test_data_dir, 'HB2B_938.nxs.h5'))
def _load_nexus_data(ipts, nexus_run, mask_file): nexus_file = '/HFIR/HB2B/IPTS-{}/nexus/HB2B_{}.nxs.h5'.format(ipts, nexus_run) converter = NeXusConvertingApp(nexus_file, mask_file) hidra_ws = converter.convert() return hidra_ws