forked from diffpy/diffpy.srmise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfromsequence.py
211 lines (170 loc) · 6.83 KB
/
fromsequence.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
##############################################################################
#
# SrMise by Luke Granlund
# (c) 2014 trustees of the Michigan State University
# (c) 2024 trustees of Columia University in the City of New York
# All rights reserved.
#
# File coded by: Luke Granlund
#
# See LICENSE.txt for license information.
#
##############################################################################
import logging
import numpy as np
import scipy.interpolate as spi
from diffpy.srmise.baselines.base import BaselineFunction
logger = logging.getLogger("diffpy.srmise")
class FromSequence(BaselineFunction):
"""Methods for evaluation of a baseline from discrete data via interpolation.
FromSequence uses cubic spline interpolation (no smoothing) on discrete
points to approximate the baseline at arbitrary points within the
interpolation domain. This baseline function permits no free parameters."""
def __init__(self, *args, **kwds):
"""Initialize baseline corresponding to sequences x and y.
Usage:
FromSequence(xlist, ylist) or
FromSequence(x=xlist, y=ylist)
FromSequence("filename") or
FromSequence(file="filename")
Parameters/Keywords
x: Sequence of x values defining baseline.
y: Sequence of y values defining baseline.
or
file: Name of file with column of x values and column of y values.
"""
if len(args) == 1 and len(kwds) == 0:
# load from file
x, y = self.readxy(args[0])
elif len(args) == 0 and ("file" in kwds and "x" not in kwds and "y" not in kwds):
# load file
x, y = self.readxy(kwds["file"])
elif len(args) == 2 and len(kwds) == 0:
# Load x, y directly from arguments
x = args[0]
y = args[1]
elif len(args) == 0 and ("x" in kwds and "y" in kwds and "file" not in kwds):
# Load x, y from keywords
x = kwds["x"]
y = kwds["y"]
else:
emsg = "Call to FromSequence does not match any allowed signature."
raise TypeError(emsg)
# Guarantee valid lengths
if len(x) != len(y):
emsg = "Sequences x and y must have the same length."
raise ValueError(emsg)
parameterdict = {}
formats = ["internal"]
default_formats = {"default_input": "internal", "default_output": "internal"}
self.spline = spi.InterpolatedUnivariateSpline(x, y)
self.minx = x[0]
self.maxx = x[-1]
metadict = {}
metadict["x"] = (x, self.xyrepr)
metadict["y"] = (y, self.xyrepr)
BaselineFunction.__init__(self, parameterdict, formats, default_formats, metadict, None, Cache=None)
# Methods required by BaselineFunction ####
def estimate_parameters(self, r, y):
"""Return empty numpy array.
A FromSequence object has no free parameters, so there is nothing
to estimate.
Parameters
r: (Numpy array) Data along r from which to estimate, Ignored
y: (Numpy array) Data along y from which to estimate, Ignored"""
return np.array([])
def _jacobianraw(self, pars, r, free):
"""Return [].
A FromSequence baseline has no parameters.
Parameters
pars: Empty sequence
r: sequence or scalar over which pars is evaluated
free: Empty sequence."""
if len(pars) != self.npars:
emsg = "Argument pars must have " + str(self.npars) + " elements."
raise ValueError(emsg)
if len(free) != self.npars:
emsg = "Argument free must have " + str(self.npars) + " elements."
raise ValueError(emsg)
return []
def _transform_parametersraw(self, pars, in_format, out_format):
"""Convert parameter values from in_format to out_format.
Parameters
pars: Sequence of parameters
in_format: A format defined for this class
out_format: A format defined for this class
Defined Formats
n/a, FromSequence has no parameters"""
temp = np.array(pars)
# Convert to intermediate format "internal"
if in_format == "internal":
pass
else:
raise ValueError("Argument 'in_format' must be one of %s." % self.parformats)
# Convert to specified output format from "internal" format.
if out_format == "internal":
pass
else:
raise ValueError("Argument 'out_format' must be one of %s." % self.parformats)
return temp
def _valueraw(self, pars, r):
"""Return value of polynomial for the given parameters and r values.
Parameters
pars: Empty sequence
r: sequence or scalar over which pars is evaluated"""
if len(pars) != self.npars:
emsg = "Argument pars must have " + str(self.npars) + " elements."
raise ValueError(emsg)
try:
if r[0] < self.minx or r[-1] > self.maxx:
logger.warn(
"Warning: Evaluating interpolating function over %s, outside safe range of %s.",
[r[0], r[-1]],
[self.minx, self.maxx],
)
except (IndexError, TypeError):
if r < self.minx or r > self.maxx:
logger.warn(
"Warning: Evaluating interpolating function at %s, outside safe range of %s.",
r,
[self.minx, self.maxx],
)
return self.spline(r)
def getmodule(self):
return __name__
def xyrepr(self, var):
"""Safe string output of x and y, compatible with eval()"""
return "[%s]" % ", ".join([repr(v) for v in var])
def readxy(self, filename):
""" """
from diffpy.srmise.srmiseerrors import SrMiseDataFormatError
# TODO: Make this safer
try:
datastring = open(filename, "rb").read()
except Exception as err:
raise err
import re
res = re.search(r"^[^#]", datastring, re.M)
if res:
datastring = datastring[res.end() :].strip()
x = []
y = []
try:
for line in datastring.split("\n"):
v = line.split()
x.append(float(v[0]))
y.append(float(v[1]))
except (ValueError, IndexError) as err:
raise SrMiseDataFormatError(str(err))
return (np.array(x), np.array(y))
# end of class FromSequence
# simple test code
if __name__ == "__main__":
r = np.arange(0, 9.42413, 0.2)
b = -(np.tanh(0.5 * r) + np.sin(0.5 * r))
f = FromSequence(r, b)
pars = np.array([])
free = np.array([])
r2 = np.arange(0, 9.42413, 0.5)
b2 = f._valueraw(pars, r2)