本文整理汇总了Python中diffpy.Structure.Structure.readStr方法的典型用法代码示例。如果您正苦于以下问题:Python Structure.readStr方法的具体用法?Python Structure.readStr怎么用?Python Structure.readStr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类diffpy.Structure.Structure
的用法示例。
在下文中一共展示了Structure.readStr方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: crystalToDiffpyStructure
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def crystalToDiffpyStructure(crystal):
"""Create a diffpy.Structure.Structure from a crystal.
This requires diffpy.Structure to be installed. This uses file IO transfer
data, so there is some inherent precision loss.
Note that the resulting structure will be in P1 symmetry.
"""
# Write the crystal to string and load it into a diffpy Structure
from cStringIO import StringIO
buf = StringIO()
crystal.CIFOutput(buf)
from diffpy.Structure import Structure
stru = Structure()
s = buf.getvalue()
stru.readStr(s)
buf.close()
return stru
示例2: test_write_and_read
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_write_and_read(self):
"""high-level check of P_cif.tostring()
"""
# high-level check
stru_check = Structure()
stru_check.read(self.cdsebulkpdffitfile)
s_s = stru_check.writeStr('cif')
stru = Structure()
stru.readStr(s_s, 'cif')
self.assertAlmostEqual(4.2352, stru.lattice.a, self.places)
self.assertAlmostEqual(4.2352, stru.lattice.b, self.places)
self.assertAlmostEqual(6.90603, stru.lattice.c, self.places)
self.assertEqual(4, len(stru))
a0 = stru[0]
self.assertEqual('Cd', a0.element)
self.assertListAlmostEqual([0.3334, 0.6667, 0.0], a0.xyz)
self.assertAlmostEqual(0.01303, a0.U[0,0])
self.assertAlmostEqual(0.01303, a0.U[1,1])
self.assertAlmostEqual(0.01402, a0.U[2,2])
a3 = stru[3]
self.assertEqual('Se', a3.element)
self.assertListAlmostEqual([0.6666, 0.333300, 0.87667], a3.xyz)
self.assertAlmostEqual(0.015673, a3.U[0,0])
self.assertAlmostEqual(0.015673, a3.U[1,1])
self.assertAlmostEqual(0.046164, a3.U[2,2])
return
示例3: test_rwStr_pdb_CdSe
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_rwStr_pdb_CdSe(self):
"""check conversion to PDB file format"""
stru = self.stru
stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
s = stru.writeStr(self.format)
# all lines should be 80 characters long
linelens = [ len(l) for l in s.split('\n') if l != "" ]
self.assertEqual(linelens, len(linelens)*[80])
# now clean and re-read structure
stru = Structure()
stru.readStr(s, self.format)
s_els = [a.element for a in stru]
f_els = ['Cd', 'Cd', 'Se', 'Se']
self.assertEqual(s_els, f_els)
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
f_lat = [ 4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0 ]
self.assertListAlmostEqual(s_lat, f_lat)
a0 = stru[0]
s_Uii = [ a0.U[i,i] for i in range(3) ]
f_Uii = [ 0.01303035, 0.01303035, 0.01401959 ]
self.assertListAlmostEqual(s_Uii, f_Uii)
s_sigUii = [ a0.sigU[i,i] for i in range(3) ]
f_sigUii = [ 0.00011127, 0.00011127, 0.00019575 ]
self.assertListAlmostEqual(s_sigUii, f_sigUii)
s_title = stru.title
f_title = "Cell structure file of CdSe #186"
self.assertEqual(s_title, f_title)
示例4: test_huge_occupancy
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_huge_occupancy(self):
"""check structure with huge occupancy can be read.
"""
self.stru.read(datafile('Ni.stru'), self.format)
self.stru[0].occupancy = 16e16
s_s = self.stru.writeStr(self.format)
stru1 = Structure()
stru1.readStr(s_s, self.format)
self.assertEqual(16e16, stru1[0].occupancy)
return
示例5: test_rwStr_xcfg_CdSe
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_rwStr_xcfg_CdSe(self):
"""check conversion to XCFG file format"""
stru = self.stru
stru.read(datafile('CdSe_bulk.stru'), 'pdffit')
s = stru.writeStr(self.format)
stru = Structure()
stru.readStr(s, self.format)
s_els = [a.element for a in stru]
f_els = ['Cd', 'Cd', 'Se', 'Se']
self.assertEqual(s_els, f_els)
s_lat = [ stru.lattice.a, stru.lattice.b, stru.lattice.c,
stru.lattice.alpha, stru.lattice.beta, stru.lattice.gamma ]
f_lat = [ 4.235204, 4.235204, 6.906027, 90.0, 90.0, 120.0 ]
self.assertListAlmostEqual(s_lat, f_lat)
a0 = stru[0]
s_Uii = [ a0.U[i,i] for i in range(3) ]
f_Uii = [ 0.01303035, 0.01303035, 0.01401959 ]
self.assertListAlmostEqual(s_Uii, f_Uii)
示例6: test_stepcut_parsing
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_stepcut_parsing(self):
"""check parsing of stepcut record from a file.
"""
stru = self.stru
stru.read(datafile("Ni.stru"), self.format)
self.assertEqual(0, stru.pdffit["stepcut"])
snoshape = stru.writeStr(format=self.format)
self.failUnless(not re.search("(?m)^shape", snoshape))
# produce a string with non-zero stepcut
stru.pdffit["stepcut"] = 13
s13 = stru.writeStr(format=self.format)
self.failUnless(re.search("(?m)^shape +stepcut, ", s13))
stru13 = Structure()
stru13.readStr(s13)
self.assertEqual(13, stru13.pdffit["stepcut"])
ni_lines = open(datafile("Ni.stru")).readlines()
ni_lines.insert(3, "shape invalid, 7\n")
sbad = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.readStr, sbad, format=self.format)
return
示例7: test_spdiameter_parsing
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def test_spdiameter_parsing(self):
"""check parsing of spdiameter record from a file.
"""
stru = self.stru
stru.read(datafile('Ni.stru'), self.format)
self.assertEqual(0, stru.pdffit['spdiameter'])
snoshape = stru.writeStr(format=self.format)
self.failUnless(not re.search('(?m)^shape', snoshape))
# produce a string with non-zero spdiameter
stru.pdffit['spdiameter'] = 13
s13 = stru.writeStr(format=self.format)
self.failUnless(re.search('(?m)^shape +sphere, ', s13))
stru13 = Structure()
stru13.readStr(s13)
self.assertEqual(13, stru13.pdffit['spdiameter'])
ni_lines = open(datafile('Ni.stru')).readlines()
ni_lines.insert(3, 'shape invalid, 7\n')
sbad = ''.join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.readStr,
sbad, format=self.format)
return
示例8: inputFormats
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
if infmt not in inputFormats():
print >> sys.stderr, "'%s' is not valid input format" % infmt
sys.exit(2)
if outfmt not in outputFormats():
print >> sys.stderr, "'%s' is not valid output format" % outfmt
sys.exit(2)
except ValueError:
print >> sys.stderr, \
"invalid format specification '%s' does not contain .." % args[0]
sys.exit(2)
# ready to do some real work
try:
strufile = args[1]
stru = Structure()
if args[1] == "-":
stru.readStr(sys.stdin.read(), infmt)
else:
stru.read(strufile, infmt)
sys.stdout.write( stru.writeStr(outfmt) )
except IndexError:
print >> sys.stderr, "strufile not specified"
sys.exit(2)
except IOError, (errno, errmsg):
print >> sys.stderr, "%s: %s" % (strufile, errmsg)
sys.exit(1)
except StructureFormatError, errmsg:
print >> sys.stderr, "%s: %s" % (strufile, errmsg)
sys.exit(1)
if __name__ == "__main__":
main()
示例9: Structure
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
# define nickel structure data
nickel_discus_data = '''
title Ni
spcgr P1
cell 3.523870, 3.523870, 3.523870, 90.000000, 90.000000, 90.000000
ncell 1, 1, 1, 4
atoms
NI 0.00000000 0.00000000 0.00000000 0.1000
NI 0.00000000 0.50000000 0.50000000 0.1000
NI 0.50000000 0.00000000 0.50000000 0.1000
NI 0.50000000 0.50000000 0.00000000 0.1000
'''
nickel = Structure()
nickel.readStr(nickel_discus_data, format='discus')
def Gpdffit2(qmax):
"""Calculate reference nickel PDF using diffpy.pdffit2
qmax -- vawevector cutoff value in 1/A
Return numpy array of (r, g).
"""
# calculate reference data using pdffit2
pf2 = diffpy.pdffit2.PdfFit()
pf2.alloc('X', qmax, 0.0, rmin, rmax, int((rmax - rmin) / rstep + 1))
pf2.add_structure(nickel)
pf2.calc()
rg = numpy.array((pf2.getR(), pf2.getpdf_fit()))
示例10: makeLaMnO3_P1
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
def makeLaMnO3_P1():
from diffpy.Structure import Structure
stru = Structure()
stru.readStr(lamno3stru)
return stru
示例11: TestP_pdffit
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
#.........这里部分代码省略.........
return
def test_read_pdffit_bad(self):
"""check exceptions when reading invalid pdffit file"""
stru = self.stru
self.assertRaises(StructureFormatError, stru.read,
datafile('Ni-bad.stru'), self.format)
self.assertRaises(StructureFormatError, stru.read,
datafile('bucky.xyz'), self.format)
return
def test_writeStr_pdffit(self):
"""check writing of normal xyz file"""
stru = self.stru
stru.read(datafile('Ni.stru'), self.format)
f_s = open(datafile('Ni.stru')).read()
f_s = re.sub('[ \t]+', ' ', f_s)
f_s = re.sub('[ \t]+\n', '\n', f_s)
s_s = stru.writeStr(self.format)
s_s = re.sub('[ \t]+', ' ', s_s)
self.assertEqual(f_s, s_s)
return
def test_huge_occupancy(self):
"""check structure with huge occupancy can be read.
"""
self.stru.read(datafile('Ni.stru'), self.format)
self.stru[0].occupancy = 16e16
s_s = self.stru.writeStr(self.format)
stru1 = Structure()
stru1.readStr(s_s, self.format)
self.assertEqual(16e16, stru1[0].occupancy)
return
def test_ignored_lines(self):
"""check skipping of ignored lines in the header
"""
r1 = 'ignored record 1'
r2 = 'ignored record 2'
ni_lines = open(datafile('Ni.stru')).readlines()
ni_lines.insert(2, r1 + '\n')
ni_lines.insert(4, r2 + '\n')
s_s1 = "".join(ni_lines)
p = self.stru.readStr(s_s1, self.format)
self.assertEqual([r1, r2], p.ignored_lines)
ni_lines.insert(-3, r1 + '\n')
s_s2 = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.readStr,
s_s2, self.format)
return
def test_spdiameter_parsing(self):
"""check parsing of spdiameter record from a file.
"""
stru = self.stru
stru.read(datafile('Ni.stru'), self.format)
self.assertEqual(0, stru.pdffit['spdiameter'])
snoshape = stru.writeStr(format=self.format)
self.failUnless(not re.search('(?m)^shape', snoshape))
# produce a string with non-zero spdiameter
stru.pdffit['spdiameter'] = 13
示例12: TestP_discus
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import readStr [as 别名]
class TestP_discus(unittest.TestCase):
"""test Parser for PDFFit file format"""
assertListAlmostEqual = assertListAlmostEqual
def setUp(self):
self.stru = Structure()
self.format = "discus"
self.places = 8
def test_read_discus_Ni(self):
"""check reading of Ni structure in discus format"""
stru = self.stru
stru.read(datafile('Ni-discus.stru'), self.format)
f_title = 'structure Ni FCC'
self.assertEqual(f_title, stru.title)
self.assertEqual('Fm-3m', stru.pdffit['spcgr'])
# cell record
abcABG = (3.52, 3.52, 3.52, 90.0, 90.0, 90.0)
self.assertEqual(abcABG, stru.lattice.abcABG())
# ncell
self.assertEqual([1, 1, 1, 4], stru.pdffit['ncell'])
self.assertEqual(4, len(stru))
# first atom
a0 = stru[0]
self.assertEqual((0.0, 0.0, 0.0), tuple(a0.xyz))
self.failUnless(not a0.anisotropy)
Biso0 = 0.1
self.assertAlmostEqual(Biso0, a0.Bisoequiv, self.places)
return
def test_except_other_formats(self):
"""check exceptions when reading files in other formats"""
badfiles = [
'LiCl-bad.cif',
'PbTe.cif',
'arginine.pdb',
'ZnSb_RT_Q28X_VM_20_fxiso.rstr',
'Ni-bad.stru',
'Ni.stru',
'BubbleRaftShort.xcfg',
'bucky-bad1.xyz',
'bucky-bad2.xyz',
'bucky-plain-bad.xyz',
'bucky-plain.xyz',
'bucky-raw.xyz',
'bucky.xyz',
'hexagon-raw-bad.xyz',
'hexagon-raw.xyz',
]
for ft in badfiles:
ff = datafile(ft)
self.assertRaises(StructureFormatError,
self.stru.read, ff, format=self.format)
return
def test_ignored_lines(self):
"""check skipping of ignored lines in the header
"""
r1 = 'ignored record 1\n'
r2 = 'ignored record 2\n'
ni_lines = open(datafile('Ni-discus.stru')).readlines()
ni_lines.insert(2, r1)
ni_lines.insert(4, r2)
s_s1 = "".join(ni_lines)
p = self.stru.readStr(s_s1, self.format)
self.assertEqual([r1.rstrip(), r2.rstrip()], p.ignored_lines)
ni_lines.append(r1)
s_s2 = "".join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.readStr,
s_s2, self.format)
return
def test_spdiameter_parsing(self):
"""check parsing of spdiameter record from a file.
"""
stru = self.stru
stru.read(datafile('Ni-discus.stru'), self.format)
self.assertEqual(0, stru.pdffit['spdiameter'])
snoshape = stru.writeStr(format=self.format)
self.failUnless(not re.search('(?m)^shape', snoshape))
# produce a string with non-zero spdiameter
stru.pdffit['spdiameter'] = 13
s13 = stru.writeStr(format=self.format)
self.failUnless(re.search('(?m)^shape +sphere, ', s13))
stru13 = Structure()
stru13.readStr(s13)
self.assertEqual(13, stru13.pdffit['spdiameter'])
ni_lines = open(datafile('Ni.stru')).readlines()
ni_lines.insert(3, 'shape invalid, 7\n')
sbad = ''.join(ni_lines)
self.assertRaises(StructureFormatError, self.stru.readStr,
sbad, format=self.format)
return
#.........这里部分代码省略.........