@@ -731,8 +731,44 @@ def load(self, fname: Union[str,Path], verbose: bool = False,
731
731
shutil .rmtree (inputdir )
732
732
733
733
# -------------------------------------------------------------------------#
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
+
734
770
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.
736
772
737
773
Args:
738
774
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
742
778
if not (savedir .is_dir ()):
743
779
savedir .mkdir (parents = True )
744
780
781
+ self .savedir = savedir
782
+
745
783
if verbose : # pragma: no cover
746
784
print (f"Saving database to: { savedir } " )
747
785
@@ -795,6 +833,8 @@ def _load_from_dir_(self, savedir: Union[str,Path], ids: list[int] = None,
795
833
if processes_number < - 1 :
796
834
raise ValueError ("Number of processes cannot be < -1" )
797
835
836
+ self .savedir = savedir
837
+
798
838
if verbose : # pragma: no cover
799
839
print (f"Reading database located at: { savedir } " )
800
840
0 commit comments