Exemplo n.º 1
0
def pull_anno(roi_fname: String):
    roi_ws = ws.get(roi_fname)
    ds = ws.get_dataset(roi_fname, '001_level', group="annotations")
    roi_parts = roi_fname.split("_")
    z_min = int(roi_parts[-6])
    z_max = int(roi_parts[-5])
    x_min = int(roi_parts[-4])
    x_max = int(roi_parts[-3])
    y_min = int(roi_parts[-2])
    y_max = int(roi_parts[-1])

    dst = DataModel.g.dataset_uri('001_level', group="annotations")
    main_anno = dataset_from_uri(dst, mode="rw")
    main_anno[z_min:z_max, x_min:x_max, y_min:y_max] = ds[:]
Exemplo n.º 2
0
def set_label_parent(
    workspace: String,
    level: String,
    label_idx: Int,
    parent_level: String,
    parent_label_idx: Int,
    parent_color: String,
):
    ds = ws.get_dataset(workspace, level, group=__group_pattern__)
    labels = ds.get_metadata("labels", {})
    logger.debug(f"Setting label parent using dataset {ds} and with labels {labels}")

    if label_idx in labels:
        labels[label_idx]["parent_level"] = parent_level
        labels[label_idx]["parent_label"] = parent_label_idx
        labels[label_idx]["parent_color"] = parent_color
    ds.set_metadata("labels", labels)
Exemplo n.º 3
0
def render_workspace(request,
                     slice_idx: Int,
                     workspace: String,
                     max_size: IntListOrNone = None,
                     binning: Int = 1,
                     clim: FloatList = [0, 1],
                     png: SmartBoolean = False,
                     **layers):
    """"""

    logger.info("render_workspace")
    database = request.context["session"]
    if "workspace_renderer" in database:
        renderer = database["workspace_renderer"]
    else:
        renderer = _Renderer()
        database["workspace_renderer"] = renderer

    renderer.clear()

    for i, (layer, data_tr, cmap_tr, clim_tr) in enumerate(KNOWN_LAYERS):
        if not layer in layers:
            continue
        logger.info("Rendering layer {}: {}".format(layer, slice_idx))
        dsname, cmap, alpha, visible = LayerParams(layers.pop(layer))
        if dsname == "__data__":
            data = ws.get_data(workspace)
        else:
            data = ws.get_dataset(workspace, dsname)
        data = data[slice_idx]
        renderer.data_size = data.shape
        if data_tr:
            data = data_tr(data)
        if cmap and cmap_tr:
            cmap = cmap_tr(cmap)
        if clim and clim_tr:
            if type(clim_tr) in [tuple, list]:
                clim = clim_tr
            else:
                clim = clim_tr(data, clim)

        if cmap == None:
            cmap = "gray"
        renderer.update_layer(
            layer,
            dsname,
            data,
            cmap=cmap,
            clim=clim,
            visible=visible,
            alpha=alpha,
            interp="nearest",
            order=i + 1,
        )

    if "annotations" in layers:
        n = len(KNOWN_LAYERS)
        levels, _, alpha, visible = layers.pop("annotations")
        for i, level in enumerate(levels):
            level, labels = level
            data = ws.get_dataset(workspace, level)
            data = data[slice_idx] & 15
            all_labels = get_labels(workspace, level, full=True)
            cmap = _label_cmap(all_labels, labels)
            clim = _Renderer.label_clim()
            renderer.update_layer(
                level,
                level,
                data,
                cmap=cmap,
                clim=clim,
                visible=visible,
                alpha=alpha,
                interp="nearest",
                order=i + n + 1,
            )

    renderer.render_workspace(max_size=max_size, binning=binning)

    if png:
        image = renderer.png.copy()
    else:
        image = renderer.image.copy()

    return encode_numpy(image)
Exemplo n.º 4
0
def get_single_level(workspace: String, level: String):
    ds = ws.get_dataset(workspace, level, group=__group_pattern__)
    return dataset_repr(ds)
Exemplo n.º 5
0
def get_level(workspace: String, level: String, full: SmartBoolean = False):
    if full == False:
        return ws.get_dataset(workspace, level, group=__group_pattern__)
    return ws.get_dataset(workspace, level)
Exemplo n.º 6
0
def create(workspace: String, roi_fname: String, roi: list,
           original_workspace: String, original_level):
    DataModel.g.current_workspace = original_workspace
    logger.debug(f"Original workspace: {original_workspace}")
    logger.debug(original_level)
    if original_level:
        anno_ds = ws.get_dataset(original_workspace,
                                 original_level,
                                 group="annotations")
        anno_ds_crop = anno_ds[roi[0]:roi[3], roi[1]:roi[4],
                               roi[2]:roi[5]] & 15
        original_labels = get_labels(original_workspace, original_level)
    roi_dict = {}
    DataModel.g.current_workspace = workspace

    if original_level:
        add_level(roi_fname)
        new_anno_ds = get_level(roi_fname, level="001_level")

    logger.debug(f"Switching to new workspace: {workspace}")
    src = DataModel.g.dataset_uri("__data__")
    with DatasetManager(src, out=None, dtype="float32", fillvalue=0) as DM:
        src_dataset = DM.sources[0]
        ds_metadata = src_dataset.get_metadata()
        print(ds_metadata)
        if not "roi_fnames" in ds_metadata:
            src_dataset.set_metadata("roi_fnames", roi_dict)
        else:
            roi_dict = src_dataset.get_metadata("roi_fnames")
        num_entries = len(roi_dict.keys())
        roi_dict[num_entries + 1] = roi_fname
        src_dataset.set_metadata("roi_fnames", roi_dict)
        metadata = dict()
        metadata['id'] = len(roi_dict.keys())
        metadata['name'] = roi_fname

    if original_level:
        label_values = np.unique(anno_ds_crop)

        for v in label_values:
            if v != 0:
                params = dict(
                    level="001_level",
                    idx=int(v) - 2,
                    name=str(int(v) - 2),
                    color="#11FF11",
                    workspace=True,
                )
                label_result = add_label(workspace=roi_fname,
                                         level="001_level")

        levels_result = get_single_level(roi_fname, level="001_level")

        cmap_colors = []
        for k, v in original_labels.items():
            cmap_colors.append(v['color'])

        for i, v in enumerate(levels_result["labels"].keys()):
            #label_rgb = (255 * label_rgb).astype(np.uint8)
            #label_hex = "#{:02x}{:02x}{:02x}".format(*label_rgb)
            label = dict(idx=int(v),
                         name=str(int(v) - 1),
                         color=cmap_colors[i])
            params = dict(level="001_level", workspace=roi_fname)
            label_result = update_label(**params, **label)

        new_anno_ds[:] = anno_ds_crop

    DataModel.g.current_workspace = original_workspace

    return metadata