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

Support Python3 & Python2 #51

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
12 changes: 8 additions & 4 deletions chemtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
# pragma pylint: disable=wildcard-import
"""The Main ChemTools Package."""

import sys

from chemtools.wrappers import *
from chemtools.toolbox import *
from chemtools.conceptual import *
from chemtools.denstools import *
from chemtools.utils import *
from chemtools.outputs import *
import horton

if sys.version_info.major == 2:
from chemtools.wrappers2 import *
import horton
horton.log.head_banner = ""
horton.log.foot_banner = ""
else:
from chemtools.wrappers3 import *

horton.log.head_banner = ""
horton.log.foot_banner = ""

__version__ = '0.9.0'
6 changes: 3 additions & 3 deletions chemtools/conceptual/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ def grand_potential_derivative(self, n_elec, order=1):
else:
# higher-order derivatives are compute with Faa Di Bruno formula
# list of hyper-hardneses (derivatives of energy w.r.t. N)
e_deriv = [self.energy_derivative(n_elec, i + 1) for i in xrange(1, order)]
g_deriv = [self.grand_potential_derivative(n_elec, k + 1) for k in xrange(1, order - 1)]
e_deriv = [self.energy_derivative(n_elec, i + 1) for i in range(1, order)]
g_deriv = [self.grand_potential_derivative(n_elec, k + 1) for k in range(1, order - 1)]
if any([item is None for item in e_deriv]) or any([item is None for item in g_deriv]):
deriv = None
else:
deriv = 0
for k in xrange(1, order - 1):
for k in range(1, order - 1):
deriv -= g_deriv[k - 1] * sp.bell(order - 1, k, e_deriv[:order - k])
deriv /= sp.bell(order - 1, order - 1, [e_deriv[0]])
return deriv
Expand Down
5 changes: 2 additions & 3 deletions chemtools/conceptual/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, expr, n0, n_energies, n_symbol=None, n0_symbol=None, guess=No
'the number of electrons.')
self._n_symb = n_symbol
# store minimum and maximum number of electrons used for interpolation
self._n_min, self._n_max = np.min(n_energies.keys()), np.max(n_energies.keys())
self._n_min, self._n_max = np.min(list(n_energies.keys())), np.max(list(n_energies.keys()))

# substitute N0 in energy expression
if n0_symbol:
Expand Down Expand Up @@ -184,7 +184,7 @@ def _solve_parameters(self, expr, n_energies, guess, opts=None):
# construct system of equations to solve
system_eqns = []
d_system_eqns = []
for n, energy in n_energies.iteritems():
for n, energy in n_energies.items():
eqn = sp.lambdify((params,), expr.subs(self._n_symb, n) - energy, 'numpy')
system_eqns.append(eqn)
d_eqn_row = []
Expand Down Expand Up @@ -234,7 +234,6 @@ def _solve_nmax(self, guess):
d_expr = self._expr.diff(self._n_symb)
n_max_eqn = sp.lambdify(self._n_symb, d_expr, 'numpy')
result = root(n_max_eqn, guess)
print result
if result.success:
n_max = np.asscalar(result.x)
# n_ceil = math.ceil(n_max)
Expand Down
2 changes: 1 addition & 1 deletion chemtools/conceptual/leastnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""

import numpy as np
from scipy.misc import factorial
from scipy.special import factorial

from chemtools.utils.utils import doc_inherit
from chemtools.conceptual.base import BaseGlobalTool
Expand Down
4 changes: 2 additions & 2 deletions chemtools/conceptual/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def check_dict_values(dict_values):
# check length of dictionary & its keys
if len(dict_values) != 3 or not all([key >= 0 for key in dict_values.keys()]):
raise ValueError("The energy model requires 3 keys corresponding to positive "
"number of electrons! Given keys={0}".format(dict_values.keys()))
"number of electrons! Given keys={0}".format(list(dict_values.keys())))
# find reference number of electrons
n_ref = sorted(dict_values.keys())[1]
if n_ref < 1:
raise ValueError("The n_ref cannot be less than one! Given n_ref={0}".format(n_ref))
# check that number of electrons differ by one
if sorted(dict_values.keys()) != [n_ref - 1, n_ref, n_ref + 1]:
raise ValueError("In current implementation, the number of electrons (keys) should "
"differ by one! Given keys={0}".format(dict_values.keys()))
"differ by one! Given keys={0}".format(list(dict_values.keys())))
# check that all values have the same type
if not all([isinstance(value, type(dict_values[n_ref])) for value in dict_values.values()]):
raise ValueError("All values in dictionary should be of the same type!")
Expand Down
2 changes: 1 addition & 1 deletion chemtools/outputs/vmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def print_vmd_script_multiple_cube(scriptfile, cubes, isosurfs=None, material='O
raise TypeError('Each iso-surface value must be a float')

if colors is None:
colors = range(len(cubes))
colors = list(range(len(cubes)))
elif not (isinstance(colors, (list, tuple)) and len(colors) == len(cubes)):
raise TypeError('The colors must be provided as a list or tuple of the same length as the '
'number of cube files')
Expand Down
5 changes: 4 additions & 1 deletion chemtools/scripts/chemtools_mot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
"""Molecular Orbital Theory (MOT) Script."""


import sys
import numpy as np

from chemtools.wrappers.molecule import Molecule
from chemtools.toolbox.motbased import OrbPart
from chemtools.scripts.common import help_cube, load_molecule_and_grid

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule


description_mot = """
Visualize Molecular Orbitals (MO) using VMD package.
Expand Down
5 changes: 4 additions & 1 deletion chemtools/scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
"""Common utility for scripts."""


import sys
import numpy as np

from chemtools.wrappers.molecule import Molecule
from chemtools.utils.cube import UniformGrid

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule


help_cube = """
cubic grid used for evaluation and visualization.
Expand Down
12 changes: 9 additions & 3 deletions chemtools/toolbox/conceptual.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
compute various conceptual density functional theory (DFT) descriptive tools.
"""


import sys
import logging

from chemtools.wrappers.molecule import Molecule
from chemtools.wrappers.grid import MolecularGrid
from chemtools.toolbox.utils import check_arg_molecule, get_matching_attr
from chemtools.toolbox.utils import get_dict_energy, get_dict_density, get_dict_population
from chemtools.conceptual.linear import LinearGlobalTool, LinearLocalTool, LinearCondensedTool
Expand All @@ -40,6 +38,14 @@
from chemtools.conceptual.exponential import ExponentialGlobalTool
from chemtools.conceptual.rational import RationalGlobalTool
from chemtools.conceptual.general import GeneralGlobalTool

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
from chemtools.wrappers2.grid import MolecularGrid
else:
from chemtools.wrappers3.molecule import Molecule
from chemtools.wrappers3.grid import MolecularGrid

try:
from pathlib2 import Path
except ImportError:
Expand Down
7 changes: 6 additions & 1 deletion chemtools/toolbox/densbased.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
# pragma pylint: disable=invalid-name
"""Density-Based Local Tools."""

import sys

from chemtools.wrappers.molecule import Molecule
from chemtools.denstools.densbased import DensGradLapKedTool

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule


class DensityLocalTool(DensGradLapKedTool):
"""Density Local Tool Class."""
Expand Down
6 changes: 5 additions & 1 deletion chemtools/toolbox/dftbased.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
"""Density Functional Theory (DFT) Based Tools."""


import sys
import numpy as np

from scipy.optimize import bisect

from chemtools.wrappers.molecule import Molecule
if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule


class DFTBasedTool(object):
Expand Down
7 changes: 6 additions & 1 deletion chemtools/toolbox/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"""Module for (non)bonding interaction analysis of Quantum Chemistry Output Files."""


import sys
import numpy as np

from chemtools.wrappers.molecule import Molecule
from chemtools.denstools.densbased import DensGradTool
from chemtools.utils.utils import doc_inherit
from chemtools.utils.cube import UniformGrid
Expand All @@ -35,6 +35,11 @@

from numpy.ma import masked_less

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule


class BaseInteraction(object):
"""Base class for (non)bonding interactions indicators."""
Expand Down
7 changes: 6 additions & 1 deletion chemtools/toolbox/kinetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
"""Kinetic Energy Density Module."""


import sys
import numpy as np

from chemtools.utils.utils import doc_inherit
from chemtools.wrappers.molecule import Molecule
from chemtools.denstools.densbased import DensGradTool, DensGradLapTool, DensGradLapKedTool

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule


class KED(object):
"""Kinetic Energy Density Class."""
Expand Down
7 changes: 6 additions & 1 deletion chemtools/toolbox/motbased.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
"""Orbital-Based Population Analysis."""


import sys
import numpy as np

from chemtools.utils.utils import doc_inherit
from chemtools.orbstools.partition import OrbitalPartitionTools
from chemtools.wrappers.molecule import Molecule, MolecularOrbitals

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule, MolecularOrbitals
else:
from chemtools.wrappers3.molecule import Molecule, MolecularOrbitals


class OrbPart(object):
Expand Down
9 changes: 7 additions & 2 deletions chemtools/toolbox/oxidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
"""Module for Oxidation State."""


import sys
import itertools
import numpy as np

from chemtools.wrappers.molecule import Molecule
from chemtools.wrappers.part import DensPart
if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
from chemtools.wrappers2.part import DensPart
else:
from chemtools.wrappers3.molecule import Molecule
from chemtools.wrappers3.part import DensPart


class EOS(object):
Expand Down
13 changes: 10 additions & 3 deletions chemtools/toolbox/test/test_conceptual_condensed.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@
"""Test chemtools.analysis.conceptual.CondensedConceptualDFT."""


import sys
import numpy as np

from numpy.testing import assert_raises, assert_equal, assert_almost_equal
from numpy.testing import assert_equal, assert_almost_equal

from chemtools.wrappers.molecule import Molecule
from chemtools.wrappers.grid import MolecularGrid
from chemtools.toolbox.conceptual import CondensedConceptualDFT

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
from chemtools.wrappers2.grid import MolecularGrid
else:
from chemtools.wrappers3.molecule import Molecule
from chemtools.wrappers3.grid import MolecularGrid

try:
from importlib_resources import path
except ImportError:
Expand Down
8 changes: 7 additions & 1 deletion chemtools/toolbox/test/test_conceptual_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
"""Test chemtools.analysis.conceptual.GlobalConceptualDFT."""


import sys
import numpy as np

from numpy.testing import assert_raises, assert_almost_equal

from chemtools.toolbox.conceptual import GlobalConceptualDFT
from chemtools.wrappers.molecule import Molecule

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule

try:
from importlib_resources import path
except ImportError:
Expand Down
11 changes: 9 additions & 2 deletions chemtools/toolbox/test/test_conceptual_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@
"""Test chemtools.analysis.conceptual.LocalConceptualDFT."""


import sys
import numpy as np

from numpy.testing import assert_raises, assert_equal, assert_almost_equal

from chemtools.wrappers.molecule import Molecule
from chemtools.wrappers.grid import MolecularGrid
from chemtools.toolbox.conceptual import LocalConceptualDFT

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
from chemtools.wrappers2.grid import MolecularGrid
else:
from chemtools.wrappers3.molecule import Molecule
from chemtools.wrappers3.grid import MolecularGrid

try:
from importlib_resources import path
except ImportError:
Expand Down
8 changes: 7 additions & 1 deletion chemtools/toolbox/test/test_densbased.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
"""Test chemtools.toolbox.densbased."""


import sys
import numpy as np

from numpy.testing import assert_allclose

from chemtools.wrappers.molecule import Molecule
from chemtools.toolbox.densbased import DensityLocalTool

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule

try:
from importlib_resources import path
except ImportError:
Expand Down
8 changes: 7 additions & 1 deletion chemtools/toolbox/test/test_dftbased.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
"""Test chemtools.toolbox.dftbased."""


import sys
import numpy as np

from numpy.testing import assert_raises, assert_array_almost_equal

from chemtools.wrappers.molecule import Molecule
from chemtools.toolbox.dftbased import DFTBasedTool

if sys.version_info.major == 2:
from chemtools.wrappers2.molecule import Molecule
else:
from chemtools.wrappers3.molecule import Molecule

try:
from importlib_resources import path
except ImportError:
Expand Down
1 change: 1 addition & 0 deletions chemtools/toolbox/test/test_elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import numpy as np
from numpy.testing import assert_allclose, assert_raises
from chemtools.toolbox.interactions import ELF, LOL

try:
from importlib_resources import path
except ImportError:
Expand Down
Loading