Questions or feedback?

opendp.extras.ibis package#

Module contents#

This module requires extra installs: pip install 'opendp[ibis]'

For convenience, all the members of this module are also available from opendp.prelude. We suggest importing under the conventional name dp:

>>> import opendp.prelude as dp

The members of this module will then be accessible at dp.ibis.

Caution

This is an early release, and is intended primarily to solicit feedback. It supports only a limited set of polars expressions, and in the future the API may change.

opendp.extras.ibis.get_connection(database_name, **kwargs)[source]#

Returns a database connection object.

Parameters:
  • database_name (str) – A supported database type. “sqlite”, for example.

  • kwargs – Connection parameters.

opendp.extras.ibis.run_on_database(query, connection, table_name)[source]#

Translates the provided query to SQL, targets the given database connection and table, runs the query on the database, and returns the result.

Parameters:
  • query – A Polars query be translated to SQL.

  • table_name (str) – The name of the database table.

  • connection – A connection object.

Example:

>>> connection = get_connection('sqlite')
>>> table_name = 'demo'
>>> import polars as pl
>>> df = pl.DataFrame({"ints": [1, 2, 3, 4]})
>>> connection.create_table(table_name, df, overwrite=True)
DatabaseTable: demo
  ints int64

>>> schema_lf = scan_database(connection, table_name)

>>> context = dp.Context.compositor(
...     data=schema_lf,
...     privacy_unit=dp.unit_of(contributions=1),
...     privacy_loss=dp.loss_of(epsilon=1.0),
...     split_evenly_over=1,
...     margins=[
...         dp.polars.Margin(max_length=1_000_000),
...     ],
... )

>>> # TODO: Support signed=True
>>> # https://github.com/opendp/polars-to-ibis/issues/175
>>> query = context.query().select(dp.len(signed=False))

>>> # TODO: Silence warning upstream.
>>> # https://github.com/opendp/polars-to-ibis/issues/167
>>> import warnings
>>> with warnings.catch_warnings():
...     warnings.simplefilter("ignore")
...     result = run_on_database(query, connection, table_name)

>>> print("DP result:", result)
DP result: [...]
>>> connection.disconnect()
opendp.extras.ibis.scan_database(connection, table_name)[source]#

Given a database table, returns a polars schema.

Parameters:
  • connection – A connection object

  • table_name (str) – The name of the database table to scan