Beispiel #1
0
 def plane_gen():
     """ Final image is single Z and T. Each plane is rows of T-slices """
     for the_c in range(size_c):
         shape = first_line
         r_length = None  # set this for first line
         t_rows = []
         for the_t in range(size_t):
             if the_t in lines:
                 shape = lines[the_t]
             elif not use_all_times:
                 continue
             the_z = shape['theZ']
             x1, y1, x2, y2 = shape['x1'], shape['y1'], shape['x2'], \
                 shape['y2']
             row_data = roi_utils.get_line_data(pixels, x1, y1, x2, y2,
                                                line_width, the_z, the_c,
                                                the_t)
             # if the row is too long, crop - if it's too short, pad
             row_height, row_length = row_data.shape
             if r_length is None:
                 r_length = row_length
             if row_length < r_length:
                 padding = r_length - row_length
                 pad_data = zeros((row_height, padding),
                                  dtype=row_data.dtype)
                 row_data = hstack([row_data, pad_data])
             elif row_length > r_length:
                 row_data = row_data[:, 0:r_length]
             t_rows.append(row_data)
         yield vstack(t_rows)
Beispiel #2
0
 def test_get_line_data(self):
     client = self.new_client()
     image = self.create_test_image(100, 100, 1, 1, 1, client.getSession())
     id = image.id.val
     conn = BlitzGateway(client_obj=client)
     image = conn.getObject("Image", id)
     pixels = image.getPrimaryPixels()
     line = get_line_data(pixels, 10, 0, 10, 20, 10)
     assert len(line) == 10
Beispiel #3
0
 def test_get_line_data(self):
     client = self.new_client()
     image = self.create_test_image(100, 100, 1, 1, 1, client.getSession())
     id = image.id.val
     conn = BlitzGateway(client_obj=client)
     image = conn.getObject("Image", id)
     pixels = image.getPrimaryPixels()
     line_width = 10
     # vertical line
     x = 10
     line = get_line_data(pixels, x, 0, x, 20, line_width)
     assert line.shape == (line_width, 20)
     # horizontal line
     y = 5
     line = get_line_data(pixels, 10, y, 50, y, line_width)
     assert line.shape == (line_width, 40)
     # diagonal line
     line = get_line_data(pixels, 0, 0, 50, 40, line_width)
     assert line.shape[0] == line_width
Beispiel #4
0
def process_polylines(conn, script_params, image, polylines, line_width, fout):
    """
    Output data from one or more polylines on an image. Attach csv to image.

    @param polylines list of theT:T, theZ:Z, points: list of (x,y)}
    """
    pixels = image.getPrimaryPixels()
    the_cs = script_params['Channels']

    for pl in polylines:
        the_t = pl['theT']
        the_z = pl['theZ']
        roi_id = pl['id']
        points = pl['points']
        for the_c in the_cs:
            ldata = []
            for l in range(len(points) - 1):
                x1, y1 = points[l]
                x2, y2 = points[l + 1]
                if round(x1 - x2) == 0 and round(y1 - y2) == 0:
                    continue
                ld = roi_utils.get_line_data(pixels, x1, y1, x2, y2,
                                             line_width, the_z, the_c, the_t)
                ldata.append(ld)
            line_data = hstack(ldata)

            if script_params['Sum_or_Average'] == 'Sum':
                output_data = line_data.sum(axis=0)
            else:
                output_data = average(line_data, axis=0)

            line_header = script_params['Sum_or_Average'] == \
                'Average, with raw data' and 'Average,' or ""

            # Image_ID, ROI_ID, Z, T, C, Line data
            fout.write('%s,%s,%s,%s,%s,%s' %
                       (image.getId(), roi_id, the_z + 1, the_t + 1, the_c + 1,
                        line_header))
            fout.write(','.join([str(d) for d in output_data]))
            fout.write('\n')

            # Optionally output raw data for each row of raw line data
            if script_params['Sum_or_Average'] == 'Average, with raw data':
                for r in range(line_width):
                    fout.write('%s,%s,%s,%s,%s,%s,' %
                               (image.getId(), roi_id, the_z + 1, the_t + 1,
                                the_c + 1, r))
                    fout.write(','.join([str(d) for d in line_data[r]]))
                    fout.write('\n')
Beispiel #5
0
def process_lines(conn, script_params, image, lines, line_width, fout):
    """
    Creates a new kymograph Image from one or more lines.
    If one line, use this for every time point.
    If multiple lines, use the first one for length and all the remaining ones
    for x1,y1 and direction, making all subsequent lines the same length as
    the first.
    """

    pixels = image.getPrimaryPixels()
    the_cs = script_params['Channels']

    for l in lines:
        the_t = l['theT']
        the_z = l['theZ']
        roi_id = l['id']
        if round(l['x1'] - l['x2']) == 0 and round(l['y1'] - l['y2']) == 0:
            continue
        for the_c in the_cs:
            line_data = []
            line_data = roi_utils.get_line_data(pixels, l['x1'], l['y1'],
                                                l['x2'], l['y2'], line_width,
                                                the_z, the_c, the_t)

            if script_params['Sum_or_Average'] == 'Sum':
                output_data = line_data.sum(axis=0)
            else:
                output_data = average(line_data, axis=0)

            line_header = script_params['Sum_or_Average'] == \
                'Average, with raw data' and 'Average,' or ""

            # Image_ID, ROI_ID, Z, T, C, Line data
            fout.write('%s,%s,%s,%s,%s,%s' %
                       (image.getId(), roi_id, the_z + 1, the_t + 1, the_c + 1,
                        line_header))
            fout.write(','.join([str(d) for d in output_data]))
            fout.write('\n')

            # Optionally output raw data for each row of raw line data
            if script_params['Sum_or_Average'] == 'Average, with raw data':
                for r in range(line_width):
                    fout.write('%s,%s,%s,%s,%s,%s,' %
                               (image.getId(), roi_id, the_z + 1, the_t + 1,
                                the_c + 1, r))
                    fout.write(','.join([str(d) for d in line_data[r]]))
                    fout.write('\n')
Beispiel #6
0
    def plane_gen():
        """ Final image is single Z and T. Each plane is rows of T-slices """
        for the_c in range(size_c):
            shape = first_shape
            t_rows = []
            for the_t in range(size_t):
                # update shape if specified for this timepoint
                if the_t in polylines:
                    shape = polylines[the_t]
                elif not use_all_times:
                    continue
                line_data = []
                points = shape['points']
                the_z = shape['theZ']
                for l in range(len(points) - 1):
                    x1, y1 = points[l]
                    x2, y2 = points[l + 1]
                    ld = roi_utils.get_line_data(pixels, x1, y1, x2, y2,
                                                 line_width, the_z, the_c,
                                                 the_t)
                    line_data.append(ld)
                row_data = hstack(line_data)
                t_rows.append(row_data)

            # have to handle any mismatch in line lengths by padding shorter
            # rows
            longest = max([row_array.shape[1] for row_array in t_rows])
            for t in range(len(t_rows)):
                t_row = t_rows[t]
                row_height, row_length = t_row.shape
                if row_length < longest:
                    padding = longest - row_length
                    pad_data = zeros((row_height, padding), dtype=t_row.dtype)
                    t_rows[t] = hstack([t_row, pad_data])
            c_data = vstack(t_rows)
            yield c_data