Skip to content

Commit d003fcd

Browse files
committed
Merge branch 'fix-unknown-occupancy'
* make unknonwn CIF occupancy default to 1. * use unix line ends in all test CIF files. Resolve #24.
2 parents 418e428 + d130955 commit d003fcd

File tree

5 files changed

+105
-44
lines changed

5 files changed

+105
-44
lines changed

src/diffpy/structure/parsers/p_cif.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _tr_atom_site_adp_type(a, value):
156156
_tr_atom_site_thermal_displace_type = _tr_atom_site_adp_type
157157

158158
def _tr_atom_site_occupancy(a, value):
159-
a.occupancy = leading_float(value)
159+
a.occupancy = leading_float(value, 1.0)
160160
_tr_atom_site_occupancy = staticmethod(_tr_atom_site_occupancy)
161161

162162
def _tr_atom_site_aniso_U_11(a, value):
@@ -622,19 +622,36 @@ def toLines(self, stru):
622622
# constant regular expression for leading_float()
623623
rx_float = re.compile(r'[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?')
624624

625-
def leading_float(s):
626-
"""Obtain first float from a string and ignore any trailing characters.
625+
def leading_float(s, d=0.0):
626+
"""Extract the first float from a string and ignore trailing characters.
627+
627628
Useful for extracting values from "value(std)" syntax.
628629
629-
Return float.
630+
Parameters
631+
----------
632+
s : str
633+
The string to be scanned for floating point value.
634+
d : float, optional
635+
The default value when `s` is "." or "?", which in CIF
636+
format stands for inapplicable and unknown, respectively.
637+
638+
Returns
639+
-------
640+
float
641+
The extracted floating point value.
642+
643+
Raises
644+
------
645+
ValueError
646+
When string does not start with a float.
630647
"""
631648
sbare = s.strip()
632649
mx = rx_float.match(sbare)
633650
if mx:
634651
rv = float(mx.group())
635652
elif sbare == '.' or sbare == '?':
636653
# CIF files may contain "." or "?" for unknown values
637-
rv = 0.0
654+
rv = d
638655
else:
639656
rv = float(sbare)
640657
return rv

src/diffpy/structure/tests/testdata/LiCl-bad.cif

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ _cell_length_a 5.12952
1111
_cell_length_b 5.12952
1212
_cell_length_c 5.12952
1313
_cell_volume 134.968
14-
_cgraph_comments 'Praezisionsbestimmung von Gitterkonstanten hygroskopischer
15-
Verbindungen (Li Cl. Na Br).
14+
_cgraph_comments 'Praezisionsbestimmung von Gitterkonstanten hygroskopischer
15+
Verbindungen (Li Cl. Na Br).
1616
'
1717
_cgraph_title 'Lithium Chloride'
1818
_chemical_formula_sum 'Li Cl'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
data_TeI
2+
_audit_creation_date 2011-02-15
3+
4+
_symmetry_space_group_name_H-M 'C 1 2/m 1'
5+
6+
_cell_length_a 15.383
7+
_cell_length_b 4.182
8+
_cell_length_c 11.999
9+
_cell_angle_alpha 90
10+
_cell_angle_beta 128.09
11+
_cell_angle_gamma 90
12+
13+
loop_
14+
_atom_site_label
15+
_atom_site_type_symbol
16+
_atom_site_fract_x
17+
_atom_site_fract_y
18+
_atom_site_fract_z
19+
_atom_site_U_iso_or_equiv
20+
_atom_site_adp_type
21+
_atom_site_occupancy
22+
Te1 Te1+ 0.306590 0.000000 0.185920 0.019227 Uani ?
23+
Te2 Te1+ 0.429170 0.500000 0.168170 0.019784 Uani ?
24+
I1 I1- 0.153740 0.500000 0.182510 0.024813 Uani ?
25+
I2 I1- 0.610730 0.500000 0.448390 0.026878 Uani ?
26+
loop_
27+
_atom_site_aniso_label
28+
_atom_site_aniso_U_11
29+
_atom_site_aniso_U_22
30+
_atom_site_aniso_U_33
31+
_atom_site_aniso_U_12
32+
_atom_site_aniso_U_13
33+
_atom_site_aniso_U_23
34+
Te1 0.021100 0.019500 0.016000 0.000000 0.010900 0.000000
35+
Te2 0.022300 0.018000 0.025400 0.000000 0.017900 0.000000
36+
I1 0.025000 0.023400 0.034500 0.000000 0.022600 0.000000
37+
I2 0.023400 0.029500 0.025300 0.000000 0.013800 0.000000
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
data_TeI
2-
_audit_creation_date 2011-02-15
3-
4-
_symmetry_space_group_name_H-M 'C 1 2/m 1'
5-
6-
_cell_length_a 15.383
7-
_cell_length_b 4.182
8-
_cell_length_c 11.999
9-
_cell_angle_alpha 90
10-
_cell_angle_beta 128.09
11-
_cell_angle_gamma 90
12-
13-
loop_
14-
_atom_site_label
15-
_atom_site_type_symbol
16-
_atom_site_fract_x
17-
_atom_site_fract_y
18-
_atom_site_fract_z
19-
_atom_site_U_iso_or_equiv
20-
_atom_site_adp_type
21-
_atom_site_occupancy
22-
Te1 Te1+ 0.306590 0.000000 0.185920 0.019227 Uani 1.0000
23-
Te2 Te1+ 0.429170 0.500000 0.168170 0.019784 Uani 1.0000
24-
I1 I1- 0.153740 0.500000 0.182510 0.024813 Uani 1.0000
25-
I2 I1- 0.610730 0.500000 0.448390 0.026878 Uani 1.0000
26-
loop_
27-
_atom_site_aniso_label
28-
_atom_site_aniso_U_11
29-
_atom_site_aniso_U_22
30-
_atom_site_aniso_U_33
31-
_atom_site_aniso_U_12
32-
_atom_site_aniso_U_13
33-
_atom_site_aniso_U_23
34-
Te1 0.021100 0.019500 0.016000 0.000000 0.010900 0.000000
35-
Te2 0.022300 0.018000 0.025400 0.000000 0.017900 0.000000
36-
I1 0.025000 0.023400 0.034500 0.000000 0.022600 0.000000
37-
I2 0.023400 0.029500 0.025300 0.000000 0.013800 0.000000
1+
data_TeI
2+
_audit_creation_date 2011-02-15
3+
4+
_symmetry_space_group_name_H-M 'C 1 2/m 1'
5+
6+
_cell_length_a 15.383
7+
_cell_length_b 4.182
8+
_cell_length_c 11.999
9+
_cell_angle_alpha 90
10+
_cell_angle_beta 128.09
11+
_cell_angle_gamma 90
12+
13+
loop_
14+
_atom_site_label
15+
_atom_site_type_symbol
16+
_atom_site_fract_x
17+
_atom_site_fract_y
18+
_atom_site_fract_z
19+
_atom_site_U_iso_or_equiv
20+
_atom_site_adp_type
21+
_atom_site_occupancy
22+
Te1 Te1+ 0.306590 0.000000 0.185920 0.019227 Uani 1.0000
23+
Te2 Te1+ 0.429170 0.500000 0.168170 0.019784 Uani 1.0000
24+
I1 I1- 0.153740 0.500000 0.182510 0.024813 Uani 1.0000
25+
I2 I1- 0.610730 0.500000 0.448390 0.026878 Uani 1.0000
26+
loop_
27+
_atom_site_aniso_label
28+
_atom_site_aniso_U_11
29+
_atom_site_aniso_U_22
30+
_atom_site_aniso_U_33
31+
_atom_site_aniso_U_12
32+
_atom_site_aniso_U_13
33+
_atom_site_aniso_U_23
34+
Te1 0.021100 0.019500 0.016000 0.000000 0.010900 0.000000
35+
Te2 0.022300 0.018000 0.025400 0.000000 0.017900 0.000000
36+
I1 0.025000 0.023400 0.034500 0.000000 0.022600 0.000000
37+
I2 0.023400 0.029500 0.025300 0.000000 0.013800 0.000000

src/diffpy/structure/tests/testp_cif.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ def test_eps(self):
249249
return
250250

251251

252+
def test_unknown_occupancy(self):
253+
"test CIF file with unknown occupancy data"
254+
stru = self.ptest.parseFile(datafile('TeI-unkocc.cif'))
255+
self.assertTrue(numpy.array_equal(16 * [1], stru.occupancy))
256+
return
257+
258+
252259
def test_nosites_cif(self):
253260
"""Test reading of CIF file with no valid sites.
254261
"""

0 commit comments

Comments
 (0)