source: trunk/GSASIIpy3.py

Last change on this file was 4534, checked in by toby, 15 months ago

implement variable limits; show cell under Dij vals

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 8.3 KB
RevLine 
[933]1'''
2*GSASIIpy3: Python 3.x Routines*
3================================
4
5Module to hold python 3-compatible code, to keep it separate from
6code that will break with __future__ options.
7
8'''
[3136]9from __future__ import division, print_function
[933]10import numpy as np
[1046]11import GSASIIpath
[1077]12GSASIIpath.SetVersionNumber("$Revision: 4534 $")
[1447]13# declare symbol (pi) and functions allowed in expressions
[933]14sind = sin = s = lambda x: np.sin(x*np.pi/180.)
15cosd = cos = c = lambda x: np.cos(x*np.pi/180.)
16tand = tan = t = lambda x: np.tan(x*np.pi/180.)
17sqrt = sq = lambda x: np.sqrt(x)
18pi = np.pi
19
[4534]20# formatting for unique cell parameters by Laue type
21cellGUIlist = [[['m3','m3m'],4,[" Unit cell: a = "],["{:.5f}"],[True],[0]],
22[['3R','3mR'],6,[" a = ",u" \u03B1 = "],["{:.5f}","{:.3f}"],[True,True],[0,3]],
23[['3','3m1','31m','6/m','6/mmm','4/m','4/mmm'],6,[" a = "," c = "],["{:.5f}","{:.5f}"],[True,True],[0,2]],
24[['mmm'],8,[" a = "," b = "," c = "],["{:.5f}","{:.5f}","{:.5f}"],
25    [True,True,True],[0,1,2]],
26[['2/m'+'a'],10,[" a = "," b = "," c = ",u" \u03B1 = "],
27    ["{:.5f}","{:.5f}","{:.5f}","{:.3f}"],[True,True,True,True,],[0,1,2,3]],
28[['2/m'+'b'],10,[" a = "," b = "," c = ",u" \u03B2 = "],
29    ["{:.5f}","{:.5f}","{:.5f}","{:.3f}"],[True,True,True,True,],[0,1,2,4]],
30[['2/m'+'c'],10,[" a = "," b = "," c = ",u" \u03B3 = "],
31    ["{:.5f}","{:.5f}","{:.5f}","{:.3f}"],[True,True,True,True,],[0,1,2,5]],
32[['-1'],7,[" a = "," b = "," c = ",u" \u03B1 = ",u" \u03B2 = ",u" \u03B3 = "],
33    ["{:.5f}","{:.5f}","{:.5f}","{:.3f}","{:.3f}","{:.3f}"],
34    [True,True,True,False,True,True,True],[0,1,2,3,4,5]]]
35
[933]36def FormulaEval(string):
37    '''Evaluates a algebraic formula into a float, if possible. Works
38    properly on fractions e.g. 2/3 only with python 3.0+ division.
39
40    Expressions such as 2/3, 3*pi, sin(45)/2, 2*sqrt(2), 2**10 can all
41    be evaluated.
42
[937]43    :param str string: Character string containing a Python expression
44      to be evaluated.
45
46    :returns: the value for the expression as a float or None if the expression does not
47      evaluate to a valid number.
48   
[933]49    '''
50    try:
51        val = float(eval(string))
52        if np.isnan(val) or np.isinf(val): return None
53    except:
54        return None
55    return val
56
[1236]57def FormatPadValue(val,maxdigits=None):
58    '''Format a float to fit in ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
59
60    :param float val: number to be formatted.
61
62    :param list maxdigits: the number of digits & places after decimal to be used for display of the
63      number (defaults to [10,2]).
64
65    :returns: a string with exactly maxdigits[0] characters (except under error conditions),
66      but last character will always be a space
67    '''
68    if maxdigits is None:
69        digits = [10,2]
70    else:
[1408]71        digits = list(maxdigits)
[1236]72    fmt = '{:'+str(digits[0])+'}'
73    s = fmt.format(FormatValue(val,digits))
74    if s[-1] == ' ':
75        return s
76    else:
77        return s+' '
78   
79
[1183]80def FormatValue(val,maxdigits=None):
[1410]81    '''Format a float to fit in at most ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
82    Note that this code has been hacked from FormatSigFigs and may have unused sections.
[937]83
84    :param float val: number to be formatted.
85
[2569]86    :param list maxdigits: the number of digits, places after decimal and 'f' or 'g' to be used for display of the
87      number (defaults to [10,2,'f']).
[937]88
[1236]89    :returns: a string with <= maxdigits characters (usually). 
[933]90    '''
[1658]91    if 'str' in str(type(val)) and (val == '?' or val == '.'):
92        return val       
[1183]93    if maxdigits is None:
[2569]94        digits = [10,2,'f']
[1183]95    else:
[1408]96        digits = list(maxdigits)
[2569]97    if len(digits) == 2:
[2574]98        digits.append('f')
[2569]99    if not val:
100        digits[2] = 'f'
101    fmt="{:"+str(digits[0])+"."+str(digits[1])+digits[2]+"}"
[1447]102    string = fmt.format(float(val)).strip() # will standard .f formatting work?
[1410]103    if len(string) <= digits[0]:
[1447]104        if ':' in string: # deal with weird bug where a colon pops up in a number when formatting (EPD 7.3.2!)
105            string = str(val)
[4534]106        if digits[1] > 0 and not 'e' in string.lower(): # strip off extra zeros on right side
[1410]107            string = string.rstrip('0')
108            if string[-1] == '.': string += "0"
109        return string
110    if val < 0: digits[0] -= 1 # negative numbers, reserve space for the sign
[1183]111    decimals = digits[0] - digits[1]
[1410]112    if abs(val) > 1e99: # for very large numbers, use scientific notation and use all digits
113        fmt = "{" + (":{:d}.{:d}g".format(digits[0],digits[0]-6))+"}"
114    elif abs(val) > 1e9:
115        fmt = "{" + (":{:d}.{:d}g".format(digits[0],digits[0]-5))+"}"
[933]116    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
[1410]117        # this clause is probably no longer needed since the number probably shows as 0.0
[1183]118        decimals = min(digits[0]-5,digits[1])
119        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
[1410]120    elif abs(val) >= 10**(decimals-1): # deal with large numbers in smaller spaces
121        decimals = max(0,digits[0]-5)
[1183]122        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
[933]123    elif abs(val) < 1: # use f format for small numbers
[1410]124        # this clause is probably no longer needed since the number probably shows as 0.0
[1183]125        decimals = min(digits[0]-3,digits[1])
126        fmt = "{" + (":{:d}.{:d}f".format(digits[0],decimals))+"}"
[933]127    else: # in range where g formatting should do what I want
[1410]128        # used?
[3825]129        decimals = digits[0] - 6
[1183]130        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
[1181]131    try:
[1236]132        return fmt.format(float(val)).strip()
[2546]133    except ValueError:
[3136]134        print ('FormatValue Error with val,maxdigits,fmt= %f %d %s'%(val,maxdigits,fmt))
[1181]135        return str(val)
[1183]136
137def FormatSigFigs(val, maxdigits=10, sigfigs=5, treatAsZero=1e-20):
138    '''Format a float to use ``maxdigits`` or fewer digits with ``sigfigs``
139    significant digits showing (if room allows).
140
141    :param float val: number to be formatted.
142
143    :param int maxdigits: the number of digits to be used for display of the
144       number (defaults to 10).
145
146    :param int sigfigs: the number of significant figures to use, if room allows
147
148    :param float treatAsZero: numbers that are less than this in magnitude
149      are treated as zero. Defaults to 1.0e-20, but this can be disabled
150      if set to None.
151
152    :returns: a string with <= maxdigits characters (I hope). 
153    '''
[1658]154    if 'str' in str(type(val)) and (val == '?' or val == '.'):
155        return val       
[1183]156    if treatAsZero is not None:
157        if abs(val) < treatAsZero:
158            return '0.0'
159    # negative numbers, leave room for a sign
[3068]160    if np.isnan(val):
161        return str(val)
162    if val < 0: maxdigits -= 1       
[1183]163    if abs(val) < 1e-99 or abs(val) > 9.999e99:
164        decimals = min(maxdigits-6,sigfigs)
165        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
166    elif abs(val) < 1e-9 or abs(val) > 9.999e9:
167        decimals = min(maxdigits-5,sigfigs)
168        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
169    elif abs(val) < 9.9999999*10**(sigfigs-maxdigits):
170        decimals = min(maxdigits-5,sigfigs)
171        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
172    elif abs(val) >= 10**sigfigs: # deal with large numbers in smaller spaces
173        decimals = min(maxdigits-5,sigfigs)
174        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
175    elif abs(val) < 1: # small numbers, add to decimal places
[3068]176        decimals = sigfigs - int(np.log10(np.abs(val)))
[1183]177        fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
178    else: # larger numbers, remove decimal places
[3068]179        decimals = sigfigs - 1 - int(np.log10(np.abs(val)))
[1255]180        if decimals <= 0: 
181            fmt = "{" + (":{:d}.0f".format(maxdigits))+"}."
182        else:
183            fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
[1183]184    try:
[1236]185        return fmt.format(float(val)).strip()
[2546]186    except ValueError:
[3136]187        print ('FormatValue Error with val,maxdigits, sigfigs, fmt=%f %d %d %s'%(val, maxdigits,sigfigs, fmt))
[1183]188        return str(val)
189
190if __name__ == '__main__':
191    for i in (1.23456789e-129,1.23456789e129,1.23456789e-99,1.23456789e99,-1.23456789e-99,-1.23456789e99):
[3136]192        print (FormatSigFigs(i),i)
[1183]193    for i in (1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000):
[3136]194        print (FormatSigFigs(1.23456789e-9*i),1.23456789e-9*i)
[1183]195    for i in (1,10,100,1000,10000,100000,1000000,10000000,100000000):
[3136]196        print (FormatSigFigs(1.23456789e9/i),1.23456789e9/i)
[1255]197
[3136]198    print (FormatSigFigs(200,10,3))
Note: See TracBrowser for help on using the repository browser.