Skip to content

Commit 872306c

Browse files
committed
Add synthesis_products field with self-loop by default
1 parent a2f51cb commit 872306c

File tree

3 files changed

+11
-47
lines changed

3 files changed

+11
-47
lines changed

pydatalab/src/pydatalab/models/samples.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ class Sample(Item, HasSynthesisInfo):
1111

1212
chemform: str | None = Field(example=["Na3P", "LiNiO2@C"])
1313
"""A string representation of the chemical formula or composition associated with this sample."""
14+

pydatalab/src/pydatalab/models/traits.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,57 +86,20 @@ class HasSynthesisInfo(BaseModel):
8686
synthesis_constituents: list[Constituent] = Field([])
8787
"""A list of references to constituent materials giving the amount and relevant inlined details of consituent items."""
8888

89+
synthesis_products: list[Constituent] = Field([])
90+
"""A list of references to constituent materials giving the amount and relevant inlined details of relevant sythesis products."""
91+
8992
synthesis_description: str | None = None
9093
"""Free-text details of the procedure applied to synthesise the sample"""
9194

9295
@root_validator
93-
def add_missing_synthesis_relationships(cls, values):
94-
"""Add any missing sample synthesis constituents to parent relationships"""
95-
from pydatalab.models.relationships import RelationshipType, TypedRelationship
96-
97-
constituents_set = set()
98-
if values.get("synthesis_constituents") is not None:
99-
existing_parent_relationship_ids = set()
100-
if values.get("relationships") is not None:
101-
existing_parent_relationship_ids = {
102-
relationship.refcode or relationship.item_id
103-
for relationship in values["relationships"]
104-
if relationship.relation == RelationshipType.PARENT
105-
}
106-
else:
107-
values["relationships"] = []
108-
109-
for constituent in values.get("synthesis_constituents", []):
110-
# If this is an inline relationship, just skip it
111-
if isinstance(constituent.item, InlineSubstance):
112-
continue
113-
114-
constituent_id = constituent.item.refcode or constituent.item.item_id
115-
116-
if constituent_id not in existing_parent_relationship_ids:
117-
relationship = TypedRelationship(
118-
relation=RelationshipType.PARENT,
119-
refcode=constituent.item.refcode,
120-
item_id=constituent.item.item_id,
121-
type=constituent.item.type,
122-
description="Is a constituent of",
123-
)
124-
values["relationships"].append(relationship)
125-
126-
# Accumulate all constituent IDs in a set to filter those that have been deleted
127-
constituents_set.add(constituent_id)
128-
129-
# Finally, filter out any parent relationships with item that were removed
130-
# from the synthesis constituents
131-
values["relationships"] = [
132-
rel
133-
for rel in values["relationships"]
134-
if not (
135-
(rel.refcode or rel.item_id) not in constituents_set
136-
and rel.relation == RelationshipType.PARENT
137-
and rel.type in ("samples", "starting_materials")
96+
def add_self_product(cls, values):
97+
if not values.get("synthesis_products"):
98+
values["synthesis_products"].append(
99+
Constituent(
100+
quantity=None, item={"type": values["type"], "refcode": values["refcode"]}
101+
)
138102
)
139-
]
140103

141104
return values
142105

pydatalab/src/pydatalab/routes/v0_1/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def get_item_data(
966966
)
967967

968968
# Must be exported to JSON first to apply the custom pydantic JSON encoders
969-
return_dict = json.loads(doc.json(exclude_unset=True))
969+
return_dict = json.loads(doc.json())
970970

971971
if item_id is None:
972972
item_id = return_dict["item_id"]

0 commit comments

Comments
 (0)