def test_run_string_to_list_throws_for_incorrectly_placed_delimiter(self): run_strings = [",1,2,3"] for run_string in run_strings: with self.assertRaises(IndexError) as context: utils.run_string_to_list(run_string) self.assertTrue( run_string + " is not a valid run string" in str(context.exception))
def update_model_from_view(self): table = self._view.get_table_contents() self._model.clear_groups() for entry in table: detector_list = run_utils.run_string_to_list(str(entry[inverse_group_table_columns['detector_ids']]), False) periods = run_utils.run_string_to_list(str(entry[inverse_group_table_columns['periods']])) group = MuonGroup(group_name=str(entry[0]), detector_ids=detector_list, periods=periods) self._model.add_group(group)
def test_run_string_to_list_throws_for_incorrectly_placed_range_separator( self): run_strings = ["-1,2,3"] for run_string in run_strings: with self.assertRaises(IndexError) as error: utils.run_string_to_list(run_string) self.assertTrue( run_string + " is not a valid run string" in str(error.exception))
def _calculate_diffs_for(self, runs: list, diffs: list) -> None: """Calculates the Diff Asymmetry workspaces for the provided runs and diffs.""" for run in runs: run_list = run_string_to_list(run) for diff_object in diffs: self._context.calculate_diff_for(run_list, diff_object) self._context.show_diff(run_list, diff_object)
def _calculate_pairs_for(self, runs: list, pairs: list) -> None: """Calculates the Pair Asymmetry workspaces for the provided runs and pairs.""" for run in runs: run_list = run_string_to_list(run) for pair_object in pairs: self._context.calculate_pair_for(run_list, pair_object) self._context.show_pair(run_list, pair_object)
def get_workspaces_to_plot(self, is_raw, plot_type, detectors: str, run_string, period=None): """ :param is_raw: Whether to use raw or rebinned data :param plot_type: plotting type, e.g Counts, Frequency Re :return: a list of workspace names """ workspace_list = [] run = run_string_to_list(run_string) # make sure the run is in the context if run not in self.context.data_context.current_runs: return workspace_list multi_period = False if self.context.data_context.num_periods(run) > 1: multi_period = True if period: workspace_list += self.get_ws_names(run_string, multi_period, str(period), detectors) else: for period_i in range( self.context.data_context.num_periods(run)): # periods start at 1 and not 0 workspace_list += self.get_ws_names( run_string, multi_period, str(period_i + 1), detectors) else: workspace_list += self.get_ws_names(run_string, multi_period, '1', detectors) return workspace_list
def validate_periods(self, period_text): try: period_list = run_utils.run_string_to_list(period_text) except IndexError: # An IndexError thrown here implies that the input string is not a valid # number list. return RowValid.invalid_for_all_runs return self._model.validate_periods_list(period_list)
def default_x_range(self, run: str, group: str) -> tuple: """Returns the x range to use by default for the background corrections. It is the second half of the data.""" run_list = run_string_to_list(run) last_good_data = self._context.last_good_data(run_list) if last_good_data != 0.0: return self._get_range_for_second_half_of_data(self._context.first_good_data(run_list), last_good_data) else: return self._get_x_range_from_counts_workspace(run, group)
def _load_periods(self): run = run_string_to_list(self.view.get_run) periods = [] if run != []: periods = [ str(period + 1) for period in range(self.context.data_context.num_periods(run)) ] self.view.add_periods(periods)
def _get_groups_from_XML(root): names, ids, periods = [], [], [] for child in root: if child.tag == "group": names += [child.attrib['name']] ids += [ run_string_utils.run_string_to_list( child.find('ids').attrib['val']) ] try: periods += [ run_string_utils.run_string_to_list( child.find('periods').attrib['val']) ] except AttributeError: # Using an old style xml file where grouping information is not stored pass return names, ids, periods
def validate_detector_ids(self, text): try: if re.match(run_utils.run_string_regex, text) and \ self._validate_detector_ids_list(run_utils.run_string_to_list(text, False)): return True except OverflowError: pass self._view.warning_popup("Invalid detector list.") return False
def get_runs(self, runs): if runs == 'All': run_list = self.data_context.current_runs else: run_list = [run_string_to_list(item) for item in runs.replace(' ', '').split(',')] flat_list = [] for sublist in run_list: flat_list += [[run] for run in sublist if len(sublist) > 1] run_list += flat_list run_list = [ run for run in run_list if run in self.data_context.current_runs] return run_list
def _create_asymmetry_workspace_for(self, run: str, group: str) -> None: """Creates the asymmetry workspace for a run and group using its corresponding Counts workspace in the ADS.""" run_list = run_string_to_list(run) group_object = self._context.group_pair_context[group] if run_list is not None and group_object is not None: self._context.calculate_asymmetry_for(run_list, group_object, rebin=False) self._context.show_group(run_list, group_object, rebin=False) if self._context._do_rebin(): self._context.calculate_asymmetry_for(run_list, group_object, rebin=True) self._context.show_group(run_list, group_object, rebin=True)
def handle_run_changed_by_user(self): run_string = self._view.get_run_edit_text() if not run_string: return try: self.run_list = run_utils.run_string_to_list(run_string) except IndexError as err: self._view.warning_popup(err.args[0]) return file_names = [ file_utils.file_path_for_instrument_and_run( self.get_current_instrument(), new_run) for new_run in self.run_list if not self._model.get_data(run=[new_run]) ] if file_names: self.load_runs(file_names) else: self.on_loading_finished()
def test_run_string_to_list_handles_non_consecutive_runs(self): run_lists = [[1, 3, 4, 5], [0, 1, 2, 3, 8, 9, 199, 200]] run_strings = ["1,3-5", "0-3,8-9,199-200"] for i in range(len(run_lists)): self.assertEqual(utils.run_string_to_list(run_strings[i]), run_lists[i])
def test_run_string_to_list_removes_duplicates(self): run_strings = ["5,5,5,5,4,3,2,1,1", "1,2,4,5,1-3,1-5,1-4"] for run_string in run_strings: run_list = utils.run_string_to_list(run_string) self.assertEqual(run_list, [1, 2, 3, 4, 5])
def test_run_string_to_list_allows_large_number_runs(self): run_string = '1-1001' expected_list = list(range(1, 1002)) self.assertEqual(utils.run_string_to_list(run_string), expected_list)
def test_run_string_to_list_correct_for_consecutive_runs(self): run_strings = ["1-5", "1,2,3,4,5"] for run_string in run_strings: run_list = utils.run_string_to_list(run_string) self.assertEqual(run_list, [1, 2, 3, 4, 5])
def test_run_string_allows_range_over_decade(self): run_string = '62268-2' run_list = [62268, 62269, 62270, 62271, 62272] self.assertEqual(utils.run_string_to_list(run_string), run_list)
def test_run_string_removes_whitespace(self): run_string = "1 -3,48- 50" run_list = [1, 2, 3, 48, 49, 50] self.assertEqual(utils.run_string_to_list(run_string), run_list)
def test_run_string_allows_trailing_comma(self): run_string = '5,4,3,2,1,' run_list = utils.run_string_to_list(run_string) self.assertEqual(run_list, [1, 2, 3, 4, 5])
def test_run_string_to_list_allows_trailing_dash_in_long_string(self): run_string = '1,2,3,10-10010-' expected_list = [1, 2, 3] + list(range(10, 10011)) self.assertEqual(utils.run_string_to_list(run_string), expected_list)
def test_run_string_to_list_allows_trailing_dash_in_short_string(self): run_string = '1,2,3,10-15-' expected_list = [1, 2, 3, 10, 11, 12, 13, 14, 15] self.assertEqual(utils.run_string_to_list(run_string), expected_list)
def string_to_list(text): return run_string_to_list(text)
def test_run_string_to_list_sorts_list_ascending(self): run_strings = ["5,4,3,2,1", "4,5,1-3"] for run_string in run_strings: run_list = utils.run_string_to_list(run_string) self.assertEqual(run_list, [1, 2, 3, 4, 5])
def test_run_string_allows_incomplete_upper_range(self): run_string = '62260-66' run_list = [62260, 62261, 62262, 62263, 62264, 62265, 62266] self.assertEqual(utils.run_string_to_list(run_string), run_list)
def test_run_string_to_list_orders_non_consecutive_runs(self): run_string = "1-3,48-50" run_list = [1, 2, 3, 48, 49, 50] self.assertEqual(utils.run_string_to_list(run_string), run_list)