Exemple #1
0
def calc_save_property(struct_dir,
                       motif_kwargs={},
                       output_dir='',
                       overwrite=False):
    """
    
    Adds motif to the property information of each Structure
    
    Arguments
    ---------
    struct_dir: file path
    motif_kwargs: see compute_motif
    output_dir: file path
        If no output_dir is provided than Structures will be saved in 
          the struct_dir
    
    """
    struct_dict = read(struct_dir)
    for struct_id, struct in struct_dict.items():
        motif = compute_motif(struct, **motif_kwargs)
        struct.set_property('motif', motif)

    if len(output_dir) == 0:
        output_dir = struct_dir
        overwrite = True
    write(output_dir, struct_dict, file_format='json', overwrite=overwrite)
Exemple #2
0
def calc_and_separate(struct_dir,
                      output_dir,
                      motif_kwargs={},
                      file_format='json',
                      overwrite=False):
    """ 
    
    Calculates the motif for each structure in the directory and then
      separates structures into sub-directories of the output_dir based on
      the structural motif.
    
    """
    struct_dict = read(struct_dir)
    motif_list = implemented_motifs()
    motif_dict = {}
    for i, motif in enumerate(motif_list):
        motif_dict[motif] = i
    output_dicts = [{} for x in range(len(motif_list))]
    for struct_id, struct in struct_dict.items():
        motif = compute_motif(struct, **motif_kwargs)
        index = motif_dict[motif]
        output_dicts[index][struct_id] = struct

    for i, motif in enumerate(motif_list):
        motif_struct_dict = output_dicts[i]
        output_motif_dir = os.path.join(output_dir, motif)
        write(output_motif_dir,
              motif_struct_dict,
              file_format=file_format,
              overwrite=overwrite)
Exemple #3
0
 def write_rstruct(self, folder, file_format="json", overwrite=False):
     """
     Writes the reconstructed geometry from the smallest molecule
     representation.
     """
     struct_id = self.struct.struct_id
     self.rstruct.set_property("num_molecules", len(self.molecules))
     self.rstruct.set_property("num_unique_molecules", len(self.unique))
     path = os.path.join(folder, struct_id)
     write(path, self.rstruct, file_format=file_format, overwrite=overwrite)
Exemple #4
0
    def write(self, output_dir, file_format="json", overwrite=False):
        """
        Write the molecule list.
        
        """
        temp_dict = StructDict()
        for molecule in self.molecules:
            temp_dict.update(molecule)

        write(output_dir,
              temp_dict,
              file_format=file_format,
              overwrite=overwrite)
Exemple #5
0
    def _calc(self):
        my_files = self.get_files(self.struct_path)
        total = len(my_files)
        for file_path in my_files:
            struct = read(file_path)
            self.pymove_class.calc_struct(struct)

            temp_dict = {struct.struct_id: struct}
            write(self.output_path,
                  temp_dict,
                  file_format=self.file_format,
                  overwrite=self.overwrite)

            total -= 1
            if self.verbose:
                print("{}: {}".format(self.rank, total))
Exemple #6
0
    def __setitem__(self, key, struct):
        """
        Adds Structure file to self.directory_path using self.file_format. 
        Updates self.id_dict with relevant struct_id and file_name. 
        
        """
        if type(struct) != Structure:
            raise Exception(
                "Cannot add object of type {}".format(type(self.file_ext)) +
                "to StructDictStream")

        file_name = "{}.{}".format(struct.struct_id, self.file_ext)
        file_path = os.path.join(self.directory_path, file_name)
        write(file_path,
              struct,
              file_format=self.file_format,
              overwrite=self.overwrite)

        ## Now that the structure is written store value in self.id_dict
        self.id_dict[struct.struct_id] = file_name
Exemple #7
0
 def write(self, output_dir, file_format="json", overwrite=False):
     """
     Writes the Driver's output to the to the output_dir argument. The only 
     specification of the Driver.write function for the pymove API are the 
     arguments specified above. The output of a Driver is not necessarily a 
     Structure, although this is the most common situation so the write 
     arguments are tailored for this purpose. In principle, the output of an 
     pymove Driver is not specified by the API and could be anything.  
     
     Usually, a Driver will only need to output a Structure file, with the 
     struct_id as the name of the output file, to the output directory. 
     This is what's done here.
     
     """
     ## Define dictionary for write API.
     temp_dict = {self.struct.struct_id: self.struct}
     write(output_dir,
           temp_dict,
           file_format=file_format,
           overwrite=overwrite)
Exemple #8
0
    def write_unique(self, folder, file_format="json", overwrite=False):
        """
        Writes unique single molecules found for the structure to a single 
        folder.
        """

        if len(self.struct.get_lattice_vectors()) > 0:
            volume = self.struct.get_unit_cell_volume()
            molecule_volume = volume / len(self.molecules)
        else:
            volume = 0
            molecule_volume = 0

        if len(self.unique) == 1:
            molecule_struct = self.unique[0]
            if molecule_volume > 0:
                molecule_struct.set_property("molecule_volume",
                                             molecule_volume)
            temp_dict = {}
            temp_dict[molecule_struct.struct_id] = molecule_struct
            write(folder,
                  temp_dict,
                  file_format=file_format,
                  overwrite=overwrite)
            return

        for i, molecule_struct in enumerate(self.unique):
            if len(self.unique) == 1:
                # Add molecular volume if there's only one type of molecule
                if molecule_volume > 0:
                    molecule_struct.set_property("molecule_volume",
                                                 molecule_volume)
            temp_dict = {}
            temp_dict[molecule_struct.struct_id] = molecule_struct
            write(folder,
                  temp_dict,
                  file_format=file_format,
                  overwrite=overwrite)
Exemple #9
0
    def write(self, filename="", mode="file"):
        """
        Write just from rank 0

        Arguments
        ---------
        filename: str
            Filename when mode is "file"
            Directory name when mode is "dict"
        mode: str
            Described in main docstring. If a mode is provided
            in the instantiation, then this will overwrite
            any setting used here.

        """
        if self.rank != 0:
            return

        if len(self.write_mode) > 0:
            mode = self.write_mode

        if self.all_results == None:
            raise Exception("Called JobParallel write without running " +
                            "calculations first.")

        if len(filename) == 0:
            temp_date = datetime.datetime.now()
            temp_date = temp_date.strftime("%Y-%m-%d_%H-%M-%S")

            if len(self.name) != 0:
                basename = self.name
            else:
                basename = "JobParallel"

            if mode == "file":
                filename = "{}_Results_{}.json".format(basename, temp_date)
            else:
                filename = "{}_Results_{}".format(basename, temp_date)

        if mode == "file":
            ## This will make all_results serializable through recursive
            ## calls
            self.all_results = self.json_serialize(self.all_results)
            with open(filename, "w") as f:
                f.write(json.dumps(self.all_results))
        elif mode == "dict":
            write_dict = {}
            for entry in self.all_results:
                settings = entry[0]
                if len(settings) == 3:
                    struct = settings[1]
                elif len(settings) == 2:
                    kwargs = settings[-1]
                    if "struct" in kwargs:
                        struct = kwargs["struct"]
                    else:
                        raise Exception(
                            "Structure could not be found in " +
                            "settings. Please contact Manny for help.")

                result = entry[-1]
                result = self.json_serialize(result)

                if len(self.name) > 0:
                    if self.name in struct.properties:
                        prop_name = "{}_JobParallel_Result".format(self.name)
                    else:
                        prop_name = self.name
                    struct.properties[prop_name] = result
                else:
                    ## This should really not be used, but it will be there
                    ## for mistakes. Ideally, the func using in the job_list
                    ## will already modify the structure properties
                    struct.properties["Temp_JobParallel_Result"] = result

                write_dict[struct.struct_id] = struct

            write(filename, write_dict, overwrite=True)

        else:
            raise Exception(
                "Write mode was not recognized: {}".format(mode) +
                " Please use one of {}".format(self.allowed_write_modes))
Exemple #10
0
###############################################################################

fm = FindMolecules(
                 mult=1.05, 
                 residues=1,
                 conformation=False, 
                 formula_check=False)


struct = read("../Example_Structures/crystals/BENZEN.cif")
fm.calc_struct(struct)

print("{} molecules found: {}".format(struct.struct_id, len(fm.molecules)))
print("{} unique molecules: {}".format(struct.struct_id, len(fm.unique)))

write(fm.unique[0].struct_id, fm.unique[0], file_format="xyz", overwrite=True)

###############################################################################
#### Example to iteratively process multiple crystal structures            ####
###############################################################################

fm = FindMolecules(
                 mult=1.05, 
                 conformation=False, 
                 formula_check=False,
                 residues=1)
struct_dict = read("../Example_Structures/crystals")
output_stream = SDS("Unique_Molecules", file_format="xyz", overwrite=True)

for struct_id,struct in struct_dict.items():
    fm.calc_struct(struct)
Exemple #11
0
 def write(self, output_dir, file_format="json", overwrite=False):
     temp_dict = {self.supercell.struct_id: self.supercell}
     write(output_dir,
           temp_dict,
           file_format=file_format,
           overwrite=overwrite)