aidevelopementtoolkit.logging_utils.file_io

convert_to_serializable_data(data: Any, keep_list: bool = True) -> Any

Recursively convert non-serializable objects to standard Python types.

Handles numpy arrays and scalars, and torch tensors (if available). Recurses into dicts, lists, and tuples, preserving the original structure.

Parameters:
  • data (Any) –

    Data to convert.

  • keep_list (bool, default: True ) –

    Controls how single-element arrays and tensors are converted.

    • If True (default), arrays are always converted to lists, so a single-element array becomes a one-element list (e.g. [42]).
    • If False, single-element arrays and tensors are unwrapped to their scalar value (e.g. 42).
Returns:
  • Any –

    The same structure with numpy/torch types replaced by standard Python types.

Examples:

>>> import numpy as np
>>> convert_to_serializable_data(np.array([1, 2, 3]))
[1, 2, 3]
>>> convert_to_serializable_data(np.array([42]))
[42]
>>> convert_to_serializable_data(np.array([42]), keep_list=False)
42
>>> convert_to_serializable_data(np.float32(3.14))
3.14
>>> convert_to_serializable_data({"a": np.array([1, 2]), "b": np.int64(7)})
{'a': [1, 2], 'b': 7}
>>> convert_to_serializable_data({"a": np.array([1, 2]), "b": np.int64(7)}, keep_list=False)
{'a': [1, 2], 'b': 7}
>>> convert_to_serializable_data(np.array([[1, 2], [3, 4]]), keep_list=False)
[[1, 2], [3, 4]]
Source code in aidevelopementtoolkit/logging_utils/file_io.py
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def convert_to_serializable_data(data: Any, keep_list: bool = True) -> Any:
    """
    Recursively convert non-serializable objects to standard Python types.

    Handles numpy arrays and scalars, and torch tensors (if available).
    Recurses into dicts, lists, and tuples, preserving the original structure.

    Parameters
    ----------
    data : Any
        Data to convert.

    keep_list : bool, default=True
        Controls how single-element arrays and tensors are converted.

        - If `True` (default), arrays are always converted to lists, so a
          single-element array becomes a one-element list (e.g. `[42]`).
        - If `False`, single-element arrays and tensors are unwrapped to
          their scalar value (e.g. `42`).

    Returns
    -------
    Any
        The same structure with numpy/torch types replaced by standard Python types.

    Examples
    --------
    >>> import numpy as np
    >>> convert_to_serializable_data(np.array([1, 2, 3]))
    [1, 2, 3]

    >>> convert_to_serializable_data(np.array([42]))
    [42]

    >>> convert_to_serializable_data(np.array([42]), keep_list=False)
    42

    >>> convert_to_serializable_data(np.float32(3.14))
    3.14

    >>> convert_to_serializable_data({"a": np.array([1, 2]), "b": np.int64(7)})
    {'a': [1, 2], 'b': 7}

    >>> convert_to_serializable_data({"a": np.array([1, 2]), "b": np.int64(7)}, keep_list=False)
    {'a': [1, 2], 'b': 7}

    >>> convert_to_serializable_data(np.array([[1, 2], [3, 4]]), keep_list=False)
    [[1, 2], [3, 4]]
    """
    try:
        import torch
        if isinstance(data, torch.Tensor):
            result = data.detach().cpu().tolist()
            if not keep_list and data.numel() == 1:
                return result[0] if isinstance(result, list) else result
            return result
    except ImportError:
        pass

    if isinstance(data, np.ndarray):
        if not keep_list and data.size == 1:
            return data.flat[0].item()
        return data.tolist()

    if isinstance(data, np.generic):
        return data.item()

    if isinstance(data, dict):
        return {k: convert_to_serializable_data(v, keep_list=keep_list) for k, v in data.items()}

    if isinstance(data, (list, tuple)):
        converted = [convert_to_serializable_data(item, keep_list=keep_list) for item in data]
        return type(data)(converted)

    return data

save_file(data: Any, path: str, append: bool = False, header: bool = False, index: bool = False) -> None

Saves data to a file. The format is deduced from the file extension.

Supported formats: .json, .yaml / .yml, .csv, .npy, .npz, .h5.

Local paths are written directly to the filesystem. Paths beginning with s3:// are written to an S3-compatible object store using the boto3 utilities.

Parameters:
  • data (Any) –

    Data to be saved. - JSON / YAML: any JSON-serialisable object. - CSV: any object convertible to a pandas.DataFrame or a pandas.DataFrame itself. - NPY: a numpy.ndarray. - NPZ / H5: a dict[str, numpy.ndarray], with dictionary keys used as array names. - Images (.png, .jpg, .jpeg, .bmp, .tiff, .webp): a PIL.Image.Image or a numpy.ndarray.

  • path (str) –

    Path to the output file, including extension.

    Examples:

    • "./tmp.json"
    • "s3://my-bucket/data/file.json"
  • append (bool, default: False ) –

    If True, extends the existing local file instead of overwriting it.

    This parameter is ignored for S3 paths, which are always overwritten.

    • JSON / YAML list: extends the list.
    • JSON / YAML dict: updates the dict.
    • CSV: appends rows (no header written).
    • NPZ / H5: adds or overwrites arrays by dictionary key.
  • header (bool, default: False ) –

    Only relevant for CSV files. If True, writes column names as the first row. When appending to an existing CSV file, the header is automatically disabled to avoid writing column names in the middle of the file.

  • index (bool, default: False ) –

    Only relevant for CSV files. Whether to write row indices.

Examples:

>>> save_file({"a": 1}, "./tmp.json")
>>> save_file([1, 2], "./tmp_list.json")
>>> save_file({"b": 2}, "./tmp.json", append=True)
>>> save_file({"x": 1}, "./tmp.yaml")
>>> save_file([{"col": 1}], "./tmp.csv")
>>> save_file({"arr": np.array([1, 2, 3])}, "./tmp.npz")
>>> save_file({"arr": np.array([1, 2, 3])}, "./tmp.h5")
>>> save_file(
...     {"a": 1},
...     "s3://my-bucket/data/file.json",
... )
Source code in aidevelopementtoolkit/logging_utils/file_io.py
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
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
def save_file(
        data: Any, 
        path: str, 
        append: bool = False,
        header: bool = False,
        index: bool = False,
    ) -> None:
    """
    Saves data to a file. The format is deduced from the file extension.

    Supported formats: `.json`, `.yaml` / `.yml`, `.csv`, `.npy`, `.npz`, `.h5`.

    Local paths are written directly to the filesystem. Paths beginning with
    `s3://` are written to an S3-compatible object store using the boto3
    utilities.

    Parameters
    ----------
    data : Any
        Data to be saved.
        - JSON / YAML: any JSON-serialisable object.
        - CSV: any object convertible to a `pandas.DataFrame` or a `pandas.DataFrame` itself.
        - NPY: a `numpy.ndarray`.
        - NPZ / H5: a `dict[str, numpy.ndarray]`, with dictionary keys used as array names.
        - Images (`.png`, `.jpg`, `.jpeg`, `.bmp`, `.tiff`, `.webp`): a `PIL.Image.Image` or a `numpy.ndarray`.

    path : str
        Path to the output file, including extension.

        Examples:

        - `"./tmp.json"`
        - `"s3://my-bucket/data/file.json"`

    append : bool, default=False
        If `True`, extends the existing local file instead of overwriting it.

        This parameter is ignored for S3 paths, which are always overwritten.

        - **JSON / YAML list**: extends the list.
        - **JSON / YAML dict**: updates the dict.
        - **CSV**: appends rows (no header written).
        - **NPZ / H5**: adds or overwrites arrays by dictionary key.

    header : bool, default=False
        Only relevant for CSV files. If `True`, writes column names as the first row.
        When appending to an existing CSV file, the header is automatically
        disabled to avoid writing column names in the middle of the file.

    index : bool, default=False
        Only relevant for CSV files. Whether to write row indices.

    Examples
    --------
    >>> save_file({"a": 1}, "./tmp.json")
    >>> save_file([1, 2], "./tmp_list.json")
    >>> save_file({"b": 2}, "./tmp.json", append=True)
    >>> save_file({"x": 1}, "./tmp.yaml")
    >>> save_file([{"col": 1}], "./tmp.csv")
    >>> save_file({"arr": np.array([1, 2, 3])}, "./tmp.npz")
    >>> save_file({"arr": np.array([1, 2, 3])}, "./tmp.h5")

    >>> save_file(
    ...     {"a": 1},
    ...     "s3://my-bucket/data/file.json",
    ... )
    """

    ext = _get_extension(path)

    # Serialize data into bytes for S3 paths
    if path.startswith("s3://"):

        bucket, s3_key = parse_s3_path(path)
        client = create_s3_client()

        serialized_data = _serialize_data(data, ext)

        write_s3_object(
            client=client,
            bucket=bucket,
            key=s3_key,
            data=serialized_data,
        )

        return

    # Create parent directory
    parent = os.path.dirname(path)
    if parent:
        os.makedirs(parent, exist_ok=True)

    # Handle local JSON files
    if ext == ".json":

        if append and os.path.exists(path):

            with open(path, "r") as f:
                existing = json.load(f)

            if isinstance(existing, list) and isinstance(data, list):
                existing.extend(data)
                data = existing

            elif isinstance(existing, dict) and isinstance(data, dict):
                existing.update(data)
                data = existing

            else:
                logger.error(
                    "Cannot append: incompatible JSON types. "
                    f"Respectively: {type(existing)} and {type(data)}.",
                    RuntimeError
                )

        with open(path, "w") as f:
            json.dump(data, f, indent=4)

    # Handle local YAML files
    elif ext in {".yaml", ".yml"}:

        if append and os.path.exists(path):
            with open(path, "r") as f:
                existing = yaml.safe_load(f)

            if isinstance(existing, list) and isinstance(data, list):
                existing.extend(data)
                data = existing

            elif isinstance(existing, dict) and isinstance(data, dict):
                existing.update(data)
                data = existing

            else:
                logger.error("Cannot append: incompatible YAML types.", RuntimeError)

        with open(path, "w") as f:
            yaml.safe_dump(data, f)

    # Handle local CSV files
    elif ext == ".csv":
        df = pd.DataFrame(data)
        if append and os.path.exists(path):
            df.to_csv(path, mode="a", header=False, index=index)
        else:
            df.to_csv(path, index=index, header=header)

    # Handle local NPY files
    elif ext == ".npy":
        np.save(path, data)

    # Handle local compressed array archives
    elif ext in {".npz", ".h5"}:
        if append and os.path.exists(path):
            arrays = load_file(path)
            arrays.update(data)
        else:
            arrays = data

        with open(path, "wb") as f:
            f.write(_serialize_data(arrays, ext))

    # Handle local image files
    elif ext in _IMAGE_EXTENSIONS:
        if isinstance(data, np.ndarray):
            data = Image.fromarray(data)
        data.save(path)

load_file(path: str, return_type: Optional[Literal['numpy', 'pandas', 'pil']] = 'pandas', process_rank: int = 0, header: Optional[int] = None) -> Any

Loads a file. The format is deduced from the file extension.

Supported formats: .json, .yaml / .yml, .csv, .npy, .npz, .h5.

Local paths are read directly from the filesystem. Paths beginning with s3:// are read from an S3-compatible object store using the boto3 utilities.

Parameters:
  • path (str) –

    Path to the file, including extension.

    Examples:

    • "./tmp.json"
    • "s3://my-bucket/data/file.json"
  • return_type (Optional[Literal['numpy', 'pandas', 'pil']], default: "pandas" ) –

    Controls the return type for CSV and image files.

    • CSV: "pandas" (default) returns a pandas.DataFrame; "numpy" returns a numpy.ndarray.
    • Images: "pil" (default for images) returns a PIL.Image.Image; "numpy" returns a numpy.ndarray.
  • process_rank (int, default: 0 ) –

    Rank of the current process (for distributed setups). This it used to determine the tqdm position, so that multiple processes can display progress bars without overlapping.

  • header (Optional[int], default: None ) –

    Only relevant for CSV files. Row number to use as column names. If None, the CSV is treated as having no header row. When return_type is "numpy", the header row is skipped and not included in the output. When return_type is "pandas", the header is passed directly to pandas.read_csv. Ignored for non-CSV files.

Returns:
  • Any –

    Parsed file content.

Examples:

>>> save_file({"a": 1}, "./tmp.json")
>>> content = load_file("./tmp.json")
>>> content["a"]
1
>>> save_file({"a": 1}, "./tmp.yaml")
>>> content = load_file("./tmp.yaml")
>>> content["a"]
1
>>> df = load_file("./tmp.csv")
>>> arr = load_file("./tmp.csv", return_type="numpy")
>>> content = load_file("s3://my-bucket/data/file.json")
>>> arrays = load_file("./tmp.npz")
Source code in aidevelopementtoolkit/logging_utils/file_io.py
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
def load_file(
        path: str,
        return_type: Optional[Literal["numpy", "pandas", "pil"]] = "pandas",
        process_rank: int = 0,
        header: Optional[int] = None,
    ) -> Any:
    """
    Loads a file. The format is deduced from the file extension.

    Supported formats: `.json`, `.yaml` / `.yml`, `.csv`, `.npy`, `.npz`, `.h5`.

    Local paths are read directly from the filesystem. Paths beginning with
    `s3://` are read from an S3-compatible object store using the boto3
    utilities.

    Parameters
    ----------
    path : str
        Path to the file, including extension.

        Examples:

        - `"./tmp.json"`
        - `"s3://my-bucket/data/file.json"`

    return_type : Optional[Literal["numpy", "pandas", "pil"]], default="pandas"
        Controls the return type for CSV and image files.

        - CSV: `"pandas"` (default) returns a `pandas.DataFrame`; `"numpy"` returns a `numpy.ndarray`.
        - Images: `"pil"` (default for images) returns a `PIL.Image.Image`; `"numpy"` returns a `numpy.ndarray`.

    process_rank : int, default=0
        Rank of the current process (for distributed setups).
        This it used to determine the tqdm position, so that multiple processes
        can display progress bars without overlapping.

    header : Optional[int], default=None
        Only relevant for CSV files. Row number to use as column names.
        If `None`, the CSV is treated as having no header row.
        When `return_type` is `"numpy"`, the header row is skipped and not
        included in the output. When `return_type` is `"pandas"`, the header
        is passed directly to `pandas.read_csv`. Ignored for non-CSV files.

    Returns
    -------
    Any
        Parsed file content.

    Examples
    --------
    >>> save_file({"a": 1}, "./tmp.json")
    >>> content = load_file("./tmp.json")
    >>> content["a"]
    1

    >>> save_file({"a": 1}, "./tmp.yaml")
    >>> content = load_file("./tmp.yaml")
    >>> content["a"]
    1

    >>> df = load_file("./tmp.csv")
    >>> arr = load_file("./tmp.csv", return_type="numpy")
    >>> content = load_file("s3://my-bucket/data/file.json")
    >>> arrays = load_file("./tmp.npz")
    """

    # Read the file into a common file-like object
    if path.startswith("s3://"):

        bucket, s3_key = parse_s3_path(path)
        client = create_s3_client()

        if file_exists(path) is False:
            logger.error(f"S3 object '{path}' does not exist.", FileNotFoundError)

        file_data = io.BytesIO(
            read_s3_object(
                client=client,
                bucket=bucket,
                key=s3_key,
                process_rank=process_rank,
            )
        )

    else:

        if not os.path.exists(path):
            logger.error(f"File '{path}' does not exist.", FileNotFoundError)


        with open(path, "rb") as f:
            file_data = io.BytesIO(f.read())

    ext = _get_extension(path)

    # Parse the file according to its extension
    if ext == ".json":
        return json.load(file_data)

    elif ext in {".yaml", ".yml"}:
        return yaml.safe_load(file_data)

    elif ext == ".csv":

        df = pd.read_csv(file_data, header=header)

        if return_type == "numpy":
            return df.to_numpy()

        elif return_type == "pandas":
            return df

        else:
            logger.error(f"Unsupported return type: '{return_type}'. Supported: 'numpy', 'pandas'.", TypeError)

    elif ext == ".npy":
        return np.load(file_data, allow_pickle=False)

    elif ext == ".npz":
        with np.load(file_data, allow_pickle=False) as npz:
            return {name: npz[name] for name in npz.files}

    elif ext == ".h5":
        arrays = {}
        with h5py.File(file_data, "r") as archive:
            def collect_dataset(name, dataset):
                if isinstance(dataset, h5py.Dataset):
                    array = dataset[()]
                    if dataset.attrs.get("_aidevelopementtoolkit_scalar", False):
                        array = array[0]
                    arrays[name] = np.asarray(array)

            archive.visititems(collect_dataset)
        return arrays

    elif ext in _IMAGE_EXTENSIONS:
        img = Image.open(file_data)
        img.load()
        if return_type == "numpy":
            return np.array(img)
        elif return_type == "pil":
            return img
        else:
            logger.error(f"Unsupported return type: '{return_type}'. Supported: 'numpy', 'pil'.", TypeError)

file_exists(path: str) -> bool

Checks if a file exists. Works for both local and S3 paths.

Parameters:
  • path (str) –

    Path to the file, including extension.

    Examples:

    • "./tmp.json"
    • "s3://my-bucket/data/file.json"
Returns:
  • bool –

    True if the file exists, False otherwise.

Examples:

>>> save_file({"a": 1}, "./tmp.json")
>>> file_exists("./tmp.json")
True
>>> file_exists("s3://my-bucket/data/file.json")
False
Source code in aidevelopementtoolkit/logging_utils/file_io.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
def file_exists(path: str) -> bool:
    """
    Checks if a file exists. Works for both local and S3 paths.

    Parameters
    ----------
    path : str
        Path to the file, including extension.

        Examples:

        - `"./tmp.json"`
        - `"s3://my-bucket/data/file.json"`

    Returns
    -------
    bool
        `True` if the file exists, `False` otherwise.

    Examples
    --------
    >>> save_file({"a": 1}, "./tmp.json")
    >>> file_exists("./tmp.json")
    True
    >>> file_exists("s3://my-bucket/data/file.json")
    False
    """

    if path.startswith("s3://"):
        bucket, key = parse_s3_path(path)
        client = create_s3_client()
        try:
            client.head_object(Bucket=bucket, Key=key)
            return True
        except ClientError as e:
            if e.response["Error"]["Code"] in ("404", "NoSuchKey"):
                return False


    else:
        return os.path.exists(path)