Created
July 2, 2018 13:53
-
-
Save thomasaarholt/b231bc2c49be33a2686b0fe99d508ad6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string = 'a*x**r+b*x+c' | |
expression = sympy.sympify(string) | |
expr = hs.model.components1D.Expression(string, 'test') | |
data = np.random.random((5,300)) | |
s = hs.signals.Signal1D(data) | |
m = s.create_model() | |
m.append(expr) | |
r,a,c = expr.r, expr.a, expr.c | |
r.value = 0 | |
r.free = False | |
a.value = 10 | |
a.free = True | |
c.value = 20 | |
a.assign_current_value_to_all() | |
r.assign_current_value_to_all() | |
c.assign_current_value_to_all() | |
subs = tuple() | |
names = tuple() | |
values = tuple() | |
# handles the case when a fixed parameter has a value such that the expression becomes independent of x | |
for para in set(expr.parameters) - set(expr.free_parameters): | |
# for simultaneous multifit, this will only be safe if all values in the map are equal: | |
if False:# Simultaneous multifit scenario | |
values_map = para.map['values'] | |
assert (values_map == values_map.flat[0]).all(), 'Please use para.assign_current_value_to_all() on parameter {}'.format(para) # flat handles the case where para is multidimensional | |
names += (para.name, ) | |
values += (para.value, ) | |
subs += ((para.name, para.value),) | |
new_expression = expression.subs(subs) | |
constant, not_constant = extract_constant_part_of_expression(new_expression, 'x') | |
names = tuple() | |
values = tuple() | |
for symbol in constant.free_symbols: | |
para = getattr(expr, str(symbol)) | |
names += (para.name, ) | |
if False: # Simultaneous multifit scenario | |
values += (para.map['values'], ) | |
else: | |
values += (para.value, ) | |
constant2 = lambdify(names, constant, 'numpy')(*values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment