Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster centralisation chart #2091

Closed
wants to merge 4 commits into from
Closed

Conversation

zslade
Copy link
Contributor

@zslade zslade commented Mar 23, 2024

Type of PR

  • BUG
  • FEAT
  • MAINT
  • DOC

Is your Pull Request linked to an existing Issue or Pull Request?

#1001

Give a brief description for the solution you have provided

Update: leaving as draft until more work done on applying graph metrics to linked justice data

Simple first version of chart for plotting cluster metrics (in this case cluster centralisation).
It is likely that this functionality will become more generic, e.g. generating plots for size, density and/or centralisation (linker.cluster_centralisation_chart -> linker.cluster_metrics_charts) so is currently non-public and unpolished.

I find the current naming convention for functions in charts.py a bit confusing because they are the same as the linker methods which call them (and I wonder whether this is best practice anyway(?)) so I have gone for _build_cluster_centralisation_chart() instead. If this naming choice makes more sense then maybe it can be adopted for the other charts as well, but happy to drop 'build' and follow suit if not.

Ideas for subsequent PRs include:

  • Get dataframe from cache if already exists
  • Make dataframe argument optional and provide default if not provided
  • Pass clustering threshold to display on chart
  • Error handling
  • Add tests
Reprex for Splink 4
import splink.comparison_library as cl
import splink.comparison_template_library as ctl
from splink import DuckDBAPI, Linker, SettingsCreator, block_on, splink_datasets

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

import pandas as pd
import altair as alt

df = splink_datasets.historical_50k

############# SPLINK MODEL ##############

settings = SettingsCreator(
    link_type="dedupe_only",
    blocking_rules_to_generate_predictions=[
        block_on("postcode_fake", "first_name"),
        block_on("first_name", "surname"),
        block_on("dob", "substr(postcode_fake,1,2)"),
        block_on("postcode_fake", "substr(dob,1,3)"),
        block_on("postcode_fake", "substr(dob,4,5)"),
    ],
    comparisons=[
        ctl.NameComparison("first_name").configure(term_frequency_adjustments=True),
        ctl.NameComparison("surname").configure(term_frequency_adjustments=True),
        cl.LevenshteinAtThresholds("postcode_fake"),
    ],
    retain_intermediate_calculation_columns=True,
)

db_api = DuckDBAPI()
linker = Linker(df, settings, database_api=db_api)

linker.estimate_u_using_random_sampling(target_rows=1e6)

linker.estimate_parameters_using_expectation_maximisation(
    block_on("first_name", "surname")
)

linker.estimate_parameters_using_expectation_maximisation(
    block_on("dob", "substr(postcode_fake, 1,3)")
)

df_predict = linker.predict()
df_clustered = linker.cluster_pairwise_predictions_at_threshold(df_predict, 0.95)


######### Generate graph metrics data for chart ##########

df_gm = linker.compute_graph_metrics(
    df_predict, df_clustered, threshold_match_probability=0.95
)

df_cm = df_gm.clusters

# test via linker
linker._cluster_centralisation_chart(df_cm)

PR Checklist

  • Added documentation for changes
  • Added feature to example notebooks or tutorial (if appropriate)
  • Added tests (if appropriate)
  • Updated CHANGELOG.md (if appropriate)
  • Made changes based off the latest version of Splink
  • Run the linter

@zslade zslade marked this pull request as ready for review March 25, 2024 09:33
@zslade zslade requested review from ADBond and samnlindsay and removed request for samnlindsay and ADBond March 25, 2024 09:33
@zslade zslade marked this pull request as draft April 10, 2024 10:32
@zslade zslade closed this Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant