TimeSeries
class#
Bases: ndarray
Array-like object representing time-series data
This object wraps the basic numpy array object, but stores (at least a reference to) a corresponding array of time values, and provides several member functions for interpolating, differentiating, and integrating.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_array |
(..., N, ...) array_like
|
Input data representing the dependent variable, in any form that can be
converted to a numpy array. This includes scalars, lists, lists of tuples,
tuples, tuples of tuples, tuples of lists, and numpy ndarrays. It can have
an arbitrary number of dimensions, but the length |
required |
time |
(N,) array_like
|
1-D array containing values of the independent variable. Values must be real, finite, and in strictly increasing order. |
required |
time_axis |
int
|
Axis along which |
required |
Source code in sxs/time_series.py
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 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 354 355 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
ddot
property
#
Differentiate modes twice with respect to time
See Also
derivative : This property simply returns self.derivative(2)
dot : Property returning self.derivative(1)
.
dot
property
#
Differentiate modes once with respect to time
See Also
derivative : This property simply returns self.derivative(1)
ddot : Property returning self.derivative(2)
.
iint
property
#
Integrate modes twice with respect to time
See Also
antiderivative : This property simply returns self.antiderivative(2)
int : Property returning self.antiderivative(1)
.
int
property
#
Integrate modes once with respect to time
See Also
antiderivative : This property simply returns self.antiderivative(1)
iint : Property returning self.antiderivative(2)
.
n_times
property
#
Size of the array along the time_axis
ndarray
property
#
View this array as a numpy ndarray
time
property
writable
#
Array of the time steps corresponding to the data
time_axis
property
#
Axis of the array along which time varies
At the time time[i]
, the corresponding values of the data are
np.take(input_array, i, axis=time_axis)
.
time_broadcast
property
#
Array of the time steps broadcast to same shape as data
This property returns a new view (usually involving no copying of memory)
of the time
array, with additional dimensions to match the shape of the
data.
__getitem__(key)
#
Extract a slice of this object
Note that slicing this object works slightly differently than slicing the underlying ndarray object, basically because we want to ensure that the returned object is still a TimeSeries object.
First, if a single element is requested along the time dimension, that
dimension will not be removed. For a 2-d ndarray arr
, taking arr[3]
will
return a 1-d array; the first dimension will be removed because only the third
element is extracted. For a 2-d TimeSeries ts
with time_axis=0
, ts[3]
will return a 2-d TimeSeries; the first dimension will just have size 1,
representing the third element. If the requested element is not along the time
dimension, the requested dimension will be removed as usual.
Also, taking an irregular slice of this object is not permitted. For example:
>>> a = np.arange(3*4).reshape(3, 4)
>>> a[a % 5 == 0]
array([ 0, 5, 10])
Even though a % 5 == 0
is a 2-d array, indexing flattens a
and the indexing
set, so that the result is a 1-d array. This probably does not make sense for
TimeSeries arrays, so attempting to do something like this raises a ValueError.
Source code in sxs/time_series.py
antiderivative(antiderivative_order=1)
#
Integrate modes with respect to time
Parameters:
Name | Type | Description | Default |
---|---|---|---|
antiderivative_order |
int
|
Order of antiderivative to evaluate. Default value is 1. Must be between -3 and 3, inclusive. |
1
|
See Also
scipy.interpolate.CubicSpline :
The function that this function is based on.
interpolate :
This function simply calls self.interpolate
with appropriate arguments.
int :
Property calling self.antiderivative(1)
.
iint :
Property calling self.antiderivative(2)
.
Source code in sxs/time_series.py
derivative(derivative_order=1)
#
Differentiate modes with respect to time
Parameters:
Name | Type | Description | Default |
---|---|---|---|
derivative_order |
int
|
Order of derivative to evaluate. Default value is 1. Must be between -3 and 3, inclusive. |
1
|
See Also
scipy.interpolate.CubicSpline :
The function that this function is based on.
interpolate :
This function simply calls self.interpolate
with appropriate arguments.
dot :
Property returning self.derivative(1)
.
ddot :
Property returning self.derivative(2)
.
Source code in sxs/time_series.py
index_closest_to(t)
#
Time index closest to the given time t
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
float
|
|
required |
Returns:
Name | Type | Description |
---|---|---|
idx |
int
|
Index such that abs(self.time[idx]-t) is as small as possible |
Source code in sxs/time_series.py
interpolate(new_time, derivative_order=0, out=None)
#
Interpolate this object to a new set of times
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_time |
array_like
|
Points to evaluate the interpolant at |
required |
derivative_order |
int
|
Order of derivative to evaluate. If negative, the antiderivative is returned. Default value of 0 returns the interpolated data without derivatives or antiderivatives. Must be between -3 and 3, inclusive. |
0
|
See Also
scipy.interpolate.CubicSpline :
The function that this function is based on.
antiderivative :
Calls this funtion with new_time=self.time
and
derivative_order=-antiderivative_order
(defaulting to a single
antiderivative).
derivative :
Calls this function new_time=self.time
and
derivative_order=derivative_order
(defaulting to a single derivative).
dot :
Property calling self.derivative(1)
.
ddot :
Property calling self.derivative(2)
.
int :
Property calling self.antiderivative(1)
.
iint :
Property calling self.antiderivative(2)
.
Notes
This function is essentially a wrapper around scipy.interpolate.CubicSpline
Source code in sxs/time_series.py
register_modification(func, **kwargs)
#
Add a record of a modification to the metadata
Note that this function does not actually run the modification; it simply records the function name and arguments in this object's metadata. You are expected to run the function for yourself, with the given keyword arguments.
Also note that the modifications will most likely be written to JSON, so you
should adjust them to be in basic formats suitable for JSON. For example, if
an argument arr
is ordinarily passed as a numpy array, you should convert to
a list, with something like arr.tolist()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
named function
|
The function that will modify (or already has modified) this object. The
function must have a |
required |
Because |
|
required | |
cannot |
|
required | |
call |
|
required |
Source code in sxs/time_series.py
truncate(abs_tolerance)
#
Truncate the precision of this object's data
in place
This function sets bits in the array data to 0 when they have lower
significance than the number given as or returned by abs_tolerance
. This is
a useful step in compressing data — though it is obviously lossy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
abs_tolerance |
(callable, float, array - like)
|
If callable, it is called with this object as the parameter, and the returned value is treated as a float or array-like would be. Floats are simply treated as a uniform absolute tolerance to be applied at all times. Array-like objects must broadcast against this array, and each element is treated as the absolute tolerance for all the elements it broadcasts to. |
callable
|
Returns:
Type | Description |
---|---|
None
|
This value is returned to serve as a reminder that this function operates in place. |
Notes
The effect is achieved by multiplying the array's data by the same power of 2
that would be required to bring the abs_tolerance
to between 1 and 2. Thus,
all digits less significant than 1 are less significant than abs_tolerance
—
meaning that we can apply the standard round
routine to set these digits to
0. We then divide by that same power of 2 to bring the array data back to
nearly its original value. By working with powers of 2, we ensure that the 0s
at the intermediate stage are represented as 0 bits in the final result.
For floats and array-like objects, all values must be strictly positive, or
inf
or nan
will result.
Source code in sxs/time_series.py
xor(reverse=False, preserve_dtype=False, **kwargs)
#
Progressively XOR data along the time axis
This function steps through an array, starting with the second element, and evaluates bitwise XOR on that element and the preceding one. This is a useful step in compressing reasonably continuous data.
See the documentation of sxs.utilities.xor
for a full description of this
function. Note that this version sets the axis
argument automatically to be
the time_axis
.