Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ripgrep = "latest"
dprint = "latest"
uv = "0.9.30"
ruff = "0.15.0"
ty = "0.0.32"
ty = "0.0.44"

"aqua:j178/prek" = "latest"

Expand Down
12 changes: 12 additions & 0 deletions STYLE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def process(data: pd.DataFrame, columns: Sequence[str] | None = None) -> Self:
- `Protocol` for structural subtyping when you need duck-typing boundaries
- Avoid `Any` -- prefer `object`, generics, or `Protocol`
- `TYPE_CHECKING` guards for heavy imports (`pandas`, `torch`, `transformers`); not needed for stdlib or lightweight imports
- `TypeIs[T]` from `typing_extensions` for side-effect-free predicates that fully validate a value as `T`. Use `bool` for ordinary predicates that do not establish a type, and reserve `TypeGuard` for cases `TypeIs` cannot express because the narrowed type is not compatible with the input type.

```python
from typing_extensions import TypeIs


def is_json_object(value: object) -> TypeIs[dict[str, JsonValue]]:
return isinstance(value, dict) and all(
isinstance(key, str) and is_json_value(item)
for key, item in value.items()
)
```

Legacy modules (`pii_replacer/`) still use `Optional`/`List`/`Dict`. Only `pii_replacer/` is excluded from `ty` type-checking (see `[tool.ty.src] exclude` in `pyproject.toml`). The remaining legacy usages will be migrated when those modules come under the type checker.

Expand Down
14 changes: 7 additions & 7 deletions mise.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions src/nemo_safe_synthesizer/cli/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
logger = get_logger(__name__)


def _require_dataframe(value: object, *, url: str) -> pd.DataFrame:
if isinstance(value, pd.DataFrame):
return value
raise TypeError(f"Expected dataset reader for {url} to return a pandas DataFrame, got {type(value).__name__}")


def _dynamic_callable(value: object) -> Any:
return value
Comment thread
binaryaaron marked this conversation as resolved.
Resolved


class DatasetInfo(BaseModel):
"""Entry in the dataset registry."""

Expand Down Expand Up @@ -96,20 +106,22 @@ def fetch(self) -> pd.DataFrame:
logger.info(f"Reading dataset from {url}")

# Determine the file extension and appropriate reader
match Path(url).suffix.lstrip("."):
reader: Any
extension = Path(url).suffix.lstrip(".")
match extension:
case "csv" | "txt":
reader = pd.read_csv
reader = _dynamic_callable(pd.read_csv)
default_load_args: dict[str, Any] = {}
case "json":
reader = pd.read_json
reader = _dynamic_callable(pd.read_json)
default_load_args = {}
case "jsonl":
reader = pd.read_json
reader = _dynamic_callable(pd.read_json)
default_load_args = {"lines": True}
case "parquet":
reader = pd.read_parquet
reader = _dynamic_callable(pd.read_parquet)
default_load_args = {}
case extension:
case _:
if not extension:
extension = f"<no extension found on url '{url}'>"
raise ValueError(f"Unsupported file extension: {extension}")
Expand All @@ -118,7 +130,7 @@ def fetch(self) -> pd.DataFrame:
final_load_args = {**default_load_args, **(self.load_args or {})}

try:
return reader(url, **final_load_args) # ty: ignore[invalid-argument-type] -- reader union includes parquet which has stricter signature
return _require_dataframe(reader(url, **final_load_args), url=url)
except Exception as e:
logger.error(f"Error reading dataset from {url}: {e}", exc_info=True)
raise
Expand Down
4 changes: 2 additions & 2 deletions src/nemo_safe_synthesizer/data_processing/actions/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def fit_and_transform_dates(
names to ``{"format": ..., "min": ...}`` dicts needed by
``transform_dates`` for reversal.
"""
date_min_dict = {}
date_min_dict: dict[str, dict[str, str]] = {}
object_cols = [col for col, col_type in df.dtypes.items() if col_type == "object"]
result_df = df.copy() if not inplace else df
for object_col in object_cols:
Expand All @@ -428,7 +428,7 @@ def fit_and_transform_dates(
dates = pd.to_datetime(result_df[object_col], format=inferred_format)
min_date = dates.min()
result_df[object_col] = (dates - min_date).dt.total_seconds()
date_min_dict[object_col] = {
date_min_dict[str(object_col)] = {
"format": inferred_format,
"min": str(min_date),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from functools import partial
from typing import Annotated, Any, Literal, Optional, Union

import numpy as np
Expand Down Expand Up @@ -77,21 +76,20 @@ def _round_datetime(self, dt: datetime, precision: timedelta) -> datetime:
rounded_ts = round(dt.timestamp() / precision.total_seconds()) * precision.total_seconds()
return datetime.fromtimestamp(rounded_ts)

def _apply_universal_params(self, samples: list[datetime]) -> list[datetime]:
ret: list[str] | list[datetime] = samples

ops = []
if self.precision is not None:
ops.append(partial(self._round_datetime, self.precision))
def _apply_universal_params(self, samples: list[datetime]) -> list[datetime] | list[str]:
if self.format is not None:
ops.append(lambda x: x.strftime(self.format))

formatted: list[str] = []
for sample in samples:
if self.precision is not None:
sample = self._round_datetime(sample, self.precision)
formatted.append(sample.strftime(self.format))
return formatted

ret: list[datetime] = []
for sample in samples:
n = sample
for op in ops:
n = op(n)
ret.append(n)

if self.precision is not None:
sample = self._round_datetime(sample, self.precision)
ret.append(sample)
return ret


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,16 @@ def _aia(

# Get all combinations of columns to be the quasi-identifiers
# This gets explosive when column count > 500
training_columns = [str(column) for column in training_df.columns]
if len(training_df.columns) < 500:
qi_combos = list(itertools.combinations(training_df.columns, quasi_identifier_count))
qi_combos = list(itertools.combinations(training_columns, quasi_identifier_count))
else:
columns = list(training_df.columns)
qi_combos = []
for i in range(len(columns) - quasi_identifier_count):
combo = set()
for i in range(len(training_columns) - quasi_identifier_count):
combo = []
for j in range(quasi_identifier_count):
combo.add(columns[i + j])
qi_combos.append(combo)
combo.append(training_columns[i + j])
qi_combos.append(tuple(combo))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

np.random.seed(5)
np.random.shuffle(qi_combos)
Expand Down Expand Up @@ -415,7 +415,6 @@ def _aia(

# As we process the attack dataset, we'll accumulate for each column the number of
# correct and incorrect predictions
training_columns = [str(column) for column in training_df.columns]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

did this just need to be defined earlier?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the intent was to define training_columns earlier so the quasi-identifier combinations and the prediction counters use the same column identity instead of mixing original column objects with stringified names.

correct = {predict_column: 0 for predict_column in training_columns}
incorrect = {predict_column: 0 for predict_column in training_columns}

Expand Down Expand Up @@ -578,7 +577,8 @@ def _aia(
for i in range(len(entropy)):
entropy_wts.append(0)
else:
arr = (entropy - min(entropy)) / (max(entropy) - min(entropy))
entropy_arr = np.asarray(entropy, dtype=float)
arr = (entropy_arr - min(entropy)) / (max(entropy) - min(entropy))
entropy_wts = arr / arr.sum()

i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ def _compute_mia(
) -> tuple[
float,
list[str],
dict[str, list[int]],
dict[str, list[int]],
dict[float, int],
dict[float, int],
]:
"""Core membership inference attack implementation for a single run.

Expand Down Expand Up @@ -286,8 +286,8 @@ def _compute_mia(
pd.concat([training_df_attack, test_df_norm]).reset_index(drop=True).sample(frac=1, random_state=run)
)

attack_synth_dist_text = [[0] for i in range(len(attack_df))]
attack_synth_indices_text = [[0] for i in range(len(attack_df))]
attack_synth_dist_text: list[list[float]] = [[0.0] for i in range(len(attack_df))]
attack_synth_indices_text: list[list[int]] = [[0] for i in range(len(attack_df))]

# Get the NN dist for text for the entire attack dataset

Expand Down Expand Up @@ -362,8 +362,8 @@ def _compute_mia(
score = 0
attack_summary = []

tp_cnts = {}
fp_cnts = {}
tp_cnts: dict[float, int] = {}
fp_cnts: dict[float, int] = {}

# Using the above text and tabular distances we now compute an overall distance score for
# every record in the attack dataset. We then conduct 36 individual mia attacks on this one big
Expand Down Expand Up @@ -519,8 +519,8 @@ def mia(

scores = []
attack_sum_values = []
tps_values = {}
fps_values = {}
tps_values: dict[float, int] = {}
fps_values: dict[float, int] = {}
for i in [0.1, 0.2, 0.3, 0.4]:
tps_values[i] = 0
fps_values[i] = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,16 @@ def generate_text_structure_similarity_figures(
"average_words_per_sentence",
"average_characters_per_word",
]
figures = []
figures: list[go.Figure] = []
for key in statistics_keys:
if training_statistics.per_record_statistics.empty or synthetic_statistics.per_record_statistics.empty:
break
figure = histogram_figure(
training_statistics.per_record_statistics[key],
synthetic_statistics.per_record_statistics[key],
)
figures.append(figure)
if figure is not None:
figures.append(figure)
if not figures:
return None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ColumnDistribution,
ColumnDistributionPlotRow,
)
from ....evaluation.components.component import Component
from ....evaluation.components.correlation import (
Correlation,
)
Expand Down Expand Up @@ -150,7 +151,7 @@ def from_dataframes(
mandatory_columns=MultimodalReport._get_config_value("mandatory_columns", [], config),
)

components = []
components: list[Component] = []

attribute_inference_protection = AttributeInferenceProtection(
score=EvaluationScore(grade=PrivacyGrade.UNAVAILABLE)
Expand Down
23 changes: 18 additions & 5 deletions src/nemo_safe_synthesizer/privacy/dp_transformers/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
from opt_einsum import contract


def _contract_tensor(expression: str, *operands: torch.Tensor) -> torch.Tensor:
result = contract(expression, *operands)
if not isinstance(result, torch.Tensor):
raise TypeError("expected opt_einsum.contract to return a torch.Tensor for torch operands")
return result


def _linear_parameter(parameter: torch.Tensor) -> nn.Parameter:
if not isinstance(parameter, nn.Parameter):
raise TypeError("expected nn.Linear parameter")
return parameter


@register_grad_sampler(nn.Linear)
def compute_linear_grad_sample(
layer: nn.Linear, activations: list[torch.Tensor], backprops: torch.Tensor
Expand All @@ -41,10 +54,10 @@ def compute_linear_grad_sample(
per-sample gradient tensor of shape ``(batch, ...)``.
"""
activation = activations[0]
ret = {}
if layer.weight.requires_grad:
gs = contract("n...i,n...j->nij", backprops.float(), activation.float())
ret[layer.weight] = gs
ret: dict[nn.Parameter, torch.Tensor] = {}
weight = _linear_parameter(layer.weight)
if weight.requires_grad:
ret[weight] = _contract_tensor("n...i,n...j->nij", backprops.float(), activation.float())
if layer.bias is not None and layer.bias.requires_grad:
ret[layer.bias] = contract("n...k->nk", backprops.float())
ret[layer.bias] = _contract_tensor("n...k->nk", backprops.float())
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return ret
Loading
Loading