Example #1
0
def exe_ml(**kwargs):
    # Sort input keyword arguments
    order = ['plot', 'network', 'stencil', 'layer', 'activation', 'batch_size', 'epochs', 'learning_rate', 'neg', 'angle', 'rot', 'data', 'smearing', 'hf', 'hf_correction', 'dropout', 'plotdata', 'flip', 'cut', 'dshift', 'shift', 'bias', 'interpolate', 'edge', 'edge2', 'custom_loss', 'gauss', 'unsharp_mask', 'amount', 'load_data',  'seed', 'addstring', 'stz_kappa', 'normalize', 'interpolate_lower']
    kwargs = {k: kwargs[k] for k in order}
    plot = kwargs.get('plot')
    # Execute machine learning
    '''
    for job in list(itpd(*kwargs.values())):
        ml(**dict(zip(kwargs.keys(), job)))
    # '''
    # '''
    if not plot[0]: # Execute training job list with multithreading
        kwargs['plotdata'] = [False]
        jobs = []
        job_list = list(itpd(*kwargs.values()))
        for job in job_list:
            # Add cpu affinity
            job = job + (int(job_list.index(job)),)
            jobs.append(Process(target=ml, args=job))
        [j.start() for j in jobs]
        [j.join() for j in jobs]
    elif plot[0]:
        # Execute validation job list with multithreading
        for job in list(itpd(*kwargs.values())):
            ml(**dict(zip(kwargs.keys(), job)))
Example #2
0
def exe_save(**kwargs):
    # Sort input keyword arguments
    order = ['plot', 'network', 'stencil', 'layer', 'activation', 'batch_size', 'epochs', 'learning_rate', 'neg', 'angle', 'rot', 'data', 'smearing', 'hf', 'hf_correction', 'dropout', 'plotdata', 'flip', 'cut', 'dshift', 'shift', 'bias', 'interpolate', 'edge', 'edge2', 'custom_loss', 'gauss', 'unsharp_mask', 'amount', 'load_data',  'seed', 'addstring', 'stz_kappa', 'normalize', 'interpolate_lower']
    kwargs = {k: kwargs[k] for k in order}
    # Execute saving job list with multithreading
    for job in list(itpd(*kwargs.values())):
        save(**dict(zip(kwargs.keys(), job)))
    def ModifyMolInPlace(self, reactants):
        # This applies transformation for all the matching pattern found in mol
        if len(self.reactantquery) != 1:
            raise ReactionQueryError('ModifyMolInPlace: Only usable for',
                                     'monomolecular transformation')
        if isinstance(reactants, Chem.Mol):
            reactants = [reactants]
        elif isinstance(reactants, tuple):
            reactants = list(reactants)
        # Error test
        if len(reactants) != len(self.reactantquery):
            raise ReactionQueryError('Number of reactant mis-match')
        for i in range(0, len(reactants)):
            if not isinstance(reactants[i], Chem.Mol):
                raise ReactionQueryError("Unrecognized instance'" +
                                         type(reactants[i]) + "'")
            reactants[i] = Chem.AddHs(reactants[i])
        # make matrix of matching index
        self.match_indexes = list()
        i = 0
        for reactant_name in self.reactantquery:
            if isinstance(self.reactantquery[reactant_name], Chem.Mol):
                match = reactants[i].GetSubstructMatches(
                    self.reactantquery[reactant_name])
                if not match:
                    return tuple()
                else:
                    self.match_indexes.append(match)
            elif isinstance(self.reactantquery[reactant_name], MolQuery):
                match = self.reactantquery[reactant_name].\
                    GetQueryMatches(reactants[i])
                if not match:
                    return tuple()
                else:
                    self.match_indexes.append(match)
            i += 1
        # make a index for combined reactants mol object
        self.combined_mol_match_index = list()
        for match in itpd(*self.match_indexes):
            row = list()
            modifier = 0
            for i in range(0, len(match)):
                if i != 0:
                    modifier = reactants[i - 1].GetNumAtoms()
                row += list(
                    map(add, list(match[i]), [modifier] * len(match[i])))
            self.combined_mol_match_index.append(row)
        # Combine reactants
        self.combined_mol = reactants[0]
        for i in range(1, len(reactants)):
            self.combined_mol = Chem.CombineMols(self.combined_mol,
                                                 reactants[i])

        self.combined_mol = Chem.RWMol(self.combined_mol)
        # Transform!!        product_list = list()
        for matches in self.combined_mol_match_index:
            for transform in self.transformations:
                transform(self.combined_mol, matches)
        return self.combined_mol
    def RunReactants(self, reactants):
        if isinstance(reactants,Chem.Mol):
            reactants = [reactants]
        elif isinstance(reactants,tuple):
            reactants = list(reactants)
        # Error test
        if len(reactants) != len(self.reactantquery):
            raise ReactionQueryError('Number of reactant mis-match')
        for i in xrange(0,len(reactants)):
            if not isinstance(reactants[i],Chem.Mol):
                raise ReactionQueryError("Unrecognized instance'"+type(reactants[i])+"'")
            reactants[i] = Chem.AddHs(reactants[i])
        # make matrix of matching index
        self.match_indexes = list()
        i = 0
        for reactant_name in self.reactantquery:
            if isinstance(self.reactantquery[reactant_name],Chem.Mol):
                match = reactants[i].GetSubstructMatches(self.reactantquery[reactant_name])
                if not match:
                    return tuple()
                else:
                    self.match_indexes.append(match)
            elif isinstance(self.reactantquery[reactant_name],MolQuery):
                match = self.reactantquery[reactant_name].GetQueryMatches(reactants[i])
                if not match:
                    return tuple()
                else:
                    self.match_indexes.append(match)
            i += 1
        # make a index for combined reactants mol object
        self.combined_mol_match_index = list()
        for match in itpd(*self.match_indexes):
            row = list()
            modifier = 0
            for i in xrange(0,len(match)):
                if i != 0:
                    modifier = reactants[i-1].GetNumAtoms()
                row += map(add,list(match[i]),[modifier]*len(match[i]))
            self.combined_mol_match_index.append(row)
        # Combine reactants
        self.combined_mol = reactants[0]
        for i in xrange(1,len(reactants)):
            self.combined_mol = Chem.CombineMols(self.combined_mol,reactants[i])

        self.combined_mol = Chem.RWMol(self.combined_mol)
        # Transform!!
        product_list = list()
        for matches in self.combined_mol_match_index:
            products = self.combined_mol.__copy__()
            for transform in self.transformations:
                transform(products, matches)
            products = Chem.GetMolFrags(products,asMols=True,sanitizeFrags=False)
            product_list.append(products)
        return tuple(product_list)
Example #5
0
def exe_save(**kwargs):
    # Sort input keyword arguments
    order = [
        'plot', 'network', 'stencil', 'layer', 'activation', 'epochs',
        'learning_rate', 'neg', 'angle', 'rot', 'data', 'smearing', 'hf',
        'hf_correction', 'dropout', 'plotdata', 'addstring'
    ]
    kwargs = {k: kwargs[k] for k in order}
    # Execute saving job list with multithreading
    for job in list(itpd(*kwargs.values())):
        save(**dict(zip(kwargs.keys(), job)))
Example #6
0
def exe_ml(**kwargs):
    # Sort input keyword arguments
    order = [
        'plot', 'network', 'stencil', 'layer', 'activation', 'epochs',
        'learning_rate', 'neg', 'angle', 'rot', 'data', 'smearing', 'hf',
        'hf_correction', 'dropout', 'plotdata', 'addstring'
    ]
    kwargs = {k: kwargs[k] for k in order}
    # Execute machine learning
    plot = kwargs.get('plot')
    if not plot[0]:  # Execute training job list with multithreading
        kwargs['plotdata'] = [False]
        jobs = []
        for job in list(itpd(*kwargs.values())):
            jobs.append(Process(target=ml, args=job))
        [j.start() for j in jobs]
        [j.join() for j in jobs]
    elif plot[0]:
        # Execute validation job list with multithreading
        for job in list(itpd(*kwargs.values())):
            ml(**dict(zip(kwargs.keys(), job)))
Example #7
0
def exe_dg(**kwargs):
    print(f'kwargs:\n{kwargs}')
    # Sort input keyword arguments
    order = ['N_values', 'stencils', 'ek', 'neg', 'silent', 'geometry', 'smearing', 'usenormal', 'dshift', 'interpolate', 'gauss', 'stz_kappa', 'interpolate_lower']
    kwargs = {k: kwargs[k] for k in order}
    # Create job list according to input arguments
    job_list = list(itpd(*kwargs.values()))
    if len(job_list) > 1:
        # Execute job list with multithreading
        jobs = []
        for job in job_list:
            # Add cpu affinity
            job = job + (int(job_list.index(job)),)
            jobs.append(Process(target=generate_data, args=job))
        [j.start() for j in jobs]
        [j.join() for j in jobs]
    else:
        # Execute job
        generate_data(**dict(zip(kwargs.keys(), job_list[0])))
Example #8
0
def exe_dg(**kwargs):
    print(f'kwargs:\n{kwargs}')
    # Sort input keyword arguments
    order = [
        'N_values', 'stencils', 'ek', 'neg', 'silent', 'geometry', 'smearing',
        'usenormal'
    ]
    kwargs = {k: kwargs[k] for k in order}
    # Create job list according to input arguments
    job_list = list(itpd(*kwargs.values()))
    if len(job_list) > 1:
        # Execute job list with multithreading
        jobs = []
        [
            jobs.append(Process(target=generate_data, args=job))
            for job in job_list
        ]
        [j.start() for j in jobs]
        [j.join() for j in jobs]
    else:
        # Execute job
        generate_data(**dict(zip(kwargs.keys(), job_list[0])))
Example #9
0
def GenerateRxnNet(initial_reactant, reaction_rules):
    """
    Generates reaction network following the algorithm from
    (Ind. Eng. Chem. Res. 2010, 49 (21), 10459-10470)

    Arguments:
    - initial_reactant:     can be smiles or mol
    - reaction_rules:       can be smarts string or rdkit.Chem.rdChemReactions.
                            ChemicalReaction object

    Return:
    - list of reaction intermediates

    Example:
    Ethane C-H scission and C-C scission
    A = generate_rxn_net('CC',['[C:1][H:2]>>[C:1].[H:2]',
                         '[C:1][C:2]>>[C:1].[C:2]'])
    for product in A:
        print(Chem.MolToSmiles(product))

    Tip:
    For dehydrogenation, you must number carbon and hydrogen i.e.
    [C][H]>>[C].[H]             (x)
    [C:1][H:1]>>[C:1].[H:2]     (o)
    Same goes for changing bond order
    [C][C]>>[C]=[C]             (x)
    [C:1][C:2]>>[C:1]=[C:2]     (o)
    Aromatization and Kekulize has some problem with several species. So, we
    don't set aromatization for sanitize, and try kekulize.

    TODO:
    - record reactions as well (make it an option as it's expensive)

    """

    # set-up reactants
    if not isinstance(initial_reactant, list):
        initial_reactant = [initial_reactant]
    if isinstance(initial_reactant[0], str):
        for i in range(0, len(initial_reactant)):
            initial_reactant[i] = Chem.MolFromSmiles(initial_reactant[i],
                                                     sanitize=False)
            # sanitize everything except aromatization set
            _sanitize_except_aromatization(initial_reactant[i])
    # Treatment necessary for radicals
    # (https://github.com/rdkit/rdkit/issues/69)
    for i in range(0, len(initial_reactant)):
        # print Chem.MolToSmiles(initial_reactant[i])
        initial_reactant[i] = Chem.AddHs(initial_reactant[i])
        # sanitize everything except aromatization set
        _sanitize_except_aromatization(initial_reactant[i])
        # Chem.SanitizeMol(initial_reactant[i])
        # Chem.Kekulize(initial_reactant[i])
        for atoms in initial_reactant[i].GetAtoms():
            atoms.SetNoImplicit(True)
        Chem.AssignRadicals(initial_reactant[i])

    # set up reactions
    if not isinstance(reaction_rules, list):
        reaction_rules = [reaction_rules]
    if isinstance(reaction_rules[0], str):
        for i in range(0, len(reaction_rules)):
            try:
                reaction_rules[i] = Read(reaction_rules[i])
            except Exception:
                reaction_rules[i] = ReactionFromSmarts(reaction_rules[i])

    # generator main algorithm
    unprocessed = initial_reactant
    processed = []
    while unprocessed:
        # Pop a molecule and put it in a processed list
        reactant0 = unprocessed[0]
        processed.insert(0, unprocessed[0])
        del unprocessed[0]
        # go through all reactions
        for reaction_rule in reaction_rules:
            # set up reactant list.
            # Generate combinatorial product list of reactants if reaction
            # requires several reactants
            reactant_list = itpd([list(range(1, len(processed)))],
                                 repeat=reaction_rule.
                                 GetNumReactantTemplates()-1)
            # go through each set of reactants
            for reactant_indexes in reactant_list:
                # Reaction
                # Make the reactant mol tuple (Runreactants only accept tuple)
                reactants = (reactant0,)
                for reactant_index in reactant_indexes:
                    reactants += (processed[reactant_index],)
                # React
                ele_reactions = reaction_rule.RunReactants(reactants)

                # Record reactions (TODO)

                # Pre-processing products
                # Go through reactiosn and make a single list of products
                products = []
                for ele_reaction in ele_reactions:
                    for mol in ele_reaction:
                        products.append(mol)
                # Treatment necessary for radicals
                # (https://github.com/rdkit/rdkit/issues/69)
                for mol in products:
                    for atoms in mol.GetAtoms():
                        atoms.SetNoImplicit(True)
                        atoms.UpdatePropertyCache(strict=False)
                    Chem.AssignRadicals(mol)
                    # Remove molecule with atoms with over valence
                for i in range(len(products)-1, -1, -1):
                    for atoms in products[i].GetAtoms():
                        if PeriodicTable.GetDefaultValence(GetPeriodicTable(),
                                                           atoms.GetAtomicNum()
                                                           ) < \
                             atoms.GetTotalValence():
                            del products[i]
                            break
                # remove duplicates
                # TODO. This removes also species with different charges
                for i in range(len(products)-1, -1, -1):
                    for j in range(0, i):
                        if products[i].GetNumAtoms() ==\
                            products[j].GetNumAtoms() and \
                            products[i].GetNumAtoms() ==\
                                len(products[i].
                                    GetSubstructMatch(products[j])):

                            del products[i]
                            break
                # update unprocessed molecule list
                # check for duplicate and append to unprocessed_list if missing
                for mol1 in products:
                    inthelist = 0
                    for mol2 in processed:
                        # first check the nubmer of atoms and then
                        # look for substructure match
                        if mol1.GetNumAtoms() == mol2.GetNumAtoms() and \
                            mol1.GetNumAtoms() == len(mol1.GetSubstructMatch
                                                      (mol2)):
                            # if it's in processed list, break
                            inthelist = 1
                            break
                    # not in the processed list. append to unprocessed
                    if inthelist == 0:
                        unprocessed.insert(0, mol1)
    # Prettify
    for i in range(0, len(processed)):
        # print Chem.MolToSmiles(processed[i])
        processed[i] = Chem.RemoveHs(processed[i], sanitize=False)
        _sanitize_except_aromatization(processed[i])
        # print Chem.MolToSmiles(processed[i])
    return processed