fable-model 0.1.7__tar.gz → 0.2.1__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.
@@ -1,18 +1,17 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fable-model
3
- Version: 0.1.7
3
+ Version: 0.2.1
4
4
  Summary: Data models utilized in the FABLE ecosystem's HTTP-based PPRL service.
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
7
7
  Keywords: record linkage,privacy,bloom filter
8
8
  Author: Maximilian Jugl
9
- Requires-Python: >=3.10
9
+ Requires-Python: >=3.11
10
10
  Classifier: Development Status :: 4 - Beta
11
11
  Classifier: Intended Audience :: Developers
12
12
  Classifier: Intended Audience :: Science/Research
13
13
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
14
14
  Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.10
16
15
  Classifier: Programming Language :: Python :: 3.11
17
16
  Classifier: Programming Language :: Python :: 3.12
18
17
  Classifier: Programming Language :: Python :: 3.13
@@ -25,22 +24,32 @@ Description-Content-Type: text/markdown
25
24
  [![Python Versions](https://img.shields.io/pypi/pyversions/fable-model?cacheSeconds=0)](https://pypi.org/project/fable-model/)
26
25
  ![Code Coverage](https://img.shields.io/badge/Coverage-98%25-brightgreen.svg)
27
26
  [![License](https://img.shields.io/pypi/l/fable-model?cacheSeconds=0)](https://pypi.org/project/fable-model/)
27
+ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
28
28
 
29
29
  # FABLE Model
30
30
 
31
31
  This package contains model classes that are used in the FABLE (**F**ederated
32
- **A**nonymized **B**loom filter **L**inkage **E**ngine) ecosystem's PPRL service for validation purposes.
33
- They were developed with the intention of creating an HTTP-based service for Bloom filter-based record linkage.
34
- It includes models for the service's data transformation, masking and bit vector matching routines.
32
+ **A**nonymized **B**loom filter **L**inkage **E**ngine) ecosystem's services for validation purposes.
33
+ They were developed with the intention of creating HTTP-based services to perform Bloom filter-based record linkage in
34
+ a federated setting.
35
+ It includes models for data transformation, masking and bit vector matching routines that are used by the
36
+ [FABLE PPRL service](https://github.com/ul-mds/fable-pprl-service) as well as models used by the
37
+ [FABLE Broker service](https://github.com/ul-mds/fable-broker).
35
38
  Validation, serialization and deserialization are done using [Pydantic](https://docs.pydantic.dev/latest/).
36
39
  It is rare to use this package directly.
37
40
  Rather, it powers the functionalities of other packages.
38
41
 
39
- ## Data models
42
+ ## Installation
43
+
44
+ ```commandline
45
+ pip install fable-model
46
+ ```
47
+
48
+ ## PPRL service
40
49
 
41
50
  Models for entity pre-processing, masking and bit vector matching are exposed through this package.
42
51
  The following examples are taken from the test suites of the
43
- [FABLE PPRL service package](https://github.com/ul-mds/fable-pprl-service) and show additional
52
+ [PPRL service package](https://github.com/ul-mds/fable-pprl-service) and show additional
44
53
  validation steps in addition to the ones native to Pydantic.
45
54
 
46
55
  ### Entity transformation
@@ -296,12 +305,19 @@ _ = EntityMaskRequest(
296
305
  ### Bit vector matching
297
306
 
298
307
  ```python
299
- from fable_model import VectorMatchRequest, MatchConfig, SimilarityMeasure, BitVectorEntity
308
+ from fable_model import (
309
+ VectorMatchRequest,
310
+ MatchConfig,
311
+ SimilarityMeasure,
312
+ BitVectorEntity,
313
+ SimilarityAggregator,
314
+ )
300
315
 
301
316
  _ = VectorMatchRequest(
302
317
  config=MatchConfig(
303
- measure=SimilarityMeasure.jaccard,
304
- threshold=0.8
318
+ measures=[SimilarityMeasure.jaccard, SimilarityMeasure.cosine],
319
+ thresholds=0.8,
320
+ aggregator=SimilarityAggregator.avg,
305
321
  ),
306
322
  domain=[
307
323
  BitVectorEntity(
@@ -318,6 +334,12 @@ _ = VectorMatchRequest(
318
334
  )
319
335
  ```
320
336
 
337
+ ## Broker service
338
+
339
+ The `broker` module contains models used by the [Broker service](https://github.com/ul-mds/fable-broker) to validate
340
+ requests and responses to create, manage and delete matching sessions.
341
+ Follow the link to the repository of that service for further information.
342
+
321
343
  ## License
322
344
 
323
345
  MIT.
@@ -2,22 +2,32 @@
2
2
  [![Python Versions](https://img.shields.io/pypi/pyversions/fable-model?cacheSeconds=0)](https://pypi.org/project/fable-model/)
3
3
  ![Code Coverage](https://img.shields.io/badge/Coverage-98%25-brightgreen.svg)
4
4
  [![License](https://img.shields.io/pypi/l/fable-model?cacheSeconds=0)](https://pypi.org/project/fable-model/)
5
+ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)
5
6
 
6
7
  # FABLE Model
7
8
 
8
9
  This package contains model classes that are used in the FABLE (**F**ederated
9
- **A**nonymized **B**loom filter **L**inkage **E**ngine) ecosystem's PPRL service for validation purposes.
10
- They were developed with the intention of creating an HTTP-based service for Bloom filter-based record linkage.
11
- It includes models for the service's data transformation, masking and bit vector matching routines.
10
+ **A**nonymized **B**loom filter **L**inkage **E**ngine) ecosystem's services for validation purposes.
11
+ They were developed with the intention of creating HTTP-based services to perform Bloom filter-based record linkage in
12
+ a federated setting.
13
+ It includes models for data transformation, masking and bit vector matching routines that are used by the
14
+ [FABLE PPRL service](https://github.com/ul-mds/fable-pprl-service) as well as models used by the
15
+ [FABLE Broker service](https://github.com/ul-mds/fable-broker).
12
16
  Validation, serialization and deserialization are done using [Pydantic](https://docs.pydantic.dev/latest/).
13
17
  It is rare to use this package directly.
14
18
  Rather, it powers the functionalities of other packages.
15
19
 
16
- ## Data models
20
+ ## Installation
21
+
22
+ ```commandline
23
+ pip install fable-model
24
+ ```
25
+
26
+ ## PPRL service
17
27
 
18
28
  Models for entity pre-processing, masking and bit vector matching are exposed through this package.
19
29
  The following examples are taken from the test suites of the
20
- [FABLE PPRL service package](https://github.com/ul-mds/fable-pprl-service) and show additional
30
+ [PPRL service package](https://github.com/ul-mds/fable-pprl-service) and show additional
21
31
  validation steps in addition to the ones native to Pydantic.
22
32
 
23
33
  ### Entity transformation
@@ -273,12 +283,19 @@ _ = EntityMaskRequest(
273
283
  ### Bit vector matching
274
284
 
275
285
  ```python
276
- from fable_model import VectorMatchRequest, MatchConfig, SimilarityMeasure, BitVectorEntity
286
+ from fable_model import (
287
+ VectorMatchRequest,
288
+ MatchConfig,
289
+ SimilarityMeasure,
290
+ BitVectorEntity,
291
+ SimilarityAggregator,
292
+ )
277
293
 
278
294
  _ = VectorMatchRequest(
279
295
  config=MatchConfig(
280
- measure=SimilarityMeasure.jaccard,
281
- threshold=0.8
296
+ measures=[SimilarityMeasure.jaccard, SimilarityMeasure.cosine],
297
+ thresholds=0.8,
298
+ aggregator=SimilarityAggregator.avg,
282
299
  ),
283
300
  domain=[
284
301
  BitVectorEntity(
@@ -295,6 +312,12 @@ _ = VectorMatchRequest(
295
312
  )
296
313
  ```
297
314
 
315
+ ## Broker service
316
+
317
+ The `broker` module contains models used by the [Broker service](https://github.com/ul-mds/fable-broker) to validate
318
+ requests and responses to create, manage and delete matching sessions.
319
+ Follow the link to the repository of that service for further information.
320
+
298
321
  ## License
299
322
 
300
323
  MIT.
@@ -38,6 +38,7 @@ from .match import (
38
38
  Match,
39
39
  BaseMatchRequest,
40
40
  MatchMethod,
41
+ SimilarityAggregator,
41
42
  )
42
43
  from .transform import (
43
44
  EmptyValueHandling,
@@ -117,4 +118,5 @@ __all__ = [
117
118
  "BaseTransformRequest",
118
119
  "BaseMatchRequest",
119
120
  "MatchMethod",
121
+ "SimilarityAggregator",
120
122
  ]
@@ -0,0 +1,76 @@
1
+ from typing import Annotated
2
+
3
+ from pydantic import BaseModel, Field
4
+
5
+ from fable_model.common import BitVectorEntity
6
+ from fable_model.match import MatchConfig
7
+
8
+
9
+ class BaseSessionModel(BaseModel):
10
+ session: str
11
+
12
+
13
+ class BaseAuthenticatedSessionModel(BaseSessionModel):
14
+ token: str
15
+
16
+
17
+ class SessionCreationRequest(BaseSessionModel):
18
+ match_config: MatchConfig
19
+ expires_in: int = 3600
20
+
21
+
22
+ class SessionUpdateResponse(BaseSessionModel):
23
+ expires_at: int
24
+
25
+
26
+ class SessionCreationResponse(SessionUpdateResponse):
27
+ token: str
28
+
29
+
30
+ SessionDeletionRequest = BaseAuthenticatedSessionModel
31
+ SessionUpdateRequest = BaseAuthenticatedSessionModel
32
+
33
+
34
+ class BitVectorMetadata(BaseModel):
35
+ name: str
36
+ value: str
37
+
38
+
39
+ class MetaBitVectorEntity(BitVectorEntity):
40
+ metadata: list[BitVectorMetadata] = Field(default_factory=list)
41
+
42
+
43
+ class BaseClientModel(BaseSessionModel):
44
+ client: str
45
+
46
+
47
+ class ClientSubmissionRequest(BaseClientModel):
48
+ vectors: list[MetaBitVectorEntity] = Field(min_length=1)
49
+
50
+
51
+ class ClientVectorBatch(BaseModel):
52
+ client: str
53
+ ids: list[int]
54
+
55
+
56
+ class VectorMatchBatch(BaseModel):
57
+ domain: ClientVectorBatch
58
+ range: list[ClientVectorBatch]
59
+ session: str
60
+ config: MatchConfig
61
+
62
+
63
+ class ClientResultRequest(BaseClientModel):
64
+ show_unfinished_results: bool = False
65
+
66
+
67
+ class MatchedClientVector(BaseModel):
68
+ vector: MetaBitVectorEntity
69
+ similarities: Annotated[list[Annotated[float, Field(ge=0, le=1)]], Field(min_length=1)]
70
+ aggregated_similarity: Annotated[float, Field(ge=0, le=1)] | None
71
+ reference_metadata: list[BitVectorMetadata]
72
+
73
+
74
+ class ClientResultResponse(BaseModel):
75
+ finished: bool
76
+ matches: list[MatchedClientVector]
@@ -1,8 +1,7 @@
1
1
  from enum import Enum
2
- from typing import Literal, Union, Annotated
2
+ from typing import Literal, Union, Annotated, Self
3
3
 
4
- from pydantic import Field, conint, confloat, model_validator
5
- from typing_extensions import Self
4
+ from pydantic import Field, model_validator
6
5
 
7
6
  from fable_model.common import ParentModel, BitVectorEntity, AttributeValueEntity
8
7
 
@@ -58,19 +57,19 @@ class FilterType(str, Enum):
58
57
 
59
58
  class CLKFilter(ParentModel):
60
59
  type: Literal[FilterType.clk] = FilterType.clk
61
- filter_size: conint(gt=0)
62
- hash_values: conint(gt=0)
60
+ filter_size: Annotated[int, Field(gt=0)]
61
+ hash_values: Annotated[int, Field(gt=0)]
63
62
 
64
63
 
65
64
  class RBFFilter(ParentModel):
66
65
  type: Literal[FilterType.rbf] = FilterType.rbf
67
- hash_values: conint(gt=0)
66
+ hash_values: Annotated[int, Field(gt=0)]
68
67
  seed: int
69
68
 
70
69
 
71
70
  class CLKRBFFilter(ParentModel):
72
71
  type: Literal[FilterType.clkrbf] = FilterType.clkrbf
73
- hash_values: conint(gt=0)
72
+ hash_values: Annotated[int, Field(gt=0)]
74
73
 
75
74
 
76
75
  AnyFilter = Union[CLKFilter, RBFFilter, CLKRBFFilter]
@@ -100,7 +99,7 @@ class PermuteHardener(ParentModel):
100
99
 
101
100
  class RandomizedResponseHardener(ParentModel):
102
101
  name: Literal[Hardener.randomized_response] = Hardener.randomized_response
103
- probability: confloat(ge=0, le=1)
102
+ probability: Annotated[float, Field(ge=0, le=1)]
104
103
  seed: int
105
104
 
106
105
 
@@ -110,9 +109,9 @@ class Rule90Hardener(ParentModel):
110
109
 
111
110
  class RehashHardener(ParentModel):
112
111
  name: Literal[Hardener.rehash] = Hardener.rehash
113
- window_size: conint(gt=0, le=32)
114
- window_step: conint(gt=0)
115
- samples: conint(gt=0)
112
+ window_size: Annotated[int, Field(gt=0, le=32)]
113
+ window_step: Annotated[int, Field(gt=0)]
114
+ samples: Annotated[int, Field(gt=0)]
116
115
 
117
116
 
118
117
  AnyHardener = Union[
@@ -126,7 +125,7 @@ AnyHardener = Union[
126
125
 
127
126
 
128
127
  class MaskConfig(ParentModel):
129
- token_size: conint(gt=1)
128
+ token_size: Annotated[int, Field(gt=1)]
130
129
  hash: HashConfig
131
130
  prepend_attribute_name: Annotated[bool, Field(default=True)]
132
131
  filter: Annotated[AnyFilter, Field(discriminator="type")]
@@ -157,8 +156,8 @@ class StaticAttributeConfig(ParentModel):
157
156
  class WeightedAttributeConfig(ParentModel):
158
157
  attribute_name: str
159
158
  salt: AttributeSalt | None = Field(default=None)
160
- weight: confloat(gt=0)
161
- average_token_count: confloat(gt=0)
159
+ weight: Annotated[float, Field(gt=0)]
160
+ average_token_count: Annotated[float, Field(gt=0)]
162
161
 
163
162
 
164
163
  class BaseMaskRequest(ParentModel):
@@ -0,0 +1,91 @@
1
+ from enum import Enum
2
+ from typing import Annotated, Any, Self
3
+
4
+ from pydantic import Field, model_validator, field_validator
5
+
6
+ from fable_model.common import ParentModel, BitVectorEntity
7
+
8
+
9
+ class MatchMethod(str, Enum):
10
+ crosswise = "crosswise"
11
+ pairwise = "pairwise"
12
+
13
+
14
+ class SimilarityMeasure(str, Enum):
15
+ dice = "dice"
16
+ cosine = "cosine"
17
+ jaccard = "jaccard"
18
+ russell_rao = "russell_rao"
19
+ sokal_sneath = "sokal_sneath"
20
+ sokal_michener = "sokal_michener"
21
+ roger_tanimoto = "roger_tanimoto"
22
+
23
+
24
+ class SimilarityAggregator(str, Enum):
25
+ none = "none"
26
+ avg = "avg"
27
+ max = "max"
28
+ min = "min"
29
+
30
+
31
+ class MatchConfig(ParentModel):
32
+ measures: Annotated[list[SimilarityMeasure], Field(min_length=1)]
33
+ thresholds: Annotated[list[Annotated[float, Field(ge=0, le=1)]], Field(min_length=1)]
34
+ aggregator: SimilarityAggregator = SimilarityAggregator.none
35
+ aggregator_args: Annotated[dict[str, Any], Field(default_factory=dict)]
36
+ method: MatchMethod = MatchMethod.crosswise
37
+
38
+ @field_validator("measures", mode="before")
39
+ @classmethod
40
+ def cast_measures(cls, v):
41
+ if isinstance(v, str):
42
+ return [v]
43
+ return v
44
+
45
+ @field_validator("thresholds", mode="before")
46
+ @classmethod
47
+ def cast_thresholds(cls, v):
48
+ if isinstance(v, (float, int)):
49
+ return [v]
50
+ return v
51
+
52
+ @model_validator(mode="after")
53
+ def validate_measures_and_thresholds(self) -> Self:
54
+ if self.aggregator == SimilarityAggregator.none:
55
+ if len(self.measures) != len(self.thresholds):
56
+ raise ValueError(
57
+ "Without an aggregator, there must be as many similarity measures as there are thresholds."
58
+ )
59
+ else:
60
+ if len(self.thresholds) != 1:
61
+ raise ValueError("With an aggregator, there need to be exactly one threshold.")
62
+
63
+ return self
64
+
65
+
66
+ class BaseMatchRequest(ParentModel):
67
+ config: MatchConfig
68
+
69
+ def with_vectors(self, domain_lst: list[BitVectorEntity], range_lst: list[BitVectorEntity]) -> "VectorMatchRequest":
70
+ return VectorMatchRequest(
71
+ config=self.config,
72
+ domain=domain_lst,
73
+ range=range_lst,
74
+ )
75
+
76
+
77
+ class VectorMatchRequest(BaseMatchRequest):
78
+ domain: Annotated[list[BitVectorEntity], Field(min_length=1)]
79
+ range: Annotated[list[BitVectorEntity], Field(min_length=1)]
80
+
81
+
82
+ class Match(ParentModel):
83
+ domain: BitVectorEntity
84
+ range: BitVectorEntity
85
+ similarities: Annotated[list[Annotated[float, Field(ge=0, le=1)]], Field(min_length=1)]
86
+ aggregated_similarity: Annotated[float, Field(ge=0, le=1)] | None
87
+
88
+
89
+ class VectorMatchResponse(ParentModel):
90
+ config: MatchConfig
91
+ matches: list[Match]
@@ -1,8 +1,7 @@
1
1
  from enum import Enum
2
- from typing import Literal, Union, Annotated
2
+ from typing import Literal, Union, Annotated, Self
3
3
 
4
- from pydantic import Field, conint, model_validator
5
- from typing_extensions import Self
4
+ from pydantic import Field, model_validator
6
5
 
7
6
  from fable_model.common import ParentModel, AttributeValueEntity
8
7
 
@@ -50,7 +49,7 @@ class MappingTransformer(ParentModel):
50
49
 
51
50
  class NumberTransformer(ParentModel):
52
51
  name: Literal[Transformer.number] = Transformer.number
53
- decimal_places: conint(ge=0)
52
+ decimal_places: Annotated[int, Field(ge=0)]
54
53
 
55
54
 
56
55
  class PhoneticCodeAlgorithm(str, Enum):
@@ -1,13 +1,13 @@
1
1
  [project]
2
2
  name = "fable-model"
3
- version = "0.1.7"
3
+ version = "0.2.1"
4
4
  description = "Data models utilized in the FABLE ecosystem's HTTP-based PPRL service."
5
5
  authors = [
6
6
  {name = "Maximilian Jugl"},
7
7
  {name = "Paul Brassel", email = "paul.brassel@medizin.uni-leipzig.de"}
8
8
  ]
9
9
  readme = "README.md"
10
- requires-python = ">=3.10"
10
+ requires-python = ">=3.11"
11
11
  dependencies = [
12
12
  "pydantic (>=2.7.0,<3.0.0)"
13
13
  ]
@@ -18,7 +18,6 @@ classifiers = [
18
18
  "Intended Audience :: Science/Research",
19
19
  "Topic :: Software Development :: Libraries :: Python Modules",
20
20
  "Programming Language :: Python :: 3",
21
- "Programming Language :: Python :: 3.10",
22
21
  "Programming Language :: Python :: 3.11",
23
22
  "Programming Language :: Python :: 3.12",
24
23
  "Programming Language :: Python :: 3.13",
@@ -40,6 +39,11 @@ markers = [
40
39
  "integration: mark a test as an integration test."
41
40
  ]
42
41
 
42
+ [tool.coverage.run]
43
+ omit = [
44
+ "fable_model/broker.py",
45
+ ]
46
+
43
47
  [build-system]
44
48
  requires = ["poetry-core>=2.0.0,<3.0.0"]
45
49
  build-backend = "poetry.core.masonry.api"
@@ -1,50 +0,0 @@
1
- from enum import Enum
2
- from typing import Annotated
3
-
4
- from pydantic import confloat, Field
5
-
6
- from fable_model.common import ParentModel, BitVectorEntity
7
-
8
-
9
- class MatchMethod(str, Enum):
10
- crosswise = "crosswise"
11
- pairwise = "pairwise"
12
-
13
-
14
- class SimilarityMeasure(str, Enum):
15
- dice = "dice"
16
- cosine = "cosine"
17
- jaccard = "jaccard"
18
-
19
-
20
- class MatchConfig(ParentModel):
21
- measure: SimilarityMeasure
22
- threshold: confloat(ge=0, le=1)
23
- method: MatchMethod = MatchMethod.crosswise
24
-
25
-
26
- class BaseMatchRequest(ParentModel):
27
- config: MatchConfig
28
-
29
- def with_vectors(self, domain_lst: list[BitVectorEntity], range_lst: list[BitVectorEntity]) -> "VectorMatchRequest":
30
- return VectorMatchRequest(
31
- config=self.config,
32
- domain=domain_lst,
33
- range=range_lst,
34
- )
35
-
36
-
37
- class VectorMatchRequest(BaseMatchRequest):
38
- domain: Annotated[list[BitVectorEntity], Field(min_length=1)]
39
- range: Annotated[list[BitVectorEntity], Field(min_length=1)]
40
-
41
-
42
- class Match(ParentModel):
43
- domain: BitVectorEntity
44
- range: BitVectorEntity
45
- similarity: float
46
-
47
-
48
- class VectorMatchResponse(ParentModel):
49
- config: MatchConfig
50
- matches: list[Match]
File without changes