Skip to content

Commit 164f6e7

Browse files
committed
switch to use logger info; update tutorial script to handle creation of updated schema when the schema already exist
1 parent 573f00a commit 164f6e7

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

docs/tutorials/python/json_schema.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ Derived annotations allow you to define default values for annotations based on
3939
## 3. Try Create Test Organization and JSON Schema if They Do Not Exist
4040
Next, try creating a test organization and register a schema if they do not already exist:
4141
```python
42-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=51-65}
42+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=52-65}
4343
```
4444

45-
Note: If you update your schema, you can re-register it with the organization and assign a new version number to reflect the changes:
45+
Note: If you update your schema, you can re-register it with the organization by assigning a new version number to reflect the changes. Synapse does not allow re-creating a schema with the same version number, so please ensure that each schema version within an organization is unique:
4646
```python
47-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=67-90}
47+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=68-99}
4848
```
4949

5050
## 4. Bind the JSON Schema to the Folder
@@ -53,7 +53,7 @@ After creating the organization, you can now bind your json schema to a test fol
5353
When you bind the schema, you may also include the boolean property `enable_derived_annotations` to have Synapse automatically calculate derived annotations based on the schema:
5454

5555
```python
56-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=93-100}
56+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=102-108}
5757
```
5858

5959
<details class="example">
@@ -76,7 +76,7 @@ JSON schema was bound successfully. Please see details below:
7676
## 5. Retrieve the Bound Schema
7777
Next, we can retrieve the bound schema:
7878
```python
79-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=102-105}
79+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=111-113}
8080
```
8181

8282
<details class="example">
@@ -105,12 +105,12 @@ JSON Schema was retrieved successfully. Please see details below:
105105
## 6. Add Invalid Annotations to the Folder and Store, and Validate the Folder against the Schema
106106
Try adding invalid annotations to your folder: This step and the step below demonstrate how the system handles invalid annotations and how the schema validation process works.
107107
```python
108-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=107-111}
108+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=116-120}
109109
```
110110

111111
Try validating the folder. You should be able to see messages related to invalid annotations.
112112
```python
113-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=115-117}
113+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=124-126}
114114
```
115115

116116

@@ -146,12 +146,12 @@ This step is only relevant for container entities, such as a folder or a project
146146

147147
Try creating a test file locally and store the file in the folder that we created earlier. Then, try adding invalid annotations to that file. This step demonstrates how the files inside a folder also inherit the schema from the parent entity.
148148
```python
149-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=121-146}
149+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=130-155}
150150
```
151151

152152
You could then use `get_schema_validation_statistics` to get information such as the number of children with invalid annotations inside a container.
153153
```python
154-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=149-151}
154+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=158-160}
155155
```
156156

157157

@@ -170,7 +170,7 @@ Validation statistics were retrieved successfully. Please see details below:
170170

171171
You could also use `get_invalid_validation` to see more detailed results of all the children inside a container, which includes all validation messages and validation exception details.
172172
```python
173-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=153-158}
173+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=162-169}
174174
```
175175

176176
<details class="example">

docs/tutorials/python/tutorial_scripts/json_schema.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# 1. Set up Synapse Python client and retrieve project
99
syn = synapseclient.Synapse()
1010
syn.login()
11-
11+
client = synapseclient.Synapse().get_client(synapse_client=syn)
1212

1313
# Retrieve test project
1414
PROJECT_ID = syn.findEntityId(
@@ -22,6 +22,7 @@
2222
ORG_NAME = "myUniqueAlzheimersResearchOrgTurtorial"
2323
VERSION = "0.0.1"
2424
NEW_VERSION = "0.0.2"
25+
2526
SCHEMA_NAME = "clinicalObservations"
2627

2728
title = "Alzheimer's Clinical Observation Schema"
@@ -52,10 +53,10 @@
5253
all_orgs = js.list_organizations()
5354
for org in all_orgs:
5455
if org["name"] == ORG_NAME:
55-
print(f"Organization {ORG_NAME} already exists.")
56+
client.logger.info(f"Organization {ORG_NAME} already exists.")
5657
break
5758
else:
58-
print(f"Creating organization {ORG_NAME}.")
59+
client.logger.info(f"Creating organization {ORG_NAME}.")
5960
js.create_organization(ORG_NAME)
6061

6162
my_test_org = js.JsonSchemaOrganization(ORG_NAME)
@@ -84,36 +85,44 @@
8485
},
8586
},
8687
}
87-
new_test_schema = my_test_org.create_json_schema(
88-
updated_schema, SCHEMA_NAME, NEW_VERSION
89-
)
9088

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
91100

92101
# 4. Bind the JSON schema to the folder
93102
schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION
94103
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
96105
)
97106
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:")
99108
pprint(vars(json_schema_version_info))
100109

101110
# 5. Retrieve the Bound Schema
102111
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:")
104113
pprint(vars(schema))
105114

106115
# 6. Add Invalid Annotations to the Folder and Store
107116
test_folder.annotations = {
108117
"patient_id": "1234",
109118
"cognitive_score": "invalid str",
110119
}
111-
test_folder.store()
120+
test_folder.store(synapse_client=syn)
112121

113122
time.sleep(2)
114123

115124
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:")
117126
pprint(vars(validation_results))
118127

119128
# 7. Create a File with Invalid Annotations and Upload It
@@ -143,16 +152,18 @@ def create_random_file(
143152
annotations = {"patient_id": "123456", "cognitive_score": "invalid child str"}
144153

145154
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)
147156
time.sleep(2)
148157

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+
)
151162
pprint(vars(validation_statistics))
152163

153164
invalid_validation = invalid_results = test_folder.get_invalid_validation(
154165
synapse_client=syn
155166
)
156167
for child in invalid_validation:
157-
print("See details of validation results: ")
168+
client.logger.info("See details of validation results: ")
158169
pprint(vars(child))

0 commit comments

Comments
 (0)