def test_fetch_correction_workspaces_when_cached(self, ads, wspexist, van_correction): van_correction.return_value = ("integ", "processed") wspexist.return_value = True vanadium_corrections.fetch_correction_workspaces("something/somewhere/ENGINX123.nxs", "ENGINX") self.assertEqual(0, van_correction.call_count) self.assertEqual(2, ads.retrieve.call_count)
def load_existing_gsas_parameters(self, file_path): if not path.exists(file_path): logger.warning("Could not open GSAS calibration file: ", file_path) return try: instrument, van_no, sample_no, params_table = self.get_info_from_file(file_path) self.update_calibration_params_table(params_table) except RuntimeError: logger.error("Invalid file selected: ", file_path) return vanadium_corrections.fetch_correction_workspaces(instrument+van_no, instrument) return instrument, van_no, sample_no
def test_fetch_correction_workspaces_when_not_cached( self, gen_paths, load_alg, van_correction, save): gen_paths.return_value = ( path.join(self.directory_name, "123" + vanadium_corrections.SAVED_FILE_INTEG_SUFFIX), path.join(self.directory_name, "123" + vanadium_corrections.SAVED_FILE_CURVE_SUFFIX)) van_correction.return_value = ("integ", "curves") vanadium_corrections.fetch_correction_workspaces( "something/somewhere/ENGINX123.nxs", "ENGINX") self.assertEqual(0, load_alg.call_count) self.assertEqual(1, van_correction.call_count) self.assertEqual(1, save.call_count)
def test_fetch_correction_workspaces_when_cached(self, gen_paths, load_alg, van_correction, save): temp_integ = tempfile.NamedTemporaryFile(dir=self.directory_name) temp_curve = tempfile.NamedTemporaryFile(dir=self.directory_name) gen_paths.return_value = (path.join(self.directory_name, temp_integ.name), path.join(self.directory_name, temp_curve.name)) van_correction.return_value = ("integ", "curves") vanadium_corrections.fetch_correction_workspaces( "something/somewhere/ENGINX123.nxs", "ENGINX") self.assertEqual(2, load_alg.call_count) self.assertEqual(0, van_correction.call_count) self.assertEqual(0, save.call_count)
def create_new_calibration(self, vanadium_path, sample_path, plot_output, instrument, rb_num=None, bank=None, spectrum_numbers=None): """ Create a new calibration from a vanadium run and sample run :param vanadium_path: Path to vanadium data file. :param sample_path: Path to sample (CeO2) data file :param plot_output: Whether the output should be plotted. :param instrument: The instrument the data relates to. :param rb_num: The RB number for file creation. :param bank: Optional parameter to crop by bank :param spectrum_numbers: Optional parameter to crop using spectrum numbers. """ van_integration, van_curves = vanadium_corrections.fetch_correction_workspaces( vanadium_path, instrument, rb_num=rb_num) sample_workspace = path_handling.load_workspace(sample_path) full_calib_path = get_setting(path_handling.INTERFACES_SETTINGS_GROUP, path_handling.ENGINEERING_PREFIX, "full_calibration") if full_calib_path is not None and path.exists(full_calib_path): full_calib = LoadAscii(full_calib_path, OutputWorkspace="det_pos", Separator="Tab") output = self.run_calibration(sample_workspace, van_integration, van_curves, bank, spectrum_numbers, full_calib_ws=full_calib) else: output = self.run_calibration(sample_workspace, van_integration, van_curves, bank, spectrum_numbers) if plot_output: self._plot_vanadium_curves() for i in range(len(output)): if spectrum_numbers: bank_name = "cropped" elif bank is None: bank_name = str(i + 1) else: bank_name = bank difa = output[i].DIFA difc = output[i].DIFC tzero = output[i].TZERO self._generate_tof_fit_workspace(difa, difc, tzero, bank_name) if bank is None and spectrum_numbers is None: self._plot_tof_fit() elif spectrum_numbers is None: self._plot_tof_fit_single_bank_or_custom(bank) else: self._plot_tof_fit_single_bank_or_custom("cropped") difa = [i.DIFC for i in output] difc = [i.DIFC for i in output] tzero = [i.TZERO for i in output] params_table = [] for i in range(len(difc)): params_table.append([i, difc[i], difa[i], tzero[i]]) self.update_calibration_params_table(params_table) calib_dir = path.join(path_handling.get_output_path(), "Calibration", "") self.create_output_files(calib_dir, difa, difc, tzero, sample_path, vanadium_path, instrument, bank, spectrum_numbers) if rb_num: user_calib_dir = path.join(path_handling.get_output_path(), "User", rb_num, "Calibration", "") self.create_output_files(user_calib_dir, difa, difc, tzero, sample_path, vanadium_path, instrument, bank, spectrum_numbers)
def focus_run(self, sample_paths: list, vanadium_path: str, plot_output: bool, instrument: str, rb_num: str, regions_dict: dict) -> None: """ Focus some data using the current calibration. :param sample_paths: The paths to the data to be focused. :param vanadium_path: Path to the vanadium file from the current calibration :param plot_output: True if the output should be plotted. :param instrument: The instrument that the data came from. :param rb_num: Number to signify the user who is running this focus :param regions_dict: dict region name -> grp_ws_name, defining region(s) of interest to focus over """ full_calib_path = get_setting( output_settings.INTERFACES_SETTINGS_GROUP, output_settings.ENGINEERING_PREFIX, "full_calibration") if not Ads.doesExist("full_inst_calib"): try: full_calib_workspace = Load(full_calib_path, OutputWorkspace="full_inst_calib") except RuntimeError: logger.error( "Error loading Full instrument calibration - this is set in the interface settings." ) return else: full_calib_workspace = Ads.retrieve("full_inst_calib") van_integration_ws, van_processed_inst_ws = vanadium_corrections.fetch_correction_workspaces( vanadium_path, instrument) # check correct region calibration(s) and grouping workspace(s) exists inst_ws = path_handling.load_workspace(sample_paths[0]) for region in regions_dict: calib_exists = self._check_region_calib_ws_exists(region) grouping_exists = self._check_region_grouping_ws_exists( regions_dict[region], inst_ws) if not (calib_exists and grouping_exists): return # loop over samples provided, focus each over region(s) specified in regions_dict output_workspaces = [] # List of collated workspaces to plot. self._last_focused_files = [] van_run_no = path_handling.get_run_number_from_path( vanadium_path, instrument) for sample_path in sample_paths: sample_workspace = path_handling.load_workspace(sample_path) run_no = path_handling.get_run_number_from_path( sample_path, instrument) # perform prefocus operations on whole instrument workspace prefocus_success = self._whole_inst_prefocus( sample_workspace, van_integration_ws, full_calib_workspace) if not prefocus_success: continue sample_plots = [ ] # if both banks focused, pass list with both so plotted on same figure for region, grouping_kwarg in regions_dict.items(): tof_output_name = str( run_no) + "_" + FOCUSED_OUTPUT_WORKSPACE_NAME + region dspacing_output_name = tof_output_name + "_dSpacing" region_calib_ws = self._get_region_calib_ws(region) curves = self._get_van_curves_for_roi(region, van_processed_inst_ws, grouping_kwarg) # perform focus over chosen region of interest self._run_focus(sample_workspace, tof_output_name, curves, grouping_kwarg, region_calib_ws) sample_plots.append(tof_output_name) self._save_output(instrument, run_no, van_run_no, region, tof_output_name, rb_num) self._save_output(instrument, run_no, van_run_no, region, dspacing_output_name, rb_num, unit="dSpacing") self._output_sample_logs(instrument, run_no, van_run_no, sample_workspace, rb_num) output_workspaces.append(sample_plots) DeleteWorkspace(sample_workspace) # remove created grouping workspace if present if Ads.doesExist("grp_ws"): DeleteWorkspace("grp_ws") # Plot the output if plot_output: for ws_names in output_workspaces: self._plot_focused_workspaces(ws_names)