phylogenie 2.0.5__tar.gz → 2.0.6__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 (30) hide show
  1. {phylogenie-2.0.5 → phylogenie-2.0.6}/PKG-INFO +2 -2
  2. {phylogenie-2.0.5 → phylogenie-2.0.6}/README.md +1 -1
  3. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/generators/alisim.py +4 -3
  4. phylogenie-2.0.6/phylogenie/generators/configs.py +39 -0
  5. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/generators/dataset.py +3 -3
  6. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/generators/factories.py +9 -9
  7. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/generators/trees.py +62 -48
  8. phylogenie-2.0.6/phylogenie/generators/typeguards.py +28 -0
  9. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/main.py +4 -0
  10. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/skyline/matrix.py +3 -3
  11. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/skyline/parameter.py +4 -4
  12. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/skyline/vector.py +7 -9
  13. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/tree.py +19 -10
  14. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/treesimulator/__init__.py +2 -0
  15. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/treesimulator/events.py +89 -73
  16. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/treesimulator/gillespie.py +29 -12
  17. phylogenie-2.0.6/phylogenie/treesimulator/model.py +154 -0
  18. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/typeguards.py +10 -10
  19. {phylogenie-2.0.5 → phylogenie-2.0.6}/pyproject.toml +1 -1
  20. phylogenie-2.0.5/phylogenie/generators/configs.py +0 -43
  21. phylogenie-2.0.5/phylogenie/generators/typeguards.py +0 -32
  22. phylogenie-2.0.5/phylogenie/treesimulator/model.py +0 -100
  23. {phylogenie-2.0.5 → phylogenie-2.0.6}/LICENSE.txt +0 -0
  24. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/__init__.py +0 -0
  25. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/generators/__init__.py +0 -0
  26. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/io.py +0 -0
  27. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/msa.py +0 -0
  28. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/py.typed +0 -0
  29. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/skyline/__init__.py +0 -0
  30. {phylogenie-2.0.5 → phylogenie-2.0.6}/phylogenie/typings.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: phylogenie
3
- Version: 2.0.5
3
+ Version: 2.0.6
4
4
  Summary: Generate phylogenetic datasets with minimal setup effort
5
5
  Author: Gabriele Marino
6
6
  Author-email: gabmarino.8601@gmail.com
@@ -45,7 +45,7 @@ Phylogenie comes packed with useful features, including:
45
45
  Simply specify the number of cores to use, and Phylogenie handles multiprocessing automatically.
46
46
 
47
47
  - **Pre-implemented parameterizations** 🎯
48
- Include canonical, fossilized birth-death, epidemiological, birth-death with exposed-infectious (BDEI), birth-death with superspreading (BDSS), and more.
48
+ Include canonical, fossilized birth-death, epidemiological, birth-death with exposed-infectious (BDEI), birth-death with superspreading (BDSS), and contact tracing (CT).
49
49
 
50
50
  - **Skyline parameter support** 🪜
51
51
  Support for piece-wise constant parameters.
@@ -27,7 +27,7 @@ Phylogenie comes packed with useful features, including:
27
27
  Simply specify the number of cores to use, and Phylogenie handles multiprocessing automatically.
28
28
 
29
29
  - **Pre-implemented parameterizations** 🎯
30
- Include canonical, fossilized birth-death, epidemiological, birth-death with exposed-infectious (BDEI), birth-death with superspreading (BDSS), and more.
30
+ Include canonical, fossilized birth-death, epidemiological, birth-death with exposed-infectious (BDEI), birth-death with superspreading (BDSS), and contact tracing (CT).
31
31
 
32
32
  - **Skyline parameter support** 🪜
33
33
  Support for piece-wise constant parameters.
@@ -46,11 +46,12 @@ class AliSimDatasetGenerator(DatasetGenerator):
46
46
  self, filename: str, rng: Generator, data: dict[str, Any]
47
47
  ) -> None:
48
48
  if self.keep_trees:
49
- base_dir = Path(filename).parent
50
- file_id = Path(filename).stem
51
- tree_filename = os.path.join(base_dir, TREES_DIRNAME, file_id)
49
+ base_dir, file_id = Path(filename).parent, Path(filename).stem
50
+ trees_dir = os.path.join(base_dir, TREES_DIRNAME)
52
51
  msas_dir = os.path.join(base_dir, MSAS_DIRNAME)
52
+ os.makedirs(trees_dir, exist_ok=True)
53
53
  os.makedirs(msas_dir, exist_ok=True)
54
+ tree_filename = os.path.join(trees_dir, file_id)
54
55
  msa_filename = os.path.join(msas_dir, file_id)
55
56
  else:
56
57
  tree_filename = f"{filename}.temp-tree"
@@ -0,0 +1,39 @@
1
+ from pydantic import BaseModel, ConfigDict
2
+
3
+ import phylogenie.typings as pgt
4
+
5
+
6
+ class Distribution(BaseModel):
7
+ type: str
8
+ model_config = ConfigDict(extra="allow")
9
+
10
+
11
+ Integer = str | int
12
+ Scalar = str | pgt.Scalar
13
+ ManyScalars = str | list[Scalar]
14
+ OneOrManyScalars = Scalar | list[Scalar]
15
+ OneOrMany2DScalars = Scalar | list[list[Scalar]]
16
+
17
+
18
+ class StrictBaseModel(BaseModel):
19
+ model_config = ConfigDict(extra="forbid")
20
+
21
+
22
+ class SkylineParameterModel(StrictBaseModel):
23
+ value: ManyScalars
24
+ change_times: ManyScalars
25
+
26
+
27
+ class SkylineVectorModel(StrictBaseModel):
28
+ value: str | list[OneOrManyScalars]
29
+ change_times: ManyScalars
30
+
31
+
32
+ class SkylineMatrixModel(StrictBaseModel):
33
+ value: str | list[OneOrMany2DScalars]
34
+ change_times: ManyScalars
35
+
36
+
37
+ SkylineParameter = Scalar | SkylineParameterModel
38
+ SkylineVector = str | pgt.Scalar | list[SkylineParameter] | SkylineVectorModel
39
+ SkylineMatrix = str | pgt.Scalar | list[SkylineVector] | SkylineMatrixModel | None
@@ -10,7 +10,7 @@ import pandas as pd
10
10
  from numpy.random import Generator, default_rng
11
11
  from tqdm import tqdm
12
12
 
13
- from phylogenie.generators.configs import DistributionConfig, StrictBaseModel
13
+ import phylogenie.generators.configs as cfg
14
14
 
15
15
 
16
16
  class DataType(str, Enum):
@@ -22,12 +22,12 @@ DATA_DIRNAME = "data"
22
22
  METADATA_FILENAME = "metadata.csv"
23
23
 
24
24
 
25
- class DatasetGenerator(ABC, StrictBaseModel):
25
+ class DatasetGenerator(ABC, cfg.StrictBaseModel):
26
26
  output_dir: str = "phylogenie-outputs"
27
27
  n_samples: int | dict[str, int] = 1
28
28
  n_jobs: int = -1
29
29
  seed: int | None = None
30
- context: dict[str, DistributionConfig] | None = None
30
+ context: dict[str, cfg.Distribution] | None = None
31
31
 
32
32
  @abstractmethod
33
33
  def _generate_one(
@@ -29,7 +29,7 @@ def _eval_expression(expression: str, data: dict[str, Any]) -> Any:
29
29
  ).tolist()
30
30
 
31
31
 
32
- def integer(x: cfg.IntegerConfig, data: dict[str, Any]) -> int:
32
+ def integer(x: cfg.Integer, data: dict[str, Any]) -> int:
33
33
  if isinstance(x, str):
34
34
  e = _eval_expression(x, data)
35
35
  if isinstance(e, int):
@@ -40,7 +40,7 @@ def integer(x: cfg.IntegerConfig, data: dict[str, Any]) -> int:
40
40
  return x
41
41
 
42
42
 
43
- def scalar(x: cfg.ScalarConfig, data: dict[str, Any]) -> pgt.Scalar:
43
+ def scalar(x: cfg.Scalar, data: dict[str, Any]) -> pgt.Scalar:
44
44
  if isinstance(x, str):
45
45
  e = _eval_expression(x, data)
46
46
  if isinstance(e, pgt.Scalar):
@@ -51,7 +51,7 @@ def scalar(x: cfg.ScalarConfig, data: dict[str, Any]) -> pgt.Scalar:
51
51
  return x
52
52
 
53
53
 
54
- def many_scalars(x: cfg.ManyScalarsConfig, data: dict[str, Any]) -> pgt.ManyScalars:
54
+ def many_scalars(x: cfg.ManyScalars, data: dict[str, Any]) -> pgt.ManyScalars:
55
55
  if isinstance(x, str):
56
56
  e = _eval_expression(x, data)
57
57
  if tg.is_many_scalars(e):
@@ -63,7 +63,7 @@ def many_scalars(x: cfg.ManyScalarsConfig, data: dict[str, Any]) -> pgt.ManyScal
63
63
 
64
64
 
65
65
  def one_or_many_scalars(
66
- x: cfg.OneOrManyScalarsConfig, data: dict[str, Any]
66
+ x: cfg.OneOrManyScalars, data: dict[str, Any]
67
67
  ) -> pgt.OneOrManyScalars:
68
68
  if isinstance(x, str):
69
69
  e = _eval_expression(x, data)
@@ -78,9 +78,9 @@ def one_or_many_scalars(
78
78
 
79
79
 
80
80
  def skyline_parameter(
81
- x: cfg.SkylineParameterConfig, data: dict[str, Any]
81
+ x: cfg.SkylineParameter, data: dict[str, Any]
82
82
  ) -> SkylineParameterLike:
83
- if isinstance(x, cfg.ScalarConfig):
83
+ if isinstance(x, cfg.Scalar):
84
84
  return scalar(x, data)
85
85
  return SkylineParameter(
86
86
  value=many_scalars(x.value, data),
@@ -89,7 +89,7 @@ def skyline_parameter(
89
89
 
90
90
 
91
91
  def skyline_vector(
92
- x: cfg.SkylineVectorConfig, data: dict[str, Any]
92
+ x: cfg.SkylineVector, data: dict[str, Any]
93
93
  ) -> SkylineVectorCoercible:
94
94
  if isinstance(x, str):
95
95
  e = _eval_expression(x, data)
@@ -132,7 +132,7 @@ def skyline_vector(
132
132
 
133
133
 
134
134
  def one_or_many_2D_scalars(
135
- x: cfg.OneOrMany2DScalarsConfig, data: dict[str, Any]
135
+ x: cfg.OneOrMany2DScalars, data: dict[str, Any]
136
136
  ) -> pgt.OneOrMany2DScalars:
137
137
  if isinstance(x, str):
138
138
  e = _eval_expression(x, data)
@@ -147,7 +147,7 @@ def one_or_many_2D_scalars(
147
147
 
148
148
 
149
149
  def skyline_matrix(
150
- x: cfg.SkylineMatrixConfig, data: dict[str, Any]
150
+ x: cfg.SkylineMatrix, data: dict[str, Any]
151
151
  ) -> SkylineMatrixCoercible | None:
152
152
  if x is None:
153
153
  return None
@@ -40,15 +40,20 @@ class ParameterizationType(str, Enum):
40
40
 
41
41
  class TreeDatasetGenerator(DatasetGenerator):
42
42
  data_type: Literal[DataType.TREES] = DataType.TREES
43
- min_tips: cfg.IntegerConfig = 1
44
- max_tips: cfg.IntegerConfig | None = None
45
- max_time: cfg.ScalarConfig = np.inf
43
+ min_tips: cfg.Integer = 1
44
+ max_tips: cfg.Integer | None = None
45
+ max_time: cfg.Scalar = np.inf
46
46
  init_state: str | None = None
47
- sampling_probability_at_present: cfg.ScalarConfig = 0.0
47
+ sampling_probability_at_present: cfg.Scalar = 0.0
48
48
  max_tries: int | None = None
49
+ notification_probability: cfg.Scalar = 0.0
50
+ max_notified_contacts: cfg.Integer = 1
51
+ samplable_states_after_notification: list[str] | None = None
52
+ sampling_rate_after_notification: cfg.SkylineParameter = np.inf
53
+ contacts_removal_probability: cfg.SkylineParameter = 1
49
54
 
50
55
  def simulate_one(self, rng: Generator, data: dict[str, Any]) -> Tree | None:
51
- events = self._get_events(rng, data)
56
+ events = self._get_events(data)
52
57
  init_state = (
53
58
  self.init_state
54
59
  if self.init_state is None
@@ -66,12 +71,21 @@ class TreeDatasetGenerator(DatasetGenerator):
66
71
  sampling_probability_at_present=scalar(
67
72
  self.sampling_probability_at_present, data
68
73
  ),
74
+ notification_probability=scalar(self.notification_probability, data),
75
+ max_notified_contacts=integer(self.max_notified_contacts, data),
76
+ samplable_states_after_notification=self.samplable_states_after_notification,
77
+ sampling_rate_after_notification=skyline_parameter(
78
+ self.sampling_rate_after_notification, data
79
+ ),
80
+ contacts_removal_probability=skyline_parameter(
81
+ self.contacts_removal_probability, data
82
+ ),
69
83
  max_tries=self.max_tries,
70
84
  seed=int(rng.integers(2**32)),
71
85
  )
72
86
 
73
87
  @abstractmethod
74
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]: ...
88
+ def _get_events(self, data: dict[str, Any]) -> list[Event]: ...
75
89
 
76
90
  def _generate_one(
77
91
  self, filename: str, rng: Generator, data: dict[str, Any]
@@ -85,15 +99,15 @@ class CanonicalTreeDatasetGenerator(TreeDatasetGenerator):
85
99
  parameterization: Literal[ParameterizationType.CANONICAL] = (
86
100
  ParameterizationType.CANONICAL
87
101
  )
88
- sampling_rates: cfg.SkylineVectorConfig
89
- birth_rates: cfg.SkylineVectorConfig = 0
90
- death_rates: cfg.SkylineVectorConfig = 0
91
- removal_probabilities: cfg.SkylineVectorConfig = 0
92
- migration_rates: cfg.SkylineMatrixConfig = None
93
- birth_rates_among_states: cfg.SkylineMatrixConfig = None
94
- states: list[str] | None = None
95
-
96
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
102
+ states: list[str]
103
+ sampling_rates: cfg.SkylineVector
104
+ birth_rates: cfg.SkylineVector = 0
105
+ death_rates: cfg.SkylineVector = 0
106
+ removal_probabilities: cfg.SkylineVector = 0
107
+ migration_rates: cfg.SkylineMatrix = None
108
+ birth_rates_among_states: cfg.SkylineMatrix = None
109
+
110
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
97
111
  return get_canonical_events(
98
112
  states=self.states,
99
113
  sampling_rates=skyline_vector(self.sampling_rates, data),
@@ -111,15 +125,15 @@ class EpidemiologicalTreeDatasetGenerator(TreeDatasetGenerator):
111
125
  parameterization: Literal[ParameterizationType.EPIDEMIOLOGICAL] = (
112
126
  ParameterizationType.EPIDEMIOLOGICAL
113
127
  )
114
- states: list[str] | None = None
115
- reproduction_numbers: cfg.SkylineVectorConfig = 0
116
- become_uninfectious_rates: cfg.SkylineVectorConfig = 0
117
- sampling_proportions: cfg.SkylineVectorConfig = 1
118
- removal_probabilities: cfg.SkylineVectorConfig = 1
119
- migration_rates: cfg.SkylineMatrixConfig = None
120
- reproduction_numbers_among_states: cfg.SkylineMatrixConfig = None
121
-
122
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
128
+ states: list[str]
129
+ reproduction_numbers: cfg.SkylineVector = 0
130
+ become_uninfectious_rates: cfg.SkylineVector = 0
131
+ sampling_proportions: cfg.SkylineVector = 1
132
+ removal_probabilities: cfg.SkylineVector = 1
133
+ migration_rates: cfg.SkylineMatrix = None
134
+ reproduction_numbers_among_states: cfg.SkylineMatrix = None
135
+
136
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
123
137
  return get_epidemiological_events(
124
138
  states=self.states,
125
139
  reproduction_numbers=skyline_vector(self.reproduction_numbers, data),
@@ -137,15 +151,15 @@ class EpidemiologicalTreeDatasetGenerator(TreeDatasetGenerator):
137
151
 
138
152
  class FBDTreeDatasetGenerator(TreeDatasetGenerator):
139
153
  parameterization: Literal[ParameterizationType.FBD] = ParameterizationType.FBD
140
- states: list[str] | None = None
141
- diversification: cfg.SkylineVectorConfig = 0
142
- turnover: cfg.SkylineVectorConfig = 0
143
- sampling_proportions: cfg.SkylineVectorConfig = 1
144
- removal_probabilities: cfg.SkylineVectorConfig = 0
145
- migration_rates: cfg.SkylineMatrixConfig = None
146
- diversification_between_types: cfg.SkylineMatrixConfig = None
147
-
148
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
154
+ states: list[str]
155
+ diversification: cfg.SkylineVector = 0
156
+ turnover: cfg.SkylineVector = 0
157
+ sampling_proportions: cfg.SkylineVector = 1
158
+ removal_probabilities: cfg.SkylineVector = 0
159
+ migration_rates: cfg.SkylineMatrix = None
160
+ diversification_between_types: cfg.SkylineMatrix = None
161
+
162
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
149
163
  return get_FBD_events(
150
164
  states=self.states,
151
165
  diversification=skyline_vector(self.diversification, data),
@@ -161,11 +175,11 @@ class FBDTreeDatasetGenerator(TreeDatasetGenerator):
161
175
 
162
176
  class BDTreeDatasetGenerator(TreeDatasetGenerator):
163
177
  parameterization: Literal[ParameterizationType.BD] = ParameterizationType.BD
164
- reproduction_number: cfg.SkylineParameterConfig
165
- infectious_period: cfg.SkylineParameterConfig
166
- sampling_proportion: cfg.SkylineParameterConfig = 1
178
+ reproduction_number: cfg.SkylineParameter
179
+ infectious_period: cfg.SkylineParameter
180
+ sampling_proportion: cfg.SkylineParameter = 1
167
181
 
168
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
182
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
169
183
  return get_BD_events(
170
184
  reproduction_number=skyline_parameter(self.reproduction_number, data),
171
185
  infectious_period=skyline_parameter(self.infectious_period, data),
@@ -175,12 +189,12 @@ class BDTreeDatasetGenerator(TreeDatasetGenerator):
175
189
 
176
190
  class BDEITreeDatasetGenerator(TreeDatasetGenerator):
177
191
  parameterization: Literal[ParameterizationType.BDEI] = ParameterizationType.BDEI
178
- reproduction_number: cfg.SkylineParameterConfig
179
- infectious_period: cfg.SkylineParameterConfig
180
- incubation_period: cfg.SkylineParameterConfig
181
- sampling_proportion: cfg.SkylineParameterConfig = 1
192
+ reproduction_number: cfg.SkylineParameter
193
+ infectious_period: cfg.SkylineParameter
194
+ incubation_period: cfg.SkylineParameter
195
+ sampling_proportion: cfg.SkylineParameter = 1
182
196
 
183
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
197
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
184
198
  return get_BDEI_events(
185
199
  reproduction_number=skyline_parameter(self.reproduction_number, data),
186
200
  infectious_period=skyline_parameter(self.infectious_period, data),
@@ -191,13 +205,13 @@ class BDEITreeDatasetGenerator(TreeDatasetGenerator):
191
205
 
192
206
  class BDSSTreeDatasetGenerator(TreeDatasetGenerator):
193
207
  parameterization: Literal[ParameterizationType.BDSS] = ParameterizationType.BDSS
194
- reproduction_number: cfg.SkylineParameterConfig
195
- infectious_period: cfg.SkylineParameterConfig
196
- superspreading_ratio: cfg.SkylineParameterConfig
197
- superspreaders_proportion: cfg.SkylineParameterConfig
198
- sampling_proportion: cfg.SkylineParameterConfig = 1
208
+ reproduction_number: cfg.SkylineParameter
209
+ infectious_period: cfg.SkylineParameter
210
+ superspreading_ratio: cfg.SkylineParameter
211
+ superspreaders_proportion: cfg.SkylineParameter
212
+ sampling_proportion: cfg.SkylineParameter = 1
199
213
 
200
- def _get_events(self, rng: Generator, data: dict[str, Any]) -> list[Event]:
214
+ def _get_events(self, data: dict[str, Any]) -> list[Event]:
201
215
  return get_BDSS_events(
202
216
  reproduction_number=skyline_parameter(self.reproduction_number, data),
203
217
  infectious_period=skyline_parameter(self.infectious_period, data),
@@ -0,0 +1,28 @@
1
+ from typing import Any, TypeGuard
2
+
3
+ import phylogenie.generators.configs as cfg
4
+ import phylogenie.typings as pgt
5
+
6
+
7
+ def is_list(x: Any) -> TypeGuard[list[Any]]:
8
+ return isinstance(x, list)
9
+
10
+
11
+ def is_list_of_scalar_configs(x: Any) -> TypeGuard[list[cfg.Scalar]]:
12
+ return is_list(x) and all(isinstance(v, cfg.Scalar) for v in x)
13
+
14
+
15
+ def is_list_of_skyline_parameter_configs(
16
+ x: Any,
17
+ ) -> TypeGuard[list[cfg.SkylineParameter]]:
18
+ return is_list(x) and all(isinstance(v, cfg.SkylineParameter) for v in x)
19
+
20
+
21
+ def is_skyline_vector_config(x: Any) -> TypeGuard[cfg.SkylineVector]:
22
+ return isinstance(
23
+ x, str | pgt.Scalar | cfg.SkylineVectorModel
24
+ ) or is_list_of_skyline_parameter_configs(x)
25
+
26
+
27
+ def is_list_of_skyline_vector_configs(x: Any) -> TypeGuard[list[cfg.SkylineVector]]:
28
+ return is_list(x) and all(is_skyline_vector_config(v) for v in x)
@@ -37,3 +37,7 @@ def main() -> None:
37
37
  args = parser.parse_args()
38
38
 
39
39
  run(args.config_path)
40
+
41
+
42
+ if __name__ == "__main__":
43
+ run("/Users/gmarino/phylogenie/examples/5.BD-CT-Trees.yaml")
@@ -1,5 +1,5 @@
1
1
  from collections.abc import Callable, Iterator
2
- from typing import TypeGuard, Union, overload
2
+ from typing import Any, TypeGuard, Union, overload
3
3
 
4
4
  import phylogenie.typeguards as tg
5
5
  import phylogenie.typings as pgt
@@ -20,7 +20,7 @@ SkylineMatrixOperand = Union[SkylineVectorOperand, "SkylineMatrix"]
20
20
  SkylineMatrixCoercible = Union[pgt.OneOrMany[SkylineVectorCoercible], "SkylineMatrix"]
21
21
 
22
22
 
23
- def is_skyline_matrix_operand(x: object) -> TypeGuard[SkylineMatrixOperand]:
23
+ def is_skyline_matrix_operand(x: Any) -> TypeGuard[SkylineMatrixOperand]:
24
24
  return isinstance(x, SkylineMatrix) or is_skyline_vector_operand(x)
25
25
 
26
26
 
@@ -142,7 +142,7 @@ class SkylineMatrix:
142
142
  def __bool__(self) -> bool:
143
143
  return any(self.params)
144
144
 
145
- def __eq__(self, other: object) -> bool:
145
+ def __eq__(self, other: Any) -> bool:
146
146
  return isinstance(other, SkylineMatrix) and self.params == other.params
147
147
 
148
148
  def __repr__(self) -> str:
@@ -1,6 +1,6 @@
1
1
  from bisect import bisect_right
2
2
  from collections.abc import Callable
3
- from typing import TypeGuard, Union
3
+ from typing import Any, TypeGuard, Union
4
4
 
5
5
  import phylogenie.typeguards as tg
6
6
  import phylogenie.typings as pgt
@@ -8,12 +8,12 @@ import phylogenie.typings as pgt
8
8
  SkylineParameterLike = Union[pgt.Scalar, "SkylineParameter"]
9
9
 
10
10
 
11
- def is_skyline_parameter_like(x: object) -> TypeGuard[SkylineParameterLike]:
11
+ def is_skyline_parameter_like(x: Any) -> TypeGuard[SkylineParameterLike]:
12
12
  return isinstance(x, pgt.Scalar | SkylineParameter)
13
13
 
14
14
 
15
15
  def is_many_skyline_parameters_like(
16
- x: object,
16
+ x: Any,
17
17
  ) -> TypeGuard[pgt.Many[SkylineParameterLike]]:
18
18
  return tg.is_many(x) and all(is_skyline_parameter_like(v) for v in x)
19
19
 
@@ -107,7 +107,7 @@ class SkylineParameter:
107
107
  def __bool__(self) -> bool:
108
108
  return any(self.value)
109
109
 
110
- def __eq__(self, other: object) -> bool:
110
+ def __eq__(self, other: Any) -> bool:
111
111
  return isinstance(other, SkylineParameter) and (
112
112
  self.value == other.value and self.change_times == other.change_times
113
113
  )
@@ -1,5 +1,5 @@
1
1
  from collections.abc import Callable, Iterator
2
- from typing import TypeGuard, Union, overload
2
+ from typing import Any, TypeGuard, Union, overload
3
3
 
4
4
  import phylogenie.typeguards as tg
5
5
  import phylogenie.typings as pgt
@@ -16,26 +16,24 @@ SkylineVectorLike = Union[pgt.Many[SkylineParameterLike], "SkylineVector"]
16
16
  SkylineVectorCoercible = Union[pgt.OneOrMany[SkylineParameterLike], "SkylineVector"]
17
17
 
18
18
 
19
- def is_skyline_vector_operand(x: object) -> TypeGuard[SkylineVectorOperand]:
19
+ def is_skyline_vector_operand(x: Any) -> TypeGuard[SkylineVectorOperand]:
20
20
  return isinstance(x, SkylineVector) or is_skyline_parameter_like(x)
21
21
 
22
22
 
23
- def is_skyline_vector_like(x: object) -> TypeGuard[SkylineVectorLike]:
23
+ def is_skyline_vector_like(x: Any) -> TypeGuard[SkylineVectorLike]:
24
24
  return isinstance(x, SkylineVector) or is_many_skyline_parameters_like(x)
25
25
 
26
26
 
27
- def is_skyline_vector_coercible(
28
- x: object,
29
- ) -> TypeGuard[SkylineVectorCoercible]:
27
+ def is_skyline_vector_coercible(x: Any) -> TypeGuard[SkylineVectorCoercible]:
30
28
  return is_skyline_parameter_like(x) or is_skyline_vector_like(x)
31
29
 
32
30
 
33
- def is_many_skyline_vectors_like(x: object) -> TypeGuard[pgt.Many[SkylineVectorLike]]:
31
+ def is_many_skyline_vectors_like(x: Any) -> TypeGuard[pgt.Many[SkylineVectorLike]]:
34
32
  return tg.is_many(x) and all(is_skyline_vector_like(v) for v in x)
35
33
 
36
34
 
37
35
  def is_many_skyline_vectors_coercible(
38
- x: object,
36
+ x: Any,
39
37
  ) -> TypeGuard[pgt.Many[SkylineVectorCoercible]]:
40
38
  return tg.is_many(x) and all(is_skyline_vector_coercible(v) for v in x)
41
39
 
@@ -131,7 +129,7 @@ class SkylineVector:
131
129
  def __bool__(self) -> bool:
132
130
  return any(self.params)
133
131
 
134
- def __eq__(self, other: object) -> bool:
132
+ def __eq__(self, other: Any) -> bool:
135
133
  return isinstance(other, SkylineVector) and self.params == other.params
136
134
 
137
135
  def __repr__(self) -> str:
@@ -1,6 +1,5 @@
1
- from __future__ import annotations
2
-
3
- from typing import Iterator
1
+ from collections.abc import Iterator
2
+ from typing import Any
4
3
 
5
4
 
6
5
  class Tree:
@@ -9,29 +8,30 @@ class Tree:
9
8
  self.branch_length = branch_length
10
9
  self.parent: Tree | None = None
11
10
  self.children: list[Tree] = []
11
+ self._features: dict[str, Any] = {}
12
12
 
13
- def add_child(self, child: Tree) -> Tree:
13
+ def add_child(self, child: "Tree") -> "Tree":
14
14
  child.parent = self
15
15
  self.children.append(child)
16
16
  return self
17
17
 
18
- def preorder_traversal(self) -> Iterator[Tree]:
18
+ def preorder_traversal(self) -> Iterator["Tree"]:
19
19
  yield self
20
20
  for child in self.children:
21
21
  yield from child.preorder_traversal()
22
22
 
23
- def postorder_traversal(self) -> Iterator[Tree]:
23
+ def postorder_traversal(self) -> Iterator["Tree"]:
24
24
  for child in self.children:
25
25
  yield from child.postorder_traversal()
26
26
  yield self
27
27
 
28
- def get_node(self, id: str) -> Tree:
28
+ def get_node(self, id: str) -> "Tree":
29
29
  for node in self:
30
30
  if node.id == id:
31
31
  return node
32
32
  raise ValueError(f"Node with id {id} not found.")
33
33
 
34
- def get_leaves(self) -> list[Tree]:
34
+ def get_leaves(self) -> list["Tree"]:
35
35
  return [node for node in self if not node.children]
36
36
 
37
37
  def get_time(self) -> float:
@@ -40,13 +40,22 @@ class Tree:
40
40
  raise ValueError(f"Branch length of node {self.id} is not set.")
41
41
  return self.branch_length + parent_time
42
42
 
43
- def copy(self) -> Tree:
43
+ def is_leaf(self) -> bool:
44
+ return not self.children
45
+
46
+ def copy(self) -> "Tree":
44
47
  new_tree = Tree(self.id, self.branch_length)
45
48
  for child in self.children:
46
49
  new_tree.add_child(child.copy())
47
50
  return new_tree
48
51
 
49
- def __iter__(self) -> Iterator[Tree]:
52
+ def get(self, key: str, default: Any = None) -> Any:
53
+ return self._features.get(key, default)
54
+
55
+ def set(self, key: str, value: Any) -> None:
56
+ self._features[key] = value
57
+
58
+ def __iter__(self) -> Iterator["Tree"]:
50
59
  return self.preorder_traversal()
51
60
 
52
61
  def __repr__(self) -> str:
@@ -4,6 +4,7 @@ from phylogenie.treesimulator.events import (
4
4
  get_BDEI_events,
5
5
  get_BDSS_events,
6
6
  get_canonical_events,
7
+ get_contact_tracing_events,
7
8
  get_epidemiological_events,
8
9
  get_FBD_events,
9
10
  )
@@ -15,6 +16,7 @@ __all__ = [
15
16
  "get_BDEI_events",
16
17
  "get_BDSS_events",
17
18
  "get_canonical_events",
19
+ "get_contact_tracing_events",
18
20
  "get_epidemiological_events",
19
21
  "get_FBD_events",
20
22
  "simulate_tree",