Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:

# Mandatory internal hooks
- repo: https://github.com/uktrade/github-standards
rev: v1.4.3 # update periodically with pre-commit autoupdate
rev: v1.4.5 # update periodically with pre-commit autoupdate
hooks:
- id: run-security-scan
verbose: false
Expand Down
2 changes: 2 additions & 0 deletions docs/api/plan/steps/models/linkers/splink.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
`SplinkLinker` uses Splink 5. See the [Splink documentation](https://moj-analytical-services.github.io/splink/) for configuration and usage.

::: matchlab.models.linkers.splinklinker
options:
show_root_heading: true
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"pyarrow>=17.0.0",
"pydantic>=2.9.2",
"rich>=13.9.4",
"splink>=4.0.5,<4.1.0",
"splink>=5.0.0,<5.1.0",
"sqlalchemy>=2.0.35",
"sqlglot[c]>=30.0.3",
"textual>=0.80",
Expand Down Expand Up @@ -132,6 +132,7 @@ docstring-code-format = true
[tool.pytest.ini_options]
testpaths = ["test"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
pythonpath = ["."]
addopts = "-n auto --dist=loadgroup"
norecursedirs = "src"
Expand Down
14 changes: 8 additions & 6 deletions src/matchlab/models/linkers/splinklinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,23 @@ class SplinkLinker(Linker):

>>> from splink import SettingsCreator, block_on
... import splink.comparison_library as cl
... import splink.comparison_template_library as ctl
...
... splink_settings = SettingsCreator(
... link_type="link_only",
... retain_matching_columns=False,
... retain_intermediate_calculation_columns=False,
... blocking_rules_to_generate_predictions=[
... block_on("company_name"),
... block_on("postcode"),
... ],
... comparisons=[
... cl.jaro_winkler_at_thresholds(
... cl.JaroWinklerAtThresholds(
... "company_name",
... [0.9, 0.6],
... ).configure(
... term_frequency_adjustments=True
... ),
... ctl.postcode_comparison("postcode"),
... cl.PostcodeComparison("postcode"),
... ]
... )
"""
Expand Down Expand Up @@ -224,6 +225,9 @@ def prepare(self, left: pl.DataFrame, right: pl.DataFrame) -> None:
# Convert to pandas for Splink compatibility
left_pd = left.with_columns(pl.col(self.left_id).cast(pl.String)).to_pandas()
right_pd = right.with_columns(pl.col(self.right_id).cast(pl.String)).to_pandas()
db_api = DuckDBAPI()
left_table = db_api.register(left_pd, dataset_display_name="l")
right_table = db_api.register(right_pd, dataset_display_name="r")

# A copy, because building a Splink linker writes into the settings it is
# given: it stamps a random `linker_uid` onto them. Handed our own object,
Expand All @@ -232,10 +236,8 @@ def prepare(self, left: pl.DataFrame, right: pl.DataFrame) -> None:
# before. A step has to address the same artifact whether or not it has been
# run, and a random value could never be part of that address anyway.
self._linker = SplinkLibLinkerClass(
input_table_or_tables=[left_pd, right_pd],
input_table_aliases=["l", "r"],
splink_dataframe_or_dataframes=[left_table, right_table],
settings=deepcopy(self.linker_settings),
db_api=DuckDBAPI(),
)

for func in self.linker_training_functions:
Expand Down
14 changes: 4 additions & 10 deletions test/models/test_linkers_deterministic.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,14 @@ def configure_splink_linker(
if not shared_fields:
raise ValueError("Must have at least one shared field")

deterministic_matching_rules: list[str] = []
blocking_rules_to_generate_predictions: list[BlockingRuleCreator] = []
comparisons: list[ComparisonCreator] = []

for field in shared_fields:
deterministic_matching_rules.append(f"l.{field} = r.{field}")
blocking_rules_to_generate_predictions.append(brl.block_on(field))
comparisons.append(cl.ExactMatch(field).configure(m_probabilities=[1, 0]))
comparisons.append(cl.ExactMatch(field).configure(m_probabilities=[1, 1e-300]))

linker_training_functions = [
{
"function": "estimate_probability_two_random_records_match",
"arguments": {
"deterministic_matching_rules": deterministic_matching_rules,
"recall": 1,
},
},
{
"function": "estimate_u_using_random_sampling",
"arguments": {"max_pairs": 1e4},
Expand All @@ -178,8 +169,11 @@ def configure_splink_linker(
# maximisation, which many of these tests can't run, since several have only one
# shared field. A pinned m is fine here, since these tests only check raw
# match/no-match behaviour, not a calibrated probability.
# Splink 5 rejects zero m/u values, so use its probability floor for mismatches.
linker_settings = SettingsCreator(
link_type="link_only",
# Splink 5 cannot predict when the no-match fixture estimates a zero prior.
probability_two_random_records_match=0.01,
retain_matching_columns=False,
retain_intermediate_calculation_columns=False,
blocking_rules_to_generate_predictions=blocking_rules_to_generate_predictions,
Expand Down
Loading
Loading