as_dict(output)
Converts an output with ExtensionType or dataclass type to dict. Annotations are preserved in the dict. Use RecursiveNamespace to load it back with annotations.
Source code in wt_ml/tuning/utils.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
calculate_curves_diff(prev_output, current_output, encodings)
Takes 2 model outputs with trackers and then calculates _calculate_curves_diff.
Source code in wt_ml/tuning/utils.py
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 |
|
calculate_curves_diff_utils(y_a, y_b, x_vals, axis=0)
Approximate \(\int_{\min{spend}}^{\max{spend}} \left(roi_i'(x) - roi_{i+1}'(x)\right)^2\) to get difference of rois or any two curves in general. Units of the result are the same as the units of the y values passed in.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_a |
ndarray
|
Curve 1 y values at each x. Shape: num_points, batch, vehicles. |
required |
y_b |
ndarray
|
Curve 2 y values at each x. Shape: num_points, batch, vehicles. |
required |
x_vals |
TensorLike
|
The common x axis values of the two curves. Shape: spend_points,. |
required |
axis |
int
|
The axis of integration, defaults to the 1st axis (spend_points). |
0
|
Returns: np.ndarray: Computed integral of squared differences between two curves.
Source code in wt_ml/tuning/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
concat_intermediaries(intermediaries, axis=0, axis_type=Axis.Batch)
Concatenates tf.experimental.ExtensionType objects in the batch_axis.
Make sure that both axis
and axis_type
matches else it leads to unexpected results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
intermediaries |
list[ExtensionType]
|
tf.experimental.ExtensionType type instance. |
required |
axis |
int
|
The axis where we need to concatenate. Defaults to 0 which is assumed to be batch. |
0
|
axis_type(Axis, |
optional
|
The annotated axis where we need to concatenate. Defaults to Axis.Batch. |
required |
Returns:
Type | Description |
---|---|
T
|
tf.experimental.ExtensionType: The concatenated output which is the same type that is passed in. |
Source code in wt_ml/tuning/utils.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
gather(output, indices, axis=0, axis_type=Axis.Batch)
Gathers slices from different params in output
according to indices
.
NOTE: this can lead to unexpected behaviour when tensors are not annotated as the axis
might be available but
it's not meant to be gathered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
list[ExtensionType]
|
tf.experimental.ExtensionType type instance. |
required |
indices |
NDArray[int32]
|
The index Tensor. Must be one of the following types: int32, int64. The values must be in range [0, params.shape[axis]). |
required |
axis |
int
|
The axis where we need to concatenate. Defaults to 0 which is assumed to be batch. |
0
|
axis_type(Axis, |
optional
|
The annotated axis where we need to concatenate. Defaults to Axis.Batch. |
required |
Returns:
Type | Description |
---|---|
T
|
tf.experimental.ExtensionType: The gathered output which is the same type that is passed in. |
Source code in wt_ml/tuning/utils.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
get_index(level, inputs, encodings)
For given level
, pd.Index | pd.MultiIndex is created.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level |
tuple[str, ...] | str | None
|
The level(s) on which we need to create the index from. |
required |
inputs |
EconomicModelInput
|
Inputs intermediearies that contains the level(s) index values. |
required |
encodings |
dict[str, dict[str, int]]
|
Encodings dict which will be used to decode the index values. |
required |
Returns:
Type | Description |
---|---|
Index | MultiIndex
|
pd.Index | pd.MultiIndex: The decoded index created based on level(s). |
Source code in wt_ml/tuning/utils.py
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 185 |
|
get_indexer(index, target)
Compute indexer and mask for new index given the current index.
The indexer should be then used as an input to ndarray.take to align the current data to the new index.
This is based on pd.Index.get_indexer. Pandas implementation doesn't support if the index
is not unique.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
MultiIndex | Index
|
Current index. |
required |
target |
Sequence[tuple | Hashable]
|
New target index, we like to compute indexes on current index. |
required |
Returns:
Type | Description |
---|---|
NDArray[intp]
|
NDArray[np.intp]: Integers from 0 to n - 1 indicating that the index at these positions matches the corresponding target values. Missing values in the target are marked by -1. |
Source code in wt_ml/tuning/utils.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
process_period_curves_diff(differences)
Processes the differences calculated for each period and curves.
Source code in wt_ml/tuning/utils.py
118 119 120 121 122 123 124 |
|
sort_batch_index(output, encodings, level=('brand', 'wholesaler'), axis=0, axis_type=Axis.Batch)
Sorts the given intermediaries on the batch axis, based on level indexes. The sort order is based on the level(s) provided. This is useful when we need to have the outputs in the same order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
EconomicIntermediaries | dict[str, EconomicIntermediaries]
|
Outputs of the model. |
required |
encodings |
dict[str, dict[str, int]]
|
Encodings dict which will be used to decode the index values. |
required |
level |
tuple[str, ...]
|
The level(s) on which the batches is ordered. Defaults to ("brand", "wholesaler"). |
('brand', 'wholesaler')
|
axis |
int
|
The batch axis. Defaults to 0 which is assumed to be batch. |
0
|
axis_type(Axis, |
optional
|
The annotated batch axis. Defaults to Axis.Batch. |
required |
Returns:
Type | Description |
---|---|
EconomicIntermediaries | dict[str, EconomicIntermediaries]
|
EconomicIntermediaries | dict[str, EconomicIntermediaries]: The sorted intermediares based on level(s). |
Source code in wt_ml/tuning/utils.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|