likelihood 2.1.22__tar.gz → 2.2.5__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.
- {likelihood-2.1.22 → likelihood-2.2.5}/PKG-INFO +6 -2
- {likelihood-2.1.22 → likelihood-2.2.5}/README.md +1 -1
- likelihood-2.2.5/likelihood/VERSION +1 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/__init__.py +1 -3
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/graph/__init__.py +2 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/graph/_nn.py +235 -4
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/graph/graph.py +38 -3
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/graph/nn.py +245 -2
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/main.py +13 -7
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/__init__.py +2 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/__init__.py +2 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/_autoencoders.py +83 -33
- likelihood-2.2.5/likelihood/models/deep/_keras_io.py +88 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/_predictor.py +38 -36
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/autoencoders.py +96 -18
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/bandit.py +124 -4
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/gan.py +117 -20
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/predictor.py +28 -36
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/deep/rl.py +43 -16
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/ensemble.py +21 -10
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/environments.py +77 -9
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/hmm.py +4 -5
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/regression.py +107 -5
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/simulation.py +97 -7
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/models/utils.py +2 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/pipes.py +42 -15
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/__init__.py +2 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/_reports.py +2 -5
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/cat_embed.py +9 -14
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/figures.py +9 -12
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/impute.py +12 -21
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/models_tools.py +388 -60
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/numeric_tools.py +48 -21
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/reports.py +16 -17
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood/tools/tools.py +137 -82
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood.egg-info/PKG-INFO +6 -2
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood.egg-info/SOURCES.txt +4 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood.egg-info/requires.txt +6 -1
- likelihood-2.2.5/pyproject.toml +12 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/setup.py +1 -0
- likelihood-2.2.5/tests/generate_docstring_report.py +345 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/tests/test_hmm_temp.py +1 -1
- likelihood-2.2.5/tests/test_linting_docstrings.py +432 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/tests/test_notebooks.py +0 -1
- likelihood-2.1.22/likelihood/VERSION +0 -1
- {likelihood-2.1.22 → likelihood-2.2.5}/LICENSE +0 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood.egg-info/dependency_links.txt +0 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/likelihood.egg-info/top_level.txt +0 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/setup.cfg +0 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/tests/__init__.py +0 -0
- {likelihood-2.1.22 → likelihood-2.2.5}/tests/test_hmm.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: likelihood
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.5
|
|
4
4
|
Summary: A package that performs the maximum likelihood algorithm.
|
|
5
5
|
Home-page: https://github.com/jzsmoreno/likelihood/
|
|
6
6
|
Author: J. A. Moreno-Guerra
|
|
@@ -21,7 +21,8 @@ Requires-Dist: flake8>=6.0.0
|
|
|
21
21
|
Requires-Dist: isort>=5.12.0
|
|
22
22
|
Requires-Dist: mypy>=1.4.1
|
|
23
23
|
Requires-Dist: numpy<3.0.0,>=1.26.4
|
|
24
|
-
Requires-Dist: pydot
|
|
24
|
+
Requires-Dist: pydot>=2.0.0
|
|
25
|
+
Requires-Dist: ruff>=0.17.4
|
|
25
26
|
Requires-Dist: matplotlib
|
|
26
27
|
Requires-Dist: packaging
|
|
27
28
|
Requires-Dist: tabulate
|
|
@@ -39,6 +40,9 @@ Requires-Dist: tensorflow>=2.15.0; extra == "full"
|
|
|
39
40
|
Requires-Dist: keras-tuner; extra == "full"
|
|
40
41
|
Requires-Dist: scikit-learn; extra == "full"
|
|
41
42
|
Requires-Dist: torch; extra == "full"
|
|
43
|
+
Provides-Extra: tests
|
|
44
|
+
Requires-Dist: pytest>=7; extra == "tests"
|
|
45
|
+
Requires-Dist: httpx>=0.27; extra == "tests"
|
|
42
46
|
Dynamic: author
|
|
43
47
|
Dynamic: author-email
|
|
44
48
|
Dynamic: classifier
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.2.5
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Likelihood: Initialize the Package
|
|
3
|
-
=====================================
|
|
1
|
+
"""Likelihood: Initialize the Package =====================================.
|
|
4
2
|
|
|
5
3
|
This is the entry point of the Likelihood package. It initializes all necessary modules and provides a central hub for accessing various tools and functions.
|
|
6
4
|
|
|
@@ -24,6 +24,22 @@ from .nn import Data
|
|
|
24
24
|
|
|
25
25
|
@tf.keras.utils.register_keras_serializable(package="Custom", name="VanillaGNNLayer")
|
|
26
26
|
class VanillaGNNLayer(tf.keras.layers.Layer):
|
|
27
|
+
"""A vanilla graph neural network layer.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
dim_in : `int`
|
|
32
|
+
The number of input features.
|
|
33
|
+
dim_out : `int`
|
|
34
|
+
The number of output features.
|
|
35
|
+
rank : `int` or `None`
|
|
36
|
+
The rank for the LoRA adapter. ``None`` disables LoRA.
|
|
37
|
+
kernel_initializer : `str`
|
|
38
|
+
The kernel initializer for the dense layer.
|
|
39
|
+
**kwargs : `dict`
|
|
40
|
+
Additional keyword arguments passed to :class:`tf.keras.layers.Layer`.
|
|
41
|
+
"""
|
|
42
|
+
|
|
27
43
|
def __init__(
|
|
28
44
|
self,
|
|
29
45
|
dim_in: int,
|
|
@@ -40,6 +56,13 @@ class VanillaGNNLayer(tf.keras.layers.Layer):
|
|
|
40
56
|
self.linear = None
|
|
41
57
|
|
|
42
58
|
def build(self, input_shape: Tuple[int, ...]) -> None:
|
|
59
|
+
"""Build the layer architecture based on the rank.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
input_shape : `Tuple[int, ...]`
|
|
64
|
+
The shape of the input tensors.
|
|
65
|
+
"""
|
|
43
66
|
if self.rank:
|
|
44
67
|
self.linear = LoRALayer(self.dim_out, rank=self.rank)
|
|
45
68
|
else:
|
|
@@ -49,11 +72,33 @@ class VanillaGNNLayer(tf.keras.layers.Layer):
|
|
|
49
72
|
super(VanillaGNNLayer, self).build(input_shape)
|
|
50
73
|
|
|
51
74
|
def call(self, x: np.ndarray, adjacency: tf.SparseTensor) -> np.ndarray:
|
|
75
|
+
"""Forward pass of the layer over the graph.
|
|
76
|
+
|
|
77
|
+
Parameters
|
|
78
|
+
----------
|
|
79
|
+
x : `np.ndarray`
|
|
80
|
+
The input node features.
|
|
81
|
+
adjacency : `tf.SparseTensor`
|
|
82
|
+
The graph adjacency matrix.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
`np.ndarray`
|
|
87
|
+
The layer output.
|
|
88
|
+
"""
|
|
52
89
|
x = self.linear(x)
|
|
53
90
|
x = tf.sparse.sparse_dense_matmul(adjacency, x)
|
|
54
91
|
return x
|
|
55
92
|
|
|
56
93
|
def get_config(self) -> Dict[str, Any]:
|
|
94
|
+
"""Get the configuration dictionary for serialization.
|
|
95
|
+
|
|
96
|
+
Returns
|
|
97
|
+
-------
|
|
98
|
+
config : `Dict[str, Any]`
|
|
99
|
+
The configuration dictionary containing layer parameters including
|
|
100
|
+
``dim_in``, ``dim_out``, ``rank``, and ``kernel_initializer``.
|
|
101
|
+
"""
|
|
57
102
|
config = super(VanillaGNNLayer, self).get_config()
|
|
58
103
|
config.update(
|
|
59
104
|
{
|
|
@@ -71,6 +116,18 @@ class VanillaGNNLayer(tf.keras.layers.Layer):
|
|
|
71
116
|
|
|
72
117
|
@classmethod
|
|
73
118
|
def from_config(cls, config: Dict[str, Any]) -> "VanillaGNNLayer":
|
|
119
|
+
"""Reconstruct the layer from its serialized config.
|
|
120
|
+
|
|
121
|
+
Parameters
|
|
122
|
+
----------
|
|
123
|
+
config : `Dict[str, Any]`
|
|
124
|
+
The serialized configuration of the layer.
|
|
125
|
+
|
|
126
|
+
Returns
|
|
127
|
+
-------
|
|
128
|
+
`VanillaGNNLayer`
|
|
129
|
+
The reconstructed layer.
|
|
130
|
+
"""
|
|
74
131
|
if config.get("kernel_initializer") is not None:
|
|
75
132
|
config["kernel_initializer"] = tf.keras.initializers.deserialize(
|
|
76
133
|
config["kernel_initializer"]
|
|
@@ -79,6 +136,22 @@ class VanillaGNNLayer(tf.keras.layers.Layer):
|
|
|
79
136
|
|
|
80
137
|
|
|
81
138
|
class VanillaGNN:
|
|
139
|
+
"""A vanilla graph neural network model.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
dim_in : `int`
|
|
144
|
+
The number of input features.
|
|
145
|
+
dim_h : `int`
|
|
146
|
+
The number of hidden features.
|
|
147
|
+
dim_out : `int`
|
|
148
|
+
The number of output features.
|
|
149
|
+
rank : `int`
|
|
150
|
+
The rank for the LoRA adapter.
|
|
151
|
+
**kwargs : `dict`
|
|
152
|
+
Additional keyword arguments passed to the model.
|
|
153
|
+
"""
|
|
154
|
+
|
|
82
155
|
def __init__(self, dim_in: int, dim_h: int, dim_out: int, rank: int = 2, **kwargs) -> None:
|
|
83
156
|
self.dim_in = dim_in
|
|
84
157
|
self.dim_h = dim_h
|
|
@@ -92,6 +165,7 @@ class VanillaGNN:
|
|
|
92
165
|
self.build()
|
|
93
166
|
|
|
94
167
|
def build(self) -> None:
|
|
168
|
+
"""Build the functional model architecture."""
|
|
95
169
|
x_in = tf.keras.Input(shape=(self.dim_in,), name="node_features")
|
|
96
170
|
adjacency_in = tf.keras.Input(shape=(None,), sparse=True, name="adjacency")
|
|
97
171
|
|
|
@@ -111,12 +185,54 @@ class VanillaGNN:
|
|
|
111
185
|
|
|
112
186
|
@tf.function
|
|
113
187
|
def __call__(self, x: np.ndarray, adjacency: tf.SparseTensor) -> np.ndarray:
|
|
188
|
+
"""Forward pass over the model.
|
|
189
|
+
|
|
190
|
+
Parameters
|
|
191
|
+
----------
|
|
192
|
+
x : `np.ndarray`
|
|
193
|
+
The node feature matrix.
|
|
194
|
+
adjacency : `tf.SparseTensor`
|
|
195
|
+
The graph adjacency matrix.
|
|
196
|
+
|
|
197
|
+
Returns
|
|
198
|
+
-------
|
|
199
|
+
`np.ndarray`
|
|
200
|
+
The model output.
|
|
201
|
+
"""
|
|
114
202
|
return self.model([x, adjacency])
|
|
115
203
|
|
|
116
204
|
def f1_macro(self, y_true: np.ndarray, y_pred: np.ndarray) -> float:
|
|
205
|
+
"""Compute the macro-averaged F1 score.
|
|
206
|
+
|
|
207
|
+
Parameters
|
|
208
|
+
----------
|
|
209
|
+
y_true : `np.ndarray`
|
|
210
|
+
The ground-truth labels.
|
|
211
|
+
y_pred : `np.ndarray`
|
|
212
|
+
The predicted labels.
|
|
213
|
+
|
|
214
|
+
Returns
|
|
215
|
+
-------
|
|
216
|
+
`float`
|
|
217
|
+
The macro-averaged F1 score.
|
|
218
|
+
"""
|
|
117
219
|
return f1_score(y_true, y_pred, average="macro")
|
|
118
220
|
|
|
119
221
|
def compute_f1_score(self, logits: tf.Tensor, labels: tf.Tensor) -> float:
|
|
222
|
+
"""Compute the F1 score from model logits.
|
|
223
|
+
|
|
224
|
+
Parameters
|
|
225
|
+
----------
|
|
226
|
+
logits : `tf.Tensor`
|
|
227
|
+
The model logits.
|
|
228
|
+
labels : `tf.Tensor`
|
|
229
|
+
The ground-truth labels.
|
|
230
|
+
|
|
231
|
+
Returns
|
|
232
|
+
-------
|
|
233
|
+
`float`
|
|
234
|
+
The macro-averaged F1 score.
|
|
235
|
+
"""
|
|
120
236
|
predictions = tf.argmax(logits, axis=1, output_type=tf.int32)
|
|
121
237
|
true_labels = tf.cast(labels, tf.int32)
|
|
122
238
|
return self.f1_macro(true_labels.numpy(), predictions.numpy())
|
|
@@ -124,6 +240,22 @@ class VanillaGNN:
|
|
|
124
240
|
def evaluate(
|
|
125
241
|
self, x: np.ndarray, adjacency: tf.SparseTensor, y: np.ndarray
|
|
126
242
|
) -> Tuple[float, float]:
|
|
243
|
+
"""Evaluate the model on the given inputs.
|
|
244
|
+
|
|
245
|
+
Parameters
|
|
246
|
+
----------
|
|
247
|
+
x : `np.ndarray`
|
|
248
|
+
The node feature matrix.
|
|
249
|
+
adjacency : `tf.SparseTensor`
|
|
250
|
+
The graph adjacency matrix.
|
|
251
|
+
y : `np.ndarray`
|
|
252
|
+
The ground-truth labels.
|
|
253
|
+
|
|
254
|
+
Returns
|
|
255
|
+
-------
|
|
256
|
+
`Tuple[float, float]`
|
|
257
|
+
The mean loss and the F1 score.
|
|
258
|
+
"""
|
|
127
259
|
y = tf.cast(y, tf.int32)
|
|
128
260
|
out = self(x, adjacency)
|
|
129
261
|
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=out)
|
|
@@ -132,19 +264,42 @@ class VanillaGNN:
|
|
|
132
264
|
return loss.numpy(), f1
|
|
133
265
|
|
|
134
266
|
def test(self, data: Data) -> float:
|
|
267
|
+
"""Evaluate the model on the test split of the data.
|
|
268
|
+
|
|
269
|
+
Parameters
|
|
270
|
+
----------
|
|
271
|
+
data : `Data`
|
|
272
|
+
The dataset containing the test split.
|
|
273
|
+
|
|
274
|
+
Returns
|
|
275
|
+
-------
|
|
276
|
+
`float`
|
|
277
|
+
The macro-averaged F1 score on the test split.
|
|
278
|
+
"""
|
|
135
279
|
data.x = tf.convert_to_tensor(data.x) if not tf.is_tensor(data.x) else data.x
|
|
136
280
|
out = self(data.x, data.adjacency)
|
|
137
281
|
test_f1 = self.compute_f1_score(out, data.y)
|
|
138
282
|
return round(test_f1, 4)
|
|
139
283
|
|
|
140
284
|
def predict(self, data: Data) -> np.ndarray:
|
|
285
|
+
"""Predict the labels for the test split of the data.
|
|
286
|
+
|
|
287
|
+
Parameters
|
|
288
|
+
----------
|
|
289
|
+
data : `Data`
|
|
290
|
+
The dataset containing the test split.
|
|
291
|
+
|
|
292
|
+
Returns
|
|
293
|
+
-------
|
|
294
|
+
`np.ndarray`
|
|
295
|
+
The predicted labels for the test split.
|
|
296
|
+
"""
|
|
141
297
|
data.x = tf.convert_to_tensor(data.x) if not tf.is_tensor(data.x) else data.x
|
|
142
298
|
out = self(data.x, data.adjacency)
|
|
143
299
|
return tf.argmax(out, axis=1, output_type=tf.int32).numpy()
|
|
144
300
|
|
|
145
301
|
def save(self, filepath: str, **kwargs) -> None:
|
|
146
|
-
"""
|
|
147
|
-
Save the complete model including all components.
|
|
302
|
+
"""Save the complete model including all components.
|
|
148
303
|
|
|
149
304
|
Parameters
|
|
150
305
|
----------
|
|
@@ -168,8 +323,7 @@ class VanillaGNN:
|
|
|
168
323
|
|
|
169
324
|
@classmethod
|
|
170
325
|
def load(cls, filepath: str) -> "VanillaGNN":
|
|
171
|
-
"""
|
|
172
|
-
Load a complete model from saved components.
|
|
326
|
+
"""Load a complete model from saved components.
|
|
173
327
|
|
|
174
328
|
Parameters
|
|
175
329
|
----------
|
|
@@ -196,6 +350,14 @@ class VanillaGNN:
|
|
|
196
350
|
return instance
|
|
197
351
|
|
|
198
352
|
def get_config(self) -> Dict[str, Any]:
|
|
353
|
+
"""Get the configuration dictionary for serialization.
|
|
354
|
+
|
|
355
|
+
Returns
|
|
356
|
+
-------
|
|
357
|
+
`Dict[str, Any]`
|
|
358
|
+
The configuration dictionary containing model parameters including
|
|
359
|
+
``dim_in``, ``dim_h``, ``dim_out``, and ``rank``.
|
|
360
|
+
"""
|
|
199
361
|
return {
|
|
200
362
|
"dim_in": self.dim_in,
|
|
201
363
|
"dim_h": self.dim_h,
|
|
@@ -205,6 +367,18 @@ class VanillaGNN:
|
|
|
205
367
|
|
|
206
368
|
@classmethod
|
|
207
369
|
def from_config(cls, config: Dict[str, Any]) -> "VanillaGNN":
|
|
370
|
+
"""Reconstruct the model from its config.
|
|
371
|
+
|
|
372
|
+
Parameters
|
|
373
|
+
----------
|
|
374
|
+
config : `Dict[str, Any]`
|
|
375
|
+
The serialized configuration of the model.
|
|
376
|
+
|
|
377
|
+
Returns
|
|
378
|
+
-------
|
|
379
|
+
`VanillaGNN`
|
|
380
|
+
The reconstructed model.
|
|
381
|
+
"""
|
|
208
382
|
return cls(
|
|
209
383
|
dim_in=config["dim_in"],
|
|
210
384
|
dim_h=config["dim_h"],
|
|
@@ -213,6 +387,13 @@ class VanillaGNN:
|
|
|
213
387
|
)
|
|
214
388
|
|
|
215
389
|
def get_build_config(self) -> Dict[str, Any]:
|
|
390
|
+
"""Get the configuration needed to build the model.
|
|
391
|
+
|
|
392
|
+
Returns
|
|
393
|
+
-------
|
|
394
|
+
`Dict[str, Any]`
|
|
395
|
+
The configuration of the model.
|
|
396
|
+
"""
|
|
216
397
|
config = {
|
|
217
398
|
"dim_in": self.dim_in,
|
|
218
399
|
"dim_h": self.dim_h,
|
|
@@ -223,6 +404,18 @@ class VanillaGNN:
|
|
|
223
404
|
|
|
224
405
|
@classmethod
|
|
225
406
|
def build_from_config(cls, config: Dict[str, Any]) -> "VanillaGNN":
|
|
407
|
+
"""Build the model from its configuration.
|
|
408
|
+
|
|
409
|
+
Parameters
|
|
410
|
+
----------
|
|
411
|
+
config : `Dict[str, Any]`
|
|
412
|
+
The configuration of the model.
|
|
413
|
+
|
|
414
|
+
Returns
|
|
415
|
+
-------
|
|
416
|
+
`VanillaGNN`
|
|
417
|
+
The built model.
|
|
418
|
+
"""
|
|
226
419
|
return cls(**config)
|
|
227
420
|
|
|
228
421
|
@tf.function
|
|
@@ -233,6 +426,24 @@ class VanillaGNN:
|
|
|
233
426
|
batch_y: np.ndarray,
|
|
234
427
|
optimizer: tf.keras.optimizers.Optimizer,
|
|
235
428
|
) -> tf.Tensor:
|
|
429
|
+
"""Perform a single training step on a batch of data.
|
|
430
|
+
|
|
431
|
+
Parameters
|
|
432
|
+
----------
|
|
433
|
+
batch_x : `np.ndarray`
|
|
434
|
+
The input node features.
|
|
435
|
+
batch_adjacency : `tf.SparseTensor`
|
|
436
|
+
The graph adjacency matrix.
|
|
437
|
+
batch_y : `np.ndarray`
|
|
438
|
+
The ground-truth labels.
|
|
439
|
+
optimizer : `tf.keras.optimizers.Optimizer`
|
|
440
|
+
The optimizer to use.
|
|
441
|
+
|
|
442
|
+
Returns
|
|
443
|
+
-------
|
|
444
|
+
`tf.Tensor`
|
|
445
|
+
The loss value.
|
|
446
|
+
"""
|
|
236
447
|
with tf.GradientTape() as tape:
|
|
237
448
|
out = self(batch_x, batch_adjacency)
|
|
238
449
|
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=batch_y, logits=out)
|
|
@@ -249,6 +460,26 @@ class VanillaGNN:
|
|
|
249
460
|
test_size: float = 0.2,
|
|
250
461
|
optimizer: str = "adam",
|
|
251
462
|
) -> Tuple[List[float], List[float], List[float], List[float]]:
|
|
463
|
+
"""Fit the model on the dataset.
|
|
464
|
+
|
|
465
|
+
Parameters
|
|
466
|
+
----------
|
|
467
|
+
data : `Data`
|
|
468
|
+
The dataset to train on.
|
|
469
|
+
epochs : `int`
|
|
470
|
+
The number of training epochs.
|
|
471
|
+
batch_size : `int`
|
|
472
|
+
The batch size for training.
|
|
473
|
+
test_size : `float`
|
|
474
|
+
The proportion of the dataset to use for testing. Default is 0.2.
|
|
475
|
+
optimizer : `str`
|
|
476
|
+
The optimizer to use. Default is "adam".
|
|
477
|
+
|
|
478
|
+
Returns
|
|
479
|
+
-------
|
|
480
|
+
`Tuple[List[float], List[float], List[float], List[float]]`
|
|
481
|
+
The lists of train losses, train F1 scores, val losses, and val F1 scores.
|
|
482
|
+
"""
|
|
252
483
|
optimizers = {
|
|
253
484
|
"sgd": tf.keras.optimizers.SGD(),
|
|
254
485
|
"adam": tf.keras.optimizers.Adam(),
|
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
"""Build and visualize dynamic graphs from feature-importance data.
|
|
2
|
+
|
|
3
|
+
This module provides :class:`DynamicGraph`, which constructs a directed,
|
|
4
|
+
weighted network from a tabular dataset using feature-importance scores and
|
|
5
|
+
renders it with :mod:`pyvis`. It can also convert the interactive network back
|
|
6
|
+
into a static :class:`networkx.Graph`.
|
|
7
|
+
|
|
8
|
+
Examples
|
|
9
|
+
--------
|
|
10
|
+
>>> import pandas as pd
|
|
11
|
+
>>> import numpy as np
|
|
12
|
+
>>> from likelihood.graph import DynamicGraph
|
|
13
|
+
>>> df = pd.DataFrame(np.random.rand(3, 100).T, columns=["x1", "x2", "x3"])
|
|
14
|
+
>>> df["y"] = np.random.rand(len(df))
|
|
15
|
+
>>> graph = DynamicGraph(df, n_importances=2)
|
|
16
|
+
>>> graph.fit()
|
|
17
|
+
"""
|
|
18
|
+
|
|
1
19
|
from typing import List
|
|
2
20
|
|
|
3
21
|
import networkx as nx
|
|
@@ -9,7 +27,17 @@ from likelihood.tools import FeatureSelection
|
|
|
9
27
|
|
|
10
28
|
|
|
11
29
|
class DynamicGraph(FeatureSelection):
|
|
12
|
-
"""
|
|
30
|
+
"""Represent a dynamic graph built from feature-importance data.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
df : `pd.DataFrame`
|
|
35
|
+
The input data containing the features.
|
|
36
|
+
n_importances : `int`
|
|
37
|
+
The number of features to use for computing the graph edges.
|
|
38
|
+
**kwargs : `dict`
|
|
39
|
+
Additional keyword arguments passed to :class:`FeatureSelection`.
|
|
40
|
+
"""
|
|
13
41
|
|
|
14
42
|
def __init__(self, df: pd.DataFrame, n_importances: int, **kwargs) -> None:
|
|
15
43
|
self.G = Network(notebook=True, cdn_resources="remote", directed=True)
|
|
@@ -25,7 +53,7 @@ class DynamicGraph(FeatureSelection):
|
|
|
25
53
|
self._make_network()
|
|
26
54
|
|
|
27
55
|
def _make_network(self) -> None:
|
|
28
|
-
"""Create nodes and edges of the network based on feature importance scores"""
|
|
56
|
+
"""Create nodes and edges of the network based on feature importance scores."""
|
|
29
57
|
self._add_nodes()
|
|
30
58
|
for i in range(len(self.all_features_imp_graph)):
|
|
31
59
|
node = self.all_features_imp_graph[i][0]
|
|
@@ -41,7 +69,7 @@ class DynamicGraph(FeatureSelection):
|
|
|
41
69
|
self.G.add_node(n_id=i, label=node)
|
|
42
70
|
|
|
43
71
|
def draw(self, name: str = "graph.html", **kwargs) -> None:
|
|
44
|
-
"""Display the network using HTML format"""
|
|
72
|
+
"""Display the network using HTML format."""
|
|
45
73
|
spring_length = kwargs.get("spring_length", 500)
|
|
46
74
|
node_distance = kwargs.get("node_distance", 100)
|
|
47
75
|
self.G.repulsion(node_distance=node_distance, spring_length=spring_length)
|
|
@@ -52,6 +80,13 @@ class DynamicGraph(FeatureSelection):
|
|
|
52
80
|
display(HTML(html_file_content))
|
|
53
81
|
|
|
54
82
|
def pyvis_to_networkx(self) -> nx.Graph:
|
|
83
|
+
"""Convert the interactive pyvis network into a static networkx graph.
|
|
84
|
+
|
|
85
|
+
Returns
|
|
86
|
+
-------
|
|
87
|
+
`nx.Graph`
|
|
88
|
+
The equivalent undirected networkx graph.
|
|
89
|
+
"""
|
|
55
90
|
nx_graph = nx.Graph()
|
|
56
91
|
nodes = [d["id"] for d in self.G.nodes]
|
|
57
92
|
for node_dic in self.G.nodes:
|