Skip to content

Commit

Permalink
added move_project_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvanderschelling committed Jan 31, 2025
1 parent b6f7d3a commit 3c13c95
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/f3dasm/_src/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Standard
import pickle
import shutil
from pathlib import Path
from typing import Any, Callable, Mapping, Optional, Type

Expand Down Expand Up @@ -448,3 +449,33 @@ def load_object(project_dir: Path, path: str | Path,

# Store the object and return the storage location
return load_function(_path)


def copy_object(object_path: Path,
old_project_dir: Path, new_project_dir: Path) -> str:

old_location = old_project_dir / EXPERIMENTDATA_SUBFOLDER / object_path
new_location = new_project_dir / EXPERIMENTDATA_SUBFOLDER / object_path

# Check if the storage parent folder exists
new_location.parent.mkdir(parents=True, exist_ok=True)

# Check if we do not overwrite an object at new_location
if new_location.exists():

stem, suffix = object_path.stem, object_path.suffix
while (new_project_dir / EXPERIMENTDATA_SUBFOLDER /
object_path.parent / f"{stem}{suffix}").exists():
try:
stem = str(int(stem) + 1) # Increment stem as integer
except ValueError:
raise ValueError((
f"Filename {object_path.name} cannot be converted "
f"to an integer.")
)

object_path = object_path.parent / f"{stem}{suffix}"
new_location = new_project_dir / EXPERIMENTDATA_SUBFOLDER / object_path

shutil.copy2(old_location, new_location)
return str(object_path)
9 changes: 9 additions & 0 deletions src/f3dasm/_src/experimentdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,15 @@ def set_project_dir(self, project_dir: Path | str):
"""
self.project_dir = _project_dir_factory(project_dir)

def move_project_dir(self, project_dir: Path | str):

Path(project_dir).mkdir(parents=True, exist_ok=True)

for _, es in self:
es.copy_project_dir(Path(project_dir))

self.set_project_dir(project_dir)

def remove_lockfile(self):
"""
Remove the lock file from the project directory
Expand Down
26 changes: 25 additions & 1 deletion src/f3dasm/_src/experimentsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import autograd.numpy as np

# Local
from ._io import load_object
from ._io import copy_object, load_object
from .design.domain import Domain

# Authorship & Credits
Expand Down Expand Up @@ -402,6 +402,30 @@ def round_dict(data: Dict[str, Any]) -> Dict[str, Any]:
self._input_data = round_dict(self._input_data)
self._output_data = round_dict(self._output_data)

def copy_project_dir(self, project_dir: Path):
for key, value in self._input_data.items():
# If the parameter is stored on disk, update the path
if isinstance(value, str) and self.domain.\
input_space[key].to_disk:
new_value = copy_object(object_path=Path(value),
old_project_dir=self.project_dir,
new_project_dir=project_dir)
# Update the path in the input data
self._input_data[key] = new_value

for key, value in self._output_data.items():
# If the parameter is stored on disk, update the path
if isinstance(value, str) and self.domain.\
output_space[key].to_disk:
new_value = copy_object(object_path=Path(value),
old_project_dir=self.project_dir,
new_project_dir=project_dir)
# Update the path in the input data

self._output_data[key] = new_value

self.project_dir = project_dir

# Exporting
# =========================================================================

Expand Down

0 comments on commit 3c13c95

Please sign in to comment.