Skip to content

experimentcontext

experimentcontext

Module for the ExperimentContext

Classes

ExperimentContext dataclass

ExperimentContext to provide the ids and storage

Functions
create_folder
create_folder(folder)

Creates a folder relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def create_folder(self, folder: str):
    """Creates a folder relative to the experiment"""
    file_system: AbstractFileSystem
    with open_location(self.fs_config) as (file_system, root_path):
        abs_folder = join(root_path, folder)
        file_system.makedirs(abs_folder, exist_ok=True)
instantiate_datadescription_from_yaml
instantiate_datadescription_from_yaml()

Instantiates a DataDescription from a yaml file

Source code in niceml/experiments/experimentcontext.py
def instantiate_datadescription_from_yaml(self) -> DataDescription:
    """Instantiates a DataDescription from a yaml file"""
    with open_location(self.fs_config) as (exp_fs, exp_root):
        data_description: DataDescription = instantiate_from_yaml(
            join(
                exp_root,
                ExperimentFilenames.CONFIGS_FOLDER,
                OpNames.OP_TRAIN.value,
                ExperimentFilenames.DATA_DESCRIPTION,
            ),
            file_system=exp_fs,
        )
    return data_description
read_csv
read_csv(data_path)

Reads a csv file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def read_csv(self, data_path: str) -> pd.DataFrame:
    """Reads a csv file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        return read_csv(join(root_path, data_path), file_system=file_system)
read_image
read_image(data_path)

Reads an image relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def read_image(self, data_path: str) -> Image.Image:
    """Reads an image relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        return read_image(join(root_path, data_path), file_system=file_system)
read_json
read_json(data_path)

reads the json file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def read_json(self, data_path: str) -> dict:
    """reads the json file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        return read_json(join(root_path, data_path), file_system=file_system)
read_parquet
read_parquet(data_path)

reads the dataframe as parquet file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def read_parquet(self, data_path: str) -> pd.DataFrame:
    """reads the dataframe as parquet file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        return read_parquet(join(root_path, data_path), file_system=file_system)
read_yaml
read_yaml(data_path)

reads the yaml file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def read_yaml(self, data_path: str) -> dict:
    """reads the yaml file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        return read_yaml(join(root_path, data_path), file_system=file_system)
update_last_modified
update_last_modified(timestamp=None)

Updates the last modified timestamp of the experiment info

Source code in niceml/experiments/experimentcontext.py
def update_last_modified(self, timestamp: Optional[str] = None):
    """Updates the last modified timestamp of the experiment info"""
    timestamp = timestamp or generate_timestamp()
    try:
        exp_info_dict = self.read_yaml(ExperimentFilenames.EXP_INFO)
        exp_info_dict[LAST_MODIFIED_KEY] = timestamp
        self.write_yaml(
            exp_info_dict, ExperimentFilenames.EXP_INFO, apply_last_modified=False
        )
    except FileNotFoundError:
        logging.getLogger(__name__).warning(
            "Could not update last modified timestamp, because the "
            "experiment info file was not found."
        )
write_csv
write_csv(
    data, data_path, apply_last_modified=True, **kwargs
)

Writes a csv file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def write_csv(
    self,
    data: pd.DataFrame,
    data_path: str,
    apply_last_modified: bool = True,
    **kwargs,
):
    """Writes a csv file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        write_csv(
            data,
            join(root_path, data_path),
            file_system=file_system,
            **kwargs,
        )
    if apply_last_modified:
        self.update_last_modified()
write_image
write_image(image, data_path, apply_last_modified=True)

Writes an image relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def write_image(
    self, image: Image.Image, data_path: str, apply_last_modified: bool = True
):
    """Writes an image relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        write_image(image, join(root_path, data_path), file_system=file_system)
    if apply_last_modified:
        self.update_last_modified()
write_json
write_json(
    data, data_path, apply_last_modified=True, **kwargs
)

Writes a json file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def write_json(
    self,
    data: dict,
    data_path: str,
    apply_last_modified: bool = True,
    **kwargs,
):
    """Writes a json file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        write_json(
            data,
            join(root_path, data_path),
            file_system=file_system,
            **kwargs,
        )
    if apply_last_modified:
        self.update_last_modified()
write_parquet
write_parquet(
    dataframe,
    data_path,
    compression="gzip",
    apply_last_modified=True,
    **kwargs
)

writes the dataframe as parquet file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def write_parquet(
    self,
    dataframe: pd.DataFrame,
    data_path: str,
    compression: Optional[str] = "gzip",
    apply_last_modified: bool = True,
    **kwargs,
):
    """writes the dataframe as parquet file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        write_parquet(
            dataframe,
            # TODO: change with join_fs_path
            join(root_path, data_path),
            compression=compression,
            file_system=file_system,
            **kwargs,
        )
    if apply_last_modified:
        self.update_last_modified()
write_yaml
write_yaml(
    data, data_path, apply_last_modified=True, **kwargs
)

writes the yaml file relative to the experiment

Source code in niceml/experiments/experimentcontext.py
def write_yaml(
    self, data: dict, data_path: str, apply_last_modified: bool = True, **kwargs
):
    """writes the yaml file relative to the experiment"""
    with open_location(self.fs_config) as (file_system, root_path):
        write_yaml(
            data,
            join(root_path, data_path),
            file_system=file_system,
            **kwargs,
        )
    if apply_last_modified:
        self.update_last_modified()

Functions