# flake8: noqa: E501
import numpy as np
import os
import re
import shutil
from glob import glob
from ..utils import sortbynum
[docs]
def file_exists(filename: str) -> bool:
"""
Check if a file with the given name exists (including its extensions).
"""
name, _ = os.path.splitext(filename)
return bool(glob(f"{name}.*"))
[docs]
def exists(pathname: str) -> bool:
"""
Check if a given path is either a file or a directory.
"""
return os.path.isdir(pathname) or os.path.isfile(pathname)
[docs]
def mkdir(pathname: str) -> None:
"""
Create a directory if it does not already exist.
"""
if not exists(pathname):
try:
os.makedirs(pathname, exist_ok=True)
print(f"Directory '{pathname}' created.")
except OSError as e:
print(f"Error creating directory {pathname}: {e}")
[docs]
def mvdir(source_: str, destination_: str) -> None:
"""
Move a directory from the source path to the destination path.
"""
if not exists(destination_):
print(f"Moving {source_} to {destination_}")
shutil.move(source_, destination_)
else:
print(f"Destination path {destination_} already exists.")
[docs]
def rmdir(source_: str) -> None:
"""
Remove a directory and its contents.
"""
if not exists(source_):
print(f'{source_} does not exist, no delete.')
else:
try:
shutil.rmtree(source_)
print(f'Deleted {source_}')
except OSError as e:
print(f"Error deleting {source_}: {e}")
[docs]
def rmfile(pathname: str) -> None:
"""
Remove a file if it exists.
"""
if exists(pathname):
os.remove(pathname)
print(f"File: '{pathname}' deleted.")
[docs]
def listdir(
dir_path: str = '*',
files_only: bool = False,
exclude: str = None,
do_sort: bool = True,
) -> list:
"""
List files in a directory, with options to filter, exclude, and sort.
"""
if '*' not in dir_path:
dir_path = os.path.join(dir_path, '*')
expanded_paths = glob(dir_path)
if files_only:
files = [f for f in expanded_paths if os.path.isfile(f)]
print(f'{len(files)} files in {dir_path}')
else:
files = expanded_paths
print(f'{len(files)} files in {dir_path}')
if exclude:
files = [file for file in files if exclude not in os.path.basename(file)]
if do_sort:
return sortbynum(files)
return files
[docs]
def get_image_paths(folder_path: str, sort_by_number: bool = True) -> list:
"""
Returns a list of full paths for all image files in the specified folder.
"""
image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp')
image_paths: list = []
if not os.path.exists(folder_path):
raise ValueError(f"The folder path '{folder_path}' does not exist")
for root, _, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(image_extensions):
image_paths.append(os.path.join(root, file))
if sort_by_number:
def extract_last_number(path: str):
filename = os.path.basename(path)
numbers = re.findall(r'\d+', filename)
if not numbers:
return (float('inf'), path)
dir_path = os.path.dirname(path)
dir_files = [f for f in image_paths if os.path.dirname(f) == dir_path]
all_numbers = [re.findall(r'\d+', os.path.basename(f)) for f in dir_files]
max_numbers = max((len(nums) for nums in all_numbers), default=0)
if max_numbers == 0:
return (float('inf'), path)
all_numbers = [nums + [None] * (max_numbers - len(nums)) for nums in all_numbers]
last_varying_pos = -1
for pos in range(max_numbers - 1, -1, -1):
values = {nums[pos] for nums in all_numbers if nums[pos] is not None}
if len(values) > 1:
last_varying_pos = pos
break
sort_pos = last_varying_pos if last_varying_pos >= 0 else len(numbers) - 1
sort_value = int(numbers[sort_pos]) if sort_pos < len(numbers) else float('inf')
return (sort_value, path)
image_paths.sort(key=extract_last_number)
return image_paths
[docs]
def pd_flatten(data: list, factor: float = 1) -> list:
"""
Flatten a list of data by splitting string elements like '[1,2,3]'
and converting them into floats. Divides each float by `factor`.
"""
tmp: list = []
for x in data:
try:
tmp.extend(x[1:-1].split(','))
except TypeError:
tmp.append(x)
return [float(x) / factor for x in tmp]
[docs]
def str_to_array(s: str) -> np.ndarray:
"""
Convert a string representation of a list (e.g., '[1, 2, 3]') into a NumPy array of floats.
"""
s = s.replace('[', '').replace(']', '')
parts = [p.strip() for p in s.split(',') if p.strip() != '']
return np.array([float(x) for x in parts])
[docs]
def pdstr_to_arrays(df) -> np.ndarray:
"""
Apply `str_to_array` to each element of a DataFrame and convert it to a NumPy array of arrays.
"""
return df.apply(str_to_array).to_numpy()
[docs]
def allfiles(dirName: str = os.getcwd()) -> list:
"""
Get a list of all files in the directory tree starting at the given path.
"""
listOfFiles: list = []
for (dirpath, dirnames, filenames) in os.walk(dirName):
listOfFiles += [os.path.join(dirpath, file) for file in filenames]
return listOfFiles