Skip to content

Commit e5fa1f0

Browse files
Add more xml metadata functions
1 parent 4713929 commit e5fa1f0

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

pybdv/downsample.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def downsample(path, in_key, out_key, factor, mode, n_threads=1, overwrite=False
129129
if overwrite and out_key in f:
130130
del f[out_key]
131131

132-
ds_out = f.create_dataset(out_key, shape=sampled_shape, chunks=chunks,
133-
compression='gzip', dtype=ds_in.dtype)
132+
ds_out = f.require_dataset(out_key, shape=sampled_shape, chunks=chunks,
133+
compression='gzip', dtype=ds_in.dtype)
134134

135135
def sample_chunk(bb):
136136

pybdv/metadata.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _check_setup(vs):
6363
if overwrite_data:
6464
vs.find('size').text = '{} {} {}'.format(nx, ny, nz)
6565
else:
66-
raise ValueError("Incompatible dataset size")
66+
raise ValueError(f"Incompatible dataset size: {shape_exp}, {(nx, ny, nz)}")
6767

6868
# check the voxel size
6969
vox = vs.find('voxelSize')
@@ -589,7 +589,7 @@ def _check_affine(trafo):
589589
for aff in affine.values():
590590
_check_affine(aff)
591591
else:
592-
raise ValueError("Invalid type for affine transformatin, expect list or dict, got %s" % type(affine))
592+
raise ValueError("Invalid type for affine transformation, expect list or dict, got %s" % type(affine))
593593

594594

595595
def _write_transformation(vregs, setup_id, timepoint, affine, resolution, overwrite):
@@ -784,6 +784,19 @@ def get_resolution(xml_path, setup_id):
784784
raise ValueError("Could not find setup %i" % setup_id)
785785

786786

787+
def get_size(xml_path, setup_id):
788+
tree = ET.parse(xml_path)
789+
root = tree.getroot()
790+
seqdesc = root.find('SequenceDescription')
791+
viewsets = seqdesc.find('ViewSetups')
792+
vsetups = viewsets.findall('ViewSetup')
793+
for vs in vsetups:
794+
if vs.find('id').text == str(setup_id):
795+
size = vs.find('size').text
796+
return tuple(int(siz) for siz in size.split())[::-1]
797+
raise ValueError("Could not find setup %i" % setup_id)
798+
799+
787800
def get_data_path(xml_path, return_absolute_path=False):
788801
""" Get path to the data.
789802
@@ -805,3 +818,45 @@ def get_data_path(xml_path, return_absolute_path=False):
805818
path = os.path.join(os.path.split(xml_path)[0], path)
806819
path = os.path.abspath(os.path.relpath(path))
807820
return path
821+
822+
823+
#
824+
# helper functions to write additional xml metadata
825+
#
826+
827+
828+
def write_size_and_resolution(xml_path, setup_id, size, resolution):
829+
""" Write size and resolution data.
830+
"""
831+
832+
if size is not None and len(size) != 3:
833+
raise ValueError(f"Expected size of length 3 instead of {len(size)}")
834+
if resolution is not None and len(resolution) != 3:
835+
raise ValueError(f"Expected resolution of length 3 instead of {len(resolution)}")
836+
837+
tree = ET.parse(xml_path)
838+
root = tree.getroot()
839+
seqdesc = root.find('SequenceDescription')
840+
viewsets = seqdesc.find('ViewSetups')
841+
vsetups = viewsets.findall('ViewSetup')
842+
843+
found_setup = False
844+
for vs in vsetups:
845+
if vs.find('id').text == str(setup_id):
846+
size_elem = vs.find('size')
847+
if size is not None:
848+
size_elem.text = ' '.join(map(str, size[::-1]))
849+
850+
res_elem = vs.find('voxelSize').find('size')
851+
if resolution is not None:
852+
res_elem.text = ' '.join(map(str, resolution[::-1]))
853+
854+
found_setup = True
855+
856+
if found_setup:
857+
# write the xml
858+
indent_xml(root)
859+
tree = ET.ElementTree(root)
860+
tree.write(xml_path)
861+
else:
862+
raise ValueError("Could not find setup %i" % setup_id)

0 commit comments

Comments
 (0)