Skip to content

Commit a86982e

Browse files
committed
ome_zarr view handles absolute and relative paths
1 parent 77a021b commit a86982e

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

ome_zarr/utils.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,20 @@ def splitall(path):
131131

132132
def view(input_path: str, port: int = 8000) -> None:
133133
# serve the parent directory in a simple server with CORS. Open browser
134-
parent_dir, image_name = os.path.split(input_path)
134+
parent_path, server_dir = os.path.split(input_path)
135135
# in case input_path had trailing slash, we go one level up...
136-
if len(image_name) == 0:
137-
parent_dir, image_name = os.path.split(parent_dir)
136+
if len(server_dir) == 0:
137+
parent_path, server_dir = os.path.split(parent_path)
138+
139+
# 'input_path' is path passed to the script. To the data dir. E.g. "ZARR/data"
140+
# 'parent_path', e.g. "ZARR" just for running http server
141+
# 'server_dir' is the name of our top-level dir E.g. "data"
142+
143+
# We will be serving the data from last dir in /parent/dir/path
144+
# so we need to use that as base for image URLs...
138145

139146
# walk the input path to find all .zattrs files...
140147
def walk(path: Path):
141-
print("walk", path, (path / ".zattrs").exists())
142148
if (path / ".zattrs").exists():
143149
yield from find_multiscales(path)
144150
else:
@@ -150,40 +156,52 @@ def walk(path: Path):
150156
else:
151157
continue
152158

153-
zarrs = list(walk(Path(input_path)))
154-
155-
for z in zarrs:
156-
# split file path into list
157-
z[2] = splitall(z[2])
159+
url = None
160+
zarrs = []
161+
if server_dir.endswith(".csv"):
162+
# open CSV in biofile finder...
163+
source = {
164+
"uri": f"http://localhost:{port}/{server_dir}",
165+
"type": "csv",
166+
"name": "biofile_finder.csv",
167+
}
168+
s = urllib.parse.quote(json.dumps(source))
169+
url = f"https://bff.allencell.org/app?source={s}"
170+
else:
171+
zarrs = list(walk(Path(input_path)))
158172

159173
# If we have just one zarr, open ome-ngff-validator in a web browser...
160174
if len(zarrs) == 1:
161175
url = (
162176
f"https://ome.github.io/ome-ngff-validator/"
163-
f"?source=http://localhost:{port}/{image_name}"
177+
f"?source=http://localhost:{port}/{server_dir}"
164178
)
165179
elif len(zarrs) > 1:
166180
# ...otherwise write to CSV file and open in BioFile Finder
167-
max_folders = max(len(z[2]) for z in zarrs)
168-
col_names = ["File Path", "File Name"] + [
169-
f"Folder {i}" for i in range(max_folders)
170-
]
171-
# open csv file and write lines...
181+
col_names = ["File Path", "File Name", "Folders"]
182+
# write csv file into the dir we're serving from...
172183
bff_csv = os.path.join(input_path, "biofile_finder.csv")
184+
173185
with open(bff_csv, "w", newline="") as csvfile:
174186
writer = csv.writer(csvfile, delimiter=",")
175187
writer.writerow(col_names)
176188
for zarr_img in zarrs:
177-
# path_to_zarr, name, dirname
178-
file_path = f"http://localhost:{port}/{zarr_img[0]}"
189+
# zarr paths start with full path to img
190+
# e.g. ZARR/data/to/img (from walk("ZARR/data"))
191+
# but we want them to be from the server_dir to img, e.g "data/to/img".
192+
# So we want relative /to/img path, from input_path -> to img
193+
relpath = os.path.relpath(zarr_img[0], input_path)
194+
# On Windows, we need to replace \\ with / in relpath for URL
195+
rel_url = "/".join(splitall(relpath))
196+
file_path = f"http://localhost:{port}/{server_dir}/{rel_url}"
179197
name = zarr_img[1]
180-
# folders list needs to be same length for every row.
181-
# e.g. ['f1', 'f2', '-', '-']
182-
folders = zarr_img[2] + ["-"] * (max_folders - len(zarr_img[2]))
183-
writer.writerow([file_path, name] + folders)
198+
# folders is "f1,f2,f3" etc.
199+
folders_path = os.path.relpath(zarr_img[2], input_path)
200+
folders = ",".join(splitall(folders_path))
201+
writer.writerow([file_path, name, folders])
184202

185203
source = {
186-
"uri": f"http://localhost:{port}/{image_name}/biofile_finder.csv",
204+
"uri": f"http://localhost:{port}/{server_dir}/biofile_finder.csv",
187205
"type": "csv",
188206
"name": "biofile_finder.csv",
189207
}
@@ -198,10 +216,14 @@ def end_headers(self) -> None:
198216
def translate_path(self, path: str) -> str:
199217
# Since we don't call the class constructor ourselves,
200218
# we set the directory here instead
201-
self.directory = parent_dir
219+
self.directory = parent_path
202220
super_path = super().translate_path(path)
203221
return super_path
204222

223+
if url is None:
224+
print("No OME-Zarr files found in", input_path)
225+
return
226+
205227
# Open in browser...
206228
webbrowser.open(url)
207229

0 commit comments

Comments
 (0)