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

Refactor and simplify how TF adjustments are made in _find_new_matches_mode and _compare_two_records_mode #2111

Merged
merged 8 commits into from
Apr 3, 2024

Conversation

RobinL
Copy link
Member

@RobinL RobinL commented Mar 28, 2024

This is the first step in a PR to remove the need for:

  • linker._find_new_matches_mode
  • linker._compare_two_records_mode

Once I began working on this, I realised that to remove these flags, we first need to simplify the tf logic.

It also resolves a longstanding bug with linker.compare_two_records whereby it would only work if term frequency tables were present, see here

#802 is therefore solved in Splink 4.

What does this do?

  • In _find_new_matches_mode and _compare_two_records_mode new records are submitted by the user. Suppose term frequency adjustments are requested for the first_name column.
  • We want to compute tf columns such as tf_first_name for these records
  • If the relevant tf table (__splink__df_tf_first_name) is registered, then we can simply left join the table of new records
  • If not, where do tf adjustments come from? We can derive them using:
select 
  __splink__compare_two_records_left.*, 
  nodes_tf__0."tf_first_name" 
from 
  __splink__compare_two_records_left 
  left join (
    select 
      distinct "first_name", 
      "tf_first_name" 
    from 
      __splink__df_concat_with_tf
  ) as nodes_tf__0 on __splink__compare_two_records_left."first_name" = nodes_tf__0."first_name"
)

  • If __splink__df_concat_with_tf is not available, we simply create te table with no adjustments
select __splink__compare_two_records_left.*, null as "tf_first_name"
    from __splink__compare_two_records_left

What do we have to be careful about?

The compare_two_records function is more fiddly than it looks because...

... if the user just wants to compare two records, there's no guarantee they've provided Splink with a full input datasets. They might have just loaded in two records. If there is no full input dataset, we can't compute the tf tables. Instead, the user would have to provide them.

(See this comment)

This PR makes this possible:

linker = Linker([], settings, DuckDBAPI())
linker.compare_two_records(recs[0], recs[1]).as_pandas_dataframe()

i.e. we can use a trained model without needing to provide a big input dataset and computing __splink__df_concat_with_tf.

This makes real time scoring easier: It would be possible to do this in a live API:

tf_data_in = # data provided by caller of http API, e.g. grabbing tfs for the specific records from a prod postgres database
records in = # data provided by caller of http API
linker = Linker([], settings, DuckDBAPI())
linker.register_term_frequency_lookup(tf_data_in)
linker.compare_two_records(recs[0], recs[1]).as_pandas_dataframe()

Other notes

(You now get a warning as follows if the tf table doesn't exist. You still get prediction results, just without any tf adjustments)

No term frequencies found for column "first_name".
To apply term frequency adjustments, you need to register a lookup using `linker.register_term_frequency_lookup`.

A previous attempt at this was made here:
#1604

left_joins.append(sql)
elif "__splink__df_concat_with_tf" in cache:
subquery = f"""
select distinct {col.name}, {col.tf_name}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the logic that derives tf columns from __splink__df_concat_with_tf to here

This logic is no longer used in linker.compute_tf_table because it's rare to need to 'backwards induce' tf tables in this way, and it adds complexity for little computational gain (tf tables are fast to compute)

@@ -75,14 +75,58 @@ def _join_tf_to_input_df_sql(linker: Linker):
return sql


def term_frequencies_from_concat_with_tf(input_column):
def _join_new_table_to_df_concat_with_tf_sql(linker: Linker, new_tablename) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we were reusing logic in _join_tf_to_input_df_sql by performing string replacement operations on its outputs.

This was confusing. Also these two functions have diverged more with this PR, so it's no longer easy to have a single function

@@ -127,6 +127,7 @@ def enqueue_df_concat(linker: Linker, pipeline: CTEPipeline) -> CTEPipeline:
# so if it exists, use it instead
elif "__splink__df_concat_with_tf" in cache:
nodes_with_tf = cache.get_with_logging("__splink__df_concat_with_tf")
nodes_with_tf.templated_name = "__splink__df_concat"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a separate bug I found while doing this PR

@RobinL RobinL changed the title Refactor tf Refactor and simplify how TF adjustments are made in _find_new_matches_mode and _compare_two_records_mode Mar 28, 2024
@RobinL RobinL requested review from ThomasHepworth and ADBond and removed request for ThomasHepworth March 28, 2024 10:58
Copy link
Contributor

@ADBond ADBond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All seems sensible - looks good to me 👍

@RobinL RobinL merged commit c85c21b into splink4_dev Apr 3, 2024
11 checks passed
@RobinL RobinL deleted the get_tfs_by_joining_to_tf_concat branch April 3, 2024 06:13
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.

2 participants