-
Notifications
You must be signed in to change notification settings - Fork 73
Add Create JSON Schema tutorial #1271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6238fb9
added new tutorial
andrewelamb 340eec0
rename file
andrewelamb 5d6c107
fixes to tutorial
andrewelamb a27e7eb
fixes to tutorial
andrewelamb 8d1bea5
renamed files
andrewelamb 8557276
added tutorial to yaml file
andrewelamb e338c9f
add data model assumption
andrewelamb 745976e
used different function for tutorial
andrewelamb 9b7bd9d
remove links
andrewelamb 8bc8ee1
Update docs/tutorials/python/tutorial_scripts/schema_operations.py
andrewelamb d0d9c0d
Update docs/tutorials/python/schema_operations.md
andrewelamb 8355913
added data model links
andrewelamb 63234bb
fix typos
andrewelamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| JSON Schema is a tool used to validate data. In Synapse, JSON Schemas can be used to validate the metadata applied to an entity such as project, file, folder, table, or view, including the [annotations](https://help.synapse.org/docs/Annotating-Data-With-Metadata.2667708522.html) applied to it. To learn more about JSON Schemas, check out [JSON-Schema.org](https://json-schema.org/). | ||
|
|
||
| Synapse supports a subset of features from [json-schema-draft-07](https://json-schema.org/draft-07). To see the list of features currently supported, see the [JSON Schema object definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html) from Synapse's REST API Documentation. | ||
|
|
||
| In this tutorial, you will learn how to create these JSON Schema using an existing data-model. | ||
andrewelamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Tutorial Purpose | ||
| You will create a JSON schema using your data model. | ||
|
|
||
| ## Prerequisites | ||
| * You have a working [installation](../installation.md) of the Synapse Python Client. | ||
andrewelamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * You have a data-model, see [here](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema) | ||
|
|
||
|
|
||
| ## 1. Set up your variables | ||
|
|
||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=10-14} | ||
| ``` | ||
|
|
||
| To create a JSON Schema you need a data-model, and the data-type you want to create. The data-model must be in either CSV or JSON-LD form. See [here](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema) for instructions on how to crate a data-model.The data-type must exist in your data-model. | ||
andrewelamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## 2. Create the JSON Schema | ||
| Try creating the JSON Schema | ||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=16-24} | ||
| ``` | ||
| You should see the JSON Schema for the datatype you selected printed. | ||
|
|
||
|
|
||
| ## Source Code for this Tutorial | ||
|
|
||
| <details class="quote"> | ||
| <summary>Click to show me</summary> | ||
|
|
||
| ```python | ||
| {!docs/tutorials/python/tutorial_scripts/schema_operations.py!} | ||
| ``` | ||
| </details> | ||
|
|
||
|
|
||
| ## Reference | ||
| - [Data models](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema) | ||
| - [JSON Schema Object Definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html) | ||
| - [JSON Schema Draft 7](https://json-schema.org/draft-07) | ||
| - [JSON-Schema.org](https://json-schema.org./) | ||
andrewelamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
24 changes: 24 additions & 0 deletions
24
docs/tutorials/python/tutorial_scripts/schema_operations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import logging | ||
|
|
||
| from synapseclient.extensions.curator.schema_generation import ( | ||
| DataModelGraph, | ||
| DataModelGraphExplorer, | ||
| DataModelParser, | ||
| create_json_schema, | ||
| ) | ||
|
|
||
| LOGGER = logging.Logger("create_json_schema") | ||
| # Put the path to your data model here, either CSV or JSONLD format | ||
| DATA_MODEL_PATH = "tests/unit/synapseclient/extensions/schema_files/example.model.csv" | ||
| # Put the name of the datatype in your data-model you want to create here | ||
| DATATYPE = "Patient" | ||
|
|
||
| data_model_parser = DataModelParser(path_to_data_model=DATA_MODEL_PATH, logger=LOGGER) | ||
| parsed_data_model = data_model_parser.parse_model() | ||
| data_model_grapher = DataModelGraph(parsed_data_model) | ||
| graph_data_model = data_model_grapher.graph | ||
| dmge = DataModelGraphExplorer(graph_data_model, logger=LOGGER) | ||
| json_schema = create_json_schema( | ||
| dmge, datatype=DATATYPE, schema_name=DATATYPE, logger=LOGGER | ||
| ) | ||
| print(json_schema) | ||
andrewelamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.