Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plate conversion #1

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions merge.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,81 @@
#!/usr/bin/env python
import os
import sys
import glob
import json
import argparse
p = argparse.ArgumentParser()
p.add_argument("dir", nargs="+")
a = p.parse_args()
rv = dict()

for dir in a.dir:
if os.path.exists(f"{dir}/.zattrs"):
with open(f"{dir}/.zattrs") as o:


def update_zattrs(zarr_dir, omero_json=None, dry_run=False):
rv = dict()
if os.path.exists(f"{zarr_dir}/.zattrs"):
with open(f"{zarr_dir}/.zattrs") as o:
rv.update(json.load(o))
else:
multiscale = {"datasets":[],"version":"0.1"}
for path in sorted(glob.glob(f"{dir}/[0-9]*")):
for path in sorted(glob.glob(f"{zarr_dir}/[0-9]*")):
path = path.split("/")[-1]
multiscale["datasets"].append({"path": path})
rv["multiscales"] = [multiscale]

with open(f"{dir}/omero.json", "r") as o:
omero = json.load(o)
if not omero_json:
with open(f"{zarr_dir}/omero.json", "r") as o:
omero_json = json.load(o)

rv["omero"] = {}
if "version" not in omero:
omero["version" ] = "0.1"
if "version" not in omero_json:
omero_json["version" ] = "0.1"
for x in ("version", "channels", "rdefs"):
rv["omero"][x] = omero[x]
rv["omero"][x] = omero_json[x]

rv["omero"]["id"] = omero["id"]
rv["omero"]["name"] = omero["meta"]["imageName"]
rv["omero"]["id"] = omero_json["id"]
rv["omero"]["name"] = omero_json["meta"]["imageName"]
for ch in rv["omero"]["channels"]:
for x in ("reverseIntensity", "emissionWave"):
ch.pop(x, None)
rv["omero"]["rdefs"].pop("invertAxis", None)
rv["omero"]["rdefs"].pop("projection", None)

with open(f"{dir}/.zattrs", "w") as o:
if args.dry_run:
print(f"Updating {zarr_dir}/.zattrs")
with open(f"{zarr_dir}/.zattrs", "w") as o:
o.write(json.dumps(rv, indent=4, sort_keys=True))


def get_zarr_directories(top_dirs, recursive):
if not recursive:
return top_dirs

if len(top_dirs) > 1:
raise Exception("Recursive option can only be used with one directory")

paths = []
for root, dirs, files in os.walk(top_dirs[0]):
for f in files:
if f.lower() == '.zattrs':
paths.append(root)
return(paths)


def get_omero_json(json_file):
if json_file:
with open(f"{json_file}", "r") as o:
return json.load(o)
else:
return None


if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("dir", nargs="+", help="the Zarr directory")
p.add_argument('--file',
help="a JSON file containing the rendering settings")
p.add_argument("--dry-run", "-n", action='store_true',
help="dry-run")
p.add_argument("--recursive", "-r", action='store_true',
help="apply recursively to all Zarr sub-directories")
args = p.parse_args()

zarr_dirs = get_zarr_directories(args.dir, args.recursive)
omero_json = get_omero_json(args.file)
for zarr_dir in zarr_dirs:
update_zattrs(zarr_dir, omero_json=omero_json, dry_run=False)