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

Store statistics in array metadata #31

Merged
merged 2 commits into from
Aug 21, 2024
Merged
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
54 changes: 50 additions & 4 deletions src/ome2024_ngff_challenge/resave.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,25 @@ def s3_string(self):
def fs_string(self):
return str(self.path / self.subpath) if self.subpath else str(self.path)

def is_s3(self):
return bool(self.bucket)

def s3_endpoint(self):
"""
Returns a representation of the S3 endpoint set on this configuration.

* "" if this is not an S3 configuration
* "default" if no explicit endpoint is set
* otherwise the URL is returned
"""
if self.is_s3():
if self.endpoint:
return self.endpoint
return "default"
return ""

def __str__(self):
if self.bucket:
if self.is_s3():
return self.s3_string()
return self.fs_string()

Expand Down Expand Up @@ -444,12 +461,39 @@ def convert_array(

after = TSMetrics(input_config.ts_config, write_config, before)

stats = {
"input": input_config.s3_endpoint(),
"output": output_config.s3_endpoint(),
"start": before.time,
"stop": after.time,
"read": after.read(),
"written": after.written(),
"elapsed": after.elapsed(),
}
LOGGER.info(f"""Re-encode (tensorstore) {input_config} to {output_config}
read: {after.read()}
write: {after.written()}
time: {after.elapsed()}
read: {stats["read"]}
write: {stats["written"]}
time: {stats["elapsed"]}
""")

## TODO: there is likely an easier way of doing this
metadata = write.kvstore["zarr.json"]
metadata = json.loads(metadata)
if "attributes" in metadata:
attributes = metadata["attributes"]
else:
attributes = {}
metadata["attributes"] = attributes
attributes["_ome2024_ngff_challenge_stats"] = stats
metadata = json.dumps(metadata)
write.kvstore["zarr.json"] = metadata

## TODO: This is not working with v3 branch nor with released version
## zr_array = zarr.open_array(store=output_config.zr_store, mode="a", zarr_format=3)
## zr_array.update_attributes({
## "_ome2024_ngff_challenge_stats": stats,
## })

verify = ts.open(verify_config).result()
LOGGER.info(f"Verifying <{output_config}>\t{read.shape}\t")
for x in range(10):
Expand Down Expand Up @@ -773,6 +817,8 @@ def main(ns: argparse.Namespace, rocrate: ROCrateWriter | None = None) -> int:
strip_version(value)
ome_attrs[key] = value

add_creator(ome_attrs)

if output_config.zr_group is not None: # otherwise dry run
# dev2: everything is under 'ome' key
output_config.zr_attrs["ome"] = ome_attrs
Expand Down