|
| 1 | +""" |
| 2 | +Here is where you'll find the code for the SubmissionView tutorial. |
| 3 | +
|
| 4 | +A SubmissionView allows you to aggregate and query submissions from one or more |
| 5 | +evaluation queues in a tabular format, similar to other view types in Synapse. |
| 6 | +
|
| 7 | +This tutorial also shows how to create an evaluation queue, submit a file to it, and |
| 8 | +update the submission status. The example uses a temporary file for the submission, |
| 9 | +which is automatically cleaned up after the script runs. You can modify the file |
| 10 | +path and name as needed. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | +import tempfile |
| 15 | + |
| 16 | +import pandas as pd |
| 17 | + |
| 18 | +from synapseclient import Evaluation, Synapse |
| 19 | +from synapseclient.models import ( |
| 20 | + Activity, |
| 21 | + Column, |
| 22 | + ColumnType, |
| 23 | + File, |
| 24 | + Project, |
| 25 | + SubmissionView, |
| 26 | + UsedURL, |
| 27 | +) |
| 28 | + |
| 29 | +syn = Synapse() |
| 30 | +syn.login() |
| 31 | + |
| 32 | +# Retrieve the project ID |
| 33 | +my_project = Project(name="My uniquely named project about Alzheimer's Disease").get() |
| 34 | +project_id = my_project.id |
| 35 | +print(f"My project ID is: {project_id}") |
| 36 | + |
| 37 | +# Step 1: Set up and create an evaluation queue |
| 38 | +evaluation_name = "Test Evaluation Queue for Alzheimer conference" |
| 39 | +evaluation_description = "Evaluation queue for testing submission view" |
| 40 | +evaluation = Evaluation( |
| 41 | + name=evaluation_name, description=evaluation_description, contentSource=project_id |
| 42 | +) |
| 43 | +evaluation = syn.store(evaluation) |
| 44 | +print(f"Created evaluation queue with ID: {evaluation.id}") |
| 45 | + |
| 46 | +# Step 2: Create a SubmissionView for the evaluation queue |
| 47 | +view = SubmissionView( |
| 48 | + name="SubmissionView for Alzheimer conference", |
| 49 | + parent_id=project_id, |
| 50 | + scope_ids=[evaluation.id], |
| 51 | + include_default_columns=True, |
| 52 | + columns=[ |
| 53 | + Column( |
| 54 | + name="metric_A", |
| 55 | + column_type=ColumnType.DOUBLE, |
| 56 | + ), |
| 57 | + Column( |
| 58 | + name="metric_B", |
| 59 | + column_type=ColumnType.DOUBLE, |
| 60 | + ), |
| 61 | + ], |
| 62 | + activity=Activity( |
| 63 | + name="Submission Review Analysis", |
| 64 | + description="Analysis of Q1 2025 challenge submissions", |
| 65 | + used=[ |
| 66 | + UsedURL( |
| 67 | + name="Challenge Homepage", |
| 68 | + url="https://sagebionetworks.org/community/challenges-portal", |
| 69 | + ) |
| 70 | + ], |
| 71 | + ), |
| 72 | +).store() |
| 73 | + |
| 74 | +print(f"My SubmissionView ID is: {view.id}") |
| 75 | + |
| 76 | +# Reorder columns for better display |
| 77 | +view.reorder_column(name="name", index=2) |
| 78 | +view.reorder_column(name="status", index=3) |
| 79 | +view.reorder_column(name="evaluationid", index=4) |
| 80 | +view.store() |
| 81 | + |
| 82 | +print("Available columns in the view:", list(view.columns.keys())) |
| 83 | + |
| 84 | +# Step 3: Create and submit a file to the evaluation queue |
| 85 | +with tempfile.NamedTemporaryFile( |
| 86 | + mode="w", suffix=".txt", delete=True, delete_on_close=False |
| 87 | +) as temp_file: |
| 88 | + with open(temp_file.name, "w") as opened_temp_file: |
| 89 | + opened_temp_file.write("This is a test submission file.") |
| 90 | + temp_file_path = temp_file.name |
| 91 | + |
| 92 | + # Upload the temporary file to Synapse |
| 93 | + submission_file = File( |
| 94 | + path=temp_file_path, parent_id=project_id, name="Test Submission" |
| 95 | + ).store() |
| 96 | + |
| 97 | + # Submit the file to the evaluation queue |
| 98 | + submission = syn.submit( |
| 99 | + evaluation=evaluation, |
| 100 | + entity=submission_file, |
| 101 | + name="Test Submission", |
| 102 | + submitterAlias="Participant 1", |
| 103 | + ) |
| 104 | + |
| 105 | + print(f"Created submission with ID: {submission.id}") |
| 106 | + |
| 107 | +# Step 4: Query and update the submission status |
| 108 | +# Query the SubmissionView to see our submission |
| 109 | +query = f"SELECT * FROM {view.id} WHERE id = '{submission.id}'" |
| 110 | +results_as_dataframe: pd.DataFrame = view.query(query=query) |
| 111 | +# Due to the eventual consistency of the system, we need to perform 2 queries |
| 112 | +results_as_dataframe: pd.DataFrame = view.query(query=query) |
| 113 | + |
| 114 | +print("Query results:") |
| 115 | +print(results_as_dataframe) |
| 116 | + |
| 117 | +# Update the status to indicate it's been scored |
| 118 | +submission_status = syn.getSubmissionStatus(submission=submission.id) |
| 119 | +print(f"Submission status: {submission_status.status}") |
| 120 | +submission_status.status = "SCORED" |
| 121 | +submission_status.submissionAnnotations["metric_A"] = 90 |
| 122 | +submission_status.submissionAnnotations["metric_B"] = 80 |
| 123 | +submission_status.score = 0.7 |
| 124 | +submission_status = syn.store(submission_status) |
| 125 | +print(f"Updated submission status to: {submission_status.status}") |
| 126 | + |
| 127 | +# Step 5: Modify the SubmissionView scope |
| 128 | +# First let's make sure we have the latest view from Synapse: |
| 129 | +view.get() |
| 130 | + |
| 131 | +# Create another evaluation queue to demonstrate adding to the scope |
| 132 | +second_evaluation = Evaluation( |
| 133 | + name="Second Test Evaluation Queue for Alzheimer conference", |
| 134 | + description="Another evaluation queue for testing submission view", |
| 135 | + contentSource=project_id, |
| 136 | +) |
| 137 | +second_evaluation = syn.store(second_evaluation) |
| 138 | +print(f"Created second evaluation queue with ID: {second_evaluation.id}") |
| 139 | + |
| 140 | +# Add the new evaluation queue to the view's scope |
| 141 | +view.scope_ids.append(second_evaluation.id) |
| 142 | +view.store() # Store the updated view |
| 143 | +print("Updated SubmissionView scope. Current scope IDs:", view.scope_ids) |
| 144 | + |
| 145 | +# Step 6: Create a snapshot of the view |
| 146 | +snapshot_info = view.snapshot( |
| 147 | + comment="Initial submission review snapshot", |
| 148 | +) |
| 149 | +print("Created snapshot of the SubmissionView:") |
| 150 | +print(snapshot_info) |
| 151 | +snapshot_version = snapshot_info.snapshot_version_number |
| 152 | +print(f"Snapshot version number: {snapshot_version}") |
| 153 | + |
| 154 | +# Step 7: Query the snapshot we just created |
| 155 | +# You may also get the snapshot version from the view object directly by looking at the version number |
| 156 | +# (which is the latest version of the view) and subtracting 1. |
| 157 | +# snapshot_version = view.version_number - 1 |
| 158 | + |
| 159 | +snapshot_query = f"SELECT * FROM {view.id}.{snapshot_version}" |
| 160 | +snapshot_results = view.query(snapshot_query) |
| 161 | +print("Query results from the snapshot:") |
| 162 | +print(snapshot_results) |
0 commit comments