Utils.misc(通用函数) 模块¶
ppsci.utils.misc
¶
AverageMeter
¶
Computes and stores the average and current value Code was based on https://github.com/pytorch/examples/blob/master/imagenet/main.py
Source code in ppsci/utils/misc.py
reset()
¶
PrettyOrderedDict
¶
Bases: OrderedDict
The ordered dict which can be prettily printed.
Examples:
>>> import ppsci
>>> dic = ppsci.utils.misc.PrettyOrderedDict()
>>> dic.update({'a':1, 'b':2, 'c':3})
>>> print(dic)
('a', 1)('b', 2)('c', 3)
Source code in ppsci/utils/misc.py
Prettydefaultdict
¶
Bases: defaultdict
The default dict which can be prettily printed.
Examples:
>>> import ppsci
>>> dic = ppsci.utils.misc.Prettydefaultdict()
>>> dic.update({'a':1, 'b':2, 'c':3})
>>> print(dic)
('a', 1)('b', 2)('c', 3)
Source code in ppsci/utils/misc.py
RankZeroOnly
¶
A context manager that ensures the code inside it is only executed by the process
with rank zero. All rank will be synchronized by dist.barrier
in
distributed environment.
NOTE: Always used for time consuming code blocks, such as initialization of log writer, saving result to disk, etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank |
Optional[int]
|
The rank of the current process. If not provided,
it will be obtained from |
None
|
Examples:
>>> import paddle.distributed as dist
>>> with RankZeroOnly(dist.get_rank()) as is_master:
... if is_master:
... # code here which should only be executed in the master process
... pass
Source code in ppsci/utils/misc.py
__enter__()
¶
Enter the context and check if the current process is the master.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the current process is the master (rank zero), False otherwise. |
__init__(rank=None)
¶
Enter the context and check if the current process is the master.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank |
Optional[int]
|
The rank of the current process. If not provided,
it will be obtained from |
None
|
Source code in ppsci/utils/misc.py
Timer
¶
Bases: ContextDecorator
Count time cost for code block within context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of timer discriminate different code block. Defaults to "Timer". |
'Timer'
|
auto_print |
bool
|
Whether print time cost when exit context. Defaults to True. |
True
|
Examples:
>>> import paddle
>>> from ppsci.utils import misc
>>> with misc.Timer("test1", auto_print=False) as timer:
... w = sum(range(0, 10))
>>> print(f"time cost of 'sum(range(0, 10))' is {timer.interval:.2f}")
time cost of 'sum(range(0, 10))' is 0.00
>>> timer = misc.Timer("cost_of_func", auto_print=False)
>>> timer.start()
>>> def func():
... w = sum(range(0, 10))
>>> func()
>>> timer.end()
>>> print(f"time cost of 'cost_of_func' is {timer.interval:.2f}")
time cost of 'cost_of_func' is 0.00
Source code in ppsci/utils/misc.py
all_gather(tensor, concat=True, axis=0)
¶
Gather tensor from all devices, concatenate them along given axis if specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
Tensor
|
Tensor to be gathered from all GPUs. |
required |
concat |
bool
|
Whether to concatenate gathered Tensors. Defaults to True. |
True
|
axis |
int
|
Axis which concatenated along. Defaults to 0. |
0
|
Returns:
Type | Description |
---|---|
Union[Tensor, List[Tensor]]
|
Union[paddle.Tensor, List[paddle.Tensor]]: Gathered Tensors. |
Examples:
>>> import paddle
>>> import ppsci
>>> import paddle.distributed as dist
>>> dist.init_parallel_env()
>>> if dist.get_rank() == 0:
... data = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
... else:
... data = paddle.to_tensor([[7, 8, 9], [10, 11, 12]])
>>> result = ppsci.utils.misc.all_gather(data)
>>> print(result.numpy())
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Source code in ppsci/utils/misc.py
concat_dict_list(dict_list)
¶
Concatenate arrays in tuple of dicts at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_list |
Sequence[Dict[str, ndarray]]
|
Sequence of dicts. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: A dict with concatenated arrays for each key. |
Examples:
>>> import numpy as np
>>> import ppsci
>>> dic1 = {"x": np.array([[1., 2.], [3., 4.]]), "y": np.array([[5., 6.], [7., 8.]])}
>>> dic2 = {"x": np.array([[1., 2.], [3., 4.]]), "y": np.array([[5., 6.], [7., 8.]])}
>>> result = ppsci.utils.misc.concat_dict_list((dic1, dic2))
>>> print(result)
{'x': array([[1., 2.],
[3., 4.],
[1., 2.],
[3., 4.]]), 'y': array([[5., 6.],
[7., 8.],
[5., 6.],
[7., 8.]])}
Source code in ppsci/utils/misc.py
convert_to_array(dict_, keys)
¶
Concatenate arrays in axis -1 in order of given keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_ |
Dict[str, ndarray]
|
Dict contains arrays. |
required |
keys |
Tuple[str, ...]
|
Concatenate keys used in concatenation. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Concatenated array. |
Examples:
>>> import numpy as np
>>> import ppsci
>>> dic = {"x": np.array([[1., 2.], [3., 4.]]),
... "y": np.array([[5., 6.], [7., 8.]]),
... "z": np.array([[9., 10.], [11., 12.]])}
>>> result = ppsci.utils.misc.convert_to_array(dic, ("x", "z"))
>>> print(result)
[[ 1. 2. 9. 10.]
[ 3. 4. 11. 12.]]
Source code in ppsci/utils/misc.py
convert_to_dict(array, keys)
¶
Split given array into single channel array at axis -1 in order of given keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ndarray
|
Array to be split. |
required |
keys |
Tuple[str, ...]
|
Keys used in split. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: Split dict. |
Examples:
>>> import numpy as np
>>> import ppsci
>>> arr = np.array([[1., 2., 3.], [4., 5., 6.]])
>>> result = ppsci.utils.misc.convert_to_dict(arr, ("x", "y", "z"))
>>> print(arr.shape)
(2, 3)
>>> for k, v in result.items():
... print(k, v.shape)
x (2, 1)
y (2, 1)
z (2, 1)
Source code in ppsci/utils/misc.py
stack_dict_list(dict_list)
¶
Stack arrays in tuple of dicts at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dict_list |
Sequence[Dict[str, ndarray]]
|
Sequence of dicts. |
required |
Returns:
Type | Description |
---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: A dict with stacked arrays for each key. |
Examples:
>>> import numpy as np
>>> import ppsci
>>> dic1 = {"x": np.array([[1., 2.], [3., 4.]]), "y": np.array([[5., 6.], [7., 8.]])}
>>> dic2 = {"x": np.array([[1., 2.], [3., 4.]]), "y": np.array([[5., 6.], [7., 8.]])}
>>> result = ppsci.utils.misc.stack_dict_list((dic1, dic2))
>>> for k, v in result.items():
... print(k, v.shape)
x (2, 2, 2)
y (2, 2, 2)
Source code in ppsci/utils/misc.py
cartesian_product(*arrays)
¶
Cartesian product for input sequence of array(s).
Reference: https://stackoverflow.com/questions/11144513/cartesian-product-of-x-and-y-array-points-into-single-array-of-2d-points
Assume shapes of input arrays are: \((N_1,), (N_2,), (N_3,), ..., (N_M,)\), then the cartesian product result will be shape of \((N_1xN_2xN_3x...xN_M, M)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays |
ndarray
|
Input arrays. |
()
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Cartesian product result of shape \((N_1xN_2xN_3x...xN_M, M)\). |
Examples:
>>> t = np.array([1, 2])
>>> x = np.array([10, 20])
>>> y = np.array([100, 200])
>>> txy = cartesian_product(t, x, y)
>>> print(txy)
[[ 1 10 100]
[ 1 10 200]
[ 1 20 100]
[ 1 20 200]
[ 2 10 100]
[ 2 10 200]
[ 2 20 100]
[ 2 20 200]]
Source code in ppsci/utils/misc.py
combine_array_with_time(x, t)
¶
Combine given data x with time sequence t. Given x with shape (N, D) and t with shape (T, ), this function will repeat t_i for N times and will concat it with data x for each t_i in t, finally return the stacked result, which is of shape (N×T, D+1).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
ndarray
|
Points data with shape (N, D). |
required |
t |
Tuple[int, ...]
|
Time sequence with shape (T, ). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Combined data with shape of (N×T, D+1). |
Examples:
>>> import numpy as np
>>> import ppsci
>>> data_point = np.arange(10).reshape((2, 5))
>>> time = (1, 2, 3)
>>> result = ppsci.utils.misc.combine_array_with_time(data_point, time)
>>> print(result)
[[1. 0. 1. 2. 3. 4.]
[1. 5. 6. 7. 8. 9.]
[2. 0. 1. 2. 3. 4.]
[2. 5. 6. 7. 8. 9.]
[3. 0. 1. 2. 3. 4.]
[3. 5. 6. 7. 8. 9.]]
Source code in ppsci/utils/misc.py
set_random_seed(seed)
¶
Set numpy, random, paddle random_seed to given seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed |
int
|
Random seed. |
required |
run_on_eval_mode(func)
¶
A decorator automatically running given class method in eval mode and keep training state unchanged after function finished.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
Class method which is expected running in eval mode. |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
Decorated class method. |
Source code in ppsci/utils/misc.py
run_at_rank0(func)
¶
A decorator that allow given function run only at rank 0 to avoid multiple logs or other events. Usually effected in distributed environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
Given function. |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
Callable
|
Wrapped function which will only run at at rank 0, skipped at other rank. |
Examples:
>>> import paddle
>>> from ppsci.utils import misc
>>> @misc.run_at_rank0
... def func():
... print(f"now_rank is {paddle.distributed.get_rank()}")
>>> func()
now_rank is 0
Source code in ppsci/utils/misc.py
plot_curve(data, xlabel='X', ylabel='Y', output_dir='./output/', smooth_step=1, use_semilogy=False)
¶
Plotting curve.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, List]
|
Dict of all data, keys are curves' name. |
required |
xlabel |
str
|
Label of x-axis. Defaults to "X". |
'X'
|
ylabel |
str
|
Label of y-axis. Defaults to "Y". |
'Y'
|
output_dir |
str
|
Output directory of figure. Defaults to "./output/". |
'./output/'
|
smooth_step |
int
|
How many points are squeezed to one point to smooth the curve. Defaults to 1. |
1
|
use_semilogy |
bool
|
Whether to set non-uniform coordinates for the y-axis. Defaults to False. |
False
|