Visitar URL original
Harden path-handling after pathlib migration by deruyter92 · Pull Request #3421 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
pathlib mkdir hardening + remaining str concat fixes
  • Loading branch information
deruyter92 committed Jul 21, 2026
commit 6859773b8559987aae87df49a2ab85c2b66145b0
2 changes: 1 addition & 1 deletion deeplabcut/generate_training_dataset/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def create(config: ProjectConfig | dict | Path | str) -> TrainingDatasetMetadata
f for f in trainset_path.iterdir() if re.match(r"Documentation_data-.+shuffle[0-9]+\.pickle", f.name)
]
else:
trainset_path.mkdir(parents=True)
trainset_path.mkdir(parents=True, exist_ok=True)
shuffle_docs = []

prefix = cfg["Task"] + cfg["date"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, proj_root, dataset_name, shuffle=1, modelprefix=""):
self.proj_root = proj_root

if modelprefix:
config_file = Path(self.proj_root) / (modelprefix + "_config.yaml")
config_file = Path(self.proj_root) / f"{modelprefix}_config.yaml"
else:
config_file = Path(self.proj_root) / "config.yaml"

Expand Down
8 changes: 4 additions & 4 deletions deeplabcut/modelzoo/video_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,15 @@ def video_inference_superanimal(

# we prepare the pseudo dataset in the same folder of the target video
pseudo_dataset_folder = video_path.with_name(f"pseudo_{video_path.stem}")
pseudo_dataset_folder.mkdir(exist_ok=True)
pseudo_dataset_folder.mkdir(exist_ok=True, parents=True)
model_folder = pseudo_dataset_folder / "checkpoints"
model_folder.mkdir(exist_ok=True)
model_folder.mkdir(exist_ok=True, parents=True)

image_folder = pseudo_dataset_folder / "images"
if image_folder.exists():
print(f"{image_folder} exists, skipping the frame extraction")
else:
image_folder.mkdir()
image_folder.mkdir(exist_ok=True, parents=True)
print(f"Video frames being extracted to {image_folder} for video adaptation.")
video_to_frames(video_path, pseudo_dataset_folder, cropping=cropping)

Expand All @@ -455,7 +455,7 @@ def video_inference_superanimal(
f"Delete the folder if you want to re-construct pseudo annotations"
)
else:
anno_folder.mkdir()
anno_folder.mkdir(exist_ok=True, parents=True)

if dest_folder is None:
pseudo_anno_dir = video_path.parent
Expand Down
2 changes: 1 addition & 1 deletion deeplabcut/pose_estimation_pytorch/apis/videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def _validate_destfolder(destfolder: str | None) -> None:
output_folder = Path(destfolder)
if not output_folder.exists():
print(f"Creating the output folder {output_folder}")
output_folder.mkdir(parents=True)
output_folder.mkdir(parents=True, exist_ok=True)

assert Path(output_folder).is_dir(), f"Output folder must be a directory: you passed '{output_folder}'"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class OpenVINOSession:
def __init__(self, cfg, device):
self.core = Core()
self.xml_path = cfg["init_weights"] + ".xml"
self.xml_path = str(Path(cfg["init_weights"]).with_suffix(".xml"))
self.device = device

# Convert a frozen graph to OpenVINO IR format
Expand All @@ -37,7 +37,7 @@ def __init__(self, cfg, device):
"--output_dir",
str(Path(cfg["init_weights"]).parent),
"--input_model",
cfg["init_weights"] + ".pb",
str(Path(cfg["init_weights"]).with_suffix(".pb")),
"--input_shape",
"[1, 747, 832, 3]",
"--extensions",
Expand Down
28 changes: 14 additions & 14 deletions deeplabcut/utils/auxfun_multianimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def convert2_maDLC(config: str | Path, userfeedback=True, forceindividual=None):

if askuser == "y" or askuser == "yes" or askuser == "Ja" or askuser == "ha": # multilanguage support :)
fn = folder / ("CollectedData_" + cfg["scorer"])
Data = pd.read_hdf(str(fn) + ".h5")
Data = pd.read_hdf(fn.with_suffix(".h5"))
conversioncode.guarantee_multiindex_rows(Data)
imindex = Data.index

Expand Down Expand Up @@ -341,13 +341,13 @@ def convert2_maDLC(config: str | Path, userfeedback=True, forceindividual=None):
dataFrame = pd.concat([dataFrame, frame], axis=1)

Data.to_hdf(
fn + "singleanimal.h5",
fn.with_name(fn.name + "singleanimal.h5"),
key="df_with_missing",
)
Data.to_csv(fn + "singleanimal.csv")
Data.to_csv(fn.with_name(fn.name + "singleanimal.csv"))

dataFrame.to_hdf(fn + ".h5", key="df_with_missing")
dataFrame.to_csv(fn + ".csv")
dataFrame.to_hdf(fn.with_suffix(".h5"), key="df_with_missing")
dataFrame.to_csv(fn.with_suffix(".csv"))


def convert_single2multiplelegacyAM(config, userfeedback=True, target=None):
Expand All @@ -370,7 +370,7 @@ def convert_single2multiplelegacyAM(config, userfeedback=True, target=None):

if askuser == "y" or askuser == "yes" or askuser == "Ja" or askuser == "ha": # multilanguage support :)
fn = folder / ("CollectedData_" + cfg["scorer"])
Data = pd.read_hdf(str(fn) + ".h5")
Data = pd.read_hdf(fn.with_suffix(".h5"))
conversioncode.guarantee_multiindex_rows(Data)
imindex = Data.index

Expand Down Expand Up @@ -413,16 +413,16 @@ def convert_single2multiplelegacyAM(config, userfeedback=True, target=None):
DataFrame = pd.concat([DataFrame, dataFrame], axis=1)

Data.to_hdf(
fn + "multianimal.h5",
fn.with_name(fn.name + "multianimal.h5"),
key="df_with_missing",
)
Data.to_csv(fn + "multianimal.csv")
Data.to_csv(fn.with_name(fn.name + "multianimal.csv"))

DataFrame.to_hdf(
fn + ".h5",
fn.with_suffix(".h5"),
key="df_with_missing",
)
DataFrame.to_csv(fn + ".csv")
DataFrame.to_csv(fn.with_suffix(".csv"))
elif target is None or target == "multi":
print("This is a single animal data set, converting to multi...", folder)
for prfxindex, prefix in enumerate(prefixes):
Expand Down Expand Up @@ -500,16 +500,16 @@ def convert_single2multiplelegacyAM(config, userfeedback=True, target=None):
DataFrame = pd.concat([DataFrame, dataFrame], axis=1)

Data.to_hdf(
fn + "singleanimal.h5",
fn.with_name(fn.name + "singleanimal.h5"),
key="df_with_missing",
)
Data.to_csv(fn + "singleanimal.csv")
Data.to_csv(fn.with_name(fn.name + "singleanimal.csv"))

DataFrame.to_hdf(
fn + ".h5",
fn.with_suffix(".h5"),
key="df_with_missing",
)
DataFrame.to_csv(fn + ".csv")
DataFrame.to_csv(fn.with_suffix(".csv"))


def form_default_inferencecfg(cfg):
Expand Down
2 changes: 1 addition & 1 deletion deeplabcut/utils/auxfun_videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def imread(image_path, mode="skimage"):
return img_as_ubyte(image)

elif mode == "cv2":
return cv2.imread(image_path, cv2.IMREAD_UNCHANGED)[..., ::-1] # ~10% faster than using cv2.cvtColor
return cv2.imread(str(image_path), cv2.IMREAD_UNCHANGED)[..., ::-1] # ~10% faster than using cv2.cvtColor


# https://docs.opencv.org/3.4.0/da/d54/group__imgproc__transform.html#ga5bb5a1fea74ea38e1a5445ca803ff121
Expand Down
6 changes: 3 additions & 3 deletions deeplabcut/utils/auxiliaryfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def attempt_to_make_folder(foldername, recursive=False):
return

if recursive:
foldername.mkdir(parents=True)
foldername.mkdir(parents=True, exist_ok=True)
else:
foldername.mkdir()
foldername.mkdir(exist_ok=True, parents=True)


def read_pickle(filename):
Expand Down Expand Up @@ -642,7 +642,7 @@ def find_analyzed_data(folder, videoname: str, scorer: str, filtered=False, trac
candidates = []
for file in grab_files_in_folder(folder, "h5"):
stem = Path(file).stem.replace("_filtered", "")
starts_by_scorer = file.startswith((videoname + scorer, videoname + scorer_legacy))
starts_by_scorer = Path(file).name.startswith((videoname + scorer, videoname + scorer_legacy))
if tracker:
matches_tracker = stem.endswith(tracker)
else:
Expand Down
2 changes: 1 addition & 1 deletion deeplabcut/utils/make_labeled_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ def _create_video_from_tracks(video, tracks, destfolder, output_name, pcutoff, s
from tqdm import tqdm

if not Path(destfolder).is_dir():
Path(destfolder).mkdir()
Path(destfolder).mkdir(exist_ok=True, parents=True)

vid = VideoWriter(video)
nframes = len(vid)
Expand Down
2 changes: 1 addition & 1 deletion deeplabcut/utils/pseudo_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def dlc3predictions_2_annotation_from_video(
assert len(predictions) == len(image_paths)
imageid2annotations = defaultdict(list)
for image_id, (prediction, image_path) in enumerate(zip(predictions, image_paths, strict=False)):
image_obj = cv2.imread(image_path)
image_obj = cv2.imread(str(image_path))
height, width, channels = image_obj.shape
imagename = Path(image_path).name
image = {
Expand Down