Пример #1
0
def copy_xml_with_newpath(xml_in,
                          xml_out,
                          data_path,
                          path_type="relative",
                          data_format=None):
    assert path_type in ("absolute", "relative")

    if data_format is None:
        data_format = get_bdv_format(xml_in)

    # get the path node inn the xml tree
    root = ET.parse(xml_in).getroot()
    seqdesc = root.find("SequenceDescription")
    imgload = seqdesc.find("ImageLoader")
    imgload.set("format", data_format)
    et = imgload.find("hdf5")
    if et is None:
        et = imgload.find("n5")
    if et is None:
        raise RuntimeError("Could not find data node")
    et.tag = data_format.split(".")[-1]
    et.text = data_path
    et.set("type", path_type)

    indent_xml(root)
    tree = ET.ElementTree(root)
    tree.write(xml_out)
Пример #2
0
def get_seg_key_xml(xml_path, scale):
    bdv_format = get_bdv_format(xml_path)
    if bdv_format == 'bdv.hdf5':
        return get_key(True, time_point=0, setup_id=0, scale=scale)
    elif bdv_format == 'bdv.n5':
        return get_key(False, time_point=0, setup_id=0, scale=scale)
    else:
        raise RuntimeError("Invalid bdv format: %s" % bdv_format)
Пример #3
0
def _get_file_format(path):
    if not os.path.exists(path):
        raise ValueError(f"{path} does not exist.")
    elif path.endswith(".xml"):
        file_format = bdv_metadata.get_bdv_format(path)
    elif path.endswith(".ome.zarr"):
        file_format = "ome.zarr"
    else:
        raise ValueError(f"Could not infer file format from {path}.")
    return file_format
Пример #4
0
def copy_file(xml_in, xml_out, storage='local'):
    if storage == 'local':
        data_path = get_data_path(xml_in, return_absolute_path=True)
        bdv_format = get_bdv_format(xml_in)
        xml_dir = os.path.split(xml_out)[0]
        data_path = os.path.relpath(data_path, start=xml_dir)
        copy_xml_with_newpath(xml_in, xml_out, data_path,
                              path_type='relative', data_format=bdv_format)
    elif storage == 'remote':
        shutil.copyfile(xml_in, xml_out)
    else:
        raise ValueError("Invalid storage spec %s" % storage)
Пример #5
0
def copy_xml_file(xml_in, xml_out, file_format):
    if file_format in ('bdv.hdf5', 'bdv.n5'):
        data_path = get_data_path(xml_in, return_absolute_path=True)
        bdv_format = get_bdv_format(xml_in)
        xml_dir = os.path.split(xml_out)[0]
        data_path = os.path.relpath(data_path, start=xml_dir)
        copy_xml_with_newpath(xml_in,
                              xml_out,
                              data_path,
                              path_type='relative',
                              data_format=bdv_format)
    elif file_format == 'bdv.n5.s3':
        shutil.copyfile(xml_in, xml_out)
    else:
        raise ValueError(f"Invalid file format {file_format}")
Пример #6
0
def add_bdv_image(xml_path,
                  root,
                  dataset_name,
                  image_name=None,
                  file_format="bdv.n5",
                  menu_name=None,
                  scale_factors=None,
                  tmp_folder=None,
                  target="local",
                  max_jobs=multiprocessing.cpu_count(),
                  is_default_dataset=False,
                  description=None,
                  trafos_for_mobie=None,
                  move_data=False,
                  int_to_uint=False):
    """Add the image(s) specified in an bdv xml file and copy the metadata.
    """
    # find how many timepoints we have
    t_start, t_stop = bdv_metadata.get_time_range(xml_path)
    if t_stop > t_start:
        raise NotImplementedError(
            "Only a single timepoint is currently supported.")

    # get the setup ids and check that image_name is compatible
    setup_ids = bdv_metadata.get_setup_ids(xml_path)

    if image_name is None:
        image_name = [None] * len(setup_ids)
    else:
        if isinstance(image_name, str):
            image_name = [image_name]

    assert len(image_name) == len(setup_ids)

    data_path = bdv_metadata.get_data_path(xml_path, return_absolute_path=True)

    # get the key for the input data format
    input_format = bdv_metadata.get_bdv_format(xml_path)

    move_only = False
    if move_data:
        if input_format == file_format:
            move_only = True
        else:
            print(
                "Different input format than target format. Will convert data instead of moving it."
            )

        if len(setup_ids) > 1:
            move_only = False
            print(
                "Cannot move XML with multiple setups. Will convert data instead of moving it."
            )

    for setup_id, name in zip(setup_ids, image_name):
        input_key = get_key(input_format == "bdv.hdf5",
                            timepoint=t_start,
                            setup_id=setup_id,
                            scale=0)

        # get the resolution, scale_factors, chunks and unit
        resolution = bdv_metadata.get_resolution(xml_path, setup_id)
        if scale_factors is None:
            scale_factors = get_scale_factors(data_path, setup_id)
            scale_factors = absolute_to_relative_scale_factors(
                scale_factors)[1:]
        with open_file(data_path, "r") as f:
            chunks = f[input_key].chunks
        unit = bdv_metadata.get_unit(xml_path, setup_id)

        # get the name of this source
        if name is None:
            name = bdv_metadata.get_name(xml_path, setup_id)

        # get the view (=MoBIE metadata) and transformation (=bdv metadata)
        # from the input bdv metadata
        view, transformation = _view_and_trafo_from_xml(
            xml_path, setup_id, t_start, name, menu_name, trafos_for_mobie)

        tmp_folder_ = None if tmp_folder is None else f"{tmp_folder}_{name}"
        add_image(data_path,
                  input_key,
                  root,
                  dataset_name,
                  image_name=name,
                  resolution=resolution,
                  scale_factors=scale_factors,
                  chunks=chunks,
                  file_format=file_format,
                  menu_name=menu_name,
                  tmp_folder=tmp_folder_,
                  target=target,
                  max_jobs=max_jobs,
                  unit=unit,
                  view=view,
                  transformation=transformation,
                  is_default_dataset=is_default_dataset,
                  description=description,
                  move_only=move_only,
                  int_to_uint=int_to_uint)