Skip to content

Commit

Permalink
Forest snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edongashi committed Nov 3, 2023
1 parent 9ef7e0f commit 3c43818
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 3 deletions.
5 changes: 4 additions & 1 deletion tests/clustering/test_stitching.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def build_rows(*cols: list[int]) -> list[MicrodataRow]:
(ColumnId(2), ColumnId(3)): build_rows(col_c_right, col_d),
}

metadata = StitchingMetadata(dimension_is_integral=[True, True, True], entropy_1dim=np.array([1.0, 2.0, 3.0]))
metadata = StitchingMetadata(
dimension_is_integral=[True, True, True, True],
entropy_1dim=np.array([1.0, 1.0, 1.0, 1.0]),
)

def materialize_tree(_forest: Forest, columns: list[ColumnId]) -> tuple[list[MicrodataRow], Combination]:
combination = tuple(sorted(columns))
Expand Down
16 changes: 14 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json
import os
from typing import Any

from syndiffix.common import *
from syndiffix.counters import UniqueAidCountersFactory
from syndiffix.forest import *
from syndiffix.microdata import apply_convertors, get_convertor
from syndiffix.tree import Branch, Leaf, Node

SALT = bytes([])
NOISELESS_SUPPRESSION = SuppressionParams(layer_sd=0.0)
Expand Down Expand Up @@ -32,6 +35,11 @@ def create_forest(
)


def _test_file_dir(filename: str) -> str:
current_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(current_dir, "data", filename)


def _load_csv(path: str, columns: list[str] | None) -> pd.DataFrame:
df = pd.read_csv(path, keep_default_na=False, na_values=[""], low_memory=False)
if columns is not None:
Expand All @@ -45,6 +53,10 @@ def load_forest(
anon_params: AnonymizationParams | None = None,
bucketization_params: BucketizationParams | None = None,
) -> Forest:
current_dir = os.path.dirname(os.path.abspath(__file__))
data_df = _load_csv(os.path.join(current_dir, "data", filename), columns)
data_df = _load_csv(_test_file_dir(filename), columns)
return create_forest(data_df, anon_params=anon_params, bucketization_params=bucketization_params)


def load_json(filename: str) -> Any:
with open(_test_file_dir(filename), "r", encoding="utf-8") as file:
return json.load(file)
82 changes: 82 additions & 0 deletions tests/data/tree.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"ranges": [[0.0, 8.0]],
"count": 32,
"children": {
"0": {
"ranges": [[0.0, 4.0]],
"count": 16,
"children": {
"0": {
"ranges": [[0.0, 2.0]],
"count": 8,
"children": {
"0": {
"ranges": [[0.0, 1.0]],
"count": 4,
"children": null
},
"1": {
"ranges": [[1.0, 2.0]],
"count": 4,
"children": null
}
}
},
"1": {
"ranges": [[2.0, 4.0]],
"count": 8,
"children": {
"0": {
"ranges": [[2.0, 3.0]],
"count": 4,
"children": null
},
"1": {
"ranges": [[3.0, 4.0]],
"count": 4,
"children": null
}
}
}
}
},
"1": {
"ranges": [[4.0, 8.0]],
"count": 16,
"children": {
"0": {
"ranges": [[4.0, 6.0]],
"count": 8,
"children": {
"0": {
"ranges": [[4.0, 5.0]],
"count": 4,
"children": null
},
"1": {
"ranges": [[5.0, 6.0]],
"count": 4,
"children": null
}
}
},
"1": {
"ranges": [[6.0, 8.0]],
"count": 8,
"children": {
"0": {
"ranges": [[6.0, 7.0]],
"count": 4,
"children": null
},
"1": {
"ranges": [[7.0, 8.0]],
"count": 4,
"children": null
}
}
}
}
}
}
}
109 changes: 109 additions & 0 deletions tests/data/tree.0_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"ranges": [
[0.0, 8.0],
[0.0, 4.0]
],
"count": 32,
"children": {
"0": {
"ranges": [
[0.0, 4.0],
[0.0, 2.0]
],
"count": 8,
"children": {
"0": {
"ranges": [
[0.0, 2.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"1": {
"ranges": [
[0.0, 2.0],
[1.0, 2.0]
],
"count": 4,
"children": null
}
}
},
"1": {
"ranges": [
[0.0, 4.0],
[2.0, 4.0]
],
"count": 8,
"children": {
"2": {
"ranges": [
[2.0, 4.0],
[2.0, 3.0]
],
"count": 4,
"children": null
},
"3": {
"ranges": [
[2.0, 4.0],
[3.0, 4.0]
],
"count": 4,
"children": null
}
}
},
"2": {
"ranges": [
[4.0, 8.0],
[0.0, 2.0]
],
"count": 8,
"children": {
"0": {
"ranges": [
[4.0, 6.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"1": {
"ranges": [
[4.0, 6.0],
[1.0, 2.0]
],
"count": 4,
"children": null
}
}
},
"3": {
"ranges": [
[4.0, 8.0],
[2.0, 4.0]
],
"count": 8,
"children": {
"2": {
"ranges": [
[6.0, 8.0],
[2.0, 3.0]
],
"count": 4,
"children": null
},
"3": {
"ranges": [
[6.0, 8.0],
[3.0, 4.0]
],
"count": 4,
"children": null
}
}
}
}
}
82 changes: 82 additions & 0 deletions tests/data/tree.0_1_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"ranges": [
[0.0, 8.0],
[0.0, 4.0],
[0.0, 2.0]
],
"count": 32,
"children": {
"0": {
"ranges": [
[0.0, 4.0],
[0.0, 2.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"1": {
"ranges": [
[0.0, 4.0],
[0.0, 2.0],
[1.0, 2.0]
],
"count": 4,
"children": null
},
"2": {
"ranges": [
[0.0, 4.0],
[2.0, 4.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"3": {
"ranges": [
[0.0, 4.0],
[2.0, 4.0],
[1.0, 2.0]
],
"count": 4,
"children": null
},
"4": {
"ranges": [
[4.0, 8.0],
[0.0, 2.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"5": {
"ranges": [
[4.0, 8.0],
[0.0, 2.0],
[1.0, 2.0]
],
"count": 4,
"children": null
},
"6": {
"ranges": [
[4.0, 8.0],
[2.0, 4.0],
[0.0, 1.0]
],
"count": 4,
"children": null
},
"7": {
"ranges": [
[4.0, 8.0],
[2.0, 4.0],
[1.0, 2.0]
],
"count": 4,
"children": null
}
}
}
38 changes: 38 additions & 0 deletions tests/data/tree.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"ranges": [[0.0, 4.0]],
"count": 32,
"children": {
"0": {
"ranges": [[0.0, 2.0]],
"count": 16,
"children": {
"0": {
"ranges": [[0.0, 1.0]],
"count": 8,
"children": null
},
"1": {
"ranges": [[1.0, 2.0]],
"count": 8,
"children": null
}
}
},
"1": {
"ranges": [[2.0, 4.0]],
"count": 16,
"children": {
"0": {
"ranges": [[2.0, 3.0]],
"count": 8,
"children": null
},
"1": {
"ranges": [[3.0, 4.0]],
"count": 8,
"children": null
}
}
}
}
}
16 changes: 16 additions & 0 deletions tests/data/tree.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ranges": [[0.0, 2.0]],
"count": 32,
"children": {
"0": {
"ranges": [[0.0, 1.0]],
"count": 16,
"children": null
},
"1": {
"ranges": [[1.0, 2.0]],
"count": 16,
"children": null
}
}
}
Loading

0 comments on commit 3c43818

Please sign in to comment.