Skip to content

Recipes

Short scripts for jobs that come up often. Each one assumes you have connected as client, and that your API key is set.

Test scripts on the demo instance first. They can create a lot of items quickly.

Bulk-create samples from a spreadsheet

Given a CSV file with columns item_id, name and chemform:

import csv

with open("samples.csv") as f:
    for row in csv.DictReader(f):
        client.create_item(
            item_id=row["item_id"],
            item_type="samples",
            item_data={"name": row["name"], "chemform": row["chemform"]},
        )
        print("created", row["item_id"])

Upload a folder of instrument files

Attach each file to the item whose ID is the start of the file name, such as jb-042_scan1.xrdml:

from pathlib import Path

for path in Path("xrd_data").glob("*.xrdml"):
    item_id = path.stem.split("_")[0]
    client.upload_file(item_id=item_id, file_path=path)

For files that keep arriving, use Beholder instead of a script.

Download all data for a collection

collection, items = client.get_collection("lfp-paper")

for item in items:
    client.get_item_files(item["item_id"])

For an archive with metadata, export the collection as an .eln file from the web app.

Audit: which samples have no data attached?

for sample in client.get_items("samples"):
    if not sample.get("nblocks"):
        print(sample["item_id"], sample.get("name", ""))

Find everything that went into an item

import networkx as nx

graph = client.get_item_graph(item_id="cell-007", as_networkx=True)
print(nx.ancestors(graph, "cell-007"))