cytometer/data.py
def read_paths_from_svg_file(file, tag='Cell', add_offset_from_filename=False, minimum_npoints=3):
    """
    Read a SVG file produced by Gimp that contains paths (contours), and return a list of paths, where each path
    is a list of (X,Y) point coordinates.

    Only paths that have a label that starts with the chosen tag are read. This allows having different types of
    objects in the SVG file (e.g. cells, edge cells, background, etc), but only read one type of objects.

    :param file: path and name of SVG file.
    :param tag: (def 'Cell'). Only paths with a label that starts with this tag will be read. The case (upper/lowercase)
    is ignored.
    :param add_offset_from_filename: (def False)
    :param minimum_npoints: (def 3) Contours with less than this number of points will be ignored.
    :return: [ path0, path1, ...] = [ [(X0,Y0), (X1,Y1), ...], ...]
    """

    # convert tag to lowercase, to avoid differentiating between "Cell" and "cell"
    tag = tag.lower()

    # extract contour as a list of (X,Y) coordinates
    def extract_contour(path, x_offset=0, y_offset=0):

        contour = []
        for pt in path:

            # (X, Y) for each point
            contour.append((np.real(pt.start) + x_offset, np.imag(pt.start) + y_offset))

            if DEBUG:
                plt.plot(*zip(*contour))

        return contour

    # extract all paths from the SVG file
    paths, attributes = svg2paths(file)

    # add offset to point coordinates from the file name
    if add_offset_from_filename:
        file_basename = os.path.basename(file)  # remove path from filename
        file_basename, _ = os.path.splitext(file_basename)  # remove extension
        file_basename = file_basename.split('_')
        row_i = file_basename.index('row')
        y_offset = float(file_basename[row_i + 1])
        col_i = file_basename.index('col')
        x_offset = float(file_basename[col_i + 1])
    else:
        x_offset = 0
        y_offset = 0

    # loop paths
    paths_out = []
    for path, attribute in zip(paths, attributes):

        # convert the name of the object to lowercase, to avoid differentiating between "Cell" and "cell"
        attribute_id = attribute['id'].lower()

        # skip if the contour's name doesn't start with the required tag, e.g. 'Cell'
        if not attribute_id.startswith(tag):
            continue

        # extract contour polygon from the path object
        contour = extract_contour(path, x_offset=x_offset, y_offset=y_offset)

        # if the contour has enough points, append to the output
        if len(contour) >= minimum_npoints:
            paths_out.append(contour)

    return paths_out

    Read a SVG file produced by Gimp that contains paths (contours), and return a list of paths, where each path
    is a list of (X,Y) point coordinates.

    Only paths that have a label that starts with the chosen tag are read. This allows having different types of
    objects in the SVG file (e.g. cells, edge cells, background, etc), but only read one type of objects.

    :param file: path and name of SVG file.
    :param tag: (def 'Cell'). Only paths with a label that starts with this tag will be read. The case (upper/lowercase)
    is ignored.
    :param add_offset_from_filename: (def False)
    :param minimum_npoints: (def 3) Contours with less than this number of points will be ignored.
    :return: [ path0, path1, ...] = [ [(X0,Y0), (X1,Y1), ...], ...]
    
Read a SVG file produced by Gimp that contains paths (contours), and return a list of paths, where each path
is a list of (X,Y) point coordinates.

Only paths that have a label that starts with the chosen tag are read. This allows having different types of
objects in the SVG file , but only read one type of objects.
Read a SVG file produced by Gimp that contains paths ( contours ) and return a list of paths where each path is a list of ( X Y ) point coordinates . Only paths that have a label that starts with the chosen tag are read . This allows having different types of objects in the SVG file but only read one type of objects .
def read_paths_from_svg_file(file, tag='Cell', add_offset_from_filename=False, minimum_npoints=3):
    tag = tag.lower()
    def extract_contour(path, x_offset=0, y_offset=0):
        contour = []
        for pt in path:
            contour.append((np.real(pt.start) + x_offset, np.imag(pt.start) + y_offset))
            if DEBUG:
                plt.plot(*zip(*contour))
        return contour
    paths, attributes = svg2paths(file)
    if add_offset_from_filename:
        file_basename = os.path.basename(file)  
        file_basename, _ = os.path.splitext(file_basename)  
        file_basename = file_basename.split('_')
        row_i = file_basename.index('row')
        y_offset = float(file_basename[row_i + 1])
        col_i = file_basename.index('col')
        x_offset = float(file_basename[col_i + 1])
    else:
        x_offset = 0
        y_offset = 0
    paths_out = []
    for path, attribute in zip(paths, attributes):
        attribute_id = attribute['id'].lower()
        if not attribute_id.startswith(tag):
            continue
        contour = extract_contour(path, x_offset=x_offset, y_offset=y_offset)
        if len(contour) >= minimum_npoints:
            paths_out.append(contour)
    return paths_out
def read_paths_from_svg_file ( file , tag = 'Cell' , add_offset_from_filename = False , minimum_npoints = 3 ) : tag = tag . lower ( ) def extract_contour ( path , x_offset = 0 , y_offset = 0 ) : contour = [ ] for pt in path : contour . append ( ( np . real ( pt . start ) + x_offset , np . imag ( pt . start ) + y_offset ) ) if DEBUG : plt . plot ( * zip ( * contour ) ) return contour paths , attributes = svg2paths ( file ) if add_offset_from_filename : file_basename = os . path . basename ( file ) file_basename , _ = os . path . splitext ( file_basename ) file_basename = file_basename . split ( '_' ) row_i = file_basename . index ( 'row' ) y_offset = float ( file_basename [ row_i + 1 ] ) col_i = file_basename . index ( 'col' ) x_offset = float ( file_basename [ col_i + 1 ] ) else : x_offset = 0 y_offset = 0 paths_out = [ ] for path , attribute in zip ( paths , attributes ) : attribute_id = attribute [ 'id' ] . lower ( ) if not attribute_id . startswith ( tag ) : continue contour = extract_contour ( path , x_offset = x_offset , y_offset = y_offset ) if len ( contour ) >= minimum_npoints : paths_out . append ( contour ) return paths_out
Read a SVG file produced by Gimp that contains paths (contours), and return a list of paths, where each path
is a list of (X,Y) point coordinates.
Read a SVG file produced by Gimp that contains paths ( contours ) and return a list of paths where each path is a list of ( X Y ) point coordinates .
"""
    Read a SVG file produced by Gimp that contains paths (contours), and return a list of paths, where each path
    is a list of (X,Y) point coordinates.

    Only paths that have a label that starts with the chosen tag are read. This allows having different types of
    objects in the SVG file (e.g. cells, edge cells, background, etc), but only read one type of objects.

    :param file: path and name of SVG file.
    :param tag: (def 'Cell'). Only paths with a label that starts with this tag will be read. The case (upper/lowercase)
    is ignored.
    :param add_offset_from_filename: (def False)
    :param minimum_npoints: (def 3) Contours with less than this number of points will be ignored.
    :return: [ path0, path1, ...] = [ [(X0,Y0), (X1,Y1), ...], ...]
    """ # convert tag to lowercase, to avoid differentiating between "Cell" and "cell" # extract contour as a list of (X,Y) coordinates # (X, Y) for each point # extract all paths from the SVG file # add offset to point coordinates from the file name # remove path from filename # remove extension # loop paths # convert the name of the object to lowercase, to avoid differentiating between "Cell" and "cell" # skip if the contour's name doesn't start with the required tag, e.g. 'Cell' # extract contour polygon from the path object # if the contour has enough points, append to the output
None
file
path and name of SVG file.
path and name of SVG file .
tag
None
add_offset_from_filename
None
minimum_npoints
(def 3) Contours with less than this number of points will be ignored.
( def 3 ) Contours with less than this number of points will be ignored .