Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface fix #44

Merged
merged 10 commits into from
Oct 17, 2023
46 changes: 45 additions & 1 deletion hippynn/databases/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ def make_generator(self, split_type, evaluation_mode, batch_size=None, subsample
if not self.splitting_completed:
raise ValueError("Database has not yet been split.")


if split_type not in self.splits:
raise ValueError(f"Split {split_type} Invalid. Current splits:{list(self.splits.keys())}")

Expand Down Expand Up @@ -226,6 +225,51 @@ def make_generator(self, split_type, evaluation_mode, batch_size=None, subsample
)

return generator

def trim_all_arrays(self,index):
"""
To be used in conjuction with remove_high_property
"""
for key in self.arr_dict:
self.arr_dict[key] = self.arr_dict[key][index]

def remove_high_property(self,key,perAtom,species_key=None,cut=None,std_factor=10):
"""
This function removes outlier data from the dataset
Must be called before splitting
"key": the property key in the dataset to check for high values
"perAtom": True if the property is defined per atom in axis 1, otherwise property is treated as full system
"std_factor": systems with values larger than this multiplier time the standard deviation of all data will be reomved. None to skip this step
"cut_factor": systems with values larger than this number are reomved. None to skip this step. This step is done first.
"""
if perAtom:
if species_key==None:
raise RuntimeError("species_key must be defined to trim a per atom quantity")
atom_ind = self.arr_dict[species_key] > 0
ndim = len(self.arr_dict[key].shape)
if cut!=None:
if perAtom:
Kmean = np.mean(self.arr_dict[key][atom_ind])
else:
Kmean = np.mean(self.arr_dict[key])
failArr = np.abs(self.arr_dict[key]-Kmean)>cut
#This does nothing with ndim=1
trimArr = np.sum(failArr,axis=tuple(range(1,ndim)))==0
self.trim_all_arrays(trimArr)

if std_factor!=None:
if perAtom:
atom_ind = self.arr_dict[species_key] > 0
Kmean = np.mean(self.arr_dict[key][atom_ind])
std_cut = np.std(self.arr_dict[key][atom_ind]) * std_factor
else:
Kmean = np.mean(self.arr_dict[key])
std_cut = np.std(self.arr_dict[key]) * std_factor
failArr = np.abs(self.arr_dict[key]-Kmean)>std_cut
#This does nothing with ndim=1
trimArr = np.sum(failArr,axis=tuple(range(1,ndim)))==0
self.trim_all_arrays(trimArr)



def compute_index_mask(indices, index_pool):
Expand Down
1 change: 0 additions & 1 deletion hippynn/interfaces/ase_interface/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import warnings
import torch

from ase.calculators import interface
from ase.calculators.calculator import compare_atoms, PropertyNotImplementedError, Calculator # Calculator is required to allow HIPNN to be used with ASE Mixing Calculators

from hippynn.graphs import find_relatives, find_unique_relative, get_subgraph, copy_subgraph, replace_node, GraphModule
Expand Down
4 changes: 2 additions & 2 deletions hippynn/interfaces/lammps_interface/mliap_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, energy_node, element_types, ndescriptors=1,
# Build the calculator
self.rcutfac, self.species_set, self.graph = setup_LAMMPS_graph(energy_node)
self.nparams = sum(p.nelement() for p in self.graph.parameters())
self.graph.to(torch.float64)
self.graph.to(torch.float32)

def compute_gradients(self, data):
pass
Expand All @@ -62,7 +62,7 @@ def compute_forces(self, data):
z_vals = self.species_set[elems+1]
pair_i = self.as_tensor(data.pair_i).type(torch.int64)
pair_j = self.as_tensor(data.pair_j).type(torch.int64)
rij = self.as_tensor(data.rij).type(torch.float64)
rij = self.as_tensor(data.rij).type(torch.float32)
nlocal = self.as_tensor(data.nlistatoms)

# note your sign for rij might need to be +1 or -1, depending on how your implementation works
Expand Down
2 changes: 1 addition & 1 deletion hippynn/layers/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def forward(self, all_features, mol_index, n_molecules):
total_hier = torch.zeros_like(total_energies)
mol_hier = torch.zeros_like(total_energies)
total_atom_hier = torch.zeros_like(total_atomen)
batch_hier = torch.zeros(1,dtype=total_energies.dtype,device=total_energies.dtype)
batch_hier = torch.zeros(1,dtype=total_energies.dtype,device=total_energies.device)

return total_energies, total_atomen, partial_sums, total_hier, total_atom_hier, mol_hier, batch_hier

Expand Down
Loading