qten.utils.io
Module reference for qten.utils.io.
io
Versioned pickle-based persistence helpers.
This module provides a lightweight filesystem store for experiment outputs and
other trusted Python objects. Objects are saved under an IO root, grouped by an
active environment name, and versioned automatically as version_<n>.pkl
files.
Repository usage
Use iodir() to configure the root storage directory,
env() to select a project or run namespace, and
save() / load() to persist
trusted objects.
Notes
The storage format uses Python pickle. Only load files produced by trusted
code and from trusted locations.
Examples:
from qten.utils import io
io.iodir(".runs")
io.env("trial-a")
version = io.save({"energy": -1.0}, "results")
rows = io.list_saved("results")
latest = io.load("results")
same = io.load("results", version=version)
iodir
iodir(
path: Optional[Union[str, PathLike[str]]] = None,
) -> str
Get or set the base directory for IO storage.
If a path is provided, it becomes the active IO directory. The directory is created if needed. If no path is provided, the current IO directory is returned; when unset, defaults to ".data".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str or PathLike[str]
|
Filesystem path to use as the IO root. If omitted, the existing root is
returned, defaulting to |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The active IO directory path. The directory is created before returning. |
Examples:
from qten.utils import io
io.iodir(".runs")
Source code in src/qten/utils/io.py
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 78 79 80 81 82 83 | |
env
env(name: Optional[str] = None) -> str
Get or set the active environment name under the IO directory.
When called without a name, returns the currently active environment if one was set during this process, otherwise raises a RuntimeError. When a name is provided, ensures a subdirectory exists under the IO directory and sets it as the active environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment name to activate, or |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The current or newly-set environment name. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no environment is set and |
Examples:
from qten.utils import io
io.iodir(".runs")
io.env("trial-a")
Source code in src/qten/utils/io.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
save
save(obj: Any, name: str) -> int
Save an object to disk as a pickle with automatic versioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to serialize. |
required |
name
|
str
|
Logical name used to group versions under the active environment. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The assigned version number. |
Raises:
| Type | Description |
|---|---|
PicklingError
|
If the object cannot be pickled. |
RuntimeError
|
If no active environment has been selected with |
Examples:
from qten.utils import io
io.env("trial-a")
version = io.save({"energy": -1.0}, "results")
Source code in src/qten/utils/io.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
load
load(name: str, version: int = -1) -> Any
Load a previously saved object by name and version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical name used to group saved versions. |
required |
version
|
int
|
Version to load; use |
-1
|
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the name or version does not exist. |
UnpicklingError
|
If the pickle data is corrupted. |
RuntimeError
|
If no active environment has been selected with |
Notes
Only load data you trust; pickle is not secure against malicious data.
Examples:
from qten.utils import io
latest = io.load("results")
first = io.load("results", version=1)
Source code in src/qten/utils/io.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
list_saved
list_saved(name: str) -> List[Dict[str, Any]]
List saved versions for a name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logical name used to group saved versions. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
Rows with |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the name does not exist. |
RuntimeError
|
If no active environment has been selected with |
Source code in src/qten/utils/io.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |