Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
toarstats
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
esde
toar-public
toarstats
Merge requests
!9
Develop
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Develop
develop
into
master
Overview
0
Commits
17
Pipelines
0
Changes
4
Merged
Ghost User
requested to merge
develop
into
master
1 year ago
Overview
0
Commits
17
Pipelines
0
Changes
4
Expand
0
0
Merge request reports
Viewing commit
d427f3ae
Prev
Next
Show latest version
4 files
+
116
−
38
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
d427f3ae
Added moving block bootstrap algorithm to the trend calculation.
· d427f3ae
Niklas Selke
authored
1 year ago
toarstats/trends/interface.py
+
44
−
17
Options
@@ -4,43 +4,70 @@ This module contains the following function:
@@ -4,43 +4,70 @@ This module contains the following function:
calculate_trend - calculate the trend using the requested method
calculate_trend - calculate the trend using the requested method
"""
"""
from
toarstats.metrics.input_checks
import
check_data
from
toarstats.trends.ols
import
ols
from
toarstats.trends.ols
import
ols
from
toarstats.trends.quant_reg
import
quant_reg
from
toarstats.trends.quant_reg
import
quant_reg
from
toarstats.trends.utils
import
deseason
ali
z
e
from
toarstats.trends.utils
import
calculate_anom
alie
s
def
calculate_trend
(
method
,
data
,
formula
=
"
value ~ datetime
"
,
quantiles
=
None
):
def
calculate_trend
(
method
,
data
,
quantiles
=
None
):
"""
Calculate the trend using the requested method.
"""
Calculate the trend using the requested method.
This function is the public interface for the ``trends`` subpackage.
This function is the public interface for the ``trends`` subpackage.
It takes all the user inputs and returns the result of the requested
It takes all the user inputs and returns the result of the requested
trend analysis.
trend analysis.
The calculation follows
"
Guidance note on best statistical practices
for TOAR analyses
"
(Chang et al. 2023,
https://arxiv.org/pdf/2304.14236.pdf) Annex E.
:param method: either ``
"
OLS
"
`` or ``
"
quant
"
``
:param method: either ``
"
OLS
"
`` or ``
"
quant
"
``
:param data: data containing a list of date time values and
:param data: data containing a list of date time values and
associated parameter values on which to calculate the
associated parameter values on which to calculate the
trend
trend
:param formula: the formula specifying the model
:param quantiles: a single quantile or a list of quantiles to
:param quantiles: a single quantile or a list of quantiles to
calculate, these must be between 0 and 1; only
calculate, these must be between 0 and 1; only
needed when ``method=
"
quant
"
``
needed when ``method=
"
quant
"
``
:raises ValueError: raised if the ``method`` parameter is not
:raises TypeError: raised if
recognized
- the ``data`` parameter is not a data frame or
series
- the index is not a datetime index
- the values are not in a one-dimensional float
or int array
:raises ValueError: raised if
- the ``method`` parameter is not recognized
- the index is empty, has duplicates or null
values
- the values array is empty or only contains
null values
- the index and values have different lengths
- any ``quantiles`` are not strictly within 0
and 1 with ``method=
"
quantreg
"
``
:return: The result of the fit or a
lis
t of fit results if
:return: The result of the fit or a
dic
t of fit results if
``method=
"
quant
"
``
and multiple quantiles are given
``method=
"
quant
"
``
"""
"""
deseasonalized_data
=
deseasonalize
(
data
)
if
method
not
in
{
"
OLS
"
,
"
quant
"
}:
if
method
==
"
OLS
"
:
fit
=
ols
(
deseasonalized_data
,
formula
)
elif
method
==
"
quant
"
:
try
:
fit
=
[
quant_reg
(
deseasonalized_data
,
formula
,
quantile
)
for
quantile
in
quantiles
]
except
TypeError
:
fit
=
quant_reg
(
deseasonalized_data
,
formula
,
quantiles
)
else
:
raise
ValueError
(
f
"
{
method
}
is not recognized, must be
'
OLS
'
or
"
raise
ValueError
(
f
"
{
method
}
is not recognized, must be
'
OLS
'
or
"
"
'
quant
'"
)
"
'
quant
'"
)
data_in
=
check_data
(
data
,
None
,
None
).
to_frame
(
"
value
"
)
if
method
==
"
quant
"
:
quantile_list
=
(
quantiles
if
isinstance
(
quantiles
,
(
list
,
set
,
tuple
))
else
[
quantiles
]
)
if
not
all
(
0
<
quantile
<
1
for
quantile
in
quantile_list
):
raise
ValueError
(
"
The quantiles must be strictly between 0 and 1.
"
)
anomalies_series
=
calculate_anomalies
(
data_in
)
if
method
==
"
quant_reg
"
:
fit
=
{
quantile
:
quant_reg
(
anomalies_series
,
quantile
)
for
quantile
in
quantile_list
}
else
:
fit
=
ols
(
anomalies_series
)
return
fit
return
fit
Loading