Prefer using email? Say hi at hello@moveshelf.com
## README: this example shows how we can create a trial on Moveshelf
# using the Moveshelf API.
# This code assumes you have implemented the 'Create subject' example,
# that you have found the subject with a given EHR-id/MRN (my_subject_mrn)
# or name (my_subject_name) within a given project (my_project), and that you have
# access to the subject ID
# This code also assumes you have implemented the 'Create session' example,
# and that you have found the session with a specific name (my_session)
# For that subject and session, to understand if we need to create a new trial or find an
# existing trial, we first check if a condition with the specified name (my_condition)
# exists. If it doesn't exist yet, or if a trial with a specific name (my_trial) is not found
# in the existing condition, we create a new trial. Otherwise, we use the exising trial.
## General configuration. Set values before running the script
my_project = "<organizationName/projectName>" # e.g. support/demoProject
my_subject_mrn = "<subjectMRN>" # subject MRN, e.g. "1234567" or None
my_subject_name = "<subjectName>" # subject name, e.g. Subject1
my_session = "YYYY-MM-DD" # session name, e.g. 2025-09-05
my_condition = "<conditionName>" # e.g. "Barefoot"
my_trial = "<trialName>" # when set to None, we increment the trial number starting with "Trial-1"
## Add here the code to retrieve the project and find an existing subject and its "subject_details"
# ... subject_found = True
## Add here the code to retrieve an existing session and get its details using "getSessionById"
# Get conditions in the session
conditions = []
conditions = util.getConditionsFromSession(session, conditions)
condition_exists = any(c["path"].replace("/", "") == my_condition for c in conditions)
condition = next(c for c in conditions if c["path"].replace("/", "") == my_condition) \
if condition_exists else {"path": my_condition, "clips": []}
clip_id = util.addOrGetTrial(api, session, condition, my_trial)
print(f"Clip created with id: {clip_id}")
# Define the path to the local JSON file
local_metadata_json = os.path.join(parent_folder, "moveshelf_config_import.json")
# Load JSON file
with open(local_metadata_json, "r") as file:
local_metadata = json.load(file)
# Extract conditionDefinition from metadata JSON
my_condition_definition = local_metadata.get("conditionDefinition", {})
## Add here the code to retrieve the project and find an existing subject and its "subject_details"
# ... subject_found = True
## Add here the code to retrieve an existing session and get its details using "getSessionById"
# Get conditions in the session
conditions = []
conditions = util.getConditionsFromSession(session, conditions)
# loop over conditions defined in conditionDefinition
for my_condition, my_trials in my_condition_definition.items():
condition_exists = any(c["path"].replace("/", "") == my_condition for c in conditions)
condition = next(c for c in conditions if c["path"].replace("/", "") == my_condition) \
if condition_exists else {"path": my_condition, "clips": []}
# Loop over trials list defined for my_condition
for my_trial in my_trials:
clip_id = util.addOrGetTrial(api, session, condition, my_trial)
print(f"Clip created with id: {clip_id}")
# Fetch trial using the trial ID
new_clip = api.getClipData(clip_id)
print(
f"Found a clip with title: {new_clip['title']},\n"
f"and id: {new_clip['id']}"
)