Skip to content

Commit

Permalink
Merge branch 'KrishPatel13-feat/privacy_api2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Krish Patel committed Aug 17, 2023
2 parents ef5427b + 688fe0a commit 20963e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
16 changes: 8 additions & 8 deletions openadapt/privacy/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""The Base (Parent) Class for Privacy Scrubbing Providers"""
"""The Base (Parent) Class for Privacy Scrubbing Providers."""

from typing import List

from PIL import Image
from pydantic import BaseModel
from pydantic import BaseModel # pylint: disable=no-name-in-module

from openadapt import config


class Modality: # pylint: disable=too-few-public-methods
"""A Base Class for Modality Types"""
"""A Base Class for Modality Types."""

TEXT = "TEXT"
PIL_IMAGE = "PIL_IMAGE"
Expand All @@ -18,13 +18,13 @@ class Modality: # pylint: disable=too-few-public-methods


class ScrubbingProvider(BaseModel):
"""A Base Class for Scrubbing Providers"""
"""A Base Class for Scrubbing Providers."""

name: str
capabilities: List[str]

class Config: # pylint: disable=too-few-public-methods
"""Pydantic Config Class"""
"""Pydantic Config Class."""

arbitrary_types_allowed = True

Expand Down Expand Up @@ -74,7 +74,7 @@ def scrub_mp4( # pylint: disable=too-many-arguments
playback_speed_multiplier: float = 1.0,
crop_start_time: int = 0,
crop_end_time: int | None = None,
):
) -> str:
"""Scrub a mp4 file.
Args:
Expand All @@ -91,13 +91,13 @@ def scrub_mp4( # pylint: disable=too-many-arguments


class ScrubbingProviderFactory: # pylint: disable=too-few-public-methods
"""A Factory Class for Scrubbing Providers"""
"""A Factory Class for Scrubbing Providers."""

@staticmethod
def get_for_modality(
modality: Modality,
) -> List[ScrubbingProvider]:
"""Get Scrubbing Providers for a given Modality
"""Get Scrubbing Providers for a given Modality.
Args:
modality (Modality): Modality Type
Expand Down
7 changes: 3 additions & 4 deletions openadapt/privacy/providers/presidio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" A Module for Presidio Scrubbing Provider Class """
"""A Module for Presidio Scrubbing Provider Class."""

from typing import List

Expand All @@ -14,7 +14,7 @@


class PresidioScrubbingProvider(ScrubbingProvider): # pylint: disable=W0223
"""A Class for Presidio Scrubbing Provider"""
"""A Class for Presidio Scrubbing Provider."""

name: str = config.SCRUB_PROVIDER_NAME[0] # pylint: disable=E1101
capabilities: List[Modality] = [Modality.TEXT, Modality.PIL_IMAGE]
Expand Down Expand Up @@ -187,8 +187,7 @@ def scrub_dict(
def scrub_list_dicts(
self, input_list: list[dict], list_keys: list = None
) -> list[dict]:
"""Scrub list of dicts to remove PII/PHI
using Presidio ANALYZER.TRF and Anonymizer.
"""Scrub list of dicts to remove PII/PHI.
Args:
input_list (list[dict]): A list of dicts to be scrubbed
Expand Down
21 changes: 9 additions & 12 deletions tests/openadapt/privacy/providers/test_presidio_scrub.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
import warnings

from PIL import Image
import pytest

from openadapt import config, scrub
from openadapt import config
from openadapt.privacy.base import Modality, ScrubbingProviderFactory
from openadapt.privacy.providers.presidio import PresidioScrubbingProvider

scrub = PresidioScrubbingProvider()


def test_scrubbing_provider_factory():
"""Test the ScrubbingProviderFactory for Modality.TEXT in PresidioScrubbingProvider."""

def test_scrubbing_provider_factory() -> None:
"""Test the ScrubbingProviderFactory for Modality.TEXT."""
providers = ScrubbingProviderFactory.get_for_modality(Modality.TEXT)

# Ensure that we get at least one provider
Expand All @@ -31,9 +29,8 @@ def test_scrubbing_provider_factory():
assert Modality.TEXT in provider.capabilities


def test_presidio_scrub_text():
def test_presidio_scrub_text() -> None:
"""Test that PresidioScrubbingProvider can scrub text."""

text = "My phone number is 123-456-7890."
expected_result = "My phone number is <PHONE_NUMBER>."

Expand All @@ -52,10 +49,10 @@ def _hex_to_rgb(hex_color: int) -> tuple[int, int, int]:
tuple[int, int, int]: RGB values.
"""
assert 0x000000 <= hex_color <= 0xFFFFFF
b = (hex_color >> 16) & 0xFF
g = (hex_color >> 8) & 0xFF
r = hex_color & 0xFF
return r, g, b
blue = (hex_color >> 16) & 0xFF
green = (hex_color >> 8) & 0xFF
red = hex_color & 0xFF
return red, green, blue


def test_scrub_image() -> None:
Expand Down Expand Up @@ -85,7 +82,7 @@ def test_scrub_image() -> None:
mask_pixels = sum(
1
for pixel in scrubbed_image.getdata()
if pixel == _hex_to_rgb(config.SCRUB_FILL_COLOR)
if pixel == _hex_to_rgb(config.SCRUB_FILL_COLOR) # pylint: disable=no-member
)
total_pixels = scrubbed_image.width * scrubbed_image.height

Expand Down

0 comments on commit 20963e7

Please sign in to comment.