1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
# stdlib
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Type, Union, no_type_check
# 3rd party
import attrs
import pytest
from coincidence.regressions import AdvancedDataRegressionFixture, AdvancedFileRegressionFixture
from domdf_python_tools.paths import PathPlus
# this package
from dom_toml.config import Config, subtable_field, table_name, to_kebab_case
from dom_toml.config.fields import Boolean, Integer, Number, String
@table_name("savitzky-golay")
@attrs.define
class SavitzkyGolayMethod(Config):
enable: bool = Boolean.field(default=True)
window: int = attrs.field(default=7)
degree: int = Integer.field(default=2)
def _convert_sg_method(method: Union[bool, "SavitzkyGolayMethod", Dict[str, Any]]) -> "SavitzkyGolayMethod":
if isinstance(method, bool):
return SavitzkyGolayMethod()
elif isinstance(method, SavitzkyGolayMethod):
return method
else:
return SavitzkyGolayMethod(**method)
@attrs.define
class IntensityMatrixMethod(Config):
crop_mass_range: Optional[Tuple[int, int]] = attrs.field(default=(50, 500))
savitzky_golay: SavitzkyGolayMethod = attrs.field(
default=SavitzkyGolayMethod(),
converter=_convert_sg_method,
)
tophat: bool = Boolean.field(default=True)
tophat_structure_size: str = String.field(default="1.5m")
@attrs.define
class PeakDetectionMethod(Config):
points: int = Integer.field(default=10)
scans: int = Integer.field(default=1)
def default_base_peak_filter() -> Sequence[int]:
"""
Returns the default value for the ``base_peak_filter`` option.
"""
return (73, 147)
@attrs.define
class PeakFilterMethod(Config):
noise_filter: bool = Boolean.field(default=True)
noise_threshold: int = Integer.field(default=2)
base_peak_filter: Set[int] = attrs.field(
default=attrs.Factory(default_base_peak_filter),
converter=tuple,
)
@attrs.define
class AlignmentMethod(Config):
rt_modulation: float = Number.field(default=2.5)
gap_penalty: float = Number.field(default=0.3)
min_peaks: int = Integer.field(default=1)
top_n_peaks: int = Integer.field(default=80)
min_peak_area: float = Number.field(default=0.0)
@attrs.define
class ConsolidateMethod(Config):
name_filter: List[str] = attrs.field(converter=list, default=attrs.Factory(list))
min_match_factor: int = Integer.field(default=600)
min_appearances: int = Integer.field(default=-1)
@attrs.define
class Method(Config):
"""
Overall GunShotMatch method.
"""
#: Method used for constructing an intensity matrix from a datafile.
intensity_matrix: IntensityMatrixMethod = subtable_field(IntensityMatrixMethod)
#: Method used for Biller-Biemann peak detection.
peak_detection: PeakDetectionMethod = subtable_field(PeakDetectionMethod)
#: Method used for peak filtering.
peak_filter: PeakFilterMethod = subtable_field(PeakFilterMethod)
#: Method used for peak alignment.
alignment: AlignmentMethod = subtable_field(AlignmentMethod)
#: Method used for consolidation (finding most likely identity for aligned peaks).
consolidate: ConsolidateMethod = subtable_field(ConsolidateMethod)
@pytest.mark.parametrize(
"cls",
[
pytest.param(AlignmentMethod, id="AlignmentMethod"),
pytest.param(PeakFilterMethod, id="PeakFilterMethod"),
pytest.param(ConsolidateMethod, id="ConsolidateMethod"),
pytest.param(PeakDetectionMethod, id="PeakDetectionMethod"),
pytest.param(SavitzkyGolayMethod, id="SavitzkyGolayMethod"),
pytest.param(IntensityMatrixMethod, id="IntensityMatrixMethod"),
pytest.param(Method, id="Method"),
],
)
def test_default_methods(advanced_data_regression: AdvancedDataRegressionFixture, cls: Type[Config]):
method = cls()
advanced_data_regression.check(method.to_dict())
@pytest.mark.parametrize(
"cls",
[
pytest.param(AlignmentMethod, id="AlignmentMethod"),
pytest.param(PeakFilterMethod, id="PeakFilterMethod"),
pytest.param(ConsolidateMethod, id="ConsolidateMethod"),
pytest.param(PeakDetectionMethod, id="PeakDetectionMethod"),
pytest.param(SavitzkyGolayMethod, id="SavitzkyGolayMethod"),
pytest.param(IntensityMatrixMethod, id="IntensityMatrixMethod"),
pytest.param(Method, id="Method"),
],
)
def test_to_toml(advanced_file_regression: AdvancedFileRegressionFixture, cls: Type[Config]):
method = cls()
advanced_file_regression.check(method.to_toml())
@pytest.mark.parametrize(
"cls, kwargs",
[
pytest.param(SavitzkyGolayMethod, {"enable": False}, id="savgol_false"),
pytest.param(SavitzkyGolayMethod, {"window": 8}, id="savgol_window"),
pytest.param(SavitzkyGolayMethod, {"window": "1m"}, id="savgol_window_str_min"),
pytest.param(SavitzkyGolayMethod, {"window": "20s"}, id="savgol_window_str_sec"),
pytest.param(SavitzkyGolayMethod, {"degree": 5}, id="savgol_degree"),
pytest.param(SavitzkyGolayMethod, {"window": 8, "degree": 5}, id="savgol_window_degree"),
pytest.param(IntensityMatrixMethod, {"crop_mass_range": [100, 200]}, id="im_mass_range"),
pytest.param(
IntensityMatrixMethod,
{"tophat_structure_size": "2m"},
id="im_tophat_structure_size",
),
pytest.param(
IntensityMatrixMethod,
{"savitzky_golay": {"enable": False}},
id="im_savgol_dict_false",
),
pytest.param(
IntensityMatrixMethod,
{"savitzky_golay": {"window": 10}},
id="im_savgol_dict_window",
),
pytest.param(IntensityMatrixMethod, {"savitzky_golay": False}, id="im_savgol_false"),
pytest.param(PeakDetectionMethod, {"points": 8}, id="peak_detection_points"),
pytest.param(PeakDetectionMethod, {"scans": 3}, id="peak_detection_scans"),
pytest.param(PeakFilterMethod, {"noise_filter": False}, id="peak_filter_noise_filter"),
pytest.param(PeakFilterMethod, {"noise_threshold": 5}, id="peak_filter_noise_threshold"),
pytest.param(
PeakFilterMethod,
{"base_peak_filter": [1, 2, 3, 4, 5]},
id="peak_filter_base_peak_filter_list",
),
pytest.param(
PeakFilterMethod,
{"base_peak_filter": {1, 2, 3, 4, 5}},
id="peak_filter_base_peak_filter_set",
),
pytest.param(AlignmentMethod, {"rt_modulation": 10}, id="alignment_rt_modulation"),
pytest.param(AlignmentMethod, {"gap_penalty": 0.2}, id="alignment_gap_penalty"),
pytest.param(AlignmentMethod, {"min_peaks": 5}, id="alignment_min_peaks"),
pytest.param(AlignmentMethod, {"top_n_peaks": 50}, id="alignment_top_n_peaks"),
pytest.param(AlignmentMethod, {"min_peak_area": 1.5e6}, id="alignment_min_peak_area"),
],
)
def test_partial_arguments(
advanced_data_regression: AdvancedDataRegressionFixture,
cls: Type[Config],
kwargs: Dict[str, Any],
):
method = cls(**kwargs)
advanced_data_regression.check(method.to_dict())
method = cls.from_dict(kwargs)
advanced_data_regression.check(method.to_dict())
def test_from_toml(advanced_data_regression: AdvancedDataRegressionFixture):
config_file = PathPlus(__file__).parent / "method.toml"
config_toml = config_file.read_text()
method = Method.from_toml(config_toml)
advanced_data_regression.check(method.to_dict())
def test_from_json(advanced_data_regression: AdvancedDataRegressionFixture):
config_file = PathPlus(__file__).parent / "method.json"
config_json = config_file.read_text()
method = Method.from_json(config_json)
advanced_data_regression.check(method.to_dict())
def test_coerce_error():
with pytest.raises(TypeError, match="Cannot convert str to a PeakFilterMethod"):
Method(peak_filter="banana") # type: ignore[arg-type]
with pytest.raises(TypeError, match="Cannot convert str to an IntensityMatrixMethod"):
Method(intensity_matrix="banana") # type: ignore[arg-type]
@attrs.define
class FieldsMethod(Config):
boolean_field: bool = Boolean.field(default=True)
integer_field: int = Integer.field(default=2)
number_field: float = Number.field(default=2.0)
string_field: float = String.field(default="abcdefg")
@attrs.define
class FieldsMethodNew(Config):
boolean_field: bool = Boolean(default=True) # type: ignore[assignment]
integer_field: int = Integer(default=2) # type: ignore[assignment]
number_field: float = Number(default=2.0) # type: ignore[assignment]
string_field: float = String(default="abcdefg") # type: ignore[assignment]
@no_type_check
@pytest.mark.parametrize("method", [FieldsMethod, FieldsMethodNew])
def test_fields(method: Type[Config]):
method(boolean_field=1234)
method(integer_field=1234)
method(number_field=1234)
method(string_field=1234)
method(boolean_field=12.34)
method(integer_field=12.34)
method(number_field=12.34)
method(string_field=12.34)
method(boolean_field="abcdefg")
with pytest.raises(ValueError, match=r"invalid literal for int\(\) with base 10: 'abcdefg'"):
method(integer_field="abcdefg")
with pytest.raises(ValueError, match=r"could not convert string to float: 'abcdefg'"):
method(number_field="abcdefg")
method(string_field="abcdefg")
method(boolean_field=None)
with pytest.raises(
TypeError,
match=r"int\(\) argument must be a string, a bytes-like object or a (real )?number, not 'NoneType'",
):
method(integer_field=None)
with pytest.raises(
TypeError,
match=r"float\(\) argument must be a string or a (real )?number, not 'NoneType'",
):
method(number_field=None)
method(string_field=None)
method(boolean_field={"abcdefg": None})
with pytest.raises(
TypeError,
match=r"int\(\) argument must be a string, a bytes-like object or a (real )?number, not 'dict'",
):
method(integer_field={"abcdefg": None})
with pytest.raises(TypeError, match=r"float\(\) argument must be a string or a (real )?number, not 'dict'"):
method(number_field={"abcdefg": None})
method(string_field={"abcdefg": None})
method().boolean_field = 1234
method().integer_field = 1234
method().number_field = 1234
method().string_field = 1234
method().boolean_field = 12.34
method().integer_field = 12.34
method().number_field = 12.34
method().string_field = 12.34
method().boolean_field = "abcdefg"
with pytest.raises(ValueError, match=r"invalid literal for int\(\) with base 10: 'abcdefg'"):
method().integer_field = "abcdefg"
with pytest.raises(ValueError, match=r"could not convert string to float: 'abcdefg'"):
method().number_field = "abcdefg"
method().string_field = "abcdefg"
method().boolean_field = None
with pytest.raises(
TypeError,
match=r"int\(\) argument must be a string, a bytes-like object or a (real )?number, not 'NoneType'",
):
method().integer_field = None
with pytest.raises(
TypeError,
match=r"float\(\) argument must be a string or a (real )?number, not 'NoneType'",
):
method().number_field = None
method().string_field = None
method().boolean_field = {"abcdefg": None}
with pytest.raises(
TypeError,
match=r"int\(\) argument must be a string, a bytes-like object or a (real )?number, not 'dict'",
):
method().integer_field = {"abcdefg": None}
with pytest.raises(TypeError, match=r"float\(\) argument must be a string or a (real )?number, not 'dict'"):
method().number_field = {"abcdefg": None}
method().string_field = {"abcdefg": None}
def test_kebab_case():
assert to_kebab_case("FooBar") == "foo-bar"
assert to_kebab_case("FooBBar") == "foo-b-bar"
assert to_kebab_case("Foo_Bar") == "foo-bar"
assert to_kebab_case("foo_bar") == "foo-bar"
assert to_kebab_case("foobar") == "foobar"
assert to_kebab_case("FooBar123") == "foo-bar123"
assert to_kebab_case(Config) == "config"
assert to_kebab_case(SavitzkyGolayMethod) == "savitzky-golay"
assert to_kebab_case(IntensityMatrixMethod) == "intensity-matrix-method"
assert to_kebab_case(PeakDetectionMethod) == "peak-detection-method"
assert to_kebab_case(PeakFilterMethod) == "peak-filter-method"
assert to_kebab_case(AlignmentMethod) == "alignment-method"
assert to_kebab_case(ConsolidateMethod) == "consolidate-method"
assert to_kebab_case(Method) == "method"
assert to_kebab_case(FieldsMethod) == "fields-method"
assert to_kebab_case(FieldsMethodNew) == "fields-method-new"
assert to_kebab_case(Config()) == "config"
assert to_kebab_case(SavitzkyGolayMethod()) == "savitzky-golay"
assert to_kebab_case(IntensityMatrixMethod()) == "intensity-matrix-method"
assert to_kebab_case(PeakDetectionMethod()) == "peak-detection-method"
assert to_kebab_case(PeakFilterMethod()) == "peak-filter-method"
assert to_kebab_case(AlignmentMethod()) == "alignment-method"
assert to_kebab_case(ConsolidateMethod()) == "consolidate-method"
assert to_kebab_case(Method()) == "method"
assert to_kebab_case(FieldsMethod()) == "fields-method"
assert to_kebab_case(FieldsMethodNew()) == "fields-method-new"
|