def save(self, projectfile): """ Save workspace to Hidra project file """ projectfile = os.path.abspath( projectfile) # confirm absolute path to make logs more readable checkdatatypes.check_file_name( projectfile, check_exist=False, check_writable=True, is_dir=False, description='Converted Hidra project file') # remove file if it already exists if os.path.exists(projectfile): self._log.information( 'Projectfile "{}" exists, removing previous version'.format( projectfile)) os.remove(projectfile) # save hydra_file = HidraProjectFile(projectfile, HidraProjectFileMode.OVERWRITE) # Set geometry hydra_file.write_instrument_geometry( HidraSetup(self._hidra_workspace.get_instrument_setup())) # save experimental data/detector counts self._hidra_workspace.save_experimental_data(hydra_file)
def test_detector_efficiency(self): """ Test methods to read and write detector efficiency Returns ------- None """ # Generate a HiDRA project file test_project_file = HidraProjectFile('test_efficient.hdf', HidraProjectFileMode.OVERWRITE) # Create a detector efficiency array mock_test_run_number = 12345 efficient_array = np.random.random_sample(1024**2) # Write to file test_project_file.write_efficiency_correction(mock_test_run_number, efficient_array) # Close file test_project_file.close() # Open file again verify_project_file = HidraProjectFile('test_efficient.hdf', HidraProjectFileMode.READONLY) # Read detector efficiency & compare verify_eff_array = verify_project_file.read_efficiency_correction() # Check assert np.allclose(efficient_array, verify_eff_array, rtol=1E-12) # Clean os.remove('test_efficient.hdf')
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_write_csv_from_project(project_file_name, csv_filename, expected_header, num_subruns, num_logs, startswith, endswith): """Test the method to export CSV file """ # load project file if not os.path.exists(project_file_name): pytest.skip('Project file {} does not exist'.format(project_file_name)) project = HidraProjectFile(project_file_name) # get information from the project file peak_tags = project.read_peak_tags() peak_collections = [ project.read_peak_parameters(tag) for tag in peak_tags ] # all tags sample_logs = project.read_sample_logs() assert sample_logs.subruns.size == num_subruns # just as a quick check # write out the csv file generator = SummaryGenerator(csv_filename) generator.setHeaderInformation({'project': '/some/place/random.h5' }) # only set one value generator.write_csv(sample_logs, peak_collections) # testing assert os.path.exists(csv_filename), '{} was not created'.format( csv_filename) # verify the file contents with open(csv_filename, 'r') as handle: # read in the file and remove whitespace contents = [line.strip() for line in handle.readlines()] # verify exact match on the header for exp, obs in zip(contents[:len(expected_header)], expected_header): assert exp == obs # verify the column headers assert contents[len(expected_header)].startswith(startswith) assert contents[len(expected_header)].endswith(endswith) assert len(contents) == len( expected_header) + 1 + num_subruns, 'Does not have full body' # verify that the number of columns is correct # columns are (subruns, seven logs, parameter values, uncertainties, d_spacing values, # strain values and uncertainties, chisq) for line in contents[len(expected_header) + 1:]: # skip past header and constant log assert len(line.split(',')) == 1 + num_logs + 7 * 2 + (2 * 2) + 1 # cleanup os.remove(csv_filename)
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 save_fit_result(self, out_file_name=''): """Save the fit result, including a copy of the rest of the file if it does not exist at the specified path. If out_file_name is empty or if it matches the parent's current file, this updates the file. Otherwise, the parent's file is copied to out_file_name and then the updated peak fit data is written to the copy. :param out_file_name: string absolute fill path for the place to save the file """ fit_result = self.parent.fit_result if fit_result is None: return if out_file_name is not None and self.parent._curr_file_name != out_file_name: copyfile(self.parent._curr_file_name, out_file_name) current_project_file = out_file_name else: current_project_file = self.parent._curr_file_name project_h5_file = HidraProjectFile(current_project_file, mode=HidraProjectFileMode.READWRITE) peakcollections = fit_result.peakcollections for peak in peakcollections: project_h5_file.write_peak_parameters(peak) project_h5_file.save(False) project_h5_file.close()
def load_hidra_project_file(self, filename, direction): try: source_project = HidraProjectFile( filename, mode=HidraProjectFileMode.READONLY) ws = HidraWorkspace(direction) ws.load_hidra_project(source_project, False, False) peaks = dict() for peak in source_project.read_peak_tags(): peaks[peak] = source_project.read_peak_parameters(peak) return ws, peaks except Exception as e: self.failureMsg.emit( f"Failed to load {filename}. Check that this is a Hidra Project File", str(e), traceback.format_exc()) return None, dict()
def test_read_log_units(self, tmpdir): project = HidraProjectFile(os.path.join(tmpdir, 'project_file.hdf'), HidraProjectFileMode.OVERWRITE) with pytest.raises(AssertionError) as exception_info: project.read_log_units('vx') assert 'Missing sample log: vx' in str(exception_info.value) project.append_experiment_log('vx', np.array([0.0, 0.1, 0.2])) assert project.read_log_units('vx') == '' project.append_experiment_log('vy', np.array([0.3, 0.4, 0.5]), units='mm') assert project.read_log_units('vy') == 'mm'
def test_reduce_with_calibration(): """Test reduction with calibration file Returns ------- """ nexus = '/HFIR/HB2B/IPTS-22731/nexus/HB2B_1017.nxs.h5' mask = '/HFIR/HB2B/shared/CALIBRATION/HB2B_MASK_Latest.xml' calibration = '/HFIR/HB2B/shared/CALIBRATION/HB2B_Latest.json' project = os.path.basename(nexus).split('.')[0] + '_WL.h5' project = os.path.join(os.getcwd(), project) try: # convert from NeXus to powder pattern _ = convertNeXusToProject(nexus, project, True, mask_file_name=mask) addPowderToProject(project, calibration_file=calibration) # load file verify_project = HidraProjectFile(project, HidraProjectFileMode.READONLY) verify_workspace = HidraWorkspace('verify calib') verify_workspace.load_hidra_project(verify_project, load_raw_counts=False, load_reduced_diffraction=True) wave_length = verify_workspace.get_wavelength(True, True) assert not np.isnan(wave_length) finally: if os.path.exists(project): os.remove(project)
def test_calibration_json(): """Test reduce data with calibration file (.json)""" # Get simulated test data project_file_name = 'tests/data/HB2B_000.h5' calib_file = 'tests/data/HB2B_CAL_Si333.json' # Import file calib_obj = calibration_file_io.read_calibration_json_file(calib_file) shift, shift_error, wave_length, wl_error, status = calib_obj # Verify result assert shift assert shift_error assert abs(wave_length - 1.4499332864) < 1E-8 assert wl_error assert status == 3 # Import project file project_file = HidraProjectFile(project_file_name, 'r') # Reduce test_workspace = workspaces.HidraWorkspace('test calibration') test_workspace.load_hidra_project(project_file, load_raw_counts=True, load_reduced_diffraction=False) # Test set and get wave length test_workspace.set_wavelength(wave_length, True) wave_length_i = test_workspace.get_wavelength(True, True) assert wave_length_i == wave_length
def test_rw_raw(): """Test read a project to workspace and write in the scope of raw data Returns ------- """ raw_project_name = os.path.join(os.getcwd(), 'data/HZB_Raw_Project.h5') # Read to workspace source_project = HidraProjectFile(raw_project_name, 'r') # To the workspace source_workspace = workspaces.HidraWorkspace('Source HZB') source_workspace.load_hidra_project(source_project, load_raw_counts=True, load_reduced_diffraction=False) # Export target_project = HidraProjectFile('HZB_HiDra_Test.h5', 'w') # Experiment data source_workspace.save_experimental_data(target_project, sub_runs=range(1, 41)) # Instrument detector_setup = source_workspace.get_instrument_setup() instrument_setup = HidraSetup(detector_setup=detector_setup) target_project.write_instrument_geometry(instrument_setup) # Save target_project.save(True) return
def save_peak_fit_result(self, project_name, hidra_file_name, peak_tag, overwrite=True): """ Save the result from peak fitting to HiDRA project file Parameters ---------- project_name: str name of peak fitting session hidra_file_name: String project file to export peaks fitting result to peak_tag : str peak tag overwrite: bool Flag to append to an existing file or overwrite it Returns ------- """ if project_name is None: optimizer = self._peak_fit_engine else: optimizer = self._peak_fitting_dict[project_name] # Determine the file IO mode if os.path.exists(hidra_file_name) and overwrite is False: # file exists and user does not want overwrite: READWRITE mode file_mode = HidraProjectFileMode.READWRITE else: # starting as a new file file_mode = HidraProjectFileMode.OVERWRITE # Create HiDRA project file hidra_project_file = HidraProjectFile(hidra_file_name, file_mode) # Export peaks optimizer.export_to_hydra_project(hidra_project_file, peak_tag) # Close hidra_project_file.close() return
def save_fit_result(self, out_file_name=''): fit_result = self.parent.fit_result if fit_result is None: return current_project_file = self.parent._curr_file_name project_h5_file = HidraProjectFile(current_project_file, mode=HidraProjectFileMode.READWRITE) peakcollections = fit_result.peakcollections for peak in peakcollections: project_h5_file.write_peak_fit_result(peak) project_h5_file.save(False) project_h5_file.close()
def test_leastsq(): """Main test for the script Returns ------- """ # Set up # reduction engine project_file_name = 'tests/data/HB2B_000.h5' engine = HidraProjectFile(project_file_name, mode=HidraProjectFileMode.READONLY) # Convert HidraProjectFile into HidraWorkspace engine_ws = HidraWorkspace('test') engine_ws._load_sample_logs(engine) engine_ws._load_raw_counts(engine) t_start = time.time() # Initalize calibration calibrator = peakfit_calibration.PeakFitCalibration( powder_engine=engine_ws, powder_lines=dSpace) calibrator.UseLSQ = True calibrator.calibrate_wave_length() calibrator._caliberr[6] = 1e-4 calibrator._caliberr[0] = 1e-4 calibrator._calibstatus = 3 # write out if os.path.exists('HB2B_CAL_Test.json'): os.remove('HB2B_CAL_Test.json') file_name = os.path.join(os.getcwd(), 'HB2B_CAL_Test.json') calibrator.write_calibration(file_name) t_stop = time.time() print('Total Time: {}'.format(t_stop - t_start)) # Compare output file with gold file for test if are_equivalent_jsons('tests/data/HB2B_CAL_Si333.json', file_name, atol=5E-3): # Same: remove file generated in test os.remove(file_name) else: print_out_json_diff('tests/data/HB2B_CAL_Si333.json', 'HB2B_CAL_Test.json') assert False, 'Test output {} is different from gold file {}'.format( file_name, 'tests/data/HB2B_CAL_Si333.json') return
def load_hidra_project(self, project_file_name, load_calibrated_instrument, load_detectors_counts, load_reduced_diffraction): """ Load hidra project file and then CLOSE! :param project_file_name: :param load_calibrated_instrument: :param load_detectors_counts: Flag to load detector counts :param load_reduced_diffraction: Flag to reduced diffraction data :return: HidraWorkspace instance """ # check inputs checkdatatypes.check_file_name(project_file_name, True, False, False, 'Project file to load') # Check if self._curr_workspace is None: raise RuntimeError( 'Call init_session to create a ReductionWorkspace') # PyRS HDF5 # Check permission of file to determine the RW mode of HidraProject file if os.access(project_file_name, os.W_OK): # Read/Write: Append mode file_mode = HidraProjectFileMode.READWRITE else: # Read only file_mode = HidraProjectFileMode.READONLY project_h5_file = HidraProjectFile(project_file_name, mode=file_mode) # Load self._curr_workspace.load_hidra_project( project_h5_file, load_raw_counts=load_detectors_counts, load_reduced_diffraction=load_reduced_diffraction) # Close project_h5_file.close() return self._curr_workspace
def main(): """ Main method to convert diffraction data in the old HDF5 format :return: """ # Load source data in 'old' format source_h5 = 'tests/testdata/16-1_TD.cor_Log.h5' reader = rs_scan_io.DiffractionDataFile() diff_data_dict, sample_logs = reader.load_rs_file(source_h5) # Create a Hidra project target_project_file_name = 'tests/testdata/Hydra_16-1_cor_log.h5' target_file = HidraProjectFile(target_project_file_name, 'w') # Create sub runs target_file.write_sub_runs(sorted(diff_data_dict.keys())) # Add (reduced) diffraction data two_theta_vector = None diff_data_matrix = None # construct the matrix of intensities for sub_run_index, sub_run_number in enumerate( sorted(diff_data_dict.keys())): two_theta_vector_i, intensity_vector_i = diff_data_dict[sub_run_index] # create data set if two_theta_vector is None: two_theta_vector = two_theta_vector_i diff_data_matrix = numpy.ndarray(shape=(len( diff_data_dict.keys()), intensity_vector_i.shape[0]), dtype='float') # END-IF # set vector diff_data_matrix[sub_run_index] = intensity_vector_i # END-FOR # Add data target_file.write_reduced_diffraction_data_set(two_theta_vector, {None: diff_data_matrix}) # Save target_file.save(verbose=True) return
def save_reduced_diffraction(self, session_name, output_name): """ Save the reduced diffraction data to file :param session_name: :param output_name: :return: """ checkdatatypes.check_file_name(output_name, False, True, False, 'Output reduced file') workspace = self._session_dict[session_name] # Open if os.path.exists(output_name): io_mode = HidraProjectFileMode.READWRITE else: io_mode = HidraProjectFileMode.OVERWRITE project_file = HidraProjectFile(output_name, io_mode) # Save workspace.save_reduced_diffraction_data(project_file) # Close project_file.save()
def save_diffraction_data(self, output_file_name=None, append_mode=False): """Save reduced diffraction data to Hidra project file Parameters ---------- output_file_name: None or str if None, then append result to the input file append_mode : bool flag to force project file in appending/READWRITE mode Returns ------- """ # Determine output file name and writing mode if output_file_name is None or output_file_name == self._hydra_file_name: file_name = self._hydra_file_name mode = HidraProjectFileMode.READWRITE elif append_mode: # Append to existing file file_name = output_file_name mode = HidraProjectFileMode.READWRITE else: file_name = output_file_name mode = HidraProjectFileMode.OVERWRITE # Sanity check if file_name is None: raise RuntimeError( 'Output file name is not set property. There is no default file name' 'or user specified output file name.') # Generate project file instance out_file = HidraProjectFile(file_name, mode) # If it is a new file, the sample logs and other information shall be exported too if mode == HidraProjectFileMode.OVERWRITE: self._hydra_ws.save_experimental_data(out_file, sub_runs=self._sub_runs, ignore_raw_counts=True) # Calibrated wave length shall be written self._hydra_ws.save_wavelength(out_file) # Write & close self._hydra_ws.save_reduced_diffraction_data(out_file, sub_runs=self._sub_runs)
def test_append_experiment_log(self, tmpdir): project = HidraProjectFile(os.path.join(tmpdir, 'project_file.hdf'), HidraProjectFileMode.OVERWRITE) group = project._project_h5[HidraConstants.RAW_DATA][ HidraConstants.SAMPLE_LOGS] project.append_experiment_log('vx', np.array([0.0, 0.1, 0.2])) with pytest.raises(KeyError): group['vx'].attrs['units'] project.append_experiment_log('vy', np.array([0.3, 0.4, 0.5]), units='mm') assert group['vy'].attrs['units'] == 'mm'
def contain_strain_stress_main(): """ Main method to test class and methods to calculate :return: """ # Source data dir_file = dict() dir_file[1] = 'tests/temp/16-1_LD.cor_Log.gaussian.hdf5' dir_file[2] = 'tests/temp/16-1_ND.cor_Log.gaussian.hdf5' dir_file[3] = 'tests/temp/16-1_TD.cor_Log.gaussian.hdf5' for dir_i in [1, 2, 3]: if not os.path.exists(dir_file[dir_i]): print( '[ERROR] File {} does not exist. Current working directory: {}' ''.format(dir_file[dir_i], os.getcwd())) sys.exit(-1) # END-FOR # Start a new project hidra_project_file = HidraProjectFile( 'tests/testdata/strain_stress_test.hdf5', HidraProjectFileMode.READONLY) print(hidra_project_file) # Get parameters of sample logs # from pyrs.utilities import rs_scan_io # TODO - continue to develop in a proper PR! # for dir_i in [1, 2, 3]: # diff_data_dict, sample_log_list = rs_scan_io.load_rs_file(dir_file[1]) # # TODO - FUTURE - Continue from here # # # set up the combo box for 3 directions # sample_logs_list = self._core.strain_stress_calculator.get_sample_logs_names(direction, to_set=False) # # self._setup_sample_logs_combo_box(sample_logs_list, direction) return
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
def test_mask(): """Test methods to read and write mask file Returns ------- None """ # Generate a HiDRA project file test_project_file = HidraProjectFile('test_mask.hdf', HidraProjectFileMode.OVERWRITE) # Create a detector mask pixel_mask = np.zeros(shape=(1024**2, ), dtype='int') pixel_mask += 1 pixel_mask[123:345] = 0 pixel_mask[21000:21019] = 0 # Create a solid angle mask solid_mask = np.array([-20, -15, -10, -5, 5, 10, 15, 20]) # Write detector mask: default and etc test_project_file.write_mask_detector_array(None, pixel_mask) test_project_file.write_mask_detector_array('test', pixel_mask) # Write solid angle mask test_project_file.write_mask_solid_angle('test', solid_mask) # Close file test_project_file.save(True) # Open file again verify_project_file = HidraProjectFile('test_mask.hdf', HidraProjectFileMode.READONLY) # Read detector default mask default_pixel_mask = verify_project_file.read_default_masks() assert np.allclose(pixel_mask, default_pixel_mask, 1.E-12) # Read detector mask & compare verify_pixel_mask = verify_project_file.read_mask_detector_array('test') assert np.allclose(pixel_mask, verify_pixel_mask, 1.E-12) # Test to read all user detector mask user_mask_dict = dict() verify_project_file.read_user_masks(user_mask_dict) assert list(user_mask_dict.keys())[0] == 'test' # Read solid angle mask & compare verify_solid_mask = verify_project_file.read_mask_solid_angle('test') assert np.allclose(solid_mask, verify_solid_mask, 1.E-2) # Clean os.remove('test_mask.hdf') return
def test_strain_io(): """Test PeakCollection writing and reading with *D reference* Returns ------- """ # Generate a unique test file now = datetime.datetime.now() test_file_name = 'test_strain_io_{}.h5'.format(now.toordinal()) test_ref_d = 1.23454321 test_ref_d2 = np.array([1.23, 1.24, 1.25]) peak_tag = 'Fake Peak D' peak_tag_2 = 'Fake Peak D Diff' # Generate a HiDRA project file test_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.OVERWRITE) # Create a ND array for output parameters param_names = PeakShape.PSEUDOVOIGT.native_parameters + BackgroundFunction.LINEAR.native_parameters data_type = list() for param_name in param_names: data_type.append((param_name, np.float32)) test_error_array = np.zeros(3, dtype=data_type) test_params_array = np.zeros(3, dtype=data_type) for i in range(3): # sub run for j, par_name in enumerate(param_names): test_params_array[par_name][i] = 2**i + 0.1 * 3**j test_error_array[par_name][i] = np.sqrt( abs(test_params_array[par_name][i])) # END-FOR chi2_array = np.array([0.323, 0.423, 0.523]) # Add test data to output peaks = PeakCollection(peak_tag, PeakShape.PSEUDOVOIGT, BackgroundFunction.LINEAR) peaks.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array, test_error_array, chi2_array) peaks.set_d_reference(test_ref_d) # Add 2nd peak peaks2 = PeakCollection(peak_tag_2, PeakShape.PSEUDOVOIGT, BackgroundFunction.LINEAR) peaks2.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array, test_error_array, chi2_array) peaks2.set_d_reference(test_ref_d2) # Write test_project_file.write_peak_parameters(peaks) test_project_file.write_peak_parameters(peaks2) # Save test_project_file.save(verbose=False) # Verify assert os.path.exists(test_file_name), 'Test project file for peak fitting result {} cannot be found.' \ ''.format(test_file_name) # import verify_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.READONLY) # check tags peak_tags = verify_project_file.read_peak_tags() assert peak_tag in peak_tags and peak_tag_2 in peak_tags assert len(peak_tags) == 2 # Get d-reference of peak 1 to check peak_info = verify_project_file.read_peak_parameters(peak_tag) verify_d_ref = peak_info.get_d_reference() gold_ref_d = np.array([test_ref_d] * 3) np.testing.assert_allclose(verify_d_ref, gold_ref_d) # Get d-reference of peak 2 to check peak_info2 = verify_project_file.read_peak_parameters(peak_tag_2) verify_d_ref_2 = peak_info2.get_d_reference() np.testing.assert_allclose(verify_d_ref_2, test_ref_d2) # Clean os.remove(test_file_name) return
def test_peak_fitting_result_io(): """Test peak fitting result's writing and reading Returns ------- """ # Generate a unique test file now = datetime.datetime.now() test_file_name = 'test_peak_io_{}.hdf'.format(now.toordinal()) # Generate a HiDRA project file test_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.OVERWRITE) # Create a ND array for output parameters param_names = PeakShape.PSEUDOVOIGT.native_parameters + BackgroundFunction.LINEAR.native_parameters data_type = list() for param_name in param_names: data_type.append((param_name, np.float32)) test_error_array = np.zeros(3, dtype=data_type) test_params_array = np.zeros(3, dtype=data_type) for i in range(3): # sub run for j, par_name in enumerate(param_names): test_params_array[par_name][i] = 2**i + 0.1 * 3**j test_error_array[par_name][i] = np.sqrt( abs(test_params_array[par_name][i])) # END-FOR chi2_array = np.array([0.323, 0.423, 0.523]) # Add test data to output peaks = PeakCollection('test fake', PeakShape.PSEUDOVOIGT, BackgroundFunction.LINEAR) peaks.set_peak_fitting_values(np.array([1, 2, 3]), test_params_array, test_error_array, chi2_array) test_project_file.write_peak_parameters(peaks) test_project_file.save(False) # Check assert os.path.exists(test_file_name), 'Test project file for peak fitting result {} cannot be found.' \ ''.format(test_file_name) print('[INFO] Peak parameter test project file: {}'.format(test_file_name)) # Import verify_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.READONLY) # get the tags peak_tags = verify_project_file.read_peak_tags() assert 'test fake' in peak_tags assert len(peak_tags) == 1 # get the parameter of certain peak_info = verify_project_file.read_peak_parameters('test fake') # peak profile assert peak_info.peak_profile == str(PeakShape.PSEUDOVOIGT) assert peak_info.background_type == str(BackgroundFunction.LINEAR) # sub runs assert np.allclose(peak_info.sub_runs, np.array([1, 2, 3])) # parameter values # print('DEBUG:\n Expected: {}\n Found: {}'.format(test_params_array, peak_info[3])) peak_values, peak_errors = peak_info.get_native_params() assert_allclose_structured_numpy_arrays(test_params_array, peak_values) # np.testing.assert_allclose(peak_info[3], test_params_array, atol=1E-12) # parameter values # assert np.allclose(peak_info[4], test_error_array, 1E-12) assert_allclose_structured_numpy_arrays(test_error_array, peak_errors) # Clean os.remove(test_file_name) return
def test_wave_length_rw(): """Test writing and reading for wave length Returns ------- """ # Set up for testing test_file_name = 'test_wave_length.h5' # Create a detector mask gold_wave_length = 1.23456 # Generate a HiDRA project file test_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.OVERWRITE) test_project_file.save(True) test_project_file.close() # Open file verify_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.READONLY) # Read wave length (not exist) wave_length_test = verify_project_file.read_wavelengths() assert np.isnan(wave_length_test), 'No wave length read out' # Close verify_project_file.close() # Generate a HiDRA project file test_project_file = HidraProjectFile(test_file_name, HidraProjectFileMode.READWRITE) # Write wave length test_project_file.write_wavelength(gold_wave_length) # Save and close test_project_file.save(True) test_project_file.close() # Open file again to verify verify_project_file2 = HidraProjectFile(test_file_name, HidraProjectFileMode.READONLY) # Read wave length (not exist) wave_length_test = verify_project_file2.read_wavelengths() assert wave_length_test == gold_wave_length # Clean os.remove(test_file_name)
def project_HB2B_938(test_data_dir): r"""A hidra project containing, among other things, units in the logs""" return HidraProjectFile(os.path.join(test_data_dir, 'HB2B_938_v2.h5'), HidraProjectFileMode.READONLY)
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)