def get_omero_data(image_id):
    conn = omero.open_connection(username=USER,
                                 password=PASSWORD,
                                 group=GROUP,
                                 port=PORT,
                                 host=HOST)
    image = omero.get_image(conn, image_id)
    image_name = image.getName()
    raw_img = omero.get_intensities(image)
    # Images from OMERO come in zctxy dimensions and locally as zcxy.
    # The easyest for the moment is to remove t from the omero image
    if raw_img.shape[2] == 1:
        raw_img = np.squeeze(raw_img, 2)
    else:
        raise Exception(
            "Image has a time dimension. Time is not yet implemented for this analysis"
        )
    pixel_size = omero.get_pixel_size(image)
    pixel_units = omero.get_pixel_size_units(image)

    conn.close()

    return {
        'image_data': raw_img,
        'image_name': image_name,
        'pixel_size': pixel_size,
        'pixel_units': pixel_units
    }
def save_line_rois(data: dict, image_id):
    conn = omero.open_connection(username=USER,
                                 password=PASSWORD,
                                 group=GROUP,
                                 port=PORT,
                                 host=HOST)
    image = omero.get_image(conn, image_id)
    nb_channels = image.getSizeC()

    for c in range(nb_channels):
        shapes = list()
        for l in range(len(data[f'ch{c:02d}_peak_positions'])):
            for p in range(2):
                if data['resolution_axis'] == 1:  # Y resolution -> horizontal rois
                    axis_len = image.getSizeX()
                    x1_pos = int((axis_len / 2) -
                                 (axis_len * data['measured_band'] / 2))
                    y1_pos = data[f'ch{c:02d}_peak_positions'][l][p]
                    x2_pos = int((axis_len / 2) +
                                 (axis_len * data['measured_band'] / 2))
                    y2_pos = data[f'ch{c:02d}_peak_positions'][l][p]
                elif data[
                        'resolution_axis'] == 2:  # X resolution -> vertical rois
                    axis_len = image.getSizeY()
                    y1_pos = int((axis_len / 2) -
                                 (axis_len * data['measured_band'] / 2))
                    x1_pos = data[f'ch{c:02d}_peak_positions'][l][p]
                    y2_pos = int((axis_len / 2) +
                                 (axis_len * data['measured_band'] / 2))
                    x2_pos = data[f'ch{c:02d}_peak_positions'][l][p]
                else:
                    raise ValueError(
                        'Only axis 1 and 2 (X and Y) are supported')

                line = omero.create_shape_line(
                    x1_pos=x1_pos + .5,
                    y1_pos=y1_pos + .5,
                    x2_pos=x2_pos + .5,
                    y2_pos=y2_pos + .5,
                    z_pos=data[f'ch{c:02d}_focus'],
                    t_pos=0,
                    line_name=f'ch{c:02d}_{l}_{p}',
                    # stroke_color=,
                    stroke_width=2)
                shapes.append(line)

        omero.create_roi(connection=conn, image=image, shapes=shapes)

    conn.close()
def save_data_key_values(key_values, image_id):

    conn = omero.open_connection(username=USER,
                                 password=PASSWORD,
                                 group=GROUP,
                                 port=PORT,
                                 host=HOST)
    try:
        map_ann = omero.create_annotation_map(connection=conn,
                                              annotation=key_values,
                                              namespace=True)
        image = omero.get_image(conn, image_id)
        omero.link_annotation(image, map_ann)

    finally:
        conn.close()
def save_data_table(table_name, names, desc, data, image_id):

    conn = omero.open_connection(username=USER,
                                 password=PASSWORD,
                                 group=GROUP,
                                 port=PORT,
                                 host=HOST)

    try:
        table_ann = omero.create_annotation_table(connection=conn,
                                                  table_name=table_name,
                                                  column_names=names,
                                                  column_descriptions=desc,
                                                  values=data)

        image = omero.get_image(conn, image_id)

        omero.link_annotation(image, table_ann)
    finally:
        conn.close()
def save_spots_point_rois(names: list, data: list, image_id):
    conn = omero.open_connection(username=USER,
                                 password=PASSWORD,
                                 group=GROUP,
                                 port=PORT,
                                 host=HOST)
    image = omero.get_image(conn, image_id)
    nb_channels = image.getSizeC()

    for c in range(nb_channels):
        shapes = list()
        for x, y, z, l in zip(
                data[names.index(f'ch{c:02d}_XWeightedCentroid')][0],
                data[names.index(f'ch{c:02d}_YWeightedCentroid')][0],
                data[names.index(f'ch{c:02d}_ZWeightedCentroid')][0],
                data[names.index(f'ch{c:02d}_MaskLabels')][0],
        ):
            shapes.append(
                omero.create_shape_point(x, y, z, shape_name=f'ch{c:02d}_{l}'))

        omero.create_roi(connection=conn, image=image, shapes=shapes)

    conn.close()