Skip to content

Commit 5377551

Browse files
xroynardfabiencasenave
authored andcommitted
Update src/plaid/containers/dataset.py
1 parent 3066d83 commit 5377551

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/plaid/containers/dataset.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,44 @@ def load(self, fname: Union[str,Path], verbose: bool = False,
731731
shutil.rmtree(inputdir)
732732

733733
# -------------------------------------------------------------------------#
734+
def add_to_dir(self, sample:Sample, savedir:str=None, verbose:bool=False) -> None:
735+
"""
736+
737+
Notes:
738+
--- if savedir is None, will look for self.savedir which will be retrieved from last previous call to load or save
739+
"""
740+
if self.savedir is None:
741+
if savedir is None:
742+
raise ValueError(f'self.savedir and savedir are None, we don’t know where to save, specify one of them before')
743+
else:
744+
self.savedir = savedir
745+
746+
# --- sample is not only saved to dir, but also added to the dataset
747+
# self.add_sample(sample)
748+
# --- if dataset already contains other Samples, they will all be saved to savedir
749+
# self._save_to_dir_(self.savedir)
750+
751+
if not (os.path.isdir(self.savedir)):
752+
os.makedirs(self.savedir)
753+
754+
if verbose: # pragma: no cover
755+
print(f"Saving database to: {self.savedir}")
756+
757+
samples_dir = os.path.join(self.savedir, 'samples')
758+
if not (os.path.isdir(samples_dir)):
759+
os.makedirs(samples_dir)
760+
761+
# find i_sample
762+
# if there are already samples in the instance, we should not take an already existing id
763+
# if there are already samples in the path, we should not take an already existing id
764+
sample_ids_in_path = [int(d.split('_')[-1]) for d in glob.glob(os.path.join(samples_dir, 'sample_*')) if os.path.isdir(d)]
765+
i_sample = max(len(self), max(sample_ids_in_path)+1)
766+
767+
sample_fname = os.path.join(samples_dir, f'sample_{i_sample:09d}')
768+
sample.save(sample_fname)
769+
734770
def _save_to_dir_(self, savedir: Union[str,Path], verbose: bool = False) -> None:
735-
"""Saves the dataset into a created sample directory and creates an 'infos.yaml' file to store additional information about the dataset.
771+
"""Saves the dataset into a sub-directory `samples` and creates an 'infos.yaml' file to store additional information about the dataset.
736772
737773
Args:
738774
savedir (Union[str,Path]): The path in which to save the files.
@@ -742,6 +778,8 @@ def _save_to_dir_(self, savedir: Union[str,Path], verbose: bool = False) -> None
742778
if not (savedir.is_dir()):
743779
savedir.mkdir(parents=True)
744780

781+
self.savedir = savedir
782+
745783
if verbose: # pragma: no cover
746784
print(f"Saving database to: {savedir}")
747785

@@ -795,6 +833,8 @@ def _load_from_dir_(self, savedir: Union[str,Path], ids: list[int] = None,
795833
if processes_number < -1:
796834
raise ValueError("Number of processes cannot be < -1")
797835

836+
self.savedir = savedir
837+
798838
if verbose: # pragma: no cover
799839
print(f"Reading database located at: {savedir}")
800840

0 commit comments

Comments
 (0)