@@ -42,7 +42,7 @@ def build_options(
4242 file_type (str): Type of file supported by Vitessce.
4343 file_path (str): Path to file.
4444 file_options (dict[str, T.Any]): Dictionary defining the options.
45- check_exist (bool, optional): Whether to check the given path to confirm the file exists.
45+ check_exist (bool, optional): Whether to check the given path to confirm the file exists.
4646 Defaults to False.
4747
4848 Returns:
@@ -120,89 +120,95 @@ def build_options(
120120
121121
122122def build_raster_options (
123- image_zarr : dict [str , dict [str , str ]], url : str
123+ images : dict [str , list [ dict [str , T . Any ] ]], url : str
124124) -> dict [str , T .Any ]:
125125 """Function that creates the View config's options for image files
126126
127127 Args:
128- image_zarr (dict[str, dict[str, str]]): Dictionary containing a metadata dictionary
129- for each image in Zarr format.
130- url (str): URL to prepend to each file in the config file.
128+ images (dict[str, list[dict[str, T.Any]]], optional): Dictionary containing for each image type key (raw and label)
129+ a list of dictionaries (one per image of that type) with the corresponding path and metadata for that image.
130+ Defaults to {}.
131+ url (str): URL to prepend to each file in the config file.
131132 The URL to the local or remote server that will serve the files
132133
133134 Returns:
134135 dict[str, T.Any]: Options dictionary for View config file
135136 """
136137 raster_options = {"renderLayers" : [], "schemaVersion" : "0.0.2" , "images" : []}
137- for image in image_zarr .keys ():
138- image_name = os .path .splitext (image )[0 ]
139- channel_names = (
140- image_zarr [image ]["channel_names" ]
141- if "channel_names" in image_zarr [image ]
142- else []
143- )
144- channel_names , isBitmask = (
145- (["Labels" ], True )
146- if image_name .split ("_" )[- 1 ] == "label" and not len (channel_names )
147- else (channel_names , False )
148- )
149- raster_options ["renderLayers" ].append (image_name )
150- raster_options ["images" ].append (
151- {
152- "name" : image_name ,
153- "url" : os .path .join (url , image ),
154- "type" : "zarr" ,
155- "metadata" : {
156- "isBitmask" : isBitmask ,
157- "dimensions" : [
158- {"field" : "t" , "type" : "quantitative" , "values" : None },
159- {
160- "field" : "channel" ,
161- "type" : "nominal" ,
162- "values" : channel_names ,
163- },
164- {"field" : "y" , "type" : "quantitative" , "values" : None },
165- {"field" : "x" , "type" : "quantitative" , "values" : None },
166- ],
167- "isPyramid" : True ,
168- "transform" : {"translate" : {"y" : 0 , "x" : 0 }, "scale" : 1 },
169- },
170- }
171- )
138+ for img_type in images .keys (): # raw, label
139+ for img in images [img_type ]:
140+ image_name = os .path .splitext (os .path .basename (img ["path" ]))[0 ]
141+ channel_names = (
142+ img ["md" ]["channel_names" ]
143+ if "channel_names" in img ["md" ] and len (img ["md" ]["channel_names" ])
144+ else (
145+ ["Labels" ]
146+ if img_type == "label"
147+ else [f"Channel { x } " for x in range (int (img ["md" ]["C" ]))]
148+ )
149+ )
150+ isBitmask = img_type == "label"
151+ raster_options ["renderLayers" ].append (image_name )
152+ raster_options ["images" ].append (
153+ {
154+ "name" : image_name ,
155+ "url" : os .path .join (url , os .path .basename (img ["path" ])),
156+ "type" : "zarr" ,
157+ "metadata" : {
158+ "isBitmask" : isBitmask ,
159+ "dimensions" : [
160+ {"field" : "t" , "type" : "quantitative" , "values" : None },
161+ {
162+ "field" : "channel" ,
163+ "type" : "nominal" ,
164+ "values" : channel_names ,
165+ },
166+ {"field" : "y" , "type" : "quantitative" , "values" : None },
167+ {"field" : "x" , "type" : "quantitative" , "values" : None },
168+ ],
169+ "isPyramid" : True ,
170+ "transform" : {"translate" : {"y" : 0 , "x" : 0 }, "scale" : 1 },
171+ },
172+ }
173+ )
172174 return raster_options
173175
174176
175177def write_json (
176- title : str = "" ,
178+ project : str = "" ,
177179 dataset : str = "" ,
178180 file_paths : list [str ] = [],
179- image_zarr : dict [str , dict [str , str ]] = {},
181+ images : dict [str , list [ dict [str , T . Any ] ]] = {},
180182 url : str = "" ,
181- outdir : str = "./" ,
182- config_filename_suffix : str = "config.json" ,
183183 options : dict [str , T .Any ] = None ,
184184 layout : str = "minimal" ,
185185 custom_layout : str = None ,
186+ title : str = "" ,
187+ description : str = "" ,
188+ config_filename_suffix : str = "config.json" ,
189+ outdir : str = "./" ,
186190) -> None :
187191 """This function writes a Vitessce View config JSON file
188192
189193 Args:
190- title (str, optional): Title to use in the config file . Defaults to "".
194+ project (str, optional): Project name . Defaults to "".
191195 dataset (str, optional): Dataset name. Defaults to "".
192196 file_paths (list[str], optional): Paths to files that will be included in the config file. Defaults to [].
193- image_zarr (dict[str, dict[str, str]], optional): Dictionary containing a metadata dictionary
194- for each image in Zarr format. Defaults to {}.
195- url (str, optional): URL to prepend to each file in the config file.
197+ images (dict[str, list[dict[str, T.Any]]], optional): Dictionary containing for each image type key (raw and label)
198+ a list of dictionaries (one per image of that type) with the corresponding path and metadata for that image.
199+ Defaults to {}.
200+ url (str, optional): URL to prepend to each file in the config file.
196201 The URL to the local or remote server that will serve the files.
197202 Defaults to "".
198- outdir (str, optional): Directory in which the config file will be written to. Defaults to "./".
199- config_filename_suffix (str, optional): Config filename suffix. Defaults to "config.json".
200203 options (dict[str, T.Any], optional): Dictionary with Vitessce config file `options`. Defaults to None.
201204 layout (str, optional): Type of predefined layout to use. Defaults to "minimal".
202205 custom_layout (str, optional): String defining a Vitessce layout following its alternative syntax.
203206 https://vitessce.github.io/vitessce-python/api_config.html#vitessce.config.VitessceConfig.layout
204207 https://github.com/vitessce/vitessce-python/blob/1e100e4f3f6b2389a899552dffe90716ffafc6d5/vitessce/config.py#L855
205208 Defaults to None.
209+ title (str, optional): Data title to show in the visualization. Defaults to "".
210+ config_filename_suffix (str, optional): Config filename suffix. Defaults to "config.json".
211+ outdir (str, optional): Directory in which the config file will be written to. Defaults to "./".
206212
207213 Raises:
208214 SystemExit: If no valid files have been input
@@ -211,17 +217,20 @@ def write_json(
211217
212218 has_files = False
213219
214- config = VitessceConfig (name = str (title ))
215- config_dataset = config .add_dataset (str (title ), str (dataset ))
220+ config = VitessceConfig (
221+ name = str (title ) if len (title ) else str (project ),
222+ description = description ,
223+ )
224+ config_dataset = config .add_dataset (str (dataset ), str (dataset ))
216225
217226 coordination_types = defaultdict (lambda : cycle (iter ([])))
218- file_paths_names = {x .split ("_ " )[- 1 ]: x for x in file_paths }
227+ file_paths_names = {x .split ("- " )[- 1 ]: x for x in file_paths }
219228 dts = set ([])
220229
221- if len (image_zarr . items () ):
230+ if images . keys () and any ([ len (images [ k ]) for k in images . keys ()] ):
222231 has_files = True
223232 config_dataset .add_file (
224- dt .RASTER , ft .RASTER_JSON , options = build_raster_options (image_zarr , url )
233+ dt .RASTER , ft .RASTER_JSON , options = build_raster_options (images , url )
225234 )
226235 dts .add (dt .RASTER )
227236
@@ -346,7 +355,7 @@ def write_json(
346355 if outdir and not os .path .isdir (outdir ):
347356 os .mkdir (outdir )
348357 with open (
349- os .path .join (outdir or "" , f"{ title } _ { dataset } _ { config_filename_suffix } " ), "w"
358+ os .path .join (outdir or "" , f"{ project } - { dataset } - { config_filename_suffix } " ), "w"
350359 ) as out_file :
351360 json .dump (config_json , out_file , indent = 2 )
352361
0 commit comments