plaid.storage.zarr.writer¶
plaid.storage.zarr.writer
¶
Zarr dataset writer module.
This module provides functionality for writing and managing datasets in Zarr format for the PLAID library. It includes utilities for generating datasets from sample generators, saving them to disk with optimized chunking, uploading to Hugging Face Hub, and configuring dataset cards with metadata and usage examples.
Key features: - Parallel and sequential dataset generation from generators - Automatic chunking for efficient storage - Integration with Hugging Face Hub for dataset sharing - Dataset card generation with splits, features, and documentation
plaid.storage.zarr.writer.write_sample
¶
Write a single PLAID sample to a Zarr group on disk.
This function serializes one Sample instance into a dedicated Zarr group
under the given split root. Each sample is written as:
sample_<zero-padded index>/
Only variable features listed in var_features_keys are written. Feature
paths are flattened before being used as Zarr array names.
Behavior
- A new Zarr group named
sample_{sample_counter:09d}is created. - Each selected feature is written as a Zarr array if its value is not
None. - NumPy arrays with Unicode dtype (
dtype.kind == 'U') are converted to UTF-8 encoded byte arrays to ensure stable storage (notably for Zarr v3). - Chunk sizes are automatically determined using
_auto_chunkswith a target chunk size of approximately 5 million elements.
Parameters:
-
split_root(Any) –Open Zarr group corresponding to a dataset split (e.g.
zarr.open_group(..., mode="a")). -
sample(Sample) –PLAID
Sampleobject to serialize. -
var_features_keys(list[str]) –List of feature paths (as defined in the variable schema) to extract and write for this sample.
-
sample_counter(int) –Global index of the sample within the split, used to generate the group name and ensure deterministic ordering.
Notes
- The function assumes
split_rootalready exists and is writable. - No schema validation is performed at write time.
- Missing features (
Nonevalues) are silently skipped. - The function is side-effect only and returns
None.
Raises:
-
ContainsGroupError–If a sample group with the same name already exists.
-
OSError / IOError–If an underlying filesystem or Zarr write error occurs.
Source code in plaid/storage/zarr/writer.py
plaid.storage.zarr.writer.generate_datasetdict_to_disk
¶
generate_datasetdict_to_disk(
output_folder,
generators,
variable_schema,
gen_kwargs=None,
num_proc=1,
verbose=False,
)
Generates and saves a dataset dictionary to disk in Zarr format.
This function processes sample generators for different dataset splits, converts samples to dictionaries, and writes them to Zarr arrays on disk. It supports both sequential and parallel processing modes. In parallel mode, gen_kwargs must be provided with batch information for each split.
Parameters:
-
output_folder(Union[str, Path]) –Base directory where the dataset will be saved. A 'data' subdirectory will be created inside this folder.
-
generators(dict[str, Callable[..., Generator[Sample, None, None]]]) –Dictionary mapping split names (e.g., "train", "test") to generator functions that yield Sample objects.
-
variable_schema(dict[str, dict]) –Schema describing the structure and types of variables/features in the samples.
-
gen_kwargs(Optional[dict[str, dict[str, IndexArrayType]]], default:None) –Optional generator arguments for parallel processing. Must include "shards_ids" for each split when num_proc > 1. Required for parallel execution.
-
num_proc(int, default:1) –Number of processes to use for parallel processing. Defaults to 1 (sequential). Must be > 1 only when gen_kwargs is provided.
-
verbose(bool, default:False) –Whether to display progress bars during processing. Defaults to False.
Returns:
-
None(None) –This function does not return a value; it writes the dataset directly to disk.
Source code in plaid/storage/zarr/writer.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 214 215 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 | |
plaid.storage.zarr.writer.push_local_datasetdict_to_hub
¶
Pushes a local dataset directory to Hugging Face Hub.
This function uploads the contents of a local directory to a specified Hugging Face repository as a dataset. It uses the HfApi to handle large folder uploads with configurable parallelism.
Parameters:
-
repo_id(str) –The Hugging Face repository ID where the dataset will be uploaded (e.g., "username/dataset_name").
-
local_dir(str or Path) –Path to the local directory containing the dataset files to upload.
-
num_workers(int, default:1) –Number of worker threads to use for uploading. Defaults to 1.
Returns:
-
None(None) –This function does not return a value; it uploads the dataset directly to Hugging Face Hub.
Source code in plaid/storage/zarr/writer.py
plaid.storage.zarr.writer.configure_dataset_card
¶
configure_dataset_card(
repo_id,
infos,
local_dir,
viewer=None,
pretty_name=None,
dataset_long_description=None,
illustration_urls=None,
arxiv_paper_urls=None,
)
Configures and pushes a dataset card to Hugging Face Hub for a zarr backend dataset.
This function generates a dataset card in YAML format with metadata, features, splits information, and usage examples. It automatically detects splits and sample counts from the local directory structure, then pushes the card to the specified Hugging Face repository.
Parameters:
-
repo_id(str) –The Hugging Face repository ID where the dataset card will be pushed.
-
infos(Infos) –Dataset metadata, including legal information like license.
-
local_dir(Union[str, Path]) –Path to the local directory containing the dataset files, expected to have a 'data' subdirectory with split folders.
-
viewer(Optional[bool], default:None) –Unused parameter for viewer configuration.
-
pretty_name(Optional[str], default:None) –A human-readable name for the dataset to display in the card.
-
dataset_long_description(Optional[str], default:None) –A detailed description of the dataset to include in the card.
-
illustration_urls(Optional[list[str]], default:None) –List of URLs to images that illustrate the dataset, displayed in the card.
-
arxiv_paper_urls(Optional[list[str]], default:None) –List of arXiv URLs for papers related to the dataset, included as sources.
Returns:
-
None(None) –This function does not return a value; it pushes the dataset card directly to Hugging Face Hub.
Source code in plaid/storage/zarr/writer.py
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |