def main(out):
    out.write(prelude)

    i = 0

    for mdl in all_descriptors():
        mdl_name = '.'.join(mdl.__name__.split('.'))
        mdl_ppr = ':py:mod:`~{}`'.format(mdl_name)
        first = True

        for Desc in get_descriptors_from_module(mdl):

            for desc in Desc.preset():
                i += 1

                if not first:
                    mdl_ppr = ''

                cnst = desc.__class__.__name__
                args = ', '.join(Descriptor._pretty(p) for p in desc.parameters())

                cnst = ':py:class:`~{}.{}` ({})'.format(mdl_name, cnst, args)

                dim = '3D' if desc.require_3D else '2D'

                out.write('    {}, {}, {}, "{}", {}\n'.format(i, mdl_ppr, desc, cnst, dim))

                first = False
def test_by_references():
    calc = Calculator(
        d
        for d in all_descriptors()
        if d.__class__ not in [Polarizability.APol, Polarizability.BPol]
    )

    calc.register([
        Polarizability.APol(True),
        Polarizability.BPol(True),
    ])

    actuals = dict()
    for mol in Chem.SDMolSupplier(os.path.join(data_dir, 'structures.sdf'), removeHs=False):
        actuals[mol.GetProp('_Name')] = {str(d): v for d, v in zip(calc.descriptors, calc(mol))}

    for path in glob(os.path.join(data_dir, '*.yaml')) + glob(os.path.join(data_dir, '**/*.yaml')):
        for test in yaml.load(open(path), Loader=Loader):
            dnames = test['names']
            if not isinstance(dnames, list):
                dnames = [dnames]

            desireds = (
                (mname, zip(dnames, values if isinstance(values, list) else [values]))
                for mname, values in test['results'].items()
            )

            digit = test.get('digit')
            if digit is None:
                assert_f = eq_
            else:
                def assert_f(a, d, m):
                    if np.isnan(d):
                        assert isinstance(a, MissingValueBase)
                        return

                    assert_almost_equal(a, d, digit, m)

            for mname, descs in desireds:
                for dname, desired in descs:
                    if not desired == 'skip':
                        yield (
                            assert_f,
                            actuals[mname][dname],
                            desired,
                            '{} of {}'.format(dname, mname)
                        )
Exemplo n.º 3
0
def test_parallel():
    calc = Calculator(all_descriptors())
    mols = list(map(Chem.AddHs, [
        Chem.MolFromSmiles('c1ccccc1'),
        Chem.MolFromSmiles('C1=CC(=C(C=C1C2=C(C=C3C(=CC(=CC3=[O+]2)O)O)O)O)O'),
        Chem.MolFromSmiles('CCCCCC'),
    ]))

    for mol in mols:
        Chem.EmbedMolecule(mol)

    for serial, parallel in zip(calc.map(mols, nproc=1, quiet=True),
                                calc.map(mols, quiet=True)):
        for d, s, p in zip(calc.descriptors, serial, parallel):
            if isinstance(s, MissingValueBase):
                yield eq_, pickle.dumps(s), pickle.dumps(p), str(d)
            else:
                yield assert_almost_equal, s, p, 7, str(d)
Exemplo n.º 4
0
def test_pickle_calculator():
    orig = Calculator(all_descriptors())
    d0 = orig.descriptors[0]
    d1 = orig.descriptors[1]
    orig.register([
        d0 + d1, d0 - d1, d0 * d1, d0 // d1, d0 % d1, d0 ** d1,
        -d0, +d1, abs(d0), math.trunc(d0),
    ])

    if six.PY3:
        orig.register([math.ceil(d0), math.floor(d1)])

    pickled = pickle.loads(pickle.dumps(orig))

    mol = Chem.MolFromSmiles('c1ccccc1C(O)O')

    for a, b in zip(orig.descriptors, pickled.descriptors):
        yield eq_, a, b

    for a, b in zip(orig(mol), pickled(mol)):
        if isinstance(a, MissingValueBase):
            yield eq_, a.__class__, b.__class__
        else:
            yield assert_almost_equal, a, b
    def member(self, member):
        if is_descriptor_class(member):
            for line in self.descriptor(member):
                yield line
        else:
            raise ValueError('unknown member {} in {}'.format(member, self.mdl.__name__))

    def members(self):
        for m in self.mdl.__all__:
            for line in self.member(getattr(self.mdl, m)):
                yield line

    def module(self):
        yield '.. automodule:: {}'.format(self.mdl.__name__)

    def print(self, file=sys.stdout):
        for line in chain(self.header(), self.module(), self.members()):
            print(line, file=file)


if __name__ == '__main__':
    print('Submodules')
    print('----------')
    print('')
    print('.. toctree::')

    for mdl in all_descriptors():
        print('    ' + mdl.__name__)
        with open('api/{}.rst'.format(mdl.__name__), 'w') as f:
            Generator(mdl).print(file=f)