|
| 1 | +Uploading files to an existing template space |
| 2 | +############################################### |
| 3 | + |
| 4 | +Who is this tutorial for |
| 5 | +============================ |
| 6 | + |
| 7 | +First, this is intended for those wishing to upload templates to TemplateFlow. |
| 8 | +Second, this is for people who want to add to a template directory that already exists. |
| 9 | +TemplateFlow consists of multiple templates sorted by the space the template is in. |
| 10 | + |
| 11 | +If the space for you template does not exist, then you should follow this tutorial: :ref: `adding_a_new_template`. |
| 12 | + |
| 13 | +This tutorial assumes you have done all the steps in the preceding tutorial: :ref: `prerequisites_to_contributing`. |
| 14 | + |
| 15 | +Big picture |
| 16 | +=============== |
| 17 | + |
| 18 | +Image files (e.g. nifti files) are hosted on OSF. |
| 19 | +All other information (e.g. tsv, json files) will be hosted on github as regular files. |
| 20 | + |
| 21 | +When you use TemplateFlow, the images are only downloaded when they are needed. |
| 22 | +This can be done with DataLad, DataLad tracks a dataset and only downloads files when they are needed. |
| 23 | +You can read more about DataLad [`Link <FIXLINK>`_]. |
| 24 | + |
| 25 | +Thus, when uploading to an existing template, there are two different steps: |
| 26 | + |
| 27 | +1. You need to place them on the OSF server. |
| 28 | +2. you need to tell DataLad about the new items so the Github repo can track the new files. |
| 29 | + |
| 30 | +Step 1: Placing the template on the OSF server |
| 31 | +================================================ |
| 32 | + |
| 33 | +Go to the TemplateFlow directory you previously initialized. |
| 34 | +Place the file you want to upload in directory and give them appropriate . |
| 35 | + |
| 36 | +Let us say you want to upload: |
| 37 | +`tpl-test/tpl-test_atlas-test1_dseg.nii.gz`, `tpl-test/tpl-test_atlas-test2_dseg.nii.gz`. |
| 38 | + |
| 39 | +>>> export OSF_PASSWORD='<some-password>' |
| 40 | +>>> osf upload tpl-test_atlas-test1_dseg.nii.gz tpl-test_atlas-test1_dseg.nii.gz |
| 41 | + |
| 42 | +If you are overwriting an existing image, use the -f flag. |
| 43 | + |
| 44 | +If you want to upload multiple images (let us say we have multiple atlases), use a for bash loop: |
| 45 | + |
| 46 | +>>> ls tpl-test/*_atlas-test*_dseg.nii.gz | parallel -j8 'osf upload {} {}' |
| 47 | + |
| 48 | +And this will iteratively upload all instances of the atlas to OSF. |
| 49 | + |
| 50 | +Step 2: Telling Datalad where the files are |
| 51 | +================================================ |
| 52 | + |
| 53 | +Github is using Datalad to track the files. |
| 54 | +You need to tell Datalad where the new files are. |
| 55 | + |
| 56 | +To do this, run the following python script, changing the tqo required variables: |
| 57 | + |
| 58 | +.. code-block:: python |
| 59 | +
|
| 60 | + from pathlib import Path |
| 61 | + from datalad_osf.utils import url_from_key, json_from_url, |
| 62 | + get_osf_recursive, osf_to_csv |
| 63 | +
|
| 64 | + ## VARIABLES THAT NEED TO BE CHANGED |
| 65 | +
|
| 66 | + # THe template you are adding too |
| 67 | + subset = 'tpl-test' |
| 68 | + # A string that identifies your new nifit files |
| 69 | + # Leave as blank if you want all files added |
| 70 | + strinfile = 'atlas-test' |
| 71 | +
|
| 72 | + ## OPTIONAL VARIABLES |
| 73 | +
|
| 74 | + # Name of output file |
| 75 | + output_filename = 'new_files.csv' |
| 76 | + # templateflow project name |
| 77 | + key = 'ue5gx' |
| 78 | +
|
| 79 | + ## Rest of script |
| 80 | +
|
| 81 | + toplevel = json_from_url(url_from_key(key)) |
| 82 | + for folder in toplevel['data']: |
| 83 | + if folder['attributes']['name'] == subset: |
| 84 | + url = folder['links']['move'] |
| 85 | + break |
| 86 | +
|
| 87 | + data = json_from_url(url) |
| 88 | + hits = ['name,link'] |
| 89 | + for item in data['data']: |
| 90 | + name = item['attributes']['name'] |
| 91 | + ext = ''.join(Path(name).suffixes) |
| 92 | + if strinfile in name and '.nii' in ext: |
| 93 | + print(item) |
| 94 | + link = item['links']['download'] |
| 95 | + path = item['attributes']['materialized'] |
| 96 | + hits.append(','.join((name, link))) |
| 97 | + Path(output_filename).write_text('\n'.join(hits)) |
| 98 | +
|
| 99 | +Either run this script interactively or save the script as a python file (e.g. 'get_datalad_urls.py') |
| 100 | +then run the file with `python get_datalad_urls.py`. |
| 101 | + |
| 102 | +Note, the above script assumes there are no subdirectories within the template folder. |
| 103 | +See end of tutorial for an example script when there are subdirectories within the template folder. |
| 104 | + |
| 105 | +This script will produce a file called new_files.csv. |
| 106 | + |
| 107 | +Finally, the contents of new_files.csv need to be uploaded via DataLad. |
| 108 | + |
| 109 | +To do this first, move the local image file into a tmp folder. |
| 110 | + |
| 111 | +> mv tpl-test/*_atlas-test*.nii.gz ~/tmp/ |
| 112 | + |
| 113 | +Then you add the new urls to DataLad. Add a message |
| 114 | + |
| 115 | +> datalad addurls new_files.csv '{link}' '{name}' --message 'My test atlases' |
| 116 | +> datalad publish |
| 117 | + |
| 118 | +Example script when subdirectories are presents |
| 119 | +================================================ |
| 120 | + |
| 121 | +.. code-block:: python |
| 122 | +
|
| 123 | + from pathlib import Path |
| 124 | + from datalad_osf.utils import url_from_key, json_from_url, get_osf_recursive, osf_to_csv |
| 125 | +
|
| 126 | + ## VARIABLES THAT NEED TO BE CHANGED |
| 127 | +
|
| 128 | + # THe template you are adding too |
| 129 | + subset = 'tpl-test' |
| 130 | + # A string that identifies your new files |
| 131 | + strinfile = 'atlas-test' |
| 132 | +
|
| 133 | + ## OPTIONAL VARIABLES |
| 134 | +
|
| 135 | + # Name of output file |
| 136 | + output_filename = 'new_files.csv' |
| 137 | + # templateflow project name |
| 138 | + key = 'ue5gx' |
| 139 | +
|
| 140 | + ## REST OF SCRIPT |
| 141 | +
|
| 142 | + toplevel = json_from_url(url_from_key(key)) |
| 143 | + for folder in toplevel['data']: |
| 144 | + if folder['attributes']['name'] == subset: |
| 145 | + url = folder['links']['move'] |
| 146 | + break |
| 147 | +
|
| 148 | + data = json_from_url(url) |
| 149 | + hits = ['name,link'] |
| 150 | + for item in data['data']: |
| 151 | + if item['attributes']['kind'] == 'folder': |
| 152 | + subdata = json_from_url(item['links']['move']) |
| 153 | + for subitem in subdata['data']: |
| 154 | + if subitem['attributes']['kind'] == 'file': |
| 155 | + name = subitem['attributes']['name'] |
| 156 | + ext = ''.join(Path(name).suffixes) |
| 157 | + if strinfile in name and '.nii' in ext: |
| 158 | + print(name) |
| 159 | + link = subitem['links']['download'] |
| 160 | + path = subitem['attributes']['materialized'] |
| 161 | + hits.append(','.join((name, link))) |
| 162 | + Path(output_filename).write_text('\n'.join(hits)) |
0 commit comments