cytometer/data.py
def aida_get_contours(annotations, layer_name='.*', return_props=False):
    """
    Concatenate items as contours in an AIDA annotations file or dict. Only 'path' and 'rectangle' types implemented.

    :param annotations: filename or dict with AIDA annotations.
    :param layer_name: (def '.*', which matches any name). Regular expression (see help for re module) that will be used
    as the pattern to march against the layer names. For example, layer_name='White adipocyte.*' will match any layer
    name like 'White adipocyte 0', 'White adipocyte 1', etc.
    :param return_props: (def False). Return extra output variable with properties of contours.
    :return:
    * items: list of concatenated contours from selected layers. [contour_0, contour_1, ...] where
    contour_i = [[x0, y0], [x1, y1], ...].
    * props: dictionary of contour properties. Currently, only implemented output is
        props['cell_prob']
    """

    # if filename provided, load annotations
    if isinstance(annotations, six.string_types):

        # parse the json file
        with open(annotations) as fp:
            annotations = ujson.load(fp)

    if type(annotations) != dict:
        raise TypeError('annotations must be type dict')

    items = []
    if return_props:
        cell_prob = []
    for l in range(len(annotations['layers'])):

        # check whether the regular expression matches the layer name
        if re.match(layer_name, annotations['layers'][l]['name']) is not None:

            for i in range(len(annotations['layers'][l]['items'])):

                if annotations['layers'][l]['items'][i]['type'] == 'path':

                    # add items to list of output items
                    items += [annotations['layers'][l]['items'][i]['segments'],]
                    if return_props:
                        cell_prob += [annotations['layers'][l]['items'][i]['cell_prob'],]

                elif annotations['layers'][l]['items'][i]['type'] == 'rectangle':

                    # extract rectangle first and last corners
                    x0 = annotations['layers'][l]['items'][i]['x']
                    y0 = annotations['layers'][l]['items'][i]['y']
                    xend = x0 + annotations['layers'][l]['items'][i]['width'] - 1
                    yend = y0 + annotations['layers'][l]['items'][i]['height'] - 1

                    # 4 corners of the rectangle, with first repeated for closed contour
                    item = [[x0, y0], [xend, y0], [xend, yend], [x0, yend], [x0, y0]]

                    # add items to list of output items
                    items += [item,]

                else:

                    warnings.warn('Unknown item type found: layer ' + str(l) + ', item ' + str(i) + ': '
                                  + annotations['layers'][l]['items'][i]['type'], SyntaxWarning)

    if return_props:
        return items, {'cell_prob':cell_prob}
    else:
        return items

    Concatenate items as contours in an AIDA annotations file or dict. Only 'path' and 'rectangle' types implemented.

    :param annotations: filename or dict with AIDA annotations.
    :param layer_name: (def '.*', which matches any name). Regular expression (see help for re module) that will be used
    as the pattern to march against the layer names. For example, layer_name='White adipocyte.*' will match any layer
    name like 'White adipocyte 0', 'White adipocyte 1', etc.
    :param return_props: (def False). Return extra output variable with properties of contours.
    :return:
    * items: list of concatenated contours from selected layers. [contour_0, contour_1, ...] where
    contour_i = [[x0, y0], [x1, y1], ...].
    * props: dictionary of contour properties. Currently, only implemented output is
        props['cell_prob']
    
Concatenate items as contours in an AIDA annotations file or dict. Only 'path' and 'rectangle' types implemented.
Concatenate items as contours in an AIDA annotations file or dict . Only ' path ' and ' rectangle ' types implemented .
def aida_get_contours(annotations, layer_name='.*', return_props=False):
    if isinstance(annotations, six.string_types):
        with open(annotations) as fp:
            annotations = ujson.load(fp)
    if type(annotations) != dict:
        raise TypeError('annotations must be type dict')
    items = []
    if return_props:
        cell_prob = []
    for l in range(len(annotations['layers'])):
        if re.match(layer_name, annotations['layers'][l]['name']) is not None:
            for i in range(len(annotations['layers'][l]['items'])):
                if annotations['layers'][l]['items'][i]['type'] == 'path':
                    items += [annotations['layers'][l]['items'][i]['segments'],]
                    if return_props:
                        cell_prob += [annotations['layers'][l]['items'][i]['cell_prob'],]
                elif annotations['layers'][l]['items'][i]['type'] == 'rectangle':
                    x0 = annotations['layers'][l]['items'][i]['x']
                    y0 = annotations['layers'][l]['items'][i]['y']
                    xend = x0 + annotations['layers'][l]['items'][i]['width'] - 1
                    yend = y0 + annotations['layers'][l]['items'][i]['height'] - 1
                    item = [[x0, y0], [xend, y0], [xend, yend], [x0, yend], [x0, y0]]
                    items += [item,]
                else:
                    warnings.warn('Unknown item type found: layer ' + str(l) + ', item ' + str(i) + ': '
                                  + annotations['layers'][l]['items'][i]['type'], SyntaxWarning)
    if return_props:
        return items, {'cell_prob':cell_prob}
    else:
        return items
def aida_get_contours ( annotations , layer_name = '.*' , return_props = False ) : if isinstance ( annotations , six . string_types ) : with open ( annotations ) as fp : annotations = ujson . load ( fp ) if type ( annotations ) != dict : raise TypeError ( 'annotations must be type dict' ) items = [ ] if return_props : cell_prob = [ ] for l in range ( len ( annotations [ 'layers' ] ) ) : if re . match ( layer_name , annotations [ 'layers' ] [ l ] [ 'name' ] ) is not None : for i in range ( len ( annotations [ 'layers' ] [ l ] [ 'items' ] ) ) : if annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'type' ] == 'path' : items += [ annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'segments' ] , ] if return_props : cell_prob += [ annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'cell_prob' ] , ] elif annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'type' ] == 'rectangle' : x0 = annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'x' ] y0 = annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'y' ] xend = x0 + annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'width' ] - 1 yend = y0 + annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'height' ] - 1 item = [ [ x0 , y0 ] , [ xend , y0 ] , [ xend , yend ] , [ x0 , yend ] , [ x0 , y0 ] ] items += [ item , ] else : warnings . warn ( 'Unknown item type found: layer ' + str ( l ) + ', item ' + str ( i ) + ': ' + annotations [ 'layers' ] [ l ] [ 'items' ] [ i ] [ 'type' ] , SyntaxWarning ) if return_props : return items , { 'cell_prob' : cell_prob } else : return items
Concatenate items as contours in an AIDA annotations file or dict.
Concatenate items as contours in an AIDA annotations file or dict .
"""
    Concatenate items as contours in an AIDA annotations file or dict. Only 'path' and 'rectangle' types implemented.

    :param annotations: filename or dict with AIDA annotations.
    :param layer_name: (def '.*', which matches any name). Regular expression (see help for re module) that will be used
    as the pattern to march against the layer names. For example, layer_name='White adipocyte.*' will match any layer
    name like 'White adipocyte 0', 'White adipocyte 1', etc.
    :param return_props: (def False). Return extra output variable with properties of contours.
    :return:
    * items: list of concatenated contours from selected layers. [contour_0, contour_1, ...] where
    contour_i = [[x0, y0], [x1, y1], ...].
    * props: dictionary of contour properties. Currently, only implemented output is
        props['cell_prob']
    """ # if filename provided, load annotations # parse the json file # check whether the regular expression matches the layer name # add items to list of output items # extract rectangle first and last corners # 4 corners of the rectangle, with first repeated for closed contour # add items to list of output items
list of concatenated contours from selected layers.
list of concatenated contours from selected layers .
annotations
filename or dict with AIDA annotations.
filename or dict with AIDA annotations .
layer_name
(def '.*', which matches any name). Regular expression  that will be used
as the pattern to march against the layer names. For example, layer_name='White adipocyte.*' will match any layer
name like 'White adipocyte 0', 'White adipocyte 1', etc.
( def ' . * ' which matches any name ) . Regular expression that will be used as the pattern to march against the layer names . For example layer_name = ' White adipocyte . * ' will match any layer name like ' White adipocyte 0 ' ' White adipocyte 1 ' etc .
return_props
(def False). Return extra output variable with properties of contours.
( def False ) . Return extra output variable with properties of contours .