molcraft 0.1.0a25__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.

Potentially problematic release.


This version of molcraft might be problematic. Click here for more details.

@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Alexander Kensert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,118 @@
1
+ Metadata-Version: 2.4
2
+ Name: molcraft
3
+ Version: 0.1.0a25
4
+ Summary: Graph Neural Networks for Molecular Machine Learning
5
+ Author-email: Alexander Kensert <alexander.kensert@gmail.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Alexander Kensert
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/compomics/molcraft
29
+ Keywords: python,machine-learning,deep-learning,graph-neural-networks,molecular-machine-learning,molecular-graphs,computational-chemistry,computational-biology
30
+ Classifier: Programming Language :: Python :: 3
31
+ Classifier: Intended Audience :: Science/Research
32
+ Classifier: License :: OSI Approved :: MIT License
33
+ Classifier: Operating System :: POSIX :: Linux
34
+ Requires-Python: >=3.10
35
+ Description-Content-Type: text/markdown
36
+ License-File: LICENSE
37
+ Requires-Dist: tensorflow>=2.16
38
+ Requires-Dist: rdkit>=2023.9.5
39
+ Requires-Dist: pandas>=1.0.3
40
+ Requires-Dist: ipython>=8.12.0
41
+ Provides-Extra: gpu
42
+ Requires-Dist: tensorflow[and-cuda]>=2.16; extra == "gpu"
43
+ Dynamic: license-file
44
+
45
+ <img src="https://github.com/akensert/molcraft/blob/main/docs/_static/molcraft-logo.png" alt="molcraft-logo" width="90%">
46
+
47
+ **Deep Learning on Molecules**: A Minimalistic GNN package for Molecular ML.
48
+
49
+ > [!NOTE]
50
+ > In progress.
51
+
52
+ ## Installation
53
+
54
+ For CPU users:
55
+
56
+ ```bash
57
+ pip install --pre molcraft
58
+ ```
59
+
60
+ For GPU users:
61
+ ```bash
62
+ pip install --pre molcraft[gpu]
63
+ ```
64
+
65
+ ## Examples
66
+
67
+ ```python
68
+ from molcraft import features
69
+ from molcraft import descriptors
70
+ from molcraft import featurizers
71
+ from molcraft import layers
72
+ from molcraft import models
73
+ import keras
74
+
75
+ featurizer = featurizers.MolGraphFeaturizer(
76
+ atom_features=[
77
+ features.AtomType(),
78
+ features.NumHydrogens(),
79
+ features.Degree(),
80
+ ],
81
+ bond_features=[
82
+ features.BondType(),
83
+ features.IsRotatable(),
84
+ ],
85
+ super_node=True,
86
+ self_loops=True,
87
+ include_hydrogens=False,
88
+ )
89
+
90
+ graph = featurizer([('N[C@@H](C)C(=O)O', 2.5), ('N[C@@H](CS)C(=O)O', 1.5)])
91
+ print(graph)
92
+
93
+ model = models.GraphModel.from_layers(
94
+ [
95
+ layers.Input(graph.spec),
96
+ layers.NodeEmbedding(dim=128),
97
+ layers.EdgeEmbedding(dim=128),
98
+ layers.GraphConv(units=128),
99
+ layers.GraphConv(units=128),
100
+ layers.GraphConv(units=128),
101
+ layers.GraphConv(units=128),
102
+ layers.Readout(),
103
+ keras.layers.Dense(units=1024, activation='elu'),
104
+ keras.layers.Dense(units=1024, activation='elu'),
105
+ keras.layers.Dense(1)
106
+ ]
107
+ )
108
+
109
+ pred = model(graph)
110
+ print(pred)
111
+
112
+ # featurizers.save_featurizer(featurizer, '/tmp/featurizer.json')
113
+ # models.save_model(model, '/tmp/model.keras')
114
+
115
+ # loaded_featurizer = featurizers.load_featurizer('/tmp/featurizer.json')
116
+ # loaded_model = models.load_model('/tmp/model.keras')
117
+ ```
118
+
@@ -0,0 +1,74 @@
1
+ <img src="https://github.com/akensert/molcraft/blob/main/docs/_static/molcraft-logo.png" alt="molcraft-logo" width="90%">
2
+
3
+ **Deep Learning on Molecules**: A Minimalistic GNN package for Molecular ML.
4
+
5
+ > [!NOTE]
6
+ > In progress.
7
+
8
+ ## Installation
9
+
10
+ For CPU users:
11
+
12
+ ```bash
13
+ pip install --pre molcraft
14
+ ```
15
+
16
+ For GPU users:
17
+ ```bash
18
+ pip install --pre molcraft[gpu]
19
+ ```
20
+
21
+ ## Examples
22
+
23
+ ```python
24
+ from molcraft import features
25
+ from molcraft import descriptors
26
+ from molcraft import featurizers
27
+ from molcraft import layers
28
+ from molcraft import models
29
+ import keras
30
+
31
+ featurizer = featurizers.MolGraphFeaturizer(
32
+ atom_features=[
33
+ features.AtomType(),
34
+ features.NumHydrogens(),
35
+ features.Degree(),
36
+ ],
37
+ bond_features=[
38
+ features.BondType(),
39
+ features.IsRotatable(),
40
+ ],
41
+ super_node=True,
42
+ self_loops=True,
43
+ include_hydrogens=False,
44
+ )
45
+
46
+ graph = featurizer([('N[C@@H](C)C(=O)O', 2.5), ('N[C@@H](CS)C(=O)O', 1.5)])
47
+ print(graph)
48
+
49
+ model = models.GraphModel.from_layers(
50
+ [
51
+ layers.Input(graph.spec),
52
+ layers.NodeEmbedding(dim=128),
53
+ layers.EdgeEmbedding(dim=128),
54
+ layers.GraphConv(units=128),
55
+ layers.GraphConv(units=128),
56
+ layers.GraphConv(units=128),
57
+ layers.GraphConv(units=128),
58
+ layers.Readout(),
59
+ keras.layers.Dense(units=1024, activation='elu'),
60
+ keras.layers.Dense(units=1024, activation='elu'),
61
+ keras.layers.Dense(1)
62
+ ]
63
+ )
64
+
65
+ pred = model(graph)
66
+ print(pred)
67
+
68
+ # featurizers.save_featurizer(featurizer, '/tmp/featurizer.json')
69
+ # models.save_model(model, '/tmp/model.keras')
70
+
71
+ # loaded_featurizer = featurizers.load_featurizer('/tmp/featurizer.json')
72
+ # loaded_model = models.load_model('/tmp/model.keras')
73
+ ```
74
+
@@ -0,0 +1,17 @@
1
+ __version__ = '0.1.0a25'
2
+
3
+ import os
4
+ os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
5
+
6
+ from molcraft import chem
7
+ from molcraft import features
8
+ from molcraft import descriptors
9
+ from molcraft import featurizers
10
+ from molcraft import layers
11
+ from molcraft import models
12
+ from molcraft import ops
13
+ from molcraft import records
14
+ from molcraft import tensors
15
+ from molcraft import callbacks
16
+ from molcraft import datasets
17
+ from molcraft import losses
@@ -0,0 +1,100 @@
1
+ import warnings
2
+ import keras
3
+ import numpy as np
4
+
5
+
6
+ class TensorBoard(keras.callbacks.TensorBoard):
7
+
8
+ def _log_weights(self, epoch):
9
+ with self._train_writer.as_default():
10
+ for layer in self.model.layers:
11
+ for weight in layer.weights:
12
+ # Use weight.path istead of weight.name to distinguish
13
+ # weights of different layers.
14
+ histogram_weight_name = weight.path + "/histogram"
15
+ self.summary.histogram(
16
+ histogram_weight_name, weight, step=epoch
17
+ )
18
+ if self.write_images:
19
+ image_weight_name = weight.path + "/image"
20
+ self._log_weight_as_image(
21
+ weight, image_weight_name, epoch
22
+ )
23
+ self._train_writer.flush()
24
+
25
+
26
+ class LearningRateDecay(keras.callbacks.LearningRateScheduler):
27
+
28
+ def __init__(self, rate: float, delay: int = 0, **kwargs):
29
+
30
+ def lr_schedule(epoch: int, lr: float):
31
+ if epoch < delay:
32
+ return float(lr)
33
+ return float(lr * keras.ops.exp(-rate))
34
+
35
+ super().__init__(schedule=lr_schedule, **kwargs)
36
+
37
+
38
+ class Rollback(keras.callbacks.Callback):
39
+ """Rollback callback.
40
+
41
+ Currently, this callback simply restores the model and (optionally) the optimizer
42
+ variables if current loss deviates too much from the best observed loss.
43
+
44
+ This callback might be useful in situations where the loss tend to spike and put
45
+ the model in an undesired/problematic high-loss parameter space.
46
+
47
+ Args:
48
+ tolerance (float):
49
+ The threshold for when the restoration is triggered. The devaiation is
50
+ calculated as follows: (current_loss - best_loss) / best_loss.
51
+ """
52
+
53
+ def __init__(
54
+ self,
55
+ tolerance: float = 0.5,
56
+ rollback_optimizer: bool = True,
57
+ ):
58
+ super().__init__()
59
+ self.tolerance = tolerance
60
+ self.rollback_optimizer = rollback_optimizer
61
+
62
+ def on_train_begin(self, logs=None):
63
+ self._rollback_weights = self._get_model_vars()
64
+ if self.rollback_optimizer:
65
+ self._rollback_optimizer_vars = self._get_optimizer_vars()
66
+ self._rollback_loss = float('inf')
67
+
68
+ def on_epoch_end(self, epoch: int, logs: dict = None):
69
+ current_loss = logs.get('val_loss', logs.get('loss'))
70
+ deviation = (current_loss - self._rollback_loss) / self._rollback_loss
71
+
72
+ if np.isnan(current_loss) or np.isinf(current_loss):
73
+ self._rollback()
74
+ # Rolling back model because of nan or inf loss
75
+ return
76
+
77
+ if deviation > self.tolerance:
78
+ self._rollback()
79
+ # Rolling back model because of large loss deviation.
80
+ return
81
+
82
+ if current_loss < self._rollback_loss:
83
+ self._save_state(current_loss)
84
+
85
+ def _save_state(self, current_loss: float) -> None:
86
+ self._rollback_loss = current_loss
87
+ self._rollback_weights = self._get_model_vars()
88
+ if self.rollback_optimizer:
89
+ self._rollback_optimizer_vars = self._get_optimizer_vars()
90
+
91
+ def _rollback(self) -> None:
92
+ self.model.set_weights(self._rollback_weights)
93
+ if self.rollback_optimizer:
94
+ self.model.optimizer.set_weights(self._rollback_optimizer_vars)
95
+
96
+ def _get_optimizer_vars(self):
97
+ return [v.numpy() for v in self.model.optimizer.variables]
98
+
99
+ def _get_model_vars(self):
100
+ return self.model.get_weights()