22from tiled .client import from_uri
33from tiled .structures .array import ArrayStructure
44import numpy as np
5+ from urllib .parse import urlparse , urlunparse
56
67# Create directory
78def create_directory (path ):
@@ -11,6 +12,24 @@ def create_directory(path):
1112 else :
1213 print (f"Local directory '{ path } ' already exsists." )
1314
15+ def ensure_parent_containers (tiled_uri , tiled_api_key ):
16+ parsed_url = urlparse (tiled_uri )
17+ path = parsed_url .path
18+ # Splitting path into parts
19+ path_parts = path .split ('/' )[1 :] # Split and remove the first empty element
20+ root_path = '/' .join (path_parts [:3 ])
21+ tiled_root = urlunparse ((parsed_url .scheme , parsed_url .netloc , root_path ,
22+ parsed_url .params , parsed_url .query , parsed_url .fragment ))
23+
24+ last_container = from_uri (tiled_root , api_key = tiled_api_key )
25+
26+ for part in path_parts :
27+ if part in last_container .keys ():
28+ last_container = last_container [part ]
29+ else :
30+ last_container = last_container .create_container (key = part )
31+ return last_container
32+
1433
1534# Tiled Saving
1635def allocate_array_space (
@@ -22,7 +41,11 @@ def allocate_array_space(
2241 array_name ,
2342 ):
2443
25- last_container = from_uri (seg_tiled_uri , api_key = seg_tiled_api_key )
44+
45+ last_container = ensure_parent_containers (seg_tiled_uri , seg_tiled_api_key )
46+
47+ assert uid not in last_container .keys (), f'uid_save: { uid } already existed in Tiled Server'
48+
2649 last_container = last_container .create_container (key = uid )
2750 array_shape = tiled_dataset .mask_client .shape if tiled_dataset .mask_client else tiled_dataset .data_client .shape
2851 structure = ArrayStructure .from_array (np .zeros (array_shape ,dtype = np .int8 ))
0 commit comments