-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
53 lines (42 loc) · 1.97 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
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
51
52
53
import random
from pathlib import Path
from datalab_api import DatalabClient, DuplicateItemError
def import_cheminventory(filename: Path, client: DatalabClient):
"""Helper function to import a ChemInventory xlsx export into datalab, using the
'starting_materials' item type.
Migrated from the original version in the main datalab repository:
https://github.com/the-grey-group/datalab/blob/43764fb494c2cc1bf9f7dc90c25594aeb79d5767/pydatalab/tasks.py#L350-L413
"""
def _generate_random_startingmaterial_id():
"""Generate 'XX' + a random 15-length string for use as an id for starting materials
that don't have a barcode.
"""
yield "".join(["XX"] + random.choices("abcdefghijklmnopqrstuvwxyz0123456789", k=15))
try:
import pandas as pd
inventory_df = pd.read_excel(filename)
except ImportError as exc:
raise ImportError(
"Please install pandas + openpyxl to use this helper function, via `pip install datalab-api[cheminventory-helper]`"
) from exc
inventory_df["type"] = "starting_materials"
inventory_df["item_id"] = inventory_df["Barcode"]
# Fill missing barcodes
inventory_df["item_id"] = inventory_df["item_id"].fillna(
inventory_df["item_id"].apply(lambda _: next(_generate_random_startingmaterial_id()))
)
inventory_df["Molecular Weight"] = inventory_df["Molecular Weight"].replace(" ", float("nan"))
counts = {"success": 0, "duplicate": 0, "failed": 0}
for item in inventory_df.to_dict(orient="records"):
try:
client.create_item(
item["item_id"], item["type"], item, collection_id="jul24-inventory-import"
)
counts["success"] += 1
except DuplicateItemError:
counts["duplicate"] += 1
except Exception as exc:
counts["failed"] += 1
print(f"Failed to import item: {item}. Error: {exc}")
continue
print(f"Done: {counts=}")