Synapse Utils¶
synapseutils
¶
synapseutils.sync
¶
This module is responsible for holding sync to/from synapse utility functions.
Functions¶
syncFromSynapse
¶
syncFromSynapse(syn: Synapse, entity: Union[str, File, Project, Folder], path: str = None, ifcollision: str = 'overwrite.local', allFiles=None, followLink: bool = False, manifest: str = 'all', downloadFile: bool = True)
Synchronizes a File entity, or a Folder entity, meaning all the files in a folder (including subfolders) from Synapse, and adds a readme manifest with file metadata.
There are a few conversions around annotations to call out here.
Conversion of objects from the REST API to Python native objects¶
The first annotation conversion is to take the annotations from the REST API and convert them into Python native objects. For example the REST API will return a milliseconds since epoch timestamp for a datetime annotation, however, we want to convert that into a Python datetime object. These conversions take place in the annotations module.
Conversion of Python native objects into strings¶
The second annotation conversion occurs when we are writing to the manifest TSV file.
In this case we need to convert the Python native objects into strings that can be
written to the manifest file. In addition we also need to handle the case where the
annotation value is a list of objects. In this case we are converting the list
into a single cell of data with a comma ,
delimiter wrapped in brackets []
.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
entity
|
A Synapse ID, a Synapse Entity object of type file, folder or project. |
path
|
An optional path where the file hierarchy will be reproduced. If not specified the files will by default be placed in the synapseCache. A path is required in order to create a manifest file. A manifest is TSV file that is automatically created that contains metadata (annotations, storage location and provenance) of all downloaded files. If no files were downloaded, no manifest file will be created.
TYPE:
|
ifcollision
|
Determines how to handle file collisions. Maybe "overwrite.local", "keep.local", or "keep.both".
TYPE:
|
allFiles
|
Deprecated and not to be used. This will be removed in v5.0.0.
DEFAULT:
|
followLink
|
Determines whether the link returns the target Entity.
TYPE:
|
manifest
|
Determines whether creating manifest file automatically. The
optional values here (
TYPE:
|
downloadFile
|
Determines whether downloading the files.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List of files |
When entity is a Project or Folder, this function will crawl all subfolders
of the project/folder specified by entity
and download all files that have
not already been downloaded. When entity is a File the function will download the
latest version of the file unless version is denoted in the synid with .version
notiation (e.g. syn123.1) If there are newer files in Synapse (or a local file
has been edited outside of the cache) since the last download then local the file
will be replaced by the new file unless "ifcollision" is changed.
If the files are being downloaded to a specific location outside of the Synapse cache a file (SYNAPSE_METADATA_MANIFEST.tsv) will also be added in the path that contains the metadata (annotations, storage location and provenance of all downloaded files).
See also:
Using this function
Download and print the paths of all downloaded files:
entities = syncFromSynapse(syn, "syn1234")
for f in entities:
print(f.path)
Source code in synapseutils/sync.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
syncToSynapse
¶
syncToSynapse(syn: Synapse, manifestFile, dryRun: bool = False, sendMessages: bool = True, retries: int = MAX_RETRIES, merge_existing_annotations: bool = True, associate_activity_to_new_version: bool = False) -> None
Synchronizes files specified in the manifest file to Synapse.
Given a file describing all of the uploads, this uploads the content to Synapse and optionally notifies you via Synapse messagging (email) at specific intervals, on errors and on completion.
Read more about the manifest file format
There are a few conversions around annotations to call out here.
Conversion of annotations from the TSV file to Python native objects¶
The first annotation conversion is from the TSV file into a Python native object. For example Pandas will read a TSV file and convert the string "True" into a boolean True, however, Pandas will NOT convert our comma delimited and bracket wrapped list of annotations into their Python native objects. This means that we need to do that conversion here after splitting them apart.
Conversion of Python native objects for the REST API¶
The second annotation conversion occurs when we are taking the Python native objects and converting them into a string that can be sent to the REST API. For example the datetime objects which may have timezone information are converted to milliseconds since epoch.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
manifestFile
|
A tsv file with file locations and metadata to be pushed to Synapse.
|
dryRun
|
Performs validation without uploading if set to True.
TYPE:
|
sendMessages
|
Sends out messages on completion if set to True.
TYPE:
|
retries
|
Number of retries to attempt if an error occurs.
TYPE:
|
merge_existing_annotations
|
If True, will merge the annotations in the manifest file with the existing annotations on Synapse. If False, will overwrite the existing annotations on Synapse with the annotations in the manifest file.
TYPE:
|
associate_activity_to_new_version
|
If True, and a version update occurs, the existing activity in Synapse will be associated with the new version. The exception is if you are specifying new values to be used/executed, it will create a new activity for the new version of the entity.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in synapseutils/sync.py
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
|
generateManifest
¶
generateManifest(syn, allFiles, filename, provenance_cache=None) -> None
Generates a manifest file based on a list of entities objects.
Read more about the manifest file format
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
|
allFiles
|
A list of File Entity objects on Synapse (can't be Synapse IDs)
|
filename
|
file where manifest will be written
|
provenance_cache
|
an optional dict of known provenance dicts keyed by entity ids
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in synapseutils/sync.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
|
generate_sync_manifest
¶
generate_sync_manifest(syn, directory_path, parent_id, manifest_path) -> None
Generate manifest for syncToSynapse from a local directory.
Read more about the manifest file format
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
|
directory_path
|
Path to local directory to be pushed to Synapse.
|
parent_id
|
Synapse ID of the parent folder/project on Synapse.
|
manifest_path
|
Path to the manifest file to be generated.
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in synapseutils/sync.py
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 |
|
readManifestFile
¶
readManifestFile(syn, manifestFile)
Verifies a file manifest and returns a reordered dataframe ready for upload.
Read more about the manifest file format
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
|
manifestFile
|
A tsv file with file locations and metadata to be pushed to Synapse.
|
RETURNS | DESCRIPTION |
---|---|
A pandas dataframe if the manifest is validated. |
Source code in synapseutils/sync.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
|
synapseutils.copy_functions
¶
Functions¶
copy
¶
copy(syn: Synapse, entity: str, destinationId: str, skipCopyWikiPage: bool = False, skipCopyAnnotations: bool = False, **kwargs) -> Dict[str, str]
- This function will assist users in copying entities ( Tables, Links, Files, Folders, Projects ), and will recursively copy everything in directories.
- A Mapping of the old entities to the new entities will be created and all the wikis of each entity will also be copied over and links to synapse Ids will be updated.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
entity
|
A synapse entity ID
TYPE:
|
destinationId
|
Synapse ID of a folder/project that the copied entity is being copied to
TYPE:
|
skipCopyWikiPage
|
Skip copying the wiki pages.
TYPE:
|
skipCopyAnnotations
|
Skips copying the annotations.
TYPE:
|
version
|
(File copy only) Can specify version of a file. Default to None
|
updateExisting
|
(File copy only) When the destination has an entity that has the same name, users can choose to update that entity. It must be the same entity type Default to False
|
setProvenance
|
(File copy only) Has three values to set the provenance of the copied entity: traceback: Sets to the source entity existing: Sets to source entity's original provenance (if it exists) None: No provenance is set
|
excludeTypes
|
(Folder/Project copy only) Accepts a list of entity types (file, table, link) which determines which entity types to not copy. Defaults to an empty list.
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, str]
|
A mapping between the original and copied entity: {'syn1234':'syn33455'} |
Using this function
Sample copy:
import synapseutils
import synapseclient
syn = synapseclient.login()
synapseutils.copy(syn, ...)
Copying Files:
synapseutils.copy(syn, "syn12345", "syn45678", updateExisting=False, setProvenance = "traceback",version=None)
Copying Folders/Projects:
# This will copy everything in the project into the destinationId except files and tables.
synapseutils.copy(syn, "syn123450","syn345678",excludeTypes=["file","table"])
Source code in synapseutils/copy_functions.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
|
changeFileMetaData
¶
changeFileMetaData(syn: Synapse, entity: Union[str, Entity], downloadAs: str = None, contentType: str = None, forceVersion: bool = True, name: str = None) -> Entity
Change File Entity metadata like the download as name.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
entity
|
Synapse entity Id or object. |
downloadAs
|
Specify filename to change the filename of a filehandle.
TYPE:
|
contentType
|
Specify content type to change the content type of a filehandle.
TYPE:
|
forceVersion
|
Indicates whether the method should increment the version of the object even if nothing has changed. Defaults to True.
TYPE:
|
name
|
Specify filename to change the filename of the file.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Entity
|
Synapse Entity |
Using this function
Updating all file 'downloadAs' names within a folder to match the name of the entity.
import synapseclient
import synapseutils
syn = synapseclient.Synapse()
syn.login()
MY_FOLDER_TO_UPDATE_ALL_FILES_IN = "syn123"
for files_to_update in syn.getChildren(
parent=MY_FOLDER_TO_UPDATE_ALL_FILES_IN, includeTypes=["file"]
):
file_to_check = syn.get(files_to_update["id"], downloadFile=False)
if file_to_check.name != file_to_check["_file_handle"]["fileName"]:
print(
f"Updating downloadAs for {file_to_check['_file_handle']['fileName']} to {file_to_check.name}"
)
synapseutils.changeFileMetaData(
syn=syn,
entity=file_to_check.id,
downloadAs=file_to_check.name,
forceVersion=False,
)
Can be used to change the filename, the filename when the file is downloaded, or the file content-type without downloading:
file_entity = syn.get(synid)
print(os.path.basename(file_entity.path)) ## prints, e.g., "my_file.txt"
file_entity = synapseutils.changeFileMetaData(syn=syn, entity=file_entity, downloadAs="my_new_downloadAs_name_file.txt", name="my_new_name_file.txt")
print(os.path.basename(file_entity.path)) ## prints, "my_new_downloadAs_name_file.txt"
print(file_entity.name) ## prints, "my_new_name_file.txt"
Source code in synapseutils/copy_functions.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
copyFileHandles
¶
copyFileHandles(syn: Synapse, fileHandles: List[Union[File, Entity]], associateObjectTypes: List[str], associateObjectIds: List[str], newContentTypes: List[str] = None, newFileNames: List[str] = None)
Given a list of fileHandle Ids or Objects, copy the fileHandles
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
fileHandles
|
List of fileHandle Ids or Objects |
associateObjectTypes
|
List of associated object types: FileEntity, TableEntity, WikiAttachment, UserProfileAttachment, MessageAttachment, TeamAttachment, SubmissionAttachment, VerificationSubmission (Must be the same length as fileHandles) |
associateObjectIds
|
List of associated object Ids: If copying a file, the objectId is the synapse id, and if copying a wiki attachment, the object id is the wiki subpage id. (Must be the same length as fileHandles) |
newContentTypes
|
List of content types. Set each item to a new content type for each file handle, or leave the item as None to keep the original content type. Default None, which keeps all original content types. |
newFileNames
|
List of filenames. Set each item to a new filename for each file handle, or leave the item as None to keep the original name. Default None, which keeps all original file names. |
RETURNS | DESCRIPTION |
---|---|
List of batch filehandle copy results, can include failureCodes: UNAUTHORIZED and NOT_FOUND |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If length of all input arguments are not the same |
Source code in synapseutils/copy_functions.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
copyWiki
¶
copyWiki(syn, entity, destinationId, entitySubPageId=None, destinationSubPageId=None, updateLinks=True, updateSynIds=True, entityMap=None)
Copies wikis and updates internal links
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
|
entity
|
A synapse ID of an entity whose wiki you want to copy
|
destinationId
|
Synapse ID of a folder/project that the wiki wants to be copied to
|
updateLinks
|
Update all the internal links. (e.g. syn1234/wiki/34345 becomes syn3345/wiki/49508)
DEFAULT:
|
updateSynIds
|
Update all the synapse ID's referenced in the wikis. (e.g. syn1234 becomes syn2345) Defaults to True but needs an entityMap
DEFAULT:
|
entityMap
|
An entity map {'oldSynId','newSynId'} to update the synapse IDs referenced in the wiki.
DEFAULT:
|
entitySubPageId
|
Can specify subPageId and copy all of its subwikis Defaults to None, which copies the entire wiki subPageId can be found: https://www.synapse.org/#!Synapse:syn123/wiki/1234 In this case, 1234 is the subPageId.
DEFAULT:
|
destinationSubPageId
|
Can specify destination subPageId to copy wikis to.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
A list of Objects with three fields: id, title and parentId. |
Source code in synapseutils/copy_functions.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 |
|
synapseutils.walk_functions
¶
Functions¶
walk
¶
walk(syn: Synapse, synId: str, includeTypes: List[str] = ['folder', 'file', 'table', 'link', 'entityview', 'dockerrepo', 'submissionview', 'dataset', 'materializedview'])
Traverse through the hierarchy of files and folders stored under the synId. Has the same behavior as os.walk()
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
synId
|
A synapse ID of a folder or project
TYPE:
|
includeTypes
|
Must be a list of entity types (ie.["file", "table"]) The "folder" type is always included so the hierarchy can be traversed
TYPE:
|
Print Project & Files in slash delimited format
Traversing through a project and print out each Folder and File
import synapseclient
import synapseutils
syn = synapseclient.login()
for directory_path, directory_names, file_name in synapseutils.walk(
syn=syn, synId="syn1234", includeTypes=["file"]
):
for directory_name in directory_names:
print(
f"Directory ({directory_name[1]}): {directory_path[0]}/{directory_name[0]}"
)
for file in file_name:
print(f"File ({file[1]}): {directory_path[0]}/{file[0]}")
The output will look like this assuming only 1 folder and 1 file in the directory:
Directory (syn12345678): My Project Name/my_directory_name
File (syn23456789): My Project Name/my_directory_name/fileA.txt
Using this function
Traversing through a project and printing out the directory path, folders, and files
walkedPath = walk(syn, "syn1234", ["file"]) #Exclude tables and views
for dirpath, dirname, filename in walkedPath:
print(dirpath)
print(dirname) #All the folders in the directory path
print(filename) #All the files in the directory path
This is a high level sequence diagram of the walk function:
sequenceDiagram
autonumber
participant walk
opt Not start_entity
walk->>client: Call `.get()` method
client-->>walk: Metadata about the root start_entity
end
alt Root is not a container
note over walk: Return early
else newpath is none
note over walk: Get directory path from name of entity and synapse ID
else
note over walk: Use path passed in from recursive call
end
loop Get children for container
walk->>client: Call `.getChildren()` method
client-->>walk: return immediate children
note over walk: Aggregation of all children into dirs and non-dirs list
end
loop For each directory
walk->>walk: Recursively call walk
end
Source code in synapseutils/walk_functions.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
synapseutils.monitor
¶
Functions¶
notifyMe
¶
Function decorator that notifies you via email whenever an function completes running or there is a failure.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
messageSubject
|
A string with subject line for sent out messages.
TYPE:
|
retries
|
Number of retries to attempt on failure
TYPE:
|
Using this function
As a decorator:
# to decorate a function that you define
from synapseutils import notifyMe
import synapseclient
syn = synapseclient.login()
@notifyMe(syn, 'Long running function', retries=2)
def my_function(x):
doing_something()
return long_runtime_func(x)
my_function(123)
Wrapping a function:
# to wrap a function that already exists
from synapseutils import notifyMe
import synapseclient
syn = synapseclient.login()
notify_decorator = notifyMe(syn, 'Long running query', retries=2)
my_query = notify_decorator(syn.tableQuery)
results = my_query("select id from syn1223")
Source code in synapseutils/monitor.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
with_progress_bar
¶
with_progress_bar(func, totalCalls, prefix='', postfix='', isBytes=False)
Wraps a function to add a progress bar based on the number of calls to that function.
PARAMETER | DESCRIPTION |
---|---|
func
|
Function being wrapped with progress Bar
|
totalCalls
|
total number of items/bytes when completed
|
prefix
|
String printed before progress bar
DEFAULT:
|
prefix
|
String printed after progress bar
DEFAULT:
|
isBytes
|
A boolean indicating weather to convert bytes to kB, MB, GB etc.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
A wrapped function that contains a progress bar |
Source code in synapseutils/monitor.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
synapseutils.migrate_functions
¶
Classes¶
MigrationResult
¶
A MigrationResult is a proxy object to the underlying sqlite db. It provides a programmatic interface that allows the caller to iterate over the file handles that were migrated without having to connect to or know the schema of the sqlite db, and also avoids the potential memory liability of putting everything into an in memory data structure that could be a liability when migrating a huge project of hundreds of thousands/millions of entities.
As this proxy object is not thread safe since it accesses an underlying sqlite db.
Source code in synapseutils/migrate_functions.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
Functions¶
get_counts_by_status
¶
get_counts_by_status()
Returns a dictionary of counts by the migration status of each indexed file/version. Keys are as follows:
INDEXED
- the file/version has been indexed and will be migrated on a call to migrate_indexed_filesMIGRATED
- the file/version has been migratedALREADY_MIGRATED
- the file/version was already stored at the target storage location and no migration is neededERRORED
- an error occurred while indexing or migrating the file/version
Source code in synapseutils/migrate_functions.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
get_migrations
¶
get_migrations()
A generator yielding each file/version in the migration index. A dictionary of the properties of the migration row is yielded as follows
YIELDS | DESCRIPTION |
---|---|
id
|
the Synapse id |
type
|
the concrete type of the entity |
version
|
the verson of the file entity (if applicable) |
row_id
|
the row of the table attached file (if applicable) |
col_id
|
the column id of the table attached file (if applicable) |
from_storage_location_id
|
|
from_file_handle_id
|
the id file handle of the existing file/version |
to_file_handle_id
|
if migrated, the new file handle id |
status
|
one of INDEXED, MIGRATED, ALREADY_MIGRATED, ERRORED indicating the status of the file/version |
exception
|
if an error was encountered indexing/migrating the file/version its stack is here |
Source code in synapseutils/migrate_functions.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
as_csv
¶
as_csv(path)
Output a flat csv file of the contents of the Migration index.
PARAMETER | DESCRIPTION |
---|---|
path
|
The path to the csv file to be created
|
RETURNS | DESCRIPTION |
---|---|
None
|
But a csv file is created at the given path with the following columns: |
id
|
the Synapse id |
type
|
the concrete type of the entity |
version
|
the verson of the file entity (if applicable) |
row_id
|
the row of the table attached file (if applicable) |
col_name
|
the column name of the column the table attached file resides in (if applicable) |
from_storage_location_id
|
the previous storage location id where the file/version was stored |
from_file_handle_id
|
the id file handle of the existing file/version |
to_file_handle_id
|
if migrated, the new file handle id |
status
|
one of INDEXED, MIGRATED, ALREADY_MIGRATED, ERRORED indicating the status of the file/version |
exception
|
if an error was encountered indexing/migrating the file/version its stack is here |
Source code in synapseutils/migrate_functions.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
Functions¶
index_files_for_migration
¶
index_files_for_migration(syn: Synapse, entity, dest_storage_location_id: str, db_path: str, source_storage_location_ids: Iterable[str] = None, file_version_strategy='new', include_table_files=False, continue_on_error=False)
Index the given entity for migration to a new storage location. This is the first step in migrating an entity to a new storage location using synapseutils.
This function will create a sqlite database at the given db_path that can be subsequently passed to the migrate_indexed_files function for actual migration. This function itself does not modify the given entity in any way.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
entity
|
A Synapse entity whose files should be migrated. Can be a Project, Folder, File entity, or Table entity. If it is a container (a Project or Folder) its contents will be recursively indexed.
|
dest_storage_location_id
|
The id of the new storage location to be migrated to.
TYPE:
|
db_path
|
A path on disk where a sqlite db can be created to store the contents of the created index.
TYPE:
|
source_storage_location_ids
|
An optional iterable of storage location ids that will be migrated. If provided, files outside of one of the listed storage locations will not be indexed for migration. If not provided, then all files not already in the destination storage location will be indexed for migrated. |
file_version_strategy
|
One of "new" (default), "all", "latest", "skip" as follows:
DEFAULT:
|
include_table_files
|
Whether to migrate files attached to tables. If False (default) then e.g. only file entities in the container will be migrated and tables will be untouched.
DEFAULT:
|
continue_on_error
|
Whether any errors encountered while indexing an entity (access etc) will be raised or instead just recorded in the index while allowing the index creation to continue. Default is False (any errors are raised).
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
A MigrationResult object that can be used to inspect the contents of the index or output the index to a CSV for manual inspection. |
Source code in synapseutils/migrate_functions.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
|
migrate_indexed_files
¶
migrate_indexed_files(syn: Synapse, db_path: str, create_table_snapshots=True, continue_on_error=False, force=False) -> Union[MigrationResult, None]
Migrate files previously indexed in a sqlite database at the given db_path using the separate index_files_for_migration function. The files listed in the index will be migrated according to the configuration of that index.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
TYPE:
|
db_path
|
A path on disk where a sqlite db was created using the index_files_for_migration function.
TYPE:
|
create_table_snapshots
|
When updating the files in any table, whether the a snapshot of the table is first created.
DEFAULT:
|
continue_on_error
|
Whether any errors encountered while migrating will be raised or instead just recorded in the sqlite database while allowing the migration to continue. Default is False (any errors are raised).
DEFAULT:
|
force
|
If running in an interactive shell, migration requires an interactice confirmation. This can be bypassed by using the force=True option.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
Union[MigrationResult, None]
|
A MigrationResult object that can be used to inspect the results of the migration. |
Source code in synapseutils/migrate_functions.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 |
|
synapseutils.describe_functions
¶
Functions¶
describe
¶
Gets a synapse entity and returns summary statistics about it.
PARAMETER | DESCRIPTION |
---|---|
syn
|
A Synapse object with user's login, e.g. syn = synapseclient.login()
|
entity
|
synapse id of the entity to be described
TYPE:
|
Using this function
Describing columns of a table
import synapseclient
import synapseutils
syn = synapseclient.login()
statistics = synapseutils(syn, entity="syn123")
print(statistics)
{
"column1": {
"dtype": "object",
"mode": "FOOBAR"
},
"column2": {
"dtype": "int64",
"mode": 1,
"min": 1,
"max": 2,
"mean": 1.4
},
"column3": {
"dtype": "bool",
"mode": false,
"min": false,
"max": true,
"mean": 0.5
}
}
RETURNS | DESCRIPTION |
---|---|
Union[dict, None]
|
A dict if the dataset is valid; None if not. |
Source code in synapseutils/describe_functions.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|