Skip to main content

Build Status Coverage Status Python 3 PyPI License: MIT

featureflow

featureflow is a python library that allows users to build feature extraction pipelines in a declarative way, and control how and where those features are persisted.

Usage

The following example will compute word frequency in individual text documents, and then over the entire corpus of documents, but featureflow isn’t limited to text data. It’s designed to work well with sequential/streaming data (e.g. audio or video) that is often processed iteratively, in small chunks.

You can see all the code in this example in one place here.

We can define a graph of processing nodes like this:

import featureflow as ff


@ff.simple_in_memory_settings
class Document(ff.BaseModel):
    """
    Define the processing graph needed to extract document-level features,
    whether, and how those features should be persisted.
    """
    raw = ff.ByteStreamFeature(
        ff.ByteStream,
        chunksize=128,
        store=True)

    checksum = ff.JSONFeature(
        CheckSum,
        needs=raw,
        store=True)

    tokens = ff.Feature(
        Tokenizer,
        needs=raw,
        store=False)

    counts = ff.JSONFeature(
        WordCount,
        needs=tokens,
        store=True)

We can define the individual processing “nodes” referenced in the graph above like this:

import featureflow as ff
from collections import Counter
import re
import hashlib

class Tokenizer(ff.Node):
    """
    Tokenize a stream of text into individual, normalized (lowercase)
    words/tokens
    """
    def __init__(self, needs=None):
        super(Tokenizer, self).__init__(needs=needs)
        self._cache = ''
        self._pattern = re.compile('(?P<word>[a-zA-Z]+)\W+')

    def _enqueue(self, data, pusher):
        self._cache += data.decode()

    def _dequeue(self):
        matches = list(self._pattern.finditer(self._cache))
        if not matches:
            raise ff.NotEnoughData()
        last_boundary = matches[-1].end()
        self._cache = self._cache[last_boundary:]
        return matches

    def _process(self, data):
        yield map(lambda x: x.groupdict()['word'].lower(), data)


class WordCount(ff.Aggregator, ff.Node):
    """
    Keep track of token frequency
    """
    def __init__(self, needs=None):
        super(WordCount, self).__init__(needs=needs)
        self._cache = Counter()

    def _enqueue(self, data, pusher):
        self._cache.update(data)


class CheckSum(ff.Aggregator, ff.Node):
    """
    Compute the checksum of a text stream
    """
    def __init__(self, needs=None):
        super(CheckSum, self).__init__(needs=needs)
        self._cache = hashlib.sha256()

    def _enqueue(self, data, pusher):
        self._cache.update(data)

    def _process(self, data):
        yield data.hexdigest()

We can also define a graph that will process an entire corpus of stored document features:

import featureflow as ff

@ff.simple_in_memory_settings
class Corpus(ff.BaseModel):
    """
    Define the processing graph needed to extract corpus-level features,
    whether, and how those features should be persisted.
    """
    docs = ff.Feature(
        lambda doc_cls: (doc.counts for doc in doc_cls),
        store=False)

    total_counts = ff.JSONFeature(
        WordCount,
        needs=docs,
        store=True)

Finally, we can execute these processing graphs and access the stored features like this:

from __future__ import print_function
import argparse

def process_urls(urls):
    for url in urls:
        Document.process(raw=url)


def summarize_document(doc):
    return 'doc {_id} with checksum {cs} contains "the" {n} times'.format(
            _id=doc._id,
            cs=doc.checksum,
            n=doc.counts.get('the', 0))


def process_corpus(document_cls):
    corpus_id = Corpus.process(docs=document_cls)
    return Corpus(corpus_id)


def summarize_corpus(corpus):
    return 'The entire text corpus contains "the" {n} times'.format(
        n=corpus.total_counts.get("the", 0))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--url',
        help='specify one or more urls of text files to ingest',
        required=True,
        action='append')
    args = parser.parse_args()

    process_urls(args.url)

    for doc in Document:
        print(summarize_document(doc))

    corpus = process_corpus(Document)
    print(summarize_corpus(corpus))

To see this in action we can:

python wordcount.py \
    --url http://textfiles.com/food/1st_aid.txt \
    --url http://textfiles.com/food/antibiot.txt \
    ...

Installation

Python headers are required. You can install by running:

apt-get install python-dev

Numpy is optional. If you’d like to use it, the Anaconda distribution is highly recommended.

Finally, just

pip install featureflow

Metadata

Release files for featureflow 3.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for featureflow 3.0.3
File Size Uploaded Provenance Actions
featureflow-3.0.3.tar.gz 36.0 kB Without provenance Details for featureflow-3.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for featureflow 3.0.3
File Interpreter ABI Platform Actions
featureflow-3.0.3-py3.7.egg Legacy Egg format - - Details for featureflow-3.0.3-py3.7.egg

Total release size: 158.1 kB

Release files / featureflow-3.0.3.tar.gz

Download URL featureflow-3.0.3.tar.gz
Size 36.0 kB
Tags Source
SHA-256 checksum
How to use checksums
23624903672b611bb30be622eb46e058e4b37c8e4983ad6686f98cc4666997e8
BLAKE2b-256 checksum
How to use checksums
5c867f0e83f59b92666dea3065eb8998df85ba00122e4554c353ee821fa67d64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / featureflow-3.0.3-py3.7.egg

Download URL featureflow-3.0.3-py3.7.egg
Size 122.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
c7673e14320850e4ef5bb06e641bf22a732d66bf0c9eddded0c899fffc54b1c5
BLAKE2b-256 checksum
How to use checksums
666702d4cb857106345315cbb180730a4287459f4bfd68e3381e077ba2e7deb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Security for featureflow 3.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.0.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page