saiph 2.0.9__tar.gz → 2.0.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {saiph-2.0.9 → saiph-2.0.10}/PKG-INFO +2 -1
- {saiph-2.0.9 → saiph-2.0.10}/pyproject.toml +2 -1
- {saiph-2.0.9 → saiph-2.0.10}/saiph/__init__.py +1 -1
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd.py +23 -9
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_test.py +108 -0
- {saiph-2.0.9 → saiph-2.0.10}/uv.lock +3 -1
- {saiph-2.0.9 → saiph-2.0.10}/.github/dependabot.yml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/pr-title.yml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/release.yml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/saiph-ci.yml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.gitignore +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.pre-commit-config.yaml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.readthedocs.yaml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/.tool-versions +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/CONTRIBUTING.md +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/LICENSE +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/README.md +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/bin/create_csv.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/conf.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/index.rst +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/requirements.txt +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/source/example/README.rst +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/source/example/plot_example.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/docs/source/reference.rst +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/fixtures/iris.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/fixtures/wbcd.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/justfile +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/release.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/ruff.toml +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/conftest.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/contribution.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/contribution_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/exception.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/inverse_transform.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/inverse_transform_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/lib/size.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/models.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/projection.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/projection_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/py.typed +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/__init__.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_sparse.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_sparse_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/mca.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/mca_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/pca.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/pca_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/__init__.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/common.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/common_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/svd.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/svd_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/serializer.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/serializer_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/__init_.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/benchmark_test.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/profile_cpu.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/profile_memory.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/saiph/visualization.py +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/setup.cfg +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/breast_cancer_wisconsin.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/breast_cancer_wisconsin_supplemental.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris_factomineR_contributions.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris_factomineR_cos2.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/wbcd_supplemental_coordinates_famd.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/wbcd_supplemental_coordinates_mca.csv +0 -0
- {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/wbcd_supplemental_coordinates_pca.csv +0 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: saiph
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.10
|
|
4
4
|
Summary: A projection package
|
|
5
5
|
Author-email: Octopize <help@octopize.io>
|
|
6
6
|
License: Apache-2.0
|
|
7
7
|
License-File: LICENSE
|
|
8
8
|
Requires-Python: <3.14,>=3.11
|
|
9
|
+
Requires-Dist: matplotlib>=3.10.9
|
|
9
10
|
Requires-Dist: msgspec>=0.19
|
|
10
11
|
Requires-Dist: numpy>=1
|
|
11
12
|
Requires-Dist: pandas>=2.2.2
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "saiph"
|
|
3
3
|
# Also modify in saiph/__init__.py
|
|
4
|
-
version = "2.0.
|
|
4
|
+
version = "2.0.10"
|
|
5
5
|
description = "A projection package"
|
|
6
6
|
authors = [
|
|
7
7
|
{name = "Octopize", email = "help@octopize.io"}
|
|
@@ -16,6 +16,7 @@ dependencies = [
|
|
|
16
16
|
"toolz>=1",
|
|
17
17
|
"pydantic>=2",
|
|
18
18
|
"msgspec>=0.19",
|
|
19
|
+
"matplotlib>=3.10.9",
|
|
19
20
|
]
|
|
20
21
|
|
|
21
22
|
[project.optional-dependencies]
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""FAMD projection module."""
|
|
2
2
|
|
|
3
3
|
import sys
|
|
4
|
+
from collections import defaultdict
|
|
4
5
|
from collections.abc import Callable
|
|
5
6
|
from itertools import chain, repeat
|
|
6
7
|
from typing import Any, cast
|
|
@@ -250,17 +251,30 @@ def scaler(model: Model, df: pd.DataFrame) -> pd.DataFrame:
|
|
|
250
251
|
df_quanti = (df_quanti - model.mean) / std_without_zeros
|
|
251
252
|
|
|
252
253
|
# scale
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
# Here we add a column with 0 if the modality is not present in the dataset but
|
|
259
|
-
# was used to train the saiph model
|
|
254
|
+
# Attach fit-time categories to each column so that pd.get_dummies emits a
|
|
255
|
+
# column for every modality seen during fit — including those absent from
|
|
256
|
+
# this transform batch (they become all-zero columns automatically).
|
|
257
|
+
# This replaces a one-by-one loop that fragmented the DataFrame block
|
|
258
|
+
# manager and emitted a PerformanceWarning after 100+ insertions.
|
|
260
259
|
if model._modalities is not None:
|
|
260
|
+
col_cats: dict[str, list[str]] = defaultdict(list)
|
|
261
261
|
for mod in model._modalities:
|
|
262
|
-
|
|
263
|
-
|
|
262
|
+
col, _, val = mod.partition(DUMMIES_SEPARATOR)
|
|
263
|
+
col_cats[col].append(val)
|
|
264
|
+
|
|
265
|
+
df_quali = pd.get_dummies(
|
|
266
|
+
df[model.original_categorical].assign(
|
|
267
|
+
**{col: pd.Categorical(df[col], categories=cats) for col, cats in col_cats.items()}
|
|
268
|
+
),
|
|
269
|
+
prefix_sep=DUMMIES_SEPARATOR,
|
|
270
|
+
dtype=np.uint8,
|
|
271
|
+
)
|
|
272
|
+
else:
|
|
273
|
+
df_quali = pd.get_dummies(
|
|
274
|
+
df[model.original_categorical].astype("category"),
|
|
275
|
+
prefix_sep=DUMMIES_SEPARATOR,
|
|
276
|
+
dtype=np.uint8,
|
|
277
|
+
)
|
|
264
278
|
df_quali = df_quali[model._modalities]
|
|
265
279
|
df_quali = (df_quali - model.prop) / np.sqrt(model.prop)
|
|
266
280
|
|
|
@@ -290,3 +290,111 @@ def test_reconstructed_df_from_weighted_model_equals_df() -> None:
|
|
|
290
290
|
model = fit(df, col_weights=[3, 1, 1, 1, 1]) # type: ignore
|
|
291
291
|
reconstructed_df = reconstruct_df_from_model(model)
|
|
292
292
|
assert_frame_equal(df, reconstructed_df)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
# ---------------------------------------------------------------------------
|
|
296
|
+
# Absent / novel modality tests
|
|
297
|
+
# ---------------------------------------------------------------------------
|
|
298
|
+
# These three tests collectively protect the behaviour of scaler() under the
|
|
299
|
+
# conditions that actually occur in the avatar pipeline:
|
|
300
|
+
#
|
|
301
|
+
# • Privacy metrics fit on 50 % of rows, then transform the other 50 %
|
|
302
|
+
# (→ holdout may be missing categories seen only in the training half).
|
|
303
|
+
# • Avatar data is transformed with the original model after generation
|
|
304
|
+
# (→ rare categories may be absent from the avatar batch).
|
|
305
|
+
# • Cross-table linkage transforms a child table with a parent-table model
|
|
306
|
+
# (→ child may have categories never seen in the parent).
|
|
307
|
+
# ---------------------------------------------------------------------------
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def test_transform_famd_absent_categories_no_performance_warning() -> None:
|
|
311
|
+
"""FAMD (mixed continuous + categorical) must not emit PerformanceWarning
|
|
312
|
+
when >100 categories are absent from the transform batch.
|
|
313
|
+
|
|
314
|
+
The existing MCA-only test covers pure-categorical input (mca.py).
|
|
315
|
+
This test covers the mixed-type path through famd.scaler, which is the
|
|
316
|
+
real-world call site in the avatar quality-metrics pipeline.
|
|
317
|
+
|
|
318
|
+
pytest is configured to turn all warnings into errors, so this test
|
|
319
|
+
currently *fails* with the fragmentation loop and will pass once the
|
|
320
|
+
loop is replaced by a single vectorised operation.
|
|
321
|
+
"""
|
|
322
|
+
rng = np.random.default_rng(42)
|
|
323
|
+
n_categories = 150 # exceeds the pandas >100 block-fragmentation threshold
|
|
324
|
+
n_rows = n_categories * 4
|
|
325
|
+
|
|
326
|
+
df_train = pd.DataFrame(
|
|
327
|
+
{
|
|
328
|
+
"num": rng.normal(size=n_rows),
|
|
329
|
+
"cat": [f"val_{i}" for i in range(n_categories)] * 4,
|
|
330
|
+
}
|
|
331
|
+
)
|
|
332
|
+
_, model = fit_transform(df_train, nf=5)
|
|
333
|
+
|
|
334
|
+
# Only the first 50 categories are present → 100 of 150 are absent.
|
|
335
|
+
df_test = pd.DataFrame(
|
|
336
|
+
{
|
|
337
|
+
"num": rng.normal(size=50),
|
|
338
|
+
"cat": [f"val_{i}" for i in range(50)],
|
|
339
|
+
}
|
|
340
|
+
)
|
|
341
|
+
coord_test = transform(df_test, model)
|
|
342
|
+
|
|
343
|
+
assert coord_test.shape == (50, 5)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def test_transform_coordinates_independent_of_batch_composition() -> None:
|
|
347
|
+
"""Coordinates for a row must not change depending on which other categories
|
|
348
|
+
happen to appear in the same transform batch.
|
|
349
|
+
|
|
350
|
+
This verifies the mathematical invariant: absent categories are filled with
|
|
351
|
+
zero *before* centering and scaling, using the fitted model.prop values.
|
|
352
|
+
The result for a given row is therefore a pure function of that row’s own
|
|
353
|
+
values plus the fitted model, with no dependency on the batch.
|
|
354
|
+
"""
|
|
355
|
+
rng = np.random.default_rng(0)
|
|
356
|
+
n = 30
|
|
357
|
+
df_train = pd.DataFrame(
|
|
358
|
+
{
|
|
359
|
+
"num": rng.normal(size=n),
|
|
360
|
+
"cat": rng.choice(["a", "b", "c"], n),
|
|
361
|
+
}
|
|
362
|
+
)
|
|
363
|
+
_, model = fit_transform(df_train)
|
|
364
|
+
|
|
365
|
+
# Row under test: num=1.0, cat="a"
|
|
366
|
+
# Batch A: only category "a" present — "b" and "c" are absent.
|
|
367
|
+
coords_isolated = transform(pd.DataFrame({"num": [1.0], "cat": ["a"]}), model)
|
|
368
|
+
|
|
369
|
+
# Batch B: all three categories present — the target row is still num=1.0 / cat="a".
|
|
370
|
+
coords_mixed_batch = transform(
|
|
371
|
+
pd.DataFrame({"num": [1.0, 2.0, 3.0], "cat": ["a", "b", "c"]}), model
|
|
372
|
+
).iloc[[0]]
|
|
373
|
+
|
|
374
|
+
assert_frame_equal(
|
|
375
|
+
coords_isolated.reset_index(drop=True),
|
|
376
|
+
coords_mixed_batch.reset_index(drop=True),
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def test_transform_novel_category_not_seen_at_fit() -> None:
|
|
381
|
+
"""A category value that was not present during fit must be silently ignored.
|
|
382
|
+
|
|
383
|
+
Rows containing an unknown category are treated as if all model-known dummy
|
|
384
|
+
columns for that variable are zero — i.e., the same as an absent known
|
|
385
|
+
category. The transform must complete without error and return finite,
|
|
386
|
+
non-NaN coordinates.
|
|
387
|
+
"""
|
|
388
|
+
df_train = pd.DataFrame(
|
|
389
|
+
{
|
|
390
|
+
"num": [1.0, 2.0, 3.0, 4.0],
|
|
391
|
+
"cat": ["a", "a", "b", "b"],
|
|
392
|
+
}
|
|
393
|
+
)
|
|
394
|
+
_, model = fit_transform(df_train)
|
|
395
|
+
|
|
396
|
+
df_novel = pd.DataFrame({"num": [1.5], "cat": ["unseen_value"]})
|
|
397
|
+
coord = transform(df_novel, model)
|
|
398
|
+
|
|
399
|
+
assert coord.shape == (1, model.nf)
|
|
400
|
+
assert not coord.isnull().any().any()
|
|
@@ -2159,9 +2159,10 @@ wheels = [
|
|
|
2159
2159
|
|
|
2160
2160
|
[[package]]
|
|
2161
2161
|
name = "saiph"
|
|
2162
|
-
version = "2.0.
|
|
2162
|
+
version = "2.0.10"
|
|
2163
2163
|
source = { editable = "." }
|
|
2164
2164
|
dependencies = [
|
|
2165
|
+
{ name = "matplotlib" },
|
|
2165
2166
|
{ name = "msgspec" },
|
|
2166
2167
|
{ name = "numpy" },
|
|
2167
2168
|
{ name = "pandas" },
|
|
@@ -2201,6 +2202,7 @@ doc = [
|
|
|
2201
2202
|
|
|
2202
2203
|
[package.metadata]
|
|
2203
2204
|
requires-dist = [
|
|
2205
|
+
{ name = "matplotlib", specifier = ">=3.10.9" },
|
|
2204
2206
|
{ name = "matplotlib", marker = "extra == 'matplotlib'", specifier = ">=3.5.2" },
|
|
2205
2207
|
{ name = "msgspec", specifier = ">=0.19" },
|
|
2206
2208
|
{ name = "numpy", specifier = ">=1" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|