API Reference¶
Data Module¶
cgcnn2.data
¶
AtomCustomJSONInitializer
¶
Bases: AtomInitializer
Initialize atom feature vectors using a JSON file, which is a python dictionary mapping from element number to a list representing the feature vector of the element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
elem_embedding_file
|
str
|
The path to the .json file |
required |
Source code in cgcnn2/data.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
AtomInitializer
¶
Bases: object
Base class for initializing the vector representation for atoms.
Use one AtomInitializer
per dataset.
Source code in cgcnn2/data.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
CIFData
¶
Bases: Dataset
The CIFData dataset is a wrapper for a dataset where the crystal structures are stored in the form of CIF files. The dataset should have the following directory structure:
root_dir |-- id_prop.csv |-- atom_init.json |-- id0.cif |-- id1.cif |-- ...
id_prop.csv: a CSV file with two columns. The first column recodes a unique ID for each crystal, and the second column recodes the value of target property.
atom_init.json: a JSON file that stores the initialization vector for each element.
ID.cif: a CIF file that recodes the crystal structure, where ID is the unique ID for the crystal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_dir
|
str
|
The path to the root directory of the dataset |
required |
max_num_nbr
|
int
|
The maximum number of neighbors while constructing the crystal graph |
12
|
radius
|
float
|
The cutoff radius for searching neighbors |
8
|
dmin
|
float
|
The minimum distance for constructing GaussianDistance |
0
|
step
|
float
|
The step size for constructing GaussianDistance |
0.2
|
random_seed
|
int
|
Random seed for shuffling the dataset |
123
|
Returns:
Name | Type | Description |
---|---|---|
atom_fea |
torch.Tensor shape (n_i, atom_fea_len) |
|
nbr_fea |
torch.Tensor shape (n_i, M, nbr_fea_len) |
|
nbr_fea_idx |
torch.LongTensor shape (n_i, M) |
|
target |
torch.Tensor shape (1, ) |
|
cif_id |
str or int |
Source code in cgcnn2/data.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
CIFData_NoTarget
¶
Bases: Dataset
The CIFData_NoTarget dataset is a wrapper for a dataset where the crystal structures are stored in the form of CIF files. The dataset should have the following directory structure:
root_dir |-- atom_init.json |-- id0.cif |-- id1.cif |-- ...
atom_init.json: a JSON file that stores the initialization vector for each element.
ID.cif: a CIF file that recodes the crystal structure, where ID is the unique ID for the crystal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_dir
|
str
|
The path to the root directory of the dataset |
required |
max_num_nbr
|
int
|
The maximum number of neighbors while constructing the crystal graph |
12
|
radius
|
float
|
The cutoff radius for searching neighbors |
8
|
dmin
|
float
|
The minimum distance for constructing GaussianDistance |
0
|
step
|
float
|
The step size for constructing GaussianDistance |
0.2
|
random_seed
|
int
|
Random seed for shuffling the dataset |
123
|
Returns:
Name | Type | Description |
---|---|---|
atom_fea |
torch.Tensor shape (n_i, atom_fea_len) |
|
nbr_fea |
torch.Tensor shape (n_i, M, nbr_fea_len) |
|
nbr_fea_idx |
torch.LongTensor shape (n_i, M) |
|
target |
torch.Tensor shape (1, ) |
|
cif_id |
str or int |
Source code in cgcnn2/data.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
|
GaussianDistance
¶
Bases: object
Expands the distance by Gaussian basis.
Unit: angstrom
Source code in cgcnn2/data.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
__init__(dmin, dmax, step, var=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dmin
|
float
|
Minimum interatomic distance |
required |
dmax
|
float
|
Maximum interatomic distance |
required |
step
|
float
|
Step size for the Gaussian filter |
required |
Source code in cgcnn2/data.py
87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
expand(distances)
¶
Apply Gaussian distance filter to a numpy distance array
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distances
|
ndarray
|
A distance matrix of any shape |
required |
Returns:
Name | Type | Description |
---|---|---|
expanded_distance |
shape (n+1)-d array Expanded distance matrix with the last dimension of length len(self.filter) |
Source code in cgcnn2/data.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
collate_pool(dataset_list)
¶
Collate a list of data and return a batch for predicting crystal properties.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_list
|
list of tuples
|
List of tuples for each data point. Each tuple contains: - atom_fea (torch.Tensor): shape (n_i, atom_fea_len) Atom features for each atom in the crystal - nbr_fea (torch.Tensor): shape (n_i, M, nbr_fea_len) Bond features for each atom's M neighbors - nbr_fea_idx (torch.LongTensor): shape (n_i, M) Indices of M neighbors of each atom - target (torch.Tensor): shape (1, ) Target value for prediction - cif_id (str or int) Unique ID for the crystal |
required |
Returns:
Name | Type | Description |
---|---|---|
N = sum(n_i); N0 = sum(i) |
||
batch_atom_fea |
torch.Tensor shape (N, orig_atom_fea_len) |
|
Atom features from atom type |
||
batch_nbr_fea |
torch.Tensor shape (N, M, nbr_fea_len) |
|
Bond features of each atom's M neighbors |
||
batch_nbr_fea_idx |
torch.LongTensor shape (N, M) |
|
Indices of M neighbors of each atom |
||
crystal_atom_idx |
list of torch.LongTensor of length N0 |
|
Mapping from the crystal idx to atom idx |
||
target |
torch.Tensor shape (N, 1) |
|
Target value for prediction |
||
batch_cif_ids |
list |
Source code in cgcnn2/data.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
train_force_split(total_set, train_ratio_force_set, train_ratio)
¶
Set up a training dataset with a forced training set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
total_set
|
str
|
The path to the total set |
required |
train_ratio_force_set
|
str
|
The path to the forced training set |
required |
train_ratio
|
float
|
The ratio of the training set |
required |
Returns:
Name | Type | Description |
---|---|---|
train_dataset |
CIFData The training dataset |
|
valid_test_dataset |
CIFData The validation set |
Source code in cgcnn2/data.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
|
Model Framework¶
cgcnn2.model
¶
ConvLayer
¶
Bases: Module
Convolutional layer for graph data.
Performs a convolutional operation on graphs, updating atom features based on their neighbors.
Source code in cgcnn2/model.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
__init__(atom_fea_len, nbr_fea_len)
¶
Initialize the ConvLayer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atom_fea_len
|
int
|
Number of atom hidden features. |
required |
nbr_fea_len
|
int
|
Number of bond (neighbor) features. |
required |
Source code in cgcnn2/model.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
forward(atom_in_fea, nbr_fea, nbr_fea_idx)
¶
Forward pass
N: Total number of atoms in the batch M: Max number of neighbors
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atom_in_fea
|
Tensor
|
Variable(torch.Tensor) shape (N, atom_fea_len) Atom hidden features before convolution |
required |
nbr_fea
|
Tensor
|
Variable(torch.Tensor) shape (N, M, nbr_fea_len) Bond features of each atom's M neighbors |
required |
nbr_fea_idx
|
LongTensor
|
shape (N, M) Indices of M neighbors of each atom |
required |
Returns:
Name | Type | Description |
---|---|---|
atom_out_fea |
Variable
|
shape (N, atom_fea_len) Atom hidden features after convolution |
Source code in cgcnn2/model.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
CrystalGraphConvNet
¶
Bases: Module
Create a crystal graph convolutional neural network for predicting total material properties.
Source code in cgcnn2/model.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
__init__(orig_atom_fea_len, nbr_fea_len, atom_fea_len=64, n_conv=3, h_fea_len=128, n_h=1, classification=False)
¶
Initialize CrystalGraphConvNet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
orig_atom_fea_len
|
int
|
Number of atom features in the input. |
required |
nbr_fea_len
|
int
|
Number of bond features. |
required |
atom_fea_len
|
int
|
Number of hidden atom features in the convolutional layers |
64
|
n_conv
|
int
|
Number of convolutional layers |
3
|
h_fea_len
|
int
|
Number of hidden features after pooling |
128
|
n_h
|
int
|
Number of hidden layers after pooling |
1
|
classification
|
bool
|
Whether to use classification or regression |
False
|
Source code in cgcnn2/model.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
forward(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
¶
Forward pass
N: Total number of atoms in the batch M: Max number of neighbors N0: Total number of crystals in the batch
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atom_fea
|
Tensor
|
Variable(torch.Tensor) shape (N, orig_atom_fea_len) Atom features from atom type |
required |
nbr_fea
|
Tensor
|
Variable(torch.Tensor) shape (N, M, nbr_fea_len) Bond features of each atom's M neighbors |
required |
nbr_fea_idx
|
LongTensor
|
shape (N, M) Indices of M neighbors of each atom |
required |
crystal_atom_idx
|
list of torch.LongTensor
|
Mapping from the crystal idx to atom idx |
required |
Returns:
Name | Type | Description |
---|---|---|
prediction |
Variable
|
shape (N, ) Atom hidden features after convolution |
Source code in cgcnn2/model.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
pooling(atom_fea, crystal_atom_idx)
¶
Pooling the atom features to crystal features
N: Total number of atoms in the batch N0: Total number of crystals in the batch
Parameters:
Name | Type | Description | Default |
---|---|---|---|
atom_fea
|
Tensor
|
Variable(torch.Tensor) shape (N, atom_fea_len) Atom feature vectors of the batch |
required |
crystal_atom_idx
|
list of torch.LongTensor
|
Mapping from the crystal idx to atom idx |
required |
Source code in cgcnn2/model.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
Utility Function¶
cgcnn2.util
¶
Normalizer
¶
Normalizes a PyTorch tensor and allows restoring it later.
This class keeps track of the mean and standard deviation of a tensor and provides methods to normalize and denormalize tensors using these statistics.
Source code in cgcnn2/util.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
__init__(tensor)
¶
Initialize the Normalizer with a sample tensor to calculate mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
Sample tensor to compute mean and standard deviation. |
required |
Source code in cgcnn2/util.py
414 415 416 417 418 419 420 421 422 |
|
denorm(normed_tensor)
¶
Denormalize a tensor using the stored mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normed_tensor
|
Tensor
|
Normalized tensor to denormalize. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Denormalized tensor. |
Source code in cgcnn2/util.py
436 437 438 439 440 441 442 443 444 445 446 |
|
load_state_dict(state_dict)
¶
Loads the mean and standard deviation from a state dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_dict
|
dict[str, Tensor]
|
State dictionary containing 'mean' and 'std'. |
required |
Source code in cgcnn2/util.py
457 458 459 460 461 462 463 464 465 |
|
norm(tensor)
¶
Normalize a tensor using the stored mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
Tensor to normalize. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Normalized tensor. |
Source code in cgcnn2/util.py
424 425 426 427 428 429 430 431 432 433 434 |
|
state_dict()
¶
Returns the state dictionary containing the mean and standard deviation.
Returns:
Type | Description |
---|---|
dict[str, Tensor]
|
dict[str, torch.Tensor]: State dictionary. |
Source code in cgcnn2/util.py
448 449 450 451 452 453 454 455 |
|
cgcnn_descriptor(model, loader, device, verbose)
¶
This function takes a pre-trained CGCNN model and a dataset, runs inference to generate predictions and features from the last layer, and returns the predictions and features. It is not necessary to have target values for the predicted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The trained CGCNN model. |
required |
loader
|
DataLoader
|
DataLoader for the dataset. |
required |
device
|
str
|
The device ('cuda' or 'cpu') where the model will be run. |
required |
verbose
|
int
|
The verbosity level of the output. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[list[float], list[Tensor]]
|
A tuple containing: - list: Model predictions - list: Crystal features from the last layer |
Notes
This function is intended for use in programmatic downstream analysis, where the user wants to continue downstream analysis using predictions or features (descriptors) generated by the model. For the command-line interface, consider using the cgcnn_pr script instead.
Source code in cgcnn2/util.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
cgcnn_pred(model_path, full_set, verbose=4, cuda=False, num_workers=0)
¶
This function takes the path to a pre-trained CGCNN model and a dataset, runs inference to generate predictions, and returns the predictions. It is not necessary to have target values for the predicted set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_path
|
str
|
Path to the file containing the pre-trained model parameters. |
required |
full_set
|
str
|
Path to the directory containing all CIF files for the dataset. |
required |
verbose
|
int
|
Verbosity level of the output. Defaults to 4. |
4
|
cuda
|
bool
|
Whether to use CUDA. Defaults to False. |
False
|
num_workers
|
int
|
Number of subprocesses for data loading. Defaults to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[list[float], list[Tensor]]
|
A tuple containing: - list: Model predictions - list: Features from the last layer |
Notes
This function is intended for use in programmatic downstream analysis, where the user wants to continue downstream analysis using predictions or features (descriptors) generated by the model. For the command-line interface, consider using the cgcnn_pr script instead.
Source code in cgcnn2/util.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
|
cgcnn_test(model, loader, device, plot_file='parity_plot.svg', results_file='results.csv', axis_limits=None, **kwargs)
¶
This function takes a pre-trained CGCNN model and a test dataset, runs inference to generate predictions, creates a parity plot comparing predicted versus actual values, and writes the results to a CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
The pre-trained CGCNN model. |
required |
loader
|
DataLoader
|
DataLoader for the dataset. |
required |
device
|
str
|
The device ('cuda' or 'cpu') where the model will be run. |
required |
plot_file
|
str
|
File path for saving the parity plot. Defaults to 'parity_plot.svg'. |
'parity_plot.svg'
|
results_file
|
str
|
File path for saving results as CSV. Defaults to 'results.csv'. |
'results.csv'
|
axis_limits
|
list
|
Limits for x and y axes of the parity plot. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments: xlabel (str): x-axis label for the parity plot. Defaults to "Actual". ylabel (str): y-axis label for the parity plot. Defaults to "Predicted". |
{}
|
Returns:
Type | Description |
---|---|
None
|
None |
Notes
This function is intended for use in a command-line interface, providing direct output of results. For programmatic downstream analysis, consider using the API functions instead, i.e. cgcnn_pred and cgcnn_descriptor.
Source code in cgcnn2/util.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
get_lr(optimizer)
¶
Extracts learning rates from a PyTorch optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer
|
Optimizer
|
The PyTorch optimizer to extract learning rates from. |
required |
Returns:
Type | Description |
---|---|
list[float]
|
list[float]: A list of learning rates, one for each parameter group in the optimizer. |
Source code in cgcnn2/util.py
59 60 61 62 63 64 65 66 67 68 69 70 |
|
id_prop_gen(cif_dir)
¶
Generates a CSV file containing IDs and properties of CIF files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cif_dir
|
str
|
Directory containing the CIF files. |
required |
Source code in cgcnn2/util.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
output_id_gen()
¶
Generates a unique output identifier based on current date and time.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A string in the format 'output_mmdd_HHMM' representing the current date and time. |
Source code in cgcnn2/util.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
unique_structures_clean(dataset_dir, delete_duplicates=False)
¶
Checks for duplicate (structurally equivalent) structures in a directory of CIF files using pymatgen's StructureMatcher and returns the count of unique structures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_dir
|
str
|
The path to the dataset containing CIF files. |
required |
delete_duplicates
|
bool
|
Whether to delete the duplicate structures, default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
grouped |
list A list of lists, where each sublist contains structurally equivalent |
|
structures. |
Source code in cgcnn2/util.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|