def case_select_all_records(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_all_records() fitting_data.select_record(2) fitting_data.select_record(5) fitting_data.select_all_records() return fitting_data, list(range(1, NUMBER_OF_RECORDS + 1))
def case_unselect_one_record(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_record(2) return ( fitting_data, [1] + list(range(3, NUMBER_OF_RECORDS + 1)), )
def test_save_and_read_csv_without_name(tmpdir): original_fitting_data = FittingData(data=RAW_DATA) original_fitting_data.save_csv(tmpdir) loaded_fitting_data = FittingData.read_from_csv( Path(tmpdir / "fitting_data.csv")) assert_fitting_data_are_equal(original_fitting_data, loaded_fitting_data)
def test_x_larger_than_size(column): with pytest.raises( FittingDataColumnIndexError, match= "^No column number 11 in data. index should be between 1 and 10$", ): FittingData(COLUMNS, **{column: 11})
def case_fitting(case_path): with open(str(case_path), mode="r") as json_file: json_obj = json.load(json_file) func_name = json_obj["fit_function"] func = FittingFunctionsRegistry.load(func_name) fix = json_obj.get("fix", None) if fix is not None: for index, value in fix: func.fix(index, value) data_dict = json_obj["data"] data = FittingData( OrderedDict([ ("x", data_dict["x"]), ("xerr", data_dict["xerr"]), ("y", data_dict["y"]), ("yerr", data_dict["yerr"]), ])) a0 = json_obj.get("a0", None) result = fit(data=data, func=func, a0=a0) decimal = json_obj.get("decimal", 2) inp = dict(actual_result=result, a0=a0, func=func, delta=np.power(10.0, -decimal)) expected_result = json_obj["result"] return inp, expected_result
def test_x_zero_index(column): with pytest.raises( FittingDataColumnIndexError, match= "^No column number 0 in data. index should be between 1 and 10$", ): FittingData(COLUMNS, **{column: 0})
def test_set_cell_not_allowed_because_of_non_existing_row(): fitting_data = FittingData(COLUMNS) value = np.random.uniform(0, 10) column = np.random.choice(COLUMNS_NAMES) with pytest.raises(FittingDataSetError, match="^Record number 13 does not exists$"): fitting_data.set_cell(NUMBER_OF_RECORDS + 1, column, value)
def case_unselect_multiple(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_record(2) fitting_data.unselect_record(5) fitting_data.unselect_record(3) fitting_data.unselect_record(10) return fitting_data, [1, 4, 6, 7, 8, 9] + list(range(11, NUMBER_OF_RECORDS + 1))
def test_save_and_read_excel_without_name(tmpdir): original_fitting_data = FittingData(data=RAW_DATA) original_fitting_data.save_excel(tmpdir, sheet=SHEET_NAME) loaded_fitting_data = FittingData.read_from_excel( Path(tmpdir / "fitting_data.xlsx"), SHEET_NAME) assert_fitting_data_are_equal(original_fitting_data, loaded_fitting_data)
def case_jumbled_columns(): fitting_data = FittingData(COLUMNS, x_column=3, xerr_column=1, y_column="b", yerr_column=9) expected_columns = Columns(x="c", xerr="a", y="b", yerr="i") return fitting_data, expected_columns
def test_exception_risen_because_of_columns_length(): data = deepcopy(COLUMNS) data["a"] = data["a"][:-2] with pytest.raises( FittingDataColumnsLengthError, match="^All columns in FittingData should have the same length$", ): FittingData(data=data)
def test_set_header_with_same_header_does_not_change_anything(header_name): fitting_data = FittingData(COLUMNS) fitting_data.set_header(header_name, header_name) for header in COLUMNS_NAMES: assert fitting_data.data[header] == pytest.approx( COLUMNS[header] ), f'Header "{header}" has changed unexpectedly'
def test_set_cell_not_allowed_because_of_non_existing_column(): fitting_data = FittingData(COLUMNS) value = np.random.uniform(0, 10) with pytest.raises( FittingDataSetError, match='^Column name "I do not exist" does not exists$' ): fitting_data.set_cell(0, "I do not exist", value)
def case_select_multiple_records(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_all_records() fitting_data.select_record(2) fitting_data.select_record(5) fitting_data.select_record(3) fitting_data.select_record(10) return fitting_data, [2, 3, 5, 10]
def test_header_cannot_already_exist(header_name): fitting_data = FittingData(COLUMNS) new_header = np.random.choice( [header for header in COLUMNS_NAMES if header != header_name] ) with pytest.raises( FittingDataSetError, match=f'^The column name:"{new_header}" is already used.$' ): fitting_data.set_header(header_name, new_header)
def test_set_selection_with_different_size(): fitting_data = FittingData(COLUMNS) def set_records_indices(): fitting_data.records_indices = [False, False, True] with pytest.raises( FittingDataColumnsSelectionError, match=f"^Should select {NUMBER_OF_RECORDS} records, only 3 selected.$", ): set_records_indices()
def test_set_selection_with_non_boolean_values(): fitting_data = FittingData(COLUMNS) def set_records_indices(): fitting_data.records_indices = [False, False, "dummy"] + [True] * ( NUMBER_OF_RECORDS - 3 ) with pytest.raises( FittingDataColumnsSelectionError, match="^When setting record indices, all values should be booleans.$", ): set_records_indices()
def test_set_cell_not_allowed_because_of_non_float_value(): fitting_data = FittingData(COLUMNS) column = np.random.choice(COLUMNS_NAMES) record = np.random.randint(1, NUMBER_OF_RECORDS + 1) value = "I'm not a float" with pytest.raises( FittingDataSetError, match=( f'^The cell at record number:"{record}", column:"{column}"' f" has invalid syntax: I'm not a float.$" ), ): fitting_data.set_cell(record, column, value)
def test_set_cell_allowed(record_number, column_name): fitting_data = FittingData(deepcopy(COLUMNS)) value = np.random.uniform(0, 10) fitting_data.set_cell( record_number=record_number, column_name=column_name, value=value, ) for column in COLUMNS_NAMES: for i in range(NUMBER_OF_RECORDS): if column == column_name and i + 1 == record_number: assert fitting_data.data[column][i] == value, "New value was not set" else: assert ( fitting_data.data[column][i] == COLUMNS[column][i] ), "Data was change unexpectedly"
def test_set_header_allowed(i): old_header = COLUMNS_NAMES[i] new_header = "new_header" fitting_data = FittingData(deepcopy(COLUMNS)) fitting_data.set_header(old_header, new_header) constant_headers = [header for header in COLUMNS_NAMES if header != old_header] for header in constant_headers: assert fitting_data.data[header] == pytest.approx( COLUMNS[header] ), f'Header "{header}" has changed unexpectedly' assert fitting_data.data[new_header] == pytest.approx( COLUMNS[old_header] ), f'Header "{new_header}" has changed unexpectedly' assert set(fitting_data.all_columns) == set( constant_headers + [new_header] ), "Did not update all columns"
def case_default(): fitting_data = FittingData(COLUMNS) expected_columns = Columns(x="a", xerr="b", y="c", yerr="d") return fitting_data, expected_columns
def test_header_cannot_be_set_to_be_empty(header_name): fitting_data = FittingData(COLUMNS) with pytest.raises( FittingDataSetError, match="^Cannot set new header to be empty$" ): fitting_data.set_header(header_name, "")
def case_unselect_two_records(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_record(2) fitting_data.unselect_record(5) return fitting_data, [1, 3, 4] + list(range(6, NUMBER_OF_RECORDS + 1))
from eddington import FittingData from tests.fitting_data import ( COLUMNS, COLUMNS_NAMES, CONTENT, DEFAULT_SHEET, NUMBER_OF_RECORDS, ) DECIMAL = 5 DIRECTORY_PATH = Path("/path/to/directory") EPSILON = 1e-5 FIT_DATA = FittingData(COLUMNS) def assert_workbook_calls(workbook, sheet_name=DEFAULT_SHEET, name="fitting_data"): worksheet = workbook.active assert worksheet.title == sheet_name assert (worksheet.append.call_count == NUMBER_OF_RECORDS + 1), "worksheet append called unexpected number of times" assert worksheet.append.call_args_list[0] == call( COLUMNS_NAMES), "worksheet append called unexpected number of times" for i in range(NUMBER_OF_RECORDS): assert worksheet.append.call_args_list[i + 1][0][0] == pytest.approx( CONTENT[i], rel=EPSILON), "worksheet append called unexpected number of times"
def case_set_selected_records(): fitting_data = FittingData(COLUMNS) fitting_data.records_indices = [False, True, False, False, True] + [False] * ( NUMBER_OF_RECORDS - 5 ) return fitting_data, [2, 5]
def case_string_yerr_column(): fitting_data = FittingData(COLUMNS, yerr_column="f") expected_columns = Columns(x="a", xerr="b", y="c", yerr="f") return fitting_data, expected_columns
def case_select_two_records(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_all_records() fitting_data.select_record(2) fitting_data.select_record(5) return fitting_data, [2, 5]
def case_x_and_y_column(): fitting_data = FittingData(COLUMNS, x_column=3, y_column="h") expected_columns = Columns(x="c", xerr="d", y="h", yerr="i") return fitting_data, expected_columns
def case_unselect_all(): fitting_data = FittingData(COLUMNS) fitting_data.unselect_all_records() return fitting_data, []
def case_int_xerr_column(): fitting_data = FittingData(COLUMNS, xerr_column=4) expected_columns = Columns(x="a", xerr="d", y="e", yerr="f") return fitting_data, expected_columns