Skip to content

Space io

space_io

Classes

SpaceToolFileIO

Bases: BaseToolFileIO

A base class to handle reading and writing tool results.

Functions
read
read(
    file_name: str | Path, directory_path: str | Path = ""
) -> SpaceToolResult

Read a space tool result.

Parameters:

  • file_name (str | Path) –

    The description is missing.

  • directory_path (str | Path, default: '' ) –

    The description is missing.

Source code in src/vimseo/io/space_io.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def read(
    self, file_name: str | Path, directory_path: str | Path = ""
) -> SpaceToolResult:
    """Read a space tool result."""

    if Path(file_name).suffix != self._EXTENSION:
        msg = f"{self.__class__.__name__} requires the file suffix to be {self._EXTENSION}."
        raise ValueError(msg)
    file_path = (
        file_name if directory_path == "" else Path(directory_path) / file_name
    )
    with Path(file_path).open() as f:
        data = json.load(f)

    result = SpaceToolResult()
    result.parameter_space = ParameterSpace()
    result.metadata = self.create_metadata(data)
    result.parameter_space = self.create_parameter_space(data)
    return result
read_buffer
read_buffer(buf: BinaryIO | TextIO) -> SpaceToolResult

Read a space tool result.

Source code in src/vimseo/io/space_io.py
65
66
67
68
69
70
71
72
73
74
def read_buffer(self, buf: BinaryIO | TextIO) -> SpaceToolResult:
    """Read a space tool result."""

    data = json.loads(buf)

    result = SpaceToolResult()
    result.parameter_space = ParameterSpace()
    result.metadata = self.create_metadata(data)
    result.parameter_space = self.create_parameter_space(data)
    return result
write
write(
    result: SpaceToolResult,
    file_base_name: str | Path = "",
    directory_path: str | Path = "",
) -> dict

Write a result.

Parameters:

  • result (SpaceToolResult) –

    The description is missing.

  • file_base_name (str | Path, default: '' ) –

    The description is missing.

  • directory_path (str | Path, default: '' ) –

    The description is missing.

Source code in src/vimseo/io/space_io.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def write(
    self,
    result: SpaceToolResult,
    file_base_name: str | Path = "",
    directory_path: str | Path = "",
) -> dict:
    data = {
        "parameter_space": self._serialize_distribution_parameters(
            result.parameter_space
        ),
        "metadata": result.metadata,
    }
    json_data = json.dumps(
        data,
        ensure_ascii=True,
        indent=4,
        cls=EnhancedJSONEncoderModelWrapper,
    )

    if file_base_name == "":
        LOGGER.warning("No base file name provided, no file is written.")
        return json_data

    if file_base_name != "":
        directory_path = (
            Path.cwd() if directory_path == "" else Path(directory_path)
        )
        file_path = directory_path / f"{file_base_name}{self._EXTENSION}"
        Path(file_path).write_text(json_data)

    return json_data