import numpy import pandas import pytest def test_check_if_xdr_ready_ok(exposure_df): from sensospot_data.columns import ( SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, ) from sensospot_data.dynamic_range import _check_if_xdr_ready exposure_df[SETTINGS_EXPOSURE_TIME] = 1 exposure_df[SETTINGS_EXPOSURE_CHANNEL] = 2 result = _check_if_xdr_ready(exposure_df) assert result is None @pytest.mark.parametrize(["run"], [[0], [1], [2]]) def test_check_if_xdr_ready_raises_error_missing_column(exposure_df, run): from sensospot_data.columns import ( SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, ) from sensospot_data.dynamic_range import _check_if_xdr_ready columns = [SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, "X"] extra_col = columns[run] exposure_df[extra_col] = 1 with pytest.raises(ValueError): _check_if_xdr_ready(exposure_df) def test_check_if_xdr_ready_raises_error_mixed_channels(exposure_df): from sensospot_data.columns import ( META_DATA_EXPOSURE_ID, SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, ) from sensospot_data.dynamic_range import _check_if_xdr_ready exposure_df[SETTINGS_EXPOSURE_TIME] = 1 exposure_df[SETTINGS_EXPOSURE_CHANNEL] = exposure_df[META_DATA_EXPOSURE_ID] with pytest.raises(ValueError): _check_if_xdr_ready(exposure_df) def test_check_if_xdr_ready_raises_error_non_numeric_time(exposure_df): from sensospot_data.columns import ( SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, ) from sensospot_data.dynamic_range import _check_if_xdr_ready exposure_df[SETTINGS_EXPOSURE_TIME] = "X" exposure_df[SETTINGS_EXPOSURE_CHANNEL] = 2 with pytest.raises(ValueError): _check_if_xdr_ready(exposure_df) def test_check_if_xdr_ready_raises_error_on_nan(exposure_df): from sensospot_data.columns import ( SETTINGS_EXPOSURE_TIME, SETTINGS_EXPOSURE_CHANNEL, ) from sensospot_data.dynamic_range import _check_if_xdr_ready exposure_df[SETTINGS_EXPOSURE_TIME] = numpy.nan exposure_df[SETTINGS_EXPOSURE_CHANNEL] = 2 with pytest.raises(ValueError): _check_if_xdr_ready(exposure_df) def test_check_overflow_limit_defaults(): from sensospot_data.columns import CALC_SPOT_OVERFLOW, RAW_DATA_SPOT_MEAN from sensospot_data.dynamic_range import _calc_overflow_info data_frame = pandas.DataFrame(data={RAW_DATA_SPOT_MEAN: [0.1, 0.5, 0.6]}) result = _calc_overflow_info(data_frame) assert list(result[CALC_SPOT_OVERFLOW]) == [False, False, True] def test_check_overflow_limit_custom_limit(): from sensospot_data.columns import CALC_SPOT_OVERFLOW from sensospot_data.dynamic_range import _calc_overflow_info data_frame = pandas.DataFrame(data={"X": [4, 2, 3, 4]}) result = _calc_overflow_info(data_frame, "X", 2) assert list(result[CALC_SPOT_OVERFLOW]) == [True, False, True, True] def test_reduce_overflow_multiple_times(normalization_data_frame): from sensospot_data.dynamic_range import ( PROBE_MULTI_INDEX, _reduce_overflow, _calc_overflow_info, ) data_frame = _calc_overflow_info(normalization_data_frame, "Saturation", 1) result = _reduce_overflow(data_frame) sorted_results = result.sort_values(by=PROBE_MULTI_INDEX) assert list(sorted_results["Value"]) == [ 1, 2, 3, 1, 10, 10, 10, 10, 100, 100, 100, 100, ] def test_reduce_overflow_only_one_exposure_time(normalization_data_frame): from sensospot_data.dynamic_range import ( SETTINGS_EXPOSURE_TIME, _reduce_overflow, _calc_overflow_info, ) normalization_data_frame[SETTINGS_EXPOSURE_TIME] = 1 data_frame = _calc_overflow_info(normalization_data_frame, "Saturation", 1) result = _reduce_overflow(data_frame) assert list(result["Value"]) == list(normalization_data_frame["Value"]) def test_blend(normalization_data_frame): from sensospot_data.dynamic_range import PROBE_MULTI_INDEX, blend result = blend(normalization_data_frame, "Saturation", 1) sorted_results = result.sort_values(by=PROBE_MULTI_INDEX) assert list(sorted_results["Value"]) == [ 1, 2, 3, 1, 10, 10, 10, 10, 100, 100, 100, 100, ] def test_blend_raises_error(normalization_data_frame): from sensospot_data.dynamic_range import SETTINGS_EXPOSURE_TIME, blend normalization_data_frame[SETTINGS_EXPOSURE_TIME] = "A" with pytest.raises(ValueError): blend(normalization_data_frame, "Saturation", 1) def test_normalize_values_no_param(normalization_data_frame): from sensospot_data.columns import RAW_DATA_NORMALIZATION_MAP from sensospot_data.dynamic_range import ( PROBE_MULTI_INDEX, blend, normalize_values, ) reduced = blend(normalization_data_frame, "Saturation", 1) result = normalize_values(reduced) sorted_results = result.sort_values(by=PROBE_MULTI_INDEX) expected_values = [1, 4, 15, 1, 10, 10, 10, 10, 100, 100, 100, 100] for normalized_col in RAW_DATA_NORMALIZATION_MAP.values(): assert list(sorted_results[normalized_col]) == expected_values def test_normalize_values_custom_param(normalization_data_frame): from sensospot_data.columns import RAW_DATA_NORMALIZATION_MAP from sensospot_data.dynamic_range import ( PROBE_MULTI_INDEX, blend, normalize_values, ) reduced = blend(normalization_data_frame, "Saturation", 1) result = normalize_values(reduced, 100) sorted_results = result.sort_values(by=PROBE_MULTI_INDEX) expected_values = [2, 8, 30, 2, 20, 20, 20, 20, 200, 200, 200, 200] for normalized_col in RAW_DATA_NORMALIZATION_MAP.values(): assert list(sorted_results[normalized_col]) == expected_values def test_create_xdr(normalization_data_frame): from sensospot_data.columns import RAW_DATA_NORMALIZATION_MAP from sensospot_data.dynamic_range import PROBE_MULTI_INDEX, create_xdr result = create_xdr(normalization_data_frame, 100, "Saturation", 1) sorted_results = result.sort_values(by=PROBE_MULTI_INDEX) expected_values = [2, 8, 30, 2, 20, 20, 20, 20, 200, 200, 200, 200] for normalized_col in RAW_DATA_NORMALIZATION_MAP.values(): assert list(sorted_results[normalized_col]) == expected_values