-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathutils.py
More file actions
146 lines (113 loc) · 3.77 KB
/
utils.py
File metadata and controls
146 lines (113 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Small shared functions."""
from collections.abc import Iterable as _Iterable
import numpy
from diffpy.utils._deprecator import build_deprecation_message, deprecated
base = "diffpy.structure"
removal_version = "4.0.0"
atomBareSymbol_deprecation_msg = build_deprecation_message(
base,
"atomBareSymbol",
"atom_bare_symbol",
removal_version,
)
def isiterable(obj):
"""``True`` if argument is iterable."""
rv = isinstance(obj, _Iterable)
return rv
def isfloat(s):
"""``True`` if argument can be converted to float."""
try:
float(s)
return True
except ValueError:
pass
return False
@deprecated(atomBareSymbol_deprecation_msg)
def atomBareSymbol(smbl):
"""This function has been deprecated and will be removed in version
4.0.0.
Please use diffpy.structure.atom_bare_symbol instead.
"""
return atom_bare_symbol(smbl)
def atom_bare_symbol(smbl):
"""Remove atom type string stripped of isotope and ion charge
symbols.
This function removes any blank, isotope numbers (0-9), leading hyphens (-), and ion charge
symbols (1-9)(+-) from the given atom type string, returning only the bare element symbol.
Parameters
----------
smbl : str
Atom type string that may include isotope numbers, ion charges, or hyphens.
Returns
-------
str
The bare element symbol.
Examples
--------
>>> atom_bare_symbol("Cl-")
'Cl'
>>> atom_bare_symbol("Ca2+")
'Ca'
>>> atom_bare_symbol("12-C")
'C'
"""
rv = smbl.strip().lstrip("0123456789-").rstrip("123456789+-")
return rv
# Helpers for the Structure class --------------------------------------------
def _link_atom_attribute(attrname, doc, toarray=numpy.array):
"""Create property wrapper that maps the specified atom attribute.
The returned property object provides convenient access to atom
attributes from the owner `Structure` class.
Parameters
----------
attrname : str
The string name of the `Atom` class attribute to be mapped.
doc : str
The docstring for the property wrapper.
toarray : callable, Optional
Factory function that converts list of attributes to `numpy.ndarray`.
Use `numpy.char.array` for string attributes.
Return a property object.
"""
from itertools import repeat
from operator import setitem
_all = slice(None)
def fget(self):
va = toarray([getattr(a, attrname) for a in self])
return va
def fset(self, value):
n = len(self)
if n == 0:
return
v0 = getattr(self[0], attrname)
# replace scalar values, but change array attributes in place
if numpy.isscalar(v0):
def setvalue(a, v):
return setattr(a, attrname, v)
else:
def setvalue(a, v):
return setitem(getattr(a, attrname), _all, v)
# avoid broadcasting if the new value is a scalar
if numpy.isscalar(value):
genvalues = repeat(value)
else:
genvalues = numpy.broadcast_to(value, (n,) + numpy.shape(v0))
for a, v in zip(self, genvalues):
setvalue(a, v)
return
rv = property(fget, fset, doc=doc)
return rv