easy_vic_build.tools.decoractors

decorators - A Python module providing utility decorators.

This module provides utility decorators for enhancing function behavior.

Decorators:

  • clock_decorator: A decorator for measuring the execution time of a function and logging the result.

  • apply_along_axis_decorator: A decorator that applies a function along a specific axis of a numpy ndarray.

Usage:

  1. clock_decorator: Wrap your function with this decorator to measure and log its execution time. Example:

    @clock_decorator(print_arg_ret=True) def your_function(args):

    # Function implementation

  2. apply_along_axis_decorator: Wrap your function with this decorator to apply it along a specified axis of a numpy ndarray. Example:

    @apply_along_axis_decorator(axis=1) def your_function(arr):

    # Function implementation

Example:

@clock_decorator(print_arg_ret=True) def example_function(x, y):

# Perform some task return x + y

@apply_along_axis_decorator(axis=0) def example_function(arr):

# Apply a function along axis 0 of the array return np.sum(arr, axis=0)

Dependencies:

  • functools: For wrapping functions and ensuring they maintain their original signature.

  • numpy: For applying functions along specific axes of ndarrays.

Functions

apply_along_axis_decorator([axis])

A decorator to apply a function along a specific axis of a numpy ndarray.

clock_decorator([print_arg_ret])

A decorator to measure and log the execution time of a function.

processing_step(step_name, save_names, ...)

resample_missing_wrapper(resample_func)

Decorator to automatically handle missing values in the input grids for a resampling function.

resample_time_series_wrapper(resample_func)

Decorator to extend a spatial resampling function to handle time series data.

test_apply_along_axis_decorator()

test_clock_decorator()

test_func_apply_along_axis_decorator(data_array)

test_func_clock_decorator(func)

The actual decorator that wraps the target function.

easy_vic_build.tools.decoractors.clock_decorator(print_arg_ret=True)[source]

A decorator to measure and log the execution time of a function.

Parameters:

print_arg_ret (bool, optional) – If True, print the function’s arguments, execution time, and return value. If False, only print the function’s name and execution time. Default is True.

Returns:

A wrapped function that logs the execution time and optionally the arguments and return value.

Return type:

function

Notes

This decorator prints the time taken for a function to execute, as well as its arguments and return value. It uses the logger module for logging if necessary.

easy_vic_build.tools.decoractors.apply_along_axis_decorator(axis=0)[source]

A decorator to apply a function along a specific axis of a numpy ndarray.

Parameters:

axis (int, optional) – The axis along which to apply the function. Default is 0, which means the function will be applied along the rows (axis 0) of the ndarray.

Returns:

A wrapped function that applies the given function along the specified axis of the input ndarray.

Return type:

function

Notes

This decorator uses np.apply_along_axis to apply a function to slices of an ndarray along the specified axis. It is useful when you need to perform element-wise operations along a particular axis of a multidimensional array.

easy_vic_build.tools.decoractors.resample_time_series_wrapper(resample_func)[source]

Decorator to extend a spatial resampling function to handle time series data.

If searched_grids_data is 1D (single time point), the function is called directly. If it is 2D (time x space), the function is applied to each time slice independently, and an array of results is returned.

Parameters:

resample_func (callable) – A function that performs resampling on a single time point (1D spatial data).

Returns:

Wrapped function that can handle 1D or 2D time series inputs.

Return type:

callable

easy_vic_build.tools.decoractors.resample_missing_wrapper(resample_func)[source]

Decorator to automatically handle missing values in the input grids for a resampling function. Supports missing_value=None (np.nan) or a specified missing value.

easy_vic_build.tools.decoractors.test_func_clock_decorator(func)[source]

The actual decorator that wraps the target function.

Parameters:

func (function) – The function to be wrapped by the decorator.

Returns:

A wrapped version of func that logs the execution time.

Return type:

function

easy_vic_build.tools.decoractors.test_clock_decorator()[source]
easy_vic_build.tools.decoractors.test_func_apply_along_axis_decorator(data_array)[source]
easy_vic_build.tools.decoractors.test_apply_along_axis_decorator()[source]
easy_vic_build.tools.decoractors.processing_step(step_name: str, save_names: str | List[str], data_level: str, deps: List[str] | None = None)[source]