io
plantdb.commons.io Link
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 3Dnumpy.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
|
File or Path or str
|
A |
required |
ext
|
str
|
File extension, defaults to "p". |
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.fsdb 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
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 |
|
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
|
File or Path or str
|
A |
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.fsdb 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
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 |
|
read_json Link
read_json(file, **kwargs)
Reads a JSON from a ROMI database file or a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
File or Path or str
|
A |
required |
Returns:
Type | Description |
---|---|
dict
|
The deserialized JSON file. |
Examples:
>>> from plantdb.commons.io import read_json, write_json
>>> from plantdb.commons.fsdb 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
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 |
|
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
|
File or Path or str
|
A |
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.fsdb 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
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
|
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
|
File or Path or str
|
A |
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.fsdb 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
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
|
read_toml Link
read_toml(file, **kwargs)
Reads a TOML from a ROMI database file or a path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
File or Path or str
|
A |
required |
Returns:
Type | Description |
---|---|
dict
|
The deserialized TOML file. |
Examples:
>>> from plantdb.commons.io import read_toml, write_toml
>>> from plantdb.commons.fsdb 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
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 |
|
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
|
File or Path or str
|
A |
required |
ext
|
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
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
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
|
File or Path or str
|
A |
required |
Returns:
Type | Description |
---|---|
TriangleMesh
|
The loaded triangular mesh object. |
Source code in plantdb/commons/io.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
|
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
|
File or Path or str
|
A |
required |
ext
|
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.fsdb 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
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 |
|
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
|
File or Path or str
|
A |
required |
Returns:
Type | Description |
---|---|
VoxelGrid
|
The loaded point cloud object. |
Source code in plantdb/commons/io.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
|
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
|
File or Path or str
|
If a |
required |
data
|
Graph
|
The (tree) graph object to save. |
required |
ext
|
str
|
File extension, defaults to "p". |
'p'
|
Examples:
>>> import networkx as nx
>>> from plantdb.commons.io import write_graph
>>> from plantdb.commons.fsdb 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
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
|
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
|
File or Path or str
|
If a |
required |
data
|
array like
|
The 2D image (RGB array) to save. |
required |
ext
|
(png, jpeg, tiff)
|
File extension, defaults to "png". |
'png'
|
Examples:
>>> import numpy as np
>>> from plantdb.commons.io import write_image
>>> from plantdb.commons.fsdb 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
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 |
|
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
|
File or Path or str
|
If a |
required |
data
|
dict
|
The dictionary to save as a JSON file. |
required |
ext
|
str
|
File extension, defaults to "json". |
'json'
|
Examples:
>>> from plantdb.commons.io import write_json
>>> from plantdb.commons.fsdb 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
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 |
|
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 |
---|---|---|---|
dbfile
|
File or Path or str
|
If a |
required |
data
|
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.fsdb 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
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
|
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
|
File or Path or str
|
If a |
required |
data
|
PointCloud
|
The point cloud object to save. |
required |
ext
|
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.fsdb 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
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
|
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
|
File or Path or str
|
If a |
required |
data
|
dict
|
The dictionary to save as a TOML file. |
required |
ext
|
str
|
File extension, defaults to "toml". |
'toml'
|
Examples:
>>> from plantdb.commons.io import write_toml
>>> from plantdb.commons.fsdb 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
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 |
|
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
|
File or Path or str
|
If a |
required |
data
|
TorchTensor
|
The torch tensor object to save. |
required |
ext
|
str
|
File extension, defaults to "pt". |
'pt'
|
Source code in plantdb/commons/io.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
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
|
File or Path or str
|
If a |
required |
data
|
TriangleMesh
|
The triangular mesh object to save. |
required |
ext
|
str
|
File extension, defaults to "ply". |
'ply'
|
Source code in plantdb/commons/io.py
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
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
|
File
|
The |
required |
data
|
array like
|
The 3D array to save as volume image. |
required |
ext
|
str
|
File extension, defaults to "tiff". |
'tiff'
|
Other Parameters:
Name | Type | Description |
---|---|---|
compress |
bool
|
Indicate if the volume file should be compressed.
Defaults to |
Examples:
>>> import numpy as np
>>> from plantdb.commons.fsdb import FSDB
>>> from plantdb.commons.io import write_volume
>>> from plantdb.commons.fsdb import Scan, Fileset, File
>>> from plantdb.commons.fsdb 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.ones((50, 10, 10)) # stupid volume file with only `1` values
>>> f_lzw = fs.create_file('test_volume_compress')
>>> write_volume(f_lzw, vol)
>>> f = fs.create_file('test_volume')
>>> write_volume(f, vol, compress=False)
>>> print(f_lzw.path().stat().st_size)
15283
>>> print(f.path().stat().st_size)
48994
Source code in plantdb/commons/io.py
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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
|
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
|
File
|
The |
required |
data
|
VoxelGrid
|
The voxel grid object to save. |
required |
ext
|
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
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
|