util
Utilities for FastEstimator.
DefaultKeyDict
¶
Bases: dict
Like collections.defaultdict but it passes the key argument to the default function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default |
Callable[[Any], Any]
|
A function which takes a key and returns a default value based on the key. |
required |
**kwargs |
Initial key/value pairs for the dictionary. |
{}
|
Source code in fastestimator\fastestimator\util\util.py
NonContext
¶
Bases: object
A class which is used to make nothing unusual happen.
Source code in fastestimator\fastestimator\util\util.py
Suppressor
¶
Bases: object
A class which can be used to silence output of function calls.
Source code in fastestimator\fastestimator\util\util.py
write
¶
A function which is invoked during print calls.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dummy |
str
|
The string which wanted to be printed. |
required |
Timer
¶
Bases: ContextDecorator
A class that can be used to time things.
x = lambda: list(map(lambda i: i + i/2, list(range(int(1e6)))))
with fe.util.Timer():
x() # Task took 0.1639 seconds
@fe.util.Timer("T2")
def func():
return x()
func() # T2 took 0.14819 seconds
Source code in fastestimator\fastestimator\util\util.py
draw
¶
get_batch_size
¶
Infer batch size from a batch dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
The batch dictionary. |
required |
Returns:
Type | Description |
---|---|
int
|
batch size. |
Source code in fastestimator\fastestimator\util\util.py
get_num_devices
¶
Determine the number of available GPUs.
Returns:
Type | Description |
---|---|
The number of available GPUs, or 1 if none are found. |
get_shape
¶
A function to find the shapes of an object or sequence of objects.
Lists or Tuples will assume that the zeroth dimension is ragged (shape==None). If entries in the list have mismatched ranks, then only the list dimension will be considered as part of the shape. If all ranks are equal, an attempt will be made to determine which of the interior dimensions are ragged.
x = fe.util.get_shape(np.ones((12,22,11))) # [12, 22, 11]
x = fe.util.get_shape([np.ones((12,22,11)), np.ones((18, 5))]) # [None]
x = fe.util.get_shape([np.ones((12,22,11)), np.ones((18, 5, 4))]) # [None, None, None, None]
x = fe.util.get_shape([np.ones((12,22,11)), np.ones((12, 22, 4))]) # [None, 12, 22, None]
x = fe.util.get_shape({"a": np.ones((12,22,11))}) # []
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Data to infer the shape of. |
required |
Returns:
Type | Description |
---|---|
List[Optional[int]]
|
A list representing the shape of the data. |
Source code in fastestimator\fastestimator\util\util.py
get_type
¶
A function to try and infer the types of data within containers.
x = fe.util.get_type(np.ones((10, 10), dtype='int32')) # "int32"
x = fe.util.get_type(tf.ones((10, 10), dtype='float16')) # "<dtype: 'float16'>"
x = fe.util.get_type(torch.ones((10, 10)).type(torch.float)) # "torch.float32"
x = fe.util.get_type([np.ones((10,10)) for i in range(4)]) # "List[float64]"
x = fe.util.get_type(27) # "int"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Data which may be wrapped in some kind of container. |
required |
Returns:
Type | Description |
---|---|
str
|
A string representation of the data type of the |
Source code in fastestimator\fastestimator\util\util.py
is_number
¶
Check if a given string can be converted into a number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arg |
str
|
A potentially numeric input string. |
required |
Returns:
Type | Description |
---|---|
bool
|
True iff |
Source code in fastestimator\fastestimator\util\util.py
pad_batch
¶
A function to pad a batch of data in-place by appending to the ends of the tensors.
data = [{"x": np.ones((2, 2)), "y": 8}, {"x": np.ones((3, 1)), "y": 4}]
fe.util.pad_batch(data, pad_value=0)
print(data) # [{'x': [[1., 1.], [1., 1.],[0., 0.]], 'y': 8}, {'x': [[1., 0.], [1., 0.], [1., 0.]]), 'y': 4}]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
List[MutableMapping[str, Any]]
|
A list of data to be padded. |
required |
pad_value |
Union[float, int]
|
The value to pad with. |
required |
Raises:
Type | Description |
---|---|
AssertionError
|
If the data within the batch do not have matching ranks. |
Source code in fastestimator\fastestimator\util\util.py
pad_data
¶
Pad data
by appending pad_value
s along it's dimensions until the target_shape
is reached.
x = np.ones((1,2))
x = fe.util.pad_data(x, target_shape=(3, 3), pad_value = -2) # [[1, 1, -2], [-2, -2, -2], [-2, -2, -2]]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
The data to be padded. |
required |
target_shape |
Tuple[int, ...]
|
The desired shape for |
required |
pad_value |
Union[float, int]
|
The value to insert into |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
The |
Source code in fastestimator\fastestimator\util\util.py
parse_modes
¶
A function to determine which modes to run on based on a set of modes potentially containing blacklist values.
m = fe.util.parse_modes({"train"}) # {"train"}
m = fe.util.parse_modes({"!train"}) # {"eval", "test", "infer"}
m = fe.util.parse_modes({"train", "eval"}) # {"train", "eval"}
m = fe.util.parse_modes({"!train", "!infer"}) # {"eval", "test"}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modes |
Set[str]
|
The desired modes to run on (possibly containing blacklisted modes). |
required |
Returns:
Type | Description |
---|---|
Set[str]
|
The modes to run on (converted to a whitelist). |
Raises:
Type | Description |
---|---|
AssertionError
|
If invalid modes are detected, or if blacklisted modes and whitelisted modes are mixed. |
Source code in fastestimator\fastestimator\util\util.py
parse_string_to_python
¶
Convert a string into a python object.
x = fe.util.parse_string_to_python("5") # 5
x = fe.util.parse_string_to_python("[5, 4, 0.3]") # [5, 4, 0.3]
x = fe.util.parse_string_to_python("{'a':5, 'b':7}") # {'a':5, 'b':7}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
str
|
An input string. |
required |
Returns:
Type | Description |
---|---|
Any
|
A python object version of the input string. |
Source code in fastestimator\fastestimator\util\util.py
prettify_metric_name
¶
Add spaces to camel case words, then swap _ for space, and capitalize each word.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
str
|
A string to be formatted. |
required |
Returns:
Type | Description |
---|---|
str
|
The formatted version of 'metric'. |
Source code in fastestimator\fastestimator\util\util.py
show_image
¶
Plots a given image onto an axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis |
plt.Axes
|
The matplotlib axis to plot on, or None for a new plot. |
None
|
fig |
plt.Figure
|
A reference to the figure to plot on, or None if new plot. |
None
|
im |
Union[np.ndarray, Tensor]
|
The image to display (width X height). |
required |
title |
Optional[str]
|
A title for the image. |
None
|
color_map |
str
|
Which colormap to use for greyscale images. |
'inferno'
|
stack_depth |
int
|
Multiple images can be drawn onto the same axis. When stack depth is greater than zero, the |
0
|
Source code in fastestimator\fastestimator\util\util.py
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 |
|
strip_prefix
¶
Remove the given prefix
from the target
if it is present there.
x = fe.util.strip_prefix("astring.json", "ast") # "ring.json"
x = fe.util.strip_prefix("astring.json", "asa") # "astring.json"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Optional[str]
|
A string to be formatted. |
required |
prefix |
Optional[str]
|
A string to be removed from |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The formatted version of |
Source code in fastestimator\fastestimator\util\util.py
strip_suffix
¶
Remove the given suffix
from the target
if it is present there.
x = fe.util.strip_suffix("astring.json", ".json") # "astring"
x = fe.util.strip_suffix("astring.json", ".yson") # "astring.json"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Optional[str]
|
A string to be formatted. |
required |
suffix |
Optional[str]
|
A string to be removed from |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
The formatted version of |
Source code in fastestimator\fastestimator\util\util.py
to_list
¶
Convert data to a list. A single None value will be converted to the empty list.
x = fe.util.to_list(None) # []
x = fe.util.to_list([None]) # [None]
x = fe.util.to_list(7) # [7]
x = fe.util.to_list([7, 8]) # [7,8]
x = fe.util.to_list({7}) # [7]
x = fe.util.to_list((7)) # [7]
x = fe.util.to_list({'a': 7}) # [{'a': 7}]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
Input data, within or without a python container. |
required |
Returns:
Type | Description |
---|---|
List[Any]
|
The input |
Source code in fastestimator\fastestimator\util\util.py
to_set
¶
Convert data to a set. A single None value will be converted to the empty set.
x = fe.util.to_set(None) # {}
x = fe.util.to_set([None]) # {None}
x = fe.util.to_set(7) # {7}
x = fe.util.to_set([7, 8]) # {7,8}
x = fe.util.to_set({7}) # {7}
x = fe.util.to_set((7)) # {7}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
Input data, within or without a python container. The |
required |
Returns:
Type | Description |
---|---|
Set[Any]
|
The input |