Skip to content
12 changes: 10 additions & 2 deletions src/osekit/core/base_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,28 @@ def folder(self, folder: Path) -> None:
"""
self._folder = folder

def move_files(self, folder: Path) -> None:
def move_files(self, folder: Path, *, keep_relative_structure: bool = True) -> None:
"""Move the dataset files to the destination folder.

Parameters
----------
folder: Path
Destination folder in which the dataset files will be moved.
keep_relative_structure: bool
If True, the relative path from the dataset to the file will be preserved.
If False, the files will be moved to ``folder`` regardless of their original
relative path to the dataset folder.

"""
for file in tqdm(
self.files,
disable=os.getenv("DISABLE_TQDM", "False").lower() in ("true", "1", "t"),
):
file.move(folder)
if not keep_relative_structure:
file.move(folder)
continue
relative_folder = folder / file.path.relative_to(self.folder).parent
file.move(relative_folder)
self._folder = folder

@property
Expand Down
5 changes: 4 additions & 1 deletion src/osekit/public/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,10 @@ def _sort_dataset(self, dataset: type[DatasetChild]) -> None:
self._sort_spectro_dataset(dataset)

def _sort_audio_dataset(self, dataset: AudioDataset) -> None:
dataset.move_files(self._get_audio_dataset_subpath(dataset))
dataset.move_files(
self._get_audio_dataset_subpath(dataset),
keep_relative_structure=False,
)

def _sort_spectro_dataset(self, dataset: SpectroDataset | LTASDataset) -> None:
raise NotImplementedError
Expand Down
3 changes: 2 additions & 1 deletion src/osekit/utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def move_tree(
continue
file_destination = destination / file.parent.relative_to(source)
file_destination.mkdir(parents=True, exist_ok=True)
shutil.move(file, file_destination / file.name)
if not (file_destination / file.name).exists():
shutil.move(file, file_destination)
if not any(destination.iterdir()):
destination.rmdir()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_core_api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def test_dataset_move(

# Folder should be changed when dataset is moved
new_destination = tmp_path / "new_destination"
dummy_dataset.move_files(new_destination)
dummy_dataset.move_files(new_destination, keep_relative_structure=False)

assert new_destination.exists()
assert new_destination.is_dir()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,13 @@ def test_rename_transform(

names = (first_name, second_name, second_name) # Tests both renaming and same name
for old, new in itertools.pairwise(names):
files = {}
for dataset in project.get_output_by_transform_name(old):
files |= {
file.path.name: file.path.relative_to(dataset.folder)
for file in dataset.files
}

project.rename_transform_with_outputs(old, new)

if old != new:
Expand All @@ -1511,6 +1518,10 @@ def test_rename_transform(
== 2
)

for dataset in project.get_output_by_transform_name(new):
for file in dataset.files:
assert file.path.relative_to(dataset.folder) == files[file.path.name]

# RENAME ERRORS
with pytest.raises(ValueError, match=r"You can't rename the original dataset."):
project.rename_transform_with_outputs(
Expand Down