Module deeporigin.src.utilities.utils
Functions
def calculate_box_dimensions(min_coords, max_coords)
-
Expand source code
def calculate_box_dimensions(min_coords, max_coords): """Calculates the dimensions of a box given its minimum and maximum coordinates. Args: min_coords (list or array): List containing 3 elements representing minimum x, y, z coordinates. max_coords (list or array): List containing 3 elements representing maximum x, y, z coordinates. Returns: list: A list of 3 float values representing [length, width, height] of the box. Raises: ValueError: If min_coords or max_coords don't have exactly 3 elements. Example: >>> min_coords = [0, 0, 0] >>> max_coords = [2, 3, 4] >>> calculate_box_dimensions(min_coords, max_coords) [2.0, 3.0, 4.0] """ # Ensure both inputs are lists or arrays of length 3 if len(min_coords) != 3 or len(max_coords) != 3: raise ValueError("min_coords and max_coords must each have exactly 3 elements.") # Calculate the dimensions length = max_coords[0] - min_coords[0] width = max_coords[1] - min_coords[1] height = max_coords[2] - min_coords[2] return [float(length), float(width), float(height)]
Calculates the dimensions of a box given its minimum and maximum coordinates.
Args
min_coords
:list
orarray
- List containing 3 elements representing minimum x, y, z coordinates.
max_coords
:list
orarray
- List containing 3 elements representing maximum x, y, z coordinates.
Returns
list
- A list of 3 float values representing [length, width, height] of the box.
Raises
ValueError
- If min_coords or max_coords don't have exactly 3 elements.
Example
>>> min_coords = [0, 0, 0] >>> max_coords = [2, 3, 4] >>> calculate_box_dimensions(min_coords, max_coords) [2.0, 3.0, 4.0]
def calculate_box_min_max(box_center, box_dimensions)
-
Expand source code
def calculate_box_min_max(box_center, box_dimensions): """Calculate minimum and maximum corners of a box given its center and dimensions. Takes a box's center point and dimensions and returns the minimum and maximum corners that define the box's boundaries in 3D space. Args: box_center (list): List of coordinates [x, y, z] for the center of the box box_dimensions (list): List of dimensions [width, height, depth] of the box Returns: tuple: Two lists containing the minimum and maximum corner coordinates - min_corner (list): Coordinates of the minimum corner [x_min, y_min, z_min] - max_corner (list): Coordinates of the maximum corner [x_max, y_max, z_max] Example: >>> center = [0, 0, 0] >>> dimensions = [2, 2, 2] >>> min_corner, max_corner = calculate_box_min_max(center, dimensions) >>> print(min_corner, max_corner) [-1.0, -1.0, -1.0] [1.0, 1.0, 1.0] """ half_dims = [dim / 2 for dim in box_dimensions] min_corner = [center - half for center, half in zip(box_center, half_dims)] max_corner = [center + half for center, half in zip(box_center, half_dims)] return min_corner, max_corner
Calculate minimum and maximum corners of a box given its center and dimensions.
Takes a box's center point and dimensions and returns the minimum and maximum corners that define the box's boundaries in 3D space.
Args
box_center
:list
- List of coordinates [x, y, z] for the center of the box
box_dimensions
:list
- List of dimensions [width, height, depth] of the box
Returns
tuple
- Two lists containing the minimum and maximum corner coordinates - min_corner (list): Coordinates of the minimum corner [x_min, y_min, z_min] - max_corner (list): Coordinates of the maximum corner [x_max, y_max, z_max]
Example
>>> center = [0, 0, 0] >>> dimensions = [2, 2, 2] >>> min_corner, max_corner = calculate_box_min_max(center, dimensions) >>> print(min_corner, max_corner) [-1.0, -1.0, -1.0] [1.0, 1.0, 1.0]
def chunker(iterable, size)
-
Expand source code
def chunker(iterable, size): """ Split an iterable into chunks of specified size. This function takes an iterable and yields lists of elements with the specified chunk size. The last chunk may be smaller if the iterable's length is not evenly divisible by the chunk size. Args: iterable: Any iterable object to be chunked. size (int): The size of each chunk. Yields: list: A list containing elements from the iterable, with length up to 'size'. Examples: >>> list(chunker([1, 2, 3, 4, 5], 2)) [[1, 2], [3, 4], [5]] >>> list(chunker('abcde', 3)) [['a', 'b', 'c'], ['d', 'e']] """ iterator = iter(iterable) for first in iterator: yield [first] + list(islice(iterator, size - 1))
Split an iterable into chunks of specified size.
This function takes an iterable and yields lists of elements with the specified chunk size. The last chunk may be smaller if the iterable's length is not evenly divisible by the chunk size.
Args
iterable
- Any iterable object to be chunked.
size
:int
- The size of each chunk.
Yields
list
- A list containing elements from the iterable, with length up to 'size'.
Examples
>>> list(chunker([1, 2, 3, 4, 5], 2)) [[1, 2], [3, 4], [5]] >>> list(chunker('abcde', 3)) [['a', 'b', 'c'], ['d', 'e']]
def move_file_with_extension(file_path, extension)
-
Expand source code
def move_file_with_extension(file_path, extension): """Rename a file by changing its extension, handling naming conflicts with numbered suffixes. This function renames a file by changing its extension. If a file with the target name already exists, the existing file is moved to a new name with a numbered suffix (e.g., '_#1'). Args: file_path (str): The path to the source file that needs to be renamed extension (str): The new extension to apply to the file (without the dot) Example: If 'test.txt' exists and you call: >>> move_file_with_extension('test.csv', 'txt') The existing 'test.txt' will be renamed to 'test_#1.txt' Notes: - The function assumes the parent directory exists and is writable - The function preserves the original filename's base name - Numbering starts at 1 and increments based on existing numbered files """ dir_path = os.path.dirname(file_path) file_name = os.path.basename(file_path) file_base_name = os.path.splitext(file_name)[0] target_file_path = os.path.join(dir_path, f"{file_base_name}.{extension}") if os.path.isfile(target_file_path): existing_files = [f for f in os.listdir(dir_path) if f.startswith(f"{file_base_name}_#") and f.endswith(f".{extension}")] counter = len(existing_files) + 1 new_file_path = os.path.join(dir_path, f"{file_base_name}_#{counter}.{extension}") shutil.move(target_file_path, new_file_path)
Rename a file by changing its extension, handling naming conflicts with numbered suffixes.
This function renames a file by changing its extension. If a file with the target name already exists, the existing file is moved to a new name with a numbered suffix (e.g., '_#1').
Args
file_path
:str
- The path to the source file that needs to be renamed
extension
:str
- The new extension to apply to the file (without the dot)
Example
If 'test.txt' exists and you call:
>>> move_file_with_extension('test.csv', 'txt') The existing 'test.txt' will be renamed to 'test_#1.txt'
Notes
- The function assumes the parent directory exists and is writable
- The function preserves the original filename's base name
- Numbering starts at 1 and increments based on existing numbered files
def remove_file(file_path)
-
Expand source code
def remove_file(file_path): """ Remove a file from the specified file path. Args: file_path (str): Path to the file that needs to be removed. Returns: None Raises: OSError: If file deletion fails due to permissions or other OS-level errors. Example: >>> remove_file('/path/to/file.txt') """ if os.path.isfile(file_path): os.remove(file_path)
Remove a file from the specified file path.
Args
file_path
:str
- Path to the file that needs to be removed.
Returns
None
Raises
OSError
- If file deletion fails due to permissions or other OS-level errors.
Example
>>> remove_file('/path/to/file.txt')