Repository navigation
Expand file tree
/
Copy pathdecay_rate.py
More file actions
122 lines (103 loc) · 4.03 KB
/
Copy pathdecay_rate.py
File metadata and controls
122 lines (103 loc) · 4.03 KB
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
#
# This file is part of the Buildings and Habitats object Model (BHoM)
# Copyright (c) 2015 - 2026, the respective contributors. All rights reserved.
#
# Each contributor holds copyright over their respective contributions.
# The project versioning (Git) records all such contribution source information.
#
#
# The BHoM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3.0 of the License, or
# (at your option) any later version.
#
# The BHoM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
#
import re
from enum import Enum, auto
import numpy as np
import pandas as pd
from ..bhom.analytics import bhom_analytics
class DecayMethod(Enum):
"""An enumeration of decay methods."""
LINEAR = auto()
PARABOLIC = auto()
SIGMOID = auto()
@bhom_analytics()
def proximity_decay(
value: float,
distance_to_value: float,
max_distance: float,
decay_method: DecayMethod = DecayMethod.LINEAR,
) -> float:
"""Calculate the "decayed" value based on proximity (up to a maximum distance).
Args:
value (float):
The value to be distributed.
distance_to_value (float):
A distance at which to return the magnitude.
max_distance (float):
The maximum distance to which magnitude is to be distributed. Beyond this, the input
value is 0.
decay_method (DecayMethod, optional):
A type of distribution (the shape of the distribution profile). Defaults to "DecayMethod.LINEAR".
Returns:
float:
The value at the given distance.
"""
distance_to_value = np.interp(distance_to_value, [0, max_distance], [0, 1])
if decay_method == DecayMethod.LINEAR:
return (1 - distance_to_value) * value
if decay_method == DecayMethod.PARABOLIC:
return (-(distance_to_value**2) + 1) * value
if decay_method == DecayMethod.SIGMOID:
return (1 - (0.5 * (np.sin(distance_to_value * np.pi - np.pi / 2) + 1))) * value
raise ValueError(f"Unknown curve type: {decay_method}")
@bhom_analytics()
def decay_rate_smoother(
series: pd.Series,
difference_threshold: float = -10,
transition_window: int = 4,
ewm_span: float = 1.25,
) -> pd.Series:
"""Helper function that adds a decay rate to a time-series for values dropping significantly
below the previous values.
Args:
series (pd.Series):
The series to modify.
difference_threshold (float, optional):
The difference between current/previous values which class as a "transition".
Defaults to -10.
transition_window (int, optional):
The number of values after the "transition" within which an exponentially weighted mean
should be applied. Defaults to 4.
ewm_span (float, optional):
The rate of decay. Defaults to 1.25.
Returns:
pd.Series:
A modified series
"""
# Find periods of major transition (where values vary significantly)
transition_index = series.diff() < difference_threshold
# Get boolean index for all periods within window from the transition indices
ewm_mask = []
n = 0
for i in transition_index:
if i:
n = 0
if n < transition_window:
ewm_mask.append(True)
else:
ewm_mask.append(False)
n += 1
# Run an EWM to get the smoothed values following changes to values
ewm_smoothed: pd.Series = series.ewm(span=ewm_span).mean()
# Choose from ewm or original values based on ewm mask
new_series = ewm_smoothed.where(ewm_mask, series)
return new_series