Example #1
0
def caller(inputs, inputs_labels, output, functions, params):
    make_dirs(output)

    # Make cells. cells are a list of regionproperties or subclasses.
    logger.info('Postprocess.\tcollecting cells...')
    store = []
    for frame, (path, pathl) in enumerate(zip(inputs, inputs_labels)):
        img, labels = imread(path), lbread(pathl)
        cells = regionprops(labels, img)
        cells = [Cell(cell) for cell in cells]
        for cell in cells:
            cell.frame = frame
            if frame > 0:
                all_labels = [i.label for i in store[frame - 1]]
                if cell.label in all_labels:
                    store[frame - 1][all_labels.index(cell.label)].nxt = cell
        store.append(cells)
    cells = [i for j in store for i in j]

    # Each function receives cells (regionprops) and finally return labels generated by cells.label
    for function, param in zip(functions, params):
        logger.info('\trunning {0}'.format(function))
        func = getattr(postprocess_operation, function)
        cells = func(cells, **param)

    logger.info('\tsaving images...')
    for frame, (path, pathl) in enumerate(zip(inputs, inputs_labels)):
        labels = cells2labels(cells, frame, lbread(pathl))
        imsave(labels, output, path, dtype=np.int16)
Example #2
0
def caller(inputs_list, inputs_labels_list, output, primary, secondary):
    make_dirs(dirname(abspath(output)))

    inputs_list = [inputs_list, ] if isinstance(inputs_list[0], str) else inputs_list
    inputs_labels_list = [inputs_labels_list, ] if isinstance(inputs_labels_list[0], str) else inputs_labels_list

    obj_names = [basename(dirname(i[0])) for i in inputs_labels_list] if primary is None else primary
    ch_names = [basename(dirname(i[0])) for i in inputs_list] if secondary is None else secondary

    store = []
    for inputs, ch in zip(inputs_list, ch_names):
        for inputs_labels, obj in zip(inputs_labels_list, obj_names):
            logger.info("Channel {0}: {1} applied...".format(ch, obj))
            for frame, (path, pathl) in enumerate(zip(inputs, inputs_labels)):
                img, labels = imread(path), lbread(pathl, nonneg=False)
                cells = regionprops(labels, img)
                if (labels < 0).any():
                    cells = add_parent(cells, labels)
                [setattr(cell, 'frame', frame) for cell in cells]
                cells = [Cell(cell) for cell in cells]
                store.append(cells)

            logger.info("\tmaking dataframe...")
            df = multi_index([i for ii in store for i in ii], obj, ch)
            if exists(join(output, 'df.csv')):
                ex_df = pd.read_csv(join(output, 'df.csv'), index_col=['object', 'ch', 'prop', 'frame'])
                ex_df.columns = pd.to_numeric(ex_df.columns)
                ex_df = ex_df.astype(np.float32)
                df = pd.concat([df, ex_df])
            df.to_csv(join(output, 'df.csv'))
    larr = df2larr(df)
    larr.save(join(output, 'df.npz'))
    logger.info("\tdf.npz saved.")
Example #3
0
def caller(inputs, inputs_labels, output, functions, params):
    make_dirs(output)
    inputs = inputs[FRAME_REVTRACK:0:-1] + inputs
    inputs_labels = inputs_labels[FRAME_REVTRACK:0:-1] + inputs_labels

    img0, labels0 = imread(inputs[0]), lbread(inputs_labels[0]).astype(
        np.int16)
    labels0 = neg2poslabels(labels0)
    imsave(labels0, output, basename(inputs[0]), dtype=np.int16)
    for holder.frame, (path,
                       pathl) in enumerate(zip(inputs[1:], inputs_labels[1:])):
        img1, labels1 = imread(path), lbread(pathl)
        labels1 = -labels1
        for fnum, (function, param) in enumerate(zip(functions, params)):
            func = getattr(track_operation, function)
            if not (labels1 < 0).any():
                continue
            labels0, labels1 = func(img0, img1, labels0, -labels1, **param)
            logger.debug('\t{0} with {1}: {2}'.format(
                function, param, len(set(labels1[labels1 < 0]))))
        logger.info("\tframe {0}: {1} objects linked and {2} unlinked.".format(
            holder.frame, len(set(labels1[labels1 > 0])),
            len(set(labels1[labels1 < 0]))))
        labels0 = neg2poslabels(labels1)
        img0 = img1
        imsave(labels0, output, path, dtype=np.int16)
Example #4
0
def call_operations(contents):
    make_dirs(contents['OUTPUT_DIR'])
    logging.basicConfig(filename=join(contents['OUTPUT_DIR'], 'log.txt'),
                        level=logging.DEBUG)
    logging.getLogger("PIL").setLevel(logging.WARNING)
    run_operations(contents['OUTPUT_DIR'], contents['operations'])
    logger.info("Caller finished.")
    return
Example #5
0
def caller(inputs_list, inputs_labels_list, output, primary, secondary):
    make_dirs(dirname(abspath(output)))

    inputs_list = [
        inputs_list,
    ] if isinstance(inputs_list[0], str) else inputs_list
    inputs_labels_list = [
        inputs_labels_list,
    ] if isinstance(inputs_labels_list[0], str) else inputs_labels_list

    obj_names = [basename(dirname(i[0]))
                 for i in inputs_labels_list] if primary is None else primary
    ch_names = [basename(dirname(i[0]))
                for i in inputs_list] if secondary is None else secondary

    for inputs, ch in zip(inputs_list, ch_names):
        for inputs_labels, obj in zip(inputs_labels_list, obj_names):
            logger.info("Channel {0}: {1} applied...".format(ch, obj))
            arr = np.ones((MAX_NUMCELL, len(PROP_SAVE), len(inputs)),
                          np.float32) * np.nan
            for frame, (path, pathl) in enumerate(zip(inputs, inputs_labels)):
                img, labels = imread(path), lbread(pathl, nonneg=False)
                cells = regionprops(labels, img)
                if (labels < 0).any():
                    cells = add_parent(cells, labels)
                [setattr(cell, 'frame', frame) for cell in cells]
                cells = [Cell(cell) for cell in cells]
                tarr = _cells2array(cells)
                index = tarr[:, 1].astype(np.int32)
                arr[index, :, frame] = tarr

            logger.info("\tmaking dataframe...")
            cellids = np.where(~np.isnan(arr[:, 0, :]).all(axis=1))[0]
            marr = np.zeros((len(cellids), arr.shape[1], arr.shape[2]))
            for pn, i in enumerate(cellids):
                marr[pn] = arr[i]
            sarr = np.swapaxes(marr, 0, 2)
            narr = sarr.reshape((sarr.shape[0] * sarr.shape[1], sarr.shape[2]),
                                order='F')
            index = pd.MultiIndex.from_product(
                [obj, ch, PROP_SAVE, range(arr.shape[-1])],
                names=['object', 'ch', 'prop', 'frame'])
            df = pd.DataFrame(narr, index=index, columns=cellids)

            if exists(join(output, FILE_NAME + '.csv')):
                ex_df = pd.read_csv(
                    join(output, FILE_NAME + '.csv'),
                    index_col=['object', 'ch', 'prop', 'frame'])
                ex_df.columns = pd.to_numeric(ex_df.columns)
                ex_df = ex_df.astype(np.float32)
                df = pd.concat([df, ex_df])
            df.to_csv(join(output, FILE_NAME + '.csv'))
    larr = df2larr(df)
    larr.save(join(output, FILE_NAME + '.npz'))
    logger.info("\t" + FILE_NAME + ".npz saved.")
Example #6
0
def caller(inputs, output, functions, params):
    holder.inputs = inputs
    make_dirs(output)

    logger.info("Functions {0} for {1} images.".format(functions, len(inputs)))

    for holder.frame, holder.path in enumerate(inputs):
        img = imread(holder.path)
        for function, param in zip(functions, params):
            func = getattr(preprocess_operation, function)
            img = func(img, **param)
        imsave(img, output, holder.path)
        logger.info("\tframe {0} done.".format(holder.frame))
Example #7
0
def caller(inputs, output, functions, params):
    make_dirs(output)
    logger.info("Functions {0} for {1} images.".format(functions, len(inputs)))

    for holder.frame, path in enumerate(inputs):
        img = imread(path)
        for function, param in zip(functions, params):
            func = getattr(segment_operation, function)
            img = func(img, **param)
        if isinstance(path, list) or isinstance(path, tuple):
            path = path[0]
        labels = clean_labels(img, radius)
        imsave(labels, output, path, dtype=np.int16)
        logger.info("\tframe {0}: {1} objects segmented.".format(
            holder.frame, len(np.unique(labels))))
Example #8
0
def caller(inputs, inputs_labels, output, functions, params):
    make_dirs(output)

    logger.info("Functions {0} for {1} images.".format(functions, len(inputs)))
    img = None
    for holder.frame, (path,
                       pathl) in enumerate(izip_longest(inputs,
                                                        inputs_labels)):
        if path is not None:
            img = imread(path)
        labels0 = lbread(pathl)
        for function, param in zip(functions, params):
            func = getattr(subdetect_operation, function)
            if img is not None:
                labels = func(labels0, img, **param)
            else:
                labels = func(labels0, **param)
        imsave(labels, output, pathl, dtype=np.int16)
        logger.info("\tframe {0} done.".format(holder.frame))