-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
184 lines (155 loc) · 4.88 KB
/
Copy path__init__.py
File metadata and controls
184 lines (155 loc) · 4.88 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
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
# Package API Configuration
# NOTE: IDE linting will show an error for .react_rs imports (ignore them)
import typing as _typing
import polars as _pl
# Import native Python code
from ._weibull import Weibull
# Define functionality to be made available to users
__all__ = [
# Rust modules
"simulate",
"constrain",
"aggregate",
"profile",
# Native Python modules
"Weibull",
]
# Implement Rust backend functionality in Python
def simulate(
df: _pl.DataFrame,
id_col: str,
age_col: str,
cost_col: str,
probs_col: str,
n_sims: int,
n_steps: int,
parallel_limit: int,
) -> _pl.DataFrame:
"""
Simulate (Rust)
---
Execute a discrete event simulation via Rust using a Weibull model
Inputs
---
- df : polars DataFrame containing input information for simulation items
- id_col : string name of column in df containing items unique ID
- age_col : string name of column in df containing initial age of each item
- cost_col : string name of column in df containing value of each item
- probabilities : list of floats represending the survival curve of the items
- n_sims : int value for the number of simulations to execute on the complete set of items
- n_steps : int value for the number of timesteps to execute the simulation over
- para_limit : int value for the maximum number of parallelised simulations to run at any one time
Returns
---
polars.DataFrame
"""
from .react_rs import simulate as rs_sim
return rs_sim(
df=df,
id_col=id_col,
age_col=age_col,
cost_col=cost_col,
probs_col=probs_col,
n_sims=n_sims,
n_steps=n_steps,
para_limit=parallel_limit,
)
def constrain(
df: _pl.DataFrame,
constrain_steps: int,
iter_regex: str,
cost_col: str,
constraints: _typing.List[int],
partition_by: str,
parallel_limit: int,
) -> _pl.DataFrame:
"""
Constrain (Rust)
---
Apply a financial limit to the output of a simulation & reallocate assets
within each unique simulation output
Inputs
---
- df : polars DataFrame containing the output of a simulation to be constrained
- iter_regex : string pattern for accessing the unique timesteps in the
- cost_col : string name of column in df containing value of each item
- constraints : list of integers representing the financial limit to be applied
in each timestep (length must match number of timesteps in simulation output)
- partition_by : string column name containing the simulation ID
- run_method : string trigger for rust run method - options: full / batched / parallel
- para_limit : int value for the maximum number of parallelised simulations to run at any one time
Returns
---
polars.DataFrame
"""
from .react_rs import constrain as rs_constrain
return rs_constrain(
df=df,
constrain_steps=constrain_steps,
iter_regex=iter_regex,
cost_col=cost_col,
constraints=constraints,
partition_by=partition_by,
para_limit=parallel_limit,
)
def aggregate(
df: _pl.DataFrame,
partition_by: str,
iter_regex: str,
target_value: int,
cost_col: str | None,
) -> _pl.DataFrame:
"""
Aggregate (Rust)
---
Inputs
---
- df : polars DataFrame containing the aggregation target table
- partition_by : string column name containing the simulation ID
- iter_regex : string pattern for accessing the unique timesteps in the
simulation output
- target_value : int value to target for aggregations, typically this will be 0
to represent when an asset is replaced
- cost_col : optional
- string column name containing the value of the items
- if provided, target_value occurances will be converted from count to value
Returns
---
polars.DataFrame
"""
from .react_rs import aggregate as rs_agg
return rs_agg(
df=df,
partition_by=partition_by,
iter_regex=iter_regex,
target_value=target_value,
cost_col=cost_col,
)
def profile(
df: _pl.DataFrame,
partition_by: str,
iter_regex: str,
parallel_limit: int,
) -> _pl.DataFrame:
"""
Profile (Rust)
---
Create a value count for a DataFrame located @ filepath
Inputs
---
- df : polars DataFrame containing the profile target table
- partition_by : string column name containing the simulation ID
- iter_regex : string pattern for accessing the unique timesteps in the
simulation output
- para_limit : int value for the maximum number of parallelised simulations to run at any one time
Returns
---
polars DataFrame
"""
from .react_rs import profile as rs_profile
return rs_profile(
df=df,
partition_by=partition_by,
iter_regex=iter_regex,
para_limit=parallel_limit,
)