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.
Files changed (68) hide show
  1. {saiph-2.0.9 → saiph-2.0.10}/PKG-INFO +2 -1
  2. {saiph-2.0.9 → saiph-2.0.10}/pyproject.toml +2 -1
  3. {saiph-2.0.9 → saiph-2.0.10}/saiph/__init__.py +1 -1
  4. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd.py +23 -9
  5. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_test.py +108 -0
  6. {saiph-2.0.9 → saiph-2.0.10}/uv.lock +3 -1
  7. {saiph-2.0.9 → saiph-2.0.10}/.github/dependabot.yml +0 -0
  8. {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/pr-title.yml +0 -0
  9. {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/release.yml +0 -0
  10. {saiph-2.0.9 → saiph-2.0.10}/.github/workflows/saiph-ci.yml +0 -0
  11. {saiph-2.0.9 → saiph-2.0.10}/.gitignore +0 -0
  12. {saiph-2.0.9 → saiph-2.0.10}/.pre-commit-config.yaml +0 -0
  13. {saiph-2.0.9 → saiph-2.0.10}/.readthedocs.yaml +0 -0
  14. {saiph-2.0.9 → saiph-2.0.10}/.tool-versions +0 -0
  15. {saiph-2.0.9 → saiph-2.0.10}/CONTRIBUTING.md +0 -0
  16. {saiph-2.0.9 → saiph-2.0.10}/LICENSE +0 -0
  17. {saiph-2.0.9 → saiph-2.0.10}/README.md +0 -0
  18. {saiph-2.0.9 → saiph-2.0.10}/bin/create_csv.py +0 -0
  19. {saiph-2.0.9 → saiph-2.0.10}/docs/conf.py +0 -0
  20. {saiph-2.0.9 → saiph-2.0.10}/docs/index.rst +0 -0
  21. {saiph-2.0.9 → saiph-2.0.10}/docs/requirements.txt +0 -0
  22. {saiph-2.0.9 → saiph-2.0.10}/docs/source/example/README.rst +0 -0
  23. {saiph-2.0.9 → saiph-2.0.10}/docs/source/example/plot_example.py +0 -0
  24. {saiph-2.0.9 → saiph-2.0.10}/docs/source/reference.rst +0 -0
  25. {saiph-2.0.9 → saiph-2.0.10}/fixtures/iris.csv +0 -0
  26. {saiph-2.0.9 → saiph-2.0.10}/fixtures/wbcd.csv +0 -0
  27. {saiph-2.0.9 → saiph-2.0.10}/justfile +0 -0
  28. {saiph-2.0.9 → saiph-2.0.10}/release.py +0 -0
  29. {saiph-2.0.9 → saiph-2.0.10}/ruff.toml +0 -0
  30. {saiph-2.0.9 → saiph-2.0.10}/saiph/conftest.py +0 -0
  31. {saiph-2.0.9 → saiph-2.0.10}/saiph/contribution.py +0 -0
  32. {saiph-2.0.9 → saiph-2.0.10}/saiph/contribution_test.py +0 -0
  33. {saiph-2.0.9 → saiph-2.0.10}/saiph/exception.py +0 -0
  34. {saiph-2.0.9 → saiph-2.0.10}/saiph/inverse_transform.py +0 -0
  35. {saiph-2.0.9 → saiph-2.0.10}/saiph/inverse_transform_test.py +0 -0
  36. {saiph-2.0.9 → saiph-2.0.10}/saiph/lib/size.py +0 -0
  37. {saiph-2.0.9 → saiph-2.0.10}/saiph/models.py +0 -0
  38. {saiph-2.0.9 → saiph-2.0.10}/saiph/projection.py +0 -0
  39. {saiph-2.0.9 → saiph-2.0.10}/saiph/projection_test.py +0 -0
  40. {saiph-2.0.9 → saiph-2.0.10}/saiph/py.typed +0 -0
  41. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/__init__.py +0 -0
  42. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_sparse.py +0 -0
  43. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/famd_sparse_test.py +0 -0
  44. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/mca.py +0 -0
  45. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/mca_test.py +0 -0
  46. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/pca.py +0 -0
  47. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/pca_test.py +0 -0
  48. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/__init__.py +0 -0
  49. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/common.py +0 -0
  50. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/common_test.py +0 -0
  51. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/svd.py +0 -0
  52. {saiph-2.0.9 → saiph-2.0.10}/saiph/reduction/utils/svd_test.py +0 -0
  53. {saiph-2.0.9 → saiph-2.0.10}/saiph/serializer.py +0 -0
  54. {saiph-2.0.9 → saiph-2.0.10}/saiph/serializer_test.py +0 -0
  55. {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/__init_.py +0 -0
  56. {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/benchmark_test.py +0 -0
  57. {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/profile_cpu.py +0 -0
  58. {saiph-2.0.9 → saiph-2.0.10}/saiph/tests/profile_memory.py +0 -0
  59. {saiph-2.0.9 → saiph-2.0.10}/saiph/visualization.py +0 -0
  60. {saiph-2.0.9 → saiph-2.0.10}/setup.cfg +0 -0
  61. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/breast_cancer_wisconsin.csv +0 -0
  62. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/breast_cancer_wisconsin_supplemental.csv +0 -0
  63. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris.csv +0 -0
  64. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris_factomineR_contributions.csv +0 -0
  65. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/iris_factomineR_cos2.csv +0 -0
  66. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/wbcd_supplemental_coordinates_famd.csv +0 -0
  67. {saiph-2.0.9 → saiph-2.0.10}/tests/fixtures/wbcd_supplemental_coordinates_mca.csv +0 -0
  68. {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.9
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.9"
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]
@@ -2,7 +2,7 @@ from .inverse_transform import inverse_transform
2
2
  from .projection import fit, fit_transform, stats, transform
3
3
 
4
4
  # Also modify in pyproject.toml
5
- __version__ = "2.0.9"
5
+ __version__ = "2.0.10"
6
6
 
7
7
  __all__ = [
8
8
  "__version__",
@@ -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
- df_quali = pd.get_dummies(
254
- df[model.original_categorical].astype("category"),
255
- prefix_sep=DUMMIES_SEPARATOR,
256
- dtype=np.uint8,
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
- if mod not in df_quali:
263
- df_quali[mod] = 0
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.9"
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