base_util
DefaultKeyDict
¶
Bases: Dict[KT, VT]
Like collections.defaultdict but it passes the key argument to the default function.
This class is intentionally not @traceable.
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/base_util.py
FEID
¶
An int wrapper class that can change how it's values are printed.
This class is intentionally not @traceable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
int
|
An integer id to be wrapped. |
required |
Source code in fastestimator/fastestimator/util/base_util.py
set_translation_dict
classmethod
¶
Provide a lookup table to be invoked during value printing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapping |
Dict[int, Any]
|
A mapping of id: printable id. |
required |
Source code in fastestimator/fastestimator/util/base_util.py
FigureFE
¶
Bases: Figure
Source code in fastestimator/fastestimator/util/base_util.py
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
show
¶
A function which will save or display plotly figures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save_path |
Optional[str]
|
The path where the figure should be saved, or None to display the figure to the screen. |
None
|
verbose |
bool
|
Whether to print out the save location. |
True
|
scale |
int
|
A scaling factor to apply when exporting to static images (to increase resolution). |
1
|
interactive |
bool
|
Whether the figure should be interactive or static. This is only applicable when save_path is None and when running inside a jupyter notebook. The advantage is that the file size of the resulting jupyter notebook can be dramatically reduced. |
True
|
Source code in fastestimator/fastestimator/util/base_util.py
Flag
¶
A mutable wrapper around a boolean.
This class is intentionally not @traceable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
bool
|
The initial value for the Flag. |
False
|
Source code in fastestimator/fastestimator/util/base_util.py
LogSplicer
¶
A class to send stdout information into a file before passing it along to the normal stdout.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_path |
str
|
The path/filename into which to append the current stdout. |
required |
Source code in fastestimator/fastestimator/util/base_util.py
NonContext
¶
Bases: object
A class which is used to make nothing unusual happen.
This class is intentionally not @traceable.
Source code in fastestimator/fastestimator/util/base_util.py
IfElse
¶
This is a helper function to make code look cleaner while using config files.
One mock example where this might be useful:
from fastestimator.util import IfElse
extra_augs = True
...
fe.Pipeline(...,
ops=[...,
IfElse(extra_augs, RandomJitter(image_in="x", image_out="x"))
]
)
If instantiating the objects in each branch is expensive, you may want to defer that until later by converting your constructors to lambda functions and then invoking the functions after the return from the IfElse:
from fastestimator.util import IfElse
use_lots_of_data = True
...
train_data = IfElse(use_lots_of_data, lambda: BigInMemoryDataset(), lambda: SmallInMemoryDataset())()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cond |
bool
|
Which branch to return. |
required |
y |
KT
|
The return value when |
required |
n |
VT
|
The return value when |
None
|
Returns:
Type | Description |
---|---|
Union[KT, VT]
|
The value according to the given cond. |
Source code in fastestimator/fastestimator/util/base_util.py
check_ds_id
¶
A function to check whether ds_ids inputs are correct inputs.
ds_ids should either be defined through whitelist, like {"ds1", "ds2"} or blacklist, like {"!ds1", "!ds2"}.
m = fe.util.parse_ds_id({"ds1"}) # {"ds1"}
m = fe.util.parse_ds_id({"!ds1"}) # {"!ds1"}
m = fe.util.parse_ds_id({"ds1", "ds2"}) # {"ds1", "ds2"}
m = fe.util.parse_ds_id({"!ds1", "!ds2"}) # {"!ds1", "!ds2"}
m = fe.util.parse_ds_id({"!ds1", "ds2"}) # Raises Assertion
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds_ids |
Set[str]
|
The desired ds_id to run on (possibly containing blacklisted ds_ids). |
required |
Returns:
Type | Description |
---|---|
Set[str]
|
The ds_ids to run or to avoid. |
Raises:
Type | Description |
---|---|
AssertionError
|
if blacklisted modes and whitelisted modes are mixed. |
Source code in fastestimator/fastestimator/util/base_util.py
filter_nones
¶
Remove any None values from the input collection.
For dictionaries this removes keys whose values are None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ins |
Collection
|
The collection to be filtered. |
required |
Returns:
Type | Description |
---|---|
Collection
|
The filtered collection. |
Source code in fastestimator/fastestimator/util/base_util.py
get_colors
¶
Get a list of colors to use in plotting.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_colors |
int
|
How many colors to return. |
required |
alpha |
float
|
What opacity value to use (0 to 1). |
1.0
|
as_numbers |
bool
|
Whether to return the values as a list of numbers [r,g,b,a] or as a string |
False
|
Returns:
Type | Description |
---|---|
Union[List[str], List[Tuple[float, float, float, float]]]
|
A list of rgba string colors. |
Source code in fastestimator/fastestimator/util/base_util.py
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/base_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"
For container to look into its element's type, its type needs to be either list or tuple, and the return string will be List[...]. All container elements need to have the same data type because it will only check its first element.
x = fe.util.get_type({"a":1, "b":2}) # "dict"
x = fe.util.get_type([1, "a"]) # "List[int]"
x = fe.util.get_type([[[1]]]) # "List[List[List[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/base_util.py
in_notebook
¶
Determine whether the code is running inside a jupyter notebook
Returns:
Type | Description |
---|---|
bool
|
True iff the code is executing inside a Jupyter notebook |
Source code in fastestimator/fastestimator/util/base_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/base_util.py
list_files
¶
Get the paths of all files in a particular root directory subject to a particular file extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_dir |
str
|
The path to the directory containing data. |
required |
file_extension |
Optional[str]
|
If provided then only files ending with the file_extension will be included. |
None
|
recursive_search |
bool
|
Whether to search within subdirectories for files. |
True
|
Returns:
Type | Description |
---|---|
List[str]
|
A list of file paths found within the directory. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the provided path isn't a directory. |
ValueError
|
If the directory has an invalid structure. |
Source code in fastestimator/fastestimator/util/base_util.py
param_to_range
¶
Convert a single int or float value to a tuple signifying a range.
x = fe.util.param_to_tuple(7) # (-7, 7)
x = fe.util.param_to_tuple([7, 8]) # (7,8))
x = fe.util.param_to_tuple((3.1, 4.3)) # (3.1, 4.3)
x = fe.util.to_set((-3.2)) # (-3.2, 3.2)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[int, float, Tuple[int, int], Tuple[float, float]]
|
Input data. |
required |
Returns:
Type | Description |
---|---|
Union[Tuple[int, int], Tuple[float, float]]
|
The input |
Source code in fastestimator/fastestimator/util/base_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/base_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/base_util.py
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/base_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/base_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/base_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) # set()
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 |
Source code in fastestimator/fastestimator/util/base_util.py
warn
¶
Print a yellow warning message to catch the users' attention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
str
|
The warning to print. |
required |