Skip to content

field_spec

MIAPPE field specification for the metadata editor UILink

Defines every editable leaf field of the canonical MIAPPE-aligned scan metadata tree (see plantdb.commons.fsdb.metadata_schema) plus the detailed explanations of each field (sourced from the MIAPPE checklist data model TSV).

It also provides helpers to flatten a nested metadata dict to a flat {dot.path: value} mapping and to rebuild the tree, which the UI uses to render editable forms and to write values back.

coerce Link

coerce(path, value)

Coerce value to the declared type of the field at path.

Parameters:

Name Type Description Default

path Link

str

Dot-separated field path whose declared type is applied.

required

value Link

Any

Value to coerce.

required

Returns:

Type Description
Any

The coerced value, the original value if it cannot be coerced, or None for empty values.

Source code in plantdb/client/metadata_app/field_spec.py
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
def coerce(path: str, value: Any) -> Any:
    """Coerce ``value`` to the declared type of the field at ``path``.

    Parameters
    ----------
    path : str
        Dot-separated field path whose declared type is applied.
    value : Any
        Value to coerce.

    Returns
    -------
    Any
        The coerced value, the original value if it cannot be coerced, or
        ``None`` for empty values.
    """
    if value is None or value == "":
        return None
    spec = FIELD_BY_PATH[path]
    ftype = spec["type"]
    try:
        if ftype == "int":
            return int(float(value))
        if ftype == "float":
            return float(value)
    except (TypeError, ValueError):
        return value
    return str(value)

collect_suggestions Link

collect_suggestions(scans, path)

Return the sorted unique non-empty values of path across scans.

Parameters:

Name Type Description Default

scans Link

list of dict of str to Any

A list of flattened scan dicts (see :func:flatten).

required

path Link

str

Dot-separated field path to collect values for.

required

Returns:

Type Description
list of str

Sorted unique values.

Source code in plantdb/client/metadata_app/field_spec.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def collect_suggestions(scans: list[dict[str, Any]], path: str) -> list[str]:
    """Return the sorted unique non-empty values of ``path`` across ``scans``.

    Parameters
    ----------
    scans : list of dict of str to Any
        A list of flattened scan dicts (see :func:`flatten`).
    path : str
        Dot-separated field path to collect values for.

    Returns
    -------
    list of str
        Sorted unique values.
    """
    values: set[str] = set()
    for flat in scans:
        v = flat.get(path)
        if v is not None and str(v) not in (None, "", "None"):
            values.add(str(v))
    return sorted(values)

flatten Link

flatten(metadata, prefix='')

Flatten a nested metadata dict into a flat {dot.path: value} mapping.

Only leaf fields declared in FIELD_SPECS are kept.

Parameters:

Name Type Description Default

metadata Link

dict of str to Any

The nested metadata dict to flatten.

required

prefix Link

str

Dot-separated path prefix to prepend (used in recursion).

''

Returns:

Type Description
dict of str to Any

The flattened mapping.

Source code in plantdb/client/metadata_app/field_spec.py
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
def flatten(metadata: dict[str, Any], prefix: str = "") -> dict[str, Any]:
    """Flatten a nested metadata dict into a flat ``{dot.path: value}`` mapping.

    Only leaf fields declared in ``FIELD_SPECS`` are kept.

    Parameters
    ----------
    metadata : dict of str to Any
        The nested metadata dict to flatten.
    prefix : str, default ''
        Dot-separated path prefix to prepend (used in recursion).

    Returns
    -------
    dict of str to Any
        The flattened mapping.
    """
    flat: dict[str, Any] = {}
    for key, value in metadata.items():
        path = f"{prefix}.{key}" if prefix else key
        if path in FIELD_BY_PATH:
            flat[path] = value
        elif isinstance(value, dict):
            flat.update(flatten(value, path))
    return flat

sections Link

sections()

Return the ordered top-level MIAPPE section names.

Returns:

Type Description
list of str

The top-level keys of the biological schema.

Source code in plantdb/client/metadata_app/field_spec.py
276
277
278
279
280
281
282
283
284
def sections() -> list[str]:
    """Return the ordered top-level MIAPPE section names.

    Returns
    -------
    list of str
        The top-level keys of the biological schema.
    """
    return list(SCAN_BIOLOGICAL_SCHEMA.keys())

specs_for_section Link

specs_for_section(section)

Return the field specs whose dot-path starts with section.

Parameters:

Name Type Description Default

section Link

str

Top-level MIAPPE section name, e.g. study.

required

Returns:

Type Description
list of FieldSpec

The field specs belonging to the section.

Source code in plantdb/client/metadata_app/field_spec.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
def specs_for_section(section: str) -> list[FieldSpec]:
    """Return the field specs whose dot-path starts with ``section``.

    Parameters
    ----------
    section : str
        Top-level MIAPPE section name, e.g. ``study``.

    Returns
    -------
    list of FieldSpec
        The field specs belonging to the section.
    """
    return [s for s in FIELD_SPECS if s["path"].startswith(section + ".")]

unflatten Link

unflatten(flat)

Rebuild a nested metadata dict from a flat {dot.path: value} mapping.

Intermediate sections are created as needed. Values are coerced to the declared field type.

Parameters:

Name Type Description Default

flat Link

dict of str to Any

The flattened mapping to rebuild.

required

Returns:

Type Description
dict of str to Any

The nested metadata tree.

Source code in plantdb/client/metadata_app/field_spec.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def unflatten(flat: dict[str, Any]) -> dict[str, Any]:
    """Rebuild a nested metadata dict from a flat ``{dot.path: value}`` mapping.

    Intermediate sections are created as needed. Values are coerced to the
    declared field type.

    Parameters
    ----------
    flat : dict of str to Any
        The flattened mapping to rebuild.

    Returns
    -------
    dict of str to Any
        The nested metadata tree.
    """
    tree: dict[str, Any] = {}
    for path, value in flat.items():
        parts = path.split(".")
        target = tree
        for part in parts[:-1]:
            target = target.setdefault(part, {})
        target[parts[-1]] = coerce(path, value)
    return tree