ssapy_toolkit.utils

Functions

ETA(idx, total_num, start_loop_time)

Estimates the remaining time to completion.

b2str(array_)

Decodes a list of byte strings into regular strings.

body_pos([body, t, coord, date, format])

Computes the position of a celestial body at a given time.

byte2str(byte_string)

Converts a byte string into a regular string.

close_to_any(a, floats, **kwargs)

Checks if any element in the array is close to any element in the list of floats.

contours_2d(points[, plot])

Computes and optionally plots the convex hull of a set of 2D points.

contours_3d(points_3d[, plot])

Computes and optionally plots the convex hull of a set of 3D points.

distance3d(x, y, z, xe, ye, ze)

Calculates the 3D Euclidean distance between two points.

divby0(n, d, Δ=None)

Performs division while handling division by zero.

eformat(f, prec, exp_digits)

Formats a floating-point number into scientific notation with the specified precision and exponent width.

elapsed_time(start_time)

Prints the elapsed time since a given start time.

extractNum(s)

Extracts the first integer from a string.

find_indices(lst, condition)

Finds indices in a list that satisfy a given condition.

find_local_extrema(arr)

Finds the local minima and maxima in a 1D array.

find_nearest(array[, value])

Finds the index and difference of the nearest element in an array to a given value.

flatten(t)

Flattens a list of lists into a single list.

graham_scan(points)

Computes the convex hull of a set of 2D points using Graham's scan algorithm.

isfloat(var_)

Checks if a variable is a float.

isint(var_)

Checks if a variable is an integer.

issorted(test_list)

Checks if a list is sorted.

isstr(var_)

Checks if a variable is a string.

kde(data_)

Computes the Kernel Density Estimate (KDE) of the given data.

nan_array([size])

Creates an array of NaNs with the specified size.

nby3shape(arr_)

Reshapes a 1D or 2D array to have 3 columns, preserving the structure.

pd_flatten(data[, factor])

Flattens the data and converts each element to a float, dividing by a factor.

pdstr_to_arrays(df)

Converts all string representations of arrays in a dataframe to numpy arrays.

remove_np_nans(numpy_array)

Removes NaN values from a numpy array.

remove_zeros(data[, axis])

Removes rows or columns where all elements are zeros.

size(a[, axis])

Returns the size of an array or the length of a specified axis.

sortbylist(X, Y)

Sorts one list based on the sorting order of another list.

sortbynum(files)

Sorts a list of file paths based on numbers embedded in the filenames.

str_to_array(s)

Converts a string of comma-separated values into a numpy array of floats.

suppress_stdout()

Context manager to suppress stdout.

ssapy_toolkit.utils.ETA(idx: int, total_num: int, start_loop_time: float) None[source]

Estimates the remaining time to completion.

Parameters:

idx (int): The current iteration index. total_num (int): The total number of iterations. start_loop_time (float): The start time of the loop, used to calculate elapsed time.

Returns:

None

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.b2str(array_: ndarray) list[source]

Decodes a list of byte strings into regular strings.

Parameters:

array_ (np.ndarray): Array of byte strings to decode.

Returns:

list: list of decoded strings.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.body_pos(body: str = 'earth', t: float = None, coord: str = 'icrs', date: float = 2451545.0, format: str = 'jd') ndarray[source]

Computes the position of a celestial body at a given time.

Parameters:

body (str): Name of the celestial body (default is ‘earth’). t (float): Time to evaluate (default is None, uses date). coord (str): Coordinate system (default is ‘icrs’). date (float): Julian date if t is not provided (default is 2451545.0). format (str): Time format (default is ‘jd’).

Returns:

np.ndarray: Position of the body in the specified coordinate system.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.byte2str(byte_string: bytes) str[source]

Converts a byte string into a regular string.

Parameters:

byte_string (bytes): The byte string to convert.

Returns:

str: The decoded string.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.close_to_any(a: ndarray, floats: ndarray, **kwargs) bool[source]

Checks if any element in the array is close to any element in the list of floats.

Parameters:

a (np.ndarray): The array to check. floats (np.ndarray): The list of floats to compare to.

Returns:

bool: True if any element in a is close to any in floats, False otherwise.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.contours_2d(points: ndarray, plot: bool = False) ndarray[source]

Computes and optionally plots the convex hull of a set of 2D points.

Parameters:

points (np.ndarray): Array of points, where each point is an (x, y) coordinate. plot (bool): Whether to plot the convex hull (default is False).

Returns:

np.ndarray: Array of points forming the convex hull.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.contours_3d(points_3d: ndarray, plot: bool = False) ConvexHull[source]

Computes and optionally plots the convex hull of a set of 3D points.

Parameters:

points_3d (np.ndarray): Array of 3D points, where each point is an (x, y, z) coordinate. plot (bool): Whether to plot the convex hull (default is False).

Returns:

scipy.spatial.ConvexHull: The convex hull object representing the 3D hull.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.distance3d(x: float, y: float, z: float, xe: float, ye: float, ze: float) float[source]

Calculates the 3D Euclidean distance between two points.

Parameters:

x, y, z (float): Coordinates of the first point. xe, ye, ze (float): Coordinates of the second point.

Returns:

float: The Euclidean distance between the two points.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.divby0(n: float, d: float, Δ: float = nan) float[source]

Performs division while handling division by zero.

Parameters:

n (float): The numerator. d (float): The denominator. Δ (float): The value to return in case of division by zero (default is NaN).

Returns:

float: The result of division, or the default value in case of zero denominator.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.eformat(f: float, prec: int, exp_digits: int) str[source]

Formats a floating-point number into scientific notation with the specified precision and exponent width.

Parameters:

f (float): The number to format. prec (int): The number of digits to show after the decimal point. exp_digits (int): The number of digits to show in the exponent part.

Returns:

str: The formatted string in scientific notation.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.elapsed_time(start_time: float) None[source]

Prints the elapsed time since a given start time.

Parameters:

start_time (float): The time at which the process started.

Returns:

None

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.extractNum(s: str) int[source]

Extracts the first integer from a string.

Parameters:

s (str): The string to extract the integer from.

Returns:

int: The first integer found in the string.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.find_indices(lst: list, condition: callable) list[source]

Finds indices in a list that satisfy a given condition.

Parameters:

lst (list): list to search through. condition (callable): A function that takes an element and returns True or False.

Returns:

list: list of indices where the condition is true.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.find_local_extrema(arr: ndarray) list[source]

Finds the local minima and maxima in a 1D array.

Parameters:

arr (np.ndarray): The input array.

Returns:

tuple: list of indices of local minima and maxima.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.find_nearest(array: ndarray, value: float = 0) tuple[source]

Finds the index and difference of the nearest element in an array to a given value.

Parameters:

array (np.ndarray): The input array. value (float): The value to find the nearest element to (default is 0).

Returns:

tuple: Index of the nearest element and the difference from the value.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.flatten(t: list) list[source]

Flattens a list of lists into a single list.

Parameters:

t (list): The input list of lists.

Returns:

list: A flattened list containing all the elements.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.graham_scan(points: ndarray) ndarray[source]

Computes the convex hull of a set of 2D points using Graham’s scan algorithm.

Parameters:

points (np.ndarray): Array of points, where each point is an (x, y) coordinate.

Returns:

np.ndarray: Array of points forming the convex hull.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.isfloat(var_: object) bool[source]

Checks if a variable is a float.

Parameters:

var_ (object): The variable to check.

Returns:

bool: True if the variable is a float, False otherwise.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.isint(var_: object) bool[source]

Checks if a variable is an integer.

Parameters:

var_ (object): The variable to check.

Returns:

bool: True if the variable is an integer, False otherwise.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.issorted(test_list: list) bool[source]

Checks if a list is sorted.

Parameters:

test_list (list): The list to check.

Returns:

bool: True if the list is sorted, False otherwise.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.isstr(var_: object) bool[source]

Checks if a variable is a string.

Parameters:

var_ (object): The variable to check.

Returns:

bool: True if the variable is a string, False otherwise.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.kde(data_: ndarray) gaussian_kde[source]

Computes the Kernel Density Estimate (KDE) of the given data.

Parameters:

data_ (np.ndarray): Input data to estimate the density of.

Returns:

stats.gaussian_kde: KDE object.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.nan_array(size: int = 1) ndarray[source]

Creates an array of NaNs with the specified size.

Parameters:

size (int): The size of the array (default is 1).

Returns:

np.ndarray: An array filled with NaNs.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.nby3shape(arr_: ndarray) ndarray[source]

Reshapes a 1D or 2D array to have 3 columns, preserving the structure.

Parameters:

arr_ (np.ndarray): Input array to reshape.

Returns:

np.ndarray: Reshaped array with 3 columns.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.pd_flatten(data: list, factor: float = 1.0) list[source]

Flattens the data and converts each element to a float, dividing by a factor.

Parameters:

data (list): list of string elements to be converted. factor (float): Factor to divide each number (default is 1.0).

Returns:

list: list of converted floats.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.pdstr_to_arrays(df: DataFrame) ndarray[source]

Converts all string representations of arrays in a dataframe to numpy arrays.

Parameters:

df (pd.DataFrame): DataFrame containing string representations of arrays.

Returns:

np.ndarray: Numpy array of the converted arrays.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.remove_np_nans(numpy_array: ndarray) ndarray[source]

Removes NaN values from a numpy array.

Parameters:

numpy_array (np.ndarray): Input array with potential NaN values.

Returns:

np.ndarray: Array with NaN values removed.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.remove_zeros(data: ndarray, axis: int = 0) ndarray[source]

Removes rows or columns where all elements are zeros.

Parameters:

data (np.ndarray): Input array. axis (int): Axis along which to remove zeros (default is 1, i.e., columns).

Returns:

np.ndarray: Array with rows/columns removed where all elements were zero.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.size(a: ndarray, axis=None) int[source]

Returns the size of an array or the length of a specified axis.

Parameters:

a (np.ndarray): Input array. axis (int): Axis to check the size along (default is None).

Returns:

int: Size of the array or length of the specified axis.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.sortbylist(X: list, Y: list) list[source]

Sorts one list based on the sorting order of another list.

Parameters:

X (list): The list to sort. Y (list): The list used to determine the sorting order.

Returns:

list: Sorted version of X according to the order in Y.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.sortbynum(files: list) list[source]

Sorts a list of file paths based on numbers embedded in the filenames.

Parameters:

files (list): list of file paths to sort.

Returns:

list: Sorted list of file paths.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.str_to_array(s: str) ndarray[source]

Converts a string of comma-separated values into a numpy array of floats.

Parameters:

s (str): Input string.

Returns:

np.ndarray: Array of floats converted from the input string.

Author: Travis Yeager (yeager7@llnl.gov)

ssapy_toolkit.utils.suppress_stdout()[source]

Context manager to suppress stdout.

Redirects output to devnull during the context. Author: Travis Yeager (yeager7@llnl.gov)