1
+ from functools import reduce
1
2
import numpy as np
2
3
from types import SimpleNamespace
3
4
from pyscf import data
4
- import equistore . core as equistore
5
+ import metatensor
5
6
import numbers
6
7
7
8
vector_label_names = SimpleNamespace (
@@ -58,13 +59,25 @@ def _get_tsize(tensor):
58
59
"""Computes the size of a tensor.
59
60
60
61
Args:
61
- tensor (equistore TensorMap): Tensor.
62
+ tensor (metatensor TensorMap): Tensor.
62
63
63
64
Returns:
64
65
The size of the tensor as an integer.
65
66
"""
66
67
return sum ([np .prod (tensor .block (key ).values .shape ) for key in tensor .keys ])
67
68
69
+ def _labels_to_array (labels ):
70
+ """Represents a set of metatensor labels as an array of the labels, using custom dtypes
71
+
72
+ Args:
73
+ labels (metatensor Labels): Labels
74
+
75
+ Returns:
76
+ labels (numpy ndarray[ndim=1, structured dtype]): the same labels
77
+ """
78
+ values = labels .values
79
+ dtype = [ (name ,values .dtype ) for name in labels .names ]
80
+ return values .view (dtype = dtype ).reshape (values .shape [0 ])
68
81
69
82
def vector_to_tensormap (mol , c ):
70
83
"""Transform a vector into a tensor map. Used by :py:func:`array_to_tensormap`.
@@ -74,7 +87,7 @@ def vector_to_tensormap(mol, c):
74
87
v (numpy ndarray): Vector.
75
88
76
89
Returns:
77
- A equistore tensor map.
90
+ A metatensor tensor map.
78
91
"""
79
92
80
93
atom_charges = list (mol .atom_charges ())
@@ -104,11 +117,11 @@ def vector_to_tensormap(mol, c):
104
117
block_prop_label_vals [label ] = np .arange (properties_count ).reshape (- 1 ,1 )
105
118
block_samp_label_vals [label ] = np .where (atom_charges == q )[0 ].reshape (- 1 ,1 )
106
119
107
- tm_labels = equistore .Labels (vector_label_names .tm , np .array (tm_label_vals ))
120
+ tm_labels = metatensor .Labels (vector_label_names .tm , np .array (tm_label_vals ))
108
121
109
- block_comp_labels = {key : equistore .Labels (vector_label_names .block_comp , block_comp_label_vals [key ]) for key in blocks }
110
- block_prop_labels = {key : equistore .Labels (vector_label_names .block_prop , block_prop_label_vals [key ]) for key in blocks }
111
- block_samp_labels = {key : equistore .Labels (vector_label_names .block_samp , block_samp_label_vals [key ]) for key in blocks }
122
+ block_comp_labels = {key : metatensor .Labels (vector_label_names .block_comp , block_comp_label_vals [key ]) for key in blocks }
123
+ block_prop_labels = {key : metatensor .Labels (vector_label_names .block_prop , block_prop_label_vals [key ]) for key in blocks }
124
+ block_samp_labels = {key : metatensor .Labels (vector_label_names .block_samp , block_samp_label_vals [key ]) for key in blocks }
112
125
113
126
# Fill in the blocks
114
127
@@ -138,8 +151,8 @@ def vector_to_tensormap(mol, c):
138
151
139
152
# Build tensor blocks and tensor map
140
153
141
- tensor_blocks = [equistore .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = [block_comp_labels [key ]], properties = block_prop_labels [key ]) for key in tm_label_vals ]
142
- tensor = equistore .TensorMap (keys = tm_labels , blocks = tensor_blocks )
154
+ tensor_blocks = [metatensor .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = [block_comp_labels [key ]], properties = block_prop_labels [key ]) for key in tm_label_vals ]
155
+ tensor = metatensor .TensorMap (keys = tm_labels , blocks = tensor_blocks )
143
156
144
157
return tensor
145
158
@@ -149,7 +162,7 @@ def tensormap_to_vector(mol, tensor):
149
162
150
163
Args:
151
164
mol (pyscf Mole): pyscf Mole object.
152
- tensor (equistore TensorMap): Tensor.
165
+ tensor (metatensor TensorMap): Tensor.
153
166
154
167
Returns:
155
168
A numpy ndarray (vector).
@@ -185,7 +198,7 @@ def matrix_to_tensormap(mol, dm):
185
198
v (numpy ndarray): Matrix.
186
199
187
200
Returns:
188
- A equistore tensor map.
201
+ A metatensor tensor map.
189
202
"""
190
203
191
204
def pairs (list1 , list2 ):
@@ -226,14 +239,14 @@ def pairs(list1, list2):
226
239
block_prop_label_vals [label ] = pairs (np .arange (properties_count1 ), np .arange (properties_count2 ))
227
240
block_samp_label_vals [label ] = pairs (np .where (atom_charges == q1 )[0 ],np .where (atom_charges == q2 )[0 ])
228
241
229
- tm_labels = equistore .Labels (matrix_label_names .tm , np .array (tm_label_vals ))
242
+ tm_labels = metatensor .Labels (matrix_label_names .tm , np .array (tm_label_vals ))
230
243
231
- block_prop_labels = {key : equistore .Labels (matrix_label_names .block_prop , block_prop_label_vals [key ]) for key in blocks }
232
- block_samp_labels = {key : equistore .Labels (matrix_label_names .block_samp , block_samp_label_vals [key ]) for key in blocks }
233
- block_comp_labels = {key : [equistore .Labels ([name ], vals ) for name , vals in zip (matrix_label_names .block_comp , block_comp_label_vals [key ])] for key in blocks }
244
+ block_prop_labels = {key : metatensor .Labels (matrix_label_names .block_prop , block_prop_label_vals [key ]) for key in blocks }
245
+ block_samp_labels = {key : metatensor .Labels (matrix_label_names .block_samp , block_samp_label_vals [key ]) for key in blocks }
246
+ block_comp_labels = {key : [metatensor .Labels ([name ], vals ) for name , vals in zip (matrix_label_names .block_comp , block_comp_label_vals [key ])] for key in blocks }
234
247
235
248
# Build tensor blocks
236
- tensor_blocks = [equistore .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
249
+ tensor_blocks = [metatensor .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
237
250
238
251
# Fill in the blocks
239
252
@@ -293,8 +306,8 @@ def pairs(list1, list2):
293
306
blocks [key ] = np .ascontiguousarray (blocks [key ][:,:,_pyscf2gpr_l1_order ,:])
294
307
295
308
# Build tensor map
296
- tensor_blocks = [equistore .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
297
- tensor = equistore .TensorMap (keys = tm_labels , blocks = tensor_blocks )
309
+ tensor_blocks = [metatensor .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
310
+ tensor = metatensor .TensorMap (keys = tm_labels , blocks = tensor_blocks )
298
311
299
312
return tensor
300
313
@@ -304,7 +317,7 @@ def tensormap_to_matrix(mol, tensor):
304
317
305
318
Args:
306
319
mol (pyscf Mole): pyscf Mole object.
307
- tensor (equistore TensorMap): Tensor.
320
+ tensor (metatensor TensorMap): Tensor.
308
321
309
322
Returns:
310
323
A numpy ndarray (matrix).
@@ -352,7 +365,7 @@ def array_to_tensormap(mol, v):
352
365
v (numpy ndarray): Array. It can be a vector or a matrix.
353
366
354
367
Returns:
355
- A equistore tensor map.
368
+ A metatensor tensor map.
356
369
"""
357
370
if v .ndim == 1 :
358
371
return vector_to_tensormap (mol , v )
@@ -367,15 +380,15 @@ def tensormap_to_array(mol, tensor):
367
380
368
381
Args:
369
382
mol (pyscf Mole): pyscf Mole object.
370
- tensor (equistore TensorMap): Tensor.
383
+ tensor (metatensor TensorMap): Tensor.
371
384
372
385
Returns:
373
386
A numpy ndarray. Matrix or vector, depending on the key names of the tensor.
374
387
"""
375
388
376
- if tensor .keys .names == tuple ( vector_label_names .tm ) :
389
+ if tensor .keys .names == vector_label_names .tm :
377
390
return tensormap_to_vector (mol , tensor )
378
- elif tensor .keys .names == tuple ( matrix_label_names .tm ) :
391
+ elif tensor .keys .names == matrix_label_names .tm :
379
392
return tensormap_to_matrix (mol , tensor )
380
393
else :
381
394
raise Exception (f'Tensor key names mismatch. Cannot determine if it is a vector or a matrix' )
@@ -385,16 +398,17 @@ def join(tensors):
385
398
"""Merge two or more tensors with the same label names avoiding information duplictaion.
386
399
387
400
Args:
388
- tensors (list): List of equistore TensorMap.
401
+ tensors (list): List of metatensor TensorMap.
389
402
390
403
Returns:
391
- A equistore TensorMap containing the information of all the input tensors.
404
+ A metatensor TensorMap containing the information of all the input tensors.
392
405
"""
393
406
394
407
if not all (tensor .keys .names == tensors [0 ].keys .names for tensor in tensors ):
395
408
raise Exception (f'Cannot merge tensors with different label names' )
396
- tm_label_vals = sorted (list (set ().union (* [set (tensor .keys .tolist ()) for tensor in tensors ])))
397
- tm_labels = equistore .Labels (tensors [0 ].keys .names , np .array (tm_label_vals ))
409
+ tm_label_vals = set ().union (* [set (_labels_to_array (tensor .keys )) for tensor in tensors ])
410
+ tm_label_vals = sorted ((tuple (value ) for value in tm_label_vals ))
411
+ tm_labels = metatensor .Labels (tensors [0 ].keys .names , np .array (tm_label_vals ))
398
412
399
413
blocks = {}
400
414
block_comp_labels = {}
@@ -403,7 +417,7 @@ def join(tensors):
403
417
block_samp_label_vals = {}
404
418
405
419
for label in tm_labels :
406
- key = tuple (label .tolist () )
420
+ key = tuple (label .values )
407
421
blocks [key ] = []
408
422
block_samp_label_vals [key ] = []
409
423
for imol ,tensor in enumerate (tensors ):
@@ -420,10 +434,10 @@ def join(tensors):
420
434
for key in blocks :
421
435
blocks [key ] = np .concatenate (blocks [key ])
422
436
block_samp_label_vals [key ] = np .array (block_samp_label_vals [key ])
423
- block_samp_labels [key ] = equistore .Labels ((_molid_name , * tensor .sample_names ), block_samp_label_vals [key ])
437
+ block_samp_labels [key ] = metatensor .Labels ((_molid_name , * tensor .sample_names ), block_samp_label_vals [key ])
424
438
425
- tensor_blocks = [equistore .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
426
- tensor = equistore .TensorMap (keys = tm_labels , blocks = tensor_blocks )
439
+ tensor_blocks = [metatensor .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
440
+ tensor = metatensor .TensorMap (keys = tm_labels , blocks = tensor_blocks )
427
441
428
442
return tensor
429
443
@@ -432,17 +446,20 @@ def split(tensor):
432
446
"""Split a tensor based on the molecule information stored within the input TensorMap.
433
447
434
448
Args:
435
- tensor (equistore TensorMap): Tensor containing several molecules.
449
+ tensor (metatensor TensorMap): Tensor containing several molecules.
436
450
437
451
Returns:
438
- N equistore TensorMap, where N is equal to the total number of diferent molecules stored within the input TensorMap.
452
+ N metatensor TensorMap, where N is equal to the total number of diferent molecules stored within the input TensorMap.
439
453
"""
440
454
441
455
if tensor .sample_names [0 ]!= _molid_name :
442
456
raise Exception (f'Tensor does not seem to contain several molecules' )
443
457
444
458
# Check if the molecule indices are continuous
445
- mollist = sorted (set (np .hstack ([np .array (tensor .block (keys ).samples .tolist ())[:,0 ] for keys in tensor .keys ])))
459
+ mollist = sorted (reduce (
460
+ lambda a ,b : a .union (b ),
461
+ [set (block .samples .column (_molid_name )) for block in tensor .blocks ()]
462
+ ))
446
463
if mollist == list (range (len (mollist ))):
447
464
tensors = [None ] * len (mollist )
448
465
else :
@@ -451,8 +468,8 @@ def split(tensor):
451
468
# Common labels
452
469
block_comp_labels = {}
453
470
block_prop_labels = {}
454
- for label in tensor .keys :
455
- key = label .tolist ( )
471
+ for label , block in tensor .items () :
472
+ key = tuple ( label .values )
456
473
block = tensor .block (label )
457
474
block_comp_labels [key ] = block .components
458
475
block_prop_labels [key ] = block .properties
@@ -463,20 +480,22 @@ def split(tensor):
463
480
block_samp_labels = {}
464
481
465
482
for label in tensor .keys :
466
- key = label .tolist ( )
483
+ key = tuple ( label .values )
467
484
block = tensor .block (label )
468
485
469
- samplelbl = [lbl for lbl in block .samples .tolist ( ) if lbl [0 ]== imol ]
470
- if len (samplelbl )== 0 :
486
+ samples = [( sample_i , lbl ) for sample_i , lbl in enumerate ( block .samples .values ) if lbl [0 ]== imol ]
487
+ if len (samples )== 0 :
471
488
continue
472
- sampleidx = [block .samples .position (lbl ) for lbl in samplelbl ]
489
+ sampleidx = [t [0 ] for t in samples ]
490
+ samplelbl = [t [1 ] for t in samples ]
491
+ #sampleidx = [block.samples.position(lbl) for lbl in samplelbl]
473
492
474
493
blocks [key ] = block .values [sampleidx ]
475
- block_samp_labels [key ] = equistore .Labels (tensor .sample_names [1 :], np .array (samplelbl )[:,1 :])
494
+ block_samp_labels [key ] = metatensor .Labels (tensor .sample_names [1 :], np .array (samplelbl )[:,1 :])
476
495
477
496
tm_label_vals = sorted (list (blocks .keys ()))
478
- tm_labels = equistore .Labels (tensor .keys .names , np .array (tm_label_vals ))
479
- tensor_blocks = [equistore .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
480
- tensors [imol ] = equistore .TensorMap (keys = tm_labels , blocks = tensor_blocks )
497
+ tm_labels = metatensor .Labels (tensor .keys .names , np .array (tm_label_vals ))
498
+ tensor_blocks = [metatensor .TensorBlock (values = blocks [key ], samples = block_samp_labels [key ], components = block_comp_labels [key ], properties = block_prop_labels [key ]) for key in tm_label_vals ]
499
+ tensors [imol ] = metatensor .TensorMap (keys = tm_labels , blocks = tensor_blocks )
481
500
482
501
return tensors
0 commit comments