|
8 | 8 | # 1. Set up Synapse Python client and retrieve project
|
9 | 9 | syn = synapseclient.Synapse()
|
10 | 10 | syn.login()
|
11 |
| - |
| 11 | +client = synapseclient.Synapse().get_client(synapse_client=syn) |
12 | 12 |
|
13 | 13 | # Retrieve test project
|
14 | 14 | PROJECT_ID = syn.findEntityId(
|
|
22 | 22 | ORG_NAME = "myUniqueAlzheimersResearchOrgTurtorial"
|
23 | 23 | VERSION = "0.0.1"
|
24 | 24 | NEW_VERSION = "0.0.2"
|
| 25 | + |
25 | 26 | SCHEMA_NAME = "clinicalObservations"
|
26 | 27 |
|
27 | 28 | title = "Alzheimer's Clinical Observation Schema"
|
|
52 | 53 | all_orgs = js.list_organizations()
|
53 | 54 | for org in all_orgs:
|
54 | 55 | if org["name"] == ORG_NAME:
|
55 |
| - print(f"Organization {ORG_NAME} already exists.") |
| 56 | + client.logger.info(f"Organization {ORG_NAME} already exists.") |
56 | 57 | break
|
57 | 58 | else:
|
58 |
| - print(f"Creating organization {ORG_NAME}.") |
| 59 | + client.logger.info(f"Creating organization {ORG_NAME}.") |
59 | 60 | js.create_organization(ORG_NAME)
|
60 | 61 |
|
61 | 62 | my_test_org = js.JsonSchemaOrganization(ORG_NAME)
|
|
84 | 85 | },
|
85 | 86 | },
|
86 | 87 | }
|
87 |
| -new_test_schema = my_test_org.create_json_schema( |
88 |
| - updated_schema, SCHEMA_NAME, NEW_VERSION |
89 |
| -) |
90 | 88 |
|
| 89 | +try: |
| 90 | + new_test_schema = my_test_org.create_json_schema( |
| 91 | + updated_schema, SCHEMA_NAME, VERSION |
| 92 | + ) |
| 93 | +except synapseclient.core.exceptions.SynapseHTTPError as e: |
| 94 | + if e.response.status_code == 400 and "already exists" in e.response.text: |
| 95 | + client.logger.warning( |
| 96 | + f"Schema {SCHEMA_NAME} already exists. Please switch to use a new version number." |
| 97 | + ) |
| 98 | + else: |
| 99 | + raise e |
91 | 100 |
|
92 | 101 | # 4. Bind the JSON schema to the folder
|
93 | 102 | schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION
|
94 | 103 | bound_schema = test_folder.bind_schema(
|
95 |
| - json_schema_uri=schema_uri, enable_derived_annotations=True |
| 104 | + json_schema_uri=schema_uri, synapse_client=syn, enable_derived_annotations=True |
96 | 105 | )
|
97 | 106 | json_schema_version_info = bound_schema.json_schema_version_info
|
98 |
| -print("JSON schema was bound successfully. Please see details below:") |
| 107 | +client.logger.info("JSON schema was bound successfully. Please see details below:") |
99 | 108 | pprint(vars(json_schema_version_info))
|
100 | 109 |
|
101 | 110 | # 5. Retrieve the Bound Schema
|
102 | 111 | schema = test_folder.get_schema()
|
103 |
| -print("JSON Schema was retrieved successfully. Please see details below:") |
| 112 | +client.logger.info("JSON Schema was retrieved successfully. Please see details below:") |
104 | 113 | pprint(vars(schema))
|
105 | 114 |
|
106 | 115 | # 6. Add Invalid Annotations to the Folder and Store
|
107 | 116 | test_folder.annotations = {
|
108 | 117 | "patient_id": "1234",
|
109 | 118 | "cognitive_score": "invalid str",
|
110 | 119 | }
|
111 |
| -test_folder.store() |
| 120 | +test_folder.store(synapse_client=syn) |
112 | 121 |
|
113 | 122 | time.sleep(2)
|
114 | 123 |
|
115 | 124 | validation_results = test_folder.validate_schema()
|
116 |
| -print("Validation was completed. Please see details below:") |
| 125 | +client.logger.info("Validation was completed. Please see details below:") |
117 | 126 | pprint(vars(validation_results))
|
118 | 127 |
|
119 | 128 | # 7. Create a File with Invalid Annotations and Upload It
|
@@ -143,16 +152,18 @@ def create_random_file(
|
143 | 152 | annotations = {"patient_id": "123456", "cognitive_score": "invalid child str"}
|
144 | 153 |
|
145 | 154 | child_file = File(path=path_to_file, parent_id=test_folder.id, annotations=annotations)
|
146 |
| -child_file = child_file.store() |
| 155 | +child_file = child_file.store(synapse_client=syn) |
147 | 156 | time.sleep(2)
|
148 | 157 |
|
149 |
| -validation_statistics = test_folder.get_schema_validation_statistics() |
150 |
| -print("Validation statistics were retrieved successfully. Please see details below:") |
| 158 | +validation_statistics = test_folder.get_schema_validation_statistics(synapse_client=syn) |
| 159 | +client.logger.info( |
| 160 | + "Validation statistics were retrieved successfully. Please see details below:" |
| 161 | +) |
151 | 162 | pprint(vars(validation_statistics))
|
152 | 163 |
|
153 | 164 | invalid_validation = invalid_results = test_folder.get_invalid_validation(
|
154 | 165 | synapse_client=syn
|
155 | 166 | )
|
156 | 167 | for child in invalid_validation:
|
157 |
| - print("See details of validation results: ") |
| 168 | + client.logger.info("See details of validation results: ") |
158 | 169 | pprint(vars(child))
|
0 commit comments