Skip to content

fsdb_tools

plantdb.commons.fsdb.fsdb_tools Link

add_metadata_to_scan Link

add_metadata_to_scan(db, scan_id, metadata)

Add metadata to a scan dataset.

Parameters:

Name Type Description Default
db FSDB

A local database instance hosting the Scan instance.

required
scan_id str

The identifier of the Scan instance in the local database.

required
metadata dict

The metadata dictionary to assign to the Scan instance.

required

Returns:

Type Description
Scan

The updated Scan instance with the new metadata.

Examples:

>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.fsdb_tools import add_metadata_to_scan
>>> db = dummy_db(with_scan=True)
>>> scan = db.get_scan("myscan_001")
>>> print(scan.metadata)
{'test': 1}
>>> scan = add_metadata_to_scan(db, "myscan_001", {"Name": "Example Scan"})
>>> print(scan.metadata)
{'test': 1, 'Name': 'Example Scan'}
>>> db.disconnect()  # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/fsdb_tools.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def add_metadata_to_scan(db, scan_id, metadata):
    """Add metadata to a scan dataset.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        A local database instance hosting the ``Scan`` instance.
    scan_id : str
        The identifier of the ``Scan`` instance in the local database.
    metadata : dict
        The metadata dictionary to assign to the ``Scan`` instance.

    Returns
    -------
    plantdb.commons.fsdb.Scan
        The updated ``Scan`` instance with the new metadata.

    Examples
    --------
    >>> from plantdb.commons.fsdb import dummy_db
    >>> from plantdb.commons.fsdb.fsdb_tools import add_metadata_to_scan
    >>> db = dummy_db(with_scan=True)
    >>> scan = db.get_scan("myscan_001")
    >>> print(scan.metadata)
    {'test': 1}
    >>> scan = add_metadata_to_scan(db, "myscan_001", {"Name": "Example Scan"})
    >>> print(scan.metadata)
    {'test': 1, 'Name': 'Example Scan'}
    >>> db.disconnect()  # clean up (delete) the temporary dummy database
    """
    if db.scan_exists(scan_id) is False:
        logger.warning(
            f"Scan '{scan_id}' does not exist in the database. Cannot add metadata."
        )
        return None
    # Get or create the scan instance
    scan = db.get_scan(scan_id, create=False)
    # Add metadata to the scan
    scan.set_metadata(metadata)
    # Return the updated scan instance
    return scan