本文整理汇总了Python中monty.io.zopen函数的典型用法代码示例。如果您正苦于以下问题:Python zopen函数的具体用法?Python zopen怎么用?Python zopen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zopen函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_zopen
def test_zopen(self):
with zopen(os.path.join(test_dir, "myfile_gz.gz"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_bz2.bz2"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile_bz2.bz2"), "rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
with zopen(os.path.join(test_dir, "myfile"), mode="rt") as f:
self.assertEqual(f.read(), "HelloWorld.\n\n")
开发者ID:davidwaroquiers,项目名称:monty,代码行数:9,代码来源:test_io.py
示例2: read_excitation_energies
def read_excitation_energies(self):
"""
Read a excitation energies after a TD-DFT calculation.
Returns:
A list: A list of tuple for each transition such as
[(energie (eV), lambda (nm), oscillatory strength), ... ]
"""
float_patt = re.compile("\s*([+-]?\d+\.\d+)")
transitions = list()
# read in file
with zopen(self.filename, "r") as f:
line = f.readline()
td = False
while line != "":
if re.search("^\sExcitation energies and oscillator strengths:", line):
td = True
if td:
if re.search("^\sExcited State\s*\d", line):
val = [float(v) for v in float_patt.findall(line)]
transitions.append(tuple(val[0:3]))
line = f.readline()
return transitions
开发者ID:boykov,项目名称:pymatgen,代码行数:27,代码来源:gaussian.py
示例3: atoms_string_from_file
def atoms_string_from_file(filename):
"""
Reads atomic shells from file such as feff.inp or ATOMS file
The lines are arranged as follows:
x y z ipot Atom Symbol Distance Number
with distance being the shell radius and ipot an integer identifying
the potential used.
Args:
filename: File name containing atomic coord data.
Returns:
Atoms string.
"""
with zopen(filename, "r") as fobject:
f = fobject.readlines()
coords = 0
atoms_str = []
for line in f:
if coords == 0:
find_atoms = line.find("ATOMS")
if find_atoms >= 0:
coords = 1
if coords == 1:
atoms_str.append(line.replace("\r", ""))
return ''.join(atoms_str)
开发者ID:zacharygibbs,项目名称:pymatgen,代码行数:30,代码来源:feffio.py
示例4: dumpfn
def dumpfn(obj, fn, *args, **kwargs):
"""
Dump to a json/yaml directly by filename instead of a File-like object.
For YAML, PyYAML must be installed. The file type is automatically
detected. YAML is assumed if the filename contains "yaml" (lower or upper
case). Otherwise, json is always assumed. Furthermore, if pyyaml is
compiled with the LibYAML library, the CDumper is automatically chosen
for ~10x faster parsing.
Args:
obj (object): Object to dump.
fn (str): filename.
\*args: Any of the args supported by json/yaml.dump.
\*\*kwargs: Any of the kwargs supported by json/yaml.dump.
Returns:
(object) Result of json.load.
"""
with zopen(fn, "wt") as fp:
if "yaml" in fn.lower():
if yaml is None:
raise RuntimeError("Loading of YAML files is not "
"possible as PyYAML is not installed.")
if "Dumper" not in kwargs:
kwargs["Dumper"] = Dumper
yaml.dump(obj, fp, *args, **kwargs)
else:
fp.write("%s" % json.dumps(obj, *args, **kwargs))
开发者ID:gmrigna,项目名称:monty,代码行数:28,代码来源:serialization.py
示例5: parse_lammps_dumps
def parse_lammps_dumps(file_pattern):
"""
Generator that parses dump file(s).
Args:
file_pattern (str): Filename to parse. The timestep wildcard
(e.g., dump.atom.'*') is supported and the files are parsed
in the sequence of timestep.
Yields:
LammpsDump for each available snapshot.
"""
files = glob.glob(file_pattern)
if len(files) > 1:
pattern = r"%s" % file_pattern.replace("*", "([0-9]+)")
pattern = pattern.replace("\\", "\\\\")
files = sorted(files,
key=lambda f: int(re.match(pattern, f).group(1)))
for fname in files:
with zopen(fname, "rt") as f:
dump_cache = []
for line in f:
if line.startswith("ITEM: TIMESTEP"):
if len(dump_cache) > 0:
yield LammpsDump.from_string("".join(dump_cache))
dump_cache = [line]
else:
dump_cache.append(line)
yield LammpsDump.from_string("".join(dump_cache))
开发者ID:ExpHP,项目名称:pymatgen,代码行数:31,代码来源:outputs.py
示例6: loadfn
def loadfn(fn, *args, **kwargs):
"""
Loads json/yaml directly from a filename instead of a File-like object.
For YAML, PyYAML must be installed. The file type is automatically
detected. YAML is assumed if the filename contains "yaml" (lower or upper
case). Otherwise, json is always assumed. Furthermore, if pyyaml is
compiled with the LibYAML library, the CLoader is automatically chosen
for ~10x faster parsing.
Args:
fn (str): filename
\*args: Any of the args supported by json/yaml.load.
\*\*kwargs: Any of the kwargs supported by json/yaml.load.
Returns:
(object) Result of json/yaml.load.
"""
with zopen(fn) as fp:
if "yaml" in fn.lower():
if yaml is None:
raise RuntimeError("Loading of YAML files is not "
"possible as PyYAML is not installed.")
if "Loader" not in kwargs:
kwargs["Loader"] = Loader
return yaml.load(fp, *args, **kwargs)
else:
return json.load(fp, *args, **kwargs)
开发者ID:gmrigna,项目名称:monty,代码行数:27,代码来源:serialization.py
示例7: gw_run
def gw_run(self):
"""
Performs FIESTA (gw) run
"""
if self.folder != os.getcwd():
init_folder = os.getcwd()
os.chdir(self.folder)
with zopen(self.log_file, "w") as fout:
subprocess.call(
[
"mpirun",
"-n",
str(self.mpi_procs),
"fiesta",
str(self.grid[0]),
str(self.grid[1]),
str(self.grid[2]),
],
stdout=fout,
)
if self.folder != os.getcwd():
os.chdir(init_folder)
开发者ID:montoyjh,项目名称:pymatgen,代码行数:25,代码来源:fiesta.py
示例8: test_potcar_map
def test_potcar_map(self):
fe_potcar = zopen(os.path.join(test_dir, "POT_GGA_PAW_PBE", "POTCAR.Fe_pv.gz")).read().decode("utf-8")
# specify V instead of Fe - this makes sure the test won't pass if the
# code just grabs the POTCAR from the config file (the config file would
# grab the V POTCAR)
potcar = Potcar(["V"], sym_potcar_map={"V": fe_potcar})
self.assertEqual(potcar.symbols, ["Fe_pv"], "Wrong symbols read in " "for POTCAR")
开发者ID:gpetretto,项目名称:pymatgen,代码行数:7,代码来源:test_inputs.py
示例9: write_mol
def write_mol(mol, filename):
"""
Write a molecule to a file based on file extension. For example, anything
ending in a "xyz" is assumed to be a XYZ file. Supported formats include
xyz, Gaussian input (gjf|g03|g09|com|inp), and pymatgen's JSON serialized
molecules.
Args:
mol (Molecule/IMolecule): Molecule to write
filename (str): A filename to write to.
"""
fname = os.path.basename(filename)
if fnmatch(fname.lower(), "*.xyz*"):
return XYZ(mol).write_file(filename)
elif any([fnmatch(fname.lower(), "*.{}*".format(r))
for r in ["gjf", "g03", "g09", "com", "inp"]]):
return GaussianInput(mol).write_file(filename)
elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"):
with zopen(filename, "wt") as f:
return f.write(str2unicode(json.dumps(mol, cls=MontyEncoder)))
else:
m = re.search("\.(pdb|mol|mdl|sdf|sd|ml2|sy2|mol2|cml|mrv)",
filename.lower())
if m:
return BabelMolAdaptor(mol).write_file(filename, m.group(1))
raise ValueError("Unrecognized file extension!")
开发者ID:ATNDiaye,项目名称:pymatgen,代码行数:27,代码来源:smartio.py
示例10: store_dataframe_as_json
def store_dataframe_as_json(dataframe, filename, compression=None,
orient='split'):
"""Store pandas dataframe as a json file.
Automatically encodes pymatgen objects as dictionaries.
Args:
dataframe (Pandas.Dataframe): A pandas dataframe.
filename (str): Path to json file.
compression (str or None): A compression mode. Valid options are "gz",
"bz2", and None. Defaults to None. If the filename does not end
in with the correct suffix it will be added automatically.
orient (str): Determines the format in which the dictionary data is
stored. This takes the same set of arguments as the `orient` option
in `pandas.DataFrame.to_dict()` function. 'split' is recommended
as it is relatively space efficient and preserves the dtype
of the index.
"""
if compression not in ["gz", "bz2", None]:
raise ValueError("Supported compression formats are 'gz' and 'bz2'.")
if compression and not filename.lower().endswith(".{}".format(compression)):
filename = "{}.{}".format(filename, compression)
write_type = "wb" if compression else "w"
with zopen(filename, write_type) as f:
data = json.dumps(dataframe.to_dict(orient=orient), cls=MontyEncoder)
if compression:
data = data.encode()
f.write(data)
开发者ID:RamyaGuru,项目名称:matminer,代码行数:31,代码来源:io.py
示例11: read
def read(self):
"""Read all snapshots from each dump file."""
for dumpfile in self.dumpfiles:
with zopen(dumpfile) as f:
snapshot = self.read_snapshot(f)
while snapshot is not None:
self.trajectory.append(snapshot)
print(snapshot.timestep, end=' ')
sys.stdout.flush()
snapshot = self.read_snapshot(f)
print()
self.trajectory.sort(key=attrgetter('timestep'))
self.trajectory.cull()
print("read {:d} snapshots".format(self.Nsnaps))
self.trajectory.time_selection.all()
self.trajectory.t0_snapshot = self.trajectory[0]
if self.dumpattrs:
print('Dumped Atom attributes: {}'.format(self.dumpattrs2str()))
else:
print('No dump column assignments')
if 'x' not in self.dumpattrs or 'y' not in self.dumpattrs or \
'z' not in self.dumpattrs:
print('dump scaling status unknown')
elif self.Nsnaps > 0:
if self.scale_original:
self.unscale()
elif self.scale_original is None:
print('dump scaling status unknown')
else:
print('dump is already unscaled')
开发者ID:chr7stos,项目名称:scikit-nano,代码行数:35,代码来源:_lammps_dump_format.py
示例12: read_mol
def read_mol(filename):
"""
Reads a molecule based on file extension. For example, anything ending in
a "xyz" is assumed to be a XYZ file. Supported formats include xyz,
gaussian input (gjf|g03|g09|com|inp), Gaussian output (.out|and
pymatgen's JSON serialized molecules. Using openbabel,
many more extensions are supported but requires openbabel to be installed.
Args:
filename (str): A filename to read from.
Returns:
A Molecule object.
"""
fname = os.path.basename(filename)
if fnmatch(fname.lower(), "*.xyz*"):
return XYZ.from_file(filename).molecule
elif any([fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["gjf", "g03", "g09", "com", "inp"]]):
return GaussianInput.from_file(filename).molecule
elif any([fnmatch(fname.lower(), "*.{}*".format(r)) for r in ["out", "lis", "log"]]):
return GaussianOutput(filename).final_structure
elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"):
with zopen(filename) as f:
s = json.load(f, cls=MontyDecoder)
if type(s) != Molecule:
raise IOError("File does not contain a valid serialized " "molecule")
return s
else:
m = re.search("\.(pdb|mol|mdl|sdf|sd|ml2|sy2|mol2|cml|mrv)", filename.lower())
if m:
return BabelMolAdaptor.from_file(filename, m.group(1)).pymatgen_mol
raise ValueError("Unrecognized file extension!")
开发者ID:georgeyumnam,项目名称:pymatgen,代码行数:33,代码来源:smart.py
示例13: write_structure
def write_structure(structure, filename):
"""
Write a structure to a file based on file extension. For example, anything
ending in a "cif" is assumed to be a Crystallographic Information Format
file. Supported formats include CIF, POSCAR, CSSR and pymatgen's JSON
serialized structures.
Args:
structure (Structure/IStructure): Structure to write
filename (str): A filename to write to.
"""
fname = os.path.basename(filename)
if fnmatch(fname, "*.cif*"):
writer = CifWriter(structure)
elif fnmatch(fname, "POSCAR*") or fnmatch(fname, "CONTCAR*"):
writer = Poscar(structure)
elif fnmatch(fname.lower(), "*.cssr*"):
writer = Cssr(structure)
elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"):
with zopen(filename, "wt") as f:
f.write(str2unicode(json.dumps(structure, cls=MontyEncoder)))
return
else:
raise ValueError("Unrecognized file extension!")
writer.write_file(filename)
开发者ID:ATNDiaye,项目名称:pymatgen,代码行数:26,代码来源:smartio.py
示例14: run
def run(self):
"""
Performs actual nwchem run.
"""
with zopen(self.output_file, 'w') as fout:
return subprocess.Popen(self.nwchem_cmd + [self.input_file],
stdout=fout)
开发者ID:czhengsci,项目名称:custodian,代码行数:7,代码来源:jobs.py
示例15: read_structure
def read_structure(filename):
"""
Reads a structure based on file extension. For example, anything ending in
a "cif" is assumed to be a Crystallographic Information Format file.
Supported formats include CIF, POSCAR/CONTCAR, CHGCAR, LOCPOT,
vasprun.xml, CSSR and pymatgen's JSON serialized structures.
Args:
filename (str): A filename to read from.
Returns:
A Structure object.
"""
fname = os.path.basename(filename)
if fnmatch(fname.lower(), "*.cif*"):
parser = CifParser(filename)
return parser.get_structures(True)[0]
elif fnmatch(fname, "POSCAR*") or fnmatch(fname, "CONTCAR*"):
return Poscar.from_file(filename, False).structure
elif fnmatch(fname, "CHGCAR*") or fnmatch(fname, "LOCPOT*"):
return Chgcar.from_file(filename).structure
elif fnmatch(fname, "vasprun*.xml*"):
return Vasprun(filename).final_structure
elif fnmatch(fname.lower(), "*.cssr*"):
cssr = Cssr.from_file(filename)
return cssr.structure
elif fnmatch(fname, "*.json*") or fnmatch(fname, "*.mson*"):
with zopen(filename) as f:
s = json.load(f, cls=PMGJSONDecoder)
if type(s) != Structure:
raise IOError("File does not contain a valid serialized "
"structure")
return s
raise ValueError("Unrecognized file extension!")
开发者ID:brendaneng1,项目名称:pymatgen,代码行数:34,代码来源:smartio.py
示例16: __init__
def __init__(self, filename, parse_box=True, dtype=float):
"""
Args:
filename (str): Filename to parse. The timestep wildcard
('*') is supported and the files are parsed in the
sequence of timestep.
parse_box (bool): Whether parse box info for each step.
Default to True.
dtype: np.dtype for atoms data array.
"""
self.filename = filename
self.parse_box = parse_box
self.dtype = dtype
fnames = glob.glob(self.filename)
if len(fnames) > 1:
pattern = r"%s" % filename.replace("*", "([0-9]+)")
pattern = pattern.replace("\\", "\\\\")
fnames = sorted(fnames,
key=lambda f: int(re.match(pattern, f).group(1)))
steps = []
for fname in fnames:
with zopen(fname, "rt") as f:
run = f.read()
dumps = run.split("ITEM: TIMESTEP")[1:]
steps.extend([self._parse_timestep(d) for d in dumps])
self.steps = steps
self.timesteps = [s["timestep"] for s in self.steps]
开发者ID:mbkumar,项目名称:pymatgen,代码行数:30,代码来源:output.py
示例17: __init__
def __init__(self, are_coops=False, filename=None):
self.are_coops = are_coops
if filename is None:
filename = "COOPCAR.lobster" if are_coops \
else "COHPCAR.lobster"
with zopen(filename, "rt") as f:
contents = f.read().split("\n")
# The parameters line is the second line in a COHPCAR file. It
# contains all parameters that are needed to map the file.
parameters = contents[1].split()
# Subtract 1 to skip the average
num_bonds = int(parameters[0]) - 1
self.efermi = float(parameters[-1])
if int(parameters[1]) == 2:
spins = [Spin.up, Spin.down]
self.is_spin_polarized = True
else:
spins = [Spin.up]
self.is_spin_polarized = False
# The COHP data start in row num_bonds + 3
data = np.array([np.array(row.split(), dtype=float)
for row in contents[num_bonds+3:]]).transpose()
self.energies = data[0]
cohp_data = {"average": {"COHP": {spin: data[1+2*s*(num_bonds+1)]
for s, spin in enumerate(spins)},
"ICOHP": {spin: data[2+2*s*(num_bonds+1)]
for s, spin in enumerate(spins)}}}
orb_cohp = {}
for bond in range(num_bonds):
bond_data = self._get_bond_data(contents[3+bond])
label = bond_data["label"]
orbs = bond_data["orbitals"]
cohp = {spin: data[2*(bond+s*(num_bonds+1))+3]
for s, spin in enumerate(spins)}
icohp = {spin: data[2*(bond+s*(num_bonds+1))+4]
for s, spin in enumerate(spins)}
if orbs is None:
cohp_data[label] = {"COHP": cohp, "ICOHP": icohp,
"length": bond_data["length"],
"sites": bond_data["sites"]}
elif label in orb_cohp:
orb_cohp[label].update({bond_data["orb_label"]:
{"COHP": cohp, "ICOHP": icohp,
"orbitals": orbs}})
else:
if label not in cohp_data:
cohp_data[label] = {"COHP": None, "ICOHP": None,
"length": bond_data["length"],
"sites": bond_data["sites"]}
orb_cohp[label] = {bond_data["orb_label"]: {"COHP": cohp,
"ICOHP": icohp,
"orbitals": orbs}}
self.orb_res_cohp = orb_cohp if orb_cohp else None
self.cohp_data = cohp_data
开发者ID:czhengsci,项目名称:pymatgen,代码行数:59,代码来源:lobster.py
示例18: from_multi_jobs_file
def from_multi_jobs_file(cls, filename):
# returns a list of QCInput objects
with zopen(filename, 'rt') as f:
# the delimiter between QChem jobs is @@@
multi_job_strings = f.read().split("@@@")
# list of individual QChem jobs
input_list = [cls.from_string(i) for i in multi_job_strings]
return input_list
开发者ID:mbkumar,项目名称:pymatgen,代码行数:8,代码来源:inputs.py
示例19: __init__
def __init__(self, filename):
self.filename = filename
with zopen(filename) as f:
log_bse = f.read()
# self.job_info = self._parse_preamble(preamble)
self.exiton = self._parse_job(log_bse)
开发者ID:albalu,项目名称:pymatgen,代码行数:8,代码来源:fiesta.py
示例20: write_file
def write_file(self, filename,cart_coords=False):
"""
Write the input string into a file
Option: see __str__ method
"""
with zopen(filename, "w") as f:
f.write(self.to_string(cart_coords))
开发者ID:boykov,项目名称:pymatgen,代码行数:8,代码来源:gaussian.py
注:本文中的monty.io.zopen函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论