anomed_anonymizer.anonymizer_server

Module Contents

Classes

InferenceResource

This resource is intended for providing inference access to a fitted anonymizer (that follows the supervised learning paradigm) and to invoke its evaluation.

Functions

supervised_learning_anonymizer_server_factory

A factory to create a web application object which hosts an anonymizer.SupervisedLearningAnonymizer, currently the most basic use case of anonymizers (privacy preserving ML models) for the AnoMed competition platform.

tabular_data_anonymizer_server_factory

A factory to create a web application object which hosts an anonymizer.TabularDataAnonymizer, … TODO.

validate_anonymizer_input_or_raise

Validate the input for an anonymizer. If validation fails, raise a falcon.HTTPError instead.

API

class anomed_anonymizer.anonymizer_server.InferenceResource(anonymizer_identifier: str, model_filepath: str | pathlib.Path, model_loader: Callable[[str | pathlib.Path], anomed_anonymizer.anonymizer.SupervisedLearningAnonymizer], default_batch_size: int, tuning_data_url: str, validation_data_url: str, utility_evaluation_url: str, download_timeout: float, upload_timeout: float)

This resource is intended for providing inference access to a fitted anonymizer (that follows the supervised learning paradigm) and to invoke its evaluation.

Initialization

Parameters:
  • anonymizer_identifier (str) – The identifier of the anonymizer which will be loaded for inference or evaluation. This is required by the corresponding challenge’s evaluation API.

  • model_filepath (str | Path) – Where to load a fitted model from for inference (argument to model_loader).

  • model_loader (Callable[[str | Path], anonymizer.SupervisedLearningAnonymizer]) – A function that is able to recover an instance of anonymizer.SupervisedLearningAnonymizer from model_filepath. In case anonymizer_obj is a wrapped object, keep in mind that this loader has to recover the outer object too, not just the inner object.

  • default_batch_size (int) – A default batch size to use, if none is specified in the requests.

  • tuning_data_url (str) – Where to obtain tuning data from (usually, this points to an API of the challenge you are submitting your anonymizer to).

  • validation_data_url (str) – Where to obtain validation data from (usually, this points to an API of the challenge you are submitting your anonymizer to).

  • utility_evaluation_url (str) – Where to submit the target values this anonymizer inferred from the validation data features (usually, this points to an API of the challenge you are submitting your anonymizer to).

  • download_timeout (float, optional) – The time in seconds to wait before a download connection is considered faulty.

  • upload_timeout (float, optional) – The time in seconds to wait before an upload connection is considered faulty.

on_post_predict(req: falcon.Request, resp: falcon.Response) None
on_post_evaluate(req: falcon.Request, resp: falcon.Response) None
anomed_anonymizer.anonymizer_server.supervised_learning_anonymizer_server_factory(anonymizer_identifier: str, anonymizer_obj: anomed_anonymizer.anonymizer.SupervisedLearningAnonymizer, model_filepath: str | pathlib.Path, default_batch_size: int, training_data_url: str, tuning_data_url: str, validation_data_url: str, utility_evaluation_url: str, model_loader: Callable[[str | pathlib.Path], anomed_anonymizer.anonymizer.SupervisedLearningAnonymizer], download_timeout: float = 10.0, upload_timeout: float = 10.0) falcon.App

A factory to create a web application object which hosts an anonymizer.SupervisedLearningAnonymizer, currently the most basic use case of anonymizers (privacy preserving ML models) for the AnoMed competition platform.

By using this factory, you don’t have to worry any web-programming issues, as they are hidden from you. The generated web app will feature the following routes (more details may be found in this project’s openapi specification):

  • [GET] /

  • [POST] /fit

  • [POST] /evaluate

  • [POST] /predict

Parameters:
  • anonymizer_identifier (str) – Passed to InferenceResource.

  • anonymizer_obj (anonymizer.SupervisedLearningAnonymizer) – An anonymizer that is based on the supervised learning paradigm.

  • model_filepath (str | Path) – Where to save the fitted model after training / where to load it back from for inference.

  • default_batch_size (int) – Passed to InferenceResource.

  • training_data_url (str) – Where to obtain training data from (usually, this points to an API of the challenge you are submitting your anonymizer to).

  • tuning_data_url (str) – Passed to InferenceResource.

  • validation_data_url (str) – Passed to InferenceResource.

  • utility_evaluation_url (str) – Passed to InferenceResource.

  • model_loader (Callable[[str | Path], anonymizer.SupervisedLearningAnonymizer]) – Passed to InferenceResource.

  • download_timeout (float, optional) – The time in seconds to wait before a download connection is considered faulty. By default 10.0. You might want to increase this if you expect that it takes some time to download the training data from the challenge.

  • upload_timeout (float, optional) – The time in seconds to wait before an upload connection is considered faulty. By default 10.0. You might want to increase this if you expect that it takes some time to upload predictions to the challenge.

Returns:

A web application object based on the falcon web framework.

Return type:

falcon.App

anomed_anonymizer.anonymizer_server.tabular_data_anonymizer_server_factory(anonymizer_identifier: str, anonymizer_obj: anomed_anonymizer.anonymizer.TabularDataAnonymizer, output_dir: str | pathlib.Path, leaky_data_url: str, utility_evaluation_url: str, download_timeout: float = 10.0, upload_timeout: float = 10.0) falcon.App

A factory to create a web application object which hosts an anonymizer.TabularDataAnonymizer, … TODO.

By using this factory, you don’t have to worry any web-programming issues, as they are hidden from you. The generated web app will feature the following routes:

  • [GET] /

  • [POST] /fit

  • [POST] /evaluate

  • [GET] /anon_data

  • [GET] /anon_data_scheme

Parameters:
  • anonymizer_identifier (str) – The identifier for the

  • anonymizer_obj (anonymizer.TabularDataAnonymizer) – The tabular data anonymizer to lift to a web application.

  • leaky_data_url (str) – Where to obtain leaky data from (usually, this points to an API of the challenge you are submitting your anonymizer to).

  • utility_evaluation_url (str) – Where to send the anonymized data and the anonymization scheme to for utility evaluation.

  • download_timeout (float, optional) – The time in seconds to wait before a download connection is considered faulty. By default 10.0. You might want to increase this if you expect that it takes some time to download the training data from the challenge.

  • upload_timeout (float, optional) – The time in seconds to wait before an upload connection is considered faulty. By default 10.0. You might want to increase this if you expect that it takes some time to upload predictions to the challenge.

Returns:

A web application object based on the falcon web framework.

Return type:

falcon.App

anomed_anonymizer.anonymizer_server.validate_anonymizer_input_or_raise(feature_array: numpy.ndarray, anonymizer: anomed_anonymizer.anonymizer_server.validate_anonymizer_input_or_raise.anonymizer, error_status: str | int | None = falcon.HTTP_INTERNAL_SERVER_ERROR, error_message: str | None = None) None

Validate the input for an anonymizer. If validation fails, raise a falcon.HTTPError instead.

Parameters:
  • feature_array (np.ndarray) – A NumPy array containing the features for this anonymizer.

  • anonymizer (anonymizer.SupervisedLearningAnonymizer) – The anonymizer to validate input for. This function will use the anonymizer’s validate_input method.

  • error_status (str | int | None, optional) – The error status to use if validation fails. By default, falcon.HTTP_INTERNAL_SERVER_ERROR.

  • error_message (str | None, optional) – The error message to output. By default None, which will result in a generic message derived from the error_status.

Raises:

falcon.HTTPError – If validation fails.