Skip to content

io

The io module of the ROMI plantdb library contains all functions for reading and writing data to database.

Hereafter we detail the formats and their associated Python types and meanings.

json

Dictionaries or lists, read and written using json.

  • Python objects: dict, list
  • File extensions: 'json'
toml

Dictionaries or lists, read and written using toml.

  • Python objects: dict, list
  • File extensions: 'toml'
2D image

RGB or RGBA image data, read and written using imageio.

  • Python objects: numpy.ndarray
  • File extensions: 'jpg', 'png'
3D volume

Grayscale or binary volume image data, read and written using imageio.

  • Python objects: numpy.ndarray
  • File extensions: 'tiff'
Labelled 3D volume

Labelled volume image data, converted to dictionary of 3D (binary) numpy arrays, read and written using numpy.

  • Python objects: dict of 3D numpy.ndarray
  • File extensions: 'npz'
Point cloud

Point clouds, read and written using open3d.

  • Python object: open3d.geometry.PointCloud
  • File extensions: 'ply'
Triangle mesh

Triangular meshes, read and written using open3d.

  • Python object: open3d.geometry.TriangleMesh
  • File extensions: 'ply'
Voxel grid

Voxel grids, read and written using open3d.

  • Python object: open3d.geometry.VoxelGrid
  • File extensions: 'ply'
Tree graph

Tree graphs, read and written using networkx.

  • Python object: networkx.Graph
  • File extensions: 'p'
Pytorch tensor

Trained tensor, read and written using torch.

  • Python object: torch.tensor
  • File extensions: 'pt'

read_graph Link

read_graph(file, **kwargs)

Reads a networkx Graph from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
Graph

The loaded (tree) graph object.

Examples:

>>> import networkx as nx
>>> from plantdb.commons.io import read_graph, write_graph
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_nx_graph")
>>> g = nx.path_graph(4)
>>> print(g)
Graph with 4 nodes and 3 edges
>>> write_graph(f, g)
>>> f = fs.get_file("test_nx_graph")
>>> g2 = read_graph(f)
>>> print(g2)
Graph with 4 nodes and 3 edges
Source code in plantdb/commons/io.py
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
def read_graph(file, **kwargs):
    """Reads a networkx ``Graph`` from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    networkx.Graph
        The loaded (tree) graph object.

    Examples
    --------
    >>> import networkx as nx
    >>> from plantdb.commons.io import read_graph, write_graph
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_nx_graph")
    >>> g = nx.path_graph(4)
    >>> print(g)
    Graph with 4 nodes and 3 edges
    >>> write_graph(f, g)
    >>> f = fs.get_file("test_nx_graph")
    >>> g2 = read_graph(f)
    >>> print(g2)
    Graph with 4 nodes and 3 edges
    """
    import pickle
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    # Load the pickled file:
    with open(file, mode='rb') as f:
        G = pickle.load(f, **kwargs)
    return G

read_image Link

read_image(file, **kwargs)

Reads a 2D image from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
ndarray

The image as an RGB(A) array.

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import read_image, write_image
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_image")
>>> rng = np.random.default_rng()
>>> img = np.array(rng.random((5, 5, 3))*255, dtype='uint8')  # an 8bit 5x5 RGB image
>>> write_image(f, img)
>>> f = fs.get_file("test_image")
>>> img2 = read_image(f)
>>> np.testing.assert_array_equal(img, img2)  # raise an exception if not equal!
Source code in plantdb/commons/io.py
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
def read_image(file, **kwargs):
    """Reads a 2D image from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    numpy.ndarray
        The image as an RGB(A) array.

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import read_image, write_image
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_image")
    >>> rng = np.random.default_rng()
    >>> img = np.array(rng.random((5, 5, 3))*255, dtype='uint8')  # an 8bit 5x5 RGB image
    >>> write_image(f, img)
    >>> f = fs.get_file("test_image")
    >>> img2 = read_image(f)
    >>> np.testing.assert_array_equal(img, img2)  # raise an exception if not equal!
    """
    import imageio.v3 as iio
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return iio.imread(file, **kwargs)

read_json Link

read_json(file, **kwargs)

Reads a JSON from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
dict

The deserialized JSON file.

Examples:

>>> from plantdb.commons.io import read_json, write_json
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_json")
>>> data = {"test": False, 'ROMI': 'RObotics for MIcrofarms'}
>>> write_json(f, data)
>>> f = fs.get_file("test_json")
>>> read_json(f)
{'test': False, 'ROMI': 'RObotics for MIcrofarms'}
Source code in plantdb/commons/io.py
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
def read_json(file, **kwargs):
    """Reads a JSON from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    dict
        The deserialized JSON file.

    Examples
    --------
    >>> from plantdb.commons.io import read_json, write_json
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_json")
    >>> data = {"test": False, 'ROMI': 'RObotics for MIcrofarms'}
    >>> write_json(f, data)
    >>> f = fs.get_file("test_json")
    >>> read_json(f)
    {'test': False, 'ROMI': 'RObotics for MIcrofarms'}
    """
    import json
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    # Load the file:
    with open(file, mode='r') as f:
        data = json.loads(f.read(), **kwargs)
    return data

read_npz Link

read_npz(file, **kwargs)

Reads a dictionary of arrays from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
dict of numpy.ndarray

The dictionary of numpy arrays.

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import read_npz, write_npz
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file('test_npz')
>>> rng = np.random.default_rng()
>>> npz = {f"{i}": rng.random((10, 10, 3)) for i in range(5)}
>>> write_npz(f, npz)
>>> f = fs.get_file("test_npz")
>>> npz2 = read_npz(f)
>>> np.testing.assert_array_equal(npz["0"], npz2["0"])  # raise an exception if not equal!
Source code in plantdb/commons/io.py
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
def read_npz(file, **kwargs):
    """Reads a dictionary of arrays from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    dict of numpy.ndarray
        The dictionary of numpy arrays.

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import read_npz, write_npz
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file('test_npz')
    >>> rng = np.random.default_rng()
    >>> npz = {f"{i}": rng.random((10, 10, 3)) for i in range(5)}
    >>> write_npz(f, npz)
    >>> f = fs.get_file("test_npz")
    >>> npz2 = read_npz(f)
    >>> np.testing.assert_array_equal(npz["0"], npz2["0"])  # raise an exception if not equal!
    """
    import numpy as np
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return dict(np.load(file, **kwargs))

read_point_cloud Link

read_point_cloud(file, **kwargs)

Reads a point cloud from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
PointCloud

The loaded point cloud object.

Examples:

>>> import open3d as o3d
>>> import numpy as np
>>> from plantdb.commons.io import read_point_cloud, write_point_cloud
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file('test_npz')
>>> pcd = o3d.geometry.PointCloud()
>>> pcd.points = o3d.utility.Vector3dVector(np.array([[1, 2, 3]]))
>>> write_point_cloud(f,pcd)
>>> f = fs.get_file('test_npz')
>>> pcd = read_point_cloud(f)
>>> print(type(pcd))
<class 'open3d.cuda.pybind.geometry.PointCloud'>
>>> print(np.asarray(pcd.points))
[[1. 2. 3.]]
Source code in plantdb/commons/io.py
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
def read_point_cloud(file, **kwargs):
    """Reads a point cloud from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    open3d.geometry.PointCloud
        The loaded point cloud object.

    Examples
    --------
    >>> import open3d as o3d
    >>> import numpy as np
    >>> from plantdb.commons.io import read_point_cloud, write_point_cloud
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file('test_npz')
    >>> pcd = o3d.geometry.PointCloud()
    >>> pcd.points = o3d.utility.Vector3dVector(np.array([[1, 2, 3]]))
    >>> write_point_cloud(f,pcd)
    >>> f = fs.get_file('test_npz')
    >>> pcd = read_point_cloud(f)
    >>> print(type(pcd))
    <class 'open3d.cuda.pybind.geometry.PointCloud'>
    >>> print(np.asarray(pcd.points))
    [[1. 2. 3.]]
    """
    from open3d import io
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return io.read_point_cloud(str(file), **kwargs)

read_toml Link

read_toml(file, **kwargs)

Reads a TOML from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
dict

The deserialized TOML file.

Examples:

>>> from plantdb.commons.io import read_toml, write_toml
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_json")
>>> data = {"test": True, 'ROMI': 'RObotics for MIcrofarms'}
>>> write_toml(f, data)
>>> f = fs.get_file("test_json")
>>> read_toml(f)
{'test': True}
Source code in plantdb/commons/io.py
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
def read_toml(file, **kwargs):
    """Reads a TOML from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    dict
        The deserialized TOML file.

    Examples
    --------
    >>> from plantdb.commons.io import read_toml, write_toml
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_json")
    >>> data = {"test": True, 'ROMI': 'RObotics for MIcrofarms'}
    >>> write_toml(f, data)
    >>> f = fs.get_file("test_json")
    >>> read_toml(f)
    {'test': True}
    """
    import toml
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    # Load the file:
    with open(file, mode='r') as f:
        data = toml.loads(f.read(), **kwargs)
    return data

read_torch Link

read_torch(file, ext='pt', **kwargs)

Reads a torch tensor from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

ext Link

str

File extension, defaults to "pt".

'pt'

Returns:

Type Description
Tensor

The loaded tensor object.

Examples:

>>> from plantdb.commons.io import read_torch
>>> from plantdb.commons.test_database import test_database
>>> db = test_database(with_models=True)
>>> db.connect()
>>> scan = db.get_scan("real_plant_analyzed")
>>> model_file = db.get_scan('models').get_fileset('models').get_file('Resnet_896_896_epoch50')
>>> model = read_torch(model_file)
>>> db.disconnect()
Source code in plantdb/commons/io.py
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
def read_torch(file, ext="pt", **kwargs):
    """Reads a torch tensor from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.
    ext : str, optional
        File extension, defaults to "pt".

    Returns
    -------
    Torch.Tensor
        The loaded tensor object.

    Examples
    --------
    >>> from plantdb.commons.io import read_torch
    >>> from plantdb.commons.test_database import test_database
    >>> db = test_database(with_models=True)
    >>> db.connect()
    >>> scan = db.get_scan("real_plant_analyzed")
    >>> model_file = db.get_scan('models').get_fileset('models').get_file('Resnet_896_896_epoch50')
    >>> model = read_torch(model_file)
    >>> db.disconnect()
    """
    import torch
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return torch.load(file, **kwargs)

read_triangle_mesh Link

read_triangle_mesh(file, **kwargs)

Reads a triangular mesh from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
TriangleMesh

The loaded triangular mesh object.

Source code in plantdb/commons/io.py
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
def read_triangle_mesh(file, **kwargs):
    """Reads a triangular mesh from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    open3d.geometry.TriangleMesh
        The loaded triangular mesh object.
    """
    from open3d import io
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return io.read_triangle_mesh(str(file), **kwargs)

read_volume Link

read_volume(file, ext='tiff', **kwargs)

Reads a volume image from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

ext Link

str

File extension, defaults to "tiff".

'tiff'

Returns:

Type Description
ndarray

The volume as a 3D array.

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import read_volume, write_volume
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file('test_volume')
>>> rng = np.random.default_rng()
>>> vol = rng.random((50, 10, 10))
>>> write_volume(f, vol)
>>> f = fs.get_file("test_volume")
>>> vol2 = read_volume(f)
>>> np.testing.assert_array_equal(vol, vol2)  # raise an exception if not equal!
Source code in plantdb/commons/io.py
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
def read_volume(file, ext="tiff", **kwargs):
    """Reads a volume image from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.
    ext : str, optional
        File extension, defaults to "tiff".

    Returns
    -------
    numpy.ndarray
        The volume as a 3D array.

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import read_volume, write_volume
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file('test_volume')
    >>> rng = np.random.default_rng()
    >>> vol = rng.random((50, 10, 10))
    >>> write_volume(f, vol)
    >>> f = fs.get_file("test_volume")
    >>> vol2 = read_volume(f)
    >>> np.testing.assert_array_equal(vol, vol2)  # raise an exception if not equal!
    """
    import imageio.v3 as iio
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return iio.imread(file, **kwargs)

read_voxel_grid Link

read_voxel_grid(file, **kwargs)

Reads a voxel grid from a ROMI database file or a path.

Parameters:

Name Type Description Default

file Link

File or Path or str

A File instance or file path, to read from.

required

Returns:

Type Description
VoxelGrid

The loaded point cloud object.

Source code in plantdb/commons/io.py
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
def read_voxel_grid(file, **kwargs):
    """Reads a voxel grid from a ROMI database file or a path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        A ``File`` instance or file path, to read from.

    Returns
    -------
    open3d.geometry.VoxelGrid
        The loaded point cloud object.
    """
    from open3d import io
    # Get path to file if in a database:
    if isinstance(file, File):
        file = file.path()
    return io.read_voxel_grid(str(file), **kwargs)

write_graph Link

write_graph(file, data, ext='p', **kwargs)

Writes a networkx Graph to a ROMI database file.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

Graph

The (tree) graph object to save.

required

ext Link

str

File extension, defaults to "p".

'p'

Examples:

>>> import networkx as nx
>>> from plantdb.commons.io import write_graph
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_nx_graph")
>>> g = nx.path_graph(4)
>>> write_graph(f, g)
Source code in plantdb/commons/io.py
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
def write_graph(file, data, ext="p", **kwargs):
    """Writes a networkx ``Graph`` to a ROMI database file.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : networkx.Graph
        The (tree) graph object to save.
    ext : str, optional
        File extension, defaults to "p".

    Examples
    --------
    >>> import networkx as nx
    >>> from plantdb.commons.io import write_graph
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_nx_graph")
    >>> g = nx.path_graph(4)
    >>> write_graph(f, g)
    """
    _writer(file, data, ext, _write_graph, **kwargs)
    return

write_image Link

write_image(file, data, ext='png', **kwargs)

Writes a 2D image to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

array like

The 2D image (RGB array) to save.

required

ext Link

(png, jpeg, tiff)

File extension, defaults to "png".

'png'

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import write_image
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_image")
>>> rng = np.random.default_rng()
>>> img = np.array(rng.random((5, 5, 3))*255, dtype='uint8')  # an 8bit 5x5 RGB image
>>> write_image(f, img)
Source code in plantdb/commons/io.py
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
def write_image(file, data, ext="png", **kwargs):
    """Writes a 2D image to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : array like
        The 2D image (RGB array) to save.
    ext : {'png', 'jpeg', 'tiff'}, optional
        File extension, defaults to "png".

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import write_image
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_image")
    >>> rng = np.random.default_rng()
    >>> img = np.array(rng.random((5, 5, 3))*255, dtype='uint8')  # an 8bit 5x5 RGB image
    >>> write_image(f, img)
    """
    _writer(file, data, ext, _write_image, **kwargs)
    return

write_json Link

write_json(file, data, ext='json', **kwargs)

Writes a JSON to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

dict

The dictionary to save as a JSON file.

required

ext Link

str

File extension, defaults to "json".

'json'

Examples:

>>> from plantdb.commons.io import write_json
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_json")
>>> data = {"test": False, 'ROMI': 'RObotics for MIcrofarms'}
>>> write_json(f, data)
Source code in plantdb/commons/io.py
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
def write_json(file, data, ext="json", **kwargs):
    """Writes a JSON to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : dict
        The dictionary to save as a JSON file.
    ext : str, optional
        File extension, defaults to "json".

    Examples
    --------
    >>> from plantdb.commons.io import write_json
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_json")
    >>> data = {"test": False, 'ROMI': 'RObotics for MIcrofarms'}
    >>> write_json(f, data)
    """
    _writer(file, data, ext, _write_json, **kwargs)
    return

write_npz Link

write_npz(file, data, **kwargs)

Writes a dictionary of arrays to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

dict of numpy.ndarray

A dictionary of arrays to save as a single compressed '.npz' file.

required

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import read_npz, write_npz
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file('test_npz')
>>> rng = np.random.default_rng()
>>> npz = {f"{i}": rng.random((10, 10, 3)) for i in range(5)}
>>> write_npz(f, npz)
Source code in plantdb/commons/io.py
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
def write_npz(file, data, **kwargs):
    """Writes a dictionary of arrays to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : dict of numpy.ndarray
        A dictionary of arrays to save as a single compressed '.npz' file.

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import read_npz, write_npz
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file('test_npz')
    >>> rng = np.random.default_rng()
    >>> npz = {f"{i}": rng.random((10, 10, 3)) for i in range(5)}
    >>> write_npz(f, npz)
    """
    _writer(file, data, 'npz', _write_npz, **kwargs)
    return

write_point_cloud Link

write_point_cloud(file, data, ext='ply', **kwargs)

Writes a point cloud to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

PointCloud

The point cloud object to save.

required

ext Link

str

File extension, defaults to "ply".

'ply'

Examples:

>>> import open3d as o3d
>>> import numpy as np
>>> from plantdb.commons.io import write_point_cloud
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file('test_npz')
>>> pcd = o3d.geometry.PointCloud()
>>> pcd.points = o3d.utility.Vector3dVector(np.array([[1, 2, 3]]))
>>> write_point_cloud(f,pcd)
Source code in plantdb/commons/io.py
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
def write_point_cloud(file, data, ext="ply", **kwargs):
    """Writes a point cloud to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : open3d.geometry.PointCloud
        The point cloud object to save.
    ext : str, optional
        File extension, defaults to "ply".

    Examples
    --------
    >>> import open3d as o3d
    >>> import numpy as np
    >>> from plantdb.commons.io import write_point_cloud
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file('test_npz')
    >>> pcd = o3d.geometry.PointCloud()
    >>> pcd.points = o3d.utility.Vector3dVector(np.array([[1, 2, 3]]))
    >>> write_point_cloud(f,pcd)
    """
    _writer(file, data, ext, _write_point_cloud, **kwargs)
    return

write_toml Link

write_toml(file, data, ext='toml', **kwargs)

Writes a TOML to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

dict

The dictionary to save as a TOML file.

required

ext Link

str

File extension, defaults to "toml".

'toml'

Examples:

>>> from plantdb.commons.io import write_toml
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.create_file("test_json")
>>> data = {"test": True, 'ROMI': 'RObotics for MIcrofarms'}
>>> write_toml(f, data)
Source code in plantdb/commons/io.py
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
def write_toml(file, data, ext="toml", **kwargs):
    """Writes a TOML to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : dict
        The dictionary to save as a TOML file.
    ext : str, optional
        File extension, defaults to "toml".

    Examples
    --------
    >>> from plantdb.commons.io import write_toml
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> f = fs.create_file("test_json")
    >>> data = {"test": True, 'ROMI': 'RObotics for MIcrofarms'}
    >>> write_toml(f, data)
    """
    _writer(file, data, ext, _write_toml, **kwargs)
    return

write_torch Link

write_torch(file, data, ext='pt', **kwargs)

Writes a torch tensor to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

TorchTensor

The torch tensor object to save.

required

ext Link

str

File extension, defaults to "pt".

'pt'
Source code in plantdb/commons/io.py
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
def write_torch(file, data, ext="pt", **kwargs):
    """Writes a torch tensor to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : TorchTensor
        The torch tensor object to save.
    ext : str, optional
        File extension, defaults to "pt".
    """
    _writer(file, data, ext, _write_torch, **kwargs)
    return

write_triangle_mesh Link

write_triangle_mesh(file, data, ext='ply', **kwargs)

Writes a triangular mesh to a ROMI database file or to a given path.

Parameters:

Name Type Description Default

file Link

File or Path or str

If a File instance, the file will be saved to the associated database/scan/fileset. Else, write the data to the given file path, ignoring the extension parameter ext.

required

data Link

TriangleMesh

The triangular mesh object to save.

required

ext Link

str

File extension, defaults to "ply".

'ply'
Source code in plantdb/commons/io.py
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
def write_triangle_mesh(file, data, ext="ply", **kwargs):
    """Writes a triangular mesh to a ROMI database file or to a given path.

    Parameters
    ----------
    file : plantdb.commons.File or pathlib.Path or str
        If a ``File`` instance, the file will be saved to the associated database/scan/fileset.
        Else, write the `data` to the given file path, ignoring the extension parameter `ext`.
    data : open3d.geometry.TriangleMesh
        The triangular mesh object to save.
    ext : str, optional
        File extension, defaults to "ply".
    """
    _writer(file, data, ext, _write_triangle_mesh, **kwargs)
    return

write_volume Link

write_volume(file, data, ext='tiff', **kwargs)

Writes a volume image to a ROMI database file.

Parameters:

Name Type Description Default

file Link

File

The File object used to write the associated file.

required

data Link

array like

The 3D array to save as volume image.

required

ext Link

str

File extension, defaults to "tiff".

'tiff'

Examples:

>>> import numpy as np
>>> from plantdb.commons.io import write_volume
>>> from plantdb.commons.test_database import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> db.connect()
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> vol = np.random.randint(0, 254, (10, 10, 10), 'uint8')  # random volume file with 8bit values
>>> f_uint8 = fs.create_file('test_volume_uint8')
>>> write_volume(f_uint8, vol)
>>> f_uint16 = fs.create_file('test_volume_uint16')
>>> write_volume(f_uint16, vol.astype('uint16'))
>>> print(f_uint8.path().stat().st_size)
2750
>>> print(f_uint16.path().stat().st_size)
3750
>>> db.disconnect()
Source code in plantdb/commons/io.py
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
def write_volume(file, data, ext="tiff", **kwargs):
    """Writes a volume image to a ROMI database file.

    Parameters
    ----------
    file : plantdb.commons.File
        The `File` object used to write the associated file.
    data : array like
        The 3D array to save as volume image.
    ext : str, optional
        File extension, defaults to "tiff".

    Examples
    --------
    >>> import numpy as np
    >>> from plantdb.commons.io import write_volume
    >>> from plantdb.commons.test_database import dummy_db
    >>> db = dummy_db(with_fileset=True)
    >>> db.connect()
    >>> scan = db.get_scan("myscan_001")
    >>> fs = scan.get_fileset("fileset_001")
    >>> vol = np.random.randint(0, 254, (10, 10, 10), 'uint8')  # random volume file with 8bit values
    >>> f_uint8 = fs.create_file('test_volume_uint8')
    >>> write_volume(f_uint8, vol)
    >>> f_uint16 = fs.create_file('test_volume_uint16')
    >>> write_volume(f_uint16, vol.astype('uint16'))
    >>> print(f_uint8.path().stat().st_size)
    2750
    >>> print(f_uint16.path().stat().st_size)
    3750
    >>> db.disconnect()
    """
    _writer(file, data, ext, _write_volume, **kwargs)
    return

write_voxel_grid Link

write_voxel_grid(file, data, ext='ply', **kwargs)

Writes a voxel grid to a ROMI database file.

Parameters:

Name Type Description Default

file Link

File

The File object used to write the associated file.

required

data Link

VoxelGrid

The voxel grid object to save.

required

ext Link

str

File extension, defaults to "ply".

'ply'
See Also

plantdb.commons.io._write_voxel_grid open3d.cuda.pybind.io.write_voxel_grid

Source code in plantdb/commons/io.py
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
def write_voxel_grid(file, data, ext="ply", **kwargs):
    """Writes a voxel grid to a ROMI database file.

    Parameters
    ----------
    file : plantdb.commons.File
        The `File` object used to write the associated file.
    data : open3d.geometry.VoxelGrid
        The voxel grid object to save.
    ext : str, optional
        File extension, defaults to "ply".

    See Also
    --------
    plantdb.commons.io._write_voxel_grid
    open3d.cuda.pybind.io.write_voxel_grid
    """
    _writer(file, data, ext, _write_voxel_grid, **kwargs)
    return