Skip to content

Commit

Permalink
Merge pull request #13 from ome/dependabot/github_actions/actions-a22…
Browse files Browse the repository at this point in the history
…372143e

build(deps): bump codecov/codecov-action from 4.3.0 to 4.5.0 in the actions group
  • Loading branch information
joshmoore authored Jul 31, 2024
2 parents 2d7cb4f + d59787c commit 0d93df8
Show file tree
Hide file tree
Showing 25 changed files with 338 additions and 282 deletions.
5 changes: 3 additions & 2 deletions .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ full_name: Josh Moore
license: BSD
org: ome
project_name: ome2024-ngff-challenge
project_short_description: Tools for converting OME-Zarr data within the ome2024-ngff-challenge
(see https://forum.image.sc/t/ome2024-ngff-challenge/97363)
project_short_description:
Tools for converting OME-Zarr data within the ome2024-ngff-challenge (see
https://forum.image.sc/t/ome2024-ngff-challenge/97363)
url: https://github.com/ome/ome2024-ngff-challenge
vcs: true
10 changes: 3 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.12"]
runs-on: [ubuntu-latest, macos-latest, windows-latest]

include:
- python-version: pypy-3.10
runs-on: ubuntu-latest
python-version: ["3.10", "3.12"]
runs-on: [ubuntu-latest]

steps:
- uses: actions/checkout@v4
Expand All @@ -68,6 +64,6 @@ jobs:
--durations=20
- name: Upload coverage report
uses: codecov/codecov-action@v4.3.0
uses: codecov/codecov-action@v4.5.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repos:
rev: "v0.4.1"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
args: ["--fix", "--show-fixes", "--exclude=dev*"]
- id: ruff-format

# - repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
76 changes: 44 additions & 32 deletions dev1/convert_zarr_v2_to_v3/convert_zarr_v2_to_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
def convert_zarr_v2_to_v3(input_url, output_url, is_root=True):
# Uses Zarr v2 for reading, Zarr(ita) v3 for writing
# reader
input_root = zarr.open_group(store=input_url) # does not seem to support https URLs
print(f'Processing {input_root.store.path}')
input_root = zarr.open_group(store=input_url) # does not seem to support https URLs
print(f"Processing {input_root.store.path}")

if is_root:
if input_root:
Expand All @@ -24,7 +24,7 @@ def convert_zarr_v2_to_v3(input_url, output_url, is_root=True):
root = zarrita.Group.create(store=output_url)
root.update_attributes(update_omezarr_attributes(input_root.attrs.asdict()))
else:
raise FileNotFoundError(f'Error parsing {input_url}')
raise FileNotFoundError(f"Error parsing {input_url}")

# writer
for label in input_root:
Expand All @@ -33,29 +33,33 @@ def convert_zarr_v2_to_v3(input_url, output_url, is_root=True):
output_path = output_url + content.name
if isinstance(content, zarr.Group):
# create zarr group
zarrita.Group.create(store=output_path,
attributes=update_omezarr_attributes(content.attrs.asdict()))
zarrita.Group.create(
store=output_path,
attributes=update_omezarr_attributes(content.attrs.asdict()),
)
convert_zarr_v2_to_v3(input_path, output_path, is_root=False)
elif isinstance(content, zarr.Array):
codecs = [bytes_codec(), blosc_codec(typesize=4)]
output_array = zarrita.Array.create(output_path,
shape=content.shape,
chunk_shape=content.chunks,
dtype=content.dtype,
codecs=codecs,
attributes=update_omezarr_attributes(content.attrs.asdict()))
output_array = zarrita.Array.create(
output_path,
shape=content.shape,
chunk_shape=content.chunks,
dtype=content.dtype,
codecs=codecs,
attributes=update_omezarr_attributes(content.attrs.asdict()),
)
output_array[:] = content
else:
print(f'Unsupported content {content}')
print(f"Unsupported content {content}")


def convert_ome_zarr_v2_to_v3(input_url, output_url):
# Uses Ome-Zarr v2 for reading, Zarr(ita) v3 for writing
location = parse_url(input_url) # supports https URLs*
location = parse_url(input_url) # supports https URLs*
if location is None:
# * under particular conditions the URL/zarr is not detected (internal .exists() returns False)
# caused by OS / PyCharm / version / pytest?
raise FileNotFoundError(f'Error parsing {input_url}')
raise FileNotFoundError(f"Error parsing {input_url}")

reader = Reader(location)
input_root_path = os.path.normpath(reader.zarr.path)
Expand All @@ -65,34 +69,42 @@ def convert_ome_zarr_v2_to_v3(input_url, output_url):
shutil.rmtree(output_url)

for image_node in reader():
print(f'Processing {image_node}')
print(f"Processing {image_node}")
metadata = image_node.metadata
axes = metadata.get('axes', [])
dimension_order = ''.join([axis.get('name') for axis in axes])
output_path = os.path.normpath(image_node.zarr.path).replace(input_root_path, output_url)
axes = metadata.get("axes", [])
dimension_order = "".join([axis.get("name") for axis in axes])
output_path = os.path.normpath(image_node.zarr.path).replace(
input_root_path, output_url
)
output_store_path = zarrita.store.make_store_path(output_path)
# create zarr group
output_zarr = zarrita.Group.create(store=output_store_path,
attributes=update_omezarr_attributes(image_node.zarr.root_attrs))
output_zarr = zarrita.Group.create(
store=output_store_path,
attributes=update_omezarr_attributes(image_node.zarr.root_attrs),
)

for level, data in enumerate(image_node.data):
codecs = [bytes_codec(), blosc_codec(typesize=4)]
# create zarr array
output_array = output_zarr.create_array(str(level),
shape=data.shape,
chunk_shape=data.chunksize,
dtype=data.dtype,
codecs=codecs)
output_array[:] = np.array(data) # set only supports ndarray; TODO: wrap inside (dask) chunking?
output_array = output_zarr.create_array(
str(level),
shape=data.shape,
chunk_shape=data.chunksize,
dtype=data.dtype,
codecs=codecs,
)
output_array[:] = np.array(
data
) # set only supports ndarray; TODO: wrap inside (dask) chunking?


if __name__ == '__main__':
#input_url = 'D:/slides/6001240.zarr'
input_url = 'https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr'
output_url = 'D:/slides/test/' + os.path.basename(input_url)
if __name__ == "__main__":
# input_url = 'D:/slides/6001240.zarr'
input_url = "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0062A/6001240.zarr"
output_url = "D:/slides/test/" + os.path.basename(input_url)

#convert_zarr_v2_to_v3(input_url, output_url)
# convert_zarr_v2_to_v3(input_url, output_url)

convert_ome_zarr_v2_to_v3(input_url, output_url)

print('done')
print("done")
20 changes: 10 additions & 10 deletions dev1/convert_zarr_v2_to_v3/environmentv2_3.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: ome2024-ngff-challenge-v2_3
channels:
- ome
- conda-forge
- defaults
- ome
- conda-forge
- defaults
dependencies:
- python=3.10
- numpy
- zarr==2.18
- ome-zarr==0.9
- pip
- pip:
- zarrita
- python=3.10
- numpy
- zarr==2.18
- ome-zarr==0.9
- pip
- pip:
- zarrita
16 changes: 12 additions & 4 deletions dev1/convert_zarr_v2_to_v3/util.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
def update_omezarr_attributes(attributes):
new_attributes = replace_attributes_value(attributes, search_label='version', new_value='0.5-dev1')
new_attributes = replace_attributes_value(
attributes, search_label="version", new_value="0.5-dev1"
)
return new_attributes


def replace_attributes_value(values, search_label, new_value):
if isinstance(values, dict):
new_values = {}
for label, value in values.items():
if isinstance(label, str) and not label.startswith('_'):
new_values[label] = replace_attributes_value(value, search_label=search_label, new_value=new_value)
if isinstance(label, str) and not label.startswith("_"):
new_values[label] = replace_attributes_value(
value, search_label=search_label, new_value=new_value
)
else:
new_values[label] = value
if search_label in values:
new_values[search_label] = new_value
elif isinstance(values, list):
new_values = []
for item in values:
new_values.append(replace_attributes_value(item, search_label=search_label, new_value=new_value))
new_values.append(
replace_attributes_value(
item, search_label=search_label, new_value=new_value
)
)
else:
new_values = values
return new_values
4 changes: 2 additions & 2 deletions dev1/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ channels:
- conda-forge
dependencies:
- tensorstore
- 'numpy<2'
- zarr # loads dependencies
- "numpy<2"
- zarr # loads dependencies
- pip:
- "--editable=git+https://github.com/will-moore/ome-zarr-py.git@zarr_v3_support#egg=ome-zarr"
- "--editable=git+https://github.com/zarr-developers/zarr-python.git@v3#egg=zarr"
2 changes: 1 addition & 1 deletion dev1/environment_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
dependencies:
- python=3.10
- numpy<2
- zarr # loads dependencies
- zarr # loads dependencies
- pip
- pip:
- tensorstore
Expand Down
56 changes: 31 additions & 25 deletions dev1/resave.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tensorstore as ts

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("input_path")
parser.add_argument("output_path")
Expand All @@ -22,32 +23,38 @@


def convert_array(input_path, output_path):
read = ts.open({
'driver': 'zarr',
'kvstore': {
'driver': 'file',
'path': input_path,
},
}).result()
read = ts.open(
{
"driver": "zarr",
"kvstore": {
"driver": "file",
"path": input_path,
},
}
).result()

shape = read.shape
chunks= read.schema.chunk_layout.read_chunk.shape
chunks = read.schema.chunk_layout.read_chunk.shape

write = ts.open({
"driver": "zarr3",
"kvstore": {
"driver": "file",
"path": output_path
},
"metadata": {
"shape": shape,
"chunk_grid": {"name": "regular", "configuration": {"chunk_shape": chunks}},
"chunk_key_encoding": {"name": "default"},
"codecs": [{"name": "blosc", "configuration": {"cname": "lz4", "clevel": 5}}],
"data_type": read.dtype,
},
"create": True,
}).result()
write = ts.open(
{
"driver": "zarr3",
"kvstore": {"driver": "file", "path": output_path},
"metadata": {
"shape": shape,
"chunk_grid": {
"name": "regular",
"configuration": {"chunk_shape": chunks},
},
"chunk_key_encoding": {"name": "default"},
"codecs": [
{"name": "blosc", "configuration": {"cname": "lz4", "clevel": 5}}
],
"data_type": read.dtype,
},
"create": True,
}
).result()

future = write.write(read)
future.result()
Expand Down Expand Up @@ -76,6 +83,5 @@ def convert_array(input_path, output_path):
for ds in multiscales[0]["datasets"]:
ds_path = ds["path"]
convert_array(
os.path.join(ns.input_path, ds_path),
os.path.join(ns.output_path, ds_path)
os.path.join(ns.input_path, ds_path), os.path.join(ns.output_path, ds_path)
)
19 changes: 9 additions & 10 deletions dev2/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.5
hooks:
# Run the linter.
- id: ruff
args: [ "--fix" ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.5
hooks:
# Run the linter.
- id: ruff
args: ["--fix"]
# Run the formatter.
- id: ruff-format
6 changes: 3 additions & 3 deletions dev2/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ channels:
dependencies:
- napari
- pyqt
- 'numpy<2'
- "numpy<2"
- tensorstore # loads dependencies
- zarr # loads dependencies
- zarr # loads dependencies
- tqdm
- pip
- pip:
- "--editable=git+https://github.com/will-moore/ome-zarr-py.git@zarr_v3_support#egg=ome-zarr"
- "--editable=git+https://github.com/zarr-developers/zarr-python.git@v3#egg=zarr"
- "--editable=git+https://github.com/ome/napari-ome-zarr.git@v3#egg=napari-ome-zarr"
- 'tensorstore>=0.1.63'
- "tensorstore>=0.1.63"
4 changes: 2 additions & 2 deletions dev2/resave.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TSMetrics:
Instances of this class capture the current tensorstore metrics.
If an existing instance is passed in on creation, it will be stored
in order to deduct prevoius values from those measured by this instance.
in order to deduct previous values from those measured by this instance.
"""

CHUNK_CACHE_READS = "/tensorstore/cache/chunk_cache/reads"
Expand Down Expand Up @@ -217,7 +217,7 @@ def convert_array(
future.result()
after = TSMetrics(read_config, write_config, before)

print(f"""Reencode (tensorstore) {input_path} to {output_path}
print(f"""Re-encode (tensorstore) {input_path} to {output_path}
read: {after.read()}
write: {after.written()}
time: {after.elapsed()}
Expand Down
Loading

0 comments on commit 0d93df8

Please sign in to comment.