Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BEA data analysis utils
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Philipp Niedermayer
BEA data analysis utils
Commits
ce08a494
Commit
ce08a494
authored
2 years ago
by
Philipp Niedermayer
Browse files
Options
Downloads
Patches
Plain Diff
Implement ODR fit
parent
20538c5e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fitting.py
+18
-3
18 additions, 3 deletions
fitting.py
with
18 additions
and
3 deletions
fitting.py
+
18
−
3
View file @
ce08a494
import
scipy
import
scipy
import
scipy.odr
import
numpy
as
np
import
numpy
as
np
...
@@ -31,12 +32,26 @@ def fit_lorenzian(S, V, log=False, **kwargs):
...
@@ -31,12 +32,26 @@ def fit_lorenzian(S, V, log=False, **kwargs):
return
param
,
param_error
,
function
,
[
X
,
function
(
X
,
*
param
)]
return
param
,
param_error
,
function
,
[
X
,
function
(
X
,
*
param
)]
def
fit_function
(
function
,
x
,
y
,
p0
=
None
,
extend
=
0
,
**
kwargs
):
def
fit_function
(
function
,
x
,
y
,
*
,
xerr
=
None
,
yerr
=
None
,
p0
=
None
,
odr
=
None
,
extend
=
0
,
**
kwargs
):
"""
"""
:param log: if the y-data is log-scaled
:param log: if the y-data is log-scaled
"""
"""
param
,
cov
=
scipy
.
optimize
.
curve_fit
(
function
,
x
,
y
,
p0
,
**
kwargs
)
if
odr
is
None
:
param_error
=
np
.
sqrt
(
np
.
abs
(
cov
.
diagonal
()))
odr
=
xerr
is
not
None
or
yerr
is
not
None
if
odr
:
# orthogonal distance regression
data
=
scipy
.
odr
.
RealData
(
x
,
y
,
sx
=
xerr
,
sy
=
yerr
)
model
=
scipy
.
odr
.
Model
(
lambda
beta
,
x
:
function
(
x
,
*
beta
))
odr
=
scipy
.
odr
.
ODR
(
data
,
model
,
p0
)
# if not odr: odr.set_job(fit_type=2) # ordinary least-squares
output
=
odr
.
run
()
param
,
param_error
=
output
.
beta
,
output
.
sd_beta
else
:
# non-linear least squares
param
,
cov
=
scipy
.
optimize
.
curve_fit
(
function
,
x
,
y
,
p0
,
**
kwargs
)
param_error
=
np
.
sqrt
(
np
.
abs
(
cov
.
diagonal
()))
mi
,
ma
=
min
(
x
),
max
(
x
)
mi
,
ma
=
min
(
x
),
max
(
x
)
X
=
np
.
linspace
(
mi
-
(
ma
-
mi
)
*
extend
,
ma
+
(
ma
-
mi
)
*
extend
,
1000
)
X
=
np
.
linspace
(
mi
-
(
ma
-
mi
)
*
extend
,
ma
+
(
ma
-
mi
)
*
extend
,
1000
)
return
param
,
param_error
,
function
,
[
X
,
function
(
X
,
*
param
)]
return
param
,
param_error
,
function
,
[
X
,
function
(
X
,
*
param
)]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment