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
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
Suppressor
¶
Bases: object
A class which can be used to silence output of function calls.
This class is intentionally not @traceable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_pyprint |
bool
|
Whether to allow python printing to occur still within this scope (and therefore only silence printing from non-python sources like c). |
False
|
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
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 |
---|---|
List[Union[str, 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 becuase 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 |