Skip to content

Commit 8c673fe

Browse files
committed
ENH: handle CIFs with unknown atom_site_aniso
- add `P_cif.anisotropy` instance attribute to remember sites with resolved anisotropy. - remove unnecessary helper function `_hasAtomSiteADPType`.
1 parent 41329be commit 8c673fe

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/diffpy/structure/parsers/p_cif.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class P_cif(StructureParser):
5151
unit in the CIF file
5252
labelindex -- dictionary mapping unique atom label to index of atom
5353
in self.asymmetric_unit
54+
anisotropy -- dictionary mapping unique atom label to displacement
55+
anisotropy resolved at that site
5456
cif_sgname -- space group name obtained by looking up the value of
5557
_space_group_name_Hall, _symmetry_space_group_name_Hall,
5658
_space_group_name_H-M_alt, _symmetry_space_group_name_H-M
@@ -241,6 +243,7 @@ def __init__(self, eps=None):
241243
self.eau = None
242244
self.asymmetric_unit = None
243245
self.labelindex = {}
246+
self.anisotropy = {}
244247
self.cif_sgname = None
245248
pass
246249

@@ -338,6 +341,7 @@ def _parseCifBlock(self, blockname):
338341
# here block contains structure, initialize output data
339342
self.stru = Structure()
340343
self.labelindex.clear()
344+
self.anisotropy.clear()
341345
# execute specialized block parsers
342346
self._parse_lattice(block)
343347
self._parse_atom_site_label(block)
@@ -385,6 +389,8 @@ def _parse_atom_site_label(self, block):
385389
"""
386390
# process _atom_site_label
387391
atom_site_loop = block.GetLoop('_atom_site_label')
392+
does_adp_type = ('_atom_site_adp_type' in atom_site_loop or
393+
'_atom_site_thermal_displace_type' in atom_site_loop)
388394
# get a list of setters for atom_site values
389395
prop_setters = P_cif._get_atom_setters(atom_site_loop)
390396
# index of the _atom_site_label item for the labelindex dictionary
@@ -401,6 +407,8 @@ def _parse_atom_site_label(self, block):
401407
a = self.stru.getLastAtom()
402408
for fset, val in zip(prop_setters, values):
403409
fset(a, val)
410+
if does_adp_type:
411+
self.anisotropy[curlabel] = a.anisotropy
404412
return
405413

406414

@@ -414,8 +422,6 @@ def _parse_atom_site_aniso_label(self, block):
414422
No return value.
415423
"""
416424
if '_atom_site_aniso_label' not in block: return
417-
# was anisotropy processed in the _atom_site_label loop?
418-
isotropy_done = _hasAtomSiteADPType(block)
419425
# something to do here:
420426
adp_loop = block.GetLoop('_atom_site_aniso_label')
421427
# index of the _atom_site_label column
@@ -424,10 +430,14 @@ def _parse_atom_site_aniso_label(self, block):
424430
prop_setters = P_cif._get_atom_setters(adp_loop)
425431
sitedatalist = zip(*adp_loop.values())
426432
for values in sitedatalist:
427-
idx = self.labelindex[values[ilb]]
433+
lb = values[ilb]
434+
if lb == '?':
435+
break
436+
idx = self.labelindex[lb]
428437
a = self.stru[idx]
429-
if not isotropy_done:
438+
if not lb in self.anisotropy:
430439
a.anisotropy = True
440+
self.anisotropy[lb] = True
431441
for fset, val in zip(prop_setters, values):
432442
fset(a, val)
433443
return
@@ -513,12 +523,11 @@ def _expandAsymmetricUnit(self, block):
513523
self.eau = ExpandAsymmetricUnit(self.spacegroup, corepos, coreUijs,
514524
eps=self.eps)
515525
# setup anisotropy according to symmetry requirements
516-
# was isotropy flag already processed
517-
isotropy_done = (_hasAtomSiteADPType(block) or
518-
'_atom_site_aniso_label' in block)
519-
if not isotropy_done:
520-
for ca, uisotropy in zip(self.stru, self.eau.Uisotropy):
526+
# unless it was already explicitly set
527+
for ca, uisotropy in zip(self.stru, self.eau.Uisotropy):
528+
if not ca.label in self.anisotropy:
521529
ca.anisotropy = not uisotropy
530+
self.anisotropy[ca.label] = ca.anisotropy
522531
# build a nested list of new atoms:
523532
newatoms = []
524533
for i, ca in enumerate(self.stru):
@@ -736,12 +745,3 @@ def _suppressCifParserOutput():
736745
finally:
737746
yapps3_compiled_rt.print_error = print_error
738747
pass
739-
740-
741-
def _hasAtomSiteADPType(block):
742-
"""Return True if the CIF specifies _atom_site_adp_type.
743-
"""
744-
atom_site_loop = block.GetLoop('_atom_site_label')
745-
rv = ('_atom_site_adp_type' in atom_site_loop or
746-
'_atom_site_thermal_displace_type' in atom_site_loop)
747-
return rv

src/diffpy/structure/tests/testp_cif.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ def test_adp_aniso_label(self):
365365
return
366366

367367

368-
@unittest.expectedFailure
369368
def test_unknown_aniso(self):
370369
"test CIF file with unknown values in the aniso block."
371370
with open(self.teiciffile) as fp:

0 commit comments

Comments
 (0)