Skip to content

Commit 6a48dd3

Browse files
committed
PNE-6482 Support default_labels in MaterialSpec constructor
This update is analagous to the one in #989 The associated utility function and test have been moved to a shared module, and the documentation has been updated
1 parent f98c68c commit 6a48dd3

File tree

6 files changed

+72
-62
lines changed

6 files changed

+72
-62
lines changed

src/citrine/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.18.0"
1+
__version__ = "3.19.0"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Optional
2+
3+
from citrine.resources.data_concepts import CITRINE_TAG_PREFIX
4+
5+
_CITRINE_DEFAULT_LABEL_PREFIX = f'{CITRINE_TAG_PREFIX}::mat_label'
6+
7+
8+
def _inject_default_label_tags(
9+
original_tags: Optional[List[str]], default_labels: Optional[List[str]]
10+
) -> Optional[List[str]]:
11+
if default_labels is None:
12+
all_tags = original_tags
13+
else:
14+
labels_as_tags = [
15+
f"{_CITRINE_DEFAULT_LABEL_PREFIX}::{label}" for label in default_labels
16+
]
17+
if original_tags is None:
18+
all_tags = labels_as_tags
19+
else:
20+
all_tags = list(original_tags) + labels_as_tags
21+
return all_tags

src/citrine/resources/material_run.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from citrine._serialization.properties import Optional as PropertyOptional
77
from citrine._serialization.properties import String, LinkOrElse
88
from citrine._utils.functions import format_escaped_url
9+
from citrine.resources._default_labels import _inject_default_label_tags
910
from citrine.resources.data_concepts import _make_link_by_uid
10-
from citrine.resources.data_concepts import CITRINE_TAG_PREFIX
1111
from citrine.resources.material_spec import MaterialSpecCollection
1212
from citrine.resources.object_runs import ObjectRun, ObjectRunCollection
1313
from gemd.entity.file_link import FileLink
@@ -53,8 +53,6 @@ class MaterialRun(
5353
default_labels: List[str], optional
5454
An optional set of default labels to apply to this material run.
5555
Default labels are used to:
56-
- Populate labels on the ingredient run, if none are explicitly
57-
specified, when the material run is later used as an ingredient
5856
- Marking the material run as a potential replacement ingredient for a
5957
particular label when generating new candidates using a
6058
design space. Note that during design, default labels are only applicable
@@ -229,22 +227,3 @@ def list_by_template(self,
229227
specs = spec_collection.list_by_template(uid=_make_link_by_uid(uid))
230228
return (run for runs in (self.list_by_spec(spec) for spec in specs)
231229
for run in runs)
232-
233-
234-
_CITRINE_DEFAULT_LABEL_PREFIX = f'{CITRINE_TAG_PREFIX}::mat_label'
235-
236-
237-
def _inject_default_label_tags(
238-
original_tags: Optional[List[str]], default_labels: Optional[List[str]]
239-
) -> Optional[List[str]]:
240-
if default_labels is None:
241-
all_tags = original_tags
242-
else:
243-
labels_as_tags = [
244-
f"{_CITRINE_DEFAULT_LABEL_PREFIX}::{label}" for label in default_labels
245-
]
246-
if original_tags is None:
247-
all_tags = labels_as_tags
248-
else:
249-
all_tags = list(original_tags) + labels_as_tags
250-
return all_tags

src/citrine/resources/material_spec.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from citrine._serialization.properties import List as PropertyList
77
from citrine._serialization.properties import Optional as PropertyOptional
88
from citrine._serialization.properties import String, LinkOrElse, Object
9+
from citrine.resources._default_labels import _inject_default_label_tags
910
from citrine.resources.object_specs import ObjectSpec, ObjectSpecCollection
1011
from gemd.entity.attribute.property_and_conditions import PropertyAndConditions
1112
from gemd.entity.file_link import FileLink
@@ -47,6 +48,11 @@ class MaterialSpec(
4748
A template bounding the valid values for this material's properties.
4849
file_links: List[FileLink], optional
4950
Links to associated files, with resource paths into the files API.
51+
default_labels: List[str], optional
52+
An optional set of default labels to apply to this material spec.
53+
Default labels are used to:
54+
- Populate labels on the ingredient spec, if none are explicitly
55+
specified, when the material spec is later used in an ingredient spec
5056
5157
"""
5258

@@ -75,12 +81,14 @@ def __init__(self,
7581
process: Optional[GEMDProcessSpec] = None,
7682
properties: Optional[List[PropertyAndConditions]] = None,
7783
template: Optional[GEMDMaterialTemplate] = None,
78-
file_links: Optional[List[FileLink]] = None):
84+
file_links: Optional[List[FileLink]] = None,
85+
default_labels: Optional[List[str]] = None):
7986
if uids is None:
8087
uids = dict()
88+
all_tags = _inject_default_label_tags(tags, default_labels)
8189
super(ObjectSpec, self).__init__()
8290
GEMDMaterialSpec.__init__(self, name=name, uids=uids,
83-
tags=tags, process=process, properties=properties,
91+
tags=all_tags, process=process, properties=properties,
8492
template=template, file_links=file_links, notes=notes)
8593

8694
def __str__(self):
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
3+
from citrine.resources._default_labels import _inject_default_label_tags
4+
5+
@pytest.mark.parametrize(
6+
"original_tags, default_labels, expected",
7+
[
8+
(None, None, None),
9+
(None, [], []),
10+
([], None, []),
11+
([], [], []),
12+
(
13+
None,
14+
["label 0", "label 1"],
15+
["citr_auto::mat_label::label 0", "citr_auto::mat_label::label 1"],
16+
),
17+
(
18+
[],
19+
["label 0", "label 1"],
20+
["citr_auto::mat_label::label 0", "citr_auto::mat_label::label 1"],
21+
),
22+
(["alpha", "beta", "gamma"], None, ["alpha", "beta", "gamma"]),
23+
(["alpha", "beta", "gamma"], [], ["alpha", "beta", "gamma"]),
24+
(
25+
["alpha", "beta", "gamma"],
26+
["label 0", "label 1"],
27+
[
28+
"alpha",
29+
"beta",
30+
"gamma",
31+
"citr_auto::mat_label::label 0",
32+
"citr_auto::mat_label::label 1",
33+
],
34+
),
35+
],
36+
)
37+
def test_inject_default_label_tags(original_tags, default_labels, expected):
38+
result = _inject_default_label_tags(original_tags, default_labels)
39+
assert result == expected

tests/resources/test_material_run.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,43 +55,6 @@ def test_invalid_collection_construction():
5555
session=session)
5656

5757

58-
@pytest.mark.parametrize(
59-
"original_tags, default_labels, expected",
60-
[
61-
(None, None, None),
62-
(None, [], []),
63-
([], None, []),
64-
([], [], []),
65-
(
66-
None,
67-
["label 0", "label 1"],
68-
["citr_auto::mat_label::label 0", "citr_auto::mat_label::label 1"],
69-
),
70-
(
71-
[],
72-
["label 0", "label 1"],
73-
["citr_auto::mat_label::label 0", "citr_auto::mat_label::label 1"],
74-
),
75-
(["alpha", "beta", "gamma"], None, ["alpha", "beta", "gamma"]),
76-
(["alpha", "beta", "gamma"], [], ["alpha", "beta", "gamma"]),
77-
(
78-
["alpha", "beta", "gamma"],
79-
["label 0", "label 1"],
80-
[
81-
"alpha",
82-
"beta",
83-
"gamma",
84-
"citr_auto::mat_label::label 0",
85-
"citr_auto::mat_label::label 1",
86-
],
87-
),
88-
],
89-
)
90-
def test_inject_default_label_tags(original_tags, default_labels, expected):
91-
result = _inject_default_label_tags(original_tags, default_labels)
92-
assert result == expected
93-
94-
9558
def test_register_material_run(collection, session):
9659
# Given
9760
session.set_response(MaterialRunDataFactory(name='Test MR 123'))

0 commit comments

Comments
 (0)