|
1 | | -import os |
2 | 1 | import time |
3 | 2 | from pprint import pprint |
4 | 3 |
|
5 | 4 | import synapseclient |
| 5 | +from synapseclient.core.utils import make_bogus_data_file |
6 | 6 | from synapseclient.models import File, Folder |
7 | 7 |
|
8 | 8 | # 1. Set up Synapse Python client and retrieve project |
9 | 9 | syn = synapseclient.Synapse() |
10 | 10 | syn.login() |
11 | | -client = synapseclient.Synapse().get_client(synapse_client=syn) |
12 | 11 |
|
13 | 12 | # Retrieve test project |
14 | 13 | PROJECT_ID = syn.findEntityId( |
|
19 | 18 | test_folder = Folder(name="clinical_data_folder", parent_id=PROJECT_ID).store() |
20 | 19 |
|
21 | 20 | # 2. Take a look at the constants and structure of the JSON schema |
22 | | -ORG_NAME = "myUniqueAlzheimersResearchOrgTurtorial" |
| 21 | +ORG_NAME = "myUniqueAlzheimersResearchOrgTutorial" |
23 | 22 | VERSION = "0.0.1" |
24 | 23 | NEW_VERSION = "0.0.2" |
25 | 24 |
|
|
53 | 52 | all_orgs = js.list_organizations() |
54 | 53 | for org in all_orgs: |
55 | 54 | 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.") |
57 | 56 | break |
58 | 57 | else: |
59 | | - client.logger.info(f"Creating organization {ORG_NAME}.") |
| 58 | + syn.logger.info(f"Creating organization {ORG_NAME}.") |
60 | 59 | js.create_organization(ORG_NAME) |
61 | 60 |
|
62 | 61 | my_test_org = js.JsonSchemaOrganization(ORG_NAME) |
|
92 | 91 | ) |
93 | 92 | except synapseclient.core.exceptions.SynapseHTTPError as e: |
94 | 93 | if e.response.status_code == 400 and "already exists" in e.response.text: |
95 | | - client.logger.warning( |
| 94 | + syn.logger.warning( |
96 | 95 | f"Schema {SCHEMA_NAME} already exists. Please switch to use a new version number." |
97 | 96 | ) |
98 | 97 | else: |
|
101 | 100 | # 4. Bind the JSON schema to the folder |
102 | 101 | schema_uri = ORG_NAME + "-" + SCHEMA_NAME + "-" + VERSION |
103 | 102 | 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 |
105 | 104 | ) |
106 | 105 | 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:") |
108 | 107 | pprint(vars(json_schema_version_info)) |
109 | 108 |
|
110 | 109 | # 5. Retrieve the Bound Schema |
111 | 110 | 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:") |
113 | 112 | pprint(vars(schema)) |
114 | 113 |
|
115 | 114 | # 6. Add Invalid Annotations to the Folder and Store |
116 | 115 | test_folder.annotations = { |
117 | 116 | "patient_id": "1234", |
118 | 117 | "cognitive_score": "invalid str", |
119 | 118 | } |
120 | | -test_folder.store(synapse_client=syn) |
| 119 | +test_folder.store() |
121 | 120 |
|
122 | 121 | time.sleep(2) |
123 | 122 |
|
124 | 123 | 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:") |
126 | 125 | pprint(vars(validation_results)) |
127 | 126 |
|
128 | 127 | # 7. Create a File with Invalid Annotations and Upload It |
129 | 128 | # 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) |
151 | 130 |
|
152 | 131 | annotations = {"patient_id": "123456", "cognitive_score": "invalid child str"} |
153 | 132 |
|
154 | 133 | 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() |
156 | 135 | time.sleep(2) |
157 | 136 |
|
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( |
160 | 139 | "Validation statistics were retrieved successfully. Please see details below:" |
161 | 140 | ) |
162 | 141 | pprint(vars(validation_statistics)) |
163 | 142 |
|
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() |
167 | 144 | for child in invalid_validation: |
168 | | - client.logger.info("See details of validation results: ") |
| 145 | + syn.logger.info("See details of validation results: ") |
169 | 146 | pprint(vars(child)) |
0 commit comments