Skip to content

Commit 1d1c7be

Browse files
committed
edit tutorial; add example for updating schema
1 parent 1359b46 commit 1d1c7be

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

docs/tutorials/python/json_schema.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ By the end of this tutorial, you will:
3535

3636
Derived annotations allow you to define default values for annotations based on schema rules, ensuring consistency and reducing manual input errors. As you can see here, you could use derived annotations to prescribe default annotation values. Please read more about derived annotations [here](https://help.synapse.org/docs/JSON-Schemas.3107291536.html#JSONSchemas-DerivedAnnotations).
3737

38-
Note: If you make an update to your schema, you can re-register your schema with the organization and give it a new version number:
39-
```python
40-
test_org.create_json_schema(new_schema_body, your_schema_name, "0.0.2")
41-
```
4238

4339
## 3. Try Create Test Organization and JSON Schema if They Do Not Exist
4440
Next, try creating a test organization and register a schema if they do not already exist:
4541
```python
46-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=51-64}
42+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=52-65}
43+
```
44+
45+
Note: If you make an update to your schema, you can re-register your schema with the organization and give it a new version number:
46+
```python
47+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=68-90}
4748
```
4849

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

5455
```python
55-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=67-73}
56+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=94-100}
5657
```
5758

5859
<details class="example">
@@ -75,7 +76,7 @@ JSON schema was bound successfully. Please see details below:
7576
## 5. Retrieve the Bound Schema
7677
Next, we can retrieve the bound schema:
7778
```python
78-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=76-78}
79+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=103-105}
7980
```
8081

8182
<details class="example">
@@ -104,12 +105,12 @@ JSON Schema was retrieved successfully. Please see details below:
104105
## 6. Add Invalid Annotations to the Folder and Store, and Validate the Folder against the Schema
105106
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.
106107
```python
107-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=70-76}
108+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=108-112}
108109
```
109110

110111
Try validating the folder. You should be able to see messages related to invalid annotations.
111112
```python
112-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=81-91}
113+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=116-118}
113114
```
114115

115116

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

146147
Try creating a test file locally and store the file in the folder that we created earlier. Then, try adding invalid annotations to a file. This step demonstrates how the files inside a folder also inherit the schema from the parent entity.
147148
```python
148-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=95-121}
149+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=122-148}
149150
```
150151

151152
You could then use `get_schema_validation_statistics` to get information such as the number of children with invalid annotations inside a container.
152153
```python
153-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=123-125}
154+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=150-152}
154155
```
155156

156157

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

170171
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.
171172
```python
172-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=127-130}
173+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=154-159}
173174
```
174175

175176
<details class="example">

docs/tutorials/python/tutorial_scripts/json_schema.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# 2. Take a look at the constants and structure of the JSON schema
2323
ORG_NAME = "myUniqueAlzheimersResearchOrgTurtorial"
2424
VERSION = "0.0.1"
25+
NEW_VERSION = "0.0.2"
2526
SCHEMA_NAME = "clinicalObservations"
2627

2728
title = "Alzheimer's Clinical Observation Schema"
@@ -49,17 +50,45 @@
4950

5051
# 3. Try create test organization and json schema if they do not exist
5152
js = syn.service("json_schema")
52-
try:
53-
my_test_org = js.JsonSchemaOrganization(ORG_NAME)
54-
except SynapseHTTPError as e:
55-
if e.response.status_code == 404:
56-
my_test_org = js.create_organization(ORG_NAME)
57-
58-
try:
59-
my_test_schema = my_test_org.get_json_schema(SCHEMA_NAME)
60-
except SynapseHTTPError as e:
61-
if e.response.status_code == 404:
62-
my_test_schema = my_test_org.create_json_schema(schema, SCHEMA_NAME, VERSION)
53+
all_orgs = js.list_organizations()
54+
for org in all_orgs:
55+
if org["name"] == ORG_NAME:
56+
print(f"Organization {ORG_NAME} already exists.")
57+
break
58+
else:
59+
print(f"Creating organization {ORG_NAME}.")
60+
js.create_organization(ORG_NAME)
61+
62+
my_test_org = js.JsonSchemaOrganization(ORG_NAME)
63+
test_schema = my_test_org.get_json_schema(SCHEMA_NAME)
64+
if not test_schema:
65+
test_schema = my_test_org.create_json_schema(schema, SCHEMA_NAME, VERSION)
66+
67+
# If you want to make an update, you can re-register your schema with the organization:
68+
updated_schema = {
69+
"$schema": "http://json-schema.org/draft-07/schema#",
70+
"$id": "https://example.com/schema/alzheimers_observation.json",
71+
"title": "my new title",
72+
"type": "object",
73+
"properties": {
74+
"patient_id": {
75+
"type": "string",
76+
"description": "A unique identifier for the patient",
77+
},
78+
"cognitive_score": {
79+
"type": "integer",
80+
"description": "Quantitative cognitive function score",
81+
},
82+
"updated_field": {
83+
"type": "string",
84+
"description": "Updated description for the field",
85+
},
86+
},
87+
}
88+
new_test_schema = my_test_org.create_json_schema(
89+
updated_schema, SCHEMA_NAME, NEW_VERSION
90+
)
91+
6392

6493
# 4. Bind the JSON schema to the folder
6594
schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION
@@ -122,7 +151,9 @@ def create_random_file(
122151
print("Validation statistics were retrieved successfully. Please see details below:")
123152
pprint(vars(validation_statistics))
124153

125-
invalid_validation = test_folder.get_invalid_validation()
154+
invalid_validation = invalid_results = test_folder.get_invalid_validation(
155+
synapse_client=syn
156+
)
126157
for child in invalid_validation:
127158
print("See details of validation results: ")
128159
pprint(vars(child))

0 commit comments

Comments
 (0)