Skip to content

Commit 7cdada6

Browse files
committed
Fix line references in JSON schema tutorial and update logging to use Synapse client logger
1 parent 97c2364 commit 7cdada6

File tree

2 files changed

+26
-49
lines changed

2 files changed

+26
-49
lines changed

docs/tutorials/python/json_schema.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ By the end of this tutorial, you will:
3030
## 2. Take a Look at the Constants and Structure of the JSON Schema
3131

3232
```python
33-
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=22-49}
33+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=21-49}
3434
```
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).
@@ -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=52-65}
42+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=51-65}
4343
```
4444

4545
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=68-99}
47+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=67-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=102-108}
56+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=101-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=111-113}
79+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=110-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=116-120}
108+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=115-119}
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=124-126}
113+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=123-125}
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=130-155}
149+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=129-134}
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=158-160}
154+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=137-141}
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=162-169}
173+
{!docs/tutorials/python/tutorial_scripts/json_schema.py!lines=143-146}
174174
```
175175

176176
<details class="example">

docs/tutorials/python/tutorial_scripts/json_schema.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import os
21
import time
32
from pprint import pprint
43

54
import synapseclient
5+
from synapseclient.core.utils import make_bogus_data_file
66
from synapseclient.models import File, Folder
77

88
# 1. Set up Synapse Python client and retrieve project
99
syn = synapseclient.Synapse()
1010
syn.login()
11-
client = synapseclient.Synapse().get_client(synapse_client=syn)
1211

1312
# Retrieve test project
1413
PROJECT_ID = syn.findEntityId(
@@ -19,7 +18,7 @@
1918
test_folder = Folder(name="clinical_data_folder", parent_id=PROJECT_ID).store()
2019

2120
# 2. Take a look at the constants and structure of the JSON schema
22-
ORG_NAME = "myUniqueAlzheimersResearchOrgTurtorial"
21+
ORG_NAME = "myUniqueAlzheimersResearchOrgTutorial"
2322
VERSION = "0.0.1"
2423
NEW_VERSION = "0.0.2"
2524

@@ -53,10 +52,10 @@
5352
all_orgs = js.list_organizations()
5453
for org in all_orgs:
5554
if org["name"] == ORG_NAME:
56-
client.logger.info(f"Organization {ORG_NAME} already exists.")
55+
syn.logger.info(f"Organization {ORG_NAME} already exists.")
5756
break
5857
else:
59-
client.logger.info(f"Creating organization {ORG_NAME}.")
58+
syn.logger.info(f"Creating organization {ORG_NAME}.")
6059
js.create_organization(ORG_NAME)
6160

6261
my_test_org = js.JsonSchemaOrganization(ORG_NAME)
@@ -92,7 +91,7 @@
9291
)
9392
except synapseclient.core.exceptions.SynapseHTTPError as e:
9493
if e.response.status_code == 400 and "already exists" in e.response.text:
95-
client.logger.warning(
94+
syn.logger.warning(
9695
f"Schema {SCHEMA_NAME} already exists. Please switch to use a new version number."
9796
)
9897
else:
@@ -101,69 +100,47 @@
101100
# 4. Bind the JSON schema to the folder
102101
schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION
103102
bound_schema = test_folder.bind_schema(
104-
json_schema_uri=schema_uri, synapse_client=syn, enable_derived_annotations=True
103+
json_schema_uri=schema_uri, enable_derived_annotations=True
105104
)
106105
json_schema_version_info = bound_schema.json_schema_version_info
107-
client.logger.info("JSON schema was bound successfully. Please see details below:")
106+
syn.logger.info("JSON schema was bound successfully. Please see details below:")
108107
pprint(vars(json_schema_version_info))
109108

110109
# 5. Retrieve the Bound Schema
111110
schema = test_folder.get_schema()
112-
client.logger.info("JSON Schema was retrieved successfully. Please see details below:")
111+
syn.logger.info("JSON Schema was retrieved successfully. Please see details below:")
113112
pprint(vars(schema))
114113

115114
# 6. Add Invalid Annotations to the Folder and Store
116115
test_folder.annotations = {
117116
"patient_id": "1234",
118117
"cognitive_score": "invalid str",
119118
}
120-
test_folder.store(synapse_client=syn)
119+
test_folder.store()
121120

122121
time.sleep(2)
123122

124123
validation_results = test_folder.validate_schema()
125-
client.logger.info("Validation was completed. Please see details below:")
124+
syn.logger.info("Validation was completed. Please see details below:")
126125
pprint(vars(validation_results))
127126

128127
# 7. Create a File with Invalid Annotations and Upload It
129128
# Then, view validation statistics and invalid validation results
130-
if not os.path.exists(os.path.expanduser("~/temp")):
131-
os.makedirs(os.path.expanduser("~/temp/testJSONSchemaFiles"), exist_ok=True)
132-
133-
name_of_file = "test_file.txt"
134-
path_to_file = os.path.join(
135-
os.path.expanduser("~/temp/testJSONSchemaFiles"), name_of_file
136-
)
137-
138-
139-
def create_random_file(
140-
path: str,
141-
) -> None:
142-
"""Create a random file with random data.
143-
144-
:param path: The path to create the file at.
145-
"""
146-
with open(path, "wb") as f:
147-
f.write(os.urandom(1))
148-
149-
150-
create_random_file(path_to_file)
129+
path_to_file = make_bogus_data_file(n=5)
151130

152131
annotations = {"patient_id": "123456", "cognitive_score": "invalid child str"}
153132

154133
child_file = File(path=path_to_file, parent_id=test_folder.id, annotations=annotations)
155-
child_file = child_file.store(synapse_client=syn)
134+
child_file = child_file.store()
156135
time.sleep(2)
157136

158-
validation_statistics = test_folder.get_schema_validation_statistics(synapse_client=syn)
159-
client.logger.info(
137+
validation_statistics = test_folder.get_schema_validation_statistics()
138+
syn.logger.info(
160139
"Validation statistics were retrieved successfully. Please see details below:"
161140
)
162141
pprint(vars(validation_statistics))
163142

164-
invalid_validation = invalid_results = test_folder.get_invalid_validation(
165-
synapse_client=syn
166-
)
143+
invalid_validation = invalid_results = test_folder.get_invalid_validation()
167144
for child in invalid_validation:
168-
client.logger.info("See details of validation results: ")
145+
syn.logger.info("See details of validation results: ")
169146
pprint(vars(child))

0 commit comments

Comments
 (0)