def write_dx(name, param_file, output, expand, res, kind): """ writes a dx volumetric file. This type of file can be used to 3d-visualize electron density using software as chimera, PyMol or VMD Example: write-dx --output=benzene.a2md.dx --expand=3.0 --res=0.25 --kind=density benzene.mol2 benzene.ppp Note: resolution and expand units are Bohr """ start = time.time() mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() with open(param_file) as f: dm.read(json.load(f)) dx = dm.eval_volume(spacing=expand, resolution=res, kind=kind) if output is None: dx.write(name.replace('.mol2', '') + '.dx') else: dx.write(output) print("writting to : {:s}".format(output)) print("TE : {:12.4f}".format(time.time() - start))
def evaluate(name, param_file, coordinates, output): """ reads a model and runs an evaluation upon the specified coordinates """ start = time.time() mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() with open(param_file) as f: dm.read(json.load(f)) try: coordinates = np.loadtxt(coordinates, dtype='float64') except ValueError: print("could not read file. please, use space separated values") print("sed -i \"s/,/ /g\" COORDINATES") sys.exit() except FileNotFoundError: print("file not found") sys.exit() assert (type(coordinates) is np.ndarray) prediction = dm.eval(coordinates) assert (type(prediction) is np.ndarray) if output is None: for i in range(prediction.size): print("{:12.4e} {:12.4e} {:12.4e} {:12.4e}".format( coordinates[i, 0], coordinates[i, 1], coordinates[i, 2], prediction[i])) else: # noinspection PyTypeChecker np.savetxt(output, np.stack([coordinates, prediction], axis=1)) print("TE : {:12.4f}".format(time.time() - start))
def predict_charges(name, predictor, device, output): """ returns a mol2 file containing charges obtained by inference Example: predict-charges --device=cpu --output=benzene.npa.mp2 benzene.mol2 """ start = time.time() dev = torch.device(device=device) mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() pars = dm.get_parametrization() model = torch.load(predictor, map_location=dev).to(dev, ) param = Parametrizer(model, device=dev) pars = param.parametrize(name, pars) dm.read(pars) charges = dm.get_a2md_charges() if output is None: for i, (sym, coords, q) in enumerate( zip(dm.get_symbols(), dm.get_coordinates(), charges)): print("{:8d} {:8s} {:8.4e} {:8.4e} {:8.4e} {:8.4e}".format( i, sym, coords[0], coords[1], coords[2], q)) else: mm = Mol2(file=name) mm.charges = charges mm.write(file=output) print("TE : {:12.4f}".format(time.time() - start))
def predict_model(name, predictor, device, output): """ returns a ppp containing an am2d density model obtained by neural network inference Example: predict-model --device=cpu --output=benzene.ml.ppp benzene.mol2 """ start = time.time() dev = torch.device(device=device) mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() pars = dm.get_parametrization() model = torch.load(MODELS[predictor], map_location=dev).to(dev, ) param = Parametrizer(model, device=dev) pars = param.parametrize(name, pars) if output is None: print(json.dumps(pars, indent=4)) else: with open(output, 'w') as f: json.dump(pars, f, indent=4, sort_keys=True) print("TE : {:12.4f}".format(time.time() - start))
def __generate_ppp(name, output, parametrization='default'): start = time.time() if output is None: output = name.replace('.mol2', '.ppp') mm = Mol2(name) dm = a2md_from_mol(mm) if parametrization == 'default': params = dm.parametrization_default elif parametrization == 'extended': params = dm.parametrization_extended elif parametrization == 'spherical': params = dm.parametrization_spherical elif parametrization == 'harmonic': params = dm.parametrization_harmonic else: print( "unknown parametrization. please, use either default, extended, spherical or harmonic" ) sys.exit() dm.parametrize(params) with open(output, "w") as f: json.dump(dm.get_parametrization(), f, indent=4, sort_keys=True) print("TE : {:12.4f}".format(time.time() - start))
def __generate_ppp(name, output): start = time.time() if output is None: output = name.replace('.mol2', '.ppp') mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() with open(output, "w") as f: json.dump(dm.get_parametrization(), f, indent=4, sort_keys=True) print("TE : {:12.4f}".format(time.time() - start))
def admin_sources(mm, reference, reference_type): if reference_type == 'wfn': reference_d = WaveFunction.from_file(filename=reference, program='g09', prefetch_dm=True) elif reference_type == 'a2md': reference_d = a2md_from_mol(mm) with open(reference) as f: reference_d.read(json.load(f)) else: logger.error("use either wfn or a2md as reference format") sys.exit() return reference_d
def admin_sources(mm, reference, reference_type): if reference_type == 'wfn': reference_d = WaveFunction(verbose=False, file=reference, batch_size=10000) elif reference_type == 'a2md': reference_d = a2md_from_mol(mm) with open(reference) as f: reference_d.read(json.load(f)) else: logger.error("use either wfn or a2md as reference format") sys.exit() return reference_d
def predict_model(name, predictor, device, output): start = time.time() dev = torch.device(device=device) mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() pars = dm.get_parametrization() model = torch.load(MODELS[predictor], map_location=dev).to(dev) param = Parametrizer(model, device=dev) pars = param.parametrize(name, pars) if output is None: print(json.dumps(pars, indent=4)) else: with open(output, 'w') as f: json.dump(pars, f, indent=4, sort_keys=True) print("TE : {:12.4f}".format(time.time() - start))
def write_dx(name, param_file, output, expand, res, kind): """ writes a dx volume file with density from an a2md fit """ start = time.time() mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() with open(param_file) as f: dm.read(json.load(f)) dx = dm.eval_volume(spacing=expand, resolution=res, kind=kind) if output is None: dx.write(name.replace('.mol2', '') + '.dx') else: dx.write(output) print("writting to : {:s}".format(output)) print("TE : {:12.4f}".format(time.time() - start))
def predict_charges(name, predictor, device, output): start = time.time() dev = torch.device(device=device) mm = Mol2(name) dm = a2md_from_mol(mm) dm.parametrize() pars = dm.get_parametrization() model = torch.load(predictor, map_location=dev).to(dev) param = Parametrizer(model, device=dev) pars = param.parametrize(name, pars) dm.read(pars) charges = dm.get_a2md_charges() if output is None: for i, (sym, coords, q) in enumerate( zip(dm.get_symbols(), dm.get_coordinates(), charges)): print("{:8d} {:8s} {:8.4e} {:8.4e} {:8.4e} {:8.4e}".format( i, sym, coords[0], coords[1], coords[2], q)) else: mm = Mol2(file=name) mm.charges = charges mm.write(file=output) print("TE : {:12.4f}".format(time.time() - start))
def dx_add_a2md_charge(name, params, dx, charge, output): mm = Mol2(name) dm = a2md_from_mol(mm) with open(params) as f: dm.read(json.load(f)) convert2au = lambda x: x*UNITS_TABLE['angstrom']['au'] r3 = UNITS_TABLE['angstrom']['au'] ** 3 fun = lambda x : -dm.eval(convert2au(x))*r3 dx1 = Volume(filename=dx) dx1.read() dx1.eval(fun) dx2 = Volume(filename=dx) dx2.read() q = dx2._Volume__dx.sum() * (dx1.get_basis()[0, 0] ** 3) + charge qt = dx1._Volume__dx.sum() * (dx1.get_basis()[0, 0] ** 3) dx1._Volume__dx = dx1._Volume__dx * (q/ qt) dx1._Volume__dx = -dx1._Volume__dx + dx2._Volume__dx dx1.write(output) return
print("-- testing exponential kernel functions") labels = torch.tensor([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], dtype=torch.long, device=torch.device('cuda:0')) x = torch.rand((2, 1000, 3), dtype=torch.float, device=torch.device('cuda:0')) r = torch.rand((2, 5, 3), dtype=torch.float, device=torch.device('cuda:0')) c = torch.ones((2, 5, 4), dtype=torch.float, device=torch.device('cuda:0')) p = gamd.forward(x, c, labels, r) print("-- done") print("-- comparing amd vs genamd") benzene_mol2 = Mol2(benzene.mol2) benzene_amd = a2md_from_mol(benzene_mol2) with open('spherical.json') as f: spherical = json.load(f) benzene_amd.parametrize(param_dict=spherical) print('-- charge = {:6.4f}'.format(benzene_amd.integrate())) dx = benzene_amd.eval_volume(spacing=3.0, resolution=0.25, kind='density') dx.write('benzene.dx') print('-- writting benzene.dx') r = (np.random.rand(1000, 3) - 0.5) * 10 l, x, c = genamd_from_mol2(benzene_mol2, device=torch.device('cuda:0')) r_torch = torch.tensor(r, device=torch.device('cuda:0'), dtype=torch.float).unsqueeze(0) p_ref = benzene_amd.eval(r) # p_val = gamd.forward(r_torch, c, l, x).data.cpu().numpy() p_val = gamd.protodensity(r_torch, l, x).data.cpu().numpy()
from a2mdio.molecules import Mol2 from a2mdio.qm import WaveFunction from a2md.models import a2md_from_mol import numpy as np import time if __name__ == '__main__': START = time.time() fun = lambda x : np.exp(-2 * np.linalg.norm(x, axis=1))/np.pi integral = pi_lebedev(fun, r_max=10.0, radial_res=200, grid='tight') print(integral) print("done!") print("TE = {:8.4f}".format(time.time() - START)) START = time.time() water_molecule = Mol2(water.mol2) water_dm = a2md_from_mol(water_molecule) water_dm.parametrize() surf = np.loadtxt(water.surfaces[1], skiprows=1, delimiter=',') water_dm.optimize(surf[:, :3], surf[:, 3], optimization_mode='unrestricted') fun = lambda x: water_dm.eval(x) integral = 0 for fx in split_space(water_molecule, fun): integral += pi_lebedev( fun=fx, r_max=15.0, radial_res=100, grid='tight' )
import numpy as np from a2md.integrate import integrate_density_functional_gradient, dkl_gradient_functional, kullback_leibler_functional from a2md.integrate import integrate_density_functional from a2md.models import a2md_from_mol from a2md.utils import RBFSymmetryCluster from a2mdio.molecules import Mol2 from a2mdio.qm import WaveFunction from a2mdtest.a2mdtests import water from a2md.utils import project from scipy.optimize import minimize if __name__ == '__main__': water_mol2 = Mol2(water.mol2) water_a2md = a2md_from_mol(water_mol2) water_wfn = WaveFunction(file=water.wfn) water_density_sample = np.loadtxt(water.surfaces[1], skiprows=1, delimiter=',') cm = RBFSymmetryCluster(verbose=False) water_a2md.parametrize() water_a2md.clustering(cm.cluster) water_a2md.optimize( training_coordinates=water_density_sample[:, :3], training_density=water_density_sample[:, 3], optimization_mode='restricted' ) n = water_a2md.get_number_optimizable_functions() x = water_a2md.get_unfrozen_integrals() q = np.array(water_a2md.atom_charges).sum() - water_a2md.get_frozen_integrals().sum()
from a2mdio.molecules import Mol2, UNITS_TABLE from a2mdtest.a2mdtests import benzene from a2md.models import a2md_from_mol from a2mdio.volumes import Volume if __name__ == '__main__': mm = Mol2(benzene.mol2) dm = a2md_from_mol(mm) dm.parametrize() w = Volume( 'C:/Users/Bruno/ownCloud/main/research/edipff/apbs/apbs.charge.dx.dx') w.read() w.eval( lambda x: dm.eval(x * UNITS_TABLE['angstrom']['au'], kind='density')) w.write('test.dx')
def __fit_call(name, sample, opt_mode, scheme, regularization_constant, output, cluster, verbose): """ ajusts the parameters of a density model to a sample of electron density """ if verbose == 0: logging.basicConfig(level=logging.CRITICAL) elif verbose == 1: logging.basicConfig(level=logging.ERROR) else: logging.basicConfig(level=logging.INFO) start = time.time() logger.info("reading inputs {:s} {:s} ".format(name, sample)) mm = Mol2(name) sample_file = sample try: logger.info("reading sample file as npy") sample = np.load(sample) except FileNotFoundError: logger.error("could not find the {:s} file".format(sample)) except OSError: try: logger.info("reading npy was unsuccesful. Trying csv") sample = np.loadtxt(sample) except ValueError: logger.error("could not read sample file neither as npy nor csv") sys.exit() except ValueError: logger.info("reading npy was unsuccesful. Trying csv") sample = np.loadtxt(sample) logger.info("reading of mol2 and sample file was succesful") logger.info("defining model") dm = a2md_from_mol(mm) logger.info("parametrizing") try: if scheme == 'default': dm.parametrize() elif scheme == 'extended': dm.parametrize(dm.parametrization_extended) elif scheme == 'harmonic': dm.parametrize(dm.parametrization_harmonic) elif scheme == 'spheric': dm.parametrize(dm.parametrization_spherical) else: print("use a default, extended, harmonic or spheric scheme") sys.exit(1) except RuntimeError: logger.error("there was some element which is not present in the input parameters") sys.exit() if cluster is not None: logger.info("using clustering of atoms and bonds") if cluster == 'rbf': logger.info("radial basis functions are used as symmetry function") rbf = utils.RBFSymmetryCluster(verbose=False) dm.clustering(rbf.cluster) else: logger.error("no found cluster method {:s}. Aborting".format(cluster)) sys.exit(1) if regularization_constant is not None: logger.info("regularization constat is changed to {:12.4e}".format(regularization_constant)) dm.set_regularization_constant(regularization_constant) logger.info("starting optimization, using a opt_mode={:s}".format(opt_mode)) dm.optimize(sample[:, :3], sample[:, 3], optimization_mode=opt_mode) logger.info("finished optimization") if output is None: output = name.replace('.mol2', '.ppp') if cluster is not None: logger.info("inflating") dm.inflate() logger.info("saving to {:s}".format(output)) with open(output, 'w') as f: json.dump(dm.get_parametrization(), f, indent=4) print("FIT NAME:{:s} SAMPLE:{:s} MODE:{:s}, TE:{:12.4f}".format(name, sample_file, opt_mode, time.time() - start))