Skip to content

Splink

matchlab.models.linkers.splinklinker

A linking methodology leveraging Splink.

Classes:

  • SplinkLinkerFunction

    A method of splink.Linker.training used to train the linker.

  • SplinkLinker

    A linker that leverages Bayesian record linkage using Splink.

Attributes:

DEFAULT_TRAINING_SEED module-attribute

DEFAULT_TRAINING_SEED = 0

SplinkLinkerFunction

Bases: BaseModel


              flowchart TD
              matchlab.models.linkers.splinklinker.SplinkLinkerFunction[SplinkLinkerFunction]

              

              click matchlab.models.linkers.splinklinker.SplinkLinkerFunction href "" "matchlab.models.linkers.splinklinker.SplinkLinkerFunction"
            

A method of splink.Linker.training used to train the linker.

Methods:

Attributes:

function instance-attribute

function: str

arguments instance-attribute

arguments: dict[str, Any]

validate_function_and_arguments

validate_function_and_arguments() -> SplinkLinkerFunction

Ensure the function and arguments are valid.

SplinkLinker

Bases: Linker


              flowchart TD
              matchlab.models.linkers.splinklinker.SplinkLinker[SplinkLinker]
              matchlab.models.linkers.base.Linker[Linker]

                              matchlab.models.linkers.base.Linker --> matchlab.models.linkers.splinklinker.SplinkLinker
                


              click matchlab.models.linkers.splinklinker.SplinkLinker href "" "matchlab.models.linkers.splinklinker.SplinkLinker"
              click matchlab.models.linkers.base.Linker href "" "matchlab.models.linkers.base.Linker"
            

A linker that leverages Bayesian record linkage using Splink.

Sampling training functions are seeded automatically with 0 when they accept a seed argument and none was provided, so repeated collections with the same settings stay deterministic and cache-safe.

Methods:

Attributes:

version class-attribute

version: int = 1

model_config class-attribute instance-attribute

model_config = ConfigDict(arbitrary_types_allowed=True)

linker_training_functions class-attribute instance-attribute

linker_training_functions: list[SplinkLinkerFunction] = Field(description='\n            A list of dictionaries where keys are the names of methods for\n            splink.Linker.training and values are dictionaries encoding the arguments of\n            those methods. Each function will be run in the order supplied.\n\n            Example:\n            \n                >>> linker_training_functions=[\n                ...     {\n                ...         "function": "estimate_probability_two_random_records_match",\n                ...         "arguments": {\n                ...             "deterministic_matching_rules": """\n                ...                 l.company_name = r.company_name\n                ...             """,\n                ...             "recall": 0.7,\n                ...         },\n                ...     },\n                ...     {\n                ...         "function": "estimate_u_using_random_sampling",\n                ...         "arguments": {"max_pairs": 1e6},\n                ...     }\n                ... ]\n            \n        ')

linker_settings class-attribute instance-attribute

linker_settings: SettingsCreator = Field(description='\n            A valid Splink SettingsCreator.\n\n            See Splink\'s documentation for a full description of available settings.\n            https://moj-analytical-services.github.io/splink/api_docs/settings_dict_guide.html\n\n            * link_type must be set to "link_only"\n            * unique_id_name is overridden to the value of left_id and right_id,\n                which must match\n\n            Example:\n\n                >>> from splink import SettingsCreator, block_on\n                ... import splink.comparison_library as cl\n                ... import splink.comparison_template_library as ctl\n                ... \n                ... splink_settings = SettingsCreator(\n                ...     retain_matching_columns=False,\n                ...     retain_intermediate_calculation_columns=False,\n                ...     blocking_rules_to_generate_predictions=[\n                ...         block_on("company_name"),\n                ...         block_on("postcode"),\n                ...     ],\n                ...     comparisons=[\n                ...         cl.jaro_winkler_at_thresholds(\n                ...             "company_name", \n                ...             [0.9, 0.6], \n                ...             term_frequency_adjustments=True\n                ...         ),\n                ...         ctl.postcode_comparison("postcode"), \n                ...     ]\n                ... )         \n        ')

threshold class-attribute instance-attribute

threshold: float | None = Field(default=None, description='\n            The score above which matches will be kept.\n\n            None is used to indicate no threshold.\n            \n            Inclusive, so a value of 1 will keep only exact matches across all \n            comparisons.\n        ', gt=0, le=1)

left_id class-attribute instance-attribute

left_id: Literal['id'] = Field(default='id', description='The unique ID field in the left data')

right_id class-attribute instance-attribute

right_id: Literal['id'] = Field(default='id', description='The unique ID field in the right data')
check_link_only() -> SplinkLinker

Ensure link_type is set to "link_only".

add_enforced_settings

add_enforced_settings() -> SplinkLinker

Ensure ID is the only field we link on.

load_linker_settings classmethod

load_linker_settings(value: str | SettingsCreator) -> SettingsCreator

Load serialised settings into SettingsCreator.

serialise_settings

serialise_settings(value: SettingsCreator, info: SerializationInfo) -> str

Convert Splink settings to string.

prepare

prepare(left: DataFrame, right: DataFrame) -> None

Build the Splink linker over left and right, and run its training functions.

Runs each function in settings.linker_training_functions, in order, against the built linker. link() then just predicts against the trained state.

link(left: DataFrame = None, right: DataFrame = None) -> DataFrame

Predict match scores using the linker trained in prepare().

left/right are accepted only to satisfy the Linker contract. The data was already fixed when prepare() built the underlying Splink linker, so passing values here logs a warning and has no effect.