pysmo.tools.utils
Pysmo's little helpers.
Functions:
| Name | Description |
|---|---|
average_datetimes |
Average a sequence of datetimes. |
pearson_matrix_vector |
Compute Pearson correlation of each row in a matrix against a vector. |
uuid_shortener |
Shorten a sequence of UUIDs to their shortest unique representation. |
average_datetimes
Average a sequence of datetimes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datetimes
|
Sequence[Timestamp]
|
Datetimes to average. |
required |
Returns:
| Type | Description |
|---|---|
Timestamp
|
Datetime representing average of all datetimes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an empty sequence is provides as input. |
Source code in src/pysmo/tools/utils.py
pearson_matrix_vector
Compute Pearson correlation of each row in a matrix against a vector.
This is a vectorized alternative to calling
scipy.stats.pearsonr in a loop. All row
correlations are computed in a single matrix operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
ndarray
|
2D array of shape |
required |
vector
|
ndarray
|
1D array of length |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1D array of length |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If the number of columns in |
Examples:
>>> import numpy as np
>>> from pysmo.tools.utils import pearson_matrix_vector
>>>
>>> vector = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
>>> matrix = np.array([
... [1.0, 2.0, 3.0, 4.0, 5.0],
... [5.0, 4.0, 3.0, 2.0, 1.0],
... [1.0, 1.0, 1.0, 1.0, 1.0],
... ])
>>> pearson_matrix_vector(matrix, vector)
array([ 1., -1., 0.])
>>>
Source code in src/pysmo/tools/utils.py
uuid_shortener
Shorten a sequence of UUIDs to their shortest unique representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uuids
|
Sequence[UUID]
|
UUIDs to shorten. |
required |
min_length
|
int
|
Minimum length of the shortened UUID strings. |
4
|
Returns:
| Type | Description |
|---|---|
dict[str, UUID]
|
Dictionary mapping shortened UUID strings to their original UUID objects. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an empty sequence is provided as input. |
ValueError
|
If |
Examples:
>>> import uuid
>>> from pysmo.tools.utils import uuid_shortener
>>>
>>> uuids = [uuid.uuid4() for _ in range(100)]
>>> for k, v in sorted(uuid_shortener(uuids).items()):
... print(f"{k} -> {v}")
...
01d7 -> 01d74256-3860-4ab6-96a4-02f23ae8cc93
03c7 -> 03c72ba8-d605-4770-8a63-f881ffd0f9d5
080a -> 080aadfb-e7c9-4b26-9141-25c63a9bedd4
0e51 -> 0e51f30d-c6a7-4e39-84b0-32ccd7c524a5
...
>>>