Skip to content

Commit 8ebae02

Browse files
Merge pull request openMetadataInitiative#81 from apdavison/improved-by-name
Improvements to the `by_name()` method; fixes openMetadataInitiative#69
2 parents 51144f9 + 5a40cb1 commit 8ebae02

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

pipeline/src/additional_methods/by_name.py.txt

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,52 @@
33
return [value for value in cls.__dict__.values() if isinstance(value, cls)]
44

55
@classmethod
6-
def by_name(cls, name):
6+
def by_name(
7+
cls,
8+
name: str,
9+
match: str = "equals",
10+
all: bool = False,
11+
):
12+
"""
13+
Search for instances in the openMINDS instance library based on their name.
14+
15+
This includes properties "name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation", and "synonyms".
16+
17+
Note that not all metadata classes have a name.
18+
19+
Args:
20+
name (str): a string to search for.
21+
match (str, optional): either "equals" (exact match - default) or "contains".
22+
all (bool, optional): Whether to return all objects that match the name, or only the first. Defaults to False.
23+
"""
24+
namelike_properties = ("name", "lookup_label", "family_name", "full_name", "short_name", "abbreviation")
725
if cls._instance_lookup is None:
826
cls._instance_lookup = {}
927
for instance in cls.instances():
10-
cls._instance_lookup[instance.name] = instance
11-
if instance.synonyms:
12-
for synonym in instance.synonyms:
13-
cls._instance_lookup[synonym] = instance
14-
return cls._instance_lookup[name]
28+
keys = []
29+
for prop_name in namelike_properties:
30+
if hasattr(instance, prop_name):
31+
keys.append(getattr(instance, prop_name))
32+
if hasattr(instance, "synonyms"):
33+
for synonym in instance.synonyms or []:
34+
keys.append(synonym)
35+
for key in keys:
36+
if key in cls._instance_lookup:
37+
cls._instance_lookup[key].append(instance)
38+
else:
39+
cls._instance_lookup[key] = [instance]
40+
if match == "equals":
41+
matches = cls._instance_lookup.get(name, None)
42+
elif match == "contains":
43+
matches = []
44+
for key, instances in cls._instance_lookup.items():
45+
if name in key:
46+
matches.extend(instances)
47+
else:
48+
raise ValueError("'match' must be either 'equals' or 'contains'")
49+
if all:
50+
return matches
51+
elif len(matches) > 0:
52+
return matches[0]
53+
else:
54+
return None

pipeline/tests/test_regressions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,34 @@ def test_issue0073b(om):
300300
ds1.is_variant_of = ds2
301301

302302
failures = ds1.validate()
303+
304+
305+
@pytest.mark.parametrize("om", [openminds.latest])
306+
def test_issue0069(om):
307+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/69
308+
# The License class has a classmethod "by_name()" which assumes License is a controlled term
309+
# (i.e., it has properties "name" and "synonyms").
310+
# However License does not have these properties, it has "short_name" and "full_name".
311+
312+
# Test with default arguments (single result, exact match)
313+
result = om.core.License.by_name("CC-BY-4.0")
314+
assert result.short_name == "CC-BY-4.0"
315+
316+
result = om.sands.ParcellationEntity.by_name("NODa,b")
317+
assert result.abbreviation == "NODa,b"
318+
319+
result = om.sands.CommonCoordinateSpace.by_name("MEBRAINS population-based monkey brain template")
320+
assert result.full_name == "MEBRAINS population-based monkey brain template"
321+
322+
assert om.controlled_terms.BiologicalOrder.by_name("rodents") == om.controlled_terms.BiologicalOrder.by_name("Rodentia") != None
323+
324+
# Test with "all=True"
325+
results = om.sands.BrainAtlasVersion.by_name("Julich-Brain Atlas", all=True)
326+
assert len(results) == 30
327+
assert all(r.short_name == "Julich-Brain Atlas" for r in results)
328+
assert len(set(r.id for r in results)) == len(results)
329+
330+
# Test with "match='contains'"
331+
results = om.core.License.by_name("Creative Commons", all=True, match="contains")
332+
assert len(results) == 7
333+
assert all("CC" in r.short_name for r in results)

0 commit comments

Comments
 (0)