|
1 | 1 | """Base class for all descriptor calculators.""" |
2 | 2 |
|
3 | 3 | from abc import abstractmethod |
4 | | -from functools import cached_property |
| 4 | +import json |
5 | 5 | import os |
6 | 6 | import tempfile |
7 | 7 |
|
8 | 8 | import ase |
| 9 | +from ase.cell import Cell |
9 | 10 | from ase.units import m |
10 | 11 | from ase.neighborlist import NeighborList, NewPrimitiveNeighborList |
11 | 12 | import numpy as np |
@@ -375,6 +376,16 @@ def calculate_from_qe_out( |
375 | 376 |
|
376 | 377 | return self._calculate(working_directory, **kwargs) |
377 | 378 |
|
| 379 | + def calculate_from_json(self, json_file, working_directory=".", **kwargs): |
| 380 | + if isinstance(json_file, str): |
| 381 | + json_dict = json.load(open(json_file, encoding="utf-8")) |
| 382 | + else: |
| 383 | + json_dict = json.load(json_file) |
| 384 | + self.grid_dimensions = json_dict["grid_dimensions"] |
| 385 | + self._atoms = ase.Atoms.fromdict(json_dict["atoms"]) |
| 386 | + self._voxel = Cell(json_dict["voxel"]["array"]) |
| 387 | + return self._calculate(working_directory, **kwargs) |
| 388 | + |
378 | 389 | def calculate_from_atoms( |
379 | 390 | self, atoms, grid_dimensions, working_directory=".", **kwargs |
380 | 391 | ): |
@@ -573,6 +584,16 @@ def convert_local_to_3d(self, descriptors_np): |
573 | 584 | ).transpose([2, 1, 0, 3]) |
574 | 585 | return descriptors_full, local_offset, local_reach |
575 | 586 |
|
| 587 | + def read_dimensions_from_json(self, json_file): |
| 588 | + if isinstance(json_file, str): |
| 589 | + json_dict = json.load(open(json_file, encoding="utf-8")) |
| 590 | + else: |
| 591 | + json_dict = json.load(json_file) |
| 592 | + grid_dimensions = json_dict["grid_dimensions"] + [ |
| 593 | + self._read_feature_dimension_from_json(json_dict) |
| 594 | + ] |
| 595 | + return grid_dimensions |
| 596 | + |
576 | 597 | # Private methods |
577 | 598 | ################# |
578 | 599 |
|
@@ -1021,5 +1042,9 @@ def _grid_to_coord(self, gridpoint): |
1021 | 1042 | def _calculate(self, outdir, **kwargs): |
1022 | 1043 | pass |
1023 | 1044 |
|
| 1045 | + @abstractmethod |
| 1046 | + def _read_feature_dimension_from_json(self, json_dict): |
| 1047 | + pass |
| 1048 | + |
1024 | 1049 | def _set_feature_size_from_array(self, array): |
1025 | 1050 | self.feature_size = np.shape(array)[-1] |
0 commit comments