@File : save_data.py @Time : 2025/04/03 10:02:16 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2025, Alejandro Marrero @Desc : None

save_results_to_files(filename_pattern, result, variables_names=None, descriptor_names=None, only_instances=False, files_format='parquet')

Saves the results of the generation to CSV files. Args: filename_pattern (str): Pattern for the filenames. result (GenResult): Result of the generation. variables_names (Sequence[str]): Names of the variables. only_instances (bool): Generate only the files with the resulting instances. Default True. If False, it would generate an history and arhice_metrics files. files_format (Literal[str] = "csv" or "parquet"): Format to store the resulting instances file. Parquet is the most efficient for large datasets.

Source code in digneapy/utils/save_data.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def save_results_to_files(
    filename_pattern: str,
    result: GenerationResult,
    variables_names: Optional[Sequence[str]] = None,
    descriptor_names: Optional[Sequence[str]] = None,
    only_instances: bool = False,
    files_format: Literal["csv", "parquet"] = "parquet",
):
    """Saves the results of the generation to CSV files.
    Args:
        filename_pattern (str): Pattern for the filenames.
        result (GenResult): Result of the generation.
        variables_names (Sequence[str]): Names of the variables.
        only_instances (bool): Generate only the files with the resulting instances.
            Default True. If False, it would generate an history and arhice_metrics files.
        files_format (Literal[str] = "csv" or "parquet"): Format to store the resulting instances file. Parquet is the most efficient for large datasets.
    """
    if files_format not in ("csv", "parquet"):
        warnings.warn(
            f"Unrecognised file format: {files_format}. Selecting parquet as fallback.",
            category=RuntimeWarning,
            stacklevel=2,
        )
        files_format = "parquet"
    if len(result.instances) != 0:
        df = pl.concat(
            [
                instance.to_df(
                    variables_names=variables_names,
                    descriptor_names=descriptor_names,
                    portfolio_names=result.solvers,
                )
                for instance in result.instances
            ],
            how="vertical_relaxed",
        )
        if df.height > 0:
            if files_format == "csv":
                df.write_csv(
                    f"{filename_pattern}_instances.csv",
                )
            elif files_format == "parquet":
                df.write_parquet(
                    f"{filename_pattern}_instances.parquet", compression_level=22
                )

        if not only_instances:
            result.history.to_df().write_csv(f"{filename_pattern}_history.csv")
            if result.metrics is not None:
                result.metrics.write_csv(f"{filename_pattern}_archive_metrics.csv")
    else:
        warnings.warn(
            "Archive in Generation result is empty. Nothing to do in save_results_to_files.",
            category=RuntimeWarning,
            stacklevel=2,
        )