From f6608223410df6c57195473e70e65914d35dfbcd Mon Sep 17 00:00:00 2001 From: "o.lyttleton@ucl.ac.uk" Date: Thu, 19 Jun 2025 09:56:32 +0100 Subject: [PATCH 1/5] Simplified example code for removing elements from items --- examples/remove_elements_from_items.py | 47 +++++++++++--------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/examples/remove_elements_from_items.py b/examples/remove_elements_from_items.py index dfe8bb9..e88bec9 100644 --- a/examples/remove_elements_from_items.py +++ b/examples/remove_elements_from_items.py @@ -32,15 +32,11 @@ # use the most up to date version of the study/series! search_sets = [{"agencyId": "uk.cls.mcs", "identifier": "0d8a7220-c61b-4542-967d-a40cb5aca430", - "version": "60"}] + "version": "58"}] -allItemsInOneQuery = C.search_items([], SearchSets=search_sets)['Results'] +studies=C.search_items([C.item_code('Study')], SearchSets=search_sets)['Results'] -studies = [x for x in allItemsInOneQuery if x['ItemType'] - == C.item_code('Study')] - -dataCollections = [x for x in allItemsInOneQuery if x['ItemType'] - == C.item_code('Data Collection')] +dataCollections=C.search_items([C.item_code('Data Collection')], SearchSets=search_sets)['Results'] dataCollectionItems = [] updatedDataCollections = [] @@ -96,10 +92,12 @@ # # Execute this code by making sure you have defined the 'validate_removal_of_references' and # 'count_elements_in_items' methods in your Python interpreter environment (e.g. by copying -# and pasting the methods code below into your Python interpreter) and typing: +# and pasting the methods code below into your Python interpreter) and typing commands such as +# the one below, which checks that the 'InstrumentReference' elements that were present in the +# objects in the dataCollectionItems array are not present in the objects in the +# updatedDataCollections array: # -# validate_removal_of_references(dataCollectionItems, updatedDataCollections, studyItems, -# updatedStudies) +# validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") def count_elements_in_items(items, elementTagname): """Given a list of items and the tag name of an element, this function counts @@ -109,21 +107,14 @@ def count_elements_in_items(items, elementTagname): elementCount += len(get_elements_of_type(item, elementTagname)) return elementCount -def validate_removal_of_references(dataCollectionItems, - updatedDataCollections, studyItems, updatedStudies): - instrumentRefsBefore = count_elements_in_items(dataCollectionItems, "InstrumentReference") - print(f"Number of instrument references in data collections before removal: {instrumentRefsBefore}") - instrumentRefsAfter = count_elements_in_items([x['Item'] for x in updatedDataCollections], "InstrumentReference") - print(f"Number of instrument references in data collections after removal: {instrumentRefsAfter}") - questionSchemeRefsBefore = count_elements_in_items(dataCollectionItems, "QuestionSchemeReference") - print(f"Number of question scheme references in data collections before removal: {questionSchemeRefsBefore}") - questionSchemeRefsAfter = count_elements_in_items([x['Item'] for x in updatedDataCollections], "QuestionSchemeReference") - print(f"Number of question scheme references in data collections after removal: {questionSchemeRefsAfter}") - physicalInstanceRefsBefore = count_elements_in_items(studyItems, "PhysicalInstanceReference") - print(f"Number of physical instance references in studies before removal: {physicalInstanceRefsBefore}") - physicalInstanceRefsAfter = count_elements_in_items([x['Item'] for x in updatedStudies], "PhysicalInstanceReference") - print(f"Number of physical instance references in studies after removal: {physicalInstanceRefsAfter}") - requiredResourcePackagesBefore = count_elements_in_items(studyItems, "RequiredResourcePackages") - print(f"Number of required resource packages in studies before removal: {requiredResourcePackagesBefore}") - requiredResourcePackagesAfter = count_elements_in_items([x['Item'] for x in updatedStudies], "RequiredResourcePackages") - print(f"Number of required resource packages in studies after removal: {requiredResourcePackagesAfter}") \ No newline at end of file +validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") +validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") +validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") +validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") + +def validate_removal_of_references(itemsBefore, itemsAfter, referenceTagName): + referencesBefore = count_elements_in_items(itemsBefore, referenceTagName) + itemTypes = set([C.item_code_inv(x['ItemType']) for x in itemsAfter]) + print(f"Number of {referenceTagName} elements in {itemTypes} items before removal: {referencesBefore}") + referencesAfter = count_elements_in_items([x['Item'] for x in itemsAfter], "VariableReference") + print(f"Number of {referenceTagName} elements in {itemTypes} items after removal: {referencesAfter}") From cb77215bfbec1d12953e03f4009ab7b26120bc1a Mon Sep 17 00:00:00 2001 From: "o.lyttleton@ucl.ac.uk" Date: Thu, 19 Jun 2025 09:59:13 +0100 Subject: [PATCH 2/5] move code --- examples/remove_elements_from_items.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/remove_elements_from_items.py b/examples/remove_elements_from_items.py index e88bec9..ce13431 100644 --- a/examples/remove_elements_from_items.py +++ b/examples/remove_elements_from_items.py @@ -107,14 +107,14 @@ def count_elements_in_items(items, elementTagname): elementCount += len(get_elements_of_type(item, elementTagname)) return elementCount -validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") -validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") -validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") -validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") - def validate_removal_of_references(itemsBefore, itemsAfter, referenceTagName): referencesBefore = count_elements_in_items(itemsBefore, referenceTagName) itemTypes = set([C.item_code_inv(x['ItemType']) for x in itemsAfter]) print(f"Number of {referenceTagName} elements in {itemTypes} items before removal: {referencesBefore}") referencesAfter = count_elements_in_items([x['Item'] for x in itemsAfter], "VariableReference") print(f"Number of {referenceTagName} elements in {itemTypes} items after removal: {referencesAfter}") + +validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") +validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") +validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") +validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") From a837a09532acf6d80b27091ac210a5facb231873 Mon Sep 17 00:00:00 2001 From: "o.lyttleton@ucl.ac.uk" Date: Thu, 19 Jun 2025 10:59:07 +0100 Subject: [PATCH 3/5] address linter error in github --- examples/remove_elements_from_items.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/remove_elements_from_items.py b/examples/remove_elements_from_items.py index ce13431..0c36e2c 100644 --- a/examples/remove_elements_from_items.py +++ b/examples/remove_elements_from_items.py @@ -114,7 +114,10 @@ def validate_removal_of_references(itemsBefore, itemsAfter, referenceTagName): referencesAfter = count_elements_in_items([x['Item'] for x in itemsAfter], "VariableReference") print(f"Number of {referenceTagName} elements in {itemTypes} items after removal: {referencesAfter}") -validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") -validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") -validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") -validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") +# If you import the validate_removal_of_references method, you need to specify that it is in the +# examples package when you execute it... + +examples.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") +examples.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") +examples.validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") +examples.validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") From e348e2d15aad596200e6e20347c8f859a2bc5390 Mon Sep 17 00:00:00 2001 From: "o.lyttleton@ucl.ac.uk" Date: Thu, 19 Jun 2025 11:57:21 +0100 Subject: [PATCH 4/5] add package name to lines executing function --- examples/remove_elements_from_items.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/remove_elements_from_items.py b/examples/remove_elements_from_items.py index 0c36e2c..c47fece 100644 --- a/examples/remove_elements_from_items.py +++ b/examples/remove_elements_from_items.py @@ -12,7 +12,7 @@ 6. Verifying that the removal of the references has been successful. """ -from lib.utility import ( +from examples.lib.utility import ( update_repository, get_elements_of_type, remove_elements_from_item @@ -115,9 +115,9 @@ def validate_removal_of_references(itemsBefore, itemsAfter, referenceTagName): print(f"Number of {referenceTagName} elements in {itemTypes} items after removal: {referencesAfter}") # If you import the validate_removal_of_references method, you need to specify that it is in the -# examples package when you execute it... +# remove_elements_from_items package when you execute it... -examples.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") -examples.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") -examples.validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") -examples.validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") +remove_elements_from_items.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") +remove_elements_from_items.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") +remove_elements_from_items.validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") +remove_elements_from_items.validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") From d2eb1809afdfa492c270f3d7eac573e43cc4c62a Mon Sep 17 00:00:00 2001 From: "o.lyttleton@ucl.ac.uk" Date: Thu, 19 Jun 2025 12:05:59 +0100 Subject: [PATCH 5/5] Include instructions for how to run method as comments, not as actual code --- examples/remove_elements_from_items.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/examples/remove_elements_from_items.py b/examples/remove_elements_from_items.py index c47fece..f331c8d 100644 --- a/examples/remove_elements_from_items.py +++ b/examples/remove_elements_from_items.py @@ -92,12 +92,15 @@ # # Execute this code by making sure you have defined the 'validate_removal_of_references' and # 'count_elements_in_items' methods in your Python interpreter environment (e.g. by copying -# and pasting the methods code below into your Python interpreter) and typing commands such as -# the one below, which checks that the 'InstrumentReference' elements that were present in the -# objects in the dataCollectionItems array are not present in the objects in the +# and pasting the methods below into your Python interpreter) and typing commands such as +# the ones below, which check that, for example, the 'InstrumentReference' elements that were +# present in the objects in the dataCollectionItems array are not present in the objects in the # updatedDataCollections array: # # validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") +# validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") +# validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") +# validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages") def count_elements_in_items(items, elementTagname): """Given a list of items and the tag name of an element, this function counts @@ -113,11 +116,3 @@ def validate_removal_of_references(itemsBefore, itemsAfter, referenceTagName): print(f"Number of {referenceTagName} elements in {itemTypes} items before removal: {referencesBefore}") referencesAfter = count_elements_in_items([x['Item'] for x in itemsAfter], "VariableReference") print(f"Number of {referenceTagName} elements in {itemTypes} items after removal: {referencesAfter}") - -# If you import the validate_removal_of_references method, you need to specify that it is in the -# remove_elements_from_items package when you execute it... - -remove_elements_from_items.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "InstrumentReference") -remove_elements_from_items.validate_removal_of_references(dataCollectionItems, updatedDataCollections, "QuestionSchemeReference") -remove_elements_from_items.validate_removal_of_references(studyItems, updatedStudies, "PhysicalInstanceReference") -remove_elements_from_items.validate_removal_of_references(studyItems, updatedStudies, "RequiredResourcePackages")