Skip to content

Commit

Permalink
Add optional parameter to store methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mpvanderschelling committed Jun 26, 2024
1 parent 5070d70 commit ca4606c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
18 changes: 11 additions & 7 deletions src/f3dasm/_src/design/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,26 @@ def from_data(cls: Type[Domain],
# Export
# =============================================================================

def store(self, filename: Path) -> None:
def store(self, filename: Path, create_tmp: bool = False) -> None:
"""Stores the Domain in a pickle file.
Parameters
----------
filename : str
Name of the file.
"""
with open(filename.with_suffix('.tmp'), 'wb') as f:
pickle.dump(self, f)
if create_tmp:
with open(filename.with_suffix('.tmp'), 'wb') as f:
pickle.dump(self, f)

# remove old file if it exists
filename.with_suffix('.pkl').unlink(missing_ok=True)
# remove old file if it exists
filename.with_suffix('.pkl').unlink(missing_ok=True)

# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.pkl'))
# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.pkl'))
else:
with open(filename.with_suffix('.pkl'), 'wb') as f:
pickle.dump(self, f)

def _cast_types_dataframe(self) -> dict:
"""Make a dictionary that provides the datatype of each parameter"""
Expand Down
19 changes: 12 additions & 7 deletions src/f3dasm/_src/experimentdata/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def to_dataframe(self) -> pd.DataFrame:
df.columns = self.names
return df.astype(object)

def store(self, filename: Path) -> None:
def store(self, filename: Path, create_tmp: bool = False) -> None:
"""Stores the data to a file.
Parameters
Expand All @@ -263,14 +263,19 @@ def store(self, filename: Path) -> None:
----
The data is stored as a csv file.
"""
# TODO: The column information is not saved in the .csv!
self.to_dataframe().to_csv(filename.with_suffix('.tmp'))

# remove the old file if it exists
filename.with_suffix('.csv').unlink(missing_ok=True)
if create_tmp:
self.to_dataframe().to_csv(filename.with_suffix('.tmp'))

# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.csv'))
# remove the old file if it exists
filename.with_suffix('.csv').unlink(missing_ok=True)

# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.csv'))

else:
# TODO: The column information is not saved in the .csv!
self.to_dataframe().to_csv(filename.with_suffix('.csv'))

def n_best_samples(self, nosamples: int,
column_name: List[str] | str) -> pd.DataFrame:
Expand Down
15 changes: 9 additions & 6 deletions src/f3dasm/_src/experimentdata/_jobqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,24 @@ def select_all(self, status: str) -> _JobQueue:
# Export
# =========================================================================

def store(self, filename: Path) -> None:
def store(self, filename: Path, create_tmp: bool = False) -> None:
"""Stores the jobs in a pickle file.
Parameters
----------
filename : Path
Path of the file.
"""
self.jobs.to_pickle(filename.with_suffix('.tmp'))
if create_tmp:
self.jobs.to_pickle(filename.with_suffix('.tmp'))

# remove old file if it exists
filename.with_suffix('.pkl').unlink(missing_ok=True)
# remove old file if it exists
filename.with_suffix('.pkl').unlink(missing_ok=True)

# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.pkl'))
# rename the file to the correct extension
filename.with_suffix('.tmp').rename(filename.with_suffix('.pkl'))
else:
self.jobs.to_pickle(filename.with_suffix('.pkl'))

def to_dataframe(self, name: str = "") -> pd.DataFrame:
"""Converts the job queue to a DataFrame.
Expand Down
19 changes: 14 additions & 5 deletions src/f3dasm/_src/experimentdata/experimentdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ def get_output_data(self,
# Export
# =========================================================================

def store(self, project_dir: Optional[Path | str] = None):
def store(self, project_dir: Optional[Path | str] = None,
create_tmp: bool = False):
"""Write the ExperimentData to disk in the project directory.
Parameters
Expand Down Expand Up @@ -589,10 +590,18 @@ def store(self, project_dir: Optional[Path | str] = None):
# Create the subdirectory if it does not exist
subdirectory.mkdir(parents=True, exist_ok=True)

self._input_data.store(subdirectory / Path(INPUT_DATA_FILENAME))
self._output_data.store(subdirectory / Path(OUTPUT_DATA_FILENAME))
self._jobs.store(subdirectory / Path(JOBS_FILENAME))
self.domain.store(subdirectory / Path(DOMAIN_FILENAME))
self._input_data.store(
filename=subdirectory / Path(INPUT_DATA_FILENAME),
create_tmp=create_tmp)
self._output_data.store(
filename=subdirectory / Path(OUTPUT_DATA_FILENAME),
create_tmp=create_tmp)
self._jobs.store(
filename=subdirectory / Path(JOBS_FILENAME),
create_tmp=create_tmp)
self.domain.store(
filename=subdirectory / Path(DOMAIN_FILENAME),
create_tmp=create_tmp)

def to_numpy(self) -> Tuple[np.ndarray, np.ndarray]:
"""
Expand Down

0 comments on commit ca4606c

Please sign in to comment.