MSchauperl / resppol

Compare 9c01119 ... +0 ... 598132b

Coverage Reach
resppol.py utilities.py __init__.py

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 1 of 3 files from the diff.

@@ -221,7 +221,7 @@
Loading
221 221
            # noinspection PyTypeChecker
222 222
            self.add_molecule(Molecule(mol2file))
223 223
224 -
    def add_molecule(self, datei,am1_mol2_file=None):
224 +
    def add_molecule(self, datei,am1_file=None):
225 225
        """
226 226
        Adds a molecule to the TrainingSet object.
227 227
        :param datei: Mol2 file of a molecule
@@ -230,7 +230,7 @@
Loading
230 230
        #ToDo Check matrix composition
231 231
232 232
        # Adds the molecule
233 -
        self.molecules.append(Molecule(datei, position=self.number_of_lines_in_X, trainingset=self,id=len(self.molecules),am1_mol2_file=am1_mol2_file))
233 +
        self.molecules.append(Molecule(datei, position=self.number_of_lines_in_X, trainingset=self,id=len(self.molecules),am1_file=am1_file))
234 234
        log.info('Added molecule number {} from file {}.'.format(len(self.molecules)-1,datei))
235 235
        # Defines the position of the molecule in the overall matrix.
236 236
        #   A   0       CT          =   B
@@ -395,7 +395,7 @@
Loading
395 395
        converged = False
396 396
        self.counter =0
397 397
        # Remove potential from AM1 charges
398 -
        self.substract_am1_potential()
398 +
        #self.substract_am1_potential()
399 399
        while converged == False and self.counter <100:
400 400
            log.warning('Optimization Step {}'.format(self.counter))
401 401
            self.optimize_bcc_alpha_step()
@@ -469,7 +469,7 @@
Loading
469 469
        :return:
470 470
        """
471 471
472 -
    def __init__(self, datei, position=0, trainingset=None,id=None,am1_mol2_file=None):
472 +
    def __init__(self, datei, position=0, trainingset=None,id=None,am1_file=None):
473 473
474 474
        # Get Molecle name from filename
475 475
        self.id = id
@@ -581,17 +581,18 @@
Loading
581 581
582 582
        # Load in charges from mol2 file:
583 583
        self.q_am1 = None
584 -
        if am1_mol2_file != None:
585 -
            self.q_am1 = []
586 -
            self.am1mol = oechem.OEMol()
587 -
            # Open Input File Stream
588 -
            ifs = oechem.oemolistream(am1_mol2_file)
589 -
590 -
            # Read from IFS to molecule
591 -
            # Create OE molecule
592 -
            oechem.OEReadMol2File(ifs, self.am1mol)
593 -
            for atom in self.am1mol.GetAtoms():
594 -
                self.q_am1.append(atom.GetPartialCharge())
584 +
        if am1_file != None:
585 +
            if '.mol2' in am1_file:
586 +
                self.q_am1 = []
587 +
                self.am1mol = oechem.OEMol()
588 +
                # Open Input File Stream
589 +
                ifs = oechem.oemolistream(am1_file)
590 +
591 +
                # Read from IFS to molecule
592 +
                # Create OE molecule
593 +
                oechem.OEReadMol2File(ifs, self.am1mol)
594 +
                for atom in self.am1mol.GetAtoms():
595 +
                    self.q_am1.append(atom.GetPartialCharge())
595 596
596 597
        # Number of lines for matrix X
597 598
        if self._mode == 'q':
@@ -732,6 +733,7 @@
Loading
732 733
        self.alpha = [np.dot(self.R[i], alphas) for i in range(len(self.R))]
733 734
        self.q_alpha[self._lines_in_A:self._lines_in_A + 3* len(self.alpha)] = np.concatenate(
734 735
            (np.concatenate((self.alpha, self.alpha)), self.alpha))
736 +
735 737
        self.q = self.q_alpha[:self._natoms]
736 738
        for conformer in self.conformers:
737 739
            conformer.q_alpha = self.q_alpha
@@ -929,9 +931,7 @@
Loading
929 931
        self.baseESP = None
930 932
        self.polESPs = list()
931 933
        self._molecule = molecule
932 -
        self.q_alpha = self._molecule.q_alpha
933 934
        self._lines_in_A = self._molecule._lines_in_A
934 -
        self.q_am1 = self._molecule.q_am1
935 935
936 936
        # Initiliaze Electric field vectors
937 937
        self.e_field_at_atom = np.zeros((3, self.natoms))
@@ -961,6 +961,15 @@
Loading
961 961
            if atom_is_present == 0:
962 962
                raise Exception("ESP grid does not belong to the conformer")
963 963
964 +
        #Substract the AM1 Potential if AM1 charges are specified
965 +
        if self._molecule.q_am1 != None:
966 +
            # calculated diatomic distances between atoms
967 +
            self.get_distances()
968 +
            # substract potential
969 +
            self.baseESP.substract_am1_potential()
970 +
            # delete distances()
971 +
            self.delete_distances()
972 +
964 973
    def add_polESP(self, *args, e_field=Q_([0.0, 0.0, 0.0], 'bohr')):
965 974
        """
966 975
        Adds the unpolarized molecule to this conformation.
@@ -975,6 +984,15 @@
Loading
975 984
        """
976 985
        self.polESPs.append(BCCPolESP(*args, conformer=self, e_field=e_field))
977 986
987 +
        #Substract the AM1 Potential if AM1 charges are specified
988 +
        if self._molecule.q_am1 != None:
989 +
            # calculated diatomic distances between atoms
990 +
            self.get_distances()
991 +
            # substract potential
992 +
            self.polESPs[-1].substract_am1_potential()
993 +
            # delete distances()
994 +
            self.delete_distances()
995 +
978 996
    # Build the matrix A for the charge optimization
979 997
    def build_matrix_A(self):
980 998
@@ -1362,35 +1380,35 @@
Loading
1362 1380
        for polESP in self.polESPs:
1363 1381
            polESP.get_electric_field()
1364 1382
1365 -
    def substract_am1_potential(self):
1366 -
        self.get_distances()
1367 -
        for ESPGRID in [self.baseESP] + self.polESPs:
1368 -
            ESPGRID.substract_am1_potential()
1369 -
        self.delete_distances()
1370 -
1371 -
    def optimize_charges_alpha(self):
1372 -
        """
1373 -
        Builds the necessary matrix and vector and performs a charge and polarizabilities optimization for this 1 conformation.
1374 -
        :return:
1375 -
        """
1376 -
        self.build_matrix_X()
1377 -
        self.build_vector_Y()
1378 -
        self.q_alpha = np.linalg.solve(self.X, self.Y)
1379 -
1380 -
    def optimize_charges_alpha_bcc(self):
1381 -
        """
1382 -
        Builds the necessary matrix and vector and performs a charge and polarizabilities optimization for this 1 conformation.
1383 -
        :return:
1384 -
        """
1385 -
        self.build_matrix_X_BCC()
1386 -
        self.build_vector_Y_BCC()
1387 -
        # Check if bccs or alphas are not in the training set and set them to zero
1388 -
        for i,row in enumerate(self.X):
1389 -
            if all(row == 0.0):
1390 -
                self.X[i][i]=1
1391 -
1392 -
1393 -
        self.q_alpha = np.linalg.solve(self.X, self.Y)
1383 +
    # def substract_am1_potential(self):
1384 +
    #     self.get_distances()
1385 +
    #     for ESPGRID in [self.baseESP] + self.polESPs:
1386 +
    #         ESPGRID.substract_am1_potential()
1387 +
    #     self.delete_distances()
1388 +
1389 +
    # def optimize_charges_alpha(self):
1390 +
    #     """
1391 +
    #     Builds the necessary matrix and vector and performs a charge and polarizabilities optimization for this 1 conformation.
1392 +
    #     :return:
1393 +
    #     """
1394 +
    #     self.build_matrix_X()
1395 +
    #     self.build_vector_Y()
1396 +
    #     self.q_alpha = np.linalg.solve(self.X, self.Y)
1397 +
1398 +
    # def optimize_charges_alpha_bcc(self):
1399 +
    #     """
1400 +
    #     Builds the necessary matrix and vector and performs a charge and polarizabilities optimization for this 1 conformation.
1401 +
    #     :return:
1402 +
    #     """
1403 +
    #     self.build_matrix_X_BCC()
1404 +
    #     self.build_vector_Y_BCC()
1405 +
    #     # Check if bccs or alphas are not in the training set and set them to zero
1406 +
    #     for i,row in enumerate(self.X):
1407 +
    #         if all(row == 0.0):
1408 +
    #             self.X[i][i]=1
1409 +
    #
1410 +
    #
1411 +
    #     self.q_alpha = np.linalg.solve(self.X, self.Y)
1394 1412
1395 1413
1396 1414
    # i constantly have to delete the distatnce matrixes as storing them is too expensive in terms of memory
@@ -1572,40 +1590,41 @@
Loading
1572 1590
            self.positions = Q_(np.loadtxt(gridfile), 'angstrom')
1573 1591
            self.esp_values = Q_(np.loadtxt(espfile), 'elementary_charge / bohr')
1574 1592
1593 +
1575 1594
    def get_electric_field(self, ):
1576 1595
        """
1577 1596
        Calculates the electric field at every atomic positions.
1578 1597
        :return:
1579 1598
        """
1580 -
        alpha = self._conformer.q_alpha[self._conformer._lines_in_A:self._conformer._lines_in_A + 3*self._conformer.natoms]
1599 +
        alpha = self._molecule.q_alpha[self._conformer._lines_in_A:self._conformer._lines_in_A + 3*self._conformer.natoms]
1581 1600
        alpha[np.where(alpha == 0.0)] += 10E-10
1582 1601
1583 1602
        # Load permanent charges for BCC method
1584 1603
        # For all other methods this is set to 0.0 STILL HAVE to implment
1585 -
        if self._conformer.q_am1 is None:
1604 +
        if self._molecule.q_am1 is None:
1586 1605
            log.warning('I do not have AM1-type charges')
1587 -
            self._conformer.q_am1 = np.zeros(self._conformer.natoms)
1606 +
            self._molecule.q_am1 = np.zeros(self._conformer.natoms)
1588 1607
        #else:
1589 1608
        #    log.info('Substracting AM1 charge potential from the ESP values')
1590 1609
        # ToDo Add Code to substract am1 charge potential
1591 1610
1592 1611
        for j in range(self._conformer.natoms):
1593 1612
            self.e_field_at_atom[0][j] = np.dot(
1594 -
                np.multiply(self._conformer.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1613 +
                np.multiply(self._molecule.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1595 1614
                self._conformer.diatomic_dist_x[j]) + np.dot(
1596 -
                np.multiply(self._conformer.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1615 +
                np.multiply(self._molecule.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1597 1616
                self._conformer.diatomic_dist_x[j]) + self._ext_e_field[0].to(
1598 1617
                'elementary_charge / angstrom / angstrom').magnitude
1599 1618
            self.e_field_at_atom[1][j] = np.dot(
1600 -
                np.multiply(self._conformer.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1619 +
                np.multiply(self._molecule.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1601 1620
                self._conformer.diatomic_dist_y[j]) + np.dot(
1602 -
                np.multiply(self._conformer.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1621 +
                np.multiply(self._molecule.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1603 1622
                self._conformer.diatomic_dist_y[j]) + self._ext_e_field[1].to(
1604 1623
                'elementary_charge / angstrom / angstrom').magnitude
1605 1624
            self.e_field_at_atom[2][j] = np.dot(
1606 -
                np.multiply(self._conformer.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1625 +
                np.multiply(self._molecule.q_alpha[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1607 1626
                self._conformer.diatomic_dist_z[j]) + np.dot(
1608 -
                np.multiply(self._conformer.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1627 +
                np.multiply(self._molecule.q_am1[:self._conformer.natoms], self._conformer._molecule.scale[j]),
1609 1628
                self._conformer.diatomic_dist_z[j]) + self._ext_e_field[2].to(
1610 1629
                'elementary_charge / angstrom / angstrom').magnitude
1611 1630
@@ -1734,7 +1753,7 @@
Loading
1734 1753
            #    self.q_pot[i]=e_dip
1735 1754
1736 1755
    def substract_am1_potential(self):
1737 -
        self.calc_esp_q_alpha(self._conformer.q_am1, mode='q')
1756 +
        self.calc_esp_q_alpha(self._molecule.q_am1, mode='q')
1738 1757
        self.esp_values = Q_(np.subtract(self.esp_values.to('elementary_charge / angstrom').magnitude, self.q_pot),
1739 1758
                         'elementary_charge / angstrom')
1740 1759
@@ -1781,7 +1800,7 @@
Loading
1781 1800
        for i in range(self._conformer.natoms):
1782 1801
            f.write(
1783 1802
                ' {} {} {} {} {}\n'.format(atoms[i][0], atoms[i][1][0], atoms[i][1][1], atoms[i][1][2],
1784 -
                                           self._conformer._molecule.q_alpha[i]))
1803 +
                                           self._molecule.q_alpha[i]))
1785 1804
        f.write(' ESP VALUES AND GRID POINT COORDINATES. #POINTS =   {}\n'.format(len(self.esp_values)))
1786 1805
        for i in range(len(self.esp_values)):
1787 1806
            try:
@@ -1812,13 +1831,14 @@
Loading
1812 1831
        self.atoms = []
1813 1832
        self.atom_positions = []
1814 1833
1834 +
        self._conformer = conformer
1835 +
        self._molecule = conformer._molecule
1815 1836
        # Checks what grid type psi4 or gaussian was used
1816 1837
        self.define_grid(*args)
1817 1838
1818 1839
        self.esp_values = None
1819 1840
        self.positions = None
1820 1841
1821 -
        self._conformer = conformer
1822 1842
1823 1843
        # External e-field is 0 in all directions
1824 1844
        vector = Q_([0, 0, 0], 'elementary_charge / bohr / bohr')
@@ -1832,6 +1852,7 @@
Loading
1832 1852
        self.e_field_at_atom = np.zeros((3, self._conformer.natoms))
1833 1853
        # No calculation of the e-fields at this stage only initializing
1834 1854
1855 +
1835 1856
# =============================================================================================
1836 1857
# BCCPolESP
1837 1858
# =============================================================================================
@@ -1851,6 +1872,7 @@
Loading
1851 1872
        self.esp_values = None
1852 1873
        self.positions = None
1853 1874
        self._conformer = conformer
1875 +
        self._molecule = conformer._molecule
1854 1876
1855 1877
        # Set external e-field
1856 1878
        self.set_ext_e_field(e_field)

Learn more Showing 1 files with coverage changes found.

Changes in resppol/resppol.py
-7
+7
Loading file...
Files Coverage
resppol 0.14% 91.61%
Project Totals (3 files) 91.61%
Loading